Skip to content

Commit

Permalink
Export cmake config package
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Nov 20, 2024
1 parent f6926dc commit d48c6c6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
51 changes: 38 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.27...3.31)

project(beman_optional26 VERSION 0.0.0 LANGUAGES CXX)

# Includes
include(CTest)
include(CPACK)
include(FetchContent)

set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets)
set(TARGET_PACKAGE_NAME ${PROJECT_NAME})
set(TARGETS_EXPORT_NAME ${TARGET_PACKAGE_NAME}-targets)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET_PACKAGE_NAME})

option(
OPTIONAL26_ENABLE_TESTING
Expand All @@ -20,6 +22,8 @@ option(

# Build the tests if enabled via the option OPTIONAL26_ENABLE_TESTING
if(OPTIONAL26_ENABLE_TESTING)
enable_testing()

# Fetch GoogleTest
FetchContent_Declare(
googletest
Expand All @@ -35,6 +39,7 @@ set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON)

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

include(CMakePackageConfigHelpers)

# This will be used to replace @PACKAGE_cmakeModulesDir@
set(cmakeModulesDir cmake/beman)
# install
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(
cmake/Config.cmake.in
BemanOptional26Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
PATH_VARS cmakeModulesDir
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/BemanOptional26Config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
COMPONENT beman_optional26_development
FILES
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PACKAGE_NAME}-config-version.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)

# # This will be used to replace @PACKAGE_cmakeModulesDir@
# set(cmakeModulesDir cmake)
# configure_package_config_file(
# cmake/Config.cmake.in
# beman_optional26-config.cmake
# INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman_optional26/
# PATH_VARS cmakeModulesDir
# NO_SET_AND_CHECK_MACRO
# NO_CHECK_REQUIRED_COMPONENTS_MACRO
# )
#
# install(
# FILES ${CMAKE_CURRENT_BINARY_DIR}/beman_optional26-config.cmake
# DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman_optional26/
# COMPONENT beman_optional26_development
# )

# Coverage
configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY)

Expand Down
30 changes: 20 additions & 10 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# cmake-format: on

set(BEMAN_OPTIONAL26_LIBRARY "beman_optional26")
cmake_minimum_required(VERSION 3.27...3.31)

project(beman_optional26_example VERSION 0.0.0 LANGUAGES CXX)

set(BEMAN_OPTIONAL26_LIBRARY "Beman::Optional26::beman_optional26")

if(PROJECT_IS_TOP_LEVEL)
find_package(beman_optional26 0.0.0 EXACT REQUIRED)
endif()

include(GNUInstallDirs)

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

# Install .
install(
TARGETS
${example}
COMPONENT
beman_optional26_examples
DESTINATION
${CMAKE_INSTALL_BINDIR}
)
if(NOT PROJECT_IS_TOP_LEVEL)
# Install .
install(
TARGETS
${example}
COMPONENT
beman_optional26_examples
DESTINATION
${CMAKE_INSTALL_BINDIR}
)
endif()
endforeach()
4 changes: 2 additions & 2 deletions include/beman/optional26/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ install(

install(
EXPORT beman_optional26_export
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman/optional26/
DESTINATION ${INSTALL_CONFIGDIR}
NAMESPACE Beman::Optional26::
FILE beman_optional26.cmake
FILE beman_optional26-config-targets.cmake
EXPORT_LINK_INTERFACE_LIBRARIES
COMPONENT beman_optional26_development
)
7 changes: 5 additions & 2 deletions src/beman/optional26/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ target_sources(

target_link_libraries(
beman_optional26_test
PRIVATE beman_optional26 GTest::gtest GTest::gtest_main
PRIVATE Beman::Optional26::beman_optional26 GTest::gtest GTest::gtest_main
)

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

add_library(constructor_fails test_constructor_fail.cpp)

target_link_libraries(constructor_fails PRIVATE beman_optional26)
target_link_libraries(
constructor_fails
PRIVATE Beman::Optional26::beman_optional26
)

set_target_properties(
constructor_fails
Expand Down

0 comments on commit d48c6c6

Please sign in to comment.