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

# Defines the configuration of Remote Build Execution (RBE).

import("//build/toolchain/remoteexec_defaults.gni")
import("//build/toolchain/siso.gni")

declare_args() {
  # Deprecated: Please use reclient_bin_dir instead.
  rbe_bin_dir = ""

  # Deprecated: Please use reclient_cfg_dir instead.
  rbe_cfg_dir = ""

  # Deprecated: Please use reclient_cros_cc_wrapper instead.
  rbe_cros_cc_wrapper = ""

  # Execution root - this should be the root of the source tree.
  # This is defined here instead of in the config file because
  # this will vary depending on where the user has placed the
  # chromium source on their system.
  rbe_exec_root = rebase_path("//")

  # Set to true to enable remote executions.
  use_remoteexec = false
}

use_reclient_default = false
if (use_remoteexec) {
  if (use_siso) {
    use_reclient_default = use_reclient_on_siso
  } else {
    use_reclient_default = use_reclient_on_ninja
  }
}

declare_args() {
  # Set to true to use re-client.
  # Set to false to use Siso's builtin RBE client.
  use_reclient = use_reclient_default

  # The directory where the re-client tooling binaries are.
  if (rbe_bin_dir != "") {
    if (current_toolchain == default_toolchain) {
      print(
          "WARNING: rbe_bin_dir is deprecated. Please use reclient_bin_dir instead.")
    }
    reclient_bin_dir = rbe_bin_dir
  } else {
    reclient_bin_dir = rebase_path("//buildtools/reclient", root_build_dir)
  }

  # The directory where the re-client configuration files are.
  if (rbe_cfg_dir != "") {
    if (current_toolchain == default_toolchain) {
      print(
          "WARNING: rbe_cfg_dir is deprecated. Please use reclient_cfg_dir instead.")
    }
    reclient_cfg_dir = rbe_cfg_dir
  } else {
    reclient_cfg_dir = "//buildtools/reclient_cfgs"
  }
}

# Check if any unsupported combination is specified.
if (use_reclient) {
  assert(use_remoteexec, "Can't enable Reclient when use_remoteexec is false")
}

if (use_remoteexec) {
  rbe_logs = [ "use_remoteexec=true" ]
} else {
  rbe_logs = [ "use_remoteexec=false" ]
}

if (use_reclient) {
  rbe_logs += [ "use_reclient=true" ]
} else {
  rbe_logs += [ "use_reclient=false" ]
}

# Siso native also uses rewrapper cfg file
# to get remote platform property.
use_reclient_cfgs =
    use_reclient || (use_remoteexec && (host_os == "win" || host_os == "mac"))

declare_args() {
  # Set to the path of the RBE reclient configuration files.
  # Configuration file selection based on operating system.
  if (!use_reclient_cfgs) {
    reclient_cc_cfg_file = ""
    reclient_py_cfg_file = ""
  } else if (use_reclient && host_os == "linux") {
    reclient_py_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/python/rewrapper_linux.cfg"
    reclient_cc_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/chromium-browser-clang/rewrapper_linux.cfg"
  } else if (host_os == "win") {
    reclient_py_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/python/rewrapper_windows.cfg"
    reclient_cc_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/chromium-browser-clang/rewrapper_windows.cfg"
  } else if (host_os == "mac") {
    reclient_py_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/python/rewrapper_mac.cfg"
    reclient_cc_cfg_file = rebase_path(reclient_cfg_dir, root_build_dir) +
                           "/chromium-browser-clang/rewrapper_mac.cfg"
  } else {
    reclient_cc_cfg_file = ""
    reclient_py_cfg_file = ""
  }

  if (use_reclient) {
    # TODO: crbug.com/342270134 - Rename reclient_cros_cc_wrapper to reclient_cros_cc_wrapper.
    # Note that reclient_cros_cc_wrapper is referenced by CrOS's chromite.
    # Set to the path of the RBE recleint wrapper for ChromeOS.
    if (rbe_cros_cc_wrapper != "") {
      if (current_toolchain == default_toolchain) {
        print(
            "WARNING: rbe_cros_cc_wrapper is deprecated. Please use reclient_cros_cc_wrapper instead.")
      }
      reclient_cros_cc_wrapper = rbe_cros_cc_wrapper
    } else {
      reclient_cros_cc_wrapper = "${reclient_bin_dir}/rewrapper"
    }
  } else {
    reclient_cros_cc_wrapper = ""
  }
}

if (use_reclient_cfgs && current_toolchain == default_toolchain) {
  # Check existence of reclient configs and show user friendly error message if
  # it doesn't.
  if (!path_exists(reclient_cc_cfg_file)) {
    # Use exec_script() to show a good error message.
    exec_script(rebase_path("//build/toolchain/check_rewrapper_cfg.py"),
                [ reclient_cc_cfg_file ],
                "",
                [ rebase_path(reclient_cc_cfg_file, ".", root_build_dir) ])
  }
}
