# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# =============================================================================
# WHAT IS THIS FILE?
# =============================================================================
#
# This is the master GN build configuration. This file is loaded after the
# build args (args.gn) for the build directory and after the toplevel ".gn"
# file (which points to this file as the build configuration).
#
# This file will be executed and the resulting context will be used to execute
# every other file in the build. So variables declared here (that don't start
# with an underscore) will be implicitly global.

# =============================================================================
# PLATFORM SELECTION
# =============================================================================
#
# There are two main things to set: "os" and "cpu". The "toolchain" is the name
# of the GN thing that encodes combinations of these things.
#
# Users typically only set the variables "target_os" and "target_cpu" in "gn
# args", the rest are set up by our build and internal to GN.
#
# There are three different types of each of these things: The "host"
# represents the computer doing the compile and never changes. The "target"
# represents the main thing we're trying to build. The "current" represents
# which configuration is currently being defined, which can be either the
# host, the target, or something completely different (like nacl). GN will
# run the same build file multiple times for the different required
# configuration in the same build.
#
# This gives the following variables:
#  - host_os, host_cpu, host_toolchain
#  - target_os, target_cpu, default_toolchain
#  - current_os, current_cpu, current_toolchain.
#
# Note the default_toolchain isn't symmetrical (you would expect
# target_toolchain). This is because the "default" toolchain is a GN built-in
# concept, and "target" is something our build sets up that's symmetrical with
# its GYP counterpart. Potentially the built-in default_toolchain variable
# could be renamed in the future.
#
# When writing build files, to do something only for the host:
#   if (current_toolchain == host_toolchain) { ...

if (host_os == "mac") {
  check_mac_system_and_cpu_script =
      rebase_path("//build/scripts/check_mac_system_and_cpu.py")
  check_darwin_system_result =
      exec_script(check_mac_system_and_cpu_script, [ "system" ], "string")

  if (check_darwin_system_result != "") {
    check_mac_host_cpu_result =
        exec_script(check_mac_system_and_cpu_script, [ "cpu" ], "string")
    if (check_mac_host_cpu_result != "") {
      host_cpu = "arm64"
    }
  }
} else if (host_os == "linux") {
  check_linux_cpu_script = rebase_path("//build/scripts/check_linux_cpu.py")
  check_linux_cpu_result =
      exec_script(check_linux_cpu_script, [ "cpu" ], "string")
  if (check_linux_cpu_result != "") {
    host_cpu = "arm64"
  }
}

declare_args() {
  product_name = ""
  device_name = ""
}

declare_args() {
  using_hb_new = true
}

declare_args() {
  ohos_indep_compiler_enable = false
}

declare_args() {
  ohos_indep_compiler_components = ""
}

declare_args() {
  preloader_output_dir = "//out/preloader/${product_name}"
}

declare_args() {
  enable_lto_O0 = false
}

declare_args() {
  enable_gn_2021 = true
}

declare_args() {
  is_llvm_build = false
}

declare_args() {
  rustc_codecheck = false
}

declare_args() {
  # Whether to use the sandbox
  use_sandbox = false

  # enable sandbox debug mode
  sandbox_debug = false
}

product_build_config =
    read_file("${preloader_output_dir}/build_config.json", "json")

global_parts_info =
    read_file("${preloader_output_dir}/parts_config.json", "json")

napi_white_list_path = "developtools/integration_verification/tools/deps_guard/rules/NO-Depends-On-NAPI"
if (!is_llvm_build) {
  if (!ohos_indep_compiler_enable) {
    napi_white_list =
        read_file("//${napi_white_list_path}/whitelist.json", "json")
  } else if (ohos_indep_compiler_enable) {
    napi_white_list = read_file(
            "//build/indep_configs/mapping/config/${napi_white_list_path}/whitelist.json",
            "json")
  }
}

