From aaec2f5d4c0de613c2d73b630ab4665f48d7e916 Mon Sep 17 00:00:00 2001 From: Steve Downey Date: Fri, 8 Nov 2024 17:59:17 -0500 Subject: [PATCH 1/2] Install by FILE_SET Create named file sets and use those for installation. Exclude tests from installation, and the empty static library. Use a project prefixed name to enable or disable tests, and default to building tests if this is the top level project. --- CMakeLists.txt | 58 +++++++++---------- Makefile | 4 +- README.md | 8 +-- examples/CMakeLists.txt | 2 +- include/beman/optional26/CMakeLists.txt | 13 +++++ src/beman/optional26/CMakeLists.txt | 49 ++++++---------- src/beman/optional26/tests/CMakeLists.txt | 11 +++- .../tests/test_constructor_fail.cpp | 2 +- 8 files changed, 76 insertions(+), 71 deletions(-) create mode 100644 include/beman/optional26/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 335a2447..d9ac85a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,11 +12,16 @@ include(FetchContent) set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets) -# Build the tests only if enabled via the CLI flag: BUILD_TESTING. -if(BUILD_TESTING) +option(OPTIONAL26_ENABLE_TESTING + "Enable building tests and test infrastructure" + ${PROJECT_IS_TOP_LEVEL}) + +# Build the tests if enabled via the option OPTIONAL26_ENABLE_TESTING +if(OPTIONAL26_ENABLE_TESTING) # Fetch GoogleTest FetchContent_Declare( googletest + EXCLUDE_FROM_ALL GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG e39786088138f2749d64e9e90e0f9902daa77c40 # release-1.15.0 @@ -24,39 +29,32 @@ if(BUILD_TESTING) FetchContent_MakeAvailable(googletest) endif() -add_subdirectory(src/beman/optional26) -add_subdirectory(examples) - -include(GNUInstallDirs) - -set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake) - -install( - EXPORT ${TARGETS_EXPORT_NAME} - NAMESPACE ${CMAKE_PROJECT_NAME} - DESTINATION ${INSTALL_CONFIGDIR} +# Create the library target and named header set for beman_optional26 +add_library(beman_optional26 STATIC) +target_sources(beman_optional26 + PUBLIC + FILE_SET beman_optional26_headers TYPE HEADERS + BASE_DIRS + src + include ) -include(CMakePackageConfigHelpers) - -write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion +if(OPTIONAL26_ENABLE_TESTING) +# Create the library target and named header set for testing beman_optional26 +# and mark the set private +add_executable(beman_optional26_test) +target_sources(beman_optional26_test + PRIVATE + FILE_SET beman_optional26_test_headers TYPE HEADERS + BASE_DIRS + src ) +endif() -configure_package_config_file( - "cmake/Config.cmake.in" - ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake - INSTALL_DESTINATION ${INSTALL_CONFIGDIR} -) +add_subdirectory(src/beman/optional26) +add_subdirectory(include/beman/optional26) -install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}Config.cmake - ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}ConfigVersion.cmake - DESTINATION ${INSTALL_CONFIGDIR} -) +add_subdirectory(examples) # Coverage configure_file("cmake/gcovr.cfg.in" gcovr.cfg @ONLY) diff --git a/Makefile b/Makefile index fc48b30a..90e9175a 100755 --- a/Makefile +++ b/Makefile @@ -63,8 +63,8 @@ TARGET:=all compile: $(_build_path)/CMakeCache.txt ## Compile the project cmake --build $(_build_path) --config $(CONFIG) --target all -- -k 0 -install: $(_build_path)/CMakeCache.txt ## Install the project - DESTDIR=$(abspath $(DEST)) ninja -C $(_build_path) -k 0 install +install: $(_build_path)/CMakeCache.txt compile ## Install the project + cmake --install $(_build_path) --config $(CONFIG) --component beman_optional26_development --verbose ctest: $(_build_path)/CMakeCache.txt ## Run CTest on current build cd $(_build_path) && ctest --output-on-failure -C $(CONFIG) diff --git a/README.md b/README.md index 833e7dab..ee435e80 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ This should build and run the tests with GCC 14 with the address and undefined b CI current build and test flows: ```shell -# Configure build: default build production code + tests (BUILD_TESTING=ON by default). +# Configure build: default build production code + tests (OPTIONAL26_ENABLE_TESTING=ON by default). $ cmake -G "Ninja Multi-Config" \ -DCMAKE_CONFIGURATION_TYPES="RelWithDebInfo;Asan" \ -DCMAKE_TOOLCHAIN_FILE=etc/clang-19-toolchain.cmake \ @@ -228,14 +228,14 @@ Total Test time (real) = 0.67 sec ##### Build Production, but Skip Tests -By default, we build and run tests. You can provide `-DBUILD_TESTING=OFF` and completely disable building tests: +By default, we build and run tests. You can provide `-DOPTIONAL26_ENABLE_TESTING=OFF` and completely disable building tests: ```shell -# Configure build: build production code, skip tests (BUILD_TESTING=OFF). +# Configure build: build production code, skip tests (OPTIONAL26_ENABLE_TESTING=OFF). $ cmake -G "Ninja Multi-Config" \ -DCMAKE_CONFIGURATION_TYPES="RelWithDebInfo;Asan" \ -DCMAKE_TOOLCHAIN_FILE=etc/clang-19-toolchain.cmake \ - -DBUILD_TESTING=OFF \ + -DOPTIONAL26_ENABLE_TESTING=OFF \ -B .build -S . -- The CXX compiler identification is Clang 19.0.0 ... diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0dbd2e19..47b37574 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,7 +30,7 @@ foreach(example ${EXAMPLES}) # Install . install( TARGETS ${example} - EXPORT ${TARGETS_EXPORT_NAME} + COMPONENT beman_optional26_examples DESTINATION ${CMAKE_INSTALL_BINDIR} ) diff --git a/include/beman/optional26/CMakeLists.txt b/include/beman/optional26/CMakeLists.txt new file mode 100644 index 00000000..319bb7ba --- /dev/null +++ b/include/beman/optional26/CMakeLists.txt @@ -0,0 +1,13 @@ +# include/beman/optional26/CMakeLists.txt -*-cmake-*- +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +target_sources(beman_optional26 + PUBLIC + FILE_SET beman_optional26_headers TYPE HEADERS + FILES + optional.hpp + detail/iterator.hpp + detail/stl_interfaces/config.hpp + detail/stl_interfaces/fwd.hpp + detail/stl_interfaces/iterator_interface.hpp +) diff --git a/src/beman/optional26/CMakeLists.txt b/src/beman/optional26/CMakeLists.txt index 41f88530..fed58827 100644 --- a/src/beman/optional26/CMakeLists.txt +++ b/src/beman/optional26/CMakeLists.txt @@ -1,40 +1,25 @@ -# cmake-format: off -# src/beman/optional26/CMakeLists.txt -*-makefile-*- +# src/beman/optional26/CMakeLists.txt -*-cmake-*- # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -# cmake-format: on -add_library(beman_optional26 STATIC optional.cpp detail/iterator.cpp) +# Ensure that optional and iterator get compiled at least once +target_sources(beman_optional26 + PUBLIC + optional.cpp + detail/iterator.cpp) -include(GNUInstallDirs) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +# The library is empty -- exclude it +install(TARGETS beman_optional26 + ARCHIVE + DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT beman_optional26_library + EXCLUDE_FROM_ALL) -target_include_directories( - beman_optional26 - PUBLIC - $ - $ - $ -) - -install( - TARGETS beman_optional26 - EXPORT ${TARGETS_EXPORT_NAME} - DESTINATION - ${CMAKE_INSTALL_LIBDIR} -) - -string(TOLOWER ${CMAKE_PROJECT_NAME} CMAKE_LOWER_PROJECT_NAME) - -install( - DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_LOWER_PROJECT_NAME} - FILES_MATCHING - PATTERN "*.hpp" -) - -target_link_libraries(beman_optional26) +install(TARGETS beman_optional26 + FILE_SET beman_optional26_headers + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + COMPONENT beman_optional26_development) # Tests -if(BUILD_TESTING) +if(OPTIONAL26_ENABLE_TESTING) add_subdirectory(tests) endif() diff --git a/src/beman/optional26/tests/CMakeLists.txt b/src/beman/optional26/tests/CMakeLists.txt index 70ebef44..5ae651e0 100644 --- a/src/beman/optional26/tests/CMakeLists.txt +++ b/src/beman/optional26/tests/CMakeLists.txt @@ -6,7 +6,7 @@ include(GoogleTest) # Tests -add_executable(beman_optional26_test) +# add_executable(beman_optional26_test) target_sources( beman_optional26_test @@ -21,6 +21,15 @@ target_sources( test_utilities.cpp ) + +target_sources(beman_optional26_test + PRIVATE + FILE_SET beman_optional26_test_headers TYPE HEADERS + FILES + test_types.hpp + test_utilities.hpp +) + target_link_libraries( beman_optional26_test PRIVATE beman_optional26 GTest::gtest GTest::gtest_main diff --git a/src/beman/optional26/tests/test_constructor_fail.cpp b/src/beman/optional26/tests/test_constructor_fail.cpp index 5b03c0e8..ecaf3e94 100644 --- a/src/beman/optional26/tests/test_constructor_fail.cpp +++ b/src/beman/optional26/tests/test_constructor_fail.cpp @@ -1,7 +1,7 @@ // src/Beman/Optional26/tests/test_constructor_fail.t.cpp -*-C++-*- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include #include #include From 50ded6a91e4d97b918bcbc397ca0d9cdffc1ca61 Mon Sep 17 00:00:00 2001 From: Steve Downey Date: Fri, 8 Nov 2024 18:20:37 -0500 Subject: [PATCH 2/2] Reformat CML.txt for lint Further CML lint --- CMakeLists.txt | 36 +++++++++++------------ examples/CMakeLists.txt | 10 ++++--- include/beman/optional26/CMakeLists.txt | 20 +++++++------ src/beman/optional26/CMakeLists.txt | 27 +++++++++-------- src/beman/optional26/tests/CMakeLists.txt | 13 ++++---- 5 files changed, 55 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d9ac85a9..3f012e31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,11 @@ include(FetchContent) set(TARGETS_EXPORT_NAME ${CMAKE_PROJECT_NAME}Targets) -option(OPTIONAL26_ENABLE_TESTING - "Enable building tests and test infrastructure" - ${PROJECT_IS_TOP_LEVEL}) +option( + OPTIONAL26_ENABLE_TESTING + "Enable building tests and test infrastructure" + ${PROJECT_IS_TOP_LEVEL} +) # Build the tests if enabled via the option OPTIONAL26_ENABLE_TESTING if(OPTIONAL26_ENABLE_TESTING) @@ -31,24 +33,22 @@ endif() # Create the library target and named header set for beman_optional26 add_library(beman_optional26 STATIC) -target_sources(beman_optional26 - PUBLIC - FILE_SET beman_optional26_headers TYPE HEADERS - BASE_DIRS - src - include +target_sources( + beman_optional26 + PUBLIC FILE_SET beman_optional26_headers TYPE HEADERS BASE_DIRS src include ) if(OPTIONAL26_ENABLE_TESTING) -# Create the library target and named header set for testing beman_optional26 -# and mark the set private -add_executable(beman_optional26_test) -target_sources(beman_optional26_test - PRIVATE - FILE_SET beman_optional26_test_headers TYPE HEADERS - BASE_DIRS - src -) + # Create the library target and named header set for testing beman_optional26 + # and mark the set private + add_executable(beman_optional26_test) + target_sources( + beman_optional26_test + PRIVATE + FILE_SET beman_optional26_test_headers + TYPE HEADERS + BASE_DIRS src + ) endif() add_subdirectory(src/beman/optional26) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 47b37574..b94bbaba 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -29,9 +29,11 @@ foreach(example ${EXAMPLES}) # Install . install( - TARGETS ${example} - COMPONENT beman_optional26_examples - DESTINATION - ${CMAKE_INSTALL_BINDIR} + TARGETS + ${example} + COMPONENT + beman_optional26_examples + DESTINATION + ${CMAKE_INSTALL_BINDIR} ) endforeach() diff --git a/include/beman/optional26/CMakeLists.txt b/include/beman/optional26/CMakeLists.txt index 319bb7ba..29150231 100644 --- a/include/beman/optional26/CMakeLists.txt +++ b/include/beman/optional26/CMakeLists.txt @@ -1,13 +1,15 @@ # include/beman/optional26/CMakeLists.txt -*-cmake-*- # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -target_sources(beman_optional26 - PUBLIC - FILE_SET beman_optional26_headers TYPE HEADERS - FILES - optional.hpp - detail/iterator.hpp - detail/stl_interfaces/config.hpp - detail/stl_interfaces/fwd.hpp - detail/stl_interfaces/iterator_interface.hpp +target_sources( + beman_optional26 + PUBLIC + FILE_SET beman_optional26_headers + TYPE HEADERS + FILES + optional.hpp + detail/iterator.hpp + detail/stl_interfaces/config.hpp + detail/stl_interfaces/fwd.hpp + detail/stl_interfaces/iterator_interface.hpp ) diff --git a/src/beman/optional26/CMakeLists.txt b/src/beman/optional26/CMakeLists.txt index fed58827..1bd7439e 100644 --- a/src/beman/optional26/CMakeLists.txt +++ b/src/beman/optional26/CMakeLists.txt @@ -2,22 +2,23 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # Ensure that optional and iterator get compiled at least once -target_sources(beman_optional26 - PUBLIC - optional.cpp - detail/iterator.cpp) +target_sources(beman_optional26 PUBLIC optional.cpp detail/iterator.cpp) # The library is empty -- exclude it -install(TARGETS beman_optional26 - ARCHIVE - DESTINATION ${CMAKE_INSTALL_LIBDIR} - COMPONENT beman_optional26_library - EXCLUDE_FROM_ALL) +install( + TARGETS beman_optional26 + ARCHIVE + DESTINATION ${CMAKE_INSTALL_LIBDIR} + COMPONENT beman_optional26_library + EXCLUDE_FROM_ALL +) -install(TARGETS beman_optional26 - FILE_SET beman_optional26_headers - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - COMPONENT beman_optional26_development) +install( + TARGETS beman_optional26 + FILE_SET beman_optional26_headers + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + COMPONENT beman_optional26_development +) # Tests if(OPTIONAL26_ENABLE_TESTING) diff --git a/src/beman/optional26/tests/CMakeLists.txt b/src/beman/optional26/tests/CMakeLists.txt index 5ae651e0..a35e019c 100644 --- a/src/beman/optional26/tests/CMakeLists.txt +++ b/src/beman/optional26/tests/CMakeLists.txt @@ -21,13 +21,12 @@ target_sources( test_utilities.cpp ) - -target_sources(beman_optional26_test - PRIVATE - FILE_SET beman_optional26_test_headers TYPE HEADERS - FILES - test_types.hpp - test_utilities.hpp +target_sources( + beman_optional26_test + PRIVATE + FILE_SET beman_optional26_test_headers + TYPE HEADERS + FILES test_types.hpp test_utilities.hpp ) target_link_libraries(