Enable linux netlink event monitoring for Android OS platform services
[libusb.git] / configure.ac
blob6dc7c6986546ceba6c08e79414ad8dacce95034c
1 dnl These m4 macros are whitespace sensitive and break if moved around much.
2 m4_define([LU_VERSION_H], m4_include([libusb/version.h]))
3 m4_define([LU_DEFINE_VERSION_ATOM],
4         [m4_define([$1], m4_bregexp(LU_VERSION_H,
5         [^#define\s*$1\s*\([0-9]*\).*], [\1]))])
6 m4_define([LU_DEFINE_VERSION_RC_ATOM],
7         [m4_define([$1], m4_bregexp(LU_VERSION_H,
8         [^#define\s*$1\s*"\(-rc[0-9]*\)".*], [\1]))])
9 dnl The m4_bregexp() returns (only) the numbers following the #define named
10 dnl in the first macro parameter. m4_define() then defines the name for use
11 dnl in AC_INIT.
13 LU_DEFINE_VERSION_ATOM([LIBUSB_MAJOR])
14 LU_DEFINE_VERSION_ATOM([LIBUSB_MINOR])
15 LU_DEFINE_VERSION_ATOM([LIBUSB_MICRO])
16 LU_DEFINE_VERSION_RC_ATOM([LIBUSB_RC])
18 AC_PREREQ([2.69])
19 AC_INIT([libusb-1.0], [LIBUSB_MAJOR[.]LIBUSB_MINOR[.]LIBUSB_MICRO[]LIBUSB_RC], [libusb-devel@lists.sourceforge.net], [libusb-1.0], [https://libusb.info])
20 AC_CONFIG_HEADERS([config.h])
21 AC_CONFIG_SRCDIR([libusb/core.c])
22 AC_CONFIG_MACRO_DIR([m4])
23 AC_PROG_CC
24 AC_PROG_CXX
25 AC_C_INLINE
26 AM_INIT_AUTOMAKE
27 LT_INIT
28 LT_LANG([Windows Resource])
30 dnl Library versioning
31 dnl These numbers should be tweaked on every release. Read carefully:
32 dnl http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
33 dnl http://sourceware.org/autobook/autobook/autobook_91.html
34 lt_current=4
35 lt_revision=0
36 lt_age=4
37 LT_LDFLAGS="-version-info ${lt_current}:${lt_revision}:${lt_age} -no-undefined"
39 m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
41 EXTRA_CPPFLAGS=
42 EXTRA_CFLAGS=
44 dnl check for -std=gnu11 compiler support (optional)
45 dnl note that we don't just check if the compiler accepts '-std=x11'
46 dnl but also that it supports the _Thread_local keyword because some compilers
47 dnl (e.g. gcc 4.8) accept the command line option but do not implement TLS
48 saved_CFLAGS="${CFLAGS}"
49 CFLAGS="-std=gnu11"
50 AC_MSG_CHECKING([if $CC supports -std=gnu11])
51 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
52         [AC_MSG_RESULT([yes])
53          c_dialect=gnu],
54         [AC_MSG_RESULT([no])
55          c_dialect=])
56 if test "x$c_dialect" != xgnu; then
57         dnl fallback check for -std=c11 compiler support (required)
58         CFLAGS="-std=c11"
59         AC_MSG_CHECKING([if $CC supports -std=c11])
60         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
61                 [AC_MSG_RESULT([yes])],
62                 [AC_MSG_RESULT([no])
63                  AC_MSG_ERROR([compiler with C11 support is required to build libusb])])
64         c_dialect=c
66 CFLAGS="${saved_CFLAGS}"
68 AC_DEFINE([_GNU_SOURCE], [1], [Enable GNU extensions.])
69 AC_DEFINE([DEFAULT_VISIBILITY], [__attribute__ ((visibility ("default")))], [Define to the attribute for default visibility.])
70 AC_DEFINE([PRINTF_FORMAT(a, b)], [__attribute__ ((__format__ (__printf__, a, b)))], [Define to the attribute for enabling parameter checks on printf-like functions.])
72 create_import_lib=
73 is_android_linux=
74 AC_MSG_CHECKING([operating system])
75 case $host in
76 *-darwin*)
77         AC_MSG_RESULT([Darwin/Mac OS X])
78         backend=darwin
79         platform=posix
80         ;;
81 *-haiku*)
82         AC_MSG_RESULT([Haiku])
83         backend=haiku
84         platform=posix
85         ;;
86 wasm*-emscripten)
87         AC_MSG_RESULT([Emscripten])
88         backend=emscripten
89         platform=posix
90         ;;
91 wasm*-unknown-none)
92         AC_MSG_ERROR([
93 --host=$host_alias is not accepted as it might become ambiguous in the future.
94 Please use an explicit --host=$host_cpu-emscripten instead.
95         ])
96         ;;
97 *-linux* | *-uclinux*)
98         dnl on Android Linux, some functions are in different places
99         case $host in
100         *-linux-android*)
101                 AC_MSG_RESULT([Android Linux])
102                 is_android_linux=yes
103                 ;;
104         *)
105                 AC_MSG_RESULT([Linux])
106                 ;;
107         esac
108         backend=linux
109         platform=posix
110         ;;
111 *-netbsd*)
112         AC_MSG_RESULT([NetBSD])
113         backend=netbsd
114         platform=posix
115         ;;
116 *-openbsd*)
117         AC_MSG_RESULT([OpenBSD])
118         backend=openbsd
119         platform=posix
120         ;;
121 *-solaris*)
122         AC_MSG_RESULT([SunOS])
123         backend=sunos
124         platform=posix
125         ;;
126 *-cygwin*)
127         AC_MSG_RESULT([Windows (using Cygwin)])
128         backend=windows
129         platform=windows
130         EXTRA_CFLAGS="-mwin32"
131         ;;
132 *-mingw* | *msys*)
133         AC_MSG_RESULT([Windows])
134         backend=windows
135         platform=windows
136         test "x$enable_shared" = xyes && create_import_lib=yes
137         EXTRA_CFLAGS="-fno-omit-frame-pointer"
138         EXTRA_LDFLAGS="-static-libgcc"
139         ;;
141         AC_MSG_RESULT([Null])
142         AC_MSG_WARN([The host being compiled for is not supported.])
143         AC_MSG_WARN([The library may compile but will not function in any useful manner.])
144         backend=null
145         platform=posix
146         ;;
147 esac
149 if test "x$platform" = xposix; then
150         AC_DEFINE([PLATFORM_POSIX], [1], [Define to 1 if compiling for a POSIX platform.])
151         AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
152         if test "x$backend" != xemscripten; then
153                 # pipe2 is detected as present on Emscripten, but it isn't actually ported and always
154                 # returns an error. https://github.com/emscripten-core/emscripten/issues/14824
155                 AC_CHECK_FUNCS([pipe2])
156         fi
157         dnl Some compilers do not support the '-pthread' option so check for it here
158         saved_CFLAGS="${CFLAGS}"
159         CFLAGS="-Wall -Werror -pthread"
160         AC_MSG_CHECKING([if $CC recognizes -pthread])
161         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
162                 [AC_MSG_RESULT([yes])
163                  AC_SUBST(THREAD_CFLAGS, [-pthread])],
164                 [AC_MSG_RESULT([no])])
165         CFLAGS="${saved_CFLAGS}"
166         dnl Android Linux and Darwin provide pthread functions directly in libc
167         dnl glibc also provides some pthread functions directly, so search for a thread-specific function
168         AC_SEARCH_LIBS([pthread_create], [pthread],
169                 [test "x$ac_cv_search_pthread_create" != "xnone required" && AC_SUBST(THREAD_LIBS, [-lpthread])],
170                 [], [])
171         dnl Check for new-style atomic builtins. We first check without linking to -latomic.
172         AC_MSG_CHECKING(whether __atomic_load_n is supported)
173         AC_LINK_IFELSE([AC_LANG_SOURCE([[
174         #include <stdint.h>
175         int main() {
176                 struct {
177                         uint64_t *v;
178                 } x;
179                 return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
180                        (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
181         }]])], GCC_ATOMIC_BUILTINS_SUPPORTED=yes, GCC_ATOMIC_BUILTINS_SUPPORTED=no)
182         AC_MSG_RESULT($GCC_ATOMIC_BUILTINS_SUPPORTED)
183         if test "x$GCC_ATOMIC_BUILTINS_SUPPORTED" != xyes; then
184                 AC_SEARCH_LIBS([__atomic_fetch_add_4], [atomic])
185         fi
186 elif test "x$platform" = xwindows; then
187         AC_DEFINE([PLATFORM_WINDOWS], [1], [Define to 1 if compiling for a Windows platform.])
188 else
189         AC_MSG_ERROR([Unknown platform])
192 case $backend in
193 darwin)
194         AC_CHECK_FUNCS([pthread_threadid_np])
195         LIBS="${LIBS} -lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation -Wl,-framework,Security"
196         AC_CHECK_HEADERS([IOKit/usb/IOUSBHostFamilyDefinitions.h])
197         ;;
198 haiku)
199         LIBS="${LIBS} -lbe"
200         ;;
201 linux)
202         AC_SEARCH_LIBS([clock_gettime], [rt], [], [], [])
203         AC_CHECK_FUNCS([pthread_setname_np])
204         AC_ARG_ENABLE([udev],
205                 [AS_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])],
206                 [use_udev=$enableval], [use_udev=yes])
207         if test "x$use_udev" = xyes; then
208                 dnl system has udev. use it or fail!
209                 AC_CHECK_HEADER([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
210                 AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
212                 # We can build umockdev tests (if available)
213                 m4_ifdef([PKG_PROG_PKG_CONFIG],[
214                         PKG_PROG_PKG_CONFIG
215                         PKG_CHECK_MODULES([UMOCKDEV], [umockdev-1.0 >= 0.16.0], [ac_have_umockdev=yes], [ac_have_umockdev=no])
216                         PKG_CHECK_MODULES([UMOCKDEV_HOTPLUG], [umockdev-1.0 >= 0.17.7], [ac_umockdev_hotplug=yes], [ac_umockdev_hotplug=no])
217                         if test $ac_umockdev_hotplug = yes; then
218                            AC_DEFINE([UMOCKDEV_HOTPLUG], [1], [UMockdev hotplug code is not racy])
219                         fi
220                 ], [])
221         else
222                 AC_CHECK_HEADERS([asm/types.h])
223                 AC_CHECK_HEADER([linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink header not found])])
224                 AC_CHECK_HEADER([sys/socket.h], [], [AC_MSG_ERROR([Linux socket header not found])])
225         fi
226         ;;
227 sunos)
228         LIBS="${LIBS} -ldevinfo"
229         ;;
230 windows)
231         AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
232         AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
233         LT_LDFLAGS="${LT_LDFLAGS} -avoid-version"
234         ;;
235 emscripten)
236         # Note: LT_LDFLAGS is not enough here because we need link flags for executable.
237         EM_LDFLAGS="--bind -s ASYNCIFY"
238         AM_LDFLAGS="${AM_LDFLAGS} ${EM_LDFLAGS} -s ASSERTIONS -s ALLOW_MEMORY_GROWTH"
239         LIBS="${LIBS} ${EM_LDFLAGS}"
240         ;;
242         dnl no special handling required
243         ;;
244 esac
246 dnl headers not available on all platforms but required on others
247 AC_CHECK_HEADERS([sys/time.h])
249 dnl check availability of clock_gettime(), except don't bother on Darwin, because the result is not used.
250 if test "x$platform" = xposix && test "x$backend" != xdarwin; then
251         AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [AC_MSG_ERROR([clock_gettime() is required on this platform])])
253         if test "x$have_clock_gettime" = xyes; then
254                 dnl the clock_gettime() function needs certain clock IDs defined
255                 AC_CHECK_DECL([CLOCK_MONOTONIC], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_MONOTONIC])], [[#include <time.h>]])
256                 dnl use the monotonic clock for condition variable timed waits if possible
257                 AC_CHECK_FUNCS([pthread_condattr_setclock], [need_clock_realtime=], [need_clock_realtime=yes])
258                 if test "x$need_clock_realtime" = xyes; then
259                         AC_CHECK_DECL([CLOCK_REALTIME], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_REALTIME])], [[#include <time.h>]])
260                 fi
261         fi
264 dnl eventfd support
265 if test "x$backend" = xlinux || test "x$backend" = xsunos; then
266         AC_ARG_ENABLE([eventfd],
267                 [AS_HELP_STRING([--enable-eventfd], [use eventfd for signalling [default=auto]])],
268                 [use_eventfd=$enableval],
269                 [use_eventfd=auto])
270         if test "x$use_eventfd" != xno; then
271                 AC_CHECK_HEADER([sys/eventfd.h], [eventfd_h=yes], [eventfd_h=])
272                 if test "x$eventfd_h" = xyes; then
273                         AC_CHECK_DECLS([EFD_NONBLOCK, EFD_CLOEXEC], [eventfd_h_ok=yes], [eventfd_h_ok=], [[#include <sys/eventfd.h>]])
274                         if test "x$eventfd_h_ok" = xyes; then
275                                 AC_CHECK_FUNC([eventfd], [eventfd_ok=yes], [eventfd_ok=])
276                                 if test "x$eventfd_ok" = xyes; then
277                                         AC_DEFINE([HAVE_EVENTFD], [1], [Define to 1 if the system has eventfd functionality.])
278                                 elif test "x$use_eventfd" = xyes; then
279                                         AC_MSG_ERROR([eventfd() function not found; glibc 2.9+ required])
280                                 fi
281                         elif test "x$use_eventfd" = xyes; then
282                                 AC_MSG_ERROR([eventfd header not usable; glibc 2.9+ required])
283                         fi
284                 elif test "x$use_eventfd" = xyes; then
285                         AC_MSG_ERROR([eventfd header not available; glibc 2.9+ required])
286                 fi
287         fi
288         AC_MSG_CHECKING([whether to use eventfd for signalling])
289         if test "x$use_eventfd" = xno; then
290                 AC_MSG_RESULT([no (disabled by user)])
291         elif test "x$eventfd_h" != xyes; then
292                 AC_MSG_RESULT([no (header not available)])
293         elif test "x$eventfd_h_ok" != xyes; then
294                 AC_MSG_RESULT([no (header not usable)])
295         elif test "x$eventfd_ok" != xyes; then
296                 AC_MSG_RESULT([no (functions not available)])
297         else
298                 AC_MSG_RESULT([yes])
299         fi
302 dnl timerfd support
303 if test "x$backend" = xlinux || test "x$backend" = xsunos; then
304         AC_ARG_ENABLE([timerfd],
305                 [AS_HELP_STRING([--enable-timerfd], [use timerfd for timing [default=auto]])],
306                 [use_timerfd=$enableval],
307                 [use_timerfd=auto])
308         if test "x$use_timerfd" != xno; then
309                 AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=yes], [timerfd_h=])
310                 if test "x$timerfd_h" = xyes; then
311                         AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [timerfd_h_ok=yes], [timerfd_h_ok=], [[#include <sys/timerfd.h>]])
312                         if test "x$timerfd_h_ok" = xyes; then
313                                 AC_CHECK_FUNC([timerfd_create], [timerfd_ok=yes], [timerfd_ok=])
314                                 if test "x$timerfd_ok" = xyes; then
315                                         AC_DEFINE([HAVE_TIMERFD], [1], [Define to 1 if the system has timerfd functionality.])
316                                 elif test "x$use_timerfd" = xyes; then
317                                         AC_MSG_ERROR([timerfd_create() function not found; glibc 2.9+ required])
318                                 fi
319                         elif test "x$use_timerfd" = xyes; then
320                                 AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
321                         fi
322                 elif test "x$use_timerfd" = xyes; then
323                         AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
324                 fi
325         fi
326         AC_MSG_CHECKING([whether to use timerfd for timing])
327         if test "x$use_timerfd" = xno; then
328                 AC_MSG_RESULT([no (disabled by user)])
329         elif test "x$timerfd_h" != xyes; then
330                 AC_MSG_RESULT([no (header not available)])
331         elif test "x$timerfd_h_ok" != xyes; then
332                 AC_MSG_RESULT([no (header not usable)])
333         elif test "x$timerfd_ok" != xyes; then
334                 AC_MSG_RESULT([no (functions not available)])
335         else
336                 AC_MSG_RESULT([yes])
337         fi
340 dnl Message logging
341 AC_ARG_ENABLE([log],
342         [AS_HELP_STRING([--disable-log], [disable all logging])],
343         [log_enabled=$enableval],
344         [log_enabled=yes])
345 if test "x$log_enabled" != xno; then
346         AC_DEFINE([ENABLE_LOGGING], [1], [Define to 1 to enable message logging.])
349 AC_ARG_ENABLE([debug-log],
350         [AS_HELP_STRING([--enable-debug-log], [start with debug message logging enabled [default=no]])],
351         [debug_log_enabled=$enableval],
352         [debug_log_enabled=no])
353 if test "x$debug_log_enabled" != xno; then
354         AC_DEFINE([ENABLE_DEBUG_LOGGING], [1], [Define to 1 to start with debug message logging enabled.])
357 AC_ARG_ENABLE([system-log],
358         [AS_HELP_STRING([--enable-system-log], [output logging messages to the systemwide log, if supported by the OS [default=no]])],
359         [system_log_enabled=$enableval],
360         [system_log_enabled=no])
361 if test "x$system_log_enabled" != xno; then
362         AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], [1], [Define to 1 to output logging messages to the systemwide log.])
363         if test "x$backend" != xwindows && test "x$is_android_linux" != xyes; then
364                 dnl Check if syslog is available in standard C library
365                 AC_CHECK_HEADER([syslog.h], [syslog_h=yes], [syslog_h=])
366                 if test "x$syslog_h" = xyes; then
367                         AC_CHECK_FUNCS([syslog])
368                 fi
369         fi
372 dnl Examples build
373 AC_ARG_ENABLE([examples-build],
374         [AS_HELP_STRING([--enable-examples-build], [build example applications [default=no]])],
375         [build_examples=$enableval],
376         [build_examples=no])
378 dnl Tests build
379 AC_ARG_ENABLE([tests-build],
380         [AS_HELP_STRING([--enable-tests-build], [build test applications [default=no]])],
381         [build_tests=$enableval],
382         [build_tests=no])
384 AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
385 AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
386 AM_CONDITIONAL([BUILD_UMOCKDEV_TEST], [test "x$ac_have_umockdev" = xyes -a "x$log_enabled" != xno -a "x$debug_log_enabled" != xyes])
387 AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
388 AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
389 AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
390 AM_CONDITIONAL([OS_LINUX], [test "x$backend" = xlinux])
391 AM_CONDITIONAL([OS_NETBSD], [test "x$backend" = xnetbsd])
392 AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
393 AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
394 AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
395 AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
396 AM_CONDITIONAL([OS_EMSCRIPTEN], [test "x$backend" = xemscripten])
397 AM_CONDITIONAL([PLATFORM_POSIX], [test "x$platform" = xposix])
398 AM_CONDITIONAL([PLATFORM_WINDOWS], [test "x$platform" = xwindows])
399 AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
401 dnl The -Wcast-function-type warning causes a flurry of warnings when compiling
402 dnl Windows with GCC 8 or later because of dynamically loaded functions
403 if test "x$backend" = xwindows; then
404         saved_CFLAGS="${CFLAGS}"
405         CFLAGS="-Werror -Wcast-function-type"
406         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
407                 [EXTRA_CFLAGS="${EXTRA_CFLAGS} -Wno-cast-function-type"],
408                 [])
409         CFLAGS="${saved_CFLAGS}"
412 dnl Some linkers do not support the '--add-stdcall-alias' option so check for it here
413 if test "x$backend" = xwindows; then
414         saved_CFLAGS="${CFLAGS}"
415         CFLAGS="-Wl,--add-stdcall-alias"
416         AC_MSG_CHECKING([if linker supports --add-stdcall-alias])
417         AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])],
418                 [AC_MSG_RESULT([yes])
419                  LT_LDFLAGS="${LT_LDFLAGS} -Wl,--add-stdcall-alias"],
420                 [AC_MSG_RESULT([no])])
421         CFLAGS="${saved_CFLAGS}"
424 SHARED_CFLAGS="-Wall -Wextra -Wshadow -Wunused -Wwrite-strings -Werror=format-security -Werror=implicit-function-declaration -Werror=implicit-int -Werror=init-self -Werror=missing-prototypes -Werror=strict-prototypes -Werror=undef -Werror=uninitialized"
426 AM_CPPFLAGS="${EXTRA_CPPFLAGS}"
427 AC_SUBST(AM_CPPFLAGS)
429 AM_CFLAGS="-std=${c_dialect}11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS}"
430 AC_SUBST(AM_CFLAGS)
432 AM_CXXFLAGS="-std=${c_dialect}++11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS} -Wmissing-declarations"
433 AC_SUBST(AM_CXXFLAGS)
435 AC_SUBST(LT_LDFLAGS)
436 AC_SUBST(AM_LDFLAGS)
438 AC_SUBST([EXTRA_LDFLAGS])
440 dnl set name of html output directory for doxygen
441 AC_SUBST(DOXYGEN_HTMLDIR, [api-1.0])
443 AC_CONFIG_FILES([libusb-1.0.pc])
444 AC_CONFIG_FILES([Makefile])
445 AC_CONFIG_FILES([libusb/Makefile])
446 AC_CONFIG_FILES([examples/Makefile])
447 AC_CONFIG_FILES([tests/Makefile])
448 AC_CONFIG_FILES([doc/Makefile])
449 AC_CONFIG_FILES([doc/doxygen.cfg])
450 AC_OUTPUT