forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnut_check_libmodbus.m4
187 lines (175 loc) · 6.55 KB
/
nut_check_libmodbus.m4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
dnl Check for LIBMODBUS compiler flags. On success, set nut_have_libmodbus="yes"
dnl and set LIBMODBUS_CFLAGS and LIBMODBUS_LIBS. On failure, set
dnl nut_have_libmodbus="no". This macro can be run multiple times, but will
dnl do the checking only once.
AC_DEFUN([NUT_CHECK_LIBMODBUS],
[
if test -z "${nut_have_libmodbus_seen}"; then
nut_have_libmodbus_seen=yes
dnl save CFLAGS and LIBS
CFLAGS_ORIG="${CFLAGS}"
LIBS_ORIG="${LIBS}"
NUT_CHECK_PKGCONFIG
AS_IF([test x"$have_PKG_CONFIG" = xyes],
[AC_MSG_CHECKING(for libmodbus version via pkg-config)
LIBMODBUS_VERSION="`$PKG_CONFIG --silence-errors --modversion libmodbus 2>/dev/null`"
if test "$?" != "0" -o -z "${LIBMODBUS_VERSION}"; then
LIBMODBUS_VERSION="none"
fi
AC_MSG_RESULT(${LIBMODBUS_VERSION} found)
],
[LIBMODBUS_VERSION="none"
AC_MSG_NOTICE([can not check libmodbus settings via pkg-config])
]
)
AS_IF([test x"$LIBMODBUS_VERSION" != xnone],
[CFLAGS="`$PKG_CONFIG --silence-errors --cflags libmodbus 2>/dev/null`"
LIBS="`$PKG_CONFIG --silence-errors --libs libmodbus 2>/dev/null`"
],
[CFLAGS="-I/usr/include/modbus"
LIBS="-lmodbus"
]
)
AC_MSG_CHECKING(for libmodbus cflags)
AC_ARG_WITH(modbus-includes,
AS_HELP_STRING([@<:@--with-modbus-includes=CFLAGS@:>@], [include flags for the libmodbus library]),
[
case "${withval}" in
yes|no)
AC_MSG_ERROR(invalid option --with(out)-modbus-includes - see docs/configure.txt)
;;
*)
CFLAGS="${withval}"
;;
esac
], [])
AC_MSG_RESULT([${CFLAGS}])
AC_MSG_CHECKING(for libmodbus ldflags)
AC_ARG_WITH(modbus-libs,
AS_HELP_STRING([@<:@--with-modbus-libs=LIBS@:>@], [linker flags for the libmodbus library]),
[
case "${withval}" in
yes|no)
AC_MSG_ERROR(invalid option --with(out)-modbus-libs - see docs/configure.txt)
;;
*)
LIBS="${withval}"
;;
esac
], [])
AC_MSG_RESULT([${LIBS}])
dnl check if libmodbus is usable
AC_CHECK_HEADERS(modbus.h, [nut_have_libmodbus=yes], [nut_have_libmodbus=no], [AC_INCLUDES_DEFAULT])
AC_CHECK_FUNCS(modbus_new_rtu, [], [nut_have_libmodbus=no])
AC_CHECK_FUNCS(modbus_new_tcp, [], [nut_have_libmodbus=no])
AC_CHECK_FUNCS(modbus_set_byte_timeout, [], [nut_have_libmodbus=no])
AC_CHECK_FUNCS(modbus_set_response_timeout, [], [nut_have_libmodbus=no])
dnl modbus_set_byte_timeout() and modbus_set_response_timeout()
dnl in 3.0.x and 3.1.x have different args (since ~2013): the
dnl older version used to accept timeout as a struct timeval
dnl instead of seconds and microseconds. Detect which we use?..
AS_IF([test x"$nut_have_libmodbus" = xyes],
[dnl Do not rely on versions if we can test actual API
AX_C_PRAGMAS
AC_LANG_PUSH([C])
AC_CACHE_CHECK([types of arguments for modbus_set_byte_timeout],
[nut_cv_func_modbus_set_byte_timeout_args],
[nut_cv_func_modbus_set_byte_timeout_args="unknown"
AC_COMPILE_IFELSE(
[dnl Try purely the old API (timeval)
AC_LANG_PROGRAM([
#include <time.h>
#include <modbus.h>
], [modbus_t *ctx; struct timeval to = (struct timeval){0};
modbus_set_byte_timeout(ctx, &to);])
], [nut_cv_func_modbus_set_byte_timeout_args="timeval"
dnl Try the old API in more detail: check
dnl if we can just assign uint32's for new
dnl code into timeval fields (exist numeric)?
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([
#include <time.h>
#include <stdint.h>
#include <modbus.h>
], [modbus_t *ctx; uint32_t to_sec = 10, to_usec = 50;
struct timeval to = (struct timeval){0};
/* TODO: Clarify and detect warning names and
* so pragmas for signed/unsigned assignment (e.g.
* for timeval definitions that have "long" fields)
*/
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SIGN_COMPARE
# pragma GCC diagnostic ignored "-Wsign-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SIGN_CONVERSION
# pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
to.tv_sec = to_sec;
to.tv_usec = to_usec;
modbus_set_byte_timeout(ctx, &to);
])
], [nut_cv_func_modbus_set_byte_timeout_args="timeval_numeric_fields"])
],
[dnl Try another API variant: new API with
dnl fields of struct timeval as numbers
dnl (checks they exist, and are compatible
dnl numeric types so compiler can convert)
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([
#include <time.h>
#include <stdint.h>
#include <modbus.h>
], [modbus_t *ctx; struct timeval to = (struct timeval){0};
/* TODO: Clarify and detect warning names and
* so pragmas for signed/unsigned assignment (e.g.
* for timeval definitions that have "long" fields)
*/
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SIGN_COMPARE
# pragma GCC diagnostic ignored "-Wsign-compare"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_SIGN_CONVERSION
# pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
uint32_t to_sec = to.tv_sec, to_usec = to.tv_usec;
modbus_set_byte_timeout(ctx, to_sec, to_usec);
])
], [nut_cv_func_modbus_set_byte_timeout_args="sec_usec_uint32_cast_timeval_fields"],
[dnl Try another API variant: new API purely (two uint32's)
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([
#include <stdint.h>
#include <modbus.h>
], [modbus_t *ctx; uint32_t to_sec = 0, to_usec = 0;
modbus_set_byte_timeout(ctx, to_sec, to_usec);])
], [nut_cv_func_modbus_set_byte_timeout_args="sec_usec_uint32"])
])
])
])
dnl NOTE: We could add similar tests to above for
dnl other time-related methods, but for now keep
dnl it simple -- and assume some same approach
dnl applies to the same generation of the library.
AC_LANG_POP([C])
AC_MSG_RESULT([Found types to use for modbus_set_byte_timeout: ${nut_cv_func_modbus_set_byte_timeout_args}])
dnl NOTE: code should check for having a token name defined e.g.:
dnl #ifdef NUT_MODBUS_TIMEOUT_ARG_sec_usec_uint32
dnl Alas, we can't pass variables as macro name to AC_DEFINE
COMMENT="Define to specify timeout method args approach for libmodbus"
AS_CASE(["${nut_cv_func_modbus_set_byte_timeout_args}"],
[timeval_numeric_fields], [AC_DEFINE([NUT_MODBUS_TIMEOUT_ARG_timeval_numeric_fields], 1, [${COMMENT}])],
[timeval], [AC_DEFINE([NUT_MODBUS_TIMEOUT_ARG_timeval], 1, [${COMMENT}])],
[sec_usec_uint32_cast_timeval_fields], [AC_DEFINE([NUT_MODBUS_TIMEOUT_ARG_sec_usec_uint32_cast_timeval_fields], 1, [${COMMENT}])],
[sec_usec_uint32], [AC_DEFINE([NUT_MODBUS_TIMEOUT_ARG_sec_usec_uint32], 1, [${COMMENT}])],
[dnl default
AC_MSG_WARN([Cannot find proper types to use for modbus_set_byte_timeout])
nut_have_libmodbus=no]
)
])
AS_IF([test x"${nut_have_libmodbus}" = x"yes"],
[LIBMODBUS_CFLAGS="${CFLAGS}"
LIBMODBUS_LIBS="${LIBS}"]
)
dnl restore original CFLAGS and LIBS
CFLAGS="${CFLAGS_ORIG}"
LIBS="${LIBS_ORIG}"
fi
])