Skip to content

Commit 6f4e8aa

Browse files
committed
Export cmake config package
1 parent 5691c2d commit 6f4e8aa

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

CMakeLists.txt

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
cmake_minimum_required(VERSION 3.27)
5+
cmake_minimum_required(VERSION 3.27...3.31)
66

77
project(beman_optional26 VERSION 0.0.0 LANGUAGES CXX)
88

99
# Includes
10-
include(CTest)
10+
include(CPACK)
1111
include(FetchContent)
1212

13-
set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)
13+
set(TARGET_PACKAGE_NAME ${PROJECT_NAME})
14+
set(TARGETS_EXPORT_NAME ${TARGET_PACKAGE_NAME}-targets)
15+
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_PACKAGE_NAME})
1416

1517
option(
1618
OPTIONAL26_ENABLE_TESTING
@@ -20,6 +22,8 @@ option(
2022

2123
# Build the tests if enabled via the option OPTIONAL26_ENABLE_TESTING
2224
if(OPTIONAL26_ENABLE_TESTING)
25+
enable_testing()
26+
2327
# Fetch GoogleTest
2428
FetchContent_Declare(
2529
googletest
@@ -35,6 +39,7 @@ set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON)
3539

3640
# Create the library target and named header set for beman_optional26
3741
add_library(beman_optional26 INTERFACE)
42+
add_library(Beman::Optional26::beman_optional26 ALIAS beman_optional26)
3843
target_sources(
3944
beman_optional26
4045
PUBLIC FILE_SET beman_optional26_headers TYPE HEADERS BASE_DIRS include
@@ -61,23 +66,43 @@ add_subdirectory(examples)
6166

6267
include(CMakePackageConfigHelpers)
6368

64-
# This will be used to replace @PACKAGE_cmakeModulesDir@
65-
set(cmakeModulesDir cmake/beman)
69+
# install
70+
write_basic_package_version_file(
71+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake
72+
VERSION ${PROJECT_VERSION}
73+
COMPATIBILITY AnyNewerVersion
74+
)
75+
6676
configure_package_config_file(
6777
cmake/Config.cmake.in
68-
BemanOptional26Config.cmake
69-
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
70-
PATH_VARS cmakeModulesDir
71-
NO_SET_AND_CHECK_MACRO
72-
NO_CHECK_REQUIRED_COMPONENTS_MACRO
78+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake
79+
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
7380
)
7481

7582
install(
76-
FILES ${CMAKE_CURRENT_BINARY_DIR}/BemanOptional26Config.cmake
77-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
78-
COMPONENT beman_optional26_development
83+
FILES
84+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake
85+
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake
86+
DESTINATION ${INSTALL_CONFIGDIR}
7987
)
8088

89+
# # This will be used to replace @PACKAGE_cmakeModulesDir@
90+
# set(cmakeModulesDir cmake)
91+
# configure_package_config_file(
92+
# cmake/Config.cmake.in
93+
# beman_optional26-config.cmake
94+
# INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman_optional26/
95+
# PATH_VARS cmakeModulesDir
96+
# NO_SET_AND_CHECK_MACRO
97+
# NO_CHECK_REQUIRED_COMPONENTS_MACRO
98+
# )
99+
#
100+
# install(
101+
# FILES ${CMAKE_CURRENT_BINARY_DIR}/beman_optional26-config.cmake
102+
# DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman_optional26/
103+
# COMPONENT beman_optional26_development
104+
# )
105+
81106
# Coverage
82107
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)
83108

examples/CMakeLists.txt

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
# cmake-format: on
55

6-
set(BEMAN_OPTIONAL26_LIBRARY "beman_optional26")
6+
cmake_minimum_required(VERSION 3.27...3.31)
7+
8+
project(beman_optional26_example VERSION 0.0.0 LANGUAGES CXX)
9+
10+
set(BEMAN_OPTIONAL26_LIBRARY "Beman::Optional26::beman_optional26")
11+
12+
if(PROJECT_IS_TOP_LEVEL)
13+
find_package(beman_optional26 0.0.0 EXACT REQUIRED)
14+
endif()
715

816
include(GNUInstallDirs)
917

@@ -27,13 +35,15 @@ foreach(example ${EXAMPLES})
2735
# Link example with the library.
2836
target_link_libraries(${example} "${BEMAN_OPTIONAL26_LIBRARY}")
2937

30-
# Install .
31-
install(
32-
TARGETS
33-
${example}
34-
COMPONENT
35-
beman_optional26_examples
36-
DESTINATION
37-
${CMAKE_INSTALL_BINDIR}
38-
)
38+
if(NOT PROJECT_IS_TOP_LEVEL)
39+
# Install .
40+
install(
41+
TARGETS
42+
${example}
43+
COMPONENT
44+
beman_optional26_examples
45+
DESTINATION
46+
${CMAKE_INSTALL_BINDIR}
47+
)
48+
endif()
3949
endforeach()

include/beman/optional26/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ install(
3333

3434
install(
3535
EXPORT beman_optional26_export
36-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
36+
DESTINATION ${INSTALL_CONFIGDIR}
3737
NAMESPACE Beman::Optional26::
38-
FILE beman_optional26.cmake
38+
FILE beman_optional26-config-targets.cmake
3939
EXPORT_LINK_INTERFACE_LIBRARIES
4040
COMPONENT beman_optional26_development
4141
)

src/beman/optional26/tests/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ target_sources(
2929

3030
target_link_libraries(
3131
beman_optional26_test
32-
PRIVATE beman_optional26 GTest::gtest GTest::gtest_main
32+
PRIVATE Beman::Optional26::beman_optional26 GTest::gtest GTest::gtest_main
3333
)
3434

3535
# Issue #32: Re-enable ASAN run CI/clang-19.
@@ -41,7 +41,10 @@ gtest_add_tests(TARGET beman_optional26_test "" AUTO)
4141

4242
add_library(constructor_fails test_constructor_fail.cpp)
4343

44-
target_link_libraries(constructor_fails PRIVATE beman_optional26)
44+
target_link_libraries(
45+
constructor_fails
46+
PRIVATE Beman::Optional26::beman_optional26
47+
)
4548

4649
set_target_properties(
4750
constructor_fails

0 commit comments

Comments
 (0)