configure.ac 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. LU_DEFINE_VERSION_ATOM([LIBUSB_MAJOR])
  13. LU_DEFINE_VERSION_ATOM([LIBUSB_MINOR])
  14. LU_DEFINE_VERSION_ATOM([LIBUSB_MICRO])
  15. LU_DEFINE_VERSION_RC_ATOM([LIBUSB_RC])
  16. AC_PREREQ([2.69])
  17. AC_INIT([libusb-1.0], [LIBUSB_MAJOR[.]LIBUSB_MINOR[.]LIBUSB_MICRO[]LIBUSB_RC], [libusb-devel@lists.sourceforge.net], [libusb-1.0], [http://libusb.info])
  18. AC_CONFIG_HEADERS([config.h])
  19. AC_CONFIG_SRCDIR([libusb/core.c])
  20. AC_CONFIG_MACRO_DIR([m4])
  21. AC_PROG_CC
  22. AC_PROG_CXX
  23. AC_C_INLINE
  24. AM_INIT_AUTOMAKE
  25. LT_INIT
  26. LT_LANG([Windows Resource])
  27. dnl Library versioning
  28. dnl These numbers should be tweaked on every release. Read carefully:
  29. dnl http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
  30. dnl http://sourceware.org/autobook/autobook/autobook_91.html
  31. lt_current=3
  32. lt_revision=0
  33. lt_age=3
  34. LT_LDFLAGS="-version-info ${lt_current}:${lt_revision}:${lt_age} -no-undefined"
  35. m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
  36. EXTRA_CPPFLAGS=
  37. EXTRA_CFLAGS=
  38. dnl check for -std=gnu11 compiler support (optional)
  39. dnl note that we don't just check if the compiler accepts '-std=x11'
  40. dnl but also that it supports the _Thread_local keyword because some compilers
  41. dnl (e.g. gcc 4.8) accept the command line option but do not implement TLS
  42. saved_CFLAGS="${CFLAGS}"
  43. CFLAGS="-std=gnu11"
  44. AC_MSG_CHECKING([if $CC supports -std=gnu11])
  45. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
  46. [AC_MSG_RESULT([yes])
  47. c_dialect=gnu],
  48. [AC_MSG_RESULT([no])
  49. c_dialect=])
  50. if test "x$c_dialect" != xgnu; then
  51. dnl fallback check for -std=c11 compiler support (required)
  52. CFLAGS="-std=c11"
  53. AC_MSG_CHECKING([if $CC supports -std=c11])
  54. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])],
  55. [AC_MSG_RESULT([yes])],
  56. [AC_MSG_RESULT([no])
  57. AC_MSG_ERROR([compiler with C11 support is required to build libusb])])
  58. c_dialect=c
  59. fi
  60. CFLAGS="${saved_CFLAGS}"
  61. AC_DEFINE([_GNU_SOURCE], [1], [Enable GNU extensions.])
  62. AC_DEFINE([DEFAULT_VISIBILITY], [__attribute__ ((visibility ("default")))], [Define to the attribute for default visibility.])
  63. AC_DEFINE([PRINTF_FORMAT(a, b)], [__attribute__ ((__format__ (__printf__, a, b)))], [Define to the attribute for enabling parameter checks on printf-like functions.])
  64. create_import_lib=
  65. is_android_linux=
  66. AC_MSG_CHECKING([operating system])
  67. case $host in
  68. *-darwin*)
  69. AC_MSG_RESULT([Darwin/Mac OS X])
  70. backend=darwin
  71. platform=posix
  72. ;;
  73. *-haiku*)
  74. AC_MSG_RESULT([Haiku])
  75. backend=haiku
  76. platform=posix
  77. ;;
  78. *-linux* | *-uclinux*)
  79. dnl on Android Linux, some functions are in different places
  80. case $host in
  81. *-linux-android*)
  82. AC_MSG_RESULT([Android Linux])
  83. is_android_linux=yes
  84. ;;
  85. *)
  86. AC_MSG_RESULT([Linux])
  87. ;;
  88. esac
  89. backend=linux
  90. platform=posix
  91. ;;
  92. *-netbsd*)
  93. AC_MSG_RESULT([NetBSD])
  94. backend=netbsd
  95. platform=posix
  96. ;;
  97. *-openbsd*)
  98. AC_MSG_RESULT([OpenBSD])
  99. backend=openbsd
  100. platform=posix
  101. ;;
  102. *-solaris*)
  103. AC_MSG_RESULT([SunOS])
  104. backend=sunos
  105. platform=posix
  106. ;;
  107. *-cygwin*)
  108. AC_MSG_RESULT([Windows (using Cygwin)])
  109. backend=windows
  110. platform=windows
  111. EXTRA_CFLAGS="-mwin32"
  112. ;;
  113. *-mingw* | *msys*)
  114. AC_MSG_RESULT([Windows])
  115. backend=windows
  116. platform=windows
  117. test "x$enable_shared" = xyes && create_import_lib=yes
  118. EXTRA_CFLAGS="-mwin32 -fno-omit-frame-pointer"
  119. EXTRA_LDFLAGS="-static-libgcc"
  120. ;;
  121. *)
  122. AC_MSG_RESULT([Null])
  123. AC_MSG_WARN([The host being compiled for is not supported.])
  124. AC_MSG_WARN([The library may compile but will not function in any useful manner.])
  125. backend=null
  126. platform=posix
  127. ;;
  128. esac
  129. if test "x$platform" = xposix; then
  130. AC_DEFINE([PLATFORM_POSIX], [1], [Define to 1 if compiling for a POSIX platform.])
  131. AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
  132. AC_CHECK_FUNCS([pipe2])
  133. dnl Some compilers do not support the '-pthread' option so check for it here
  134. saved_CFLAGS="${CFLAGS}"
  135. CFLAGS="-Wall -Werror -pthread"
  136. AC_MSG_CHECKING([if $CC recognizes -pthread])
  137. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
  138. [AC_MSG_RESULT([yes])
  139. AC_SUBST(THREAD_CFLAGS, [-pthread])],
  140. [AC_MSG_RESULT([no])])
  141. CFLAGS="${saved_CFLAGS}"
  142. dnl Android Linux and Darwin provide pthread functions directly in libc
  143. dnl glibc also provides some pthread functions directly, so search for a thread-specific function
  144. AC_SEARCH_LIBS([pthread_create], [pthread],
  145. [test "x$ac_cv_search_pthread_create" != "xnone required" && AC_SUBST(THREAD_LIBS, [-lpthread])],
  146. [], [])
  147. AC_SEARCH_LIBS([__atomic_fetch_add_4], [atomic])
  148. elif test "x$platform" = xwindows; then
  149. AC_DEFINE([PLATFORM_WINDOWS], [1], [Define to 1 if compiling for a Windows platform.])
  150. else
  151. AC_MSG_ERROR([Unknown platform])
  152. fi
  153. case $backend in
  154. darwin)
  155. AC_CHECK_FUNCS([pthread_threadid_np])
  156. LIBS="${LIBS} -lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation -Wl,-framework,Security"
  157. AC_CHECK_HEADERS([IOKit/usb/IOUSBHostFamilyDefinitions.h])
  158. ;;
  159. haiku)
  160. LIBS="${LIBS} -lbe"
  161. ;;
  162. linux)
  163. AC_SEARCH_LIBS([clock_gettime], [rt], [], [], [])
  164. AC_CHECK_FUNCS([pthread_setname_np])
  165. AC_ARG_ENABLE([udev],
  166. [AS_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])],
  167. [use_udev=$enableval], [use_udev=yes])
  168. if test "x$use_udev" = xyes; then
  169. dnl system has udev. use it or fail!
  170. AC_CHECK_HEADER([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
  171. AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
  172. # We can build umockdev tests (if available)
  173. PKG_PROG_PKG_CONFIG
  174. PKG_CHECK_MODULES(UMOCKDEV, umockdev-1.0 >= 0.16.0, ac_have_umockdev=yes, ac_have_umockdev=no)
  175. PKG_CHECK_MODULES(UMOCKDEV_HOTPLUG, umockdev-1.0 >= 0.17.7, ac_umockdev_hotplug=yes, ac_umockdev_hotplug=no)
  176. if test "x$ac_umockdev_hotplug" = xyes; then
  177. AC_DEFINE([UMOCKDEV_HOTPLUG], [1], [UMockdev hotplug code is not racy])
  178. fi
  179. else
  180. AC_CHECK_HEADERS([asm/types.h])
  181. AC_CHECK_HEADER([linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink header not found])])
  182. AC_CHECK_HEADER([sys/socket.h], [], [AC_MSG_ERROR([Linux socket header not found])])
  183. fi
  184. ;;
  185. sunos)
  186. LIBS="${LIBS} -ldevinfo"
  187. ;;
  188. windows)
  189. AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
  190. AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Define to the oldest supported Windows version.])
  191. LT_LDFLAGS="${LT_LDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
  192. ;;
  193. *)
  194. dnl no special handling required
  195. ;;
  196. esac
  197. dnl headers not available on all platforms but required on others
  198. AC_CHECK_HEADERS([sys/time.h])
  199. if test "x$platform" = xposix; then
  200. dnl check availability of clock_gettime()
  201. if test "x$backend" = xdarwin; then
  202. dnl need to verify that OS X target is 10.12 or later for clock_gettime()
  203. AC_MSG_CHECKING([whether OS X target version is 10.12 or later])
  204. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
  205. #include <AvailabilityMacros.h>
  206. #if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
  207. # error "Target OS X version is too old"
  208. #endif
  209. ], [])],
  210. [AC_MSG_RESULT([yes])
  211. osx_10_12_or_later=yes],
  212. [AC_MSG_RESULT([no])
  213. osx_10_12_or_later=])
  214. if test "x$osx_10_12_or_later" = xyes; then
  215. AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [have_clock_gettime=])
  216. else
  217. AC_MSG_NOTICE([clock_gettime() is not available on target OS X version])
  218. fi
  219. else
  220. AC_CHECK_FUNCS([clock_gettime], [have_clock_gettime=yes], [AC_MSG_ERROR([clock_gettime() is required on this platform])])
  221. fi
  222. if test "x$have_clock_gettime" = xyes; then
  223. dnl the clock_gettime() function needs certain clock IDs defined
  224. AC_CHECK_DECL([CLOCK_MONOTONIC], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_MONOTONIC])], [[#include <time.h>]])
  225. dnl use the monotonic clock for condition variable timed waits if possible
  226. AC_CHECK_FUNCS([pthread_condattr_setclock], [need_clock_realtime=], [need_clock_realtime=yes])
  227. if test "x$need_clock_realtime" = xyes; then
  228. AC_CHECK_DECL([CLOCK_REALTIME], [], [AC_MSG_ERROR([C library headers missing definition for CLOCK_REALTIME])], [[#include <time.h>]])
  229. fi
  230. fi
  231. fi
  232. dnl eventfd support
  233. if test "x$backend" = xlinux || test "x$backend" = xsunos; then
  234. AC_ARG_ENABLE([eventfd],
  235. [AS_HELP_STRING([--enable-eventfd], [use eventfd for signalling [default=auto]])],
  236. [use_eventfd=$enableval],
  237. [use_eventfd=auto])
  238. if test "x$use_eventfd" != xno; then
  239. AC_CHECK_HEADER([sys/eventfd.h], [eventfd_h=yes], [eventfd_h=])
  240. if test "x$eventfd_h" = xyes; then
  241. AC_CHECK_DECLS([EFD_NONBLOCK, EFD_CLOEXEC], [eventfd_h_ok=yes], [eventfd_h_ok=], [[#include <sys/eventfd.h>]])
  242. if test "x$eventfd_h_ok" = xyes; then
  243. AC_CHECK_FUNC([eventfd], [eventfd_ok=yes], [eventfd_ok=])
  244. if test "x$eventfd_ok" = xyes; then
  245. AC_DEFINE([HAVE_EVENTFD], [1], [Define to 1 if the system has eventfd functionality.])
  246. elif test "x$use_eventfd" = xyes; then
  247. AC_MSG_ERROR([eventfd() function not found; glibc 2.9+ required])
  248. fi
  249. elif test "x$use_eventfd" = xyes; then
  250. AC_MSG_ERROR([eventfd header not usable; glibc 2.9+ required])
  251. fi
  252. elif test "x$use_eventfd" = xyes; then
  253. AC_MSG_ERROR([eventfd header not available; glibc 2.9+ required])
  254. fi
  255. fi
  256. AC_MSG_CHECKING([whether to use eventfd for signalling])
  257. if test "x$use_eventfd" = xno; then
  258. AC_MSG_RESULT([no (disabled by user)])
  259. elif test "x$eventfd_h" != xyes; then
  260. AC_MSG_RESULT([no (header not available)])
  261. elif test "x$eventfd_h_ok" != xyes; then
  262. AC_MSG_RESULT([no (header not usable)])
  263. elif test "x$eventfd_ok" != xyes; then
  264. AC_MSG_RESULT([no (functions not available)])
  265. else
  266. AC_MSG_RESULT([yes])
  267. fi
  268. fi
  269. dnl timerfd support
  270. if test "x$backend" = xlinux || test "x$backend" = xsunos; then
  271. AC_ARG_ENABLE([timerfd],
  272. [AS_HELP_STRING([--enable-timerfd], [use timerfd for timing [default=auto]])],
  273. [use_timerfd=$enableval],
  274. [use_timerfd=auto])
  275. if test "x$use_timerfd" != xno; then
  276. AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=yes], [timerfd_h=])
  277. if test "x$timerfd_h" = xyes; then
  278. AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [timerfd_h_ok=yes], [timerfd_h_ok=], [[#include <sys/timerfd.h>]])
  279. if test "x$timerfd_h_ok" = xyes; then
  280. AC_CHECK_FUNC([timerfd_create], [timerfd_ok=yes], [timerfd_ok=])
  281. if test "x$timerfd_ok" = xyes; then
  282. AC_DEFINE([HAVE_TIMERFD], [1], [Define to 1 if the system has timerfd functionality.])
  283. elif test "x$use_timerfd" = xyes; then
  284. AC_MSG_ERROR([timerfd_create() function not found; glibc 2.9+ required])
  285. fi
  286. elif test "x$use_timerfd" = xyes; then
  287. AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
  288. fi
  289. elif test "x$use_timerfd" = xyes; then
  290. AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
  291. fi
  292. fi
  293. AC_MSG_CHECKING([whether to use timerfd for timing])
  294. if test "x$use_timerfd" = xno; then
  295. AC_MSG_RESULT([no (disabled by user)])
  296. elif test "x$timerfd_h" != xyes; then
  297. AC_MSG_RESULT([no (header not available)])
  298. elif test "x$timerfd_h_ok" != xyes; then
  299. AC_MSG_RESULT([no (header not usable)])
  300. elif test "x$timerfd_ok" != xyes; then
  301. AC_MSG_RESULT([no (functions not available)])
  302. else
  303. AC_MSG_RESULT([yes])
  304. fi
  305. fi
  306. dnl Message logging
  307. AC_ARG_ENABLE([log],
  308. [AS_HELP_STRING([--disable-log], [disable all logging])],
  309. [log_enabled=$enableval],
  310. [log_enabled=yes])
  311. if test "x$log_enabled" != xno; then
  312. AC_DEFINE([ENABLE_LOGGING], [1], [Define to 1 to enable message logging.])
  313. fi
  314. AC_ARG_ENABLE([debug-log],
  315. [AS_HELP_STRING([--enable-debug-log], [start with debug message logging enabled [default=no]])],
  316. [debug_log_enabled=$enableval],
  317. [debug_log_enabled=no])
  318. if test "x$debug_log_enabled" != xno; then
  319. AC_DEFINE([ENABLE_DEBUG_LOGGING], [1], [Define to 1 to start with debug message logging enabled.])
  320. fi
  321. AC_ARG_ENABLE([system-log],
  322. [AS_HELP_STRING([--enable-system-log], [output logging messages to the systemwide log, if supported by the OS [default=no]])],
  323. [system_log_enabled=$enableval],
  324. [system_log_enabled=no])
  325. if test "x$system_log_enabled" != xno; then
  326. AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], [1], [Define to 1 to output logging messages to the systemwide log.])
  327. if test "x$backend" != xwindows && test "x$is_android_linux" != xyes; then
  328. dnl Check if syslog is available in standard C library
  329. AC_CHECK_HEADER([syslog.h], [syslog_h=yes], [syslog_h=])
  330. if test "x$syslog_h" = xyes; then
  331. AC_CHECK_FUNCS([syslog])
  332. fi
  333. fi
  334. fi
  335. dnl Examples build
  336. AC_ARG_ENABLE([examples-build],
  337. [AS_HELP_STRING([--enable-examples-build], [build example applications [default=no]])],
  338. [build_examples=$enableval],
  339. [build_examples=no])
  340. dnl Tests build
  341. AC_ARG_ENABLE([tests-build],
  342. [AS_HELP_STRING([--enable-tests-build], [build test applications [default=no]])],
  343. [build_tests=$enableval],
  344. [build_tests=no])
  345. AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
  346. AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
  347. AM_CONDITIONAL([BUILD_UMOCKDEV_TEST], [test "x$ac_have_umockdev" = xyes -a "x$log_enabled" != xno])
  348. AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
  349. AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
  350. AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
  351. AM_CONDITIONAL([OS_LINUX], [test "x$backend" = xlinux])
  352. AM_CONDITIONAL([OS_NETBSD], [test "x$backend" = xnetbsd])
  353. AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
  354. AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
  355. AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
  356. AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
  357. AM_CONDITIONAL([PLATFORM_POSIX], [test "x$platform" = xposix])
  358. AM_CONDITIONAL([PLATFORM_WINDOWS], [test "x$platform" = xwindows])
  359. AM_CONDITIONAL([USE_UDEV], [test "x$use_udev" = xyes])
  360. dnl The -Wcast-function-type warning causes a flurry of warnings when compiling
  361. dnl Windows with GCC 8 or later because of dynamically loaded functions
  362. if test "x$backend" = xwindows; then
  363. saved_CFLAGS="${CFLAGS}"
  364. CFLAGS="-Werror -Wcast-function-type"
  365. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
  366. [EXTRA_CFLAGS="${EXTRA_CFLAGS} -Wno-cast-function-type"],
  367. [])
  368. CFLAGS="${saved_CFLAGS}"
  369. fi
  370. 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"
  371. AM_CPPFLAGS="${EXTRA_CPPFLAGS}"
  372. AC_SUBST(AM_CPPFLAGS)
  373. AM_CFLAGS="-std=${c_dialect}11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS}"
  374. AC_SUBST(AM_CFLAGS)
  375. AM_CXXFLAGS="-std=${c_dialect}++11 ${EXTRA_CFLAGS} ${SHARED_CFLAGS} -Wmissing-declarations"
  376. AC_SUBST(AM_CXXFLAGS)
  377. AC_SUBST(LT_LDFLAGS)
  378. AC_SUBST([EXTRA_LDFLAGS])
  379. dnl set name of html output directory for doxygen
  380. AC_SUBST(DOXYGEN_HTMLDIR, [api-1.0])
  381. AC_CONFIG_FILES([libusb-1.0.pc])
  382. AC_CONFIG_FILES([Makefile])
  383. AC_CONFIG_FILES([libusb/Makefile])
  384. AC_CONFIG_FILES([examples/Makefile])
  385. AC_CONFIG_FILES([tests/Makefile])
  386. AC_CONFIG_FILES([doc/Makefile])
  387. AC_CONFIG_FILES([doc/doxygen.cfg])
  388. AC_OUTPUT