Skip to content

Fix mingw build (only) #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Makefile.in
src/modbus-config.h
src/modbus-version.h
src/win32/modbus.dll.manifest
src/stamp-h3
tests/unit-test.h

# mkdocs
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ You will only need to install automake, autoconf, libtool and a C compiler (gcc
or clang) to compile the library and asciidoc and xmlto to generate the
documentation (optional).

To install, just run the usual dance, `./configure && make install`. Run
`./autogen.sh` first to generate the `configure` script if required.
To install, just run the usual dance, `./configure && make install`.
Run `./autogen.sh` first to generate the `configure` script if required.
You may be required to use `gmake` on platforms where default `make` is
different (BSD make, Sun make, etc.)

You can change installation directory with prefix option, eg. `./configure
--prefix=/usr/local/`. You have to check that the installation library path is
Expand Down
135 changes: 131 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ AC_INIT([libmodbus],
AC_CONFIG_SRCDIR([src/modbus.c])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([check-news foreign 1.11 silent-rules tar-pax subdir-objects])
AM_PROG_CC_C_O
AC_PROG_CPP
AC_PROG_CC
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
Expand Down Expand Up @@ -89,7 +91,6 @@ AC_CHECK_HEADERS([ \
linux/serial.h \
netdb.h \
netinet/in.h \
netinet/ip.h \
netinet/tcp.h \
sys/ioctl.h \
sys/params.h \
Expand All @@ -101,6 +102,17 @@ AC_CHECK_HEADERS([ \
unistd.h \
])

dnl On some platforms like FreeBSD and OpenIndiana (illumos) the
dnl netinet/ip.h requires netinet/in.h explicitly included first:
AC_CHECK_HEADERS([ \
netinet/ip.h \
], [], [], [
AC_INCLUDES_DEFAULT
#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
])

# Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's necessary to
# workaround that problem and Cygwin doesn't define MSG_DONTWAIT.
AC_CHECK_DECLS([__CYGWIN__])
Expand All @@ -109,7 +121,7 @@ AC_CHECK_DECLS([__CYGWIN__])
AC_SEARCH_LIBS(accept, network socket)

# Checks for library functions.
AC_CHECK_FUNCS([accept4 gai_strerror getaddrinfo gettimeofday inet_pton inet_ntop select socket strerror strlcpy])
AC_CHECK_FUNCS([accept4 gai_strerror getaddrinfo gettimeofday select socket strerror strlcpy])

# Required for MinGW with GCC v4.8.1 on Win7
AC_DEFINE(WINVER, 0x0501, _)
Expand All @@ -129,12 +141,19 @@ AC_TYPE_UINT32_T
AC_TYPE_UINT8_T

if test "$os_cygwin" = "false"; then
AC_CHECK_HEADERS([windows.h], HAVE_WINDOWS_H=yes)

# Required for getaddrinfo (TCP IP - IPv6)
AC_CHECK_HEADERS([winsock2.h], HAVE_WINSOCK2_H=yes)
if test "x$HAVE_WINSOCK2_H" = "xyes"; then
LIBS="$LIBS -lws2_32"
AC_SUBST(LIBS)
AC_SUBST(LIBS)
fi

dnl Can bring inet_ntop()/inet_pton()... or not, depending on distro
dnl (e.g. mingw "native" with MSYS2 or cross-built from Linux); that
dnl is further checked below:
AC_CHECK_HEADERS([ws2tcpip.h], HAVE_WS2TCPIP_H=yes)
fi

if test "$os_sunos" = "true"; then
Expand All @@ -153,7 +172,99 @@ WARNING_CFLAGS="-Wall \
-Wsign-compare -Wchar-subscripts \
-Wstrict-prototypes -Wshadow \
-Wformat-security"
AC_SUBST([WARNING_CFLAGS])

dnl FIXME: define more thoroughly if C++ code ever appears here
WARNING_CXXFLAGS="$WARNING_CFLAGS"

dnl Adapted from NUT v2.8.2 configure.ac :
myCFLAGS="$CFLAGS"
AS_IF([test "${GCC}" = "yes"],
[CFLAGS="$myCFLAGS -Werror -Werror=implicit-function-declaration"],
[dnl # Don't know what to complain about for unknown compilers
dnl # FIXME: We presume here they have at least a "-Werror" option
CFLAGS="$myCFLAGS -Werror"
])

AC_CACHE_CHECK([for inet_ntop() with IPv4 and IPv6 support],
[ac_cv_func_inet_ntop],
[AC_LANG_PUSH([C])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
#if HAVE_WINDOWS_H
# undef inline
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# if HAVE_WINSOCK2_H
# include <winsock2.h>
# endif
# if HAVE_WS2TCPIP_H
# include <ws2tcpip.h>
# endif
#else
# include <arpa/inet.h>
#endif
#include <stdio.h>
]],
[[/* const char* inet_ntop(int af, const void* src, char* dst, size_t cnt); */
char buf[128];
printf("%s", inet_ntop(AF_INET, "1.2.3.4", buf, 10));
printf("%s", inet_ntop(AF_INET6, "::1", buf, 10))
/* autoconf adds ";return 0;" */
]])],
[ac_cv_func_inet_ntop=yes], [ac_cv_func_inet_ntop=no]
)
AC_LANG_POP([C])
])
AS_IF([test x"${ac_cv_func_inet_ntop}" = xyes],
[AC_DEFINE([HAVE_INET_NTOP], 1, [defined if system has the inet_ntop() method])],
[AC_MSG_WARN([Required C library routine inet_ntop() not found])
AS_IF([test "${os_win32}" = "true"], [AC_MSG_WARN([Windows antivirus might block this test])])
]
)

