Skip to content

Commit

Permalink
add_flag cmake function update
Browse files Browse the repository at this point in the history
  • Loading branch information
qdrvm-ci committed Jul 29, 2024
1 parent 4501df7 commit 718ff88
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
59 changes: 28 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ include(cmake/functions.cmake)

include(cmake/san.cmake)

# check if compiler flags are supported
check_cxx_compiler_flag("-Werror" COMPILER_SUPPORTS_WERROR)

# setup compilation flags
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$")
# cmake-format: off
Expand All @@ -129,35 +126,35 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$")
add_flag(-Wno-format-nonliteral) # prints way too many warnings from spdlog
add_flag(-Wno-gnu-zero-variadic-macro-arguments) # https://stackoverflow.com/questions/21266380/is-the-gnu-zero-variadic-macro-arguments-safe-to-ignore

if(WERROR AND COMPILER_SUPPORTS_WERROR)
add_compile_options(-Werror) # turn all warnings into errors
else()
if(WERROR)
add_flag(-Werror) # turn all warnings into errors
endif()

# cmake-format: on
if ((("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang)$")
AND (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "12.0"))
OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU)$")
AND (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "9.0")))
# use new options format for clang >= 12 and gnu >= 9
# cmake-format: off
add_flag(-Werror=unused-lambda-capture) # error if lambda capture is unused
add_flag(-Werror=return-type) # warning: control reaches end of non-void function [-Wreturn-type]
add_flag(-Werror=sign-compare) # warn the user if they compare a signed and unsigned numbers
# breaks soralog headers
# add_flag(-Werror=reorder) # field '$1' will be initialized after field '$2'
add_flag(-Werror=mismatched-tags) # warning: class '$1' was previously declared as struct
add_flag(-Werror=switch) # unhandled values in a switch statement
# cmake-format: on
else ()
# promote to errors
# cmake-format: off
add_flag(-Werror-unused-lambda-capture) # error if lambda capture is unused
add_flag(-Werror-return-type) # warning: control reaches end of non-void function [-Wreturn-type]
add_flag(-Werror-non-virtual-dtor) # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
add_flag(-Werror-sign-compare) # warn the user if they compare a signed and unsigned numbers
add_flag(-Werror-reorder) # field '$1' will be initialized after field '$2'
add_flag(-Werror-switch) # unhandled values in a switch statement
# cmake-format: on
if ((("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang)$")
AND (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "12.0"))
OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU)$")
AND (${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL "9.0")))
# use new options format for clang >= 12 and gnu >= 9
# cmake-format: off
add_flag(-Werror=unused-lambda-capture) # error if lambda capture is unused
add_flag(-Werror=return-type) # warning: control reaches end of non-void function [-Wreturn-type]
add_flag(-Werror=sign-compare) # warn the user if they compare a signed and unsigned numbers
# breaks soralog headers
# add_flag(-Werror=reorder) # field '$1' will be initialized after field '$2'
add_flag(-Werror=mismatched-tags) # warning: class '$1' was previously declared as struct
add_flag(-Werror=switch) # unhandled values in a switch statement
# cmake-format: on
else ()
# promote to errors
# cmake-format: off
add_flag(-Werror-unused-lambda-capture) # error if lambda capture is unused
add_flag(-Werror-return-type) # warning: control reaches end of non-void function [-Wreturn-type]
add_flag(-Werror-non-virtual-dtor) # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
add_flag(-Werror-sign-compare) # warn the user if they compare a signed and unsigned numbers
add_flag(-Werror-reorder) # field '$1' will be initialized after field '$2'
add_flag(-Werror-switch) # unhandled values in a switch statement
# cmake-format: on
endif ()
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#msvc
Expand Down
5 changes: 3 additions & 2 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ endfunction()

# conditionally applies flag. If flag is supported by current compiler, it will be added to compile options.
function(add_flag flag)
check_cxx_compiler_flag(${flag} FLAG_${flag})
if(FLAG_${flag} EQUAL 1)
string(REPLACE "-" "_" flag_var ${flag})
check_cxx_compiler_flag(${flag} FLAG${flag_var})
if(FLAG${flag_var} EQUAL 1)
add_compile_options(${flag})
endif()
endfunction()
Expand Down

0 comments on commit 718ff88

Please sign in to comment.