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