CMakeLists.txt 9.0 KB

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