product_company = product_build_config.product_company
if (defined(product_build_config.device_company)) {
  device_company = product_build_config.device_company
} else {
  device_company = ""
}
if (defined(product_build_config.device_build_path)) {
  device_build_path = product_build_config.device_build_path
} else {
  device_build_path = ""
}
target_os = product_build_config.target_os
target_cpu = product_build_config.target_cpu
product_toolchain = product_build_config.product_toolchain_label
is_host_product = false
if (defined(product_build_config.is_host_product)) {
  is_host_product = product_build_config.is_host_product
}
compile_mode = "cross"
if (defined(product_build_config.compile_mode)) {
  compile_mode = product_build_config.compile_mode
}
if (product_toolchain == "") {
  product_toolchain = "//build/toolchain/ohos:ohos_clang_$target_cpu"
}
if (defined(product_build_config.ext_root_proc_conf_path)) {
  ext_root_proc_conf_path = product_build_config.ext_root_proc_conf_path
} else {
  ext_root_proc_conf_path = ""
}
if (defined(product_build_config.ext_critical_proc_conf_path)) {
  ext_critical_proc_conf_path = product_build_config.ext_critical_proc_conf_path
} else {
  ext_critical_proc_conf_path = ""
}
if (defined(product_build_config.ext_sanitizer_check_list_path)) {
  ext_sanitizer_check_list_path =
      product_build_config.ext_sanitizer_check_list_path
} else {
  joint_check_list_path = "//vendor/${product_company}/${product_name}/security_config/sanitizer_check_list.gni"
  sanitize_list_exist =
      exec_script(rebase_path("//build/scripts/check_file_exist.py"),
                  [ rebase_path("${joint_check_list_path}") ],
                  "string")
  if (sanitize_list_exist != "") {
    ext_sanitizer_check_list_path = joint_check_list_path
  }
}
if (defined(product_build_config.enable_ramdisk)) {
  enable_ramdisk = product_build_config.enable_ramdisk
} else {
  enable_ramdisk = false
}
if (defined(product_build_config.chipprod_config_path)) {
  chipprod_config_path = product_build_config.chipprod_config_path
  import("${chipprod_config_path}/chip_product_list.gni")
} else {
  chip_product_list = []
}

if (defined(product_build_config.ext_sdk_config_file)) {
  ext_sdk_config_file = product_build_config.ext_sdk_config_file
}

if (defined(product_build_config.ext_ndk_config_file)) {
  ext_ndk_config_file = product_build_config.ext_ndk_config_file
}

if (defined(product_build_config.enable_absystem)) {
  enable_absystem = product_build_config.enable_absystem
} else {
  enable_absystem = false
}

if (defined(product_build_config.enable_mesa3d)) {
  enable_mesa3d = product_build_config.enable_mesa3d
} else {
  enable_mesa3d = false
}

if (defined(product_build_config.build_selinux)) {
  build_selinux = product_build_config.build_selinux
} else {
  build_selinux = false
}

if (defined(product_build_config.build_seccomp)) {
  build_seccomp = product_build_config.build_seccomp
} else {
  build_seccomp = false
}

if (defined(product_build_config.support_jsapi)) {
  support_jsapi = product_build_config.support_jsapi
} else {
  if (defined(global_parts_info) && !defined(global_parts_info.arkui_napi)) {
    support_jsapi = false
  } else {
    support_jsapi = true
  }
}
if (defined(product_build_config.ext_sign_hap_py_path)) {
  sign_hap_py_path = product_build_config.ext_sign_hap_py_path
}

if (defined(product_build_config.global_ext_var_file)) {
  import(product_build_config.global_ext_var_file)
}

if (target_os == "") {
  target_os = "ohos"
}

if (target_cpu == "") {
  if (target_os == "ohos" || target_os == "android" || target_os == "ios") {
    target_cpu = "arm"
  } else {
    target_cpu = host_cpu
  }
}

if (current_cpu == "") {
  current_cpu = target_cpu
}
if (current_os == "") {
  current_os = target_os
}

declare_args() {
  is_mini_system = false
  is_small_system = false
  is_standard_system = false
}

if (ohos_indep_compiler_enable) {
  is_standard_system = true
  musl_is_legacy = false
}

if (is_mini_system) {
  os_level = "mini"
}
if (is_small_system) {
  os_level = "small"
}
if (is_standard_system) {
  os_level = "standard"
}

declare_args() {
  is_large_system = !(is_standard_system || is_small_system || is_mini_system)
}

declare_args() {
  factory = false
}

is_lite_system = is_mini_system || is_small_system

