NetdataCompilerFlags.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # SPDX-License-Identifier: GPL-3.0-or-later
  2. # Functions to simplify handling of extra compiler flags.
  3. include(CheckCCompilerFlag)
  4. include(CheckCXXCompilerFlag)
  5. # Construct a pre-processor safe name
  6. #
  7. # This takes a specified value, and assigns the generated name to the
  8. # specified target.
  9. function(make_cpp_safe_name value target)
  10. string(REPLACE "-" "_" tmp "${value}")
  11. string(REPLACE "=" "_" tmp "${tmp}")
  12. set(${target} "${tmp}" PARENT_SCOPE)
  13. endfunction()
  14. # Conditionally add an extra compiler flag to C and C++ flags.
  15. #
  16. # If the language flags already match the `match` argument, skip this flag.
  17. # Otherwise, check for support for `flag` and if support is found, add it to
  18. # the compiler flags for the run.
  19. function(add_simple_extra_compiler_flag match flag)
  20. set(CMAKE_REQUIRED_FLAGS "-Werror")
  21. make_cpp_safe_name("${flag}" flag_name)
  22. if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
  23. check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
  24. endif()
  25. if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
  26. check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
  27. endif()
  28. if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
  29. add_compile_options("${flag}")
  30. add_link_options("${flag}")
  31. endif()
  32. endfunction()
  33. # Same as add_simple_extra_compiler_flag, but check for a second flag if the
  34. # first one is unsupported.
  35. function(add_double_extra_compiler_flag match flag1 flag2)
  36. set(CMAKE_REQUIRED_FLAGS "-Werror")
  37. make_cpp_safe_name("${flag1}" flag1_name)
  38. make_cpp_safe_name("${flag2}" flag2_name)
  39. if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
  40. check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name})
  41. if(NOT HAVE_C_${flag1_name})
  42. check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name})
  43. endif()
  44. endif()
  45. if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
  46. check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name})
  47. if(NOT HAVE_CXX_${flag1_name})
  48. check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name})
  49. endif()
  50. endif()
  51. if(HAVE_C_${flag1_name} AND HAVE_CXX_${flag1_name})
  52. add_compile_options("${flag1}")
  53. add_link_options("${flag1}")
  54. elseif(HAVE_C_${flag2_name} AND HAVE_CXX${flag2_name})
  55. add_compile_options("${flag2}")
  56. add_link_options("${flag2}")
  57. endif()
  58. endfunction()
  59. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  60. option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
  61. else()
  62. option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE)
  63. endif()
  64. option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False)
  65. mark_as_advanced(ENABLE_ADDRESS_SANITIZER)
  66. if(ENABLE_ADDRESS_SANITIZER)
  67. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
  68. endif()
  69. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
  70. if(NOT ${DISABLE_HARDENING})
  71. add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector")
  72. add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2")
  73. add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection")
  74. add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full")
  75. add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard")
  76. endif()
  77. foreach(FLAG function-sections data-sections)
  78. add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}")
  79. endforeach()
  80. add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined")
  81. add_simple_extra_compiler_flag("-fexceptions" "-fexceptions")