Skip to content

Commit def8c12

Browse files
committed
Install cmake config package
Add find-package-test Prevent undefined cmake values
1 parent 043fd7b commit def8c12

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

CMakeLists.txt

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

3838
# includes
3939
include(GNUInstallDirs)
40+
include(CMakePackageConfigHelpers)
4041
include(CPack)
4142

4243
add_library(beman.inplace_vector INTERFACE)
@@ -63,9 +64,14 @@ target_compile_features(
6364
"$<$<NOT:$<COMPILE_FEATURES:cxx_std_23>>:cxx_std_20>"
6465
)
6566

66-
set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config)
67-
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets)
68-
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
67+
# export cmake config package
68+
set(TARGET_NAME inplace_vector)
69+
set(TARGET_NAMESPACE beman)
70+
set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME})
71+
set(TARGET_PREFIX ${TARGET_NAMESPACE}.${TARGET_NAME}) # -> PROJECT_NAME?
72+
set(TARGET_PACKAGE_NAME ${TARGET_NAME}-config)
73+
set(TARGETS_EXPORT_NAME ${TARGET_NAME}-targets)
74+
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_NAME})
6975

7076
# Install the InplaceVector library to the appropriate destination
7177
install(
@@ -74,6 +80,32 @@ install(
7480
FILE_SET inplace_vector_public_headers
7581
)
7682

83+
write_basic_package_version_file(
84+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake
85+
VERSION ${PROJECT_VERSION}
86+
COMPATIBILITY AnyNewerVersion
87+
)
88+
89+
configure_package_config_file(
90+
cmake/config.cmake.in
91+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake
92+
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
93+
)
94+
95+
install(
96+
FILES
97+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}.cmake
98+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-version.cmake
99+
DESTINATION ${INSTALL_CONFIGDIR}
100+
)
101+
102+
install(
103+
EXPORT ${TARGETS_EXPORT_NAME}
104+
FILE ${TARGETS_EXPORT_NAME}.cmake
105+
DESTINATION "${INSTALL_CONFIGDIR}"
106+
NAMESPACE ${TARGET_NAMESPACE}::
107+
)
108+
77109
if(BEMAN_INPLACE_VECTOR_BUILD_TESTS)
78110
enable_testing()
79111
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66
# Tests
77
add_executable(beman.inplace_vector.test inplace_vector.test.cpp)
88

9-
target_link_libraries(beman.inplace_vector.test PRIVATE beman.inplace_vector)
9+
target_link_libraries(beman.inplace_vector.test PRIVATE beman::inplace_vector)
1010

1111
add_test(NAME beman.inplace_vector.test COMMAND beman.inplace_vector.test)
12+
13+
# test if the targets are findable from the build directory
14+
# NOTE: For an INTERFACE library and multi config generators, we may always use
15+
# the -C Debug config type instead of $<CONFIG> to prevent problems! CK
16+
if(CMAKE_BUILD_TYPE STREQUAL Debug)
17+
if(NOT DEFINED CMAKE_CXX_STANDARD)
18+
set(CMAKE_CXX_STANDARD 20)
19+
endif()
20+
if(NOT DEFINED CMAKE_PREFIX_PATH)
21+
set(CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})
22+
endif()
23+
add_test(
24+
NAME find-package-test
25+
COMMAND
26+
${CMAKE_CTEST_COMMAND} --verbose --output-on-failure -C Debug
27+
--build-and-test "${PROJECT_SOURCE_DIR}/examples"
28+
"${CMAKE_CURRENT_BINARY_DIR}/find-package-test" --build-generator
29+
${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM}
30+
--build-options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
31+
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
32+
"-DCMAKE_BUILD_TYPE=Debug"
33+
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
34+
)
35+
endif()

0 commit comments

Comments
 (0)