# =============================================================================
# BUILD FLAGS
# =============================================================================
#
# This block lists input arguments to the build, along with their default
# values.
#
# If a value is specified on the command line, it will overwrite the defaults
# given in a declare_args block, otherwise the default will be used.
#
# YOU SHOULD ALMOST NEVER NEED TO ADD FLAGS TO THIS FILE. GN allows any file in
# the build to declare build flags. If you need a flag for a single component,
# you can just declare it in the corresponding BUILD.gn file.
#
# - If your feature is a single target, say //components/foo, you can put
#   a declare_args() block in //components/foo/BUILD.gn and use it there.
#   Nobody else in the build needs to see the flag.
#
# - Defines based on build variables should be implemented via the generated
#   build flag header system. See //build/buildflag_header.gni. You can put
#   the buildflag_header target in the same file as the build flag itself. You
#   should almost never set "defines" directly.
#
# - If your flag toggles a target on and off or toggles between different
#   versions of similar things, write a "group" target that forwards to the
#   right target (or no target) depending on the value of the build flag. This
#   group can be in the same BUILD.gn file as the build flag, and targets can
#   depend unconditionally on the group rather than duplicating flag checks
#   across many targets.
#
# - If a semi-random set of build files REALLY needs to know about a define and
#   the above pattern for isolating the build logic in a forwarding group
#   doesn't work, you can put the argument in a .gni file. This should be put
#   in the lowest level of the build that knows about this feature (which should
#   almost always be outside of the //build directory!).
#
# Other flag advice:
#
# - Use boolean values when possible. If you need a default value that expands
#   to some complex thing in the default case (like the location of the
#   compiler which would be computed by a script), use a default value of -1 or
#   the empty string. Outside of the declare_args block, conditionally expand
#   the default value as necessary.
#
# - Use a name like "use_foo" or "is_foo" (whatever is more appropriate for
#   your feature) rather than just "foo".
#
# - Write good comments directly above the declaration with no blank line.
#   These comments will appear as documentation in "gn args --list".
#
# - Don't call exec_script inside declare_args. This will execute the script
#   even if the value is overridden, which is wasteful. See first bullet.

declare_args() {
  # Set to enable the official build level of optimization. This has nothing
  # to do with branding, but enables an additional level of optimization above
  # release (!is_debug). This might be better expressed as a tri-state
  # (debug, release, official) but for historical reasons there are two
  # separate flags.
  is_official_build = false

  # Whether we're a traditional desktop unix.
  is_desktop_linux = current_os == "linux"

  # Set to true when compiling with the Clang compiler.
  is_clang = current_os != "linux" ||
             (current_cpu != "s390x" && current_cpu != "s390" &&
              current_cpu != "ppc64" && current_cpu != "ppc" &&
              current_cpu != "mips" && current_cpu != "mips64")

  # Allows the path to a custom target toolchain to be injected as a single
  # argument, and set as the default toolchain.
  custom_toolchain = ""

  # This should not normally be set as a build argument.  It's here so that
  # every toolchain can pass through the "global" value via toolchain_args().
  host_toolchain = ""

  # target platform
  target_platform = "phone"

  # Whether it is test.
  is_test = false

  # Whether it is double framework.
  is_double_framework = false

  # build for cross platform
  is_arkui_x = false
}

declare_args() {
  use_musl = true
}

declare_args() {
  is_emulator = false
}

asdk_libs_dir = "//prebuilts/asdk_libs"

# Whether it is a phone product.
is_phone_product = "${target_platform}" == "phone"

# Whether it is a ivi product.
is_ivi_product = "${target_platform}" == "ivi"

is_wearable_product = "${target_platform}" == "wearable"

is_intellitv_product = "${target_platform}" == "intellitv"

if ((target_os == "ohos" && target_cpu == "x86_64") ||
    device_company == "emulator") {
  is_emulator = true
}

# different host platform tools directory.
if (host_os == "linux") {
  if (host_cpu == "arm64") {
    host_platform_dir = "linux-aarch64"
  } else {
    host_platform_dir = "linux-x86_64"
  }
} else if (host_os == "mac") {
  if (host_cpu == "arm64") {
    host_platform_dir = "darwin-arm64"
  } else {
    host_platform_dir = "darwin-x86_64"
  }
} else {
  assert(false, "Unsupported host_os: $host_os")
}

declare_args() {
  # Debug build. Enabling official builds automatically sets is_debug to false.
  is_debug = false
}

declare_args() {
  # Profile build. Enabling official builds automatically sets is_profile to false.
  is_profile = false
}

declare_args() {
  # The runtime mode ("debug", "profile", "release")
  runtime_mode = "release"
}

declare_args() {
  # Enable mini debug info, it will add .gnu_debugdata
  # section in each stripped sofile

  # Currently, we don't publish ohos-adapted python on m1 platform,
  # So that we disable mini debug info on m1 platform until
  # ohos-adapted python publishing on m1 platform
  if (host_os == "mac") {
    full_mini_debug = false
  } else {
    full_mini_debug = true
  }
}

