Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 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 | ######################################################## |
| 9 | # Setting compiler properties for gcc / g++ compilers. # |
| 10 | ######################################################## |
| 11 | |
| 12 | ##################################################### |
| 13 | # This section covers flags related to optimization # |
| 14 | ##################################################### |
| 15 | set_compiler_property(PROPERTY no_optimization -O0) |
| 16 | if(CMAKE_C_COMPILER_VERSION VERSION_LESS "4.8.0") |
| 17 | set_compiler_property(PROPERTY optimization_debug -O0) |
| 18 | else() |
| 19 | set_compiler_property(PROPERTY optimization_debug -Og) |
| 20 | endif() |
| 21 | set_compiler_property(PROPERTY optimization_speed -O2) |
| 22 | set_compiler_property(PROPERTY optimization_size -Os) |
| 23 | |
Marc Herbert | 16337e8 | 2024-02-16 05:46:39 +0000 | [diff] [blame] | 24 | if(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) |
| 27 | endif() |
Radoslaw Koppel | 26c8776 | 2024-01-30 12:32:32 +0100 | [diff] [blame] | 28 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 29 | ####################################################### |
| 30 | # This section covers flags related to warning levels # |
| 31 | ####################################################### |
| 32 | |
| 33 | # GCC Option standard warning base in Zephyr |
Jaroslaw Stelter | 69913ad | 2022-07-04 17:22:18 +0200 | [diff] [blame] | 34 | check_set_compiler_property(PROPERTY warning_base |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 35 | -Wall |
Torsten Rasmussen | c2974e2 | 2022-09-09 15:07:04 +0200 | [diff] [blame] | 36 | "SHELL:-Wformat -Wformat-security" |
| 37 | "SHELL:-Wformat -Wno-format-zero-length" |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 38 | ) |
| 39 | |
Ryan McClelland | 018dbcf | 2023-04-22 14:23:57 -0700 | [diff] [blame] | 40 | # C implicit promotion rules will want to make floats into doubles very easily |
| 41 | check_set_compiler_property(APPEND PROPERTY warning_base -Wdouble-promotion) |
| 42 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 43 | check_set_compiler_property(APPEND PROPERTY warning_base -Wno-pointer-sign) |
| 44 | |
| 45 | # Prohibit void pointer arithmetic. Illegal in C99 |
| 46 | check_set_compiler_property(APPEND PROPERTY warning_base -Wpointer-arith) |
| 47 | |
Anas Nashif | c7bc638 | 2021-02-15 09:14:56 -0500 | [diff] [blame] | 48 | # not portable |
| 49 | check_set_compiler_property(APPEND PROPERTY warning_base -Wexpansion-to-defined) |
| 50 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 51 | # GCC options for warning levels 1, 2, 3, when using `-DW=[1|2|3]` |
| 52 | set_compiler_property(PROPERTY warning_dw_1 |
| 53 | -Waggregate-return |
| 54 | -Wcast-align |
| 55 | -Wdisabled-optimization |
| 56 | -Wnested-externs |
| 57 | -Wshadow |
| 58 | ) |
| 59 | check_set_compiler_property(APPEND PROPERTY warning_dw_1 |
| 60 | -Wlogical-op |
| 61 | -Wmissing-field-initializers |
| 62 | ) |
| 63 | |
| 64 | set_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 | ) |
| 74 | check_set_compiler_property(APPEND PROPERTY warning_dw_2 |
| 75 | -Wpacked-bitfield-compat |
| 76 | -Wvla |
| 77 | ) |
| 78 | set_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 | ) |
| 88 | check_set_compiler_property(APPEND PROPERTY warning_dw_3 |
| 89 | -Wpacked-bitfield-compat |
| 90 | -Wvla |
| 91 | ) |
| 92 | |
Daniel Leung | 7d16ab5 | 2020-11-09 10:29:20 -0800 | [diff] [blame] | 93 | check_set_compiler_property(PROPERTY warning_extended -Wno-unused-but-set-variable) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 94 | |
Torsten Rasmussen | 27e1fd6 | 2020-09-07 08:46:28 +0200 | [diff] [blame] | 95 | check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 96 | |
| 97 | set_compiler_property(PROPERTY warning_error_misra_sane -Werror=vla) |
| 98 | |
| 99 | set_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. |
| 111 | set_compiler_property(PROPERTY cstd -std=) |
| 112 | |
| 113 | if (NOT CONFIG_NEWLIB_LIBC AND |
Keith Packard | 5acd82e | 2022-11-07 13:50:51 -0800 | [diff] [blame] | 114 | NOT (CONFIG_PICOLIBC AND NOT CONFIG_PICOLIBC_USE_MODULE) AND |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 115 | NOT COMPILER STREQUAL "xcc" AND |
Sylvio Alves | c9f6d18 | 2022-03-02 17:46:45 -0300 | [diff] [blame] | 116 | NOT CONFIG_HAS_ESPRESSIF_HAL AND |
Alberto Escolar Piedras | 56dc20e | 2023-05-26 13:23:43 +0200 | [diff] [blame] | 117 | NOT CONFIG_NATIVE_BUILD) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 118 | set_compiler_property(PROPERTY nostdinc -nostdinc) |
| 119 | set_compiler_property(APPEND PROPERTY nostdinc_include ${NOSTDINC}) |
| 120 | endif() |
| 121 | |
Keith Packard | b3073f0 | 2022-10-13 15:04:00 -0700 | [diff] [blame] | 122 | set_compiler_property(PROPERTY no_printf_return_value -fno-printf-return-value) |
| 123 | |
Stephanos Ioannidis | c2859bc | 2021-09-11 22:08:45 +0900 | [diff] [blame] | 124 | set_compiler_property(TARGET compiler-cpp PROPERTY nostdincxx "-nostdinc++") |
| 125 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 126 | # Required C++ flags when using gcc |
| 127 | set_property(TARGET compiler-cpp PROPERTY required "-fcheck-new") |
| 128 | |
Andy Ross | 728230a | 2023-02-28 13:19:01 -0800 | [diff] [blame] | 129 | # 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 Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 135 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp98 "-std=c++98") |
Andy Ross | 728230a | 2023-02-28 13:19:01 -0800 | [diff] [blame] | 136 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp11 "-std=c++11") |
| 137 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp14 "-std=c++14") |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 138 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp17 "-std=c++17" "-Wno-register") |
Peter Bigot | c7ad0b5 | 2020-12-18 07:52:33 -0600 | [diff] [blame] | 139 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp2a "-std=c++2a" |
| 140 | "-Wno-register" "-Wno-volatile") |
Alexander Wachter | ad130f2 | 2021-07-14 10:50:21 +0200 | [diff] [blame] | 141 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp20 "-std=c++20" |
| 142 | "-Wno-register" "-Wno-volatile") |
| 143 | set_property(TARGET compiler-cpp PROPERTY dialect_cpp2b "-std=c++2b" |
| 144 | "-Wno-register" "-Wno-volatile") |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 145 | |
Stephanos Ioannidis | 1f2c1c6 | 2022-08-11 03:30:17 +0900 | [diff] [blame] | 146 | # Flag for disabling strict aliasing rule in C and C++ |
| 147 | set_compiler_property(PROPERTY no_strict_aliasing -fno-strict-aliasing) |
| 148 | |
Nikolay Agishev | 0dec4cf | 2022-12-22 15:46:04 +0400 | [diff] [blame] | 149 | # Extra warning options |
| 150 | set_property(TARGET compiler PROPERTY warnings_as_errors -Werror) |
| 151 | set_property(TARGET asm PROPERTY warnings_as_errors -Werror -Wa,--fatal-warnings) |
| 152 | |
Nazar Kazakov | 9713f0d | 2022-02-24 12:00:55 +0000 | [diff] [blame] | 153 | # Disable exceptions flag in C++ |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 154 | set_property(TARGET compiler-cpp PROPERTY no_exceptions "-fno-exceptions") |
| 155 | |
| 156 | # Disable rtti in C++ |
| 157 | set_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 |
| 165 | set_compiler_property(PROPERTY coverage -fprofile-arcs -ftest-coverage -fno-inline) |
| 166 | |
| 167 | # Security canaries. |
| 168 | set_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 Ceolin | d16c5b9 | 2023-08-01 15:07:57 -0700 | [diff] [blame] | 171 | if(CONFIG_STACK_CANARIES_TLS) |
| 172 | check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=tls) |
| 173 | else() |
| 174 | check_set_compiler_property(APPEND PROPERTY security_canaries -mstack-protector-guard=global) |
| 175 | endif() |
| 176 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 177 | |
| 178 | if(NOT CONFIG_NO_OPTIMIZATIONS) |
| 179 | # _FORTIFY_SOURCE: Detect common-case buffer overflows for certain functions |
Keith Packard | cdc686e | 2023-11-19 00:33:23 -0800 | [diff] [blame] | 180 | # _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 Packard | 62bc9bf | 2022-04-26 19:24:11 -0700 | [diff] [blame] | 185 | set_compiler_property(PROPERTY security_fortify_run_time _FORTIFY_SOURCE=2) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 186 | endif() |
| 187 | |
| 188 | # gcc flag for a hosted (no-freestanding) application |
| 189 | check_set_compiler_property(APPEND PROPERTY hosted -fno-freestanding) |
| 190 | |
Nazar Kazakov | 9713f0d | 2022-02-24 12:00:55 +0000 | [diff] [blame] | 191 | # gcc flag for a freestanding application |
Jaroslaw Stelter | 69913ad | 2022-07-04 17:22:18 +0200 | [diff] [blame] | 192 | check_set_compiler_property(PROPERTY freestanding -ffreestanding) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 193 | |
Daniel Leung | 5c037fe | 2022-02-01 09:44:00 -0800 | [diff] [blame] | 194 | # Flag to enable debugging |
| 195 | set_compiler_property(PROPERTY debug -g) |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 196 | |
Fabio Baltieri | 3f8f713 | 2023-03-29 10:11:46 +0100 | [diff] [blame] | 197 | # Flags to save temporary object files |
| 198 | set_compiler_property(PROPERTY save_temps -save-temps=obj) |
| 199 | |
Keith Packard | 424590f | 2023-07-05 10:13:08 -0700 | [diff] [blame] | 200 | # Flag to specify linker script |
| 201 | set_compiler_property(PROPERTY linker_script -T) |
| 202 | |
Gerard Marull-Paretas | 99ebe39 | 2023-05-05 09:58:12 +0200 | [diff] [blame] | 203 | # Flags to not track macro expansion |
| 204 | set_compiler_property(PROPERTY no_track_macro_expansion -ftrack-macro-expansion=0) |
| 205 | |
Daniel Leung | 5c037fe | 2022-02-01 09:44:00 -0800 | [diff] [blame] | 206 | # GCC 11 by default emits DWARF version 5 which cannot be parsed by |
| 207 | # pyelftools. Can be removed once pyelftools supports v5. |
| 208 | check_set_compiler_property(APPEND PROPERTY debug -gdwarf-4) |
Daniel Leung | 72c2f18 | 2021-06-01 13:17:22 -0700 | [diff] [blame] | 209 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 210 | set_compiler_property(PROPERTY no_common -fno-common) |
| 211 | |
| 212 | # GCC compiler flags for imacros. The specific header must be appended by user. |
| 213 | set_compiler_property(PROPERTY imacros -imacros) |
| 214 | |
Anas Nashif | 399a0b4 | 2022-04-21 10:38:32 -0400 | [diff] [blame] | 215 | set_compiler_property(PROPERTY gprof -pg) |
| 216 | |
Lauren Murphy | e6b8c50 | 2021-05-18 15:41:45 -0500 | [diff] [blame] | 217 | # GCC compiler flag for turning off thread-safe initialization of local statics |
| 218 | set_property(TARGET compiler-cpp PROPERTY no_threadsafe_statics "-fno-threadsafe-statics") |
| 219 | |
Torsten Rasmussen | c55c64e | 2020-08-18 14:47:53 +0200 | [diff] [blame] | 220 | # Required ASM flags when using gcc |
| 221 | set_property(TARGET asm PROPERTY required "-xassembler-with-cpp") |
Arvin Farahmand | b8f5968 | 2021-04-15 11:20:10 -0400 | [diff] [blame] | 222 | |
| 223 | # gcc flag for colourful diagnostic messages |
Daniel Leung | 70d0077 | 2021-06-30 13:30:16 -0700 | [diff] [blame] | 224 | if (NOT COMPILER STREQUAL "xcc") |
Arvin Farahmand | b8f5968 | 2021-04-15 11:20:10 -0400 | [diff] [blame] | 225 | set_compiler_property(PROPERTY diagnostic -fdiagnostics-color=always) |
Daniel Leung | 70d0077 | 2021-06-30 13:30:16 -0700 | [diff] [blame] | 226 | endif() |
Yonatan Schachter | 0f73144 | 2022-02-16 21:02:08 +0200 | [diff] [blame] | 227 | |
| 228 | # Compiler flag for disabling pointer arithmetic warnings |
| 229 | set_compiler_property(PROPERTY warning_no_pointer_arithmetic "-Wno-pointer-arith") |
Flavio Ceolin | 8259931 | 2022-08-22 08:47:03 -0700 | [diff] [blame] | 230 | |
| 231 | #Compiler flags for disabling position independent code / executable |
| 232 | set_compiler_property(PROPERTY no_position_independent |
| 233 | -fno-pic |
| 234 | -fno-pie |
| 235 | ) |
Flavio Ceolin | ac5d45a | 2023-01-22 12:47:36 -0800 | [diff] [blame] | 236 | |
| 237 | set_compiler_property(PROPERTY no_global_merge "") |
Daniel Leung | e38fc6d | 2023-08-11 13:40:05 -0700 | [diff] [blame] | 238 | |
| 239 | set_compiler_property(PROPERTY warning_shadow_variables -Wshadow) |
Grant Ramsay | a3ff19a | 2023-11-13 09:34:50 +1300 | [diff] [blame] | 240 | |
| 241 | set_compiler_property(PROPERTY no_builtin -fno-builtin) |
| 242 | set_compiler_property(PROPERTY no_builtin_malloc -fno-builtin-malloc) |