Use CMakePackageConfigHelpers to generate module configuration

This commit is contained in:
Florian Albrechtskirchinger 2022-09-23 12:11:50 +02:00
parent 37453d0903
commit 3efc881192
No known key found for this signature in database
GPG Key ID: 19618CE9B2D4BE6D
2 changed files with 36 additions and 23 deletions

View File

@ -94,21 +94,33 @@ configure_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
)
# generate CMake module configuration and version files
configure_file(
include(CMakePackageConfigHelpers)
# generate CMake module configuration file
configure_package_config_file(
${JSON_CMAKE_CONFIG_TEMPLATE}
${JSON_CMAKE_PROJECT_CONFIG_FILE}
@ONLY
INSTALL_DESTINATION ${JSON_CONFIG_INSTALL_DIR}
NO_SET_AND_CHECK_MACRO
)
# use a custom package version config file instead of
# write_basic_package_version_file to ensure that it's architecture-independent
# https://github.com/nlohmann/json/issues/1697
configure_file(
"cmake/nlohmann_jsonConfigVersion.cmake.in"
${JSON_CMAKE_VERSION_CONFIG_FILE}
@ONLY
)
# generate CMake module version file
if(CMAKE_VERSION VERSION_EQUAL "3.14" OR CMAKE_VERSION VERSION_GREATER "3.14")
write_basic_package_version_file(
${JSON_CMAKE_VERSION_CONFIG_FILE}
COMPATIBILITY SameMajorVersion
ARCH_INDEPENDENT
)
else()
# use a custom package version config file instead of
# write_basic_package_version_file to ensure that it's architecture-independent
# https://github.com/nlohmann/json/issues/1697
configure_file(
"cmake/nlohmann_jsonConfigVersion.cmake.in"
${JSON_CMAKE_VERSION_CONFIG_FILE}
@ONLY
)
endif()
#############################################################################
# install files and targets

View File

@ -1,15 +1,16 @@
include(FindPackageHandleStandardArgs)
set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG ${CMAKE_CURRENT_LIST_FILE})
find_package_handle_standard_args(@PROJECT_NAME@ CONFIG_MODE)
@PACKAGE_INIT@
if(NOT TARGET @PROJECT_NAME@::@JSON_TARGET_NAME@)
include("${CMAKE_CURRENT_LIST_DIR}/@JSON_TARGETS_EXPORT_NAME@.cmake")
if((NOT TARGET @JSON_TARGET_NAME@) AND
(NOT @PROJECT_NAME@_FIND_VERSION OR
@PROJECT_NAME@_FIND_VERSION VERSION_LESS 3.2.0))
add_library(@JSON_TARGET_NAME@ INTERFACE IMPORTED)
set_target_properties(@JSON_TARGET_NAME@ PROPERTIES
INTERFACE_LINK_LIBRARIES @PROJECT_NAME@::@JSON_TARGET_NAME@
)
include(FindPackageHandleStandardArgs)
set(nlohmann_json_CONFIG ${CMAKE_CURRENT_LIST_FILE})
find_package_handle_standard_args(nlohmann_json CONFIG_MODE)
if(NOT TARGET nlohmann_json::nlohmann_json)
include("${CMAKE_CURRENT_LIST_DIR}/nlohmann_jsonTargets.cmake")
if((NOT TARGET nlohmann_json)
AND (NOT nlohmann_json_FIND_VERSION OR nlohmann_json_FIND_VERSION VERSION_LESS 3.2.0))
add_library(nlohmann_json INTERFACE IMPORTED)
set_target_properties(nlohmann_json PROPERTIES INTERFACE_LINK_LIBRARIES nlohmann_json::nlohmann_json)
endif()
endif()
check_required_components(nlohmann_json)