config-file packaging for find_package() in linking CMake projects

This commit is contained in:
Gregor Weiss 2024-01-22 15:28:51 +01:00
parent 9d85a8c8e2
commit 395fb176c5
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756
4 changed files with 67 additions and 3 deletions

View file

@ -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()
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})

View file

@ -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()

20
bwc-config.cmake.in Normal file
View file

@ -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()

View file

@ -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 $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include/library/public>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
#*--------------------------------------------------------*#
# 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})