From e20f04e4f33f04a4835cb23b38a2c35723d2563f Mon Sep 17 00:00:00 2001 From: Vito Gamberini Date: Mon, 26 May 2025 20:40:52 -0400 Subject: [PATCH] Update CMakeLists, prepare for packaging * Converts beman::scope to interface library * Adds CMake config packaging routines * Removes vestigal non-header source file * Tests/examples link to library instead of raw source include --- .gitignore | 3 ++ CMakeLists.txt | 72 ++++++++++++++++++++++++++++++---- cmake/beman.scope-config.cmake | 8 ++++ examples/CMakeLists.txt | 2 +- src/beman/scope/CMakeLists.txt | 25 ------------ src/beman/scope/scope.cpp | 3 -- tests/CMakeLists.txt | 6 +-- 7 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 cmake/beman.scope-config.cmake delete mode 100644 src/beman/scope/CMakeLists.txt delete mode 100644 src/beman/scope/scope.cpp diff --git a/.gitignore b/.gitignore index da160f8..6c1ce4b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # ignore emacs temp files *~ \#*\# + +# ignore vscode settings +.vscode diff --git a/CMakeLists.txt b/CMakeLists.txt index cb96b62..0a3a79e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,29 +2,87 @@ cmake_minimum_required(VERSION 3.25) -project(beman.scope DESCRIPTION "Generic Scope Guard" LANGUAGES CXX) - -enable_testing() +project( + beman.scope + DESCRIPTION "Generic Scope Guard" + LANGUAGES CXX + VERSION 0.0.1 +) # [CMAKE.SKIP_TESTS] option( BEMAN_SCOPE_BUILD_TESTS - "Enable building tests and test infrastructure. Default: ON. Values: { ON, OFF }." + "Enable building tests and test infrastructure. Default: ${PROJECT_IS_TOP_LEVEL}. Values: { ON, OFF }." ${PROJECT_IS_TOP_LEVEL} ) # [CMAKE.SKIP_EXAMPLES] option( BEMAN_SCOPE_BUILD_EXAMPLES - "Enable building examples. Default: ON. Values: { ON, OFF }." + "Enable building examples. Default: ${PROJECT_IS_TOP_LEVEL}. Values: { ON, OFF }." + ${PROJECT_IS_TOP_LEVEL} +) + +option( + BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE + "Enable creating and installing a CMake config-file package. Default: ${PROJECT_IS_TOP_LEVEL}. Values: { ON, OFF }." ${PROJECT_IS_TOP_LEVEL} ) -include(GNUInstallDirs) +add_library(beman.scope INTERFACE) +add_library(beman::scope ALIAS beman.scope) + +# gersemi: off +set_target_properties( + beman.scope + PROPERTIES + VERIFY_INTERFACE_HEADER_SETS ON + EXPORT_NAME scope +) + +target_sources( + beman.scope + INTERFACE + FILE_SET HEADERS + BASE_DIRS include + FILES include/beman/scope/scope.hpp +) + +install( + TARGETS beman.scope + EXPORT beman.scope-targets + COMPONENT beman.scope + FILE_SET HEADERS +) +# gersemi: on + +if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE) + include(GNUInstallDirs) + include(CMakePackageConfigHelpers) + + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/beman.scope-config-version.cmake + COMPATIBILITY ExactVersion + ) -# todo rm add_subdirectory(src/beman/scope) + install( + FILES + cmake/beman.scope-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/beman.scope-config-version.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope + COMPONENT beman.scope + ) + + install( + EXPORT beman.scope-targets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope + NAMESPACE beman:: + COMPONENT beman.scope + ) +endif() if(BEMAN_SCOPE_BUILD_TESTS) + enable_testing() add_subdirectory(tests) endif() diff --git a/cmake/beman.scope-config.cmake b/cmake/beman.scope-config.cmake new file mode 100644 index 0000000..e4102c5 --- /dev/null +++ b/cmake/beman.scope-config.cmake @@ -0,0 +1,8 @@ +include(${CMAKE_CURRENT_LIST_DIR}/beman.scope-targets.cmake) + +foreach(comp IN LISTS beman.scope_FIND_COMPONENTS) + if(beman.scope_FIND_REQUIRED_${comp}) + set(beman.scope_FOUND FALSE) + return() + endif() +endforeach() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a205765..bfe8ef5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,5 +7,5 @@ message("Examples to be built: ${ALL_EXAMPLES}") foreach(example ${ALL_EXAMPLES}) add_executable(${example}) target_sources(${example} PRIVATE ${example}.cpp) - target_include_directories(${example} PRIVATE ${CMAKE_SOURCE_DIR}/include) + target_link_libraries(${example} PRIVATE beman::scope) endforeach() diff --git a/src/beman/scope/CMakeLists.txt b/src/beman/scope/CMakeLists.txt deleted file mode 100644 index c3b27fa..0000000 --- a/src/beman/scope/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_library(beman.scope) -add_library(beman::scope ALIAS beman.scope) - -target_sources(beman.scope PRIVATE scope.cpp) - -target_sources( - beman.scope - PUBLIC - FILE_SET HEADERS - BASE_DIRS ${PROJECT_SOURCE_DIR}/include - FILES ${PROJECT_SOURCE_DIR}/include/beman/scope/scope.hpp -) - -set_target_properties(beman.scope PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON) - -install( - TARGETS beman.scope - EXPORT beman.scope - DESTINATION - $<$:debug/>${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION $<$:debug/>${CMAKE_INSTALL_BINDIR} - FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} -) diff --git a/src/beman/scope/scope.cpp b/src/beman/scope/scope.cpp deleted file mode 100644 index 6912bb7..0000000 --- a/src/beman/scope/scope.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e183b45..55d7d48 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -13,16 +13,14 @@ set(ALL_TESTNAMES scope_success scope_exit scope_fail unique_resource) message("Tests to be built: ${ALL_TESTNAMES}") -include(CTest) include(Catch) foreach(testname ${ALL_TESTNAMES}) add_executable(test.${testname}) target_sources(test.${testname} PRIVATE ${testname}.test.cpp) - target_include_directories( + target_link_libraries( test.${testname} - PRIVATE ${CMAKE_SOURCE_DIR}/include + PRIVATE Catch2::Catch2WithMain beman::scope ) - target_link_libraries(test.${testname} PRIVATE Catch2::Catch2WithMain) catch_discover_tests(test.${testname}) endforeach()