declare_args() {
  # Full Debug mode. Setting optimize level to "-O0" and symbol level to "-g3".
  # It should be used with "is_debug"
  ohos_full_debug = false
}

declare_args() {
  # Specifies which components use the DEBUG compilation level
  # Using this arg like this, --gn-args 'enable_debug_components="component1,component2"'
  enable_debug_components = ""
}

declare_args() {
  # We use this arg to split "enable_debug_components" to a list
  debug_components = string_split(enable_debug_components, ",")
}

declare_args() {
  build_xts = false
  precise_xts = false
}

declare_args() {
  # Component build. Setting to true compiles targets declared as "components"
  # as shared libraries loaded dynamically. This speeds up development time.
  # When false, components will be linked statically.
  #
  # For more information see
  # https://chromium.googlesource.com/chromium/src/+/master/docs/component_build.md
  is_component_build = true
}

declare_args() {
  enable_adlt = false
  adlt_exe = ""
  allowed_lib_list = ""
  adlt_lib_name = ""
}

assert(!(is_debug && is_official_build), "Can't do official debug builds")

# ==============================================================================
# TOOLCHAIN SETUP
# ==============================================================================
#
# Here we set the default toolchain, as well as the variable host_toolchain
# which will identify the toolchain corresponding to the local system when
# doing cross-compiles. When not cross-compiling, this will be the same as the
# default toolchain.
#
# We do this before anything else to make sure we complain about any
# unsupported os/cpu combinations as early as possible.

if (host_toolchain == "") {
  # This should only happen in the top-level context.
  # In a specific toolchain context, the toolchain_args()
  # block should have propagated a value down.

  if (host_os == "linux") {
    if (target_os != "linux") {
      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
    } else if (is_clang) {
      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
    } else {
      host_toolchain = "//build/toolchain/linux:$host_cpu"
    }
  } else if (host_os == "mac") {
    host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
  } else if (host_os == "win") {
    if (target_cpu == "x86" || target_cpu == "x64") {
      if (is_clang) {
        host_toolchain = "//build/toolchain/win:win_clang_$target_cpu"
      } else {
        host_toolchain = "//build/toolchain/win:$target_cpu"
      }
    } else if (is_clang) {
      host_toolchain = "//build/toolchain/win:win_clang_$host_cpu"
    } else {
      host_toolchain = "//build/toolchain/win:$host_cpu"
    }
  } else {
    assert(false, "Unsupported host_os: $host_os")
  }
}

if (is_standard_system) {
  _default_toolchain = ""

  if (target_os == "ohos") {
    assert(host_os == "linux" || host_os == "mac",
           "ohos builds are only supported on Linux and Mac hosts.")
    _default_toolchain = product_toolchain
  } else if (target_os == "linux") {
    if (is_clang) {
      _default_toolchain = "//build/toolchain/linux:clang_$target_cpu"
    } else {
      _default_toolchain = "//build/toolchain/linux:$target_cpu"
    }
  } else if (target_os == "android") {
    assert(host_os == "linux" || host_os == "mac",
           "AOSP builds are only supported on Linux and Mac hosts.")
    _default_toolchain = "//build_plugins/toolchain/aosp:aosp_clang_$target_cpu"
  } else if (target_os == "ios") {
    _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_$target_cpu"
    import("//build_plugins/config/ios/ios_sdk.gni")  # For use_ios_simulator
    if (use_ios_simulator) {
      if (target_cpu == "arm64") {
        _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_arm64_sim"
      } else {
        _default_toolchain = "//build_plugins/toolchain/ios:ios_clang_x64_sim"
      }
    }
  } else {
    assert(false, "Unsupported target_os: $target_os")
  }

  # If a custom toolchain has been set in the args, set it as default. Otherwise,
  # set the default toolchain for the platform (if any).
  if (custom_toolchain != "") {
    set_default_toolchain(custom_toolchain)
  } else if (_default_toolchain != "") {
    set_default_toolchain(_default_toolchain)
  }
}

# =============================================================================
# OS DEFINITIONS
# =============================================================================
#
# We set these various is_FOO booleans for convenience in writing OS-based
# conditions.
#
# - is_ohos, is_chromeos, and is_win should be obvious.
# - is_mac is set only for desktop Mac.
# - is_posix is true for mac and any Unix-like system (basically everything
#   except Windows).
# - is_linux is true for desktop Linux and ChromeOS.
#
# Do not add more is_* variants here for random lesser-used Unix systems like
# aix or one of the BSDs. If you need to check these, just check the
# current_os value directly.

