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