From 5573eea1e60952484e4d11a01e15a468ca2b5b9f Mon Sep 17 00:00:00 2001 From: ririyeye <200610237@qq.com> Date: Mon, 29 Sep 2025 14:11:11 +0800 Subject: [PATCH] add `LIBUSB_ENABLE_WINDOWS_HOTPLUG` option in windows --- CMakeLists.txt | 15 +++++++++++++++ config.h.in | 3 +++ 2 files changed, 18 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5464724f..aadb5b6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,10 @@ function(generate_config_file) set(ENABLE_DEBUG_LOGGING ${LIBUSB_ENABLE_DEBUG_LOGGING}) endif() + if(WIN32 AND LIBUSB_ENABLE_WINDOWS_HOTPLUG) + set(LIBUSB_WINDOWS_HOTPLUG 1) + endif() + if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY) endif() @@ -104,6 +108,10 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux") option(LIBUSB_ENABLE_UDEV "Enable udev backend for device enumeration" ON) endif() +if(WIN32) + option(LIBUSB_ENABLE_WINDOWS_HOTPLUG "Enable Windows hotplug support" OFF) +endif() + set(LIBUSB_GEN_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}/gen_include") generate_config_file() @@ -160,6 +168,13 @@ if(WIN32) ) target_compile_definitions(usb-1.0 PRIVATE $<$:_CRT_SECURE_NO_WARNINGS=1>) target_link_libraries(usb-1.0 PRIVATE windowsapp) + if(LIBUSB_ENABLE_WINDOWS_HOTPLUG) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/windows_hotplug.c" + "${LIBUSB_ROOT}/os/windows_hotplug.h" + ) + target_compile_definitions(usb-1.0 PRIVATE LIBUSB_WINDOWS_HOTPLUG=1) + endif() set_target_properties(usb-1.0 PROPERTIES UNITY_BUILD OFF) else() # common POSIX/non-Windows sources diff --git a/config.h.in b/config.h.in index 2001a65c..2300ff23 100644 --- a/config.h.in +++ b/config.h.in @@ -52,6 +52,9 @@ /* #define to 1 if compiling for a Windows platform. */ #cmakedefine PLATFORM_WINDOWS 1 +/* #define to 1 to enable Windows hotplug support. */ +#cmakedefine LIBUSB_WINDOWS_HOTPLUG 1 + #if defined(__GNUC__) #define PRINTF_FORMAT(a, b) __attribute__ ((format (__printf__, a, b)))