if (current_os == "win" || current_os == "winuwp") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = false
  is_mac = false
  is_nacl = false
  is_posix = false
  is_win = true
  is_mingw = false
  is_android = false
  is_ios = false
} else if (current_os == "mac") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = false
  is_mac = true
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = false
  is_android = false
  is_ios = false
} else if (current_os == "ohos") {
  is_aix = false
  is_ohos = true
  is_chromeos = false
  is_linux = false
  is_mac = false
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = false
  is_android = false
  is_ios = false
} else if (current_os == "linux") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = true
  is_mac = false
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = false
  is_android = false
  is_ios = false
} else if (current_os == "mingw") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = false
  is_mac = false
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = true
  is_android = false
  is_ios = false
} else if (current_os == "android") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = false
  is_mac = false
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = false
  is_android = true
  is_ios = false
} else if (current_os == "ios") {
  is_aix = false
  is_ohos = false
  is_chromeos = false
  is_linux = false
  is_mac = false
  is_nacl = false
  is_posix = true
  is_win = false
  is_mingw = false
  is_android = false
  is_ios = true
}

# =============================================================================
# SOURCES FILTERS
# =============================================================================
#
# These patterns filter out platform-specific files when assigning to the
# sources variable. The magic variable |sources_assignment_filter| is applied
# to each assignment or appending to the sources variable and matches are
# automatically removed.
#
# Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
# boundary = end of string or slash) are supported, and the entire string
# must match the pattern (so you need "*.cc" to match all .cc files, for
# example).

# DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
# below.
sources_assignment_filter = []

if (!is_win && !is_mingw) {
  sources_assignment_filter += [
    "*_win.cc",
    "*_win.h",
    "*_win_unittest.cc",
    "*\bwin/*",
    "*.def",
  ]
}
if (!is_mac || !is_ios) {
  sources_assignment_filter += [
    "*_mac.h",
    "*_mac.cc",
    "*_mac.mm",
    "*_mac_unittest.h",
    "*_mac_unittest.cc",
    "*_mac_unittest.mm",
    "*\bmac/*",
    "*_cocoa.h",
    "*_cocoa.cc",
    "*_cocoa.mm",
    "*_cocoa_unittest.h",
    "*_cocoa_unittest.cc",
    "*_cocoa_unittest.mm",
    "*\bcocoa/*",
  ]
}
if (!is_linux && !is_ohos && !is_android && !is_ios) {
  sources_assignment_filter += [
    "*_linux.h",
    "*_linux.cc",
    "*_linux_unittest.h",
    "*_linux_unittest.cc",
    "*\blinux/*",
  ]
}
if (!is_ohos) {
  sources_assignment_filter += []
}