AC_CACHE_CHECK([for inet_pton() with IPv4 and IPv6 support],
[ac_cv_func_inet_pton],
[AC_LANG_PUSH([C])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
#if HAVE_WINDOWS_H
# undef inline
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# if HAVE_WINSOCK2_H
# include <winsock2.h>
# endif
# if HAVE_WS2TCPIP_H
# include <ws2tcpip.h>
# endif
#else
# include <arpa/inet.h>
#endif
#include <stdio.h>
]],
[[/* int inet_pton(int af, const char *src, char *dst); */
struct in_addr ipv4;
struct in6_addr ipv6;
printf("%i ", inet_pton(AF_INET, "1.2.3.4", &ipv4));
printf("%i ", inet_pton(AF_INET6, "::1", &ipv6))
/* autoconf adds ";return 0;" */
]])],
[ac_cv_func_inet_pton=yes], [ac_cv_func_inet_pton=no]
)
AC_LANG_POP([C])
])
AS_IF([test x"${ac_cv_func_inet_pton}" = xyes],
[AC_DEFINE([HAVE_INET_PTON], 1, [defined if system has the inet_pton() method])],
[AC_MSG_WARN([Required C library routine inet_pton() not found])
AS_IF([test "${os_win32}" = "true"], [AC_MSG_WARN([Windows antivirus might block this test])])
]
)
CFLAGS="$myCFLAGS"


# Check for libusb-1.0
AC_ARG_WITH([libusb],
Expand Down Expand Up @@ -221,6 +332,22 @@ AS_IF([test "x$enable_debug" = "xyes"], [
CXXFLAGS="-O2"
])

dnl NOTE: Do not pass these among C(XX)FLAGS to the configure script itself,
dnl they can break common tests unexpectedly. Variants below should work for
dnl GCC and CLANG, and other compilers that emulate them in terms of CLI API.
AC_ARG_ENABLE([Werror],
[AS_HELP_STRING([--enable-Werror],
[Enable compilation failure on warnings (default is no)])],
[enable_Werror=$enableval],
[enable_Werror=no])
AS_IF([test "x$enable_Werror" = "xyes"], [
WARNING_CFLAGS="$WARNING_CFLAGS -Werror"
WARNING_CXXFLAGS="$WARNING_CXXFLAGS -Werror"
])

AC_SUBST([WARNING_CFLAGS])
AC_SUBST([WARNING_CXXFLAGS])

AC_OUTPUT
AC_MSG_RESULT([
$PACKAGE $VERSION
Expand Down
Loading