compiler_flags.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Those are flags not to test for CXX compiler.
  2. list(APPEND CXX_EXCLUDED_OPTIONS
  3. -Werror=implicit-int
  4. -Wold-style-definition
  5. -Wno-pointer-sign
  6. )
  7. ########################################################
  8. # Setting compiler properties for gcc / g++ compilers. #
  9. ########################################################
  10. #####################################################
  11. # This section covers flags related to optimization #
  12. #####################################################
  13. set_compiler_property(PROPERTY no_optimization -O0)
  14. if(CMAKE_C_COMPILER_VERSION VERSION_LESS "4.8.0")
  15. set_compiler_property(PROPERTY optimization_debug -O0)
  16. else()
  17. set_compiler_property(PROPERTY optimization_debug -Og)
  18. endif()
  19. set_compiler_property(PROPERTY optimization_speed -O2)
  20. set_compiler_property(PROPERTY optimization_size -Os)
  21. #######################################################
  22. # This section covers flags related to warning levels #
  23. #######################################################
  24. # GCC Option standard warning base in Zephyr
  25. set_compiler_property(PROPERTY warning_base
  26. -Wall
  27. -Wformat
  28. -Wformat-security
  29. -Wno-format-zero-length
  30. -Wno-main
  31. )
  32. check_set_compiler_property(APPEND PROPERTY warning_base -Wno-pointer-sign)
  33. # Prohibit void pointer arithmetic. Illegal in C99
  34. check_set_compiler_property(APPEND PROPERTY warning_base -Wpointer-arith)
  35. # not portable
  36. check_set_compiler_property(APPEND PROPERTY warning_base -Wexpansion-to-defined)
  37. if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "9.1.0")
  38. set_compiler_property(APPEND PROPERTY warning_base
  39. # FIXME: Remove once #16587 is fixed
  40. -Wno-address-of-packed-member
  41. )
  42. endif()
  43. # GCC options for warning levels 1, 2, 3, when using `-DW=[1|2|3]`
  44. set_compiler_property(PROPERTY warning_dw_1
  45. -Waggregate-return
  46. -Wcast-align
  47. -Wdisabled-optimization
  48. -Wnested-externs
  49. -Wshadow
  50. )
  51. check_set_compiler_property(APPEND PROPERTY warning_dw_1
  52. -Wlogical-op
  53. -Wmissing-field-initializers
  54. )
  55. set_compiler_property(PROPERTY warning_dw_2
  56. -Wbad-function-cast
  57. -Wcast-qual
  58. -Wconversion
  59. -Wpacked
  60. -Wpadded
  61. -Wpointer-arith
  62. -Wredundant-decls
  63. -Wswitch-default
  64. )
  65. check_set_compiler_property(APPEND PROPERTY warning_dw_2
  66. -Wpacked-bitfield-compat
  67. -Wvla
  68. )
  69. set_compiler_property(PROPERTY warning_dw_3
  70. -Wbad-function-cast
  71. -Wcast-qual
  72. -Wconversion
  73. -Wpacked
  74. -Wpadded
  75. -Wpointer-arith
  76. -Wredundant-decls
  77. -Wswitch-default
  78. )
  79. check_set_compiler_property(APPEND PROPERTY warning_dw_3
  80. -Wpacked-bitfield-compat
  81. -Wvla
  82. )
  83. check_set_compiler_property(PROPERTY warning_extended -Wno-unused-but-set-variable)
  84. check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int)
  85. check_set_compiler_property(PROPERTY warning_error_all -Werror)
  86. set_compiler_property(PROPERTY warning_error_misra_sane -Werror=vla)
  87. set_compiler_property(PROPERTY warning_error_coding_guideline
  88. -Werror=vla
  89. -Wimplicit-fallthrough=2
  90. -Wconversion
  91. -Woverride-init
  92. )
  93. ###########################################################################
  94. # This section covers flags related to C or C++ standards / standard libs #
  95. ###########################################################################
  96. # GCC compiler flags for C standard. The specific standard must be appended by user.
  97. set_compiler_property(PROPERTY cstd -std=)
  98. if (NOT CONFIG_NEWLIB_LIBC AND
  99. NOT COMPILER STREQUAL "xcc" AND
  100. NOT ZEPHYR_TOOLCHAIN_VARIANT STREQUAL "espressif" AND
  101. NOT CONFIG_NATIVE_APPLICATION)
  102. set_compiler_property(PROPERTY nostdinc -nostdinc)
  103. set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})
  104. endif()
  105. set_compiler_property(TARGET compiler-cpp PROPERTY nostdincxx "-nostdinc++")
  106. # Required C++ flags when using gcc
  107. set_property(TARGET compiler-cpp PROPERTY required "-fcheck-new")
  108. # GCC compiler flags for C++ dialects
  109. set_property(TARGET compiler-cpp PROPERTY dialect_cpp98 "-std=c++98")
  110. set_property(TARGET compiler-cpp PROPERTY dialect_cpp11 "-std=c++11" "-Wno-register")
  111. set_property(TARGET compiler-cpp PROPERTY dialect_cpp14 "-std=c++14" "-Wno-register")
  112. set_property(TARGET compiler-cpp PROPERTY dialect_cpp17 "-std=c++17" "-Wno-register")
  113. set_property(TARGET compiler-cpp PROPERTY dialect_cpp2a "-std=c++2a"
  114. "-Wno-register" "-Wno-volatile")
  115. set_property(TARGET compiler-cpp PROPERTY dialect_cpp20 "-std=c++20"
  116. "-Wno-register" "-Wno-volatile")
  117. set_property(TARGET compiler-cpp PROPERTY dialect_cpp2b "-std=c++2b"
  118. "-Wno-register" "-Wno-volatile")
  119. # Disable exeptions flag in C++
  120. set_property(TARGET compiler-cpp PROPERTY no_exceptions "-fno-exceptions")
  121. # Disable rtti in C++
  122. set_property(TARGET compiler-cpp PROPERTY no_rtti "-fno-rtti")
  123. ###################################################
  124. # This section covers all remaining C / C++ flags #
  125. ###################################################
  126. # gcc flags for coverage generation
  127. set_compiler_property(PROPERTY coverage -fprofile-arcs -ftest-coverage -fno-inline)
  128. # Security canaries.
  129. set_compiler_property(PROPERTY security_canaries -fstack-protector-all)
  130. # Only a valid option with GCC 7.x and above, so let's do check and set.
  131. check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=global)
  132. if(NOT CONFIG_NO_OPTIMIZATIONS)
  133. # _FORTIFY_SOURCE: Detect common-case buffer overflows for certain functions
  134. # _FORTIFY_SOURCE=1 : Compile-time checks (requires -O1 at least)
  135. # _FORTIFY_SOURCE=2 : Additional lightweight run-time checks
  136. set_compiler_property(PROPERTY security_fortify _FORTIFY_SOURCE=2)
  137. endif()
  138. # gcc flag for a hosted (no-freestanding) application
  139. check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding)
  140. # gcc flag for a freestandingapplication
  141. set_compiler_property(PROPERTY freestanding -ffreestanding)
  142. # Flag to enable debugging
  143. set_compiler_property(PROPERTY debug -g)
  144. # GCC 11 by default emits DWARF version 5 which cannot be parsed by
  145. # pyelftools. Can be removed once pyelftools supports v5.
  146. check_set_compiler_property(APPEND PROPERTY debug -gdwarf-4)
  147. set_compiler_property(PROPERTY no_common -fno-common)
  148. # GCC compiler flags for imacros. The specific header must be appended by user.
  149. set_compiler_property(PROPERTY imacros -imacros)
  150. # GCC compiler flags for sanitizing.
  151. set_compiler_property(PROPERTY sanitize_address -fsanitize=address)
  152. set_compiler_property(PROPERTY sanitize_undefined -fsanitize=undefined)
  153. # GCC compiler flag for turning off thread-safe initialization of local statics
  154. set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics "-fno-threadsafe-statics")
  155. # Required ASM flags when using gcc
  156. set_property(TARGET asm PROPERTY required "-xassembler-with-cpp")
  157. # gcc flag for colourful diagnostic messages
  158. if (NOT COMPILER STREQUAL "xcc")
  159. set_compiler_property(PROPERTY diagnostic -fdiagnostics-color=always)
  160. endif()