#set_sources_assignment_filter(sources_assignment_filter)
if (is_standard_system) {
  if (!is_host_product) {
    file_exist = exec_script(rebase_path("//build/scripts/check_file_exist.py"),
                             [ rebase_path("//${device_build_path}/config.gni") ],
                             "string")
    if (file_exist != "") {
      import("//${device_build_path}/config.gni")
    }
  }

  # =============================================================================
  # TARGET DEFAULTS
  # =============================================================================
  #
  # Set up the default configuration for every build target of the given type.
  # The values configured here will be automatically set on the scope of the
  # corresponding target. Target definitions can add or remove to the settings
  # here as needed.
  #
  # WHAT GOES HERE?
  #
  # Other than the main compiler and linker configs, the only reason for a config
  # to be in this list is if some targets need to explicitly override that config
  # by removing it. This is how targets opt-out of flags. If you don't have that
  # requirement and just need to add a config everywhere, reference it as a
  # sub-config of an existing one, most commonly the main "compiler" one.

  # Holds all configs used for running the compiler.
  default_compiler_configs = [
    "//build/config:feature_flags",
    "//build/config/compiler:afdo",
    "//build/config/compiler:afdo_optimize_size",
    "//build/config/compiler:compiler",
    "//build/config/compiler:compiler_arm_fpu",
    "//build/config/compiler:compiler_arm_thumb",
    "//build/config/compiler:chromium_code",
    "//build/config/compiler:default_include_dirs",
    "//build/config/compiler:default_optimization",
    "//build/config/compiler:default_stack_frames",
    "//build/config/compiler:default_symbols",
    "//build/config/compiler:export_dynamic",
    "//build/config/compiler:no_exceptions",
    "//build/config/compiler:no_rtti",
    "//build/config/compiler:runtime_library",
    "//build/config/compiler:thin_archive",
    "//build/config/compiler:no_common",
    "//build/config/coverage:default_coverage",
    "//build/config/sanitizers:default_sanitizer_flags",
    "//build/config/security:default_security_configs",
    "//build/config/rust:rust_config",
    "//build/config:predefined_macro",
    "//build/config:cust_board_config",
  ]

  if (is_ohos) {
    default_compiler_configs += [
      "//build/config/ohos:default_orderfile_instrumentation",
      "//build/config/gcc:symbol_visibility_inline_hidden",
    ]
  }

  if (is_clang) {
    default_compiler_configs += [
      "//build/config/clang:find_bad_constructs",
      "//build/config/clang:extra_warnings",
    ]
  }

  if (is_ohos && is_clang && target_cpu == "arm64") {
    default_compiler_configs +=
        [ "//build/config/security:stack_protector_ret_strong_config" ]
  }

  # Debug/release-related defines.
  if (is_debug) {
    default_compiler_configs += [ "//build/config:debug" ]
  } else {
    default_compiler_configs += [ "//build/config:release" ]
  }

  if (is_android) {
    default_compiler_configs += [
      "//build_plugins/config/aosp:default_orderfile_instrumentation",
      "//build_plugins/config/aosp:lld_pack_relocations",
      "//build/config/gcc:symbol_visibility_inline_hidden",
    ]
  }

  # Static libraries and source sets use only the compiler ones.
  default_static_library_configs = default_compiler_configs
  default_source_set_configs = default_compiler_configs

  # Executable defaults.
  default_executable_configs = default_compiler_configs + [
                                 "//build/config:default_libs",
                                 "//build/config:executable_config",
                               ]
  if (is_android) {
    default_executable_configs += [ "//build_plugins/config/aosp:default_libs" ]
  }

  # Shared library and loadable module defaults (also for components in component
  # mode).
  default_shared_library_configs = default_compiler_configs + [
                                     "//build/config:default_libs",
                                     "//build/config:shared_library_config",
                                   ]
  if (is_android) {
    default_shared_library_configs +=
        [ "//build_plugins/config/aosp:default_libs" ]
  }
}

