From c6c34b02d15054ec76e965617cd8798aff3d1dad Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 29 Mar 2022 10:38:34 +0200 Subject: [PATCH] add and use WarningsAsErrors cmake module export cxx_std_20 with target_compile_features() but do not export pedantic Waning options! --- CMakeLists.txt | 9 ++++++--- cmake/WarningsAsErrors.cmake | 12 ++++++++++++ test/CMakeLists.txt | 11 ++--------- 3 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 cmake/WarningsAsErrors.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 71b2af0..70b0670 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,13 +45,16 @@ file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/ # Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface # target: add_library(${PROJECT_NAME} INTERFACE) add_library(${PROJECT_NAME} ${headers} ${sources}) -set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) +target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20) + +# enable compiler warnings +include(cmake/WarningsAsErrors.cmake) # being a cross-platform target, we enforce standards conformance on MSVC -target_compile_options(${PROJECT_NAME} PUBLIC "$<$:/permissive->") +target_compile_options(${PROJECT_NAME} PRIVATE "$<$:/permissive->") # Link dependencies -target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt) +target_link_libraries(${PROJECT_NAME} PUBLIC fmt::fmt) target_include_directories( ${PROJECT_NAME} PUBLIC $ diff --git a/cmake/WarningsAsErrors.cmake b/cmake/WarningsAsErrors.cmake new file mode 100644 index 0000000..b23660c --- /dev/null +++ b/cmake/WarningsAsErrors.cmake @@ -0,0 +1,12 @@ +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC") + target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX) +elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror -Wshadow) + + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 10.0) + target_compile_options(${PROJECT_NAME} PRIVATE -Wdeprecated-copy-dtor -Wnewline-eof) + endif() +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3dd0879..ae27db6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,17 +33,10 @@ endif() file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) add_executable(${PROJECT_NAME} ${sources}) target_link_libraries(${PROJECT_NAME} doctest::doctest Greeter::Greeter) -set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) # enable compiler warnings -if(NOT TEST_INSTALLED_VERSION) - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") - target_compile_options(Greeter PUBLIC -Wall -Wpedantic -Wextra -Werror) - elseif(MSVC) - target_compile_options(Greeter PUBLIC /W4 /WX) - target_compile_definitions(${PROJECT_NAME} PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS) - endif() -endif() +include(../cmake/WarningsAsErrors.cmake) # ---- Add GreeterTests ----