1# Those are flags not to test for CXX compiler. 2list(APPEND CXX_EXCLUDED_OPTIONS 3 -Werror=implicit-int 4 -Wold-style-definition 5 -Wno-pointer-sign 6) 7 8######################################################## 9# Setting compiler properties for gcc / g++ compilers. # 10######################################################## 11 12##################################################### 13# This section covers flags related to optimization # 14##################################################### 15set_compiler_property(PROPERTY no_optimization -O0) 16if(CMAKE_C_COMPILER_VERSION VERSION_LESS "4.8.0") 17 set_compiler_property(PROPERTY optimization_debug -O0) 18else() 19 set_compiler_property(PROPERTY optimization_debug -Og) 20endif() 21set_compiler_property(PROPERTY optimization_speed -O2) 22set_compiler_property(PROPERTY optimization_size -Os) 23set_compiler_property(PROPERTY optimization_size_aggressive -Oz) 24 25if(CMAKE_C_COMPILER_VERSION GREATER_EQUAL "4.5.0") 26 set_compiler_property(PROPERTY optimization_lto -flto=auto) 27 set_compiler_property(PROPERTY prohibit_lto -fno-lto) 28endif() 29 30####################################################### 31# This section covers flags related to warning levels # 32####################################################### 33 34# GCC Option standard warning base in Zephyr 35check_set_compiler_property(PROPERTY warning_base 36 -Wall 37 "SHELL:-Wformat -Wformat-security" 38 "SHELL:-Wformat -Wno-format-zero-length" 39) 40 41# C implicit promotion rules will want to make floats into doubles very easily 42check_set_compiler_property(APPEND PROPERTY warning_base -Wdouble-promotion) 43 44check_set_compiler_property(APPEND PROPERTY warning_base -Wno-pointer-sign) 45 46# Prohibit void pointer arithmetic. Illegal in C99 47check_set_compiler_property(APPEND PROPERTY warning_base -Wpointer-arith) 48 49# not portable 50check_set_compiler_property(APPEND PROPERTY warning_base -Wexpansion-to-defined) 51 52# GCC options for warning levels 1, 2, 3, when using `-DW=[1|2|3]` 53set_compiler_property(PROPERTY warning_dw_1 54 -Waggregate-return 55 -Wcast-align 56 -Wdisabled-optimization 57 -Wnested-externs 58 -Wshadow 59) 60check_set_compiler_property(APPEND PROPERTY warning_dw_1 61 -Wlogical-op 62 -Wno-missing-field-initializers 63) 64 65set_compiler_property(PROPERTY warning_dw_2 66 -Wbad-function-cast 67 -Wcast-qual 68 -Wconversion 69 -Wpacked 70 -Wpadded 71 -Wpointer-arith 72 -Wredundant-decls 73 -Wswitch-default 74 -Wmissing-field-initializers 75) 76check_set_compiler_property(APPEND PROPERTY warning_dw_2 77 -Wpacked-bitfield-compat 78 -Wvla 79) 80set_compiler_property(PROPERTY warning_dw_3 81 -Wbad-function-cast 82 -Wcast-qual 83 -Wconversion 84 -Wpacked 85 -Wpadded 86 -Wpointer-arith 87 -Wredundant-decls 88 -Wswitch-default 89) 90check_set_compiler_property(APPEND PROPERTY warning_dw_3 91 -Wpacked-bitfield-compat 92 -Wvla 93) 94 95check_set_compiler_property(PROPERTY warning_extended -Wno-unused-but-set-variable) 96 97check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int) 98 99set_compiler_property(PROPERTY warning_error_misra_sane -Werror=vla) 100 101set_compiler_property(PROPERTY warning_error_coding_guideline 102 -Werror=vla 103 -Wimplicit-fallthrough=2 104 -Wconversion 105 -Woverride-init 106) 107 108########################################################################### 109# This section covers flags related to C or C++ standards / standard libs # 110########################################################################### 111 112# GCC compiler flags for C standard. The specific standard must be appended by user. 113set_compiler_property(PROPERTY cstd -std=) 114 115if (NOT CONFIG_NEWLIB_LIBC AND 116 NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND 117 NOT COMPILER STREQUAL "xcc" AND 118 NOT CONFIG_HAS_ESPRESSIF_HAL AND 119 NOT CONFIG_NATIVE_BUILD) 120 set_compiler_property(PROPERTY nostdinc -nostdinc) 121 set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC}) 122endif() 123 124set_compiler_property(PROPERTY no_printf_return_value -fno-printf-return-value) 125 126set_property(TARGET compiler-cpp PROPERTY nostdincxx "-nostdinc++") 127 128# Required C++ flags when using gcc 129set_property(TARGET compiler-cpp PROPERTY required "-fcheck-new") 130 131# GCC compiler flags for C++ dialect: "register" variables and some 132# "volatile" usage generates warnings by default in standard versions 133# higher than 17 and 20 respectively. Zephyr uses both, so turn off 134# the warnings where needed (but only on the compilers that generate 135# them, older toolchains like xcc don't understand the command line 136# flags!) 137set_property(TARGET compiler-cpp PROPERTY dialect_cpp98 "-std=c++98") 138set_property(TARGET compiler-cpp PROPERTY dialect_cpp11 "-std=c++11") 139set_property(TARGET compiler-cpp PROPERTY dialect_cpp14 "-std=c++14") 140set_property(TARGET compiler-cpp PROPERTY dialect_cpp17 "-std=c++17" "-Wno-register") 141set_property(TARGET compiler-cpp PROPERTY dialect_cpp2a "-std=c++2a" 142 "-Wno-register" "-Wno-volatile") 143set_property(TARGET compiler-cpp PROPERTY dialect_cpp20 "-std=c++20" 144 "-Wno-register" "-Wno-volatile") 145set_property(TARGET compiler-cpp PROPERTY dialect_cpp2b "-std=c++2b" 146 "-Wno-register" "-Wno-volatile") 147 148# Flag for disabling strict aliasing rule in C and C++ 149set_compiler_property(PROPERTY no_strict_aliasing -fno-strict-aliasing) 150 151# Extra warning options 152set_property(TARGET compiler PROPERTY warnings_as_errors -Werror) 153set_property(TARGET asm PROPERTY warnings_as_errors -Werror -Wa,--fatal-warnings) 154 155# Disable exceptions flag in C++ 156set_property(TARGET compiler-cpp PROPERTY no_exceptions "-fno-exceptions") 157 158# Disable rtti in C++ 159set_property(TARGET compiler-cpp PROPERTY no_rtti "-fno-rtti") 160 161 162################################################### 163# This section covers all remaining C / C++ flags # 164################################################### 165 166# gcc flags for coverage generation 167set_compiler_property(PROPERTY coverage -fprofile-arcs -ftest-coverage -fno-inline) 168 169# Security canaries. 170set_compiler_property(PROPERTY security_canaries -fstack-protector) 171set_compiler_property(PROPERTY security_canaries_strong -fstack-protector-strong) 172set_compiler_property(PROPERTY security_canaries_all -fstack-protector-all) 173set_compiler_property(PROPERTY security_canaries_explicit -fstack-protector-explicit) 174 175# Only a valid option with GCC 7.x and above, so let's do check and set. 176if(CONFIG_STACK_CANARIES_TLS) 177 check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=tls) 178 check_set_compiler_property(APPEND PROPERTY security_canaries_strong -mstack-protector-guard=tls) 179 check_set_compiler_property(APPEND PROPERTY security_canaries_all -mstack-protector-guard=tls) 180 check_set_compiler_property(APPEND PROPERTY security_canaries_explicit -mstack-protector-guard=tls) 181else() 182 check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=global) 183 check_set_compiler_property(APPEND PROPERTY security_canaries_global -mstack-protector-guard=global) 184 check_set_compiler_property(APPEND PROPERTY security_canaries_all -mstack-protector-guard=global) 185 check_set_compiler_property(APPEND PROPERTY security_canaries_explicit -mstack-protector-guard=global) 186endif() 187 188 189if(NOT CONFIG_NO_OPTIMIZATIONS) 190 # _FORTIFY_SOURCE: Detect common-case buffer overflows for certain functions 191 # _FORTIFY_SOURCE=1 : Loose checking (use wide bounds checks) 192 # _FORTIFY_SOURCE=2 : Tight checking (use narrow bounds checks) 193 # GCC always does compile-time bounds checking for string/mem functions, so 194 # there's no additional value to set here 195 set_compiler_property(PROPERTY security_fortify_compile_time) 196 set_compiler_property(PROPERTY security_fortify_run_time _FORTIFY_SOURCE=2) 197endif() 198 199# gcc flag for a hosted (no-freestanding) application 200check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding) 201 202# gcc flag for a freestanding application 203check_set_compiler_property(PROPERTY freestanding -ffreestanding) 204 205# Flag to enable debugging 206set_compiler_property(PROPERTY debug -g) 207 208# Flags to save temporary object files 209set_compiler_property(PROPERTY save_temps -save-temps=obj) 210 211# Flag to specify linker script 212set_compiler_property(PROPERTY linker_script -T) 213 214# Flags to not track macro expansion 215set_compiler_property(PROPERTY no_track_macro_expansion -ftrack-macro-expansion=0) 216 217# GCC 11 by default emits DWARF version 5 which cannot be parsed by 218# pyelftools. Can be removed once pyelftools supports v5. 219check_set_compiler_property(APPEND PROPERTY debug -gdwarf-4) 220 221set_compiler_property(PROPERTY no_common -fno-common) 222 223# GCC compiler flags for imacros. The specific header must be appended by user. 224set_compiler_property(PROPERTY imacros -imacros) 225 226set_compiler_property(PROPERTY gprof -pg) 227 228# GCC compiler flag for turning off thread-safe initialization of local statics 229set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics "-fno-threadsafe-statics") 230 231# Required ASM flags when using gcc 232set_property(TARGET asm PROPERTY required "-xassembler-with-cpp") 233 234# gcc flag for colourful diagnostic messages 235if (NOT COMPILER STREQUAL "xcc") 236set_compiler_property(PROPERTY diagnostic -fdiagnostics-color=always) 237endif() 238 239# Compiler flag for disabling pointer arithmetic warnings 240set_compiler_property(PROPERTY warning_no_pointer_arithmetic "-Wno-pointer-arith") 241 242#Compiler flags for disabling position independent code / executable 243set_compiler_property(PROPERTY no_position_independent 244 -fno-pic 245 -fno-pie 246) 247 248set_compiler_property(PROPERTY no_global_merge "") 249 250set_compiler_property(PROPERTY warning_shadow_variables -Wshadow) 251 252set_compiler_property(PROPERTY no_builtin -fno-builtin) 253set_compiler_property(PROPERTY no_builtin_malloc -fno-builtin-malloc) 254 255set_compiler_property(PROPERTY specs -specs=) 256 257set_compiler_property(PROPERTY include_file -include) 258