# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Toolchain-related configuration that may be needed outside the context of the
# toolchain() rules themselves.

import("//build/toolchain/sysroot.gni")

declare_args() {
  # If this is set to true, we use the revision in the llvm repo to determine
  # the CLANG_REVISION to use, instead of the version hard-coded into
  # //tools/clang/scripts/update.py. This should only be used in
  # conjunction with setting the llvm_force_head_revision DEPS variable when
  # `gclient runhooks` is run as well.
  llvm_force_head_revision = false

  # If this arg is set to true, we use the ohos Mainline LLVM.
  llvm_ohos_mainline = false

  # Equivalent to llvm_force_head_revision, but for rust. When true, we expect
  # to find a locally-build version of rust rather than the version specified
  # in //tools/clang/scripts/update.py.
  rust_force_head_revision = false

  # Used for binary size analysis.
  generate_linker_map = is_android && is_official_build

  # Whether this toolchain is to be used for building host tools that are
  # consumed during the build process. That includes proc macros and Cargo build
  # scripts.
  toolchain_for_rust_host_build_tools = false

  # If false, the toolchain overrides `use_partition_alloc_as_malloc` in
  # PartitionAlloc, to allow use of the system allocator.
  toolchain_allows_use_partition_alloc_as_malloc = true

  # Use -MD instead of -MMD for compiler commands. This is useful for tracking
  # the comprehensive set of dependencies.  It's also required when building
  # without the sysroot so that updates to system header files trigger a
  # rebuild (when using the sysroot, the CR_SYSROOT_KEY define takes care of
  # this already).
  system_headers_in_deps = !use_sysroot
}

declare_args() {
  if (llvm_ohos_mainline) {
    clang_version = "22"
  } else {
    clang_version = "22"
  }
}

# Extension for shared library files (including leading dot).
if (is_apple) {
  shlib_extension = ".dylib"
} else if (is_posix || is_fuchsia) {
  shlib_extension = ".so"
} else if (is_win) {
  shlib_extension = ".dll"
} else if (is_wasm) {
  # WebAssembly does not stably support shared libraries. (as of Oct 2019)
  shlib_extension = ".wasm"
} else {
  assert(false, "Platform not supported")
}

# Same extension but for the host platform. We have significantly fewer host
# platforms.
if (host_os == "mac") {
  host_shlib_extension = ".dylib"
} else if (host_os == "win") {
  host_shlib_extension = ".dll"
} else if (host_os == "linux" || host_os == "aix" || host_os == "zos") {
  host_shlib_extension = ".so"
} else {
  assert(false, "Host platform not supported")
}

# Prefix for shared library files.
if (is_posix || is_fuchsia) {
  shlib_prefix = "lib"
} else {
  shlib_prefix = ""
}

# Directory for shared library files.
if (is_fuchsia) {
  shlib_subdir = "/lib"
} else {
  shlib_subdir = ""
}

# While other "tool"s in a toolchain are specific to the target of that
# toolchain, the "stamp" and "copy" tools are really generic to the host;
# but each toolchain must define them separately.  GN doesn't allow a
# template instantiation inside a toolchain definition, so some boilerplate
# has to be repeated in each toolchain to define these two tools.  These
# four variables reduce the duplication in that boilerplate.
stamp_description = "STAMP {{output}}"
copy_description = "COPY {{source}} {{output}}"
if (host_os == "win") {
  _tool_wrapper_path =
      rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)

  stamp_command = "cmd /c type nul > \"{{output}}\""
  copy_command = "\"$python_path\" $_tool_wrapper_path recursive-mirror {{source}} {{output}}"
} else {
  stamp_command = "touch {{output}}"
  copy_command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
}

# This variable is true if the current toolchain is one of the target
# toolchains, i.e. a toolchain which is being used to build the main Chrome
# binary. This generally means "not the host toolchain", but in the case where
# we're targeting the host it's true then as well. We do require current_os to
# match target_os so that for example we avoid considering Android as a target
# toolchain when targeting CrOS.
is_a_target_toolchain =
    (current_toolchain != host_toolchain ||
     default_toolchain == host_toolchain) && current_os == target_os

# A toolchain for building tools that run on the host machine and need to use
# the system allocator. This toolchain does not use PartitionAlloc-Everywhere by
# design. We use a name with `_host` injected into it to avoid colliding with
# toolchains of the same name (but different path) between different OSes.
host_system_allocator_toolchain = "${host_toolchain}_host_with_system_allocator"

# A toolchain for building tools that run on the default target machine and need
# to use the system allocator. This toolchain does not use
# PartitionAlloc-Everywhere by design.
default_system_allocator_toolchain =
    "${default_toolchain}_with_system_allocator"