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