1# Copyright (c) 2022-2023 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14import("$build_root/config/compiler/compiler.gni")
15import("$build_root/config/sanitizers/sanitizers.gni")
16import("$build_root/toolchain/toolchain.gni")
17if (current_cpu == "arm" || current_cpu == "arm64") {
18  import("$build_root/config/arm.gni")
19}
20
21declare_args() {
22  treat_warnings_as_errors = true
23}
24
25use_rtti = use_cfi_diag || is_ubsan_vptr || is_ubsan_security
26
27config("rtti") {
28  if (is_win) {
29    cflags_cc = [ "/GR" ]
30  } else {
31    cflags_cc = [ "-frtti" ]
32  }
33}
34
35config("no_exceptions") {
36  if (is_linux) {
37    cflags_cc = [ "-fno-exceptions" ]
38    cflags_objcc = cflags_cc
39  }
40}
41
42config("optimize_speed") {
43  if (is_linux) {
44    cflags_cc = [ "-O3" ]
45  }
46}
47
48config("no_rtti") {
49  # Some sanitizer configs may require RTTI to be left enabled globally
50  if (!use_rtti) {
51    if (is_win) {
52      cflags_cc = [ "/GR-" ]
53    } else {
54      cflags_cc = [ "-fno-rtti" ]
55      cflags_objcc = cflags_cc
56    }
57  }
58}
59
60config("exceptions") {
61  cflags_cc = [ "-fexceptions" ]
62}
63
64common_optimize_on_cflags = []
65common_optimize_on_ldflags = []
66
67if (is_ohos) {
68  common_optimize_on_ldflags += [
69    "-Wl,--warn-shared-textrel",  # Warn in case of text relocations.
70  ]
71}
72
73if (!(is_mac || is_ios)) {
74  # Non-Mac Posix flags.
75
76  common_optimize_on_ldflags += [
77    # Specifically tell the linker to perform optimizations.
78    # -O2 enables string tail merge optimization in gold and lld.
79    "-Wl,-O2",
80  ]
81  if (!is_mingw) {
82    common_optimize_on_ldflags += [ "-Wl,--gc-sections" ]
83  }
84
85  common_optimize_on_cflags += [
86    # Put data and code in their own sections, so that unused symbols
87    # can be removed at link time with --gc-sections.
88    "-fdata-sections",
89    "-ffunction-sections",
90
91    # Don't emit the GCC version ident directives, they just end up in the
92    # .comment section taking up binary size.
93    "-fno-ident",
94  ]
95}
96
97# Turn off optimizations.
98config("no_optimize") {
99  if (is_ohos_or_android) {
100    ldflags = common_optimize_on_ldflags
101    cflags = common_optimize_on_cflags
102
103    # On ohos we kind of optimize some things that don't affect debugging
104    # much even when optimization is disabled to get the binary size down.
105    if (is_clang) {
106      cflags += [ "-Oz" ]
107    } else {
108      cflags += [ "-Os" ]
109    }
110  }
111}
112
113# Default "optimization on" config.
114config("optimize") {
115  ldflags = common_optimize_on_ldflags
116  cflags = common_optimize_on_cflags
117  if (optimize_for_size) {
118    # Favor size over speed.
119    if (is_clang) {
120      cflags += [ "-O2" ]
121    } else {
122      cflags += [ "-Os" ]
123    }
124  } else {
125    cflags += [ "-O2" ]
126  }
127}
128
129# The default optimization applied to all targets. This will be equivalent to
130# either "optimize" or "no_optimize", depending on the build flags.
131config("default_optimization") {
132  if (is_debug) {
133    configs = [ ":no_optimize" ]
134  } else {
135    configs = [ ":optimize" ]
136  }
137}
138
139# default_warnings ------------------------------------------------------------
140#
141# Collects all warning flags that are used by default.This way these
142# flags are guaranteed to appear on the compile command line after -Wall.
143config("default_warnings") {
144  cflags = []
145  cflags_cc = []
146  ldflags = []
147
148  # Suppress warnings about ABI changes on ARM (Clang doesn't give this
149  # warning).
150  if (current_cpu == "arm" && !is_clang) {
151    cflags += [ "-Wno-psabi" ]
152  }
153
154  if (!is_clang) {
155    cflags_cc += [
156      # See comment for -Wno-c++11-narrowing.
157      "-Wno-narrowing",
158    ]
159
160    # -Wunused-local-typedefs is broken in gcc,
161    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872
162    cflags += [ "-Wno-unused-local-typedefs" ]
163
164    # Don't warn about "maybe" uninitialized. Clang doesn't include this
165    # in -Wall but gcc does, and it gives false positives.
166    cflags += [ "-Wno-maybe-uninitialized" ]
167    cflags += [ "-Wno-deprecated-declarations" ]
168
169    # -Wcomment gives too many false positives in the case a
170    # backslash ended comment line is followed by a new line of
171    # comments
172    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638
173    cflags += [ "-Wno-comments" ]
174  }
175
176  # Common Clang and GCC warning setup.
177  if (!is_win || is_clang) {
178    cflags += [
179      # Disables.
180      "-Wno-missing-field-initializers",  # "struct foo f = {0};"
181      "-Wno-unused-parameter",  # Unused function parameters.
182    ]
183  }
184
185  if (is_mingw) {
186    cflags += [
187      "-Wno-error=c99-designator",
188      "-Wno-error=anon-enum-enum-conversion",
189      "-Wno-error=implicit-fallthrough",
190      "-Wno-error=sizeof-array-div",
191      "-Wno-error=reorder-init-list",
192      "-Wno-error=range-loop-construct",
193      "-Wno-error=deprecated-copy",
194      "-Wno-error=implicit-int-float-conversion",
195      "-Wno-error=inconsistent-dllimport",
196      "-Wno-error=abstract-final-class",
197      "-Wno-error=sign-compare",
198      "-Wno-unknown-warning-option",
199    ]
200  }
201
202  if (target_cpu == "mipsel") {
203    cflags += [
204      "-Wno-extra-semi",
205      "-Wno-error=tautological-constant-out-of-range-compare",
206      "-Wno-shift-count-overflow",
207      "-Wno-constant-conversion",
208    ]
209  }
210
211  if (is_clang) {
212    cflags += [
213      # This warns on using ints as initializers for floats in
214      # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
215      "-Wno-c++11-narrowing",
216      "-Wno-unneeded-internal-declaration",
217    ]
218    if (current_os == "android") {
219      # Disable calls to out-of-line helpers, don't implement atomic operations for these calls.
220      # (Keep consistent with the default of clang15 in directory prebuilts.)
221      cflags += [ "-mno-outline-atomics" ]
222    }
223    if (use_musl) {
224      cflags += [
225        "-Wno-error=c99-designator",
226        "-Wno-error=anon-enum-enum-conversion",
227        "-Wno-error=implicit-fallthrough",
228        "-Wno-error=sizeof-array-div",
229        "-Wno-error=reorder-init-list",
230        "-Wno-error=range-loop-construct",
231        "-Wno-error=deprecated-copy",
232        "-Wno-error=implicit-int-float-conversion",
233        "-Wno-error=inconsistent-dllimport",
234        "-Wno-error=unknown-warning-option",
235        "-Wno-error=abstract-final-class",
236        "-Wno-error=sign-compare",
237        "-Wno-error=int-in-bool-context",
238        "-Wno-error=xor-used-as-pow",
239        "-Wno-error=return-stack-address",
240        "-Wno-error=dangling-gsl",
241        "-Wno-unused-but-set-variable",
242        "-Wno-deprecated-declarations",
243        "-Wno-unused-but-set-parameter",
244        "-Wno-null-pointer-subtraction",
245        "-Wno-unqualified-std-cast-call",
246      ]
247    }
248  }
249}
250
251config("compiler") {
252  asmflags = []
253  cflags = []
254  cflags_c = []
255  cflags_cc = []
256  cflags_objc = []
257  cflags_objcc = []
258  ldflags = []
259  defines = []
260  configs = []
261  inputs = []
262
263  # System-specific flags. If your compiler flags apply to one of the
264  # categories here, add it to the associated file to keep this shared config
265  # smaller.
266  if (is_android) {
267    configs += [ "$build_root/config/aosp:compiler" ]
268  }
269  if (is_mingw) {
270    configs += [ "$build_root/config/mingw:compiler" ]
271    cflags += [ "-fno-stack-protector" ]
272  } else if (is_ohos) {
273    configs += [ "$build_root/config/ohos:compiler" ]
274  } else if (is_mac) {
275    configs += [ "$build_root/config/mac:compiler" ]
276  }
277  if (is_linux || is_ohos || is_android) {
278    cflags += [ "-fPIC" ]
279    ldflags += [ "-fPIC" ]
280    if (!is_clang) {
281      # Use pipes for communicating between sub-processes. Faster.
282      # (This flag doesn't do anything with Clang.)
283      cflags += [ "-pipe" ]
284    }
285
286    ldflags += [
287      "-Wl,-z,noexecstack",
288      "-Wl,-z,now",
289      "-Wl,-z,relro",
290    ]
291
292    # Compiler instrumentation can introduce dependencies in DSOs to symbols in
293    # the executable they are loaded into, so they are unresolved at link-time.
294    if (is_ohos || (!using_sanitizer && !is_safestack)) {
295      ldflags += [
296        "-Wl,-z,defs",
297        "-Wl,--as-needed",
298      ]
299    }
300
301    # # Change default thread stack size to 2MB for asan.
302    if (is_ohos && using_sanitizer) {
303      ldflags += [ "-Wl,-z,stack-size=2097152" ]
304    }
305  }
306
307  if (is_posix && use_lld) {
308    # Explicitly select LLD, or else some link actions would fail and print
309    # "/usr/bin/ld ... {SharedLibrayName}.so ... not found".
310    ldflags += [ "-fuse-ld=lld" ]
311    if (current_cpu == "arm64") {
312      # Reduce the page size from 65536 in order to reduce binary size slightly
313      # by shrinking the alignment gap between segments. This also causes all
314      # segments to be mapped adjacently, which breakpad relies on.
315      ldflags += [ "-Wl,-z,max-page-size=4096" ]
316    }
317  }
318
319  configs += [
320    # See the definitions below.
321    ":compiler_cpu_abi",
322    ":compiler_codegen",
323  ]
324
325  if (is_ohos && is_clang && (target_cpu == "arm" || target_cpu == "arm64")) {
326    ldflags += [ "-Wl,--pack-dyn-relocs=android+relr" ]
327  }
328
329  if (is_linux) {
330    cflags += [ "-pthread" ]
331    # Do not use the -pthread ldflag here since it becomes a no-op
332    # when using -nodefaultlibs, which would cause an unused argument
333    # error.  "-lpthread" is added in $build_root/config:default_libs.
334  }
335  if (is_clang) {
336    cflags += [ "-fcolor-diagnostics" ]
337    cflags += [ "-fmerge-all-constants" ]
338  }
339
340  use_xcode_clang = false
341  if (is_clang && !use_xcode_clang) {
342    cflags += [
343      "-Xclang",
344      "-mllvm",
345      "-Xclang",
346      "-instcombine-lower-dbg-declare=0",
347    ]
348  }
349
350  if (is_clang) {
351    cflags += [ "-no-canonical-prefixes" ]
352  }
353
354  # specify language standard version.
355  if (is_linux || is_ohos || is_android || current_os == "aix") {
356    if (is_clang) {
357      cflags_cc += [ "-std=c++17" ]
358    } else {
359      # In standards-conforming mode, some features like macro "##__VA_ARGS__"
360      # are not supported by gcc. Add flags to support these features.
361      cflags_cc += [ "-std=gnu++17" ]
362    }
363  } else if (!is_win && !is_mingw) {
364    cflags_cc += [ "-std=c++17" ]
365  }
366}
367
368# This provides the basic options to select the target CPU and ABI.
369# It is factored out of "compiler" so that special cases can use this
370# without using everything that "compiler" brings in.  Options that
371# tweak code generation for a particular CPU do not belong here!
372# See "compiler_codegen", below.
373config("compiler_cpu_abi") {
374  cflags = []
375  ldflags = []
376  defines = []
377
378  if (is_posix && !is_mac) {
379    # CPU architecture. We may or may not be doing a cross compile now, so for
380    # simplicity we always explicitly set the architecture.
381    if (current_cpu == "x64") {
382      cflags += [
383        "-m64",
384        "-march=x86-64",
385      ]
386      ldflags += [ "-m64" ]
387    } else if (current_cpu == "x86") {
388      cflags += [
389        "-m32",
390        "-msse2",
391        "-mfpmath=sse",
392        "-mmmx",
393      ]
394      ldflags += [ "-m32" ]
395    } else if (current_cpu == "arm") {
396      if (is_clang && !is_ohos) {
397        cflags += [ "--target=arm-linux-gnueabihf" ]
398        ldflags += [ "--target=arm-linux-gnueabihf" ]
399      }
400      cflags += [
401        "-march=$arm_arch",
402        "-mfloat-abi=$arm_float_abi",
403      ]
404      if (arm_tune != "") {
405        cflags += [ "-mtune=$arm_tune" ]
406      }
407    } else if (current_cpu == "arm64") {
408      if (is_clang && !is_ohos && !is_android) {
409        cflags += [ "--target=aarch64-linux-gnu" ]
410        ldflags += [ "--target=aarch64-linux-gnu" ]
411      }
412      if (is_clang && (is_ohos || is_android)) {
413        ldflags += [ "-Wl,--hash-style=gnu" ]
414      }
415      if (!is_android) {
416        cflags += [
417          "-march=$arm_arch",
418          "-mfloat-abi=$arm_float_abi",
419          "-mfpu=$arm_fpu",
420        ]
421      } else {
422        cflags += [ "-march=$arm_arch" ]
423      }
424      ldflags += [ "-march=$arm_arch" ]
425    }
426  }
427
428  asmflags = cflags
429  if (current_cpu == "arm64") {
430    asmflags += [ "-march=armv8.2-a+dotprod+fp16" ]
431  }
432}
433
434config("compiler_codegen") {
435  configs = []
436  cflags = []
437
438  if (is_posix && !is_mac) {
439    if (current_cpu == "x86") {
440      if (is_clang) {
441        cflags += [ "-momit-leaf-frame-pointer" ]
442      }
443    } else if (current_cpu == "arm") {
444      if (is_ohos && !is_clang) {
445        # Clang doesn't support these flags.
446        cflags += [
447          "-fno-tree-sra",
448          "-fno-caller-saves",
449        ]
450      }
451    }
452  }
453
454  asmflags = cflags
455}
456
457config("default_include_dirs") {
458  include_dirs = [
459    "//",
460    root_gen_dir,
461  ]
462}
463
464config("runtime_config") {
465  configs = []
466  if (is_posix) {
467    configs += [ "$build_root/config/posix:runtime_config" ]
468  }
469
470  # System-specific flags. If your compiler flags apply to one of the
471  # categories here, add it to the associated file to keep this shared config
472  # smaller.
473  if (is_ohos) {
474    configs += [ "$build_root/config/ohos:runtime_config" ]
475  }
476
477  if (is_android) {
478    configs += [ "$build_root/config/aosp:runtime_config" ]
479  }
480
481  if (is_mac) {
482    configs += [ "$build_root/config/mac:runtime_config" ]
483  }
484}
485
486config("ark_code") {
487  cflags = [ "-Wall" ]
488  if (treat_warnings_as_errors) {
489    cflags += [ "-Werror" ]
490    ldflags = [ "-Werror" ]
491  }
492  if (is_clang) {
493    # Enable extra warnings for ark_code when we control the compiler.
494    cflags += [ "-Wextra" ]
495  }
496
497  if (is_clang) {
498    cflags += [
499      "-Wimplicit-fallthrough",
500      "-Wthread-safety",
501    ]
502  }
503
504  configs = [ ":default_warnings" ]
505}
506