#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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.

import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/v8_target_cpu.gni")
import("//build/toolchain/cc_wrapper.gni")
import("//build/toolchain/clang_static_analyzer.gni")
import("//build/toolchain/toolchain.gni")
import("//build/ohos_var.gni")

if (defined(remote_execution) && remote_execution) {
  import("${rbe_path}/rbe.gni")
}

if (!is_arkui_x) {
  import("//build/rust/rustc_toolchain.gni")
}

if (is_nacl) {
  # To keep NaCl variables out of builds that don't include NaCl, all
  # variables defined in nacl/config.gni referenced here should be protected by
  # is_nacl conditions.
  import("//build/config/nacl/config.gni")
}

declare_args() {
  # Enables allowlist generation for IDR_ grit defines seen by the compiler.
  # Currently works only on ohos and enabled by default for release builds.
  # Requires debug info, so disabled for symbol_level=0 & strip_debug_info=true.
  enable_resource_allowlist_generation =
      is_ohos && !is_debug &&
      # Always enable for official builds, but enable for release builds by
      # default only when other args allow.
      (is_official_build ||
       (!strip_debug_info && symbol_level > 0 && !is_component_build))
}

declare_args() {
  share_ccache = ""
  toolchain_uses_remoteexec = false
}

# When the arg is set via args.gn, it applies to all toolchains. In order to not
# hit the assert in grit_rule.gni, explicitly disable for host toolchains.
if (is_linux && target_os == "ohos") {
  enable_resource_allowlist_generation = false
}

# Path to the Clang static analysis wrapper script.
# REVIEWERS: can you suggest a better location for this?
# GN is really picky about dead stores of variables except at the global scope.
analyzer_wrapper =
    rebase_path("//build/toolchain/clang_static_analyzer_wrapper.py",
                root_build_dir) + " --mode=clang"

