cmake: find libusb through LibUSB cmake module

main
Anonymous Maarten 2023-11-29 21:47:05 +01:00 committed by Sam Lantinga
parent 2a775cad6f
commit 0e6fcf947a
2 changed files with 78 additions and 8 deletions

73
cmake/FindLibUSB.cmake Normal file
View File

@ -0,0 +1,73 @@
include(FindPackageHandleStandardArgs)
set(LibUSB_PKG_CONFIG_SPEC libusb-1.0>=1.0.16)
set(LibUSB_MIN_API_VERSION 0x01000102)
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_LibUSB ${LibUSB_PKG_CONFIG_SPEC})
endif()
find_library(LibUSB_LIBRARY
NAMES usb-1.0 libusb-1.0
HINTS ${PC_LibUSB_LIBRARY_DIRS}
)
find_path(LibUSB_INCLUDE_PATH
NAMES libusb.h
PATH_SUFFIXES libusb-1.0
HINTS ${PC_LibUSB_INCLUDE_DIRS}
)
set(LibUSB_API_VERSION "LibUSB_API_VERSION-NOTFOUND")
if(LibUSB_INCLUDE_PATH AND EXISTS "${LibUSB_INCLUDE_PATH}/libusb.h")
file(READ "${LibUSB_INCLUDE_PATH}/libusb.h" LIBUSB_H_TEXT)
if("${LIBUSB_H_TEXT}" MATCHES "#define[ \t]+LIBUSBX?_API_VERSION[ \t]+(0x[0-9a-fA-F]+)" )
set(LibUSB_API_VERSION "${CMAKE_MATCH_1}")
endif()
endif()
if(LibUSB_API_VERSION)
math(EXPR LibUSB_MIN_API_VERSION_decimal "${LibUSB_MIN_API_VERSION}")
math(EXPR LibUSB_API_VERSION_decimal "${LibUSB_API_VERSION}")
if(NOT LibUSB_MIN_API_VERSION_decimal LESS_EQUAL LibUSB_API_VERSION_decimal)
set(LibUSB_LIBRARY "LibUSB_LIBRARY-NOTFOUND")
endif()
else()
set(LibUSB_LIBRARY "LibUSB_LIBRARY-NOTFOUND")
endif()
set(LibUSB_COMPILE_OPTIONS "" CACHE STRING "Extra compile options of LibUSB")
set(LibUSB_LINK_LIBRARIES "" CACHE STRING "Extra link libraries of LibUSB")
set(LibUSB_LINK_FLAGS "" CACHE STRING "Extra link flags of LibUSB")
if(LibUSB_LIBRARY AND LibUSB_INCLUDE_PATH)
if(PC_LibUSB_FOUND)
set(LibUSB_VERSION "${PC_LibUSB_VERSION}")
else()
set(LibUSB_VERSION "1.0.16-or-higher")
endif()
else()
set(LibUSB_VERSION "LibUSB_VERSION-NOTFOUND")
endif()
find_package_handle_standard_args(LibUSB
VERSION_VAR LibUSB_VERSION
REQUIRED_VARS LibUSB_LIBRARY LibUSB_INCLUDE_PATH
)
if(LibUSB_FOUND AND NOT TARGET LibUSB::LibUSB)
add_library(LibUSB::LibUSB IMPORTED UNKNOWN)
set_target_properties(LibUSB::LibUSB
PROPERTIES
IMPORTED_LOCATION "${LibUSB_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibUSB_INCLUDE_PATH}"
INTERFACE_COMPILE_OPTIONS "${LibUSB_COMPILE_OPTIONS}"
INTERFACE_LINK_LIBRARIES "${LibUSB_LINK_LIBRARIES}"
INTERFACE_LINK_OPTIONS "${LibUSB_LINK_OPTIONS}"
)
endif()

View File

@ -1039,13 +1039,10 @@ macro(CheckHIDAPI)
set(HAVE_HIDAPI ON)
if(SDL_HIDAPI_LIBUSB)
set(HAVE_LIBUSB FALSE)
set(LibUSB_PKG_CONFIG_SPEC libusb-1.0>=1.0.16)
pkg_check_modules(PC_LIBUSB IMPORTED_TARGET ${LibUSB_PKG_CONFIG_SPEC})
if(PC_LIBUSB_FOUND)
find_package(LibUSB)
if(LibUSB_FOUND)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_INCLUDES ${PC_LIBUSB_INCLUDE_DIRS})
list(APPEND CMAKE_REQUIRED_LIBRARIES PkgConfig::PC_LIBUSB)
list(APPEND CMAKE_REQUIRED_LIBRARIES LibUSB::LibUSB)
check_c_source_compiles("
#include <stddef.h>
#include <libusb.h>
@ -1060,9 +1057,9 @@ macro(CheckHIDAPI)
if(SDL_HIDAPI_LIBUSB_SHARED AND USB_1.0_LIB_SONAME)
set(HAVE_HIDAPI_LIBUSB_SHARED ON)
set(SDL_LIBUSB_DYNAMIC "\"${USB_1.0_LIB_SONAME}\"")
sdl_link_dependency(hidapi INCLUDES $<TARGET_PROPERTY:PkgConfig::PC_LIBUSB,INTERFACE_INCLUDE_DIRECTORIES>)
sdl_link_dependency(hidapi INCLUDES $<TARGET_PROPERTY:LibUSB::LibUSB,INTERFACE_INCLUDE_DIRECTORIES>)
else()
sdl_link_dependency(hidapi LIBS PkgConfig::PC_LIBUSB PKG_CONFIG_PREFIX PC_LIBUSB PKG_CONFIG_SPECS ${LibUSB_PKG_CONFIG_SPEC})
sdl_link_dependency(hidapi LIBS LibUSB::LibUSB PKG_CONFIG_SPECS "${LibUSB_PKG_CONFIG_SPEC}" CMAKE_MODULE LibUSB)
endif()
endif()
endif()