From ba8e365eca5d0867440aef41cc45e61355899547 Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 20 Oct 2023 09:19:58 +0200 Subject: [PATCH 01/10] Add warning flags via target instead of variable. --- CMakeLists.txt | 16 +++++----------- tests/cmake/KassertTestHelper.cmake | 2 +- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fb02dc..54f061e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,10 +58,8 @@ target_include_directories(kassert_base INTERFACE "$ Date: Fri, 20 Oct 2023 10:37:56 +0200 Subject: [PATCH 02/10] Better CMake dependency management. --- .gitmodules | 3 -- CMakeLists.txt | 49 +++++++++++++++++++++++++++++ extern/googletest | 1 - tests/cmake/KassertTestHelper.cmake | 2 +- 4 files changed, 50 insertions(+), 5 deletions(-) delete mode 160000 extern/googletest diff --git a/.gitmodules b/.gitmodules index 42a6fe7..8fba731 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "extern/googletest"] - path = extern/googletest - url = https://github.com/google/googletest.git [submodule "extern/doxygen-awesome-css"] path = extern/doxygen-awesome-css url = https://github.com/jothepro/doxygen-awesome-css.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 54f061e..597df61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,54 @@ if (TARGET kassert) return() endif () +# dependencies +include(FetchContent) +if (POLICY CMP0135) + cmake_policy(SET CMP0135 OLD) +endif() +set(fetch_content_deps googletest) + +set(fetch_content_supported_flags "") +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) + list(APPEND fetch_content_supported_flags OVERRIDE_FIND_PACKAGE) +endif () +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.25) + list(APPEND fetch_content_supported_flags SYSTEM) +endif () +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + list(APPEND fetch_content_supported_flags EXCLUDE_FROM_ALL) +endif () + +# Using CMake >= 3.24 we override find_package and it "just works". +# For older versions we simulate the same behavior by first making the dependency +# available and then writing an empty `googletest-config.cmake` such that +# find_package does not fail. +# +# This does not allow for easy overriding of dependencies in top-level projects +# because then the toplevel also has to override the config file, but this is +# the price to pay for using old CMake versions. +# +# When using CMake >= 3.24 a library user can override the depencies by +# providing an earlier FetchContent_Declare declaration with the `OVERRIDE_FIND_PACKAGE` flag enabled. +# Only the first call to FetchContent_Declare for each dependency will be registered. + +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip + ${fetch_content_supported_flags} +) + +function(ensure_googletest) + if (CMAKE_VERSION VERSION_LESS 3.24) + if(NOT googletest_POPULATED) + FetchContent_MakeAvailable(googletest) + set(googletest_DIR ${CMAKE_BINARY_DIR}/my_pkgRedirects/ PARENT_SCOPE) + file(WRITE ${CMAKE_BINARY_DIR}/my_pkgRedirects/googletest-config.cmake "") + endif() + endif() +endfunction() + + if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) # folder support for IDEs set_property(GLOBAL PROPERTY USE_FOLDERS ON) @@ -124,5 +172,6 @@ add_library(kassert::kassert ALIAS kassert) # Testing and examples are only built if this is the main project or if KASSERT_BUILD_TESTS is set (OFF by default) if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR KASSERT_BUILD_TESTS) + ensure_googletest() add_subdirectory(tests) endif () diff --git a/extern/googletest b/extern/googletest deleted file mode 160000 index af29db7..0000000 --- a/extern/googletest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit af29db7ec28d6df1c7f0f745186884091e602e07 diff --git a/tests/cmake/KassertTestHelper.cmake b/tests/cmake/KassertTestHelper.cmake index 1f39fef..aae4776 100644 --- a/tests/cmake/KassertTestHelper.cmake +++ b/tests/cmake/KassertTestHelper.cmake @@ -1,4 +1,4 @@ -add_subdirectory("${PROJECT_SOURCE_DIR}/extern/googletest" "extern/googletest") +find_package(googletest REQUIRED) include(GoogleTest) From ee67b58bfe6afe12d3f90ca8c5e82431635d9adf Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 20 Oct 2023 10:43:18 +0200 Subject: [PATCH 03/10] Formatting. --- CMakeLists.txt | 89 +++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 597df61..3fc1f49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ endif () include(FetchContent) if (POLICY CMP0135) cmake_policy(SET CMP0135 OLD) -endif() +endif () set(fetch_content_deps googletest) set(fetch_content_supported_flags "") @@ -28,34 +28,34 @@ if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) list(APPEND fetch_content_supported_flags EXCLUDE_FROM_ALL) endif () -# Using CMake >= 3.24 we override find_package and it "just works". -# For older versions we simulate the same behavior by first making the dependency -# available and then writing an empty `googletest-config.cmake` such that -# find_package does not fail. +# Using CMake >= 3.24 we override find_package and it "just works". For older versions we simulate the same behavior by +# first making the dependency available and then writing an empty `googletest-config.cmake` such that find_package does +# not fail. # -# This does not allow for easy overriding of dependencies in top-level projects -# because then the toplevel also has to override the config file, but this is -# the price to pay for using old CMake versions. +# This does not allow for easy overriding of dependencies in top-level projects because then the toplevel also has to +# override the config file, but this is the price to pay for using old CMake versions. # -# When using CMake >= 3.24 a library user can override the depencies by -# providing an earlier FetchContent_Declare declaration with the `OVERRIDE_FIND_PACKAGE` flag enabled. -# Only the first call to FetchContent_Declare for each dependency will be registered. +# When using CMake >= 3.24 a library user can override the depencies by providing an earlier FetchContent_Declare +# declaration with the `OVERRIDE_FIND_PACKAGE` flag enabled. Only the first call to FetchContent_Declare for each +# dependency will be registered. FetchContent_Declare( - googletest - URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip - ${fetch_content_supported_flags} + googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip + ${fetch_content_supported_flags} ) -function(ensure_googletest) +function (ensure_googletest) if (CMAKE_VERSION VERSION_LESS 3.24) - if(NOT googletest_POPULATED) + if (NOT googletest_POPULATED) FetchContent_MakeAvailable(googletest) - set(googletest_DIR ${CMAKE_BINARY_DIR}/my_pkgRedirects/ PARENT_SCOPE) + set(googletest_DIR + ${CMAKE_BINARY_DIR}/my_pkgRedirects/ + PARENT_SCOPE + ) file(WRITE ${CMAKE_BINARY_DIR}/my_pkgRedirects/googletest-config.cmake "") - endif() - endif() -endfunction() + endif () + endif () +endfunction () if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) @@ -107,38 +107,37 @@ target_include_directories(kassert_base INTERFACE "$ Date: Thu, 6 Feb 2025 22:49:37 +0100 Subject: [PATCH 04/10] Remove some old cruft. --- CMakeLists.txt | 52 -------------------------------------------------- 1 file changed, 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fc1f49..69fa41f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,50 +12,7 @@ endif () # dependencies include(FetchContent) -if (POLICY CMP0135) - cmake_policy(SET CMP0135 OLD) -endif () -set(fetch_content_deps googletest) -set(fetch_content_supported_flags "") -if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) - list(APPEND fetch_content_supported_flags OVERRIDE_FIND_PACKAGE) -endif () -if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.25) - list(APPEND fetch_content_supported_flags SYSTEM) -endif () -if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) - list(APPEND fetch_content_supported_flags EXCLUDE_FROM_ALL) -endif () - -# Using CMake >= 3.24 we override find_package and it "just works". For older versions we simulate the same behavior by -# first making the dependency available and then writing an empty `googletest-config.cmake` such that find_package does -# not fail. -# -# This does not allow for easy overriding of dependencies in top-level projects because then the toplevel also has to -# override the config file, but this is the price to pay for using old CMake versions. -# -# When using CMake >= 3.24 a library user can override the depencies by providing an earlier FetchContent_Declare -# declaration with the `OVERRIDE_FIND_PACKAGE` flag enabled. Only the first call to FetchContent_Declare for each -# dependency will be registered. - -FetchContent_Declare( - googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip - ${fetch_content_supported_flags} -) - -function (ensure_googletest) - if (CMAKE_VERSION VERSION_LESS 3.24) - if (NOT googletest_POPULATED) - FetchContent_MakeAvailable(googletest) - set(googletest_DIR - ${CMAKE_BINARY_DIR}/my_pkgRedirects/ - PARENT_SCOPE - ) - file(WRITE ${CMAKE_BINARY_DIR}/my_pkgRedirects/googletest-config.cmake "") - endif () - endif () -endfunction () if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) @@ -86,15 +43,6 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) endif () endif () -# Require out-of-source builds -file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH) -if (EXISTS "${LOC_PATH}") - message( - FATAL_ERROR - "You cannot build in a source directory (or any directory with a CMakeLists.txt file). Please make a build subdirectory. Feel free to remove CMakeCache.txt and CMakeFiles." - ) -endif () - option(KASSERT_WARNINGS_ARE_ERRORS OFF) option(KASSERT_BUILD_TESTS OFF) From 6e576638db5aa805b11471f32e7b30b41fe775fc Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 7 Feb 2025 08:24:36 +0100 Subject: [PATCH 05/10] Better handling of assertions levels and exception mode via CMake properties. --- CMakeLists.txt | 62 +++++++++-------------------- extern/CMakeLists.txt | 1 - tests/CMakeLists.txt | 1 + tests/cmake/KassertTestHelper.cmake | 16 ++++++-- 4 files changed, 32 insertions(+), 48 deletions(-) delete mode 100644 extern/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 69fa41f..b2b5778 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,19 +9,12 @@ project( if (TARGET kassert) return() endif () +option(KASSERT_BUILD_TESTS "Build the tests." ${PROJECT_IS_TOP_LEVEL}) # dependencies include(FetchContent) - - -if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) - # folder support for IDEs - set_property(GLOBAL PROPERTY USE_FOLDERS ON) - - # this has to be enabled in the main CMakeLists file - include(CTest) - +if (PROJECT_IS_TOP_LEVEL) # find Doxygen find_package(Doxygen) if (DOXYGEN_FOUND) @@ -43,17 +36,12 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) endif () endif () -option(KASSERT_WARNINGS_ARE_ERRORS OFF) -option(KASSERT_BUILD_TESTS OFF) - -add_subdirectory(extern) - -# Dummy target without any compile definitions -- used for unit tests only -add_library(kassert_base INTERFACE) -target_include_directories(kassert_base INTERFACE "$") +add_library(kassert INTERFACE) +target_include_directories(kassert INTERFACE include) # set C++ standard to C++17 -target_compile_features(kassert_base INTERFACE cxx_std_17) +target_compile_features(kassert INTERFACE cxx_std_17) + add_library(kassert_warnings INTERFACE) target_compile_options( kassert_warnings @@ -89,36 +77,24 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") ) endif () -# OFF by default. -if (KASSERT_WARNINGS_ARE_ERRORS) - target_compile_options(kassert_warnings INTERFACE "-Werror") -endif () - -# Actual library target with compile definitions -add_library(kassert INTERFACE) -target_link_libraries(kassert INTERFACE kassert_base) - -if (NOT DEFINED KASSERT_EXCEPTION_MODE OR KASSERT_EXCEPTION_MODE EQUAL 1) - message(STATUS "Exception mode enabled.") - target_compile_definitions(kassert INTERFACE -DKASSERT_EXCEPTION_MODE) -else () - message(STATUS "Exception mode disabled.") -endif () +# THROWING_KASSERT is able to throw an exception if an assertion fails. If KASSERT_EXCEPTION_MODE is defined, than an +# exception will be thrown upon failure, otherwise it behaves like regular KASSERT. You can either manually #define it, +# or by setting the KASSERT_EXCEPTION_MODE target property to ON on the target which links to kassert. +set(exception_mode_enabled "$>") +target_compile_definitions(kassert INTERFACE "$<${exception_mode_enabled}:-DKASSERT_EXCEPTION_MODE>") # The assertion level controls which assertions are enabled during runtime. Assertion levels can be set explicitly using -# the -DKASSERT_ASSERTION_LEVEL=... flag. -if (NOT DEFINED KASSERT_ASSERTION_LEVEL) - message(WARNING "Assertion level no set. Default to level 0.") - set(KASSERT_ASSERTION_LEVEL 0) -else () - message(STATUS "Assertion level set to ${KASSERT_ASSERTION_LEVEL}.") -endif () -target_compile_definitions(kassert INTERFACE -DKASSERT_ASSERTION_LEVEL=${KASSERT_ASSERTION_LEVEL}) +# the -DKASSERT_ASSERTION_LEVEL=... flag. You can also set the KASSERT_ASSERTION_LEVEL target property to the desired +# level on the target which links to kassert. This will properly #define the assertion level for the target. +set(assertion_level_set "$>") +set(assertion_level "$") +target_compile_definitions(kassert INTERFACE "$<${assertion_level_set}:-DKASSERT_ASSERTION_LEVEL=${assertion_level}>") add_library(kassert::kassert ALIAS kassert) # Testing and examples are only built if this is the main project or if KASSERT_BUILD_TESTS is set (OFF by default) -if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR KASSERT_BUILD_TESTS) - ensure_googletest() +if (KASSERT_BUILD_TESTS) + # this has to be enabled in the main CMakeLists file + include(CTest) add_subdirectory(tests) endif () diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt deleted file mode 100644 index 8b13789..0000000 --- a/extern/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 54e670c..c091dc1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,4 +1,5 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + include(KassertTestHelper) include(GoogleTest) diff --git a/tests/cmake/KassertTestHelper.cmake b/tests/cmake/KassertTestHelper.cmake index aae4776..d7d66d5 100644 --- a/tests/cmake/KassertTestHelper.cmake +++ b/tests/cmake/KassertTestHelper.cmake @@ -1,4 +1,12 @@ -find_package(googletest REQUIRED) +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest + GIT_TAG v1.15.2 +) + +FetchContent_MakeAvailable(googletest) + include(GoogleTest) @@ -9,12 +17,12 @@ include(GoogleTest) function (kassert_register_test KASSERT_TARGET_NAME) cmake_parse_arguments("KASSERT" "EXCEPTION_MODE" "" "FILES" ${ARGN}) add_executable(${KASSERT_TARGET_NAME} ${KASSERT_FILES}) - target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE gtest gtest_main gmock kassert_base) + target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE gtest gtest_main gmock kassert) target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE kassert_warnings) - gtest_discover_tests(${KASSERT_TARGET_NAME} WORKING_DIRECTORY ${PROJECT_DIR}) + gtest_discover_tests(${KASSERT_TARGET_NAME}) if (KASSERT_EXCEPTION_MODE) - target_compile_options(${KASSERT_TARGET_NAME} PRIVATE -DKASSERT_EXCEPTION_MODE) + set_target_properties(${KASSERT_TARGET_NAME} PROPERTIES KASSERT_EXCEPTION_MODE ON) endif () endfunction () From 4d6ff5b55ea54230908877b14681977d5a55e0d3 Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 7 Feb 2025 12:18:25 +0100 Subject: [PATCH 06/10] CMake installation. --- CMakeLists.txt | 40 ++++++++++++++++++++++++++--- cmake/kassertConfig.cmake | 1 + tests/cmake/KassertTestHelper.cmake | 3 ++- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 cmake/kassertConfig.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b2b5778..435a3b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,9 @@ -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 3.14) project( kassert DESCRIPTION "Assertion library for KaMPIng" LANGUAGES CXX + VERSION 0.2.0 ) # include guard to prevent duplicate targets when including this project as a subdirectory @@ -37,7 +38,7 @@ if (PROJECT_IS_TOP_LEVEL) endif () add_library(kassert INTERFACE) -target_include_directories(kassert INTERFACE include) +target_include_directories(kassert INTERFACE $) # set C++ standard to C++17 target_compile_features(kassert INTERFACE cxx_std_17) @@ -92,9 +93,42 @@ target_compile_definitions(kassert INTERFACE "$<${assertion_level_set}:-DKASSERT add_library(kassert::kassert ALIAS kassert) -# Testing and examples are only built if this is the main project or if KASSERT_BUILD_TESTS is set (OFF by default) +# Tests are only built if KASSERT_BUILD_TESTS is set (OFF by default, ON if this is the top-level project) if (KASSERT_BUILD_TESTS) # this has to be enabled in the main CMakeLists file include(CTest) add_subdirectory(tests) endif () + +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/kassertConfigVersion.cmake + VERSION ${kassert_PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT # requires CMake 3.14 +) +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/kassertConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/kassertConfig.cmake COPYONLY +) + +include(GNUInstallDirs) +install( + TARGETS kassert + EXPORT kassertTargets + INCLUDES + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) +install( + DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILES_MATCHING + PATTERN "*.hpp" +) +install( + EXPORT kassertTargets + FILE kassertTargets.cmake + NAMESPACE kassert:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kassert +) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kassertConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/kassertConfig.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/kassert +) diff --git a/cmake/kassertConfig.cmake b/cmake/kassertConfig.cmake new file mode 100644 index 0000000..1e06456 --- /dev/null +++ b/cmake/kassertConfig.cmake @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/kassertTargets.cmake") diff --git a/tests/cmake/KassertTestHelper.cmake b/tests/cmake/KassertTestHelper.cmake index d7d66d5..b25f084 100644 --- a/tests/cmake/KassertTestHelper.cmake +++ b/tests/cmake/KassertTestHelper.cmake @@ -1,10 +1,11 @@ include(FetchContent) + +set(INSTALL_GTEST OFF) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest GIT_TAG v1.15.2 ) - FetchContent_MakeAvailable(googletest) From e5ced17bd481894d505cef4b097919a388655ade Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 7 Feb 2025 15:16:39 +0100 Subject: [PATCH 07/10] Update README. --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7e3920d..0879a08 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,9 @@ KASSERT(a + a == b, "Under the assumption that a is " << a << ", the world is a ``` Use `THROWING_KASSERT` to throw an exception if the assertion fails. -This requires the CMake option `KASSERT_EXCEPTION_MODE` to be set. -If exception mode is not enabled, `THROWING_KASSERT` acts the same as `KASSERT`. +This requires the target property `KASSERT_EXCEPTION_MODE` to be set +to `ON` on the target linking against `kassert`. If exception mode is +not enabled, `THROWING_KASSERT` acts the same as `KASSERT`. ```c++ THROWING_KASSERT(1 + 1 == 3, "The world is a lie!"); @@ -53,9 +54,15 @@ argument, followed by the remaining arguments `[, ...]` passed to `THROWING_KASS Assertions are enabled if their assertion level (optional third parameter of `KASSERT`) is **less than or equal to** the active assertion level. The default level is `kassert::assert::normal` (30). -Set the CMake variable `KASSERT_ASSERTION_LEVEL` to the numeric value of the desired assertion level. +Set the CMake target property `KASSERT_ASSERTION_LEVEL` on you target linking against `kassert` to the numeric value of the desired assertion level. If omitted, the assertion level is set to `0`, which disables all assertions. +```cmake +add_library(my_code ...) +target_link_libraries(my_code PRIVATE kassert::kassert) +set_target_properties(my_code PROPERTIES KASSERT_ASSERTION_LEVEL 10) +``` + ### Custom Assertion Levels You are free to define your own assertion levels. For instance: From cce26802983bb7358efab103fa01f9f920669b7b Mon Sep 17 00:00:00 2001 From: Niklas Uhl Date: Sun, 9 Feb 2025 13:59:30 +0100 Subject: [PATCH 08/10] Emulate `PROJECT_IS_TOP_LEVEL` for CMake < 3.21. --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 435a3b8..a227944 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,9 +6,17 @@ project( VERSION 0.2.0 ) + # include guard to prevent duplicate targets when including this project as a subdirectory if (TARGET kassert) return() +endif() +if (CMAKE_VERSION VERSION_LESS 3.21) + if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(PROJECT_IS_TOP_LEVEL TRUE) + else () + set(PROJECT_IS_TOP_LEVEL FALSE) + endif () endif () option(KASSERT_BUILD_TESTS "Build the tests." ${PROJECT_IS_TOP_LEVEL}) From 95f0bbc11a0cd353b8276b06fc634527a642ef21 Mon Sep 17 00:00:00 2001 From: Niklas Uhl Date: Sun, 9 Feb 2025 14:13:35 +0100 Subject: [PATCH 09/10] Fix include guard. --- CMakeLists.txt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a227944..bffc8fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,11 @@ project( VERSION 0.2.0 ) - # include guard to prevent duplicate targets when including this project as a subdirectory -if (TARGET kassert) +if (TARGET kassert::kassert) return() endif() + if (CMAKE_VERSION VERSION_LESS 3.21) if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) set(PROJECT_IS_TOP_LEVEL TRUE) @@ -20,9 +20,6 @@ if (CMAKE_VERSION VERSION_LESS 3.21) endif () option(KASSERT_BUILD_TESTS "Build the tests." ${PROJECT_IS_TOP_LEVEL}) -# dependencies -include(FetchContent) - if (PROJECT_IS_TOP_LEVEL) # find Doxygen find_package(Doxygen) From 6839b14d09c47d89c51fc96957d033e7ab1e3497 Mon Sep 17 00:00:00 2001 From: Niklas Uhl Date: Sun, 9 Feb 2025 14:15:36 +0100 Subject: [PATCH 10/10] CMake formatting. --- CMakeLists.txt | 4 ++-- tests/cmake/KassertTestHelper.cmake | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bffc8fd..e923e6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,8 @@ project( # include guard to prevent duplicate targets when including this project as a subdirectory if (TARGET kassert::kassert) return() -endif() - +endif () + if (CMAKE_VERSION VERSION_LESS 3.21) if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) set(PROJECT_IS_TOP_LEVEL TRUE) diff --git a/tests/cmake/KassertTestHelper.cmake b/tests/cmake/KassertTestHelper.cmake index b25f084..7eefdf8 100644 --- a/tests/cmake/KassertTestHelper.cmake +++ b/tests/cmake/KassertTestHelper.cmake @@ -2,13 +2,12 @@ include(FetchContent) set(INSTALL_GTEST OFF) FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest - GIT_TAG v1.15.2 + googletest + GIT_REPOSITORY https://github.com/google/googletest + GIT_TAG v1.15.2 ) FetchContent_MakeAvailable(googletest) - include(GoogleTest) # Convenience wrapper for adding tests for Kassert.