CMakeLists.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. if (MINGW OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux") OR APPLE)
  2. set(LIBUSB_FOUND ON CACHE BOOL "libusb is present" FORCE)
  3. set(LIBUSB_VERSION "1.0.24" CACHE STRING "libusb version string" FORCE)
  4. # GNU toolchains for some reason doesn't work with the later half of this CMakeLists after
  5. # updating to 1.0.24, so we do it the old-fashioned way for now.
  6. # Require autoconf and libtoolize here, rather than crash during compilation
  7. find_program(AUTOCONF autoconf)
  8. if ("${AUTOCONF}" STREQUAL "AUTOCONF-NOTFOUND")
  9. message(FATAL_ERROR "Required program `autoconf` not found.")
  10. endif()
  11. find_program(LIBTOOLIZE libtoolize)
  12. if ("${LIBTOOLIZE}" STREQUAL "LIBTOOLIZE-NOTFOUND")
  13. message(FATAL_ERROR "Required program `libtoolize` not found.")
  14. endif()
  15. set(LIBUSB_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/libusb")
  16. set(LIBUSB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libusb")
  17. # Workarounds for MSYS/MinGW
  18. if (MSYS)
  19. # CMake on Windows passes `C:/`, but we need `/C/` or `/c/` to use `configure`
  20. string(REPLACE ":/" "/" LIBUSB_SRC_DIR "${LIBUSB_SRC_DIR}")
  21. set(LIBUSB_SRC_DIR "/${LIBUSB_SRC_DIR}")
  22. # And now that we are using /C/ for srcdir but everything else is using C:/, we need to
  23. # compile everything in the source directory, else `configure` won't think the build
  24. # environment is sane.
  25. set(LIBUSB_PREFIX "${LIBUSB_SRC_DIR}")
  26. endif()
  27. set(LIBUSB_CONFIGURE "${LIBUSB_SRC_DIR}/configure")
  28. set(LIBUSB_MAKEFILE "${LIBUSB_PREFIX}/Makefile")
  29. if (MINGW)
  30. set(LIBUSB_LIBRARIES "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.dll.a" CACHE PATH "libusb library path" FORCE)
  31. set(LIBUSB_SHARED_LIBRARY "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.dll")
  32. set(LIBUSB_SHARED_LIBRARY_DEST "${CMAKE_BINARY_DIR}/bin/libusb-1.0.dll")
  33. set(LIBUSB_CONFIGURE_ARGS --host=x86_64-w64-mingw32 --build=x86_64-windows)
  34. else()
  35. set(LIBUSB_LIBRARIES "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.a" CACHE PATH "libusb library path" FORCE)
  36. endif()
  37. set(LIBUSB_INCLUDE_DIRS "${LIBUSB_SRC_DIR}/libusb" CACHE PATH "libusb headers path" FORCE)
  38. # MINGW: causes "externals/libusb/libusb/libusb/os/windows_winusb.c:1427:2: error: conversion to non-scalar type requested", so cannot statically link it for now.
  39. if (NOT MINGW)
  40. set(LIBUSB_CFLAGS "-DGUID_DEVINTERFACE_USB_DEVICE=\\(GUID\\){0xA5DCBF10,0x6530,0x11D2,{0x90,0x1F,0x00,0xC0,0x4F,0xB9,0x51,0xED}}")
  41. endif()
  42. make_directory("${LIBUSB_PREFIX}")
  43. add_custom_command(
  44. OUTPUT
  45. "${LIBUSB_LIBRARIES}"
  46. COMMAND
  47. make
  48. WORKING_DIRECTORY
  49. "${LIBUSB_PREFIX}"
  50. )
  51. add_custom_command(
  52. OUTPUT
  53. "${LIBUSB_MAKEFILE}"
  54. COMMAND
  55. env
  56. CC="${CMAKE_C_COMPILER}"
  57. CXX="${CMAKE_CXX_COMPILER}"
  58. CFLAGS="${LIBUSB_CFLAGS}"
  59. sh "${LIBUSB_CONFIGURE}"
  60. ${LIBUSB_CONFIGURE_ARGS}
  61. --srcdir="${LIBUSB_SRC_DIR}"
  62. WORKING_DIRECTORY
  63. "${LIBUSB_PREFIX}"
  64. )
  65. add_custom_command(
  66. OUTPUT
  67. "${LIBUSB_CONFIGURE}"
  68. COMMAND
  69. sh "${LIBUSB_SRC_DIR}/bootstrap.sh"
  70. WORKING_DIRECTORY
  71. "${LIBUSB_SRC_DIR}"
  72. )
  73. add_custom_command(
  74. OUTPUT
  75. "${LIBUSB_SHARED_LIBRARY_DEST}"
  76. COMMAND
  77. cp "${LIBUSB_SHARED_LIBRARY}" "${LIBUSB_SHARED_LIBRARY_DEST}"
  78. )
  79. add_custom_target(usb-bootstrap DEPENDS "${LIBUSB_CONFIGURE}")
  80. add_custom_target(usb-configure DEPENDS "${LIBUSB_MAKEFILE}" usb-bootstrap)
  81. add_custom_target(usb-build ALL DEPENDS "${LIBUSB_LIBRARIES}" usb-configure)
  82. # Workaround since static linking didn't work out -- We need to copy the DLL to the bin directory
  83. add_custom_target(usb-copy ALL DEPENDS "${LIBUSB_SHARED_LIBRARY_DEST}" usb-build)
  84. add_library(usb INTERFACE)
  85. add_dependencies(usb usb-copy)
  86. target_link_libraries(usb INTERFACE "${LIBUSB_LIBRARIES}")
  87. target_include_directories(usb INTERFACE "${LIBUSB_INCLUDE_DIRS}")
  88. if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  89. Include(FindPkgConfig)
  90. pkg_check_modules(LIBUDEV REQUIRED libudev)
  91. if (LIBUDEV_FOUND)
  92. target_include_directories(usb INTERFACE "${LIBUDEV_INCLUDE_DIRS}")
  93. target_link_libraries(usb INTERFACE "${LIBUDEV_STATIC_LIBRARIES}")
  94. endif()
  95. endif()
  96. else() # MINGW OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  97. # Ensure libusb compiles with UTF-8 encoding on MSVC
  98. if(MSVC)
  99. add_compile_options(/utf-8)
  100. endif()
  101. add_library(usb STATIC EXCLUDE_FROM_ALL
  102. libusb/libusb/core.c
  103. libusb/libusb/core.c
  104. libusb/libusb/descriptor.c
  105. libusb/libusb/hotplug.c
  106. libusb/libusb/io.c
  107. libusb/libusb/strerror.c
  108. libusb/libusb/sync.c
  109. )
  110. set_target_properties(usb PROPERTIES VERSION 1.0.24)
  111. if(WIN32)
  112. target_include_directories(usb
  113. BEFORE
  114. PUBLIC
  115. libusb/libusb
  116. PRIVATE
  117. "${CMAKE_CURRENT_BINARY_DIR}"
  118. )
  119. if (NOT MINGW)
  120. target_include_directories(usb BEFORE PRIVATE libusb/msvc)
  121. endif()
  122. # Works around other libraries providing their own definition of USB GUIDs (e.g. SDL2)
  123. target_compile_definitions(usb PRIVATE "-DGUID_DEVINTERFACE_USB_DEVICE=(GUID){ 0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}}")
  124. else()
  125. target_include_directories(usb
  126. # turns out other projects also have "config.h", so make sure the
  127. # LibUSB one comes first
  128. BEFORE
  129. PUBLIC
  130. libusb/libusb
  131. PRIVATE
  132. "${CMAKE_CURRENT_BINARY_DIR}"
  133. )
  134. endif()
  135. if(WIN32 OR CYGWIN)
  136. target_sources(usb PRIVATE
  137. libusb/libusb/os/threads_windows.c
  138. libusb/libusb/os/windows_winusb.c
  139. libusb/libusb/os/windows_usbdk.c
  140. libusb/libusb/os/windows_common.c
  141. )
  142. set(OS_WINDOWS TRUE)
  143. elseif(APPLE)
  144. target_sources(usb PRIVATE
  145. libusb/libusb/os/darwin_usb.c
  146. )
  147. find_library(COREFOUNDATION_LIBRARY CoreFoundation)
  148. find_library(IOKIT_LIBRARY IOKit)
  149. find_library(OBJC_LIBRARY objc)
  150. target_link_libraries(usb PRIVATE
  151. ${COREFOUNDATION_LIBRARY}
  152. ${IOKIT_LIBRARY}
  153. ${OBJC_LIBRARY}
  154. )
  155. set(OS_DARWIN TRUE)
  156. elseif(ANDROID)
  157. target_sources(usb PRIVATE
  158. libusb/libusb/os/linux_usbfs.c
  159. libusb/libusb/os/linux_netlink.c
  160. )
  161. find_library(LOG_LIBRARY log)
  162. target_link_libraries(usb PRIVATE ${LOG_LIBRARY})
  163. set(OS_LINUX TRUE)
  164. elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  165. target_sources(usb PRIVATE
  166. libusb/libusb/os/linux_usbfs.c
  167. )
  168. find_package(Libudev)
  169. if(LIBUDEV_FOUND)
  170. target_sources(usb PRIVATE
  171. libusb/libusb/os/linux_udev.c
  172. )
  173. target_link_libraries(usb PRIVATE "${LIBUDEV_LIBRARIES}")
  174. target_include_directories(usb PRIVATE "${LIBUDEV_INCLUDE_DIR}")
  175. set(HAVE_LIBUDEV TRUE)
  176. set(USE_UDEV TRUE)
  177. else()
  178. target_sources(usb PRIVATE
  179. libusb/libusb/os/linux_netlink.c
  180. )
  181. endif()
  182. set(OS_LINUX TRUE)
  183. elseif(${CMAKE_SYSTEM_NAME} MATCHES "NetBSD")
  184. target_sources(usb PRIVATE
  185. libusb/libusb/os/netbsd_usb.c
  186. )
  187. set(OS_NETBSD TRUE)
  188. elseif(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
  189. target_sources(usb PRIVATE
  190. libusb/libusb/os/openbsd_usb.c
  191. )
  192. set(OS_OPENBSD TRUE)
  193. endif()
  194. if(UNIX)
  195. target_sources(usb PRIVATE
  196. libusb/libusb/os/events_posix.c
  197. libusb/libusb/os/threads_posix.c
  198. )
  199. find_package(Threads REQUIRED)
  200. if(THREADS_HAVE_PTHREAD_ARG)
  201. target_compile_options(usb PUBLIC "-pthread")
  202. endif()
  203. if(CMAKE_THREAD_LIBS_INIT)
  204. target_link_libraries(usb PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
  205. endif()
  206. set(THREADS_POSIX TRUE)
  207. elseif(WIN32)
  208. target_sources(usb PRIVATE
  209. libusb/libusb/os/events_windows.c
  210. libusb/libusb/os/threads_windows.c
  211. )
  212. endif()
  213. include(CheckFunctionExists)
  214. include(CheckIncludeFiles)
  215. include(CheckTypeSize)
  216. check_include_files(asm/types.h HAVE_ASM_TYPES_H)
  217. check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
  218. check_include_files(linux/filter.h HAVE_LINUX_FILTER_H)
  219. check_include_files(linux/netlink.h HAVE_LINUX_NETLINK_H)
  220. check_include_files(poll.h HAVE_POLL_H)
  221. check_include_files(signal.h HAVE_SIGNAL_H)
  222. check_include_files(strings.h HAVE_STRINGS_H)
  223. check_type_size("struct timespec" STRUCT_TIMESPEC)
  224. check_function_exists(syslog HAVE_SYSLOG_FUNC)
  225. check_include_files(syslog.h HAVE_SYSLOG_H)
  226. check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
  227. check_include_files(sys/time.h HAVE_SYS_TIME_H)
  228. check_include_files(sys/types.h HAVE_SYS_TYPES_H)
  229. set(CMAKE_EXTRA_INCLUDE_FILES poll.h)
  230. check_type_size("nfds_t" nfds_t)
  231. unset(CMAKE_EXTRA_INCLUDE_FILES)
  232. if(HAVE_NFDS_T)
  233. set(POLL_NFDS_TYPE "nfds_t")
  234. else()
  235. set(POLL_NFDS_TYPE "unsigned int")
  236. endif()
  237. check_include_files(sys/timerfd.h USBI_TIMERFD_AVAILABLE)
  238. configure_file(config.h.in config.h)
  239. endif() # MINGW OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux")