Merge pull request #816 from fhuberts/compiler-flags-are-for-gcc

Compiler flags are for gcc
This commit is contained in:
Leonid Stryzhevskyi 2023-08-16 02:17:01 +03:00 committed by GitHub
commit 96bff7a7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,3 @@
#cmake_minimum_required(VERSION 3.1)
###################################################################################################
## INCLUDES #######################################################################################
###################################################################################################
@ -11,6 +9,22 @@ include(CheckCXXCompilerFlag)
## FUNCTIONS ######################################################################################
###################################################################################################
#
# 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)
foreach(flag ${ARGN})
string(REGEX REPLACE "[^a-zA-Z0-9]+" "_" flag_var "CXXFLAG_${flag}")
@ -22,13 +36,30 @@ function(add_cxx_compiler_flags var)
set(${var} "${${var}}" PARENT_SCOPE)
endfunction()
function(add_compiler_flags)
foreach(flag ${ARGN})
add_cxx_compiler_flags(CMAKE_CXX_FLAGS ${flag})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
endforeach()
#
# 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
#
function(remove_any_compiler_flags var)
foreach(flag ${ARGN})
string(REPLACE "${flag}" "" ${var} "${${var}}")
@ -44,6 +75,11 @@ function(remove_compiler_flags)
endforeach()
endfunction()
#
# Save and Restore compiler flags
#
function(save_compiler_flags)
set(CMAKE_CXX_FLAGS_SAVED "${CMAKE_CXX_FLAGS}" CACHE STRING "Saved compiler C++ flags" FORCE)
endfunction()
@ -53,6 +89,7 @@ function(restore_compiler_flags)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
endfunction()
###################################################################################################
## COMPILER FLAGS #################################################################################
###################################################################################################
@ -62,158 +99,190 @@ string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
#
# Generic flags
#
add_compiler_flags("-Wall")
add_compiler_flags("-Wextra")
add_compiler_flags("-g3")
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_compiler_flags(4.6 "-Wall")
add_compiler_flags(4.6 "-Wextra")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
# Preserve debug information flags
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_compiler_flags(4.6 "-g3")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
# C/C++ Warning Flags
# See https://github.com/vadz/gcc-warnings-tools for gcc flags and versions
#
add_compiler_flags("-Wcast-align") # gcc 4.6.0
#add_compiler_flags("-Wconversion") # gcc 4.6.0
#add_compiler_flags("-Wdouble-promotion") # gcc 4.6.0
add_compiler_flags("-Wformat=2") # gcc 4.6.0
add_compiler_flags("-Winvalid-pch") # gcc 4.6.0
add_compiler_flags("-Wmissing-declarations") # gcc 4.6.0
add_compiler_flags("-Wmissing-format-attribute") # gcc 4.6.0
add_compiler_flags("-Wmissing-include-dirs") # gcc 4.6.0
add_compiler_flags("-Wpointer-arith") # gcc 4.6.0
add_compiler_flags("-Wredundant-decls") # gcc 4.6.0
#add_compiler_flags("-Wshadow") # gcc 4.6.0
#add_compiler_flags("-Wsign-conversion") # gcc 4.6.0
#add_compiler_flags("-Wsuggest-attribute=const") # gcc 4.6.0
#add_compiler_flags("-Wsuggest-attribute=noreturn") # gcc 4.6.0
#add_compiler_flags("-Wsuggest-attribute=pure") # gcc 4.6.0
add_compiler_flags("-Wswitch-default") # gcc 4.6.0
add_compiler_flags("-Wswitch-enum") # gcc 4.6.0
add_compiler_flags("-Wtype-limits") # gcc 4.6.0
add_compiler_flags("-Wundef") # gcc 4.6.0
add_compiler_flags("-Wuninitialized") # gcc 4.6.0
add_compiler_flags("-Wunknown-pragmas") # gcc 4.6.0
add_compiler_flags("-Wunsafe-loop-optimizations") # gcc 4.6.0
add_compiler_flags("-Wunused-but-set-parameter") # gcc 4.6.0
add_compiler_flags("-Wunused-but-set-variable") # gcc 4.6.0
add_compiler_flags("-Wunused-function") # gcc 4.6.0
add_compiler_flags("-Wunused") # gcc 4.6.0
add_compiler_flags("-Wunused-label") # gcc 4.6.0
add_compiler_flags("-Wunused-macros") # gcc 4.6.0
#add_compiler_flags("-Wunused-parameter") # gcc 4.6.0
add_compiler_flags("-Wunused-result") # gcc 4.6.0
add_compiler_flags("-Wunused-value") # gcc 4.6.0
add_compiler_flags("-Wunused-variable") # gcc 4.6.0
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
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_compiler_flags("-Wunused-local-typedefs") # gcc 4.7.0
add_compiler_flags(4.7 "-Wunused-local-typedefs")
#add_compiler_flags("-Wsuggest-attribute=format") # gcc 4.8.0
add_compiler_flags(4.8 "-Wformat=2")
#add_compiler_flags(4.8 "-Wsuggest-attribute=format")
add_compiler_flags("-Wformat-signedness") # gcc 5.1.0
#add_compiler_flags("-Wsuggest-final-methods") # gcc 5.1.0
#add_compiler_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_compiler_flags("-Wduplicated-cond") # gcc 6.1.0
#add_compiler_flags("-Wlogical-op") # gcc 6.1.0
add_compiler_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_compiler_flags("-Wduplicated-branches") # gcc 7.1.0
add_compiler_flags("-Wformat-overflow=2") # gcc 7.1.0
add_compiler_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_compiler_flags("-Wcast-align=strict") # gcc 8.1.0
#add_compiler_flags("-Wsuggest-attribute=cold") # gcc 8.1.0
add_compiler_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_compiler_flags("-Warith-conversion") # gcc 10.1.0
#add_compiler_flags(10.1 "-Warith-conversion")
add_compiler_flags("-Wctad-maybe-unsupported") # gcc 11.1.0
add_compiler_flags("-Wdeprecated-enum-enum-conversion") # gcc 11.1.0
add_compiler_flags("-Wdeprecated-enum-float-conversion") # gcc 11.1.0
add_compiler_flags("-Winvalid-imported-macros") # gcc 11.1.0
add_compiler_flags("-Wrange-loop-construct") # gcc 11.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)
add_compiler_flags("-Warray-compare") # gcc 12.1.0
add_compiler_flags("-Wbidi-chars=unpaired,ucn") # gcc 12.1.0
add_compiler_flags("-Winfinite-recursion") # gcc 12.1.0
add_compiler_flags("-Wopenacc-parallelism") # gcc 12.1.0
add_compiler_flags("-ftrivial-auto-var-init=zero") # gcc 12.1.0
add_compiler_flags("-Wtrivial-auto-var-init") # gcc 12.1.0
#
# C++ Warning Flags
# See https://github.com/vadz/gcc-warnings-tools for gcc flags and versions
#
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
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
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_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")
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")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
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
#
# Allow the linker to remove unused data and functions
#
add_compiler_flags("-fdata-sections")
add_compiler_flags("-ffunction-sections")
add_compiler_flags("-fno-common")
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_compiler_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)
#
# Hardening flags (see https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc)
#
IF (NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
add_compiler_flags("-D_FORTIFY_SOURCE=2")
ENDIF (NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
add_compiler_flags("-D_GLIBCXX_ASSERTIONS")
add_compiler_flags("-fasynchronous-unwind-tables")
add_compiler_flags("-fexceptions")
add_compiler_flags("-fstack-clash-protection")
add_compiler_flags("-fstack-protector-strong")
add_compiler_flags("-grecord-gcc-switches")
add_compiler_flags("-fcf-protection")
add_compiler_flags("-pipe")
add_compiler_flags("-Wformat=2 -Werror=format-security -Wno-format-nonliteral")
add_compiler_flags("-fPIE")
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_compiler_flags("-Wl,-z,defs")
add_compiler_flags("-Wl,-z,now")
add_compiler_flags("-Wl,-z,relro")
endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
# Profiling and Debugging (see https://fedoraproject.org/wiki/Changes/fno-omit-frame-pointer)
# Hardening flags
# See https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc
#
#add_compiler_flags("-fno-omit-frame-pointer")
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
IF (NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
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_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)
#
# Profiling and Debugging
# See https://fedoraproject.org/wiki/Changes/fno-omit-frame-pointer
#
#if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# add_compiler_flags(4.6 "-fno-omit-frame-pointer")
#endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
# Disable some warnings
#
add_compiler_flags("-Wno-dangling-reference")
add_compiler_flags("-Wno-pessimizing-move")
add_compiler_flags("-Wno-sign-compare")
add_compiler_flags("-Wno-suggest-attribute=format")
add_compiler_flags("-Wno-unused-parameter")
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
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)
#
# Sanitize flags
#
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fsanitize=address")
#add_cxx_compiler_flags(CMAKE_CXX_FLAGS "-fsanitize=thread")
#if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# add_compiler_flags(4.6 "-fsanitize=address")
# add_compiler_flags(4.6 "-fsanitize=thread")
#endif (CMAKE_CXX_COMPILER_ID MATCHES GNU)