From fad3eb259cf795d114c5401be8e957f28bb3d872 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Sat, 22 Feb 2025 09:10:29 +0100 Subject: [PATCH] depends: libusb: build with cmake --- contrib/depends/packages/hidapi.mk | 5 + contrib/depends/packages/libusb.mk | 19 +- .../patches/hidapi/cmake-fix-libusb.patch | 20 ++ contrib/depends/patches/libusb/CMakeLists.txt | 238 ++++++++++++++++++ contrib/depends/patches/libusb/config.h.in | 75 ++++++ .../patches/libusb/fix-c11-check.patch | 22 -- 6 files changed, 344 insertions(+), 35 deletions(-) create mode 100644 contrib/depends/patches/hidapi/cmake-fix-libusb.patch create mode 100644 contrib/depends/patches/libusb/CMakeLists.txt create mode 100644 contrib/depends/patches/libusb/config.h.in delete mode 100644 contrib/depends/patches/libusb/fix-c11-check.patch diff --git a/contrib/depends/packages/hidapi.mk b/contrib/depends/packages/hidapi.mk index 54860abd..19ea1cb2 100644 --- a/contrib/depends/packages/hidapi.mk +++ b/contrib/depends/packages/hidapi.mk @@ -4,11 +4,16 @@ $(package)_download_path=https://github.com/libusb/hidapi/archive/refs/tags $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_sha256_hash=a5714234abe6e1f53647dd8cba7d69f65f71c558b7896ed218864ffcf405bcbd $(package)_linux_dependencies=libusb eudev +$(package)_patches=cmake-fix-libusb.patch define $(package)_set_vars $(package)_config_opts+=-DBUILD_SHARED_LIBS=OFF endef +define $(package)_preprocess_cmds + patch -p1 < $($(package)_patch_dir)/cmake-fix-libusb.patch +endef + define $(package)_config_cmds $($(package)_cmake) . endef diff --git a/contrib/depends/packages/libusb.mk b/contrib/depends/packages/libusb.mk index f984fa3b..65d59109 100644 --- a/contrib/depends/packages/libusb.mk +++ b/contrib/depends/packages/libusb.mk @@ -4,23 +4,20 @@ $(package)_download_path=https://github.com/libusb/libusb/archive/refs/tags $(package)_download_file=v$($(package)_version).tar.gz $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_sha256_hash=e8f18a7a36ecbb11fb820bd71540350d8f61bcd9db0d2e8c18a6fb80b214a3de -$(package)_patches=fix-c11-check.patch +$(package)_patches=CMakeLists.txt config.h.in define $(package)_preprocess_cmds - patch -p1 < $($(package)_patch_dir)/fix-c11-check.patch && \ - cp -f $(BASEDIR)/config.guess $(BASEDIR)/config.sub . && \ - autoreconf -i + cp -f $($(package)_patch_dir)/CMakeLists.txt . && \ + cp -f $($(package)_patch_dir)/config.h.in . endef define $(package)_set_vars - $(package)_config_opts=--disable-shared - $(package)_config_opts_linux=--with-pic --disable-udev - $(package)_config_opts_mingw32=--disable-udev - $(package)_config_opts_darwin=--disable-udev + $(package)_config_opts := -DBUILD_SHARED_LIBS=OFF + $(package)_config_opts_linux += -DLIBUSB_ENABLE_UDEV=OFF endef define $(package)_config_cmds - $($(package)_autoconf) + $($(package)_cmake) . endef define $(package)_build_cmd @@ -30,7 +27,3 @@ endef define $(package)_stage_cmds $(MAKE) DESTDIR=$($(package)_staging_dir) install endef - -define $(package)_postprocess_cmds - cp -f lib/libusb-1.0.a lib/libusb.a -endef diff --git a/contrib/depends/patches/hidapi/cmake-fix-libusb.patch b/contrib/depends/patches/hidapi/cmake-fix-libusb.patch new file mode 100644 index 00000000..a60edf1c --- /dev/null +++ b/contrib/depends/patches/hidapi/cmake-fix-libusb.patch @@ -0,0 +1,20 @@ +diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt +index 6cd48c4..9411468 100644 +--- a/libusb/CMakeLists.txt ++++ b/libusb/CMakeLists.txt +@@ -11,9 +11,12 @@ target_link_libraries(hidapi_libusb PUBLIC hidapi_include) + if(TARGET usb-1.0) + target_link_libraries(hidapi_libusb PRIVATE usb-1.0) + else() +- include(FindPkgConfig) +- pkg_check_modules(libusb REQUIRED IMPORTED_TARGET libusb-1.0>=1.0.9) +- target_link_libraries(hidapi_libusb PRIVATE PkgConfig::libusb) ++ find_library(libusb-1.0 usb-1.0) ++ find_path(LIBUSB_INCLUDE_DIR ++ NAMES libusb.h ++ PATH_SUFFIXES libusb-1.0) ++ target_link_libraries(hidapi_libusb PRIVATE ${libusb-1.0}) ++ include_directories(${LIBUSB_INCLUDE_DIR}) + endif() + + find_package(Threads REQUIRED) diff --git a/contrib/depends/patches/libusb/CMakeLists.txt b/contrib/depends/patches/libusb/CMakeLists.txt new file mode 100644 index 00000000..4f9d863e --- /dev/null +++ b/contrib/depends/patches/libusb/CMakeLists.txt @@ -0,0 +1,238 @@ +# From https://github.com/libusb/libusb-cmake/tree/main + +cmake_minimum_required(VERSION 3.16) + +get_filename_component(LIBUSB_ROOT "libusb" ABSOLUTE) + +# Get the version information from version.h ignoring the nano version as it appears in version_nano.h and so we need it? +file(READ "${LIBUSB_ROOT}/version.h" VERSIONHEADERDATA) +string(REGEX MATCH "#define LIBUSB_MAJOR ([0-9]*)" _ ${VERSIONHEADERDATA}) +set(LIBUSB_VERSION_MAJOR ${CMAKE_MATCH_1}) +string(REGEX MATCH "#define LIBUSB_MINOR ([0-9]*)" _ ${VERSIONHEADERDATA}) +set(LIBUSB_VERSION_MINOR ${CMAKE_MATCH_1}) +string(REGEX MATCH "#define LIBUSB_MICRO ([0-9]*)" _ ${VERSIONHEADERDATA}) +set(LIBUSB_VERSION_MICRO ${CMAKE_MATCH_1}) +set(LIBUSB_VERSION "${LIBUSB_VERSION_MAJOR}.${LIBUSB_VERSION_MINOR}.${LIBUSB_VERSION_MICRO}") + +project(usb-1.0 + DESCRIPTION "A cross-platform library to access USB devices" + VERSION ${LIBUSB_VERSION} + LANGUAGES C +) +if(EMSCRIPTEN) + set(CMAKE_CXX_STANDARD 20) + enable_language(CXX) +endif() + +# This function generates all the local variables what end up getting written to config. +# We use a function as any vars set in this context don't mess with the rest of the file. +# e.g. Logging LIBUSB_ENABLE_LOGGING mapps to ENABLE_LOGGING in the config, keeps it clean +function(generate_config_file) + include(CheckIncludeFiles) + include(CheckFunctionExists) + include(CheckSymbolExists) + include(CheckStructHasMember) + include(CheckCCompilerFlag) + + check_function_exists(clock_gettime HAVE_CLOCK_GETTIME) + check_function_exists(pthread_condattr_setclock HAVE_PTHREAD_CONDATTR_SETCLOCK) + check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP) + check_function_exists(pthread_threadid_np HAVE_PTHREAD_THREADID_NP) + check_function_exists(eventfd HAVE_EVENTFD) + check_function_exists(pipe2 HAVE_PIPE2) + check_function_exists(syslog HAVE_SYSLOG) + + check_include_files(asm/types.h HAVE_ASM_TYPES_H) + check_include_files(sys/eventfd.h HAVE_EVENTFD) + check_include_files(string.h HAVE_STRING_H) + check_include_files(sys/time.h HAVE_SYS_TIME_H) + + check_symbol_exists(timerfd_create "sys/timerfd.h" HAVE_TIMERFD) + check_symbol_exists(nfds_t "poll.h" HAVE_NFDS_T) + + check_struct_has_member("struct timespec" tv_sec time.h HAVE_STRUCT_TIMESPEC) + + if(HAVE_VISIBILITY) + set(DEFAULT_VISIBILITY "__attribute__((visibility(\"default\")))") + else() + set(DEFAULT_VISIBILITY "" ) + endif() + + # Set vars that will be written into the config file. + if(WIN32) + set(PLATFORM_WINDOWS 1) + else() + set(PLATFORM_POSIX 1) + endif() + + if(LIBUSB_ENABLE_LOGGING) + set(ENABLE_LOGGING ${LIBUSB_ENABLE_LOGGING}) + endif() + if(LIBUSB_ENABLE_DEBUG_LOGGING) + set(ENABLE_DEBUG_LOGGING ${LIBUSB_ENABLE_DEBUG_LOGGING}) + endif() + + if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU") + check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY) + endif() + + file(MAKE_DIRECTORY "${LIBUSB_GEN_INCLUDES}") + if(NOT MSVC) + set(_GNU_SOURCE TRUE) + endif() + configure_file(config.h.in "${LIBUSB_GEN_INCLUDES}/config.h" @ONLY) +endfunction() + +if(BUILD_SHARED_LIBS) + set(LIBUSB_BUILD_SHARED_LIBS_DEFAULT ON) +else() + set(LIBUSB_BUILD_SHARED_LIBS_DEFAULT OFF) +endif() + +option(LIBUSB_BUILD_SHARED_LIBS "Build Shared Libraries for libusb" ${LIBUSB_BUILD_SHARED_LIBS_DEFAULT}) +option(LIBUSB_BUILD_TESTING "Build Tests" OFF) +if(LIBUSB_BUILD_TESTING) + enable_testing() +endif() + +option(LIBUSB_BUILD_EXAMPLES "Build Example Applications" OFF) + +option(LIBUSB_INSTALL_TARGETS "Install libusb targets" ON) +option(LIBUSB_TARGETS_INCLUDE_USING_SYSTEM "Make targets include paths System" ON) + +option(LIBUSB_ENABLE_LOGGING "Enable Logging" ON) +option(LIBUSB_ENABLE_DEBUG_LOGGING "Enable Debug Logging" OFF) +if(CMAKE_SYSTEM_NAME MATCHES "Linux") + option(LIBUSB_ENABLE_UDEV "Enable udev backend for device enumeration" ON) +endif() + +set(LIBUSB_GEN_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}/gen_include") +generate_config_file() + +if(LIBUSB_BUILD_SHARED_LIBS) + add_library(usb-1.0 SHARED) +else() + add_library(usb-1.0 STATIC) +endif() + +set_target_properties(usb-1.0 PROPERTIES + PREFIX lib # to be consistent with mainline libusb build system(s) +) + +# common sources +target_sources(usb-1.0 PRIVATE + "${LIBUSB_GEN_INCLUDES}/config.h" + "${LIBUSB_ROOT}/core.c" + "${LIBUSB_ROOT}/descriptor.c" + "${LIBUSB_ROOT}/hotplug.c" + "${LIBUSB_ROOT}/io.c" + "${LIBUSB_ROOT}/libusb.h" + "${LIBUSB_ROOT}/libusbi.h" + "${LIBUSB_ROOT}/strerror.c" + "${LIBUSB_ROOT}/sync.c" + "${LIBUSB_ROOT}/version.h" + "${LIBUSB_ROOT}/version_nano.h" +) +target_include_directories(usb-1.0 + PRIVATE + "${LIBUSB_GEN_INCLUDES}" + "${LIBUSB_ROOT}/os" +) + +if (LIBUSB_TARGETS_INCLUDE_USING_SYSTEM) + target_include_directories(usb-1.0 SYSTEM PUBLIC "${LIBUSB_ROOT}") +else() + target_include_directories(usb-1.0 PUBLIC "${LIBUSB_ROOT}") +endif() + +if(WIN32) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/libusb-1.0.def" + "${LIBUSB_ROOT}/os/events_windows.c" + "${LIBUSB_ROOT}/os/events_windows.h" + "${LIBUSB_ROOT}/os/threads_windows.c" + "${LIBUSB_ROOT}/os/threads_windows.h" + "${LIBUSB_ROOT}/os/windows_common.c" + "${LIBUSB_ROOT}/os/windows_common.h" + "${LIBUSB_ROOT}/os/windows_usbdk.c" + "${LIBUSB_ROOT}/os/windows_usbdk.h" + "${LIBUSB_ROOT}/os/windows_winusb.c" + "${LIBUSB_ROOT}/os/windows_winusb.h" + $<$:${LIBUSB_ROOT}/libusb-1.0.rc> + ) + target_compile_definitions(usb-1.0 PRIVATE $<$:_CRT_SECURE_NO_WARNINGS=1>) + target_link_libraries(usb-1.0 PRIVATE windowsapp) + set_target_properties(usb-1.0 PROPERTIES UNITY_BUILD OFF) +else() + # common POSIX/non-Windows sources + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/events_posix.c" + "${LIBUSB_ROOT}/os/events_posix.h" + "${LIBUSB_ROOT}/os/threads_posix.c" + "${LIBUSB_ROOT}/os/threads_posix.h" + ) + if(CMAKE_SYSTEM_NAME MATCHES "Linux") + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/linux_usbfs.c" + "${LIBUSB_ROOT}/os/linux_usbfs.h" + ) + if(LIBUSB_ENABLE_UDEV) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/linux_udev.c" + ) + target_link_libraries(usb-1.0 PRIVATE udev) + target_compile_definitions(usb-1.0 PRIVATE HAVE_LIBUDEV=1) + else() + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/linux_netlink.c" + ) + endif() + find_package(Threads REQUIRED) + target_link_libraries(usb-1.0 PRIVATE Threads::Threads) + elseif(ANDROID) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/linux_netlink.c" + "${LIBUSB_ROOT}/os/linux_usbfs.c" + "${LIBUSB_ROOT}/os/linux_usbfs.h" + ) + target_link_libraries(usb-1.0 PRIVATE android log) + elseif(APPLE) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/darwin_usb.c" + "${LIBUSB_ROOT}/os/darwin_usb.h" + ) + target_link_libraries(usb-1.0 PRIVATE + "-framework Foundation" + "-framework IOKit" + "-framework Security" + ) + elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD") + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/netbsd_usb.c" + ) + elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/openbsd_usb.c" + ) + elseif(EMSCRIPTEN) + target_sources(usb-1.0 PRIVATE + "${LIBUSB_ROOT}/os/emscripten_webusb.cpp" + ) + target_compile_options(usb-1.0 PRIVATE -pthread) + else() + message(FATAL_ERROR "Unsupported target platform: ${CMAKE_SYSTEM_NAME}") + endif() +endif() + +if(LIBUSB_BUILD_TESTING) + add_subdirectory(tests) +endif() + +if(LIBUSB_BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + +if(LIBUSB_INSTALL_TARGETS) + install(TARGETS usb-1.0) + install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "include/libusb-1.0") +endif() diff --git a/contrib/depends/patches/libusb/config.h.in b/contrib/depends/patches/libusb/config.h.in new file mode 100644 index 00000000..2001a65c --- /dev/null +++ b/contrib/depends/patches/libusb/config.h.in @@ -0,0 +1,75 @@ +/* #define to the attribute for default visibility. */ +#define DEFAULT_VISIBILITY @DEFAULT_VISIBILITY@ + +/* #define to 1 to start with debug message logging enabled. */ +#cmakedefine ENABLE_DEBUG_LOGGING 1 + +/* #define to 1 to enable message logging. */ +#cmakedefine ENABLE_LOGGING 1 + +/* #define to 1 if you have the header file. */ +#cmakedefine HAVE_ASM_TYPES_H + +/* #define to 1 if you have the `clock_gettime' function. */ +#cmakedefine HAVE_CLOCK_GETTIME 1 + +/* #define to 1 if the system has eventfd functionality. */ +#cmakedefine HAVE_EVENTFD 1 + +/* #define to 1 if the system has the type `nfds_t'. */ +#cmakedefine HAVE_NFDS_T 1 + +/* #define to 1 if you have the `pipe2' function. */ +#cmakedefine HAVE_PIPE2 1 + +/* #define to 1 if you have the `pthread_condattr_setclock' function. */ +#cmakedefine HAVE_PTHREAD_CONDATTR_SETCLOCK 1 + +/* #define to 1 if you have the `pthread_setname_np' function. */ +#cmakedefine HAVE_PTHREAD_SETNAME_NP 1 + +/* #define to 1 if you have the `pthread_threadid_np' function. */ +#cmakedefine HAVE_PTHREAD_THREADID_NP 1 + +/* #define to 1 if you have the header file. */ +#cmakedefine HAVE_STRING_H 1 + +/* #define to 1 if the system has the type `struct timespec'. */ +#cmakedefine HAVE_STRUCT_TIMESPEC 1 + +/* #define to 1 if you have the `syslog' function. */ +#cmakedefine HAVE_SYSLOG 1 + +/* #define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_TIME_H 1 + +/* #define to 1 if the system has timerfd functionality. */ +#cmakedefine HAVE_TIMERFD 1 + +/* #define to 1 if compiling for a POSIX platform. */ +#cmakedefine PLATFORM_POSIX 1 + +/* #define to 1 if compiling for a Windows platform. */ +#cmakedefine PLATFORM_WINDOWS 1 + + +#if defined(__GNUC__) + #define PRINTF_FORMAT(a, b) __attribute__ ((format (__printf__, a, b))) +#else + #define PRINTF_FORMAT(a, b) +#endif + +/* #define to 1 if you have the ANSI C header files. */ +#cmakedefine STDC_HEADERS 1 + +/* #define to 1 to output logging messages to the systemwide log. */ +#cmakedefine USE_SYSTEM_LOGGING_FACILITY + +/* Enable GNU extensions. */ +#cmakedefine _GNU_SOURCE 1 + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +/* #undef inline */ +#endif diff --git a/contrib/depends/patches/libusb/fix-c11-check.patch b/contrib/depends/patches/libusb/fix-c11-check.patch deleted file mode 100644 index 93b00576..00000000 --- a/contrib/depends/patches/libusb/fix-c11-check.patch +++ /dev/null @@ -1,22 +0,0 @@ -diff --git a/configure.ac b/configure.ac -index 6dc7c698..eebe10a1 100644 ---- a/configure.ac -+++ b/configure.ac -@@ -46,7 +46,7 @@ dnl note that we don't just check if the compiler accepts '-std=x11' - dnl but also that it supports the _Thread_local keyword because some compilers - dnl (e.g. gcc 4.8) accept the command line option but do not implement TLS - saved_CFLAGS="${CFLAGS}" --CFLAGS="-std=gnu11" -+CFLAGS="${saved_CFLAGS} -std=gnu11" - AC_MSG_CHECKING([if $CC supports -std=gnu11]) - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])], - [AC_MSG_RESULT([yes]) -@@ -55,7 +55,7 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])], - c_dialect=]) - if test "x$c_dialect" != xgnu; then - dnl fallback check for -std=c11 compiler support (required) -- CFLAGS="-std=c11" -+ CFLAGS="${saved_CFLAGS} -std=c11" - AC_MSG_CHECKING([if $CC supports -std=c11]) - AC_COMPILE_IFELSE([AC_LANG_PROGRAM([_Thread_local int x;], [x = 42;])], - [AC_MSG_RESULT([yes])], -- 2.52.0