# Lite OS use different buildconfig.gn
if (is_lite_system) {
  import("//build/lite/ohos_var.gni")
  import("${device_config_path}/config.gni")
  target_arch_cflags = board_cflags
  if (board_arch != "") {
    target_arch_cflags += [ "-march=$board_arch" ]
  }
  if (board_cpu != "") {
    target_arch_cflags += [ "-mcpu=$board_cpu" ]
  }

  arch = "arm"
  if (current_cpu == "arm64") {
    arch = "aarch64"
  } else if (current_cpu == "riscv32") {
    arch = "riscv32"
  } else if (current_cpu == "loongarch64") {
    arch = "loongarch64"
  }

  if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "liteos_m") {
    target_triple = "$arch-liteos-ohos"
  } else if (ohos_kernel_type == "linux") {
    target_triple = "$arch-linux-ohos"
  }

  if (defined(board_configed_sysroot) && board_configed_sysroot != "") {
    ohos_current_sysroot = board_configed_sysroot
  }

  # Only gcc available for liteos_m.
  if (ohos_kernel_type == "liteos_m" || ohos_kernel_type == "linux") {
    use_board_toolchain = true
  }

  toolchain_cmd_suffix = ""
  if (host_os == "win") {
    toolchain_cmd_suffix = ".exe"
  }

  # enable ccache if ccache is installed,
  # or enable xcache if xcache is installed.
  if (ohos_build_enable_ccache) {
    compile_prefix = "ccache "
  } else if (ohos_build_enable_xcache) {
    compile_prefix = "/opt/buildtools/nextbuild/xcache "
  } else {
    compile_prefix = ""
  }

  # Load board adapter dir from board config.
  if (board_adapter_dir != "") {
    ohos_board_adapter_dir = board_adapter_dir
    ohos_vendor_adapter_dir = board_adapter_dir
  }

  # Set current toolchain with to board configuration.
  if (board_toolchain != "" && use_board_toolchain) {
    ohos_build_compiler = board_toolchain_type
    if (board_toolchain_path != "") {
      compile_prefix += "${board_toolchain_path}/${board_toolchain_prefix}"
    } else {
      compile_prefix += "${board_toolchain_prefix}"
    }
    set_default_toolchain("//build/lite/toolchain:${board_toolchain}")
    if (board_toolchain_type == "gcc") {
      default_compiler_configs = []
      ohos_current_cc_command = "${compile_prefix}gcc$toolchain_cmd_suffix"
      ohos_current_cxx_command = "${compile_prefix}g++$toolchain_cmd_suffix"
      ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
      ohos_current_ld_command = ohos_current_cc_command
      ohos_current_strip_command =
          "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
      default_compiler_configs += [ "//build/lite/config:gcc_opt" ]
    } else if (board_toolchain_type == "clang") {
      default_compiler_configs = []
      ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
      ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
      compile_prefix += "llvm-"
      ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
      ohos_current_ld_command = ohos_current_cxx_command
      ohos_current_strip_command =
          "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
      default_compiler_configs += [ "//build/lite/config:clang_opt" ]
    } else if (board_toolchain_type == "iccarm") {
      import("//build/config/compiler/lite/iccarm/iccarm.gni")
      ohos_current_cc_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
      ohos_current_cxx_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
      ohos_current_ar_command = "${compile_prefix}iarchive$toolchain_cmd_suffix"
      ohos_current_as_command = "${compile_prefix}iasmarm$toolchain_cmd_suffix"
      ohos_current_ld_command = "${compile_prefix}ilinkarm$toolchain_cmd_suffix"
      ohos_current_strip_command =
          "${compile_prefix}ielftool$toolchain_cmd_suffix --strip"
    } else {
      default_compiler_configs = []
    }

    # Overwrite ld cmd by customed cmd.
    if (defined(board_customed_ld_cmd) && board_customed_ld_cmd != "") {
      ohos_current_ld_command = board_customed_ld_cmd
    }
  } else {
    # OHOS default toolchain
    default_compiler_configs = []
    ohos_build_compiler = "clang"
    ohos_clang_toolchain_dir = rebase_path("${ohos_build_compiler_dir}/bin")
    compile_prefix += "$ohos_clang_toolchain_dir/"
    ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
    ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
    compile_prefix += "llvm-"
    ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
    ohos_current_ld_command = ohos_current_cxx_command
    ohos_current_strip_command =
        "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
    if (current_os == "ohos") {
      set_default_toolchain("//build/lite/toolchain:linux_x86_64_ohos_clang")
      default_compiler_configs += [
        "//build/lite/config:ohos_clang",
        "//build/lite/config:clang_opt",
      ]
    } else {
      set_default_toolchain("//build/toolchain/linux:clang_$target_cpu")
      default_compiler_configs += [
        "//build/config:default_libs",
        "//build/config:executable_config",
        "//build/config/compiler:default_include_dirs",
        "//build/config/compiler:compiler",
      ]
    }
  }
  if (board_toolchain_type != "iccarm") {
    if (current_os == "ohos") {
      default_compiler_configs += [
        "//build/lite/config:cpu_arch",
        "//build/lite/config:common",
        "//build/lite/config:default_link_path",
      ]
    }

    if (ohos_build_type == "debug") {
      default_compiler_configs += [ "//build/lite/config:debug" ]
    } else if (ohos_build_type == "release") {
      default_compiler_configs += [ "//build/lite/config:release" ]
    }

    if (ohos_kernel_type == "liteos_a" && current_os == "ohos") {
      default_compiler_configs +=
          [ "//build/lite/config/kernel/liteos/cortex_a:default" ]
    }

    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
      default_compiler_configs += [
        "//build/lite/config:security",
        "//build/lite/config:exceptions",
      ]
    } else if (ohos_kernel_type == "liteos_m") {
      default_compiler_configs += [ "//build/lite/config:stack_protector" ]
    }

    default_compiler_configs += [
      "//build/lite/config:language_c",
      "//build/lite/config:language_cpp",
      "//build/config/sanitizers:default_sanitizer_flags",
    ]

    if (current_os == "ohos") {
      default_compiler_configs += [
        "//build/lite/config:kernel_macros",
        "//build/lite/config:board_config",
      ]
    }
    if (current_os == "ohos" && !is_mini_system && !ohos_kernel_is_prebuilt) {
      default_compiler_configs += [ "//build/lite/config:sysroot_flags" ]
    }
    default_shared_library_configs =
        default_compiler_configs +
        [ "//build/lite/config:shared_library_config" ]
    default_static_library_configs = default_compiler_configs
    default_executable_configs = default_compiler_configs
    if (ohos_kernel_type != "liteos_m") {
      default_static_library_configs +=
          [ "//build/lite/config:static_pie_config" ]
      default_executable_configs += [ "//build/lite/config:static_pie_config" ]
      default_executable_configs +=
          [ "//build/lite/config:pie_executable_config" ]
    }
    default_executable_configs += [ "//build/lite/config:board_exe_ld_flags" ]
  }
}

