Skip to content
Open
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
22 changes: 22 additions & 0 deletions .github/workflows/meson-build.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
187 changes: 187 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -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 <sys/socket.h>')
# 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 <winsock2.h>\n#include <ws2tcpip.h>\n#include <string.h>'
else
func_prefix = '''#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>'''
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 <asm/termbits.h>')
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)
2 changes: 2 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
option('tests', type : 'boolean', value : true,
description : 'Build tests')
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 = *~
47 changes: 47 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
35 changes: 35 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -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)
Loading