chromium/build/toolchain/cros/BUILD.gn

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

import("//build/config/compiler/compiler.gni")
import("//build/config/sysroot.gni")
import("//build/toolchain/cros/cros_config.gni")
import("//build/toolchain/cros_toolchain.gni")
import("//build/toolchain/gcc_toolchain.gni")

# This is mostly identical to gcc_toolchain, but handles relativizing toolchain
# paths. This is needed for CrOS since these paths often change based on the
# environment. For example, cxx is a relative path picked up on $PATH in the
# chroot. But in Simple Chrome, cxx is a system-absolute path.
template("cros_toolchain") {
  if (lacros_use_chromium_toolchain) {
    clang_toolchain(target_name) {
      forward_variables_from(invoker, "*")
    }
  } else {
    gcc_toolchain(target_name) {
      forward_variables_from(invoker, "*")

      toolchain_args.cc_wrapper = ""
      toolchain_args.clang_use_chrome_plugins = false

      # CrOS's target toolchain wrapper prefers to invoke rewrapper itself, so
      # pass it the rewrapper path via cmd-line arg. Otherwise, for both CrOS's
      # host wrapper (used in the ebuild) and Chrome's clang (used in Simple
      # Chrome), prepend rewrapper like normal.
      if (use_reclient && invoker.needs_rewrapper_path_arg) {
        extra_cppflags += "--rewrapper-path $reclient_cros_cc_wrapper --rewrapper-cfg ${reclient_cc_cfg_file}"
      }

      # Relativize path if compiler is specified such that not to lookup from
      # $PATH and cc/cxx does not contain additional flags.
      if (cc != get_path_info(cc, "file") &&
          string_replace(cc, " ", "") == cc) {
        cc = rebase_path(cc, root_build_dir)
      }
      if (cxx != get_path_info(cxx, "file") &&
          string_replace(cxx, " ", "") == cxx) {
        cxx = rebase_path(cxx, root_build_dir)
      }
      if (ar != get_path_info(ar, "file") &&
          string_replace(ar, " ", "") == ar) {
        ar = rebase_path(ar, root_build_dir)
      }
      if (ld != get_path_info(ld, "file") &&
          string_replace(ld, " ", "") == ld) {
        ld = rebase_path(ld, root_build_dir)
      }
    }
  }
}

# This is the normal toolchain for most targets.
cros_toolchain("target") {
  toolchain_args = {
    current_cpu = target_cpu
    current_os = "chromeos"
    sysroot = target_sysroot
  }

  if (!lacros_use_chromium_toolchain) {
    ar = cros_target_ar
    cc = cros_target_cc
    cxx = cros_target_cxx
    ld = cros_target_ld

    if (cros_target_nm != "") {
      nm = cros_target_nm
    }
    if (cros_target_readelf != "") {
      readelf = cros_target_readelf
    }
    extra_cflags = cros_target_extra_cflags
    extra_cppflags = cros_target_extra_cppflags
    extra_cxxflags = cros_target_extra_cxxflags
    extra_ldflags = cros_target_extra_ldflags

    needs_rewrapper_path_arg = true
  }
}

# This is a special toolchain needed just for the nacl_bootstrap target in
# //native_client/src/trusted/service_runtime/linux. It is identical
# to ":target" except that it forces `use_debug_fission1 and `use_sysroot` off,
# and allows the user to set different sets of extra flags.
cros_toolchain("nacl_bootstrap") {
  toolchain_args = {
    if (target_cpu == "arm64") {
      current_cpu = "arm"
    } else {
      current_cpu = target_cpu
    }
    current_os = "chromeos"
    use_debug_fission = false
    use_sysroot = false
  }

  if (!lacros_use_chromium_toolchain) {
    ar = cros_target_ar
    cc = cros_target_cc
    cxx = cros_target_cxx
    ld = cros_target_ld

    if (cros_target_nm != "") {
      nm = cros_target_nm
    }
    if (cros_target_readelf != "") {
      readelf = cros_target_readelf
    }
    extra_cflags = cros_nacl_bootstrap_extra_cflags
    extra_cppflags = cros_nacl_bootstrap_extra_cppflags
    extra_cxxflags = cros_nacl_bootstrap_extra_cxxflags
    extra_ldflags = cros_nacl_bootstrap_extra_ldflags

    needs_rewrapper_path_arg = true
  }

  # We build for ARM32, even when the rest of the build targets ARM64.
  if (target_cpu == "arm64") {
    ar = cros_nacl_helper_arm32_ar
    cc = cros_nacl_helper_arm32_cc
    cxx = cros_nacl_helper_arm32_cxx
    ld = cros_nacl_helper_arm32_ld

    # Avoid accidental use of Arm64 sysroot because of SYSROOT
    # env variable set in ChromeOS builds.
    toolchain_args.sysroot = cros_nacl_helper_arm32_sysroot
  }
}

# This is a special toolchain needed just for the nacl_helper target for
# building an Arm32 nacl_helper binary on Arm64 ChromeOS targets.
cros_toolchain("nacl_helper_arm32") {
  toolchain_args = {
    current_cpu = "arm"
    current_os = "chromeos"
    use_debug_fission = false
    sysroot = cros_nacl_helper_arm32_sysroot

    # Disable some uses of libraries that this build does not require. The
    # sysroot for this build does not provide them, and they would be pulled in
    # by indirect dependencies of nacl_helper otherwise.
    use_cras = false
    use_nss_certs = false
    use_system_libdrm = false
    use_system_libsync = false
  }
  ar = cros_nacl_helper_arm32_ar
  cc = cros_nacl_helper_arm32_cc
  cxx = cros_nacl_helper_arm32_cxx
  ld = cros_nacl_helper_arm32_ld
  readelf = cros_nacl_helper_arm32_readelf

  extra_cflags = ""
  extra_cppflags = ""
  extra_cxxflags = ""
  extra_ldflags = ""

  if (!lacros_use_chromium_toolchain) {
    needs_rewrapper_path_arg = true
  }
}

