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