From 464f37b114addd757c6260850d7c4e0ec65c850a Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 25 Nov 2024 09:41:38 +0100 Subject: [PATCH] Install cmake config package Add find-package-test Prevent undefined cmake values --- CMakeLists.txt | 38 +++++++++++++++++++++-- cmake/config.cmake.in | 7 +++++ examples/CMakeLists.txt | 14 +++++++-- tests/beman/inplace_vector/CMakeLists.txt | 26 +++++++++++++++- 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 cmake/config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 52b19f5..b4ead02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ set(CPACK_GENERATOR TGZ) # includes include(GNUInstallDirs) +include(CMakePackageConfigHelpers) include(CPack) add_library(beman.inplace_vector INTERFACE) @@ -63,9 +64,14 @@ target_compile_features( "$<$>:cxx_std_20>" ) -set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config) -set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets) -set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) +# export cmake config package +set(TARGET_NAME inplace_vector) +set(TARGET_NAMESPACE beman) +set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME}) +set(TARGET_PREFIX ${TARGET_NAMESPACE}.${TARGET_NAME}) # -> PROJECT_NAME? +set(TARGET_PACKAGE_NAME ${TARGET_NAME}-config) +set(TARGETS_EXPORT_NAME ${TARGET_NAME}-targets) +set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME}) # Install the InplaceVector library to the appropriate destination install( @@ -74,6 +80,32 @@ install( FILE_SET inplace_vector_public_headers ) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) + +configure_package_config_file( + cmake/config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake + INSTALL_DESTINATION ${INSTALL_CONFIGDIR} +) + +install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake + DESTINATION ${INSTALL_CONFIGDIR} +) + +install( + EXPORT ${TARGETS_EXPORT_NAME} + FILE ${TARGETS_EXPORT_NAME}.cmake + DESTINATION "${INSTALL_CONFIGDIR}" + NAMESPACE ${TARGET_NAMESPACE}:: +) + if(BEMAN_INPLACE_VECTOR_BUILD_TESTS) enable_testing() add_subdirectory(tests/beman/inplace_vector) diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in new file mode 100644 index 0000000..e6ed3e2 --- /dev/null +++ b/cmake/config.cmake.in @@ -0,0 +1,7 @@ +# cmake/Config.cmake.in -*-makefile-*- +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake") +check_required_components("@TARGET_NAME@") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 496bc08..353eaee 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,8 +1,17 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +cmake_minimum_required(VERSION 3.25...3.31) + +project(beman_inplace_vector_example LANGUAGES CXX) + +if(PROJECT_IS_TOP_LEVEL) + find_package(inplace_vector 1.0.0 EXACT REQUIRED) + enable_testing() +endif() + set(ALL_EXAMPLES fibonacci) -message("Examples to be built: ${ALL_EXAMPLES}") +message(STATUS "Examples to be built: ${ALL_EXAMPLES}") foreach(example ${ALL_EXAMPLES}) add_executable(beman.inplace_vector.examples.${example}) @@ -12,6 +21,7 @@ foreach(example ${ALL_EXAMPLES}) ) target_link_libraries( beman.inplace_vector.examples.${example} - beman.inplace_vector + beman::inplace_vector ) + add_test(NAME ${example} COMMAND beman.inplace_vector.examples.${example}) endforeach() diff --git a/tests/beman/inplace_vector/CMakeLists.txt b/tests/beman/inplace_vector/CMakeLists.txt index 0dc7351..25eb05a 100644 --- a/tests/beman/inplace_vector/CMakeLists.txt +++ b/tests/beman/inplace_vector/CMakeLists.txt @@ -6,6 +6,30 @@ # Tests add_executable(beman.inplace_vector.test inplace_vector.test.cpp) -target_link_libraries(beman.inplace_vector.test PRIVATE beman.inplace_vector) +target_link_libraries(beman.inplace_vector.test PRIVATE beman::inplace_vector) add_test(NAME beman.inplace_vector.test COMMAND beman.inplace_vector.test) + +# test if the targets are findable from the build directory +# NOTE: For an INTERFACE library and multi config generators, we may always use +# the -C Debug config type instead of $ to prevent problems! CK +if(CMAKE_BUILD_TYPE STREQUAL Debug) + if(NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 20) + endif() + if(NOT DEFINED CMAKE_PREFIX_PATH) + set(CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX}) + endif() + add_test( + NAME find-package-test + COMMAND + ${CMAKE_CTEST_COMMAND} --verbose --output-on-failure -C Debug + --build-and-test "${PROJECT_SOURCE_DIR}/examples" + "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" --build-generator + ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" + "-DCMAKE_BUILD_TYPE=Debug" + "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" + ) +endif()