Skip to content

Commit

Permalink
Install cmake config package
Browse files Browse the repository at this point in the history
Add find-package-test

Prevent undefined cmake values
  • Loading branch information
ClausKlein committed Nov 25, 2024
1 parent aab71ef commit 464f37b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
38 changes: 35 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(CPACK_GENERATOR TGZ)

# includes
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CPack)

add_library(beman.inplace_vector INTERFACE)
Expand All @@ -63,9 +64,14 @@ target_compile_features(
"$<$<NOT:$<COMPILE_FEATURES:cxx_std_23>>: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(
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
@@ -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@")
14 changes: 12 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
Expand All @@ -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()
26 changes: 25 additions & 1 deletion tests/beman/inplace_vector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<CONFIG> 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()

0 comments on commit 464f37b

Please sign in to comment.