# This template defines a toolchain for something that works like gcc
# (including clang).
#
# It requires the following variables specifying the executables to run:
#  - ar
#  - cc
#  - cxx
#  - ld
#
# Optional parameters that control the tools:
#
#  - extra_cflags
#      Extra flags to be appended when compiling C files (but not C++ files).
#  - extra_cppflags
#      Extra flags to be appended when compiling both C and C++ files. "CPP"
#      stands for "C PreProcessor" in this context, although it can be
#      used for non-preprocessor flags as well. Not to be confused with
#      "CXX" (which follows).
#  - extra_cxxflags
#      Extra flags to be appended when compiling C++ files (but not C files).
#  - extra_asmflags
#      Extra flags to be appended when compiling assembly.
#  - extra_ldflags
#      Extra flags to be appended when linking
#
#  - libs_section_prefix
#  - libs_section_postfix
#      The contents of these strings, if specified, will be placed around
#      the libs section of the linker line. It allows one to inject libraries
#      at the beginning and end for all targets in a toolchain.
#  - solink_libs_section_prefix
#  - solink_libs_section_postfix
#      Same as libs_section_{pre,post}fix except used for solink instead of link.
#  - link_outputs
#      The content of this array, if specified, will be added to the list of
#      outputs from the link command. This can be useful in conjunction with
#      the post_link parameter.
#  - use_unstripped_as_runtime_outputs
#      When |strip| is set, mark unstripped executables as runtime deps rather
#      than stripped ones.
#  - post_link
#      The content of this string, if specified, will be run as a separate
#      command following the the link command.
#  - deps
#      Just forwarded to the toolchain definition.
#  - executable_extension
#      If this string is specified it will be used for the file extension
#      for an executable, rather than using no extension; targets will
#      still be able to override the extension using the output_extension
#      variable.
#  - rebuild_define
#      The contents of this string, if specified, will be passed as a #define
#      to the toolchain. It can be used to force recompiles whenever a
#      toolchain is updated.
#  - shlib_extension
#      If this string is specified it will be used for the file extension
#      for a shared library, rather than default value specified in
#      toolchain.gni
#  - strip
#      Location of the strip executable. When specified, strip will be run on
#      all shared libraries and executables as they are built. The pre-stripped
#      artifacts will be put in lib.unstripped/ and exe.unstripped/.
template("gcc_toolchain") {
  toolchain(target_name) {
    assert(defined(invoker.ar), "gcc_toolchain() must specify a \"ar\" value")
    assert(defined(invoker.cc), "gcc_toolchain() must specify a \"cc\" value")
    assert(defined(invoker.cxx), "gcc_toolchain() must specify a \"cxx\" value")
    assert(defined(invoker.ld), "gcc_toolchain() must specify a \"ld\" value")

    # This define changes when the toolchain changes, forcing a rebuild.
    # Nothing should ever use this define.
    if (defined(invoker.rebuild_define)) {
      rebuild_string = "-D" + invoker.rebuild_define + " "
    } else {
      rebuild_string = ""
    }

    # GN's syntax can't handle more than one scope dereference at once, like
    # "invoker.toolchain_args.foo", so make a temporary to hold the toolchain
    # args so we can do "invoker_toolchain_args.foo".
    assert(defined(invoker.toolchain_args),
           "Toolchains must specify toolchain_args")
    invoker_toolchain_args = invoker.toolchain_args
    assert(defined(invoker_toolchain_args.current_cpu),
           "toolchain_args must specify a current_cpu")
    assert(defined(invoker_toolchain_args.current_os),
           "toolchain_args must specify a current_os")

    # When invoking this toolchain not as the default one, these args will be
    # passed to the build. They are ignored when this is the default toolchain.
    toolchain_args = {
      # Populate toolchain args from the invoker.
      forward_variables_from(invoker_toolchain_args, "*")

      # The host toolchain value computed by the default toolchain's setup
      # needs to be passed through unchanged to all secondary toolchains to
      # ensure that it's always the same, regardless of the values that may be
      # set on those toolchains.
      host_toolchain = host_toolchain

      if (!defined(invoker_toolchain_args.v8_current_cpu)) {
        v8_current_cpu = invoker_toolchain_args.current_cpu
      }
    }

    # When the invoker has explicitly overridden remote_execution, otherwise default
    # to the global one. This works because the only reasonable override
    # that toolchains might supply for these values are to force-disable them.
    if (defined(toolchain_args.remote_execution)) {
      toolchain_uses_remoteexec = toolchain_args.remote_execution
    } else if (defined(remote_execution)) {
      toolchain_uses_remoteexec = remote_execution
    }

    if (is_clang && use_clang_static_analyzer &&
        (!defined(invoker.is_clang_analysis_supported) ||
         invoker.is_clang_analysis_supported)) {
      compiler_prefix = "${analyzer_wrapper} "
      asm = invoker.cc
    } else {
      if (defined(toolchain_args.cc_wrapper)) {
        toolchain_cc_wrapper = toolchain_args.cc_wrapper
      } else {
        toolchain_cc_wrapper = cc_wrapper
      }
      if (share_ccache != "") {
        compiler_prefix = "CCACHE_DIR=" + share_ccache +
                          " CCACHE_NOHASHDIR=1 ${toolchain_cc_wrapper} "
      } else {
        compiler_prefix = "${toolchain_cc_wrapper} "
      }
    }

    # For remote execution
    link_prefix = ""
    ar_prefix = ""
    if (toolchain_uses_remoteexec) {
      compiler_prefix = "${rbe_bin_dir}/rewrapper -cfg=${rbe_cfg_dir}/rewrapper_compile.cfg ${rbe_compile_missing_inputs} -exec_root=${exec_root} "
    }

    # For hitest
    if (defined(use_hitest) && use_hitest) {
      compiler_prefix = "${hitest_wrapper} "
      link_prefix = "${hitest_wrapper} "
    }
    cc = compiler_prefix + invoker.cc
    cxx = compiler_prefix + invoker.cxx
    ar = ar_prefix + invoker.ar
    ld = link_prefix + invoker.ld
    if (!defined(asm)) {
      if (!toolchain_uses_remoteexec) {
        asm = cc
      } else {
        asm = invoker.cc
      }
    }
    if (defined(invoker.readelf)) {
      readelf = invoker.readelf
    } else {
      readelf = "readelf"
    }
    if (defined(invoker.nm)) {
      nm = invoker.nm
    } else {
      nm = "nm"
    }

    if (defined(invoker.shlib_extension)) {
      default_shlib_extension = invoker.shlib_extension
    } else {
      default_shlib_extension = shlib_extension
    }

    if (defined(invoker.executable_extension)) {
      default_executable_extension = invoker.executable_extension
    } else {
      default_executable_extension = ""
    }

    if (!is_arkui_x) {
      if (defined(invoker.dylib_extension)) {
        default_dylib_extension = invoker.dylib_extension
      } else {
        default_dylib_extension = dylib_extension
      }

      if (defined(invoker.rlib_extension)) {
        default_rlib_extension = invoker.rlib_extension
      } else {
        default_rlib_extension = rlib_extension
      }
    }

    # Bring these into our scope for string interpolation with default values.
    if (defined(invoker.libs_section_prefix)) {
      libs_section_prefix = invoker.libs_section_prefix
    } else {
      libs_section_prefix = ""
    }

    if (defined(invoker.libs_section_postfix)) {
      libs_section_postfix = invoker.libs_section_postfix
    } else {
      libs_section_postfix = ""
    }

    if (defined(invoker.solink_libs_section_prefix)) {
      solink_libs_section_prefix = invoker.solink_libs_section_prefix
    } else {
      solink_libs_section_prefix = ""
    }

    if (defined(invoker.solink_libs_section_postfix)) {
      solink_libs_section_postfix = invoker.solink_libs_section_postfix
    } else {
      solink_libs_section_postfix = ""
    }

    if (defined(invoker.extra_cflags) && invoker.extra_cflags != "") {
      extra_cflags = " " + invoker.extra_cflags
    } else {
      extra_cflags = ""
    }

    if (defined(invoker.extra_cppflags) && invoker.extra_cppflags != "") {
      extra_cppflags = " " + invoker.extra_cppflags
    } else {
      extra_cppflags = ""
    }

    if (defined(invoker.extra_cxxflags) && invoker.extra_cxxflags != "") {
      extra_cxxflags = " " + invoker.extra_cxxflags
    } else {
      extra_cxxflags = ""
    }

    if (defined(invoker.extra_asmflags) && invoker.extra_asmflags != "") {
      extra_asmflags = " " + invoker.extra_asmflags
    } else {
      extra_asmflags = ""
    }

    if (defined(invoker.extra_ldflags) && invoker.extra_ldflags != "") {
      extra_ldflags = " " + invoker.extra_ldflags
    } else {
      extra_ldflags = ""
    }

    enable_linker_map = defined(invoker.enable_linker_map) &&
                        invoker.enable_linker_map && generate_linker_map

    # These library switches can apply to all tools below.
    lib_switch = "-l"
    lib_dir_switch = "-L"

    # Object files go in this directory.
    object_subdir = "{{source_out_dir}}/{{label_name}}"

    tool("cc") {
      depfile = "{{output}}.d"
      command = "$cc -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_c}}${extra_cppflags}${extra_cflags} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CC {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
    }

    tool("cxx") {
      depfile = "{{output}}.d"
      command = "$cxx -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}${extra_cppflags}${extra_cxxflags} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CXX {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
    }

    tool("asm") {
      # For GCC we can just use the C compiler to compile assembly.
      depfile = "{{output}}.d"
      command = "$asm -MMD -MF $depfile ${rebuild_string}{{defines}} {{include_dirs}} {{asmflags}}${extra_asmflags} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "ASM {{output}}"
      outputs = [ "$object_subdir/{{source_name_part}}.o" ]
    }

    tool("alink") {
      if (current_os == "aix") {
        # AIX does not support either -D (deterministic output) or response
        # files.
        command = "$ar -X64 {{arflags}} -r -c -s {{output}} {{inputs}}"
      } else {
        rspfile = "{{output}}.rsp"
        rspfile_content = "{{inputs}}"
        command = "\"$ar\" {{arflags}} -r -c -s -D {{output}} @\"$rspfile\""
      }

      # Remove the output file first so that ar doesn't try to modify the
      # existing file.
      if (host_os == "win") {
        tool_wrapper_path =
            rebase_path("//build/toolchain/win/tool_wrapper.py", root_build_dir)
        command = "cmd /c $python_path $tool_wrapper_path delete-file {{output}} && $command"
      } else {
        command = "rm -f {{output}} && $command"
      }

      # Almost all targets build with //build/config/compiler:thin_archive which
      # adds -T to arflags.
      description = "AR {{output}}"
      outputs = [ "{{output_dir}}/{{target_output_name}}{{output_extension}}" ]

      # Shared libraries go in the target out directory by default so we can
      # generate different targets with the same name and not have them collide.
      default_output_dir = "{{target_out_dir}}"
      default_output_extension = ".a"
      output_prefix = "lib"
    }

    tool("solink") {
      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
      sofile = "{{output_dir}}/$soname"  # Possibly including toolchain dir.
      rspfile = sofile + ".rsp"
      pool = "//build/toolchain:link_pool($default_toolchain)"

      is_mingw_link = false
      if (invoker_toolchain_args.current_os == "mingw") {
        is_mingw_link = true
        libname = "{{target_output_name}}.lib"
        libfile = "{{output_dir}}/$libname"
      }

      if (defined(invoker.strip)) {
        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$sofile"
      } else {
        unstripped_sofile = sofile
      }

      link_command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" @\"$rspfile\""

      if (!is_mingw_link) {
        link_command = "$link_command -Wl,-soname=\"$soname\""
      } else {
        link_command = "$link_command -Wl,--out-implib,{{root_out_dir}}/lib.unstripped/$libfile"
      }

      # Generate a map file to be used for binary size analysis.
      # Map file adds ~10% to the link time on a z620.
      map_switch = ""
      if (enable_linker_map && is_official_build) {
        map_file = "$unstripped_sofile.map.gz"
        map_switch = " --map-file \"$map_file\""
      }

      assert(defined(readelf), "to solink you must have a readelf")
      assert(defined(nm), "to solink you must have an nm")
      strip_switch = ""
      if (defined(invoker.strip)) {
        strip_switch = "--strip=${invoker.strip} "
        if (current_cpu == "arm" && is_standard_system) {
          strip_debug_whitelist =
              rebase_path("//build/toolchain/strip_debug_whitelist.txt",
                          root_build_dir)
          strip_switch += "--strip-debug-whitelist=${strip_debug_whitelist} "
        }
      }
      adlt_switch = ""
      if (enable_adlt && allowed_lib_list != "") {
        adlt_switch = "--target-name={{label_name}}" +
                      " --target-out-dir={{target_out_dir}}" +
                      " --allowed-lib-list=" + rebase_path(allowed_lib_list)
      }

      # This needs a Python script to avoid using a complex shell command
      # requiring sh control structures, pipelines, and POSIX utilities.
      # The host might not have a POSIX shell and utilities (e.g. Windows).
      solink_wrapper =
          rebase_path("//build/toolchain/gcc_solink_wrapper.py", root_build_dir)
      _clang_base_dir = rebase_path(toolchains_dir)
      command = "$python_path \"$solink_wrapper\" --readelf=\"$readelf\" --nm=\"$nm\" $strip_switch $adlt_switch --sofile=\"$unstripped_sofile\" $map_switch --output=\"$sofile\" --clang-base-dir=\"$_clang_base_dir\""
      if (is_mingw_link) {
        command = "$command --libfile=\"$libfile\""
      }
      if (full_mini_debug && !is_debug) {
        command = "$command --mini-debug"
      }
      command = "$command -- $link_command"

      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"

      description = "SOLINK $sofile"

      # Declares toolchain solink to be compiled as whole-archive by default.
      toolchain_whole_archive = true

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      default_output_extension = default_shlib_extension

      default_output_dir = "{{root_out_dir}}"

      output_prefix = "lib"

      # Since the above commands only updates the .TOC file when it changes, ask
      # Ninja to check if the timestamp actually changed to know if downstream
      # dependencies should be recompiled.
      restat = true

      # Tell GN about the output files. It will link to the sofile
      outputs = [ sofile ]
      if (sofile != unstripped_sofile) {
        outputs += [ unstripped_sofile ]
        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
            invoker.use_unstripped_as_runtime_outputs) {
          runtime_outputs = [ unstripped_sofile ]
        }
      }
      if (defined(map_file)) {
        outputs += [ map_file ]
      }

      if (is_mingw_link) {
        outputs += [ libfile ]
        link_output = libfile
        depend_output = libfile
      } else {
        link_output = sofile
        depend_output = sofile
      }
    }

    tool("solink_module") {
      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
      sofile = "{{output_dir}}/$soname"
      rspfile = sofile + ".rsp"
      pool = "//build/toolchain:link_pool($default_toolchain)"

      if (defined(invoker.strip)) {
        unstripped_sofile = "{{root_out_dir}}/lib.unstripped/$sofile"
      } else {
        unstripped_sofile = sofile
      }

      command = "$ld -shared {{ldflags}}${extra_ldflags} -o \"$unstripped_sofile\" -Wl,-soname=\"$soname\" @\"$rspfile\""

      if (defined(invoker.strip)) {
        strip_command = "${invoker.strip} -o \"$sofile\" \"$unstripped_sofile\""
        command += " && " + strip_command
      }
      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive $solink_libs_section_prefix {{libs}} $solink_libs_section_postfix"

      description = "SOLINK_MODULE $sofile"

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      if (defined(invoker.loadable_module_extension)) {
        default_output_extension = invoker.loadable_module_extension
      } else {
        default_output_extension = default_shlib_extension
      }

      default_output_dir = "{{root_out_dir}}"

      output_prefix = "lib"

      outputs = [ sofile ]
      if (sofile != unstripped_sofile) {
        outputs += [ unstripped_sofile ]
        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
            invoker.use_unstripped_as_runtime_outputs) {
          runtime_outputs = [ unstripped_sofile ]
        }
      }
    }

    tool("link") {
      exename = "{{target_output_name}}{{output_extension}}"
      outfile = "{{output_dir}}/$exename"
      rspfile = "$outfile.rsp"
      unstripped_outfile = outfile
      pool = "//build/toolchain:link_pool($default_toolchain)"

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      default_output_extension = default_executable_extension

      default_output_dir = "{{root_out_dir}}"

      if (defined(invoker.strip)) {
        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$outfile"
      }

      # Generate a map file to be used for binary size analysis.
      # Map file adds ~10% to the link time on a z620.
      map_switch = ""
      if (enable_linker_map && is_official_build) {
        map_file = "$unstripped_outfile.map.gz"
        map_switch = " --map-file \"$map_file\""
      }

      start_group_flag = ""
      end_group_flag = ""
      if (current_os != "aix") {
        # the "--start-group .. --end-group" feature isn't available on the aix ld.
        start_group_flag = "-Wl,--start-group"
        end_group_flag = "-Wl,--end-group "
      }
      _clang_rt_dso_full_path = ""
      if (is_asan && invoker_toolchain_args.current_os == "ohos") {
        if (use_hwasan) {
          _clang_rt_dso_full_path = rebase_path(
                  "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.hwasan.so",
                  root_build_dir)
        } else if (invoker_toolchain_args.current_cpu == "arm64") {
          _clang_rt_dso_full_path = rebase_path(
                  "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.asan.so",
                  root_build_dir)
        } else if (invoker_toolchain_args.current_cpu == "riscv64") {
          _clang_rt_dso_full_path = rebase_path(
                  "$clang_base_path/lib/clang/$clang_version/lib/riscv64-linux-ohos/libclang_rt.asan.so",
                  root_build_dir)
        } else if (invoker_toolchain_args.current_cpu == "loongarch64") {
          _clang_rt_dso_full_path = rebase_path(
                  "$clang_base_path/lib/clang/$clang_version/lib/loongarch64-linux-ohos/libclang_rt.asan.so",
                  root_build_dir)
        } else {
          _clang_rt_dso_full_path = rebase_path(
                  "$clang_base_path/lib/clang/$clang_version/lib/arm-linux-ohos/libclang_rt.asan.so",
                  root_build_dir)
        }
      } else if (is_tsan && target_cpu == "arm64" &&
                 invoker_toolchain_args.current_os == "ohos" && !is_win &&
                 !is_mingw) {
        _clang_rt_dso_full_path = rebase_path(
                "$clang_base_path/lib/clang/$clang_version/lib/aarch64-linux-ohos/libclang_rt.tsan.so",
                root_build_dir)
      }
      link_command = "$ld {{ldflags}}${extra_ldflags} -o \"$unstripped_outfile\" $libs_section_prefix $start_group_flag $_clang_rt_dso_full_path @\"$rspfile\" {{solibs}} {{libs}} $end_group_flag $libs_section_postfix"

      strip_switch = ""

      if (defined(invoker.strip)) {
        strip_switch = " --strip=\"${invoker.strip}\" --unstripped-file=\"$unstripped_outfile\""
      }
      if ((is_asan || is_tsan) && invoker_toolchain_args.current_os == "ohos") {
        strip_switch =
            "$strip_switch --clang_rt_dso_path=\"$_clang_rt_dso_full_path\""
      }

      link_wrapper =
          rebase_path("//build/toolchain/gcc_link_wrapper.py", root_build_dir)
      _clang_base_dir = rebase_path(toolchains_dir)
      command = "$python_path \"$link_wrapper\" --output=\"$outfile\"$strip_switch$map_switch --clang-base-dir=\"$_clang_base_dir\" "
      if (full_mini_debug && !is_debug) {
        command = "$command --mini-debug"
      }
      if (remove_unstripped_exe) {
        command = "$command --remove-unstripped-exe"
      }
      command = "$command -- $link_command"
      description = "LINK $outfile"
      rspfile_content = "{{inputs}}"
      outputs = [ outfile ]
      if (outfile != unstripped_outfile) {
        outputs += [ unstripped_outfile ]
        if (defined(invoker.use_unstripped_as_runtime_outputs) &&
            invoker.use_unstripped_as_runtime_outputs) {
          runtime_outputs = [ unstripped_outfile ]
        }
      }
      if (defined(invoker.link_outputs)) {
        outputs += invoker.link_outputs
      }
      if (defined(map_file)) {
        outputs += [ map_file ]
      }
    }

    # These two are really entirely generic, but have to be repeated in
    # each toolchain because GN doesn't allow a template to be used here.
    # See //build/toolchain/toolchain.gni for details.
    tool("stamp") {
      command = stamp_command
      description = stamp_description
    }
    tool("copy") {
      command = copy_command
      description = copy_description
    }

    if (!is_arkui_x) {
      cc_command_args = ""
      if (defined(invoker.cc_command_args)) {
        cc_command_args = invoker.cc_command_args
      }
      if (invoker_toolchain_args.current_os == "mingw") {
        rust_dynamic = ""
      } else {
        rust_dynamic = "-C prefer-dynamic"
      }
      minidebug_switch = ""
      if (full_mini_debug && !is_debug) {
        minidebug_switch = " --mini-debug"
      }
      rust_sysroot_relative_to_out = rebase_path(rust_sysroot, root_out_dir)
      rustc_wrapper =
          rebase_path("//build/toolchain/rustc_wrapper.py", root_build_dir)
      _clang_base_dir = rebase_path(toolchains_dir)
      tool("rust_staticlib") {
        staticlibname =
            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.a".
        outfile = "{{target_out_dir}}/$staticlibname"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        pool = "//build/toolchain:link_pool($default_toolchain)"

        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile -- --crate-name {{crate_name}} $rust_dynamic {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $outfile LDFLAGS RUSTENV {{rustenv}}"

        description = "RUST staticlib $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ outfile ]
        default_output_extension = ".a"
        output_prefix = "lib"
      }

      tool("rust_rlib") {
        rlibname =
            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.rlib".
        outfile = "{{output_dir}}/$rlibname"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        # Don't add rspfile in rust_rlib tool.
        pool = "//build/toolchain:link_pool($default_toolchain)"

        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile -- --crate-name {{crate_name}} $rust_dynamic {{source}} --crate-type {{crate_type}} $cc_command_args {{rustdeps}} {{externs}} --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $outfile LDFLAGS RUSTENV {{rustenv}}"
        description = "RUST rlib $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ outfile ]
        default_output_extension = default_rlib_extension
        output_prefix = "lib"
      }
      if (invoker_toolchain_args.current_os == "mingw") {
        cdylib_link_option = "--out-implib"
      } else {
        cdylib_link_option = "-soname"
      }
      tool("rust_cdylib") {
        cdylibname =
            "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.z.so".
        outfile = "{{output_dir}}/$cdylibname"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"

        pool = "//build/toolchain:link_pool($default_toolchain)"

        strip_level = "none"  # rustc supports none, debuginfo and symbols
                              # three strip degree.

        strip_switch = " -C strip=$strip_level"

        minidebug_switch = ""
        if (full_mini_debug && !is_debug) {
          minidebug_switch = " --mini-debug"
        }

        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -Clink-arg=-Wl,$cdylib_link_option=\"$cdylibname\" -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"

        description = "RUST cdylib $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ unstripped_outfile ]
        outputs += [ outfile ]
        default_output_extension = default_shlib_extension
        output_prefix = "lib"
      }

      tool("rust_bin") {
        exename = "{{target_output_name}}{{output_extension}}"
        outfile = "{{output_dir}}/$exename"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        unstripped_outfile = "{{root_out_dir}}/exe.unstripped/$outfile"

        pool = "//build/toolchain:link_pool($default_toolchain)"

        strip_level = "none"  # rustc supports none, debuginfo and symbols
                              # three strip degree.

        strip_switch = " -C strip=$strip_level"
        minidebug_switch = ""
        if (full_mini_debug && !is_debug) {
          minidebug_switch = " --mini-debug"
        }
        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS RUSTENV {{rustenv}}"

        description = "RUST bin $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ unstripped_outfile ]
        outputs += [ outfile ]
        default_output_extension = default_executable_extension
      }

      tool("rust_dylib") {
        dylibname =
            "{{target_output_name}}{{output_extension}}"  # e.g.
                                                          # "libfoo.dylib.so".

        outfile = "{{output_dir}}/$dylibname"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"

        pool = "//build/toolchain:link_pool($default_toolchain)"

        strip_level = "none"  # rustc supports none, debuginfo and symbols
                              # three strip degree.

        strip_switch = " -C strip=$strip_level"
        minidebug_switch = ""
        if (full_mini_debug && !is_debug) {
          minidebug_switch = " --mini-debug"
        }
        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"

        description = "RUST dylib $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ unstripped_outfile ]
        outputs += [ outfile ]
        default_output_extension = default_dylib_extension
        output_prefix = "lib"
      }

      tool("rust_macro") {
        dylibname =
            "{{target_output_name}}{{output_extension}}"  # e.g.
                                                          # "libfoo.dylib.so".

        outfile = "{{output_dir}}/$dylibname"
        depfile = "$outfile.d"
        rspfile = "$outfile.rsp"
        rspfile_content = "{{rustdeps}} {{externs}}"

        unstripped_outfile = "{{root_out_dir}}/lib.unstripped/$outfile"

        pool = "//build/toolchain:link_pool($default_toolchain)"

        strip_level = "none"  # rustc supports none, debuginfo and symbols
                              # three strip degree.

        strip_switch = " -C strip=$strip_level"
        minidebug_switch = ""
        if (full_mini_debug && !is_debug) {
          minidebug_switch = " --mini-debug"
        }
        command = "$python_path \"$rustc_wrapper\" --clang-base-dir=$_clang_base_dir --clippy-driver=$clippy_driver --rustc=$rustc --depfile=$depfile --rsp=$rspfile --output=$outfile --unstripped-file=$unstripped_outfile --strip=$llvm_strip $minidebug_switch -- --crate-name {{crate_name}} $rust_dynamic $strip_switch {{source}} --crate-type {{crate_type}} $cc_command_args --emit=dep-info=$depfile,link -Z dep-info-omit-d-target -Z unstable-options {{rustflags}} -o $unstripped_outfile LDFLAGS {{ldflags}} RUSTENV {{rustenv}}"

        description = "RUST proc-macro $outfile"
        rust_sysroot = rust_sysroot_relative_to_out
        outputs = [ unstripped_outfile ]
        outputs += [ outfile ]
        default_output_extension = default_dylib_extension
        output_prefix = "lib"
      }
    }

    tool("action") {
      pool = "//build/toolchain:action_pool($default_toolchain)"
    }

    forward_variables_from(invoker, [ "deps" ])
  }
}

