CMakeLists.txt 8.1 KB

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