Skip to content

Commit

Permalink
#1602: CMake: enable warnings in tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cz4rs committed Nov 23, 2021
1 parent 98666b0 commit 419599e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
32 changes: 20 additions & 12 deletions cmake/turn_on_warnings.cmake
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
include(CheckCXXCompilerFlag)

function(enable_cxx_compiler_flag_if_supported flag)
macro(add_cxx_compiler_flag_if_supported flag)
check_cxx_compiler_flag("${flag}" flag_supported)
if(flag_supported)
target_compile_options(${vt_target} PRIVATE ${flag})
list(APPEND VT_WARNING_FLAGS "${flag}")
endif()
unset(flag_supported CACHE)
endfunction()
endmacro()

function(turn_on_warnings vt_target)
enable_cxx_compiler_flag_if_supported("-Wall")
enable_cxx_compiler_flag_if_supported("-pedantic")
enable_cxx_compiler_flag_if_supported("-Wshadow")
enable_cxx_compiler_flag_if_supported("-Wno-unknown-pragmas")
enable_cxx_compiler_flag_if_supported("-Wsign-compare")
if(NOT DEFINED VT_WARNING_FLAGS)
add_cxx_compiler_flag_if_supported("-Wall")
add_cxx_compiler_flag_if_supported("-pedantic")
add_cxx_compiler_flag_if_supported("-Wshadow")
add_cxx_compiler_flag_if_supported("-Wno-unknown-pragmas")
add_cxx_compiler_flag_if_supported("-Wsign-compare")
# Not really a warning, is still diagnostic related..
enable_cxx_compiler_flag_if_supported("-ftemplate-backtrace-limit=100")
add_cxx_compiler_flag_if_supported("-ftemplate-backtrace-limit=100")

if (vt_werror_enabled) # Treat warning as errors
enable_cxx_compiler_flag_if_supported("-Werror")
add_cxx_compiler_flag_if_supported("-Werror")
endif()
endfunction()
endif()

set(
VT_WARNING_FLAGS ${VT_WARNING_FLAGS} CACHE INTERNAL
"Project's warning options")

macro(turn_on_warnings vt_target)
target_compile_options(${vt_target} PRIVATE ${VT_WARNING_FLAGS})
endmacro()
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ set(
tutorial
)

include(turn_on_warnings)

macro(add_example example_name)
set(EXAMPLE_FILE "${example_name}.cc")

add_executable(${example_name} ${EXAMPLE_FILE})
add_dependencies(examples ${example_name})

turn_on_warnings(${example_name})

if (vt_unity_build_enabled)
set_target_properties(${example_name} PROPERTIES UNITY_BUILD ON)
endif()
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set_target_properties(gtest_main PROPERTIES FOLDER extern)
#set_target_properties(gmock_main PROPERTIES FOLDER extern)

include(GoogleTest)
include(turn_on_warnings)

function(add_unit_test unit_test_name unit_test_files uses_mpi)
add_executable(
Expand All @@ -76,6 +77,8 @@ function(add_unit_test unit_test_name unit_test_files uses_mpi)
target_include_directories(${unit_test_name} PRIVATE ${PROJECT_TEST_UNIT_DIR})
target_include_directories(${unit_test_name} PRIVATE ${PROJECT_GTEST_INCLUDE_DIR})

turn_on_warnings(${unit_test_name})

link_target_with_vt(
TARGET ${unit_test_name}
DEFAULT_LINK_SET
Expand Down

0 comments on commit 419599e

Please sign in to comment.