cros_toolchain("host") {
  toolchain_args = {
    current_cpu = host_cpu
    current_os = "linux"
    sysroot = cros_host_sysroot
  }

  if (!lacros_use_chromium_toolchain) {
    # These are args for the template.
    ar = cros_host_ar
    cc = cros_host_cc
    cxx = cros_host_cxx
    ld = cros_host_ld

    if (cros_host_nm != "") {
      nm = cros_host_nm
    }
    if (cros_host_readelf != "") {
      readelf = cros_host_readelf
    }
    extra_cflags = cros_host_extra_cflags
    extra_cppflags = cros_host_extra_cppflags
    extra_cxxflags = cros_host_extra_cxxflags
    extra_ldflags = cros_host_extra_ldflags

    needs_rewrapper_path_arg = false
  }
}

cros_toolchain("v8_snapshot") {
  toolchain_args = {
    if (target_cpu == "x86" || target_cpu == "arm" || target_cpu == "mipsel") {
      current_cpu = "x86"
    } else {
      current_cpu = "x64"
    }
    v8_current_cpu = v8_target_cpu
    current_os = "linux"
    sysroot = cros_v8_snapshot_sysroot
  }

  if (!lacros_use_chromium_toolchain) {
    # These are args for the template.
    ar = cros_v8_snapshot_ar
    cc = cros_v8_snapshot_cc
    cxx = cros_v8_snapshot_cxx
    ld = cros_v8_snapshot_ld

    if (cros_v8_snapshot_nm != "") {
      nm = cros_v8_snapshot_nm
    }
    if (cros_v8_snapshot_readelf != "") {
      readelf = cros_v8_snapshot_readelf
    }
    extra_cflags = cros_v8_snapshot_extra_cflags
    extra_cppflags = cros_v8_snapshot_extra_cppflags
    extra_cxxflags = cros_v8_snapshot_extra_cxxflags
    extra_ldflags = cros_v8_snapshot_extra_ldflags

    needs_rewrapper_path_arg = false
  }
}

# This toolchain is used when we want to build Lacros using alternate toolchain.
# To use this, you need to set gn arg 'also_build_lacros_chrome_for_architecture'.
# See build/config/chromeos/ui_mode.gni
if (also_build_lacros_chrome_for_architecture != "") {
  cros_toolchain("lacros_clang") {
    if (also_build_lacros_chrome_for_architecture == "amd64") {
      lacros_args =
          read_file("//build/args/chromeos/amd64-generic-crostoolchain.gni",
                    "scope")
    } else if (also_build_lacros_chrome_for_architecture == "arm") {
      lacros_args =
          read_file("//build/args/chromeos/arm-generic-crostoolchain.gni",
                    "scope")
    } else if (also_build_lacros_chrome_for_architecture == "arm64") {
      lacros_args =
          read_file("//build/args/chromeos/arm64-generic-crostoolchain.gni",
                    "scope")
    } else {
      assert(false,
             "also_build_lacros_chrome_for_architecture is not " +
                 "one of the supported architectures.")
    }

    toolchain_args = {
      forward_variables_from(lacros_args, "*")

      # TODO(crbug.com/40823173) Change to a better way to set gn args.
      # The following gn args are present in ash config like
      # //build/args/chromeos/atlas.gni but not in
      # //build/args/chromeos/amd64-generic-crostoolchain.gni.
      # So we need to reset them to the default value where Lacros needs.
      # Starts from here.
      ozone_auto_platforms = true
      ozone_platform = ""
      ozone_platform_gbm = -1
      ozone_platform_headless = false

      # Ends here.

      current_os = "chromeos"
      target_os = "chromeos"
      current_cpu = current_cpu
      also_build_lacros_chrome_for_architecture = ""
      chromeos_is_browser_only = true
      use_clang_coverage = false

      # Do not propagate AFDO settings from Ash build to Lacros build.
      # Specifically, Lacros uses PGO by default, so this would
      # conflict.
      clang_use_default_sample_profile = false
      clang_sample_profile_path = ""
    }
    if (!lacros_use_chromium_toolchain) {
      # These are args for the template.
      ar = lacros_args.cros_target_ar
      cc = lacros_args.cros_target_cc
      cxx = lacros_args.cros_target_cxx
      ld = lacros_args.cros_target_ld

      if (defined(lacros_args.cros_target_nm) &&
          lacros_args.cros_target_nm != "") {
        nm = lacros_args.cros_target_nm
      }
      if (defined(lacros_args.cros_target_readelf) &&
          lacros_args.cros_target_readelf != "") {
        readelf = lacros_args.cros_target_readelf
      }
      extra_cflags = lacros_args.cros_target_extra_cflags
      extra_cppflags = lacros_args.cros_target_extra_cppflags
      extra_cxxflags = lacros_args.cros_target_extra_cxxflags
      extra_ldflags = lacros_args.cros_target_extra_ldflags

      needs_rewrapper_path_arg = true
    }
  }
}