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