From 4868fe2fdeb7ffa58d2b42b898b23bc889471445 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Wed, 20 Nov 2024 08:15:06 +0100 Subject: [PATCH] Export cmake config package --- CMakeLists.txt | 39 +++++++++++-------- CMakePresets.json | 46 +++++++---------------- examples/CMakeLists.txt | 30 +++++++++------ include/beman/optional26/CMakeLists.txt | 4 +- src/beman/optional26/tests/CMakeLists.txt | 7 +++- 5 files changed, 64 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b4897d..88af46a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,15 +2,18 @@ # # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.27...3.31) project(beman_optional26 VERSION 0.0.0 LANGUAGES CXX) # Includes -include(CTest) -include(FetchContent) +include(CPack) +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) -set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets) +set(TARGET_PACKAGE_NAME ${PROJECT_NAME}) +set(TARGETS_EXPORT_NAME ${TARGET_PACKAGE_NAME}-targets) +set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_PACKAGE_NAME}) option( OPTIONAL26_ENABLE_TESTING @@ -20,6 +23,8 @@ option( # Build the tests if enabled via the option OPTIONAL26_ENABLE_TESTING if(OPTIONAL26_ENABLE_TESTING) + include(FetchContent) + # Fetch GoogleTest FetchContent_Declare( googletest @@ -29,12 +34,15 @@ if(OPTIONAL26_ENABLE_TESTING) e39786088138f2749d64e9e90e0f9902daa77c40 # release-1.15.0 ) FetchContent_MakeAvailable(googletest) + + enable_testing() endif() set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON) # Create the library target and named header set for beman_optional26 add_library(beman_optional26 INTERFACE) +add_library(Beman::Optional26::beman_optional26 ALIAS beman_optional26) target_sources( beman_optional26 PUBLIC FILE_SET beman_optional26_headers TYPE HEADERS BASE_DIRS include @@ -59,23 +67,24 @@ add_subdirectory(include/beman/optional26) add_subdirectory(examples) -include(CMakePackageConfigHelpers) +# install +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) -# This will be used to replace @PACKAGE_cmakeModulesDir@ -set(cmakeModulesDir cmake/beman) configure_package_config_file( cmake/Config.cmake.in - BemanOptional26Config.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/ - PATH_VARS cmakeModulesDir - NO_SET_AND_CHECK_MACRO - NO_CHECK_REQUIRED_COMPONENTS_MACRO + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake + INSTALL_DESTINATION ${INSTALL_CONFIGDIR} ) install( - FILES ${CMAKE_CURRENT_BINARY_DIR}/BemanOptional26Config.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/ - COMPONENT beman_optional26_development + FILES + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake + DESTINATION ${INSTALL_CONFIGDIR} ) # Coverage diff --git a/CMakePresets.json b/CMakePresets.json index 79c017a..a5b9dc4 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -2,7 +2,7 @@ "version": 6, "cmakeMinimumRequired": { "major": 3, - "minor": 29, + "minor": 27, "patch": 2 }, "configurePresets": [ @@ -88,35 +88,24 @@ "configuration": "Asan", "targets": [ "all_verify_interface_header_sets", - "all" + "all", + "install" ] }, { "name": "system", "inherits": "common", - "configurePreset": "system", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "system" }, { "name": "gcc-14", "inherits": "common", - "configurePreset": "gcc-14", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "gcc-14" }, { "name": "gcc-13", "inherits": "common", - "configurePreset": "gcc-13", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "gcc-13" }, { "name": "gcc-12", @@ -131,29 +120,22 @@ { "name": "clang-19", "inherits": "common", - "configurePreset": "clang-19", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "clang-19" }, { "name": "clang-18", "inherits": "common", - "configurePreset": "clang-18", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "clang-18" }, { "name": "clang-17", "inherits": "common", - "configurePreset": "clang-17", - "targets": [ - "all_verify_interface_header_sets", - "all" - ] + "configurePreset": "clang-17" + }, + { + "name": "clang-16", + "inherits": "common", + "configurePreset": "clang-16" } ], "testPresets": [ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b94bbab..8a2af8d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,9 +3,15 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # cmake-format: on -set(BEMAN_OPTIONAL26_LIBRARY "beman_optional26") +cmake_minimum_required(VERSION 3.27...3.31) -include(GNUInstallDirs) +project(beman_optional26_example VERSION 0.0.0 LANGUAGES CXX) + +set(BEMAN_OPTIONAL26_LIBRARY "Beman::Optional26::beman_optional26") + +if(PROJECT_IS_TOP_LEVEL) + find_package(beman_optional26 0.0.0 EXACT REQUIRED) +endif() # List of all buildable examples. set(EXAMPLES @@ -27,13 +33,15 @@ foreach(example ${EXAMPLES}) # Link example with the library. target_link_libraries(${example} "${BEMAN_OPTIONAL26_LIBRARY}") - # Install . - install( - TARGETS - ${example} - COMPONENT - beman_optional26_examples - DESTINATION - ${CMAKE_INSTALL_BINDIR} - ) + if(NOT PROJECT_IS_TOP_LEVEL) + # Install . + install( + TARGETS + ${example} + COMPONENT + beman_optional26_examples + DESTINATION + ${CMAKE_INSTALL_BINDIR} + ) + endif() endforeach() diff --git a/include/beman/optional26/CMakeLists.txt b/include/beman/optional26/CMakeLists.txt index e43fc93..e4c2028 100644 --- a/include/beman/optional26/CMakeLists.txt +++ b/include/beman/optional26/CMakeLists.txt @@ -33,9 +33,9 @@ install( install( EXPORT beman_optional26_export - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/ + DESTINATION ${INSTALL_CONFIGDIR} NAMESPACE Beman::Optional26:: - FILE beman_optional26.cmake + FILE beman_optional26-config-targets.cmake EXPORT_LINK_INTERFACE_LIBRARIES COMPONENT beman_optional26_development ) diff --git a/src/beman/optional26/tests/CMakeLists.txt b/src/beman/optional26/tests/CMakeLists.txt index 5a2c1ad..f7e2142 100644 --- a/src/beman/optional26/tests/CMakeLists.txt +++ b/src/beman/optional26/tests/CMakeLists.txt @@ -29,7 +29,7 @@ target_sources( target_link_libraries( beman_optional26_test - PRIVATE beman_optional26 GTest::gtest GTest::gtest_main + PRIVATE Beman::Optional26::beman_optional26 GTest::gtest GTest::gtest_main ) # Issue #32: Re-enable ASAN run CI/clang-19. @@ -41,7 +41,10 @@ gtest_add_tests(TARGET beman_optional26_test "" AUTO) add_library(constructor_fails test_constructor_fail.cpp) -target_link_libraries(constructor_fails PRIVATE beman_optional26) +target_link_libraries( + constructor_fails + PRIVATE Beman::Optional26::beman_optional26 +) set_target_properties( constructor_fails