CMakeLists.txt 8.5 KB

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