Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / build / config / compiler / BUILD.gn
1 # Copyright (c) 2020 Project CHIP Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import("//build_overrides/build.gni")
16 import("//build_overrides/pigweed.gni")
17
18 import("${build_root}/config/compiler/compiler.gni")
19 import("${build_root}/config/target.gni")
20
21 if (current_os == "mac") {
22   import("${build_root}/config/mac/mac_sdk.gni")
23 }
24
25 declare_args() {
26   # Enable -Werror. This can be disabled if using a different compiler
27   # with unfixed or unsupported wanings.
28   treat_warnings_as_errors = true
29 }
30
31 if (current_cpu == "arm" || current_cpu == "arm64") {
32   import("${build_root}/config/arm.gni")
33 } else if (current_cpu == "x86" || current_cpu == "x86_64") {
34   import("${build_root}/config/x86.gni")
35 }
36
37 config("release") {
38   defines = [ "NDEBUG" ]
39 }
40
41 config("debug_default") {
42   if (!is_debug) {
43     configs = [ ":release" ]
44   }
45 }
46
47 config("abi_default") {
48   cflags = []
49   if (current_os == "mac") {
50     cflags += [
51       "-target",
52       mac_target_triple,
53     ]
54   } else if (current_os == "ios") {
55     # iOS ABI currently set by chip_xcode_build_connector.sh
56   } else if (current_cpu == "arm" || current_cpu == "arm64") {
57     if (arm_arch != "") {
58       cflags += [ "-march=${arm_arch}" ]
59     }
60     if (arm_cpu != "") {
61       cflags += [ "-mcpu=${arm_cpu}" ]
62     }
63     if (arm_tune != "") {
64       cflags += [ "-mtune=${arm_tune}" ]
65     }
66     if (arm_abi != "") {
67       cflags += [ "-mabi=${arm_abi}" ]
68     }
69     if (arm_fpu != "") {
70       cflags += [ "-mfpu=${arm_fpu}" ]
71     }
72     if (arm_float_abi != "") {
73       cflags += [ "-mfloat-abi=${arm_float_abi}" ]
74     }
75     if (arm_use_thumb) {
76       cflags += [ "-mthumb" ]
77     }
78   } else if (current_cpu == "x86" || current_cpu == "x86_64") {
79     if (x86_arch != "") {
80       cflags += [ "-march=${x86_arch}" ]
81     }
82     if (x86_cpu != "") {
83       cflags += [ "-mcpu=${x86_cpu}" ]
84     }
85     if (x86_tune != "") {
86       cflags += [ "-mtune=${x86_tune}" ]
87     }
88
89     if (current_cpu == "x86_64") {
90       cflags += [
91         "-msse4.2",
92         "-mpopcnt",
93         "-m64",
94       ]
95     } else if (current_cpu == "x86") {
96       cflags += [
97         "-mssse3",
98         "-mfpmath=sse",
99         "-m32",
100       ]
101     }
102   }
103   asmflags = cflags
104   ldflags = cflags
105 }
106
107 config("target_default") {
108   if (current_toolchain == default_toolchain) {
109     defines = target_defines
110     cflags = target_cflags
111     cflags_c = target_cflags_c
112     cflags_cc = target_cflags_cc
113     ldflags = target_ldflags
114   }
115 }
116
117 config("optimize_zero") {
118   cflags = [ "-O0" ]
119   ldflags = cflags
120 }
121
122 config("optimize_default") {
123   if (is_debug) {
124     if (optimize_debug) {
125       configs = [ "$dir_pw_build:optimize_debugging" ]
126     } else {
127       configs = [ ":optimize_zero" ]
128     }
129   } else {
130     if (optimize_for_size) {
131       configs = [ "$dir_pw_build:optimize_size" ]
132     } else {
133       configs = [ "$dir_pw_build:optimize_speed" ]
134     }
135
136     if (current_os != "mac") {
137       ldflags = [
138         "-Wl,-O2",
139         "-Wl,--gc-sections",
140       ]
141     }
142   }
143 }
144
145 config("disabled_warnings") {
146   cflags = [
147     "-Wno-deprecated-declarations",
148     "-Wno-unknown-warning-option",
149     "-Wno-missing-field-initializers",
150     "-Wno-unused-parameter",
151   ]
152   if (!is_debug) {
153     # assert() causes unused variable warnings in release.
154     cflags += [ "-Wno-unused" ]
155   }
156   if (!is_clang) {
157     cflags += [
158       "-Wno-psabi",
159       "-Wno-cast-function-type",
160     ]
161   }
162 }
163
164 config("strict_warnings") {
165   cflags = [
166     "-Wall",
167     "-Wextra",
168     "-Wshadow",
169     "-Wunreachable-code",
170   ]
171
172   cflags_cc = [ "-Wnon-virtual-dtor" ]
173
174   ldflags = []
175
176   if (treat_warnings_as_errors) {
177     cflags += [ "-Werror" ]
178     ldflags += [ "-Werror" ]
179   }
180
181   if (is_clang) {
182     cflags += [
183       "-Wimplicit-fallthrough",
184       "-Wheader-hygiene",
185     ]
186   }
187
188   if (current_os == "linux" || current_os == "android") {
189     ldflags += [ "-Wl,-z,defs" ]
190   }
191 }
192
193 config("warnings_default") {
194   configs = [
195     ":strict_warnings",
196     ":disabled_warnings",
197   ]
198
199   if (current_os != "mac" && current_os != "ios") {
200     ldflags = [ "-Wl,--fatal-warnings" ]
201   }
202
203   if (current_os != "mac" && current_os != "ios" && current_os != "linux" &&
204       current_os != "win") {
205     cflags = [ "-Wstack-usage=8192" ]
206   }
207 }
208
209 config("symbols_default") {
210   cflags = [ "-g${symbol_level}" ]
211 }
212
213 config("gnu14") {
214   cflags_c = [ "-std=gnu11" ]
215   cflags_objc = [ "-std=gnu11" ]
216   cflags_cc = [ "-std=gnu++14" ]
217   cflags_objcc = [ "-std=gnu++14" ]
218 }
219
220 config("std_default") {
221   configs = [ ":gnu14" ]
222 }
223
224 config("cosmetic_default") {
225   configs = [ "$dir_pw_build:colorize_output" ]
226 }
227
228 config("runtime_default") {
229   if (is_clang) {
230     configs = [
231       "$dir_pw_toolchain/host_clang:no_system_libcpp",
232       "$dir_pw_toolchain/host_clang:xcode_sysroot",
233     ]
234   }
235   if (current_os == "linux") {
236     libs = [
237       "dl",
238       "pthread",
239       "rt",
240     ]
241   }
242 }
243
244 # To use different sanitizer options, use `gn args .` in the out folder and
245 # use settings like:
246 #
247 #   is_clang=true
248 #   is_debug=true
249 #   optimize_for_size=false
250 #   is_asan=true
251 #   is_sanitize_fatal=false
252 #
253
254 declare_args() {
255   # Enable address sanitizer
256   is_asan = false
257
258   # Enable memory sanitizer
259   is_msan = false
260
261   # enable undefined behavior sanitizer
262   is_ubsan = false
263
264   # Exit on sanitize error. Generally standard libraries may get errors
265   # so not stopping on the first error is often useful
266   is_sanitize_fatal = true
267 }
268
269 config("sanitize_address") {
270   cflags = [
271     "-fsanitize=address",
272     "-fno-omit-frame-pointer",
273   ]
274   ldflags = cflags
275 }
276
277 config("sanitize_memory") {
278   cflags = [
279     "-fsanitize=memory",
280     "-fsanitize-memory-track-origins",
281     "-fno-omit-frame-pointer",
282   ]
283   ldflags = cflags
284 }
285
286 config("sanitize_undefined_behavior") {
287   cflags = [
288     "-fvisibility=hidden",
289     "-fsanitize=undefined",
290     "-fsanitize=float-divide-by-zero",
291     "-fsanitize=unsigned-integer-overflow",
292     "-fsanitize=implicit-conversion",
293     "-fsanitize=nullability",
294   ]
295   ldflags = cflags
296 }
297
298 config("sanitize_recover_all") {
299   cflags = [ "-fsanitize-recover=all" ]
300   ldflags = cflags
301 }
302
303 config("sanitize_default") {
304   configs = []
305
306   if (is_asan) {
307     configs += [ ":sanitize_address" ]
308   }
309
310   if (is_msan) {
311     configs += [ ":sanitize_memory" ]
312   }
313
314   if (is_ubsan) {
315     configs += [ ":sanitize_undefined_behavior" ]
316   }
317
318   if (!is_sanitize_fatal) {
319     configs += [ ":sanitize_recover_all" ]
320   }
321 }
322
323 config("fuzzing_default") {
324 }
325
326 config("no_rtti") {
327   cflags_cc = [ "-fno-rtti" ]
328 }
329
330 config("rtti") {
331   cflags_cc = [ "-frtti" ]
332 }
333
334 config("rtti_default") {
335   configs = [ ":no_rtti" ]
336 }
337
338 config("no_exceptions") {
339   cflags = [ "-fno-exceptions" ]
340 }
341
342 config("exceptions") {
343   cflags = [ "-fexceptions" ]
344 }
345
346 config("exceptions_default") {
347   configs = [ ":no_exceptions" ]
348 }
349
350 config("unwind_tables") {
351   cflags = [ "-funwind-tables" ]
352 }
353
354 config("no_unwind_tables") {
355   cflags = [
356     "-fno-unwind-tables",
357     "-fno-asynchronous-unwind-tables",
358   ]
359 }
360
361 config("unwind_tables_default") {
362   if (exclude_unwind_tables) {
363     configs = [ ":no_unwind_tables" ]
364   } else {
365     configs = [ ":unwind_tables" ]
366   }
367 }
368
369 config("size_default") {
370   cflags = [
371     "-fno-common",
372     "-ffunction-sections",
373     "-fdata-sections",
374   ]
375 }
376
377 config("stack_protector_default") {
378   cflags = [ "-fno-stack-protector" ]
379 }
380
381 config("pic_default") {
382   if (enable_pic) {
383     cflags = [ "-fPIC" ]
384     ldflags = cflags
385   }
386 }
387
388 config("pie_default") {
389   if (enable_pie) {
390     ldflags = [ "-pie" ]
391   }
392 }
393
394 config("aliasing_default") {
395   cflags = [ "-fno-strict-aliasing" ]
396 }
397
398 config("specs_default") {
399   if (current_cpu == "arm" && current_os == "freertos") {
400     cflags = [
401       "--specs=nosys.specs",
402       "--specs=nano.specs",
403     ]
404
405     libs = [ "nosys" ]
406
407     ldflags = cflags
408   }
409 }