2021-06-03 00:49:35 -07:00
set ( LIBUSB_FOUND ON CACHE BOOL "libusb is present" FORCE )
set ( LIBUSB_VERSION "1.0.24" CACHE STRING "libusb version string" FORCE )
if ( MINGW OR ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" ) )
2021-06-02 23:49:53 -07:00
# GNU toolchains for some reason doesn't work with the later half of this CMakeLists after
# updating to 1.0.24, so we do it the old-fashioned way for now.
2021-05-30 19:26:06 -07:00
set ( LIBUSB_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/libusb" )
set ( LIBUSB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libusb" )
2021-06-02 23:49:53 -07:00
2021-05-30 19:26:06 -07:00
# Workarounds for MSYS/MinGW
if ( MSYS )
# CMake on Windows passes `C:/`, but we need `/C/` or `/c/` to use `configure`
string ( REPLACE ":/" "/" LIBUSB_SRC_DIR "${LIBUSB_SRC_DIR}" )
set ( LIBUSB_SRC_DIR "/${LIBUSB_SRC_DIR}" )
2020-08-25 19:21:32 -07:00
2021-05-30 19:26:06 -07:00
# And now that we are using /C/ for srcdir but everything else is using C:/, we need to
# compile everything in the source directory, else `configure` won't think the build
# environment is sane.
set ( LIBUSB_PREFIX "${LIBUSB_SRC_DIR}" )
2020-08-25 19:21:32 -07:00
endif ( )
2020-09-02 16:30:20 -07:00
2021-05-30 19:26:06 -07:00
set ( LIBUSB_CONFIGURE "${LIBUSB_SRC_DIR}/configure" )
set ( LIBUSB_MAKEFILE "${LIBUSB_PREFIX}/Makefile" )
2021-06-02 23:49:53 -07:00
if ( MINGW )
2021-06-03 00:49:35 -07:00
set ( LIBUSB_LIBRARIES "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.dll.a" CACHE PATH "libusb library path" FORCE )
2021-06-02 23:49:53 -07:00
set ( LIBUSB_SHARED_LIBRARY "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.dll" )
set ( LIBUSB_SHARED_LIBRARY_DEST "${CMAKE_BINARY_DIR}/bin/libusb-1.0.dll" )
set ( LIBUSB_CONFIGURE_ARGS --host=x86_64-w64-mingw32 --build=x86_64-windows )
else ( )
2021-06-03 01:38:29 -07:00
set ( LIBUSB_LIBRARIES "${LIBUSB_PREFIX}/libusb/.libs/libusb-1.0.a" CACHE PATH "libusb library path" FORCE )
2021-06-02 23:49:53 -07:00
endif ( )
2021-05-30 19:26:06 -07:00
2021-06-03 00:49:35 -07:00
set ( LIBUSB_INCLUDE_DIRS "${LIBUSB_SRC_DIR}/libusb" CACHE PATH "libusb headers path" FORCE )
2021-06-03 01:38:29 -07:00
# 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.
if ( NOT MINGW )
set ( LIBUSB_CFLAGS "-DGUID_DEVINTERFACE_USB_DEVICE=\\(GUID\\){0xA5DCBF10,0x6530,0x11D2,{0x90,0x1F,0x00,0xC0,0x4F,0xB9,0x51,0xED}}" )
endif ( )
2021-05-30 19:26:06 -07:00
make_directory ( "${LIBUSB_PREFIX}" )
add_custom_command (
O U T P U T
2021-06-03 00:49:35 -07:00
" $ { L I B U S B _ L I B R A R I E S } "
2021-05-30 19:26:06 -07:00
C O M M A N D
m a k e
W O R K I N G _ D I R E C T O R Y
" $ { L I B U S B _ P R E F I X } "
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
add_custom_command (
O U T P U T
" $ { L I B U S B _ M A K E F I L E } "
C O M M A N D
2021-06-03 01:53:01 -07:00
e n v
2021-06-03 01:38:29 -07:00
C F L A G S = " $ { L I B U S B _ C F L A G S } "
2021-06-03 01:53:01 -07:00
s h " $ { L I B U S B _ C O N F I G U R E } "
2021-05-30 19:26:06 -07:00
$ { L I B U S B _ C O N F I G U R E _ A R G S }
- - s r c d i r = " $ { L I B U S B _ S R C _ D I R } "
W O R K I N G _ D I R E C T O R Y
" $ { L I B U S B _ P R E F I X } "
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
add_custom_command (
O U T P U T
" $ { L I B U S B _ C O N F I G U R E } "
C O M M A N D
2021-06-03 01:53:01 -07:00
s h " $ { L I B U S B _ S R C _ D I R } / b o o t s t r a p . s h "
2021-05-30 19:26:06 -07:00
W O R K I N G _ D I R E C T O R Y
" $ { L I B U S B _ S R C _ D I R } "
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
add_custom_command (
O U T P U T
" $ { L I B U S B _ S H A R E D _ L I B R A R Y _ D E S T } "
C O M M A N D
2021-06-03 01:53:01 -07:00
c p " $ { L I B U S B _ S H A R E D _ L I B R A R Y } " " $ { L I B U S B _ S H A R E D _ L I B R A R Y _ D E S T } "
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
2021-06-02 23:49:53 -07:00
add_custom_target ( usb-bootstrap DEPENDS "${LIBUSB_CONFIGURE}" )
add_custom_target ( usb-configure DEPENDS "${LIBUSB_MAKEFILE}" usb-bootstrap )
2021-06-03 00:49:35 -07:00
add_custom_target ( usb-build ALL DEPENDS "${LIBUSB_LIBRARIES}" usb-configure )
2021-05-30 19:26:06 -07:00
# Workaround since static linking didn't work out -- We need to copy the DLL to the bin directory
add_custom_target ( usb-copy ALL DEPENDS "${LIBUSB_SHARED_LIBRARY_DEST}" usb-build )
add_library ( usb INTERFACE )
2021-06-02 23:49:53 -07:00
add_dependencies ( usb usb-copy )
2021-06-03 00:49:35 -07:00
target_link_libraries ( usb INTERFACE "${LIBUSB_LIBRARIES}" )
target_include_directories ( usb INTERFACE "${LIBUSB_INCLUDE_DIRS}" )
2021-06-02 23:49:53 -07:00
if ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
2021-06-03 01:38:29 -07:00
Include ( FindPkgConfig )
pkg_check_modules ( LIBUDEV REQUIRED libudev )
if ( LIBUDEV_FOUND )
target_include_directories ( usb INTERFACE "${LIBUDEV_INCLUDE_DIRS}" )
target_link_libraries ( usb INTERFACE "${LIBUDEV_STATIC_LIBRARIES}" )
2021-06-02 23:49:53 -07:00
endif ( )
endif ( )
2021-06-03 00:49:35 -07:00
else ( ) # MINGW OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
2021-05-30 19:26:06 -07:00
# Ensure libusb compiles with UTF-8 encoding on MSVC
if ( MSVC )
add_compile_options ( /utf-8 )
endif ( )
add_library ( usb STATIC EXCLUDE_FROM_ALL
l i b u s b / l i b u s b / c o r e . c
l i b u s b / l i b u s b / c o r e . c
l i b u s b / l i b u s b / d e s c r i p t o r . c
l i b u s b / l i b u s b / h o t p l u g . c
l i b u s b / l i b u s b / i o . c
l i b u s b / l i b u s b / s t r e r r o r . c
l i b u s b / l i b u s b / s y n c . c
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
set_target_properties ( usb PROPERTIES VERSION 1.0.24 )
if ( WIN32 )
target_include_directories ( usb
B E F O R E
P U B L I C
l i b u s b / l i b u s b
P R I V A T E
" $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } "
2020-08-25 19:21:32 -07:00
)
2021-05-30 19:26:06 -07:00
if ( NOT MINGW )
target_include_directories ( usb BEFORE PRIVATE libusb/msvc )
endif ( )
# Works around other libraries providing their own definition of USB GUIDs (e.g. SDL2)
target_compile_definitions ( usb PRIVATE "-DGUID_DEVINTERFACE_USB_DEVICE=(GUID){ 0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}}" )
2020-08-25 19:21:32 -07:00
else ( )
2021-05-30 19:26:06 -07:00
target_include_directories ( usb
# turns out other projects also have "config.h", so make sure the
# LibUSB one comes first
B E F O R E
P U B L I C
l i b u s b / l i b u s b
P R I V A T E
" $ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } "
)
endif ( )
if ( WIN32 OR CYGWIN )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / t h r e a d s _ w i n d o w s . c
l i b u s b / l i b u s b / o s / w i n d o w s _ w i n u s b . c
l i b u s b / l i b u s b / o s / w i n d o w s _ u s b d k . c
l i b u s b / l i b u s b / o s / w i n d o w s _ c o m m o n . c
)
set ( OS_WINDOWS TRUE )
elseif ( APPLE )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / d a r w i n _ u s b . c
)
find_library ( COREFOUNDATION_LIBRARY CoreFoundation )
find_library ( IOKIT_LIBRARY IOKit )
find_library ( OBJC_LIBRARY objc )
target_link_libraries ( usb PRIVATE
$ { C O R E F O U N D A T I O N _ L I B R A R Y }
$ { I O K I T _ L I B R A R Y }
$ { O B J C _ L I B R A R Y }
)
set ( OS_DARWIN TRUE )
elseif ( ANDROID )
2020-08-25 19:21:32 -07:00
target_sources ( usb PRIVATE
2021-05-30 19:26:06 -07:00
l i b u s b / l i b u s b / o s / l i n u x _ u s b f s . c
2020-08-25 19:21:32 -07:00
l i b u s b / l i b u s b / o s / l i n u x _ n e t l i n k . c
)
2021-05-30 19:26:06 -07:00
find_library ( LOG_LIBRARY log )
target_link_libraries ( usb PRIVATE ${ LOG_LIBRARY } )
set ( OS_LINUX TRUE )
elseif ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / l i n u x _ u s b f s . c
)
find_package ( Libudev )
if ( LIBUDEV_FOUND )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / l i n u x _ u d e v . c
)
target_link_libraries ( usb PRIVATE "${LIBUDEV_LIBRARIES}" )
target_include_directories ( usb PRIVATE "${LIBUDEV_INCLUDE_DIR}" )
set ( HAVE_LIBUDEV TRUE )
set ( USE_UDEV TRUE )
else ( )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / l i n u x _ n e t l i n k . c
)
endif ( )
set ( OS_LINUX TRUE )
elseif ( ${ CMAKE_SYSTEM_NAME } MATCHES "NetBSD" )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / n e t b s d _ u s b . c
)
set ( OS_NETBSD TRUE )
elseif ( ${ CMAKE_SYSTEM_NAME } MATCHES "OpenBSD" )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / o p e n b s d _ u s b . c
)
set ( OS_OPENBSD TRUE )
2020-08-25 19:21:32 -07:00
endif ( )
2021-05-30 19:26:06 -07:00
if ( UNIX )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / e v e n t s _ p o s i x . c
l i b u s b / l i b u s b / o s / t h r e a d s _ p o s i x . c
)
find_package ( Threads REQUIRED )
if ( THREADS_HAVE_PTHREAD_ARG )
target_compile_options ( usb PUBLIC "-pthread" )
endif ( )
if ( CMAKE_THREAD_LIBS_INIT )
target_link_libraries ( usb PRIVATE "${CMAKE_THREAD_LIBS_INIT}" )
endif ( )
set ( THREADS_POSIX TRUE )
elseif ( WIN32 )
target_sources ( usb PRIVATE
l i b u s b / l i b u s b / o s / e v e n t s _ w i n d o w s . c
l i b u s b / l i b u s b / o s / t h r e a d s _ w i n d o w s . c
)
2020-08-25 19:21:32 -07:00
endif ( )
2021-05-30 19:26:06 -07:00
include ( CheckFunctionExists )
include ( CheckIncludeFiles )
include ( CheckTypeSize )
check_include_files ( asm/types.h HAVE_ASM_TYPES_H )
check_function_exists ( gettimeofday HAVE_GETTIMEOFDAY )
check_include_files ( linux/filter.h HAVE_LINUX_FILTER_H )
check_include_files ( linux/netlink.h HAVE_LINUX_NETLINK_H )
check_include_files ( poll.h HAVE_POLL_H )
check_include_files ( signal.h HAVE_SIGNAL_H )
check_include_files ( strings.h HAVE_STRINGS_H )
check_type_size ( "struct timespec" STRUCT_TIMESPEC )
check_function_exists ( syslog HAVE_SYSLOG_FUNC )
check_include_files ( syslog.h HAVE_SYSLOG_H )
check_include_files ( sys/socket.h HAVE_SYS_SOCKET_H )
check_include_files ( sys/time.h HAVE_SYS_TIME_H )
check_include_files ( sys/types.h HAVE_SYS_TYPES_H )
set ( CMAKE_EXTRA_INCLUDE_FILES poll.h )
check_type_size ( "nfds_t" nfds_t )
unset ( CMAKE_EXTRA_INCLUDE_FILES )
if ( HAVE_NFDS_T )
set ( POLL_NFDS_TYPE "nfds_t" )
else ( )
set ( POLL_NFDS_TYPE "unsigned int" )
2020-08-25 19:21:32 -07:00
endif ( )
2021-05-30 19:26:06 -07:00
check_include_files ( sys/timerfd.h USBI_TIMERFD_AVAILABLE )
configure_file ( config.h.in config.h )
2021-06-03 00:49:35 -07:00
endif ( ) # MINGW OR (${CMAKE_SYSTEM_NAME} MATCHES "Linux")