Only test compiler flags when needed

Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
This commit is contained in:
Ferry Huberts 2023-08-14 21:48:49 +02:00
parent 0d2985cacd
commit 2a6c8e2692

View File

@ -10,7 +10,19 @@ include(CheckCXXCompilerFlag)
###################################################################################################
#
# Test compiler flags and add them when they are supported
# Add compiler flags without test (assume they are supported)
#
function(add_cxx_compiler_flags_no_test var)
foreach(flag ${ARGN})
set(${var} "${${var}} ${flag}")
endforeach()
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
#
# Add compiler flags with test (do not assume they are supported)
#
function(add_cxx_compiler_flags var)
@ -25,6 +37,25 @@ function(add_cxx_compiler_flags var)
endfunction()
#
# Add compiler flags
#
function(add_compiler_flags version)
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL ${version})
foreach(flag ${ARGN})
add_cxx_compiler_flags_no_test(CMAKE_CXX_FLAGS ${flag})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
endforeach()
else (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL ${version})
foreach(flag ${ARGN})
add_cxx_compiler_flags(CMAKE_CXX_FLAGS ${flag})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
endforeach()
endif (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL ${version})
endfunction()
#
# Remove compiler flags
#
@ -69,8 +100,8 @@ string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
# Generic flags
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wall")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wextra")
add_compiler_flags(4.6 "-Wall")
add_compiler_flags(4.6 "-Wextra")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -78,7 +109,7 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# Preserve debug information flags
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-g3")
add_compiler_flags(4.6 "-g3")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -87,73 +118,73 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# See https://github.com/vadz/gcc-warnings-tools for gcc flags and versions
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wcast-align") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wconversion") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wdouble-promotion") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wformat=2") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Winvalid-pch") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wmissing-declarations") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wmissing-format-attribute") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wmissing-include-dirs") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wpointer-arith") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wredundant-decls") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wshadow") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsign-conversion") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=const") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=noreturn") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=pure") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wswitch-default") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wswitch-enum") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wtype-limits") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wundef") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wuninitialized") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunknown-pragmas") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunsafe-loop-optimizations") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-but-set-parameter") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-but-set-variable") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-function") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-label") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-macros") # gcc 4.6.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-parameter") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-result") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-value") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-variable") # gcc 4.6.0
add_compiler_flags(4.6 "-Wcast-align")
#add_compiler_flags(4.6 "-Wconversion")
#add_compiler_flags(4.6 "-Wdouble-promotion")
add_compiler_flags(4.6 "-Winvalid-pch")
add_compiler_flags(4.6 "-Wmissing-declarations")
add_compiler_flags(4.6 "-Wmissing-format-attribute")
add_compiler_flags(4.6 "-Wmissing-include-dirs")
add_compiler_flags(4.6 "-Wpointer-arith")
add_compiler_flags(4.6 "-Wredundant-decls")
#add_compiler_flags(4.6 "-Wshadow")
#add_compiler_flags(4.6 "-Wsign-conversion")
#add_compiler_flags(4.6 "-Wsuggest-attribute=const")
#add_compiler_flags(4.6 "-Wsuggest-attribute=noreturn")
#add_compiler_flags(4.6 "-Wsuggest-attribute=pure")
add_compiler_flags(4.6 "-Wswitch-default")
add_compiler_flags(4.6 "-Wswitch-enum")
add_compiler_flags(4.6 "-Wtype-limits")
add_compiler_flags(4.6 "-Wundef")
add_compiler_flags(4.6 "-Wuninitialized")
add_compiler_flags(4.6 "-Wunknown-pragmas")
add_compiler_flags(4.6 "-Wunsafe-loop-optimizations")
add_compiler_flags(4.6 "-Wunused-but-set-parameter")
add_compiler_flags(4.6 "-Wunused-but-set-variable")
add_compiler_flags(4.6 "-Wunused-function")
add_compiler_flags(4.6 "-Wunused")
add_compiler_flags(4.6 "-Wunused-label")
add_compiler_flags(4.6 "-Wunused-macros")
#add_compiler_flags(4.6 "-Wunused-parameter")
add_compiler_flags(4.6 "-Wunused-result")
add_compiler_flags(4.6 "-Wunused-value")
add_compiler_flags(4.6 "-Wunused-variable")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wunused-local-typedefs") # gcc 4.7.0
add_compiler_flags(4.7 "-Wunused-local-typedefs")
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=format") # gcc 4.8.0
add_compiler_flags(4.8 "-Wformat=2")
#add_compiler_flags(4.8 "-Wsuggest-attribute=format")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wformat-signedness") # gcc 5.1.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-final-methods") # gcc 5.1.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-final-types") # gcc 5.1.0
add_compiler_flags(5.1 "-Wformat-signedness")
#add_compiler_flags(5.1 "-Wsuggest-final-methods")
#add_compiler_flags(5.1 "-Wsuggest-final-types")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wduplicated-cond") # gcc 6.1.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wlogical-op") # gcc 6.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wnull-dereference") # gcc 6.1.0
add_compiler_flags(6.1 "-Wduplicated-cond")
#add_compiler_flags(6.1 "-Wlogical-op")
add_compiler_flags(6.1 "-Wnull-dereference")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wduplicated-branches") # gcc 7.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wformat-overflow=2") # gcc 7.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wformat-truncation=2") # gcc 7.1.0
add_compiler_flags(7.1 "-Wduplicated-branches")
add_compiler_flags(7.1 "-Wformat-overflow=2")
add_compiler_flags(7.1 "-Wformat-truncation=2")
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wcast-align=strict") # gcc 8.1.0
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=cold") # gcc 8.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-attribute=malloc") # gcc 8.1.0
#add_compiler_flags(8.1 "-Wcast-align=strict")
#add_compiler_flags(8.1 "-Wsuggest-attribute=cold")
add_compiler_flags(8.1 "-Wsuggest-attribute=malloc")
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Warith-conversion") # gcc 10.1.0
#add_compiler_flags(10.1 "-Warith-conversion")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wctad-maybe-unsupported") # gcc 11.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wdeprecated-enum-enum-conversion") # gcc 11.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wdeprecated-enum-float-conversion") # gcc 11.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Winvalid-imported-macros") # gcc 11.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wrange-loop-construct") # gcc 11.1.0
add_compiler_flags(11.1 "-Wctad-maybe-unsupported")
add_compiler_flags(11.1 "-Wdeprecated-enum-enum-conversion")
add_compiler_flags(11.1 "-Wdeprecated-enum-float-conversion")
add_compiler_flags(11.1 "-Winvalid-imported-macros")
add_compiler_flags(11.1 "-Wrange-loop-construct")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Warray-compare") # gcc 12.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wbidi-chars=unpaired,ucn") # gcc 12.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Winfinite-recursion") # gcc 12.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wopenacc-parallelism") # gcc 12.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-ftrivial-auto-var-init=zero") # gcc 12.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wtrivial-auto-var-init") # gcc 12.1.0
add_compiler_flags(12.1 "-ftrivial-auto-var-init=zero")
add_compiler_flags(12.1 "-Warray-compare")
add_compiler_flags(12.1 "-Wbidi-chars=unpaired,ucn")
add_compiler_flags(12.1 "-Winfinite-recursion")
add_compiler_flags(12.1 "-Wopenacc-parallelism")
add_compiler_flags(12.1 "-Wtrivial-auto-var-init")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -162,29 +193,29 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# See https://github.com/vadz/gcc-warnings-tools for gcc flags and versions
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wctor-dtor-privacy") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wnoexcept") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wold-style-cast") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Woverloaded-virtual") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsign-promo") # gcc 4.6.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wstrict-null-sentinel") # gcc 4.6.0
add_compiler_flags(4.6 "-Wctor-dtor-privacy")
add_compiler_flags(4.6 "-Wnoexcept")
add_compiler_flags(4.6 "-Wold-style-cast")
add_compiler_flags(4.6 "-Woverloaded-virtual")
add_compiler_flags(4.6 "-Wsign-promo")
add_compiler_flags(4.6 "-Wstrict-null-sentinel")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wzero-as-null-pointer-constant") # gcc 4.7.0
add_compiler_flags(4.7 "-Wzero-as-null-pointer-constant")
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wuseless-cast") # gcc 4.8.0
#add_compiler_flags(4.8 "-Wuseless-cast")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wconditionally-supported") # gcc 4.9.0
add_compiler_flags(4.9 "-Wconditionally-supported")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wsuggest-override") # gcc 5.1.0
add_compiler_flags(5.1 "-Wsuggest-override")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wnoexcept-type") # gcc 7.1.0
add_compiler_flags(7.1 "-Wnoexcept-type")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wextra-semi") # gcc 8.1.0
add_compiler_flags(8.1 "-Wextra-semi")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wcomma-subscript") # gcc 10.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wmismatched-tags") # gcc 10.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wredundant-tags") # gcc 10.1.0
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wvolatile") # gcc 10.1.0
add_compiler_flags(10.1 "-Wcomma-subscript")
add_compiler_flags(10.1 "-Wmismatched-tags")
add_compiler_flags(10.1 "-Wredundant-tags")
add_compiler_flags(10.1 "-Wvolatile")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -192,10 +223,10 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# Allow the linker to remove unused data and functions
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fdata-sections")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-ffunction-sections")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fno-common")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wl,--gc-sections")
add_compiler_flags(4.6 "-fdata-sections")
add_compiler_flags(4.6 "-ffunction-sections")
add_compiler_flags(4.6 "-fno-common")
add_compiler_flags(4.6 "-Wl,--gc-sections")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -205,21 +236,22 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
IF (NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-D_FORTIFY_SOURCE=2")
add_cxx_compiler_flags_no_test(CMAKE_CXX_FLAGS "-D_FORTIFY_SOURCE=2")
ENDIF (NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-D_GLIBCXX_ASSERTIONS")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fasynchronous-unwind-tables")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fexceptions")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fstack-clash-protection")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fstack-protector-strong")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-grecord-gcc-switches")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fcf-protection")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-pipe")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wformat=2 -Werror=format-security -Wno-format-nonliteral")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fPIE")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wl,-z,defs")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wl,-z,now")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wl,-z,relro")
add_cxx_compiler_flags_no_test(CMAKE_CXX_FLAGS "-D_GLIBCXX_ASSERTIONS")
add_compiler_flags(4.6 "-fasynchronous-unwind-tables")
add_compiler_flags(4.6 "-fexceptions")
add_compiler_flags(4.6 "-fstack-clash-protection")
add_compiler_flags(4.6 "-fstack-protector-strong")
add_compiler_flags(4.6 "-grecord-gcc-switches")
add_compiler_flags(4.6 "-fcf-protection")
add_compiler_flags(4.6 "-pipe")
add_compiler_flags(4.6 "-Werror=format-security")
add_compiler_flags(4.6 "-Wno-format-nonliteral")
add_compiler_flags(4.6 "-fPIE")
add_compiler_flags(4.6 "-Wl,-z,defs")
add_compiler_flags(4.6 "-Wl,-z,now")
add_compiler_flags(4.6 "-Wl,-z,relro")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -228,7 +260,7 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# See https://fedoraproject.org/wiki/Changes/fno-omit-frame-pointer
#
#if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fno-omit-frame-pointer")
# add_compiler_flags(4.6 "-fno-omit-frame-pointer")
#endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -236,11 +268,14 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# Disable some warnings
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wno-dangling-reference")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wno-pessimizing-move")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wno-sign-compare")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wno-suggest-attribute=format")
add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-Wno-unused-parameter")
add_compiler_flags(4.6 "-Wno-sign-compare")
add_compiler_flags(4.6 "-Wno-unused-parameter")
add_compiler_flags(4.8 "-Wno-suggest-attribute=format")
add_compiler_flags(9.1 "-Wno-pessimizing-move")
add_compiler_flags(13.0 "-Wno-dangling-reference")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
@ -248,6 +283,6 @@ endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# Sanitize flags
#
#if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fsanitize=address")
# add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fsanitize=thread")
# add_compiler_flags(4.6 "-fsanitize=address")
# add_compiler_flags(4.6 "-fsanitize=thread")
#endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)