configure.ac: Fix compilation of Haiku's C++ convenience library
[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_CFLAGS=
42
43 dnl check for -std=gnu11 compiler support (optional)
44 dnl note that we don't just check if the compiler accepts '-std=x11'
45 dnl but also that it supports the _Thread_local keyword because some compilers
46 dnl (e.g. gcc 4.8) accept the command line option but do not implement TLS
47 saved_CFLAGS="${CFLAGS}"
48 CFLAGS="-std=gnu11"
49 AC_MSG_CHECKING([if $CC supports -std=gnu11])
50 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
51         [AC_MSG_RESULT([yes])
52          c_dialect=gnu],
53         [AC_MSG_RESULT([no])
54          c_dialect=])
55 if test "x$c_dialect" != xgnu; then
56         dnl fallback check for -std=c11 compiler support (required)
57         CFLAGS="-std=c11"
58         AC_MSG_CHECKING([if $CC supports -std=c11])
59         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
60                 [AC_MSG_RESULT([yes])],
61                 [AC_MSG_RESULT([no])
62                  AC_MSG_ERROR([compiler with C11 support is required to build libusb])])
63         c_dialect=c
64 fi
65 CFLAGS="${saved_CFLAGS}"
66
67 AC_DEFINE([_GNU_SOURCE], [1], [Enable GNU extensions.])
68 AC_DEFINE([DEFAULT_VISIBILITY], [__attribute__ ((visibility ("default")))], [Define to the attribute for default visibility.])
69
70 create_import_lib=
71 is_android_linux=
72 AC_MSG_CHECKING([operating system])
73 case $host in
74 *-darwin*)
75         AC_MSG_RESULT([Darwin/Mac OS X])
76         backend=darwin
77         poll=posix
78         threads=posix
79         ;;
80 *-haiku*)
81         AC_MSG_RESULT([Haiku])
82         backend=haiku
83         poll=posix
84         threads=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         poll=posix
99         threads=posix
100         ;;
101 *-netbsd*)
102         AC_MSG_RESULT([NetBSD])
103         backend=netbsd
104         poll=posix
105         threads=posix
106         ;;
107 *-openbsd*)
108         AC_MSG_RESULT([OpenBSD])
109         backend=openbsd
110         poll=posix
111         threads=posix
112         ;;
113 *-solaris*)
114         AC_MSG_RESULT([SunOS])
115         backend=sunos
116         poll=posix
117         threads=posix
118         ;;
119 *-cygwin*)
120         AC_MSG_RESULT([Windows (using Cygwin)])
121         backend=windows
122         poll=windows
123         threads=posix
124         ;;
125 *-mingw* | *msys*)
126         AC_MSG_RESULT([Windows])
127         backend=windows
128         poll=windows
129         threads=windows
130         test "x$enable_shared" = xyes && create_import_lib=yes
131         EXTRA_CFLAGS="-fno-omit-frame-pointer"
132         ;;
133 *)
134         AC_MSG_RESULT([Null])
135         AC_MSG_WARN([The host being compiled for is not supported.])
136         AC_MSG_WARN([The library may compile but will not function in any useful manner.])
137         backend="null"
138         poll=posix
139         threads="posix"
140         ;;
141 esac
142
143 if test "x$poll" = xposix; then
144         AC_DEFINE([POLL_POSIX], [1], [Define to 1 if using the POSIX poll() implementation.])
145         AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
146         AC_CHECK_FUNCS([pipe2])
147 elif test "x$poll" = xwindows; then
148         AC_DEFINE([POLL_WINDOWS], [1], [Define to 1 if using the Windows poll() implementation.])
149 else
150         AC_MSG_ERROR([Unknown poll implementation])
151 fi
152
153 if test "x$threads" = xposix; then
154         AC_DEFINE([THREADS_POSIX], [1], [Define to 1 if using POSIX threads.])
155         AC_SUBST(THREAD_CFLAGS, [-pthread])
156         dnl Android Linux and Darwin provide pthread functions directly in libc
157         dnl glibc also provides some pthread functions directly, so search for a thread-specific function
158         AC_SEARCH_LIBS([pthread_key_create], [pthread], [AC_SUBST(THREAD_LIBS, [-lpthread])], [], [])
159 elif test "x$threads" = xwindows; then
160         AC_DEFINE([THREADS_WINDOWS], [1], [Define to 1 if using Windows threads.])
161 else
162         AC_MSG_ERROR([Unknown threads implementation])
163 fi
164
165 case $backend in
166 darwin)
167         AC_CHECK_FUNCS([pthread_threadid_np])
168         LIBS="${LIBS} -lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
169         ;;
170 haiku)
171         LIBS="${LIBS} -lbe"
172         ;;
173 linux)
174         AC_SEARCH_LIBS([clock_gettime], [rt], [], [], [])
175         AC_CHECK_FUNCS([pthread_setname_np])
176         AC_ARG_ENABLE([udev],
177                 [AC_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])],
178                 [use_udev=$enableval], [use_udev=yes])
179         if test "x$use_udev" = xyes; then
180                 dnl system has udev. use it or fail!
181                 AC_CHECK_HEADER([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
182                 AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
183         else
184                 AC_CHECK_HEADERS([asm/types.h])
185                 AC_CHECK_HEADER([linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink header not found])])
186                 AC_CHECK_HEADER([sys/socket.h], [], [AC_MSG_ERROR([Linux socket header not found])])
187         fi
188         ;;
189 sunos)
190         LIBS="${LIBS} -ldevinfo"
191         ;;
192 windows)
193         AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
194         AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
195         LT_LDFLAGS="${LT_LDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
196         ;;
197 *)
198         dnl no special handling required
199         ;;
200 esac
201
202 dnl headers not available on all platforms but required on others
203 AC_CHECK_HEADERS([sys/time.h])
204
205 dnl the clock_gettime() function needs certain clock IDs defined
206 AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [have_clock_gettime=])
207 if test "x$have_clock_gettime" = xyes; then
208         AC_CHECK_DECL([CLOCK_REALTIME], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_REALTIME])], [[#include <time.h>]])
209         AC_CHECK_DECL([CLOCK_MONOTONIC], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_MONOTONIC])], [[#include <time.h>]])
210 elif test "x$backend" != xdarwin && test "x$backend" != xwindows; then
211         AC_MSG_ERROR([clock_gettime() is required on this platform])
212 fi
213
214 dnl timerfd support
215 if test "x$backend" = xlinux || test "x$backend" = xsunos; then
216         AC_ARG_ENABLE([timerfd],
217                 [AS_HELP_STRING([--enable-timerfd], [use timerfd for timing [default=auto]])],
218                 [use_timerfd=$enableval],
219                 [use_timerfd=auto])
220         if test "x$use_timerfd" != xno; then
221                 AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=yes], [timerfd_h=])
222                 if test "x$timerfd_h" = xyes; then
223                         AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [timerfd_h_ok=yes], [timerfd_h_ok=], [[#include <sys/timerfd.h>]])
224                         if test "x$timerfd_h_ok" = xyes; then
225                                 AC_CHECK_FUNC([timerfd_create], [timerfd_ok=yes], [timerfd_ok=])
226                                 if test "x$timerfd_ok" = xyes; then
227                                         AC_DEFINE([HAVE_TIMERFD], [1], [Define to 1 if the system has timerfd functionality.])
228                                 elif test "x$use_timerfd" = xyes; then
229                                         AC_MSG_ERROR([timerfd_create() function not found; glibc 2.9+ required])
230                                 fi
231                         elif test "x$use_timerfd" = xyes; then
232                                 AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
233                         fi
234                 elif test "x$use_timerfd" = xyes; then
235                         AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
236                 fi
237         fi
238         AC_MSG_CHECKING([whether to use timerfd for timing])
239         if test "x$use_timerfd" = xno; then
240                 AC_MSG_RESULT([no (disabled by user)])
241         elif test "x$timerfd_h" != xyes; then
242                 AC_MSG_RESULT([no (header not available)])
243         elif test "x$timerfd_h_ok" != xyes; then
244                 AC_MSG_RESULT([no (header not usable)])
245         elif test "x$timerfd_ok" != xyes; then
246                 AC_MSG_RESULT([no (functions not available)])
247         else
248                 AC_MSG_RESULT([yes])
249         fi
250 fi
251
252 dnl Message logging
253 AC_ARG_ENABLE([log],
254         [AS_HELP_STRING([--disable-log], [disable all logging])],
255         [log_enabled=$enableval],
256         [log_enabled=yes])
257 if test "x$log_enabled" != xno; then
258         AC_DEFINE([ENABLE_LOGGING], [1], [Define to 1 to enable message logging.])
259 fi
260
261 AC_ARG_ENABLE([debug-log],
262         [AS_HELP_STRING([--enable-debug-log], [start with debug message logging enabled [default=no]])],
263         [debug_log_enabled=$enableval],
264         [debug_log_enabled=no])
265 if test "x$debug_log_enabled" != xno; then
266         AC_DEFINE([ENABLE_DEBUG_LOGGING], [1], [Define to 1 to start with debug message logging enabled.])
267 fi
268
269 AC_ARG_ENABLE([system-log],
270         [AS_HELP_STRING([--enable-system-log], [output logging messages to the systemwide log, if supported by the OS [default=no]])],
271         [system_log_enabled=$enableval],
272         [system_log_enabled=no])
273 if test "x$system_log_enabled" != xno; then
274         AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], [1], [Define to 1 to output logging messages to the systemwide log.])
275         if test "x$backend" != xwindows && test "x$is_android_linux" != xyes; then
276                 dnl Check if syslog is available in standard C library
277                 AC_CHECK_HEADER([syslog.h], [syslog_h=yes], [syslog_h=])
278                 if test "x$syslog_h" = xyes; then
279                         AC_CHECK_FUNCS([syslog])
280                 fi
281         fi
282 fi
283
284 dnl Examples build
285 AC_ARG_ENABLE([examples-build],
286         [AS_HELP_STRING([--enable-examples-build], [build example applications [default=no]])],
287         [build_examples=$enableval],
288         [build_examples=no])
289 if test "x$build_examples" != xno; then
290         dnl sigaction needed for some example programs
291         AC_CHECK_FUNC([sigaction], [have_sigaction=yes], [have_sigaction=])
292 fi
293
294 dnl Tests build
295 AC_ARG_ENABLE([tests-build],
296         [AS_HELP_STRING([--enable-tests-build], [build test applications [default=no]])],
297         [build_tests=$enableval],
298         [build_tests=no])
299
300 AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
301 AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
302 AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
303 AM_CONDITIONAL([HAVE_SIGACTION], [test "x$have_sigaction" = xyes])
304 AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
305 AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
306 AM_CONDITIONAL([OS_LINUX], [test "x$backend" = xlinux])
307 AM_CONDITIONAL([OS_NETBSD], [test "x$backend" = xnetbsd])
308 AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
309 AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
310 AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
311 AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
312 AM_CONDITIONAL([POLL_POSIX], [test "x$poll" = xposix])
313 AM_CONDITIONAL([POLL_WINDOWS], [test "x$poll" = xwindows])
314 AM_CONDITIONAL([THREADS_POSIX], [test "x$threads" = xposix])
315 AM_CONDITIONAL([THREADS_WINDOWS], [test "x$threads" = xwindows])
316 AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
317
318 dnl The -Wcast-function-type warning causes a flurry of warnings when compiling
319 dnl Windows with GCC 8 or later because of dynamically loaded functions
320 if test "x$backend" = xwindows; then
321         saved_CFLAGS="${CFLAGS}"
322         CFLAGS="-Werror -Wcast-function-type"
323         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
324                 [EXTRA_CFLAGS="${EXTRA_CFLAGS} -Wno-cast-function-type"],
325                 [])
326         CFLAGS="${saved_CFLAGS}"
327 fi
328
329 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"
330
331 AM_CFLAGS="-std=${c_dialect}11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS}"
332 AC_SUBST(AM_CFLAGS)
333
334 AM_CXXFLAGS="-std=${c_dialect}++11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS} -Wmissing-declarations"
335 AC_SUBST(AM_CXXFLAGS)
336
337 AC_SUBST(LT_LDFLAGS)
338
339 dnl set name of html output directory for doxygen
340 AC_SUBST(DOXYGEN_HTMLDIR, [api-1.0])
341
342 AC_CONFIG_FILES([libusb-1.0.pc])
343 AC_CONFIG_FILES([Makefile])
344 AC_CONFIG_FILES([libusb/Makefile])
345 AC_CONFIG_FILES([examples/Makefile])
346 AC_CONFIG_FILES([tests/Makefile])
347 AC_CONFIG_FILES([doc/Makefile])
348 AC_CONFIG_FILES([doc/doxygen.cfg])
349 AC_OUTPUT