From 4778961caef34e187d76441ac6beed184cb83451 Mon Sep 17 00:00:00 2001 From: Yegor Yefremov Date: Sun, 18 Oct 2026 00:00:50 +0200 Subject: [PATCH] Add experimental Meson build system Provide Meson support alongside the autotools build. Based on the work by Jon Ringle in https://github.com/stephane/libmodbus/pull/666, updated for the current tree: - derive the shared library version from the libtool current:revision:age numbers so the library file names match libtool (libmodbus.so.5.1.0) - port the TIOCSRS485, TIOCM_RTS, struct termios2 and __CYGWIN__ checks, plus the missing function/header checks - force-include config.h and define the system extension macros like AC_USE_SYSTEM_EXTENSIONS does - link the platform socket libraries (ws2_32, nsl/socket, network) - generate libmodbus.pc, build all test programs and run the unit and proxy tests serially (they use fixed TCP ports) - add a 'tests' option equivalent to --disable-tests - add a GitHub workflow building and testing with Meson The Meson files are also shipped in the autotools dist tarball. Co-authored-by: Jon Ringle Co-Authored-By: Claude Opus 5.5 --- .github/workflows/meson-build.yml | 22 ++++ Makefile.am | 2 +- README.md | 18 ++- configure.ac | 2 + meson.build | 187 ++++++++++++++++++++++++++++++ meson_options.txt | 2 + src/Makefile.am | 2 +- src/meson.build | 47 ++++++++ tests/Makefile.am | 2 +- tests/meson.build | 35 ++++++ 10 files changed, 315 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/meson-build.yml create mode 100644 meson.build create mode 100644 meson_options.txt create mode 100644 src/meson.build create mode 100644 tests/meson.build diff --git a/.github/workflows/meson-build.yml b/.github/workflows/meson-build.yml new file mode 100644 index 000000000..86c104b50 --- /dev/null +++ b/.github/workflows/meson-build.yml @@ -0,0 +1,22 @@ +name: Meson build libmodbus + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v6.0.1 + - name: install meson + run: sudo apt-get update && sudo apt-get install -y meson ninja-build + - name: meson setup + run: meson setup build + - name: meson compile + run: meson compile -C build + - name: meson test + run: meson test -C build --print-errorlogs diff --git a/Makefile.am b/Makefile.am index a80e70a69..39bab0c73 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,7 +4,7 @@ AM_MAKEFLAGS = --no-print-directory pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libmodbus.pc -EXTRA_DIST = libmodbus.pc.in +EXTRA_DIST = libmodbus.pc.in meson.build meson_options.txt CLEANFILES += libmodbus.pc dist_doc_DATA = AUTHORS NEWS.md README.md diff --git a/README.md b/README.md index 479da4c63..6c318d946 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,21 @@ date (run `ldconfig` as root if required). The library provides a _libmodbus.pc_ file to use with `pkg-config` to ease your program compilation and linking. +### Meson (experimental) + +The library can also be built with [Meson](https://mesonbuild.com) (0.56 or +later) as an experimental alternative to autotools: + +```shell +meson setup build +meson compile -C build +meson test -C build +meson install -C build +``` + +Use `-Dtests=false` to skip building the tests and `-Ddefault_library=static` +(or `both`) to build a static library. + If you want to compile with Microsoft Visual Studio, you should follow the instructions in `./src/win32/README.md`. @@ -66,7 +81,8 @@ For a quick test of libmodbus, you can run the following programs in two shells: By default, all TCP unit tests will be executed (see --help for options). -It's also possible to run the unit tests with `make check`. +It's also possible to run the unit tests with `make check` (or `meson test -C +build` with Meson). ## To report a bug or to contribute diff --git a/configure.ac b/configure.ac index d77167025..4e56c85d9 100644 --- a/configure.ac +++ b/configure.ac @@ -11,6 +11,8 @@ # Take care to update the libtool versioning when required (LIBMODBUS_LD_*). # http://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html # +# Keep the version and ABI numbers in meson.build in sync. +# m4_define([libmodbus_version_major], [3]) m4_define([libmodbus_version_minor], [2]) m4_define([libmodbus_version_micro], [0]) diff --git a/meson.build b/meson.build new file mode 100644 index 000000000..63d1f9505 --- /dev/null +++ b/meson.build @@ -0,0 +1,187 @@ +# Experimental Meson build, provided alongside the autotools one. +# +# Keep the version and ABI numbers below in sync with configure.ac. +project('libmodbus', 'c', + version : '3.2.0', + license : 'LGPL-2.1-or-later', + meson_version : '>= 0.56.0', + default_options : [ + 'buildtype=debugoptimized', + 'default_library=shared', + 'warning_level=1', + ]) + +libmodbus_version = meson.project_version() +ver_arr = libmodbus_version.split('.') + +ver_conf = configuration_data() +ver_conf.set('LIBMODBUS_VERSION', libmodbus_version) +ver_conf.set('LIBMODBUS_VERSION_MAJOR', ver_arr[0]) +ver_conf.set('LIBMODBUS_VERSION_MINOR', ver_arr[1]) +ver_conf.set('LIBMODBUS_VERSION_MICRO', ver_arr[2]) + +# ABI version (libtool -version-info current:revision:age) +# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html +libmodbus_ld_current = 6 +libmodbus_ld_revision = 0 +libmodbus_ld_age = 1 + +cc = meson.get_compiler('c') +host_system = host_machine.system() + +cdata = configuration_data() + +# Equivalent of AC_USE_SYSTEM_EXTENSIONS (needed for accept4 among others) +cdata.set('_GNU_SOURCE', 1) +cdata.set('_ALL_SOURCE', 1) +cdata.set('_POSIX_PTHREAD_SEMANTICS', 1) +cdata.set('_TANDEM_SOURCE', 1) +cdata.set('__EXTENSIONS__', 1) + +check_headers = [ + 'arpa/inet.h', + 'byteswap.h', + 'errno.h', + 'fcntl.h', + 'inttypes.h', + 'limits.h', + 'linux/serial.h', + 'netdb.h', + 'netinet/in.h', + 'netinet/ip.h', + 'netinet/tcp.h', + 'stdint.h', + 'sys/ioctl.h', + 'sys/params.h', + 'sys/socket.h', + 'sys/time.h', + 'sys/types.h', + 'termios.h', + 'time.h', + 'unistd.h', +] + +foreach h : check_headers + if cc.has_header(h) + cdata.set('HAVE_' + h.underscorify().to_upper(), 1) + endif +endforeach + +# 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. +cdata.set10('HAVE_DECL___CYGWIN__', cc.has_header_symbol('stddef.h', '__CYGWIN__')) + +# System libraries required for networking +libmodbus_deps = [] +if host_system == 'windows' + # Required for getaddrinfo (TCP IP - IPv6) + libmodbus_deps += cc.find_library('ws2_32') + # Required for MinGW with GCC v4.8.1 on Win7 + cdata.set('WINVER', '0x0501') +elif host_system == 'sunos' + libmodbus_deps += [cc.find_library('nsl'), cc.find_library('socket')] +elif not cc.has_function('accept', prefix : '#include ') + # libnetwork for Haiku, libsocket for QNX + foreach l : ['network', 'socket'] + dep = cc.find_library(l, required : false) + if dep.found() and cc.has_function('accept', dependencies : dep) + libmodbus_deps += dep + break + endif + endforeach +endif + +if host_system == 'windows' + cdata.set('HAVE_WINSOCK2_H', 1) + func_prefix = '#include \n#include \n#include ' +else + func_prefix = '''#include +#include +#include +#include +#include +#include +#include ''' +endif + +check_functions = [ + 'accept4', + 'gai_strerror', + 'getaddrinfo', + 'gettimeofday', + 'inet_pton', + 'inet_ntop', + 'select', + 'socket', + 'strerror', + 'strlcpy', +] + +foreach f : check_functions + if cc.has_function(f, prefix : '#define _GNU_SOURCE\n' + func_prefix, + dependencies : libmodbus_deps) + cdata.set('HAVE_' + f.to_upper(), 1) + endif +endforeach + +# Check for RS485 support (Linux kernel version 2.6.28+) +cdata.set10('HAVE_DECL_TIOCSRS485', cc.has_header_symbol('sys/ioctl.h', 'TIOCSRS485')) +# Check for RTS flags +cdata.set10('HAVE_DECL_TIOCM_RTS', cc.has_header_symbol('sys/ioctl.h', 'TIOCM_RTS')) +# Check for termios2 support +if cc.has_type('struct termios2', prefix : '#include ') + cdata.set('HAVE_STRUCT_TERMIOS2', 1) +endif + +config_h = configure_file(output : 'config.h', configuration : cdata) +root_inc = include_directories('.') + +# Like the autotools build, force-include config.h in every translation unit +# as some sources test HAVE_* macros before including it themselves. +if cc.get_argument_syntax() == 'msvc' + config_h_args = ['/FI' + meson.current_build_dir() / 'config.h'] +else + config_h_args = ['-include', meson.current_build_dir() / 'config.h'] +endif + +add_project_arguments(cc.get_supported_arguments([ + '-Wmissing-declarations', + '-Wmissing-prototypes', + '-Wnested-externs', + '-Wpointer-arith', + '-Wsign-compare', + '-Wchar-subscripts', + '-Wstrict-prototypes', + '-Wshadow', + '-Wformat-security', +]), language : 'c') + +subdir('src') + +if get_option('tests') + subdir('tests') +endif + +pkg = import('pkgconfig') +pkg.generate(libmodbus, + name : 'modbus', + filebase : 'libmodbus', + description : 'Modbus library', + subdirs : 'modbus') + +install_data('AUTHORS', 'NEWS.md', 'README.md', + install_dir : get_option('datadir') / 'doc' / meson.project_name()) + +summary({ + 'prefix' : get_option('prefix'), + 'sysconfdir' : get_option('sysconfdir'), + 'libdir' : get_option('libdir'), + 'includedir' : get_option('includedir'), +}, section : 'Directories') + +summary({ + 'compiler' : cc.get_id(), + 'buildtype' : get_option('buildtype'), + 'library' : get_option('default_library'), + 'tests' : get_option('tests'), +}, section : 'Configuration', bool_yn : true) diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 000000000..2a301c794 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,2 @@ +option('tests', type : 'boolean', value : true, + description : 'Build tests') diff --git a/src/Makefile.am b/src/Makefile.am index 157e9200c..91ed71a54 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -38,5 +38,5 @@ libmodbusincludedir = $(includedir)/modbus libmodbusinclude_HEADERS = modbus.h modbus-version.h modbus-rtu.h modbus-tcp.h DISTCLEANFILES = modbus-version.h -EXTRA_DIST += modbus-version.h.in +EXTRA_DIST += modbus-version.h.in meson.build CLEANFILES = *~ diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 000000000..d9bad09be --- /dev/null +++ b/src/meson.build @@ -0,0 +1,47 @@ +modbus_version_h = configure_file(input : 'modbus-version.h.in', + output : 'modbus-version.h', + configuration : ver_conf) + +if host_system == 'windows' + configure_file(input : 'win32/modbus.dll.manifest.in', + output : 'modbus.dll.manifest', + configuration : ver_conf) +endif + +sources = files( + 'modbus.c', + 'modbus-data.c', + 'modbus-rtu.c', + 'modbus-tcp.c', +) + +install_headers( + 'modbus.h', + modbus_version_h, + 'modbus-rtu.h', + 'modbus-tcp.h', + subdir : 'modbus') + +lib_c_args = config_h_args +if cc.get_argument_syntax() == 'msvc' + lib_c_args += ['-DDLLBUILD'] +endif + +# Map libtool's current:revision:age to the same file names libtool produces +lt_major = libmodbus_ld_current - libmodbus_ld_age +lt_version = '@0@.@1@.@2@'.format(lt_major, libmodbus_ld_age, libmodbus_ld_revision) +darwin_current = libmodbus_ld_current + 1 + +libmodbus = library('modbus', sources, + version : lt_version, + soversion : lt_major, + darwin_versions : [darwin_current, '@0@.@1@'.format(darwin_current, libmodbus_ld_revision)], + c_args : lib_c_args, + include_directories : root_inc, + dependencies : libmodbus_deps, + install : true) + +libmodbus_dep = declare_dependency(link_with : libmodbus, + include_directories : include_directories('.')) + +meson.override_dependency('libmodbus', libmodbus_dep) diff --git a/tests/Makefile.am b/tests/Makefile.am index 45d0f8e72..db865c365 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -EXTRA_DIST = README.md unit-tests.sh proxy-tests.sh LICENSE +EXTRA_DIST = README.md unit-tests.sh proxy-tests.sh LICENSE meson.build noinst_PROGRAMS = \ bandwidth-server-one \ diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 000000000..b3e32231d --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,35 @@ +configure_file(input : 'unit-test.h.in', + output : 'unit-test.h', + configuration : cdata) + +test_programs = [ + 'bandwidth-server-one', + 'bandwidth-server-many-up', + 'bandwidth-client', + 'random-test-server', + 'random-test-client', + 'unit-test-server', + 'unit-test-client', + 'proxy-test-server', + 'proxy-test-client', + 'version', +] + +foreach p : test_programs + executable(p, p + '.c', + c_args : config_h_args, + include_directories : root_inc, + dependencies : libmodbus_dep) +endforeach + +# The scripts start the programs from the current directory and the servers +# listen on fixed TCP ports, so the tests must not run in parallel. +test('unit-tests', find_program('unit-tests.sh'), + workdir : meson.current_build_dir(), + is_parallel : false, + timeout : 120) + +test('proxy-tests', find_program('proxy-tests.sh'), + workdir : meson.current_build_dir(), + is_parallel : false, + timeout : 120)