From 395fb176c5438e09a3291df025dd2e5e07122c30 Mon Sep 17 00:00:00 2001 From: Gregor Weiss Date: Mon, 22 Jan 2024 15:28:51 +0100 Subject: [PATCH] config-file packaging for find_package() in linking CMake projects --- CMakeLists.txt | 26 +++++++++++++++++++++++++- bwc-config-version.cmake.in | 16 ++++++++++++++++ bwc-config.cmake.in | 20 ++++++++++++++++++++ src/library/CMakeLists.txt | 8 ++++++-- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 bwc-config-version.cmake.in create mode 100644 bwc-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 64b7c50..0579efe 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,13 @@ if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${BWC_SOURCE_DIR}/${CMAKE_INSTALL_LIBDIR}) endif() +#*--------------------------------------------------------*# +# Set the target installation directory of the config-file # +# packaging configuration files. # +#*--------------------------------------------------------*# +set(CMAKE_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/bwc + CACHE PATH "Installation directory for config-file package cmake files") + #*--------------------------------------------------------*# # Suggest the C standard (C99) used by the compiler. # #*--------------------------------------------------------*# @@ -181,4 +188,21 @@ add_subdirectory(src/library) #*--------------------------------------------------------*# if(${TOOL}) add_subdirectory(src/tools) -endif() \ No newline at end of file +endif() + +#*--------------------------------------------------------*# +# Config-file packaging # +#*--------------------------------------------------------*# +export(TARGETS bwclib NAMESPACE bwc:: FILE "bwc-targets.cmake") +configure_file(${PROJECT_SOURCE_DIR}/bwc-config.cmake.in + "${PROJECT_BINARY_DIR}/bwc-config.cmake" @ONLY) +configure_file(${PROJECT_SOURCE_DIR}/bwc-config-version.cmake.in + "${PROJECT_BINARY_DIR}/bwc-config-version.cmake" @ONLY) + +install( FILES "${PROJECT_BINARY_DIR}/bwc-config.cmake" + "${PROJECT_BINARY_DIR}/bwc-config-version.cmake" + DESTINATION ${CMAKE_INSTALL_CMAKEDIR}) + +install( EXPORT bwc-targets + NAMESPACE bwc:: + DESTINATION ${CMAKE_INSTALL_CMAKEDIR}) \ No newline at end of file diff --git a/bwc-config-version.cmake.in b/bwc-config-version.cmake.in new file mode 100644 index 0000000..941b2f0 --- /dev/null +++ b/bwc-config-version.cmake.in @@ -0,0 +1,16 @@ +set(PACKAGE_VERSION_MAJOR @BWC_VERSION_MAJOR@) +set(PACKAGE_VERSION_MINOR @BWC_VERSION_MINOR@) +set(PACKAGE_VERSION_PATCH @BWC_VERSION_PATCH@) + +set(PACKAGE_VERSION @BWC_VERSION@) + +# Check whether the requested PACKAGE_FIND_VERSION is compatible +if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION OR + PACKAGE_VERSION_MAJOR GREATER PACKAGE_FIND_VERSION_MAJOR) + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if(PACKAGE_VERSION VERSION_EQUAL PACKAGE_FIND_VERSION) + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif() \ No newline at end of file diff --git a/bwc-config.cmake.in b/bwc-config.cmake.in new file mode 100644 index 0000000..934d6ee --- /dev/null +++ b/bwc-config.cmake.in @@ -0,0 +1,20 @@ +# - Config file for the bwc package +# +# It defines the following variables +# BWC_INCLUDE_DIRS - include directories for bwc +# BWC_LIBRARIES - libraries to link against +# +# And the following imported targets: +# bwc::bwclib + +include("${CMAKE_CURRENT_LIST_DIR}/bwc-config-version.cmake") + +include(FindPackageHandleStandardArgs) +set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}") +find_package_handle_standard_args(${CMAKE_FIND_PACKAGE_NAME} CONFIG_MODE) + +if(NOT TARGET bwc::bwclib) + include("${CMAKE_CURRENT_LIST_DIR}/bwc-targets.cmake") + set(BWC_LIBRARIES "bwc::bwclib") + get_target_property(BWC_INCLUDE_DIRS bwc::bwclib INTERFACE_INCLUDE_DIRECTORIES) +endif() diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 02780e5..67a0083 100755 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -107,7 +107,9 @@ set_target_properties(bwclib PROPERTIES VERSION ${BWC_VERSION} #*--------------------------------------------------------*# # Setup up the include directory for the BigWhoop library. # #*--------------------------------------------------------*# -target_include_directories(bwclib PRIVATE ${CMAKE_SOURCE_DIR}/include/library/private) +target_include_directories(bwclib PRIVATE ${CMAKE_SOURCE_DIR}/include/library/private + PUBLIC $ + $) #*--------------------------------------------------------*# # Link the BigWhoop library to the math.h library. # @@ -115,9 +117,11 @@ target_include_directories(bwclib PRIVATE ${CMAKE_SOURCE_DIR}/include/library/pr target_link_libraries(bwclib PRIVATE m) #*--------------------------------------------------------*# -# Setup the install directories. # +# Setup the install directories and target exporting for # +# config-file packaging. # #*--------------------------------------------------------*# install( TARGETS bwclib + EXPORT bwc-targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})