CMakeLists.txt 9.5 KB

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