Skip to content

Commit 9a7fd7f

Browse files
committed
Install cmake config package
Add find-package-test Prevent undefined cmake values
1 parent 9f39d9a commit 9a7fd7f

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

CMakeLists.txt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ option(
3636
set(CPACK_GENERATOR TGZ)
3737

3838
include(GNUInstallDirs)
39+
include(CMakePackageConfigHelpers)
3940
include(CPack)
4041

4142
add_library(beman.inplace_vector INTERFACE)
@@ -57,9 +58,14 @@ set_target_properties(
5758
)
5859
target_compile_features(beman.inplace_vector INTERFACE cxx_std_23)
5960

60-
set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config)
61-
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets)
62-
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
61+
# export cmake config package
62+
set(TARGET_NAME inplace_vector)
63+
set(TARGET_NAMESPACE beman)
64+
set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME})
65+
set(TARGET_PREFIX ${TARGET_NAMESPACE}.${TARGET_NAME}) # -> PROJECT_NAME?
66+
set(TARGET_PACKAGE_NAME ${TARGET_NAME}-config)
67+
set(TARGETS_EXPORT_NAME ${TARGET_NAME}-targets)
68+
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME})
6369

6470
# Install the InplaceVector library to the appropriate destination
6571
install(
@@ -68,6 +74,32 @@ install(
6874
FILE_SET inplace_vector_public_headers
6975
)
7076

77+
write_basic_package_version_file(
78+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake
79+
VERSION ${PROJECT_VERSION}
80+
COMPATIBILITY AnyNewerVersion
81+
)
82+
83+
configure_package_config_file(
84+
cmake/config.cmake.in
85+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake
86+
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
87+
)
88+
89+
install(
90+
FILES
91+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake
92+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake
93+
DESTINATION ${INSTALL_CONFIGDIR}
94+
)
95+
96+
install(
97+
EXPORT ${TARGETS_EXPORT_NAME}
98+
FILE ${TARGETS_EXPORT_NAME}.cmake
99+
DESTINATION "${INSTALL_CONFIGDIR}"
100+
NAMESPACE ${TARGET_NAMESPACE}::
101+
)
102+
71103
if(BEMAN_INPLACE_VECTOR_BUILD_TESTS)
72104
enable_testing()
73105
add_subdirectory(tests/beman/inplace_vector)

cmake/config.cmake.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# cmake/Config.cmake.in -*-makefile-*-
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
@PACKAGE_INIT@
5+
6+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
7+
check_required_components("@TARGET_NAME@")

examples/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3+
cmake_minimum_required(VERSION 3.25...3.31)
4+
5+
project(beman_inplace_vector_example LANGUAGES CXX)
6+
7+
if(PROJECT_IS_TOP_LEVEL)
8+
find_package(inplace_vector 1.0.0 EXACT REQUIRED)
9+
enable_testing()
10+
endif()
11+
312
set(ALL_EXAMPLES fibonacci)
413

5-
message("Examples to be built: ${ALL_EXAMPLES}")
14+
message(STATUS "Examples to be built: ${ALL_EXAMPLES}")
615

716
foreach(example ${ALL_EXAMPLES})
817
add_executable(beman.inplace_vector.examples.${example})
@@ -12,6 +21,7 @@ foreach(example ${ALL_EXAMPLES})
1221
)
1322
target_link_libraries(
1423
beman.inplace_vector.examples.${example}
15-
beman.inplace_vector
24+
beman::inplace_vector
1625
)
26+
add_test(NAME ${example} COMMAND beman.inplace_vector.examples.${example})
1727
endforeach()

tests/beman/inplace_vector/CMakeLists.txt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,40 @@
55

66
# Tests
77
add_executable(beman.inplace_vector.test inplace_vector.test.cpp)
8-
9-
target_link_libraries(beman.inplace_vector.test PRIVATE beman.inplace_vector)
10-
8+
target_link_libraries(beman.inplace_vector.test PRIVATE beman::inplace_vector)
119
add_test(NAME beman.inplace_vector.test COMMAND beman.inplace_vector.test)
1210

1311
# Migrated test from original implementation
1412
add_executable(beman.inplace_vector.ref-test ref_impl.test.cpp)
1513
target_link_libraries(
1614
beman.inplace_vector.ref-test
17-
PRIVATE beman.inplace_vector
15+
PRIVATE beman::inplace_vector
1816
)
1917
add_test(
2018
NAME beman.inplace_vector.ref-test
2119
COMMAND beman.inplace_vector.ref-test
2220
)
21+
22+
# test if the targets are findable from the build directory
23+
# NOTE: For an INTERFACE library and multi config generators, we may always use
24+
# the -C Debug config type instead of $<CONFIG> to prevent problems! CK
25+
if(CMAKE_BUILD_TYPE STREQUAL Debug)
26+
if(NOT DEFINED CMAKE_CXX_STANDARD)
27+
set(CMAKE_CXX_STANDARD 23)
28+
endif()
29+
if(NOT DEFINED CMAKE_PREFIX_PATH)
30+
set(CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})
31+
endif()
32+
add_test(
33+
NAME find-package-test
34+
COMMAND
35+
${CMAKE_CTEST_COMMAND} --verbose --output-on-failure -C Debug
36+
--build-and-test "${PROJECT_SOURCE_DIR}/examples"
37+
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test" --build-generator
38+
${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM}
39+
--build-options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
40+
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
41+
"-DCMAKE_BUILD_TYPE=Debug"
42+
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
43+
)
44+
endif()

0 commit comments

Comments
 (0)