From a68689a8735176f3b608c90be08d9c9169c6b843 Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 20 Oct 2023 09:19:58 +0200 Subject: [PATCH 1/3] 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 6c25343..32deab9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,10 +53,8 @@ target_include_directories(kassert_base INTERFACE include) # set C++ standard to C++17 target_compile_features(kassert_base INTERFACE cxx_std_17) - -list( - APPEND - KASSERT_WARNING_FLAGS +add_library(kassert_warnings INTERFACE) +target_compile_options(kassert_warnings INTERFACE "-Wall" "-Wextra" "-Wconversion" @@ -70,9 +68,7 @@ list( ) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - list( - APPEND - KASSERT_WARNING_FLAGS + target_compile_options(kassert_warnings INTERFACE "-Wcast-align" "-Wnull-dereference" "-Wpedantic" @@ -82,9 +78,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") endif () if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - list( - APPEND - KASSERT_WARNING_FLAGS + target_compile_options(kassert_warnings INTERFACE "-Wcast-align" "-Wnull-dereference" "-Wpedantic" @@ -97,7 +91,7 @@ endif () # OFF by default. if (KASSERT_WARNINGS_ARE_ERRORS) - list(APPEND KASSERT_WARNING_FLAGS "-Werror") + target_compile_options(kassert_warnings INTERFACE "-Werror") endif () # Actual library target with compile definitions diff --git a/tests/cmake/KassertTestHelper.cmake b/tests/cmake/KassertTestHelper.cmake index 466cec6..1f39fef 100644 --- a/tests/cmake/KassertTestHelper.cmake +++ b/tests/cmake/KassertTestHelper.cmake @@ -10,7 +10,7 @@ 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_compile_options(${KASSERT_TARGET_NAME} PRIVATE ${KASSERT_WARNING_FLAGS}) + target_link_libraries(${KASSERT_TARGET_NAME} PRIVATE kassert_warnings) gtest_discover_tests(${KASSERT_TARGET_NAME} WORKING_DIRECTORY ${PROJECT_DIR}) if (KASSERT_EXCEPTION_MODE) From 21949707e149782e1d24f658767fba4558c155c7 Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 20 Oct 2023 10:37:56 +0200 Subject: [PATCH 2/3] Better CMake dependency management. --- .gitmodules | 3 -- CMakeLists.txt | 48 +++++++++++++++++++++++++++++ extern/googletest | 1 - tests/cmake/KassertTestHelper.cmake | 2 +- 4 files changed, 49 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 32deab9..1441260 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,53 @@ project( LANGUAGES CXX ) +# 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) @@ -119,5 +166,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 45a174612fc1394d68658c53ce67471a63915f9a Mon Sep 17 00:00:00 2001 From: Tim Niklas Uhl Date: Fri, 20 Oct 2023 10:43:18 +0200 Subject: [PATCH 3/3] Formatting. --- CMakeLists.txt | 89 +++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1441260..586487d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ project( include(FetchContent) if (POLICY CMP0135) cmake_policy(SET CMP0135 OLD) -endif() +endif () set(fetch_content_deps googletest) set(fetch_content_supported_flags "") @@ -23,34 +23,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) # folder support for IDEs @@ -101,38 +101,37 @@ target_include_directories(kassert_base INTERFACE include) # set C++ standard to C++17 target_compile_features(kassert_base INTERFACE cxx_std_17) add_library(kassert_warnings INTERFACE) -target_compile_options(kassert_warnings INTERFACE - "-Wall" - "-Wextra" - "-Wconversion" - "-Wnon-virtual-dtor" - "-Woverloaded-virtual" - "-Wshadow" - "-Wsign-conversion" - "-Wundef" - "-Wunreachable-code" - "-Wunused" +target_compile_options( + kassert_warnings + INTERFACE "-Wall" + "-Wextra" + "-Wconversion" + "-Wnon-virtual-dtor" + "-Woverloaded-virtual" + "-Wshadow" + "-Wsign-conversion" + "-Wundef" + "-Wunreachable-code" + "-Wunused" ) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - target_compile_options(kassert_warnings INTERFACE - "-Wcast-align" - "-Wnull-dereference" - "-Wpedantic" - "-Wextra-semi" - "-Wno-gnu-zero-variadic-macro-arguments" + target_compile_options( + kassert_warnings INTERFACE "-Wcast-align" "-Wnull-dereference" "-Wpedantic" "-Wextra-semi" + "-Wno-gnu-zero-variadic-macro-arguments" ) endif () if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - target_compile_options(kassert_warnings INTERFACE - "-Wcast-align" - "-Wnull-dereference" - "-Wpedantic" - "-Wnoexcept" - "-Wsuggest-attribute=const" - "-Wsuggest-attribute=noreturn" - "-Wsuggest-override" + target_compile_options( + kassert_warnings + INTERFACE "-Wcast-align" + "-Wnull-dereference" + "-Wpedantic" + "-Wnoexcept" + "-Wsuggest-attribute=const" + "-Wsuggest-attribute=noreturn" + "-Wsuggest-override" ) endif ()