From 9bc98383b1fc7371d681c966e7af2e06ffe07f17 Mon Sep 17 00:00:00 2001 From: Andreas Michelis Date: Thu, 24 Jul 2025 22:21:17 +0300 Subject: [PATCH 1/7] Added target export capabilities and solidified installation procedure --- CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5464724f..29fd79e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,9 @@ cmake_minimum_required(VERSION 3.16) +# To Auto-determine the installation suffixes +enable_language(C) +include(GNUInstallDirs) + get_filename_component(LIBUSB_ROOT "libusb/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? @@ -138,9 +142,17 @@ target_include_directories(usb-1.0 ) if (LIBUSB_TARGETS_INCLUDE_USING_SYSTEM) - target_include_directories(usb-1.0 SYSTEM PUBLIC "${LIBUSB_ROOT}") + target_include_directories(usb-1.0 + SYSTEM PUBLIC + $ + $ + ) else() - target_include_directories(usb-1.0 PUBLIC "${LIBUSB_ROOT}") + target_include_directories(usb-1.0 + PUBLIC + $ + $ + ) endif() if(WIN32) @@ -230,7 +242,33 @@ 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() +if (LIBUSB_INSTALL_TARGETS) + if (MSVC) + set(LIBUSB_INSTALL_LIBRARY_DIR ${CMAKE_INSTALL_BINDIR}) + else () + set(LIBUSB_INSTALL_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR}) + endif () + install(TARGETS usb-1.0 + EXPORT usb-1.0-targets + LIBRARY DESTINATION ${LIBUSB_INSTALL_LIBRARY_DIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) + install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libusb-1.0") + install(EXPORT usb-1.0-targets + FILE libusb-targets.cmake + NAMESPACE libusb:: + DESTINATION lib/cmake/libusb + ) + + include(CMakePackageConfigHelpers) + write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" + VERSION ${LIBUSB_VERSION} + COMPATIBILITY SameMajorVersion + ) + install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" + DESTINATION lib/cmake/libusb + ) +endif () + From c3d9a3b3dd633229e0526be9d825f3680b703298 Mon Sep 17 00:00:00 2001 From: Andreas Michelis Date: Thu, 24 Jul 2025 22:33:27 +0300 Subject: [PATCH 2/7] Minor bugfix on Version exporting --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29fd79e0..43848fd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,7 +268,7 @@ if (LIBUSB_INSTALL_TARGETS) ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" - DESTINATION lib/cmake/libusb + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libusb" ) endif () From 2ea7510b57f03538eb3fde649450a95e7f8f945d Mon Sep 17 00:00:00 2001 From: Andreas Michelis Date: Sat, 26 Jul 2025 14:14:54 +0300 Subject: [PATCH 3/7] Added runtime install destination to avoid inconsistencies with different CMake versions --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43848fd7..b86f87b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -250,6 +250,7 @@ if (LIBUSB_INSTALL_TARGETS) endif () install(TARGETS usb-1.0 EXPORT usb-1.0-targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${LIBUSB_INSTALL_LIBRARY_DIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) From 735f118c60bda4cdb7ea05922da3f1bdcf4c1bfa Mon Sep 17 00:00:00 2001 From: Andreas Michelis Date: Sun, 27 Jul 2025 17:47:49 +0300 Subject: [PATCH 4/7] Moved `GNUInstallDirs` inclusion after `project` to avoid using `enable_language`. --- CMakeLists.txt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b86f87b7..2d098501 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,5 @@ cmake_minimum_required(VERSION 3.16) -# To Auto-determine the installation suffixes -enable_language(C) -include(GNUInstallDirs) - get_filename_component(LIBUSB_ROOT "libusb/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? @@ -26,6 +22,9 @@ if(EMSCRIPTEN) enable_language(CXX) endif() +# To Auto-determine the installation suffixes +include(GNUInstallDirs) + # 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 From 87c195764e1b847dbbdcd5a8508960a95756b5ae Mon Sep 17 00:00:00 2001 From: Andreas Michelis Date: Wed, 30 Jul 2025 22:25:37 +0300 Subject: [PATCH 5/7] Removed all `GNUInstallDirs`-related works (including MSVC differentiation) --- CMakeLists.txt | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d098501..0957c6d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,9 +22,6 @@ if(EMSCRIPTEN) enable_language(CXX) endif() -# To Auto-determine the installation suffixes -include(GNUInstallDirs) - # 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 @@ -242,18 +239,12 @@ if(LIBUSB_BUILD_EXAMPLES) endif() if (LIBUSB_INSTALL_TARGETS) - if (MSVC) - set(LIBUSB_INSTALL_LIBRARY_DIR ${CMAKE_INSTALL_BINDIR}) - else () - set(LIBUSB_INSTALL_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR}) - endif () install(TARGETS usb-1.0 EXPORT usb-1.0-targets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${LIBUSB_INSTALL_LIBRARY_DIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) - install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libusb-1.0") + + install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "include/libusb-1.0") + install(EXPORT usb-1.0-targets FILE libusb-targets.cmake NAMESPACE libusb:: @@ -261,14 +252,15 @@ if (LIBUSB_INSTALL_TARGETS) ) include(CMakePackageConfigHelpers) + write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" VERSION ${LIBUSB_VERSION} COMPATIBILITY SameMajorVersion ) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libusb" + DESTINATION "lib/cmake/libusb" ) -endif () - +endif() From 6d1b81ba18c36704241bdeeaed3a08c0a8d96c4a Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sun, 19 Oct 2025 13:44:55 +0300 Subject: [PATCH 6/7] refactor --- CMakeLists.txt | 72 +++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f53b1aaa..a4bdc770 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,8 @@ else() set(LIBUSB_BUILD_SHARED_LIBS_DEFAULT OFF) endif() +include(CMakeDependentOption) + 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) @@ -102,6 +104,8 @@ endif() option(LIBUSB_BUILD_EXAMPLES "Build Example Applications" OFF) option(LIBUSB_INSTALL_TARGETS "Install libusb targets" ON) +cmake_dependent_option(LIBUSB_EXPORT_INSTALL_TARGETS "Generate export files for installed targets" ON LIBUSB_INSTALL_TARGETS OFF) + option(LIBUSB_TARGETS_INCLUDE_USING_SYSTEM "Make targets include paths System" ON) option(LIBUSB_ENABLE_LOGGING "Enable Logging" ON) @@ -114,9 +118,20 @@ if(WIN32) option(LIBUSB_ENABLE_WINDOWS_HOTPLUG "Enable Windows hotplug support" OFF) endif() +# + +if(LIBUSB_INSTALL_TARGETS) + # to make sure all variables, like CMAKE_INSTALL_LIBDIR, are initialized + include(GNUInstallDirs) +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() @@ -147,20 +162,18 @@ target_include_directories(usb-1.0 "${LIBUSB_ROOT}/os" ) -if (LIBUSB_TARGETS_INCLUDE_USING_SYSTEM) - target_include_directories(usb-1.0 - SYSTEM PUBLIC - $ - $ - ) +if(LIBUSB_TARGETS_INCLUDE_USING_SYSTEM) + set(LIBUSB_SYSTEM_FLAGS SYSTEM) else() - target_include_directories(usb-1.0 - PUBLIC - $ - $ - ) + set(LIBUSB_SYSTEM_FLAGS) endif() +target_include_directories(usb-1.0 + ${LIBUSB_SYSTEM_FLAGS} PUBLIC + $ + $ +) + if(WIN32) target_sources(usb-1.0 PRIVATE "${LIBUSB_ROOT}/libusb-1.0.def" @@ -255,29 +268,34 @@ if(LIBUSB_BUILD_EXAMPLES) add_subdirectory(examples) endif() -if (LIBUSB_INSTALL_TARGETS) +if(LIBUSB_INSTALL_TARGETS) + # to make sure all variables, like CMAKE_INSTALL_LIBDIR, are initialized + include(GNUInstallDirs) + + install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libusb-1.0") + install(TARGETS usb-1.0 - EXPORT usb-1.0-targets + EXPORT usb-1.0-targets ) - install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "include/libusb-1.0") - - install(EXPORT usb-1.0-targets - FILE libusb-targets.cmake + if(LIBUSB_EXPORT_INSTALL_TARGETS) + install(EXPORT usb-1.0-targets + FILE usb-1.0-targets.cmake NAMESPACE libusb:: - DESTINATION lib/cmake/libusb - ) + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libusb" + ) - include(CMakePackageConfigHelpers) + include(CMakePackageConfigHelpers) - write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" + write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/libusb-config-version.cmake" VERSION ${LIBUSB_VERSION} COMPATIBILITY SameMajorVersion - ) + ) - install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/libusbConfigVersion.cmake" - DESTINATION "lib/cmake/libusb" - ) + install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/libusb-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libusb" + ) + endif() endif() From c5ef33d4f0c359a8ed394286084b89ff4e32d6db Mon Sep 17 00:00:00 2001 From: Ihor Dutchak Date: Sun, 19 Oct 2025 13:51:37 +0300 Subject: [PATCH 7/7] cleanup --- CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4bdc770..8acc84cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,9 +269,6 @@ if(LIBUSB_BUILD_EXAMPLES) endif() if(LIBUSB_INSTALL_TARGETS) - # to make sure all variables, like CMAKE_INSTALL_LIBDIR, are initialized - include(GNUInstallDirs) - install(FILES "${LIBUSB_ROOT}/libusb.h" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libusb-1.0") install(TARGETS usb-1.0