enforce standards conformance on MSVC and fix warning flags (#10)
* enforce standards conformance on MSVC * add \W4 flag for test compilation * enable compiler warnings for the library target when building tests * update note on testing frameworks * turns out only clang understands strongly typed enums
This commit is contained in:
parent
a2a6674101
commit
a4881dda8b
3 changed files with 20 additions and 15 deletions
|
@ -10,7 +10,7 @@ project(Greeter
|
||||||
|
|
||||||
# ---- Include guards ----
|
# ---- Include guards ----
|
||||||
|
|
||||||
if(${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR})
|
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
|
||||||
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.")
|
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
@ -35,17 +35,20 @@ CPMAddPackage(
|
||||||
|
|
||||||
# ---- Create library ----
|
# ---- Create library ----
|
||||||
|
|
||||||
# Note: for single header libraries create an interface target instead:
|
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface target:
|
||||||
# add_library(Greeter INTERFACE)
|
# add_library(Greeter INTERFACE)
|
||||||
# set_target_properties(Greeter PROPERTIES INTERFACE_COMPILE_FEATURES cxx_std_17)
|
# set_target_properties(Greeter PROPERTIES INTERFACE_COMPILE_FEATURES cxx_std_17)
|
||||||
|
|
||||||
add_library(Greeter ${headers} ${sources})
|
add_library(Greeter ${headers} ${sources})
|
||||||
|
|
||||||
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
|
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17)
|
||||||
|
|
||||||
|
# beeing a cross-platform target, we enforce enforce standards conformance on MSVC
|
||||||
|
target_compile_options(Greeter PUBLIC "$<$<BOOL:${MSVC}>:/permissive->")
|
||||||
|
|
||||||
# Link dependencies (if required)
|
# Link dependencies (if required)
|
||||||
# target_link_libraries(Greeter PUBLIC cxxopts)
|
# target_link_libraries(Greeter PUBLIC cxxopts)
|
||||||
|
|
||||||
# Note: change PUBLIC to INTERFACE for single header libraries
|
|
||||||
target_include_directories(Greeter
|
target_include_directories(Greeter
|
||||||
PUBLIC
|
PUBLIC
|
||||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
|
||||||
|
|
|
@ -6,10 +6,7 @@ Greeter::Greeter(std::string _name) : name(_name) {}
|
||||||
|
|
||||||
std::string Greeter::greet(LanguageCode lang) const {
|
std::string Greeter::greet(LanguageCode lang) const {
|
||||||
switch (lang) {
|
switch (lang) {
|
||||||
#if defined(_WIN32) || defined(WIN32)
|
|
||||||
// this silences a MSVC warning as it does not seem to understand strongly-typed enums
|
|
||||||
default:
|
default:
|
||||||
#endif
|
|
||||||
case LanguageCode::EN:
|
case LanguageCode::EN:
|
||||||
return "Hello, " + name + "!";
|
return "Hello, " + name + "!";
|
||||||
case LanguageCode::DE:
|
case LanguageCode::DE:
|
||||||
|
|
|
@ -40,26 +40,31 @@ file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
|
||||||
add_executable(GreeterTests ${sources})
|
add_executable(GreeterTests ${sources})
|
||||||
target_link_libraries(GreeterTests doctest Greeter)
|
target_link_libraries(GreeterTests doctest Greeter)
|
||||||
|
|
||||||
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
|
||||||
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-Wall -pedantic -Wextra -Werror")
|
|
||||||
else()
|
# enable compiler warnings
|
||||||
set_target_properties(GreeterTests PROPERTIES CXX_STANDARD 17)
|
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 -pedantic -Wextra -Werror)
|
||||||
|
elseif(MSVC)
|
||||||
|
target_compile_options(Greeter PUBLIC /W4)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# ---- Add GreeterTests ----
|
# ---- Add GreeterTests ----
|
||||||
|
|
||||||
ENABLE_TESTING()
|
ENABLE_TESTING()
|
||||||
|
|
||||||
# use doctest_discover_tests for better CTest support
|
# Note: doctest and similar testing frameworks can automatically configure CMake tests
|
||||||
|
# For other testing frameworks add the tests target instead:
|
||||||
|
# ADD_TEST(GreeterTests GreeterTests)
|
||||||
|
|
||||||
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
|
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
|
||||||
doctest_discover_tests(GreeterTests)
|
doctest_discover_tests(GreeterTests)
|
||||||
|
|
||||||
# Note: for general testing frameworks use:
|
|
||||||
# ADD_TEST(GreeterTests GreeterTests)
|
|
||||||
|
|
||||||
# ---- code coverage ----
|
# ---- code coverage ----
|
||||||
|
|
||||||
if (ENABLE_TEST_COVERAGE)
|
if (ENABLE_TEST_COVERAGE)
|
||||||
set_target_properties(Greeter PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS "-O0 -g -fprofile-arcs -ftest-coverage --coverage")
|
target_compile_options(Greeter PRIVATE -O0 -g -fprofile-arcs -ftest-coverage --coverage)
|
||||||
target_link_options(Greeter PUBLIC "--coverage")
|
target_link_options(Greeter PUBLIC "--coverage")
|
||||||
endif()
|
endif()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue