SDL: cmake: find libusb through LibUSB cmake module

From 0e6fcf947a7d980f669e520e36dd3880c20e2637 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 29 Nov 2023 21:47:05 +0100
Subject: [PATCH] cmake: find libusb through LibUSB cmake module

---
 cmake/FindLibUSB.cmake | 73 ++++++++++++++++++++++++++++++++++++++++++
 cmake/sdlchecks.cmake  | 13 +++-----
 2 files changed, 78 insertions(+), 8 deletions(-)
 create mode 100644 cmake/FindLibUSB.cmake

diff --git a/cmake/FindLibUSB.cmake b/cmake/FindLibUSB.cmake
new file mode 100644
index 0000000000000..2488735022186
--- /dev/null
+++ b/cmake/FindLibUSB.cmake
@@ -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()
+
diff --git a/cmake/sdlchecks.cmake b/cmake/sdlchecks.cmake
index 7fae2524b4e17..eb9906bb82c50 100644
--- a/cmake/sdlchecks.cmake
+++ b/cmake/sdlchecks.cmake
@@ -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()