blob: 14cd3e678891b5960aec242f1f51bec57467b50a [file] [log] [blame]
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +02001# 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
Marc Herbert16337e82024-02-16 05:46:39 +000024if(CMAKE_C_COMPILER_VERSION GREATER_EQUAL "4.5.0")
25 set_compiler_property(PROPERTY optimization_lto -flto)
26 set_compiler_property(PROPERTY prohibit_lto -fno-lto)
27endif()
Radoslaw Koppel26c87762024-01-30 12:32:32 +010028
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020029#######################################################
30# This section covers flags related to warning levels #
31#######################################################
32
33# GCC Option standard warning base in Zephyr
Jaroslaw Stelter69913ad2022-07-04 17:22:18 +020034check_set_compiler_property(PROPERTY warning_base
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020035 -Wall
Torsten Rasmussenc2974e22022-09-09 15:07:04 +020036 "SHELL:-Wformat -Wformat-security"
37 "SHELL:-Wformat -Wno-format-zero-length"
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020038)
39
Ryan McClelland018dbcf2023-04-22 14:23:57 -070040# C implicit promotion rules will want to make floats into doubles very easily
41check_set_compiler_property(APPEND PROPERTY warning_base -Wdouble-promotion)
42
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020043check_set_compiler_property(APPEND PROPERTY warning_base -Wno-pointer-sign)
44
45# Prohibit void pointer arithmetic. Illegal in C99
46check_set_compiler_property(APPEND PROPERTY warning_base -Wpointer-arith)
47
Anas Nashifc7bc6382021-02-15 09:14:56 -050048# not portable
49check_set_compiler_property(APPEND PROPERTY warning_base -Wexpansion-to-defined)
50
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020051# GCC options for warning levels 1, 2, 3, when using `-DW=[1|2|3]`
52set_compiler_property(PROPERTY warning_dw_1
53 -Waggregate-return
54 -Wcast-align
55 -Wdisabled-optimization
56 -Wnested-externs
57 -Wshadow
58)
59check_set_compiler_property(APPEND PROPERTY warning_dw_1
60 -Wlogical-op
61 -Wmissing-field-initializers
62)
63
64set_compiler_property(PROPERTY warning_dw_2
65 -Wbad-function-cast
66 -Wcast-qual
67 -Wconversion
68 -Wpacked
69 -Wpadded
70 -Wpointer-arith
71 -Wredundant-decls
72 -Wswitch-default
73)
74check_set_compiler_property(APPEND PROPERTY warning_dw_2
75 -Wpacked-bitfield-compat
76 -Wvla
77)
78set_compiler_property(PROPERTY warning_dw_3
79 -Wbad-function-cast
80 -Wcast-qual
81 -Wconversion
82 -Wpacked
83 -Wpadded
84 -Wpointer-arith
85 -Wredundant-decls
86 -Wswitch-default
87)
88check_set_compiler_property(APPEND PROPERTY warning_dw_3
89 -Wpacked-bitfield-compat
90 -Wvla
91)
92
Daniel Leung7d16ab52020-11-09 10:29:20 -080093check_set_compiler_property(PROPERTY warning_extended -Wno-unused-but-set-variable)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020094
Torsten Rasmussen27e1fd62020-09-07 08:46:28 +020095check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +020096
97set_compiler_property(PROPERTY warning_error_misra_sane -Werror=vla)
98
99set_compiler_property(PROPERTY warning_error_coding_guideline
100 -Werror=vla
101 -Wimplicit-fallthrough=2
102 -Wconversion
103 -Woverride-init
104)
105
106###########################################################################
107# This section covers flags related to C or C++ standards / standard libs #
108###########################################################################
109
110# GCC compiler flags for C standard. The specific standard must be appended by user.
111set_compiler_property(PROPERTY cstd -std=)
112
113if (NOT CONFIG_NEWLIB_LIBC AND
Keith Packard5acd82e2022-11-07 13:50:51 -0800114 NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200115 NOT COMPILER STREQUAL "xcc" AND
Sylvio Alvesc9f6d182022-03-02 17:46:45 -0300116 NOT CONFIG_HAS_ESPRESSIF_HAL AND
Alberto Escolar Piedras56dc20e2023-05-26 13:23:43 +0200117 NOT CONFIG_NATIVE_BUILD)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200118 set_compiler_property(PROPERTY nostdinc -nostdinc)
119 set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC})
120endif()
121
Keith Packardb3073f02022-10-13 15:04:00 -0700122set_compiler_property(PROPERTY no_printf_return_value -fno-printf-return-value)
123
Stephanos Ioannidisc2859bc2021-09-11 22:08:45 +0900124set_compiler_property(TARGET compiler-cpp PROPERTY nostdincxx "-nostdinc++")
125
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200126# Required C++ flags when using gcc
127set_property(TARGET compiler-cpp PROPERTY required "-fcheck-new")
128
Andy Ross728230a2023-02-28 13:19:01 -0800129# GCC compiler flags for C++ dialect: "register" variables and some
130# "volatile" usage generates warnings by default in standard versions
131# higher than 17 and 20 respectively. Zephyr uses both, so turn off
132# the warnings where needed (but only on the compilers that generate
133# them, older toolchains like xcc don't understand the command line
134# flags!)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200135set_property(TARGET compiler-cpp PROPERTY dialect_cpp98 "-std=c++98")
Andy Ross728230a2023-02-28 13:19:01 -0800136set_property(TARGET compiler-cpp PROPERTY dialect_cpp11 "-std=c++11")
137set_property(TARGET compiler-cpp PROPERTY dialect_cpp14 "-std=c++14")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200138set_property(TARGET compiler-cpp PROPERTY dialect_cpp17 "-std=c++17" "-Wno-register")
Peter Bigotc7ad0b52020-12-18 07:52:33 -0600139set_property(TARGET compiler-cpp PROPERTY dialect_cpp2a "-std=c++2a"
140 "-Wno-register" "-Wno-volatile")
Alexander Wachterad130f22021-07-14 10:50:21 +0200141set_property(TARGET compiler-cpp PROPERTY dialect_cpp20 "-std=c++20"
142 "-Wno-register" "-Wno-volatile")
143set_property(TARGET compiler-cpp PROPERTY dialect_cpp2b "-std=c++2b"
144 "-Wno-register" "-Wno-volatile")
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200145
Stephanos Ioannidis1f2c1c62022-08-11 03:30:17 +0900146# Flag for disabling strict aliasing rule in C and C++
147set_compiler_property(PROPERTY no_strict_aliasing -fno-strict-aliasing)
148
Nikolay Agishev0dec4cf2022-12-22 15:46:04 +0400149# Extra warning options
150set_property(TARGET compiler PROPERTY warnings_as_errors -Werror)
151set_property(TARGET asm PROPERTY warnings_as_errors -Werror -Wa,--fatal-warnings)
152
Nazar Kazakov9713f0d2022-02-24 12:00:55 +0000153# Disable exceptions flag in C++
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200154set_property(TARGET compiler-cpp PROPERTY no_exceptions "-fno-exceptions")
155
156# Disable rtti in C++
157set_property(TARGET compiler-cpp PROPERTY no_rtti "-fno-rtti")
158
159
160###################################################
161# This section covers all remaining C / C++ flags #
162###################################################
163
164# gcc flags for coverage generation
165set_compiler_property(PROPERTY coverage -fprofile-arcs -ftest-coverage -fno-inline)
166
167# Security canaries.
168set_compiler_property(PROPERTY security_canaries -fstack-protector-all)
169
170# Only a valid option with GCC 7.x and above, so let's do check and set.
Flavio Ceolind16c5b92023-08-01 15:07:57 -0700171if(CONFIG_STACK_CANARIES_TLS)
172 check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=tls)
173else()
174 check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=global)
175endif()
176
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200177
178if(NOT CONFIG_NO_OPTIMIZATIONS)
179 # _FORTIFY_SOURCE: Detect common-case buffer overflows for certain functions
Keith Packardcdc686e2023-11-19 00:33:23 -0800180 # _FORTIFY_SOURCE=1 : Loose checking (use wide bounds checks)
181 # _FORTIFY_SOURCE=2 : Tight checking (use narrow bounds checks)
182 # GCC always does compile-time bounds checking for string/mem functions, so
183 # there's no additional value to set here
184 set_compiler_property(PROPERTY security_fortify_compile_time)
Keith Packard62bc9bf2022-04-26 19:24:11 -0700185 set_compiler_property(PROPERTY security_fortify_run_time _FORTIFY_SOURCE=2)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200186endif()
187
188# gcc flag for a hosted (no-freestanding) application
189check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding)
190
Nazar Kazakov9713f0d2022-02-24 12:00:55 +0000191# gcc flag for a freestanding application
Jaroslaw Stelter69913ad2022-07-04 17:22:18 +0200192check_set_compiler_property(PROPERTY freestanding -ffreestanding)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200193
Daniel Leung5c037fe2022-02-01 09:44:00 -0800194# Flag to enable debugging
195set_compiler_property(PROPERTY debug -g)
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200196
Fabio Baltieri3f8f7132023-03-29 10:11:46 +0100197# Flags to save temporary object files
198set_compiler_property(PROPERTY save_temps -save-temps=obj)
199
Keith Packard424590f2023-07-05 10:13:08 -0700200# Flag to specify linker script
201set_compiler_property(PROPERTY linker_script -T)
202
Gerard Marull-Paretas99ebe392023-05-05 09:58:12 +0200203# Flags to not track macro expansion
204set_compiler_property(PROPERTY no_track_macro_expansion -ftrack-macro-expansion=0)
205
Daniel Leung5c037fe2022-02-01 09:44:00 -0800206# GCC 11 by default emits DWARF version 5 which cannot be parsed by
207# pyelftools. Can be removed once pyelftools supports v5.
208check_set_compiler_property(APPEND PROPERTY debug -gdwarf-4)
Daniel Leung72c2f182021-06-01 13:17:22 -0700209
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200210set_compiler_property(PROPERTY no_common -fno-common)
211
212# GCC compiler flags for imacros. The specific header must be appended by user.
213set_compiler_property(PROPERTY imacros -imacros)
214
Anas Nashif399a0b42022-04-21 10:38:32 -0400215set_compiler_property(PROPERTY gprof -pg)
216
Lauren Murphye6b8c502021-05-18 15:41:45 -0500217# GCC compiler flag for turning off thread-safe initialization of local statics
218set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics "-fno-threadsafe-statics")
219
Torsten Rasmussenc55c64e2020-08-18 14:47:53 +0200220# Required ASM flags when using gcc
221set_property(TARGET asm PROPERTY required "-xassembler-with-cpp")
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400222
223# gcc flag for colourful diagnostic messages
Daniel Leung70d00772021-06-30 13:30:16 -0700224if (NOT COMPILER STREQUAL "xcc")
Arvin Farahmandb8f59682021-04-15 11:20:10 -0400225set_compiler_property(PROPERTY diagnostic -fdiagnostics-color=always)
Daniel Leung70d00772021-06-30 13:30:16 -0700226endif()
Yonatan Schachter0f731442022-02-16 21:02:08 +0200227
228# Compiler flag for disabling pointer arithmetic warnings
229set_compiler_property(PROPERTY warning_no_pointer_arithmetic "-Wno-pointer-arith")
Flavio Ceolin82599312022-08-22 08:47:03 -0700230
231#Compiler flags for disabling position independent code / executable
232set_compiler_property(PROPERTY no_position_independent
233 -fno-pic
234 -fno-pie
235)
Flavio Ceolinac5d45a2023-01-22 12:47:36 -0800236
237set_compiler_property(PROPERTY no_global_merge "")
Daniel Leunge38fc6d2023-08-11 13:40:05 -0700238
239set_compiler_property(PROPERTY warning_shadow_variables -Wshadow)
Grant Ramsaya3ff19a2023-11-13 09:34:50 +1300240
241set_compiler_property(PROPERTY no_builtin -fno-builtin)
242set_compiler_property(PROPERTY no_builtin_malloc -fno-builtin-malloc)