set_defaults("executable") {
  configs = default_executable_configs
}

set_defaults("static_library") {
  configs = default_static_library_configs
}

set_defaults("shared_library") {
  configs = default_shared_library_configs
}

set_defaults("rust_library") {
  configs = default_compiler_configs
}

set_defaults("rust_proc_macro") {
  configs = default_compiler_configs
}

set_defaults("source_set") {
  configs = default_compiler_configs
}

# Sets default dependencies for executable and shared_library targets.
#
# Variables
#   no_default_deps: If true, no standard dependencies will be added.
target_type_list = [
  "executable",
  "loadable_module",
  "shared_library",
  "static_library",
  "rust_library",
  "source_set",
  "rust_proc_macro",
]

foreach(_target_type, target_type_list) {
  template(_target_type) {
    target(_target_type, target_name) {
      forward_variables_from(invoker, "*", [ "no_default_deps" ])
      if (!defined(deps)) {
        deps = []
      }
      if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
        if (is_lite_system && current_os == "ohos") {
          deps += [ "//third_party/musl:sysroot_lite" ]
        } else {
          deps += [ "//build/config:${_target_type}_deps" ]
        }
      }
    }
  }
}

action_target_type_list = [
  "action",
  "action_foreach",
]

if (is_lite_system && current_os == "ohos") {
  foreach(_target_type, action_target_type_list) {
    template(_target_type) {
      target(_target_type, target_name) {
        forward_variables_from(invoker, "*", [ "no_default_deps" ])
        if (!defined(deps)) {
          deps = []
        }
        if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
          deps += [ "//third_party/musl:sysroot_lite" ]
        }
      }
    }
  }
}

import("//build/sandbox/sandbox.gni")

if (is_standard_system && use_sandbox) {
  no_sandbox_script_list =
      rebase_path(sandbox_config.no_sandbox_script_list, "//")

  foreach(_target_type, action_target_type_list) {
    template(_target_type) {
      script_path = rebase_path(invoker.script, "//")
      target_label = get_label_info(":${target_name}", "label_no_toolchain")

      if (sandbox_debug) {
        print("action label: ${target_label}, script: ${script_path}")
      }

      if (filter_include(no_sandbox_script_list, [ script_path ]) != []) {
        target(_target_type, target_name) {
          forward_variables_from(invoker, "*", [ "no_default_deps" ])
          if (!defined(depfile) ||
              filter_include(args, [ rebase_path(depfile, root_build_dir) ]) ==
              []) {
            # Trusted script that do not use the sandbox need to configure the depfile field and pass to script
            print("WARNING: ${target_label} not set depfile or not pass args")
          } else {
            not_needed([ "target_label" ])
          }
        }
      } else {
        metadata_target = "${target_name}__sandbox_metadata"
        metadata_file = "${target_out_dir}/${metadata_target}.json"

        target(_target_type, target_name) {
          forward_variables_from(invoker,
                                 [
                                   "testonly",
                                   "visibility",
                                   "outputs",
                                 ])
          forward_variables_from(invoker,
                                 "*",
                                 [
                                   "no_default_deps",
                                   "script",
                                   "args",
                                 ])

          script = sandbox_config.sandbox_script
          args = [
            "--sandbox-metadata",
            rebase_path(metadata_file, root_build_dir),
            "--script-file",
            rebase_path(invoker.script, root_build_dir),
          ]

          if (sandbox_debug) {
            args += [ "--debug" ]
          }

          if (defined(invoker.args) && invoker.args != []) {
            args += [ "--" ]
            args += invoker.args
          }

          if (!defined(deps)) {
            deps = []
          }
          deps += [ ":${metadata_target}" ]
        }

        action_output =
            rebase_path(get_target_outputs(":${target_name}"), root_build_dir)
        sandbox_metadata("${metadata_target}") {
          forward_variables_from(invoker,
                                 [
                                   "custom_mount_dir",
                                   "inputs",
                                   "sources",
                                 ])
          if (defined(invoker.depfile)) {
            depfile = invoker.depfile
          } else {
            if (_target_type == "action_foreach") {
              depfile = "${target_gen_dir}/{{source_name_part}}__sandbox.d"
            } else {
              depfile = "${target_gen_dir}/${target_name}__sandbox.d"
            }
          }
        }
      }
    }
  }
}