NetdataCompilerFlags.cmake 3.8 KB

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