core: Suppress hotplug events during initial enumeration
[platform/upstream/libusb.git] / configure.ac
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.
12
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])
17
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], [http://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])
29
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=3
35 lt_revision=0
36 lt_age=3
37 LT_LDFLAGS="-version-info ${lt_current}:${lt_revision}:${lt_age} -no-undefined"
38
39 m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
40
41 EXTRA_CPPFLAGS=
42 EXTRA_CFLAGS=
43
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
65 fi
66 CFLAGS="${saved_CFLAGS}"
67
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.])
71
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 *-linux* | *-uclinux*)
87         dnl on Android Linux, some functions are in different places
88         case $host in
89         *-linux-android*)
90                 AC_MSG_RESULT([Android Linux])
91                 is_android_linux=yes
92                 ;;
93         *)
94                 AC_MSG_RESULT([Linux])
95                 ;;
96         esac
97         backend=linux
98         platform=posix
99         ;;
100 *-netbsd*)
101         AC_MSG_RESULT([NetBSD])
102         backend=netbsd
103         platform=posix
104         ;;
105 *-openbsd*)
106         AC_MSG_RESULT([OpenBSD])
107         backend=openbsd
108         platform=posix
109         ;;
110 *-solaris*)
111         AC_MSG_RESULT([SunOS])
112         backend=sunos
113         platform=posix
114         ;;
115 *-cygwin*)
116         AC_MSG_RESULT([Windows (using Cygwin)])
117         backend=windows
118         platform=windows
119         EXTRA_CFLAGS="-mwin32"
120         ;;
121 *-mingw* | *msys*)
122         AC_MSG_RESULT([Windows])
123         backend=windows
124         platform=windows
125         test "x$enable_shared" = xyes && create_import_lib=yes
126         EXTRA_CFLAGS="-mwin32 -fno-omit-frame-pointer"
127         ;;
128 *)
129         AC_MSG_RESULT([Null])
130         AC_MSG_WARN([The host being compiled for is not supported.])
131         AC_MSG_WARN([The library may compile but will not function in any useful manner.])
132         backend=null
133         platform=posix
134         ;;
135 esac
136
137 if test "x$platform" = xposix; then
138         AC_DEFINE([PLATFORM_POSIX], [1], [Define to 1 if compiling for a POSIX platform.])
139         AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
140         AC_CHECK_FUNCS([pipe2])
141         dnl Some compilers do not support the '-pthread' option so check for it here
142         saved_CFLAGS="${CFLAGS}"
143         CFLAGS="-Wall -Werror -pthread"
144         AC_MSG_CHECKING([if $CC recognizes -pthread])
145         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
146                 [AC_MSG_RESULT([yes])
147                  AC_SUBST(THREAD_CFLAGS, [-pthread])],
148                 [AC_MSG_RESULT([no])])
149         CFLAGS="${saved_CFLAGS}"
150         dnl Android Linux and Darwin provide pthread functions directly in libc
151         dnl glibc also provides some pthread functions directly, so search for a thread-specific function
152         AC_SEARCH_LIBS([pthread_create], [pthread],
153                 [test "x$ac_cv_search_pthread_create" != "xnone required" && AC_SUBST(THREAD_LIBS, [-lpthread])],
154                 [], [])
155 elif test "x$platform" = xwindows; then
156         AC_DEFINE([PLATFORM_WINDOWS], [1], [Define to 1 if compiling for a Windows platform.])
157 else
158         AC_MSG_ERROR([Unknown platform])
159 fi
160
161 case $backend in
162 darwin)
163         AC_CHECK_FUNCS([pthread_threadid_np])
164         LIBS="${LIBS} -lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation -Wl,-framework,Security"
165         AC_CHECK_HEADERS([IOKit/usb/IOUSBHostFamilyDefinitions.h])
166         ;;
167 haiku)
168         LIBS="${LIBS} -lbe"
169         ;;
170 linux)
171         AC_ARG_ENABLE([usbhost_api],
172                 [AC_HELP_STRING([--enable-usbhost-api], [Request to deviced to obtain dev node fds [default=no]])],
173                 [], [enable_usbhost_api="no"])
174         if test "x$enable_usbhost_api" = "xyes"; then
175            PKG_CHECK_MODULES([USBHOST_API], [dbus-1])
176            AC_DEFINE(USE_USBHOST_API, 1, [Request to deviced to obtain dev node fds])
177         fi
178
179         AC_SUBST(USE_USBHOST_API)
180
181         AC_SEARCH_LIBS([clock_gettime], [rt], [], [], [])
182         AC_CHECK_FUNCS([pthread_setname_np])
183         AC_ARG_ENABLE([udev],
184                 [AS_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])],
185                 [use_udev=$enableval], [use_udev=yes])
186         if test "x$use_udev" = xyes; then
187                 dnl system has udev. use it or fail!
188                 AC_CHECK_HEADER([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
189                 AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
190         else
191                 AC_CHECK_HEADERS([asm/types.h])
192                 AC_CHECK_HEADER([linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink header not found])])
193                 AC_CHECK_HEADER([sys/socket.h], [], [AC_MSG_ERROR([Linux socket header not found])])
194         fi
195         ;;
196 sunos)
197         LIBS="${LIBS} -ldevinfo"
198         ;;
199 windows)
200         AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
201         AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
202         LT_LDFLAGS="${LT_LDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
203         ;;
204 *)
205         dnl no special handling required
206         ;;
207 esac
208
209 dnl headers not available on all platforms but required on others
210 AC_CHECK_HEADERS([sys/time.h])
211
212 if test "x$platform" = xposix; then
213         dnl check availability of clock_gettime()
214         if test "x$backend" = xdarwin; then
215                 dnl need to verify that OS X target is 10.12 or later for clock_gettime()
216                 AC_MSG_CHECKING([whether OS X target version is 10.12 or later])
217                 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
218                                 #include <AvailabilityMacros.h>
219                                 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12
220                                 # error "Target OS X version is too old"
221                                 #endif
222                         ], [])],
223                         [AC_MSG_RESULT([yes])
224                          osx_10_12_or_later=yes],
225                         [AC_MSG_RESULT([no])
226                          osx_10_12_or_later=])
227                 if test "x$osx_10_12_or_later" = xyes; then
228                         AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [have_clock_gettime=])
229                 else
230                         AC_MSG_NOTICE([clock_gettime() is not available on target OS X version])
231                 fi
232         else
233                 AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [AC_MSG_ERROR([clock_gettime() is required on this platform])])
234         fi
235
236         if test "x$have_clock_gettime" = xyes; then
237                 dnl the clock_gettime() function needs certain clock IDs defined
238                 AC_CHECK_DECL([CLOCK_MONOTONIC], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_MONOTONIC])], [[#include <time.h>]])
239                 dnl use the monotonic clock for condition variable timed waits if possible
240                 AC_CHECK_FUNCS([pthread_condattr_setclock], [need_clock_realtime=], [need_clock_realtime=yes])
241                 if test "x$need_clock_realtime" = xyes; then
242                         AC_CHECK_DECL([CLOCK_REALTIME], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_REALTIME])], [[#include <time.h>]])
243                 fi
244         fi
245 fi
246
247 dnl eventfd support
248 if test "x$backend" = xlinux || test "x$backend" = xsunos; then
249         AC_ARG_ENABLE([eventfd],
250                 [AS_HELP_STRING([--enable-eventfd], [use eventfd for signalling [default=auto]])],
251                 [use_eventfd=$enableval],
252                 [use_eventfd=auto])
253         if test "x$use_eventfd" != xno; then
254                 AC_CHECK_HEADER([sys/eventfd.h], [eventfd_h=yes], [eventfd_h=])
255                 if test "x$eventfd_h" = xyes; then
256                         AC_CHECK_DECLS([EFD_NONBLOCK, EFD_CLOEXEC], [eventfd_h_ok=yes], [eventfd_h_ok=], [[#include <sys/eventfd.h>]])
257                         if test "x$eventfd_h_ok" = xyes; then
258                                 AC_CHECK_FUNC([eventfd], [eventfd_ok=yes], [eventfd_ok=])
259                                 if test "x$eventfd_ok" = xyes; then
260                                         AC_DEFINE([HAVE_EVENTFD], [1], [Define to 1 if the system has eventfd functionality.])
261                                 elif test "x$use_eventfd" = xyes; then
262                                         AC_MSG_ERROR([eventfd() function not found; glibc 2.9+ required])
263                                 fi
264                         elif test "x$use_eventfd" = xyes; then
265                                 AC_MSG_ERROR([eventfd header not usable; glibc 2.9+ required])
266                         fi
267                 elif test "x$use_eventfd" = xyes; then
268                         AC_MSG_ERROR([eventfd header not available; glibc 2.9+ required])
269                 fi
270         fi
271         AC_MSG_CHECKING([whether to use eventfd for signalling])
272         if test "x$use_eventfd" = xno; then
273                 AC_MSG_RESULT([no (disabled by user)])
274         elif test "x$eventfd_h" != xyes; then
275                 AC_MSG_RESULT([no (header not available)])
276         elif test "x$eventfd_h_ok" != xyes; then
277                 AC_MSG_RESULT([no (header not usable)])
278         elif test "x$eventfd_ok" != xyes; then
279                 AC_MSG_RESULT([no (functions not available)])
280         else
281                 AC_MSG_RESULT([yes])
282         fi
283 fi
284
285 dnl timerfd support
286 if test "x$backend" = xlinux || test "x$backend" = xsunos; then
287         AC_ARG_ENABLE([timerfd],
288                 [AS_HELP_STRING([--enable-timerfd], [use timerfd for timing [default=auto]])],
289                 [use_timerfd=$enableval],
290                 [use_timerfd=auto])
291         if test "x$use_timerfd" != xno; then
292                 AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=yes], [timerfd_h=])
293                 if test "x$timerfd_h" = xyes; then
294                         AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [timerfd_h_ok=yes], [timerfd_h_ok=], [[#include <sys/timerfd.h>]])
295                         if test "x$timerfd_h_ok" = xyes; then
296                                 AC_CHECK_FUNC([timerfd_create], [timerfd_ok=yes], [timerfd_ok=])
297                                 if test "x$timerfd_ok" = xyes; then
298                                         AC_DEFINE([HAVE_TIMERFD], [1], [Define to 1 if the system has timerfd functionality.])
299                                 elif test "x$use_timerfd" = xyes; then
300                                         AC_MSG_ERROR([timerfd_create() function not found; glibc 2.9+ required])
301                                 fi
302                         elif test "x$use_timerfd" = xyes; then
303                                 AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
304                         fi
305                 elif test "x$use_timerfd" = xyes; then
306                         AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
307                 fi
308         fi
309         AC_MSG_CHECKING([whether to use timerfd for timing])
310         if test "x$use_timerfd" = xno; then
311                 AC_MSG_RESULT([no (disabled by user)])
312         elif test "x$timerfd_h" != xyes; then
313                 AC_MSG_RESULT([no (header not available)])
314         elif test "x$timerfd_h_ok" != xyes; then
315                 AC_MSG_RESULT([no (header not usable)])
316         elif test "x$timerfd_ok" != xyes; then
317                 AC_MSG_RESULT([no (functions not available)])
318         else
319                 AC_MSG_RESULT([yes])
320         fi
321 fi
322
323 dnl Message logging
324 AC_ARG_ENABLE([log],
325         [AS_HELP_STRING([--disable-log], [disable all logging])],
326         [log_enabled=$enableval],
327         [log_enabled=yes])
328 if test "x$log_enabled" != xno; then
329         AC_DEFINE([ENABLE_LOGGING], [1], [Define to 1 to enable message logging.])
330 fi
331
332 AC_ARG_ENABLE([debug-log],
333         [AS_HELP_STRING([--enable-debug-log], [start with debug message logging enabled [default=no]])],
334         [debug_log_enabled=$enableval],
335         [debug_log_enabled=no])
336 if test "x$debug_log_enabled" != xno; then
337         AC_DEFINE([ENABLE_DEBUG_LOGGING], [1], [Define to 1 to start with debug message logging enabled.])
338 fi
339
340 AC_ARG_ENABLE([system-log],
341         [AS_HELP_STRING([--enable-system-log], [output logging messages to the systemwide log, if supported by the OS [default=no]])],
342         [system_log_enabled=$enableval],
343         [system_log_enabled=no])
344 if test "x$system_log_enabled" != xno; then
345         AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], [1], [Define to 1 to output logging messages to the systemwide log.])
346         if test "x$backend" != xwindows && test "x$is_android_linux" != xyes; then
347                 dnl Check if syslog is available in standard C library
348                 AC_CHECK_HEADER([syslog.h], [syslog_h=yes], [syslog_h=])
349                 if test "x$syslog_h" = xyes; then
350                         AC_CHECK_FUNCS([syslog])
351                 fi
352         fi
353 fi
354
355 dnl Examples build
356 AC_ARG_ENABLE([examples-build],
357         [AS_HELP_STRING([--enable-examples-build], [build example applications [default=no]])],
358         [build_examples=$enableval],
359         [build_examples=no])
360
361 dnl Tests build
362 AC_ARG_ENABLE([tests-build],
363         [AS_HELP_STRING([--enable-tests-build], [build test applications [default=no]])],
364         [build_tests=$enableval],
365         [build_tests=no])
366
367 AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
368 AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
369 AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
370 AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
371 AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
372 AM_CONDITIONAL([OS_LINUX], [test "x$backend" = xlinux])
373 AM_CONDITIONAL([OS_NETBSD], [test "x$backend" = xnetbsd])
374 AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
375 AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
376 AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
377 AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
378 AM_CONDITIONAL([PLATFORM_POSIX], [test "x$platform" = xposix])
379 AM_CONDITIONAL([PLATFORM_WINDOWS], [test "x$platform" = xwindows])
380 AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
381 AM_CONDITIONAL([USE_USBHOST_API], [test "x$enable_usbhost_api" = "xyes"])
382
383 dnl The -Wcast-function-type warning causes a flurry of warnings when compiling
384 dnl Windows with GCC 8 or later because of dynamically loaded functions
385 if test "x$backend" = xwindows; then
386         saved_CFLAGS="${CFLAGS}"
387         CFLAGS="-Werror -Wcast-function-type"
388         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
389                 [EXTRA_CFLAGS="${EXTRA_CFLAGS} -Wno-cast-function-type"],
390                 [])
391         CFLAGS="${saved_CFLAGS}"
392 fi
393
394 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"
395
396 AM_CPPFLAGS="${EXTRA_CPPFLAGS}"
397 AC_SUBST(AM_CPPFLAGS)
398
399 AM_CFLAGS="-std=${c_dialect}11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS}"
400 AC_SUBST(AM_CFLAGS)
401
402 AM_CXXFLAGS="-std=${c_dialect}++11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS} -Wmissing-declarations"
403 AC_SUBST(AM_CXXFLAGS)
404
405 AC_SUBST(LT_LDFLAGS)
406
407 dnl set name of html output directory for doxygen
408 AC_SUBST(DOXYGEN_HTMLDIR, [api-1.0])
409
410 AC_CONFIG_FILES([libusb-1.0.pc])
411 AC_CONFIG_FILES([Makefile])
412 AC_CONFIG_FILES([libusb/Makefile])
413 AC_CONFIG_FILES([examples/Makefile])
414 AC_CONFIG_FILES([tests/Makefile])
415 AC_CONFIG_FILES([doc/Makefile])
416 AC_CONFIG_FILES([doc/doxygen.cfg])
417 AC_OUTPUT