Skip to content

Fix portable build of libmodbus (upstream) #793

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
650d783
configure.ac: report if we would deliver static and/or shared library…
jimklimov Nov 12, 2024
446c2c3
configure.ac: pretty up AC_MSG_RESULT() for current PACKAGE $VERSION …
jimklimov Jan 14, 2025
68791f5
configure.ac: allow to --enable-Werror in test builds
jimklimov Jan 18, 2025
08cb12f
src/modbus-tcp.c: fix setsockopt() argument type for WIN32 cross-buil…
jimklimov Jan 18, 2025
63c6f2d
configure.ac, src/modbus-tcp.c: import fallback inet_pton() and inet_…
jimklimov Jan 18, 2025
c7dfb95
configure.ac: fix detection of netinet/ip.h on platforms where it req…
jimklimov Jan 18, 2025
024d58b
README.md: clarify possible need for GNU make
jimklimov Jan 18, 2025
ca8b499
GitIgnore src/stamp-h3
jimklimov Jan 18, 2025
9fa858e
src/modbus.c: _modbus_receive_msg(): printf("\n") to flush after angl…
jimklimov Jan 18, 2025
4d0a737
src/modbus.c: _modbus_receive_msg(): if "ctx->backend->select" failed…
jimklimov Jan 18, 2025
360c0ab
src/modbus.c: response_exception(): fflush() before and after vfprint…
jimklimov Jan 18, 2025
cbf51f3
src/modbus-tcp.c: modbus_tcp_listen(): clarify failed TCP listener si…
jimklimov Jan 18, 2025
8641e87
configure.ac: auto-adjust _PKG_VER_SEPARATOR length to _PKG_VER_TITLE…
jimklimov Jan 26, 2025
20d90a3
configure.ac: fix order of AM_PROG_CC_C_O vs. AM_PROG_CC
jimklimov Jan 26, 2025
73afcf8
configure.ac: stub AM_SILENT_RULES on platforms that lack it
jimklimov Jan 26, 2025
225ae04
configure.ac: use a fallback _PKG_VER_SEPARATOR if sed fails
jimklimov Jan 27, 2025
4785f5f
Makefile.am: avoid AM_MAKEFLAGS values that confuse non-GNU make impl…
jimklimov Jan 27, 2025
1126701
configure.ac: stub AM_SILENT_RULES on platforms that lack it - and re…
jimklimov Jan 27, 2025
da4a1f0
README.md: document dependency of "make distcheck" on PAX-capable TAR…
jimklimov Jan 28, 2025
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 @@ -34,6 +34,7 @@ Makefile.in
/stamp-h1
src/modbus-version.h
src/win32/modbus.dll.manifest
src/stamp-h3
tests/unit-test.h

# mkdocs
Expand Down
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CLEANFILES =
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
AM_MAKEFLAGS = --no-print-directory

# GNU make-ism, precludes builds with BSD and Sun Makes at least
#AM_MAKEFLAGS = --no-print-directory

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libmodbus.pc
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ 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 may also require GNU `tar`
impementation (or some other archivation tool, possibly `cpio`, that
would be recognized by your installed version of `automake` as supporting
the "pax" archive format) to pass the optional `make distcheck` routine.

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
150 changes: 143 additions & 7 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ 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])
AC_PROG_CPP
AC_PROG_CC
AM_PROG_CC_C_O
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
AC_CONFIG_MACRO_DIR([m4])
AM_SILENT_RULES([yes])
dnl This feature seems to require automake-1.13 or newer (1.11+ by other info)
dnl On very old systems can comment it away with little loss (then automake-1.10
dnl is known to suffice):
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])],
[AC_MSG_NOTICE([Silent Rules feature not defined in this automake version, skipped])])

LIBMODBUS_VERSION_MAJOR=libmodbus_version_major
LIBMODBUS_VERSION_MINOR=libmodbus_version_minor
Expand Down Expand Up @@ -89,7 +95,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 +106,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 +125,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 +145,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 +176,98 @@ 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"

# Build options
AC_ARG_ENABLE(tests,
Expand Down Expand Up @@ -186,10 +300,29 @@ 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])

_PKG_VER_TITLE="$PACKAGE $VERSION"
_PKG_VER_SEPARATOR="`echo "${_PKG_VER_TITLE}" | sed 's,.,=,g'`" \
&& test x"${_PKG_VER_SEPARATOR}" != x || _PKG_VER_SEPARATOR="================"
AC_OUTPUT
AC_MSG_RESULT([
$PACKAGE $VERSION
===============
${_PKG_VER_TITLE}
${_PKG_VER_SEPARATOR}

prefix: ${prefix}
sysconfdir: ${sysconfdir}
Expand All @@ -200,5 +333,8 @@ AC_MSG_RESULT([
cflags: ${CFLAGS} ${WARNING_CFLAGS}
ldflags: ${LDFLAGS}

build shared lib: ${enable_shared}
build static lib: ${enable_static}

tests: ${enable_tests}
])
Loading