1# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# =============================================================================
6# WHAT IS THIS FILE?
7# =============================================================================
8#
9# This is the master GN build configuration. This file is loaded after the
10# build args (args.gn) for the build directory and after the toplevel ".gn"
11# file (which points to this file as the build configuration).
12#
13# This file will be executed and the resulting context will be used to execute
14# every other file in the build. So variables declared here (that don't start
15# with an underscore) will be implicitly global.
16
17# =============================================================================
18# PLATFORM SELECTION
19# =============================================================================
20#
21# There are two main things to set: "os" and "cpu". The "toolchain" is the name
22# of the GN thing that encodes combinations of these things.
23#
24# Users typically only set the variables "target_os" and "target_cpu" in "gn
25# args", the rest are set up by our build and internal to GN.
26#
27# There are three different types of each of these things: The "host"
28# represents the computer doing the compile and never changes. The "target"
29# represents the main thing we're trying to build. The "current" represents
30# which configuration is currently being defined, which can be either the
31# host, the target, or something completely different (like nacl). GN will
32# run the same build file multiple times for the different required
33# configuration in the same build.
34#
35# This gives the following variables:
36#  - host_os, host_cpu, host_toolchain
37#  - target_os, target_cpu, default_toolchain
38#  - current_os, current_cpu, current_toolchain.
39#
40# Note the default_toolchain isn't symmetrical (you would expect
41# target_toolchain). This is because the "default" toolchain is a GN built-in
42# concept, and "target" is something our build sets up that's symmetrical with
43# its GYP counterpart. Potentially the built-in default_toolchain variable
44# could be renamed in the future.
45#
46# When writing build files, to do something only for the host:
47#   if (current_toolchain == host_toolchain) { ...
48
49check_mac_system_and_cpu_script =
50    rebase_path("//build/scripts/check_mac_system_and_cpu.py")
51check_darwin_system_result =
52    exec_script(check_mac_system_and_cpu_script, [ "system" ], "string")
53
54if (check_darwin_system_result != "") {
55  check_mac_host_cpu_result =
56      exec_script(check_mac_system_and_cpu_script, [ "cpu" ], "string")
57  if (check_mac_host_cpu_result != "") {
58    host_cpu = "arm64"
59  }
60}
61
62declare_args() {
63  product_name = ""
64  device_name = ""
65}
66
67declare_args() {
68  using_hb_new = true
69}
70
71declare_args() {
72  ohos_indep_compiler_enable = false
73}
74
75declare_args() {
76  preloader_output_dir = "//out/preloader/${product_name}"
77}
78
79declare_args() {
80  enable_lto_O0 = false
81}
82
83declare_args() {
84  enable_gn_2021 = true
85}
86
87declare_args() {
88  is_llvm_build = false
89}
90
91declare_args() {
92  rustc_codecheck = false
93}
94
95product_build_config =
96    read_file("${preloader_output_dir}/build_config.json", "json")
97
98global_parts_info =
99  read_file("${preloader_output_dir}/parts_config.json", "json")
100
101napi_white_list_path = "developtools/integration_verification/tools/deps_guard/rules/NO-Depends-On-NAPI"
102if (!is_llvm_build) {
103  if (!ohos_indep_compiler_enable) {
104    napi_white_list = read_file(
105            "//${napi_white_list_path}/whitelist.json",
106            "json")
107  } else if (ohos_indep_compiler_enable) {
108    napi_white_list = read_file(
109            "//build/indep_configs/mapping/config/${napi_white_list_path}/whitelist.json",
110            "json")
111  }
112}
113
114product_company = product_build_config.product_company
115device_company = product_build_config.device_company
116device_build_path = product_build_config.device_build_path
117target_os = product_build_config.target_os
118target_cpu = product_build_config.target_cpu
119product_toolchain = product_build_config.product_toolchain_label
120if (product_toolchain == "") {
121  product_toolchain = "//build/toolchain/ohos:ohos_clang_$target_cpu"
122}
123if (defined(product_build_config.ext_root_proc_conf_path)) {
124  ext_root_proc_conf_path = product_build_config.ext_root_proc_conf_path
125} else {
126  ext_root_proc_conf_path = ""
127}
128if (defined(product_build_config.ext_critical_proc_conf_path)) {
129  ext_critical_proc_conf_path = product_build_config.ext_critical_proc_conf_path
130} else {
131  ext_critical_proc_conf_path = ""
132}
133if (defined(product_build_config.ext_sanitizer_check_list_path)) {
134  ext_sanitizer_check_list_path =
135      product_build_config.ext_sanitizer_check_list_path
136} else {
137  joint_check_list_path = "//vendor/${product_company}/${product_name}/security_config/sanitizer_check_list.gni"
138  sanitize_list_exist =
139      exec_script(rebase_path("//build/scripts/check_file_exist.py"),
140                  [ rebase_path("${joint_check_list_path}") ],
141                  "string")
142  if (sanitize_list_exist != "") {
143    ext_sanitizer_check_list_path = joint_check_list_path
144  }
145}
146if (defined(product_build_config.enable_ramdisk)) {
147  enable_ramdisk = product_build_config.enable_ramdisk
148} else {
149  enable_ramdisk = false
150}
151if (defined(product_build_config.chipprod_config_path)) {
152  chipprod_config_path = product_build_config.chipprod_config_path
153  import("${chipprod_config_path}/chip_product_list.gni")
154} else {
155  chip_product_list = []
156}
157
158if (defined(product_build_config.ext_sdk_config_file)) {
159  ext_sdk_config_file = product_build_config.ext_sdk_config_file
160}
161
162if (defined(product_build_config.ext_ndk_config_file)) {
163  ext_ndk_config_file = product_build_config.ext_ndk_config_file
164}
165
166if (defined(product_build_config.enable_absystem)) {
167  enable_absystem = product_build_config.enable_absystem
168} else {
169  enable_absystem = false
170}
171
172if (defined(product_build_config.enable_mesa3d)) {
173  enable_mesa3d = product_build_config.enable_mesa3d
174} else {
175  enable_mesa3d = false
176}
177
178if (defined(product_build_config.build_selinux)) {
179  build_selinux = product_build_config.build_selinux
180} else {
181  build_selinux = false
182}
183
184if (defined(product_build_config.build_seccomp)) {
185  build_seccomp = product_build_config.build_seccomp
186} else {
187  build_seccomp = false
188}
189
190if (defined(product_build_config.support_jsapi)) {
191  support_jsapi = product_build_config.support_jsapi
192} else {
193  if (defined(global_parts_info) && !defined(global_parts_info.arkui_napi)) {
194    support_jsapi = false
195  } else {
196    support_jsapi = true
197  }
198}
199if (defined(product_build_config.ext_sign_hap_py_path)) {
200  sign_hap_py_path = product_build_config.ext_sign_hap_py_path
201}
202
203if (defined(product_build_config.global_ext_var_file)) {
204  import(product_build_config.global_ext_var_file)
205}
206
207if (target_os == "") {
208  target_os = "ohos"
209}
210
211if (target_cpu == "") {
212  if (target_os == "ohos" || target_os == "android" || target_os == "ios") {
213    target_cpu = "arm"
214  } else {
215    target_cpu = host_cpu
216  }
217}
218
219if (current_cpu == "") {
220  current_cpu = target_cpu
221}
222if (current_os == "") {
223  current_os = target_os
224}
225
226declare_args() {
227  is_mini_system = false
228  is_small_system = false
229  is_standard_system = false
230}
231
232if (ohos_indep_compiler_enable) {
233  is_standard_system = true
234  musl_is_legacy = false
235}
236
237if (is_mini_system) {
238  os_level = "mini"
239}
240if (is_small_system) {
241  os_level = "small"
242}
243if (is_standard_system) {
244  os_level = "standard"
245}
246
247declare_args() {
248  is_large_system = !(is_standard_system || is_small_system || is_mini_system)
249}
250
251declare_args() {
252  factory = false
253}
254
255is_lite_system = is_mini_system || is_small_system
256
257# =============================================================================
258# BUILD FLAGS
259# =============================================================================
260#
261# This block lists input arguments to the build, along with their default
262# values.
263#
264# If a value is specified on the command line, it will overwrite the defaults
265# given in a declare_args block, otherwise the default will be used.
266#
267# YOU SHOULD ALMOST NEVER NEED TO ADD FLAGS TO THIS FILE. GN allows any file in
268# the build to declare build flags. If you need a flag for a single component,
269# you can just declare it in the corresponding BUILD.gn file.
270#
271# - If your feature is a single target, say //components/foo, you can put
272#   a declare_args() block in //components/foo/BUILD.gn and use it there.
273#   Nobody else in the build needs to see the flag.
274#
275# - Defines based on build variables should be implemented via the generated
276#   build flag header system. See //build/buildflag_header.gni. You can put
277#   the buildflag_header target in the same file as the build flag itself. You
278#   should almost never set "defines" directly.
279#
280# - If your flag toggles a target on and off or toggles between different
281#   versions of similar things, write a "group" target that forwards to the
282#   right target (or no target) depending on the value of the build flag. This
283#   group can be in the same BUILD.gn file as the build flag, and targets can
284#   depend unconditionally on the group rather than duplicating flag checks
285#   across many targets.
286#
287# - If a semi-random set of build files REALLY needs to know about a define and
288#   the above pattern for isolating the build logic in a forwarding group
289#   doesn't work, you can put the argument in a .gni file. This should be put
290#   in the lowest level of the build that knows about this feature (which should
291#   almost always be outside of the //build directory!).
292#
293# Other flag advice:
294#
295# - Use boolean values when possible. If you need a default value that expands
296#   to some complex thing in the default case (like the location of the
297#   compiler which would be computed by a script), use a default value of -1 or
298#   the empty string. Outside of the declare_args block, conditionally expand
299#   the default value as necessary.
300#
301# - Use a name like "use_foo" or "is_foo" (whatever is more appropriate for
302#   your feature) rather than just "foo".
303#
304# - Write good comments directly above the declaration with no blank line.
305#   These comments will appear as documentation in "gn args --list".
306#
307# - Don't call exec_script inside declare_args. This will execute the script
308#   even if the value is overridden, which is wasteful. See first bullet.
309
310declare_args() {
311  # Set to enable the official build level of optimization. This has nothing
312  # to do with branding, but enables an additional level of optimization above
313  # release (!is_debug). This might be better expressed as a tri-state
314  # (debug, release, official) but for historical reasons there are two
315  # separate flags.
316  is_official_build = false
317
318  # Whether we're a traditional desktop unix.
319  is_desktop_linux = current_os == "linux"
320
321  # Set to true when compiling with the Clang compiler.
322  is_clang = current_os != "linux" ||
323             (current_cpu != "s390x" && current_cpu != "s390" &&
324              current_cpu != "ppc64" && current_cpu != "ppc" &&
325              current_cpu != "mips" && current_cpu != "mips64")
326
327  # Allows the path to a custom target toolchain to be injected as a single
328  # argument, and set as the default toolchain.
329  custom_toolchain = ""
330
331  # This should not normally be set as a build argument.  It's here so that
332  # every toolchain can pass through the "global" value via toolchain_args().
333  host_toolchain = ""
334
335  # target platform
336  target_platform = "phone"
337
338  # Whether it is test.
339  is_test = false
340
341  # Whether it is double framework.
342  is_double_framework = false
343
344  # build for cross platform
345  is_arkui_x = false
346}
347
348declare_args() {
349  use_musl = true
350}
351
352declare_args() {
353  is_emulator = false
354}
355
356asdk_libs_dir = "//prebuilts/asdk_libs"
357
358# Whether it is a phone product.
359is_phone_product = "${target_platform}" == "phone"
360
361# Whether it is a ivi product.
362is_ivi_product = "${target_platform}" == "ivi"
363
364is_wearable_product = "${target_platform}" == "wearable"
365
366is_intellitv_product = "${target_platform}" == "intellitv"
367
368if ((target_os == "ohos" && target_cpu == "x86_64") ||
369    device_company == "emulator") {
370  is_emulator = true
371}
372
373# different host platform tools directory.
374if (host_os == "linux") {
375  host_platform_dir = "linux-x86_64"
376} else if (host_os == "mac") {
377  if (host_cpu == "arm64") {
378    host_platform_dir = "darwin-arm64"
379  } else {
380    host_platform_dir = "darwin-x86_64"
381  }
382} else {
383  assert(false, "Unsupported host_os: $host_os")
384}
385
386declare_args() {
387  # Debug build. Enabling official builds automatically sets is_debug to false.
388  is_debug = false
389}
390
391declare_args() {
392  # Profile build. Enabling official builds automatically sets is_profile to false.
393  is_profile = false
394}
395
396declare_args() {
397  # The runtime mode ("debug", "profile", "release")
398  runtime_mode = "release"
399}
400
401declare_args() {
402  # Enable mini debug info, it will add .gnu_debugdata
403  # section in each stripped sofile
404
405  # Currently, we don't publish ohos-adapted python on m1 platform,
406  # So that we disable mini debug info on m1 platform until
407  # ohos-adapted python publishing on m1 platform
408  if (host_os == "mac") {
409    full_mini_debug = false
410  } else {
411    full_mini_debug = true
412  }
413}
414
415declare_args() {
416  # Full Debug mode. Setting optimize level to "-O0" and symbol level to "-g3".
417  # It should be used with "is_debug"
418  ohos_full_debug = false
419}
420
421declare_args() {
422  # Specifies which components use the DEBUG compilation level
423  # Using this arg like this, --gn-args 'enable_debug_components="component1,component2"'
424  enable_debug_components = ""
425}
426
427declare_args() {
428  # We use this arg to split "enable_debug_components" to a list
429  debug_components = string_split(enable_debug_components, ",")
430}
431
432declare_args() {
433  build_xts = false
434  precise_xts = false
435}
436
437declare_args() {
438  # Component build. Setting to true compiles targets declared as "components"
439  # as shared libraries loaded dynamically. This speeds up development time.
440  # When false, components will be linked statically.
441  #
442  # For more information see
443  # https://chromium.googlesource.com/chromium/src/+/master/docs/component_build.md
444  is_component_build = true
445}
446
447declare_args() {
448  enable_adlt = false
449  adlt_exe = ""
450  allowed_lib_list = ""
451  adlt_lib_name = ""
452}
453
454assert(!(is_debug && is_official_build), "Can't do official debug builds")
455
456# ==============================================================================
457# TOOLCHAIN SETUP
458# ==============================================================================
459#
460# Here we set the default toolchain, as well as the variable host_toolchain
461# which will identify the toolchain corresponding to the local system when
462# doing cross-compiles. When not cross-compiling, this will be the same as the
463# default toolchain.
464#
465# We do this before anything else to make sure we complain about any
466# unsupported os/cpu combinations as early as possible.
467
468if (host_toolchain == "") {
469  # This should only happen in the top-level context.
470  # In a specific toolchain context, the toolchain_args()
471  # block should have propagated a value down.
472
473  if (host_os == "linux") {
474    if (target_os != "linux") {
475      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
476    } else if (is_clang) {
477      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
478    } else {
479      host_toolchain = "//build/toolchain/linux:$host_cpu"
480    }
481  } else if (host_os == "mac") {
482    host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
483  } else if (host_os == "win") {
484    if (target_cpu == "x86" || target_cpu == "x64") {
485      if (is_clang) {
486        host_toolchain = "//build/toolchain/win:win_clang_$target_cpu"
487      } else {
488        host_toolchain = "//build/toolchain/win:$target_cpu"
489      }
490    } else if (is_clang) {
491      host_toolchain = "//build/toolchain/win:win_clang_$host_cpu"
492    } else {
493      host_toolchain = "//build/toolchain/win:$host_cpu"
494    }
495  } else {
496    assert(false, "Unsupported host_os: $host_os")
497  }
498}
499
500if (is_standard_system) {
501  _default_toolchain = ""
502
503  if (target_os == "ohos") {
504    assert(host_os == "linux" || host_os == "mac",
505           "ohos builds are only supported on Linux and Mac hosts.")
506    _default_toolchain = product_toolchain
507  } else if (target_os == "linux") {
508    if (is_clang) {
509      _default_toolchain = "//build/toolchain/linux:clang_$target_cpu"
510    } else {
511      _default_toolchain = "//build/toolchain/linux:$target_cpu"
512    }
513  } else if (target_os == "android") {
514    assert(host_os == "linux" || host_os == "mac",
515           "AOSP builds are only supported on Linux and Mac hosts.")
516    _default_toolchain = "//build_plugins/toolchain/aosp:aosp_clang_$target_cpu"
517  } else if (target_os == "ios") {
518    _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_$target_cpu"
519    import("//build_plugins/config/ios/ios_sdk.gni")  # For use_ios_simulator
520    if (use_ios_simulator) {
521      if (target_cpu == "arm64") {
522        _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_arm64_sim"
523      } else {
524        _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_x64_sim"
525      }
526    }
527  } else {
528    assert(false, "Unsupported target_os: $target_os")
529  }
530
531  # If a custom toolchain has been set in the args, set it as default. Otherwise,
532  # set the default toolchain for the platform (if any).
533  if (custom_toolchain != "") {
534    set_default_toolchain(custom_toolchain)
535  } else if (_default_toolchain != "") {
536    set_default_toolchain(_default_toolchain)
537  }
538}
539
540# =============================================================================
541# OS DEFINITIONS
542# =============================================================================
543#
544# We set these various is_FOO booleans for convenience in writing OS-based
545# conditions.
546#
547# - is_ohos, is_chromeos, and is_win should be obvious.
548# - is_mac is set only for desktop Mac.
549# - is_posix is true for mac and any Unix-like system (basically everything
550#   except Windows).
551# - is_linux is true for desktop Linux and ChromeOS.
552#
553# Do not add more is_* variants here for random lesser-used Unix systems like
554# aix or one of the BSDs. If you need to check these, just check the
555# current_os value directly.
556
557if (current_os == "win" || current_os == "winuwp") {
558  is_aix = false
559  is_ohos = false
560  is_chromeos = false
561  is_linux = false
562  is_mac = false
563  is_nacl = false
564  is_posix = false
565  is_win = true
566  is_mingw = false
567  is_android = false
568  is_ios = false
569} else if (current_os == "mac") {
570  is_aix = false
571  is_ohos = false
572  is_chromeos = false
573  is_linux = false
574  is_mac = true
575  is_nacl = false
576  is_posix = true
577  is_win = false
578  is_mingw = false
579  is_android = false
580  is_ios = false
581} else if (current_os == "ohos") {
582  is_aix = false
583  is_ohos = true
584  is_chromeos = false
585  is_linux = false
586  is_mac = false
587  is_nacl = false
588  is_posix = true
589  is_win = false
590  is_mingw = false
591  is_android = false
592  is_ios = false
593} else if (current_os == "linux") {
594  is_aix = false
595  is_ohos = false
596  is_chromeos = false
597  is_linux = true
598  is_mac = false
599  is_nacl = false
600  is_posix = true
601  is_win = false
602  is_mingw = false
603  is_android = false
604  is_ios = false
605} else if (current_os == "mingw") {
606  is_aix = false
607  is_ohos = false
608  is_chromeos = false
609  is_linux = false
610  is_mac = false
611  is_nacl = false
612  is_posix = true
613  is_win = false
614  is_mingw = true
615  is_android = false
616  is_ios = false
617} else if (current_os == "android") {
618  is_aix = false
619  is_ohos = false
620  is_chromeos = false
621  is_linux = false
622  is_mac = false
623  is_nacl = false
624  is_posix = true
625  is_win = false
626  is_mingw = false
627  is_android = true
628  is_ios = false
629} else if (current_os == "ios") {
630  is_aix = false
631  is_ohos = false
632  is_chromeos = false
633  is_linux = false
634  is_mac = false
635  is_nacl = false
636  is_posix = true
637  is_win = false
638  is_mingw = false
639  is_android = false
640  is_ios = true
641}
642
643# =============================================================================
644# SOURCES FILTERS
645# =============================================================================
646#
647# These patterns filter out platform-specific files when assigning to the
648# sources variable. The magic variable |sources_assignment_filter| is applied
649# to each assignment or appending to the sources variable and matches are
650# automatically removed.
651#
652# Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
653# boundary = end of string or slash) are supported, and the entire string
654# must match the pattern (so you need "*.cc" to match all .cc files, for
655# example).
656
657# DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
658# below.
659sources_assignment_filter = []
660
661if (!is_win && !is_mingw) {
662  sources_assignment_filter += [
663    "*_win.cc",
664    "*_win.h",
665    "*_win_unittest.cc",
666    "*\bwin/*",
667    "*.def",
668  ]
669}
670if (!is_mac || !is_ios) {
671  sources_assignment_filter += [
672    "*_mac.h",
673    "*_mac.cc",
674    "*_mac.mm",
675    "*_mac_unittest.h",
676    "*_mac_unittest.cc",
677    "*_mac_unittest.mm",
678    "*\bmac/*",
679    "*_cocoa.h",
680    "*_cocoa.cc",
681    "*_cocoa.mm",
682    "*_cocoa_unittest.h",
683    "*_cocoa_unittest.cc",
684    "*_cocoa_unittest.mm",
685    "*\bcocoa/*",
686  ]
687}
688if (!is_linux && !is_ohos && !is_android && !is_ios) {
689  sources_assignment_filter += [
690    "*_linux.h",
691    "*_linux.cc",
692    "*_linux_unittest.h",
693    "*_linux_unittest.cc",
694    "*\blinux/*",
695  ]
696}
697if (!is_ohos) {
698  sources_assignment_filter += []
699}
700
701#set_sources_assignment_filter(sources_assignment_filter)
702if (is_standard_system) {
703  file_exist = exec_script(rebase_path("//build/scripts/check_file_exist.py"),
704                           [ rebase_path("//${device_build_path}/config.gni") ],
705                           "string")
706  if (file_exist != "") {
707    import("//${device_build_path}/config.gni")
708  }
709
710  # =============================================================================
711  # TARGET DEFAULTS
712  # =============================================================================
713  #
714  # Set up the default configuration for every build target of the given type.
715  # The values configured here will be automatically set on the scope of the
716  # corresponding target. Target definitions can add or remove to the settings
717  # here as needed.
718  #
719  # WHAT GOES HERE?
720  #
721  # Other than the main compiler and linker configs, the only reason for a config
722  # to be in this list is if some targets need to explicitly override that config
723  # by removing it. This is how targets opt-out of flags. If you don't have that
724  # requirement and just need to add a config everywhere, reference it as a
725  # sub-config of an existing one, most commonly the main "compiler" one.
726
727  # Holds all configs used for running the compiler.
728  default_compiler_configs = [
729    "//build/config:feature_flags",
730    "//build/config/compiler:afdo",
731    "//build/config/compiler:afdo_optimize_size",
732    "//build/config/compiler:compiler",
733    "//build/config/compiler:compiler_arm_fpu",
734    "//build/config/compiler:compiler_arm_thumb",
735    "//build/config/compiler:chromium_code",
736    "//build/config/compiler:default_include_dirs",
737    "//build/config/compiler:default_optimization",
738    "//build/config/compiler:default_stack_frames",
739    "//build/config/compiler:default_symbols",
740    "//build/config/compiler:export_dynamic",
741    "//build/config/compiler:no_exceptions",
742    "//build/config/compiler:no_rtti",
743    "//build/config/compiler:runtime_library",
744    "//build/config/compiler:thin_archive",
745    "//build/config/compiler:no_common",
746    "//build/config/coverage:default_coverage",
747    "//build/config/sanitizers:default_sanitizer_flags",
748    "//build/config/security:default_security_configs",
749    "//build/config/rust:rust_config",
750    "//build/config:predefined_macro",
751  ]
752
753  if (is_ohos) {
754    default_compiler_configs += [
755      "//build/config/ohos:default_orderfile_instrumentation",
756      "//build/config/gcc:symbol_visibility_inline_hidden",
757    ]
758  }
759
760  if (is_clang) {
761    default_compiler_configs += [
762      "//build/config/clang:find_bad_constructs",
763      "//build/config/clang:extra_warnings",
764    ]
765  }
766
767  if (is_ohos && is_clang && target_cpu == "arm64") {
768    default_compiler_configs +=
769        [ "//build/config/security:stack_protector_ret_strong_config" ]
770  }
771
772  # Debug/release-related defines.
773  if (is_debug) {
774    default_compiler_configs += [ "//build/config:debug" ]
775  } else {
776    default_compiler_configs += [ "//build/config:release" ]
777  }
778
779  if (is_android) {
780    default_compiler_configs += [
781      "//build_plugins/config/aosp:default_orderfile_instrumentation",
782      "//build_plugins/config/aosp:lld_pack_relocations",
783      "//build/config/gcc:symbol_visibility_inline_hidden",
784    ]
785  }
786
787  # Static libraries and source sets use only the compiler ones.
788  default_static_library_configs = default_compiler_configs
789  default_source_set_configs = default_compiler_configs
790
791  # Executable defaults.
792  default_executable_configs = default_compiler_configs + [
793                                 "//build/config:default_libs",
794                                 "//build/config:executable_config",
795                               ]
796  if (is_android) {
797    default_executable_configs += [ "//build_plugins/config/aosp:default_libs" ]
798  }
799
800  # Shared library and loadable module defaults (also for components in component
801  # mode).
802  default_shared_library_configs = default_compiler_configs + [
803                                     "//build/config:default_libs",
804                                     "//build/config:shared_library_config",
805                                   ]
806  if (is_android) {
807    default_shared_library_configs +=
808        [ "//build_plugins/config/aosp:default_libs" ]
809  }
810}
811
812# Lite OS use different buildconfig.gn
813if (is_lite_system) {
814  import("//build/lite/ohos_var.gni")
815  import("${device_config_path}/config.gni")
816  target_arch_cflags = board_cflags
817  if (board_arch != "") {
818    target_arch_cflags += [ "-march=$board_arch" ]
819  }
820  if (board_cpu != "") {
821    target_arch_cflags += [ "-mcpu=$board_cpu" ]
822  }
823
824  arch = "arm"
825  if (current_cpu == "arm64") {
826    arch = "aarch64"
827  }
828
829  if (current_cpu == "loongarch64") {
830    arch = "loongarch64"
831  }
832
833  if (ohos_kernel_type == "liteos_a") {
834    target_triple = "$arch-liteos-ohos"
835  } else if (ohos_kernel_type == "linux") {
836    target_triple = "$arch-linux-ohos"
837  }
838
839  if (defined(board_configed_sysroot) && board_configed_sysroot != "") {
840    ohos_current_sysroot = board_configed_sysroot
841  }
842
843  # Only gcc available for liteos_m.
844  if (ohos_kernel_type == "liteos_m" || ohos_kernel_type == "linux") {
845    use_board_toolchain = true
846  }
847
848  toolchain_cmd_suffix = ""
849  if (host_os == "win") {
850    toolchain_cmd_suffix = ".exe"
851  }
852
853  # enable ccache if ccache is installed,
854  # or enable xcache if xcache is installed.
855  if (ohos_build_enable_ccache) {
856    compile_prefix = "ccache "
857  } else if (ohos_build_enable_xcache) {
858    compile_prefix = "/opt/buildtools/nextbuild/xcache "
859  } else {
860    compile_prefix = ""
861  }
862
863  # Load board adapter dir from board config.
864  if (board_adapter_dir != "") {
865    ohos_board_adapter_dir = board_adapter_dir
866    ohos_vendor_adapter_dir = board_adapter_dir
867  }
868
869  # Set current toolchain with to board configuration.
870  if (board_toolchain != "" && use_board_toolchain) {
871    ohos_build_compiler = board_toolchain_type
872    if (board_toolchain_path != "") {
873      compile_prefix += "${board_toolchain_path}/${board_toolchain_prefix}"
874    } else {
875      compile_prefix += "${board_toolchain_prefix}"
876    }
877    set_default_toolchain("//build/lite/toolchain:${board_toolchain}")
878    if (board_toolchain_type == "gcc") {
879      default_compiler_configs = []
880      ohos_current_cc_command = "${compile_prefix}gcc$toolchain_cmd_suffix"
881      ohos_current_cxx_command = "${compile_prefix}g++$toolchain_cmd_suffix"
882      ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
883      ohos_current_ld_command = ohos_current_cc_command
884      ohos_current_strip_command =
885          "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
886      default_compiler_configs += [ "//build/lite/config:gcc_opt" ]
887    } else if (board_toolchain_type == "clang") {
888      default_compiler_configs = []
889      ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
890      ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
891      compile_prefix += "llvm-"
892      ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
893      ohos_current_ld_command = ohos_current_cc_command
894      ohos_current_strip_command =
895          "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
896      default_compiler_configs += [ "//build/lite/config:clang_opt" ]
897    } else if (board_toolchain_type == "iccarm") {
898      import("//build/config/compiler/lite/iccarm/iccarm.gni")
899      ohos_current_cc_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
900      ohos_current_cxx_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
901      ohos_current_ar_command = "${compile_prefix}iarchive$toolchain_cmd_suffix"
902      ohos_current_as_command = "${compile_prefix}iasmarm$toolchain_cmd_suffix"
903      ohos_current_ld_command = "${compile_prefix}ilinkarm$toolchain_cmd_suffix"
904      ohos_current_strip_command =
905          "${compile_prefix}ielftool$toolchain_cmd_suffix --strip"
906    } else {
907      default_compiler_configs = []
908    }
909
910    # Overwrite ld cmd by customed cmd.
911    if (defined(board_customed_ld_cmd) && board_customed_ld_cmd != "") {
912      ohos_current_ld_command = board_customed_ld_cmd
913    }
914  } else {
915    # OHOS default toolchain
916    default_compiler_configs = []
917    ohos_build_compiler = "clang"
918    ohos_clang_toolchain_dir = rebase_path("${ohos_build_compiler_dir}/bin")
919    compile_prefix += "$ohos_clang_toolchain_dir/"
920    ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
921    ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
922    compile_prefix += "llvm-"
923    ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
924    ohos_current_ld_command = ohos_current_cxx_command
925    ohos_current_strip_command =
926        "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
927    if (current_os == "ohos") {
928      set_default_toolchain("//build/lite/toolchain:linux_x86_64_ohos_clang")
929      default_compiler_configs += [
930        "//build/lite/config:ohos_clang",
931        "//build/lite/config:clang_opt",
932      ]
933    } else {
934      set_default_toolchain("//build/toolchain/linux:clang_$target_cpu")
935      default_compiler_configs += [
936        "//build/config:default_libs",
937        "//build/config:executable_config",
938        "//build/config/compiler:default_include_dirs",
939        "//build/config/compiler:compiler",
940      ]
941    }
942  }
943  if (board_toolchain_type != "iccarm") {
944    if (current_os == "ohos") {
945      default_compiler_configs += [
946        "//build/lite/config:board_config",
947        "//build/lite/config:cpu_arch",
948        "//build/lite/config:common",
949        "//build/lite/config:default_link_path",
950      ]
951    }
952
953    if (ohos_build_type == "debug") {
954      default_compiler_configs += [ "//build/lite/config:debug" ]
955    } else if (ohos_build_type == "release") {
956      default_compiler_configs += [ "//build/lite/config:release" ]
957    }
958
959    if (ohos_kernel_type == "liteos_a" && current_os == "ohos") {
960      default_compiler_configs +=
961          [ "//build/lite/config/kernel/liteos/cortex_a:default" ]
962    }
963
964    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
965      default_compiler_configs += [
966        "//build/lite/config:security",
967        "//build/lite/config:exceptions",
968      ]
969    } else if (ohos_kernel_type == "liteos_m") {
970      default_compiler_configs += [ "//build/lite/config:stack_protector" ]
971    }
972
973    default_compiler_configs += [
974      "//build/lite/config:language_c",
975      "//build/lite/config:language_cpp",
976      "//build/config/sanitizers:default_sanitizer_flags",
977    ]
978
979    if (current_os == "ohos") {
980      default_compiler_configs += [ "//build/lite/config:kernel_macros" ]
981    }
982
983    default_shared_library_configs =
984        default_compiler_configs +
985        [ "//build/lite/config:shared_library_config" ]
986    default_static_library_configs = default_compiler_configs
987    default_executable_configs = default_compiler_configs
988    if (ohos_kernel_type != "liteos_m") {
989      default_static_library_configs +=
990          [ "//build/lite/config:static_pie_config" ]
991      default_executable_configs += [ "//build/lite/config:static_pie_config" ]
992      default_executable_configs +=
993          [ "//build/lite/config:pie_executable_config" ]
994    }
995    default_executable_configs += [ "//build/lite/config:board_exe_ld_flags" ]
996  }
997}
998
999set_defaults("executable") {
1000  configs = default_executable_configs
1001}
1002
1003set_defaults("static_library") {
1004  configs = default_static_library_configs
1005}
1006
1007set_defaults("shared_library") {
1008  configs = default_shared_library_configs
1009}
1010
1011set_defaults("rust_library") {
1012  configs = default_compiler_configs
1013}
1014
1015set_defaults("rust_proc_macro") {
1016  configs = default_compiler_configs
1017}
1018
1019set_defaults("source_set") {
1020  configs = default_compiler_configs
1021}
1022
1023# Sets default dependencies for executable and shared_library targets.
1024#
1025# Variables
1026#   no_default_deps: If true, no standard dependencies will be added.
1027target_type_list = [
1028  "executable",
1029  "loadable_module",
1030  "shared_library",
1031  "static_library",
1032  "rust_library",
1033  "source_set",
1034  "rust_proc_macro",
1035]
1036
1037foreach(_target_type, target_type_list) {
1038  template(_target_type) {
1039    target(_target_type, target_name) {
1040      forward_variables_from(invoker, "*", [ "no_default_deps" ])
1041      if (!defined(deps)) {
1042        deps = []
1043      }
1044      if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
1045        if (is_lite_system && current_os == "ohos") {
1046          deps += [ "//third_party/musl:sysroot_lite" ]
1047        } else {
1048          deps += [ "//build/config:${_target_type}_deps" ]
1049        }
1050      }
1051    }
1052  }
1053}
1054
1055if (is_lite_system && current_os == "ohos") {
1056  _target_type_list = [
1057    "action",
1058    "action_foreach",
1059  ]
1060
1061  foreach(_target_type, _target_type_list) {
1062    template(_target_type) {
1063      target(_target_type, target_name) {
1064        forward_variables_from(invoker, "*", [ "no_default_deps" ])
1065        if (!defined(deps)) {
1066          deps = []
1067        }
1068        if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
1069          deps += [ "//third_party/musl:sysroot_lite" ]
1070        }
1071      }
1072    }
1073  }
1074}
1075