# This is a shorthand for gcc_toolchain instances based on the Chromium-built
# version of Clang. Only the toolchain_cpu and toolchain_os variables need to
# be specified by the invoker, and optionally toolprefix if it's a
# cross-compile case. Note that for a cross-compile case this toolchain
# requires a config to pass the appropriate -target option, or else it will
# actually just be doing a native compile. The invoker can optionally override
# use_gold too.
template("clang_toolchain") {
  if (defined(invoker.toolprefix)) {
    toolprefix = invoker.toolprefix
  } else {
    toolprefix = ""
  }

  gcc_toolchain(target_name) {
    prefix = rebase_path("$clang_base_path/bin", root_build_dir)
    cc = "$prefix/clang"
    cxx = "$prefix/clang++"
    ld = cxx
    readelf = "${toolprefix}readelf"
    ar = "${prefix}/llvm-ar"
    nm = "${toolprefix}nm"

    forward_variables_from(invoker,
                           [
                             "strip",
                             "is_clang_analysis_supported",
                             "enable_linker_map",
                             "use_unstripped_as_runtime_outputs",
                             "rust_abi_target",
                           ])

    toolchain_args = {
      if (defined(invoker.toolchain_args)) {
        forward_variables_from(invoker.toolchain_args, "*")
      }
      is_clang = true
    }

    if (defined(invoker.shlib_extension) && invoker.shlib_extension != "") {
      shlib_extension = invoker.shlib_extension
    }
    if (defined(rust_abi_target)) {
      if (rust_abi_target == "x86_64-unknown-linux-gnu") {
        clang_lib_path = rebase_path("$clang_base_path/lib", root_build_dir)
        cc_command_args = "--target=${rust_abi_target} -Clinker=$clang -lstdc++ -lclang -L${clang_lib_path} -Clink-arg=-fuse-ld=lld -Clink-arg=-v"
      }
    }
  }
}