diff options
author | Austin S. Hemmelgarn <austin@netdata.cloud> | 2024-05-01 07:04:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-01 07:04:04 -0400 |
commit | cc33fbe673ed4e593b6debb126d08fee77f1b018 (patch) | |
tree | 43bdf2a0408385c1d71b55e40895b567d43e69cc | |
parent | d8c1537324faa05d73cdaff080ebe05dca84719a (diff) |
Clean up handling of compiler flags in CMake. (#17532)
* Move all handling of compilation flags inot compiler flags module.
Also, make including the module do all the required compiler flag
changes.
* Switch compiler flag handling to (mostly) use properties.
This makes it easier to override individual flags on a per-target
basis and also results in slightly simpler CMake code.
* Fix typos in compiler flag handling.
* Fix missing quotes.
-rw-r--r-- | CMakeLists.txt | 49 | ||||
-rw-r--r-- | packaging/cmake/Modules/NetdataCompilerFlags.cmake | 69 | ||||
-rw-r--r-- | packaging/cmake/Modules/NetdataProtobuf.cmake | 4 |
3 files changed, 51 insertions, 71 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8214e392e5..4ab193b9f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,15 +82,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release") endif() -option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False) -mark_as_advanced(ENABLE_ADDRESS_SANITIZER) - -if(ENABLE_ADDRESS_SANITIZER) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address") -endif() - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexceptions") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}") +include(NetdataCompilerFlags) set(CMAKE_EXPORT_COMPILE_COMMANDS On) @@ -198,45 +190,6 @@ if(NEED_PROTOBUF) endif() # -# handling of extra compiler flags -# - -include(NetdataCompilerFlags) - -# Disable hardening for debug builds by default. -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE) -else() - option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE) -endif() - -set(EXTRA_HARDENING_C_FLAGS "") -set(EXTRA_HARDENING_CXX_FLAGS "") - -set(EXTRA_OPT_C_FLAGS "") -set(EXTRA_OPT_CXX_FLAGS "") - -if(NOT ${DISABLE_HARDENING}) - add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector" EXTRA_HARDENING) - add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2" EXTRA_HARDENING) - add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection" EXTRA_HARDENING) - add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full" EXTRA_HARDENING) - add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard" EXTRA_HARDENING) -endif() - -foreach(FLAG function-sections data-sections) - add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}" EXTRA_OPT) -endforeach() - -add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined" EXTRA_OPT) - -foreach(RELTYP RELEASE DEBUG RELWITHDEBINFO MINSIZEREL) - foreach(L C CXX) - set(CMAKE_${L}_FLAGS_${RELTYP} "${CMAKE_${L}_FLAGS_${RELTYP}} ${EXTRA_HARDENING_C_FLAGS} ${EXTRA_OPT_C_FLAGS}") - endforeach() -endforeach() - -# # detect OS # diff --git a/packaging/cmake/Modules/NetdataCompilerFlags.cmake b/packaging/cmake/Modules/NetdataCompilerFlags.cmake index 894e244ce6..28b43b4ec4 100644 --- a/packaging/cmake/Modules/NetdataCompilerFlags.cmake +++ b/packaging/cmake/Modules/NetdataCompilerFlags.cmake @@ -20,30 +20,29 @@ endfunction() # # If the language flags already match the `match` argument, skip this flag. # Otherwise, check for support for `flag` and if support is found, add it to -# the language-specific `target` flag group. -function(add_simple_extra_compiler_flag match flag target) +# the compiler flags for the run. +function(add_simple_extra_compiler_flag match flag) set(CMAKE_REQUIRED_FLAGS "-Werror") make_cpp_safe_name("${flag}" flag_name) if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) check_c_compiler_flag("${flag}" HAVE_C_${flag_name}) - if(HAVE_C_${flag_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag}" PARENT_SCOPE) - endif() endif() if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name}) - if(HAVE_CXX_${flag_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag}" PARENT_SCOPE) - endif() + endif() + + if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name}) + add_compile_options("${flag}") + add_link_options("${flag}") endif() endfunction() # Same as add_simple_extra_compiler_flag, but check for a second flag if the # first one is unsupported. -function(add_double_extra_compiler_flag match flag1 flag2 target) +function(add_double_extra_compiler_flag match flag1 flag2) set(CMAKE_REQUIRED_FLAGS "-Werror") make_cpp_safe_name("${flag1}" flag1_name) @@ -51,25 +50,53 @@ function(add_double_extra_compiler_flag match flag1 flag2 target) if(NOT ${CMAKE_C_FLAGS} MATCHES ${match}) check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name}) - if(HAVE_C_${flag1_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag1}" PARENT_SCOPE) - else() + if(NOT HAVE_C_${flag1_name}) check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name}) - if(HAVE_C_${flag2_name}) - set(${target}_C_FLAGS "${${target}_C_FLAGS} ${flag2}" PARENT_SCOPE) - endif() endif() endif() if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match}) check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name}) - if(HAVE_CXX_${flag1_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag1}" PARENT_SCOPE) - else() + if(NOT HAVE_CXX_${flag1_name}) check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name}) - if(HAVE_CXX_${flag2_name}) - set(${target}_CXX_FLAGS "${${target}_CXX_FLAGS} ${flag2}" PARENT_SCOPE) - endif() endif() endif() + + if(HAVE_C_${flag1_name} AND HAVE_CXX_${flag1_name}) + add_compile_options("${flag1}") + add_link_options("${flag1}") + elseif(HAVE_C_${flag2_name} AND HAVE_CXX${flag2_name}) + add_compile_options("${flag2}") + add_link_options("${flag2}") + endif() endfunction() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE) +else() + option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE) +endif() + +option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False) +mark_as_advanced(ENABLE_ADDRESS_SANITIZER) + +if(ENABLE_ADDRESS_SANITIZER) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address") +endif() + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}") + +if(NOT ${DISABLE_HARDENING}) + add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector") + add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2") + add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection") + add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full") + add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard") +endif() + +foreach(FLAG function-sections data-sections) + add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}") +endforeach() + +add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined") +add_simple_extra_compiler_flag("-fexecptions" "-fexceptions") diff --git a/packaging/cmake/Modules/NetdataProtobuf.cmake b/packaging/cmake/Modules/NetdataProtobuf.cmake index d4ae3aec61..bc0ffc03be 100644 --- a/packaging/cmake/Modules/NetdataProtobuf.cmake +++ b/packaging/cmake/Modules/NetdataProtobuf.cmake @@ -47,8 +47,8 @@ function(netdata_bundle_protobuf) set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER) - string(REPLACE "-fsanitize=address" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) - string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) + string(REPLACE "-fsanitize=address" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # ignore debhelper set(FETCHCONTENT_FULLY_DISCONNECTED Off) |