blob: cb1e61ddd2ce445d9faf72842056ccce2b3ad7bc [file] [log] [blame] [edit]
# Copyright 2024 The Fuchsia 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/compiler.gni")
import("//build/config/fuchsia/platform_version.gni")
import("//build/config/jobserver.gni")
import("//build/sdk/config.gni")
import("//build/testing/config.gni")
import("//build/toolchain/resultstore.gni")
import("//build/tracer/tracer.gni")
import("//sdk/config.gni")
# Create a GN directory, and build a target label for a particular
# target_cpu and api_level by calling `gn gen` and then `ninja` in that
# directory.
#
# Parameters
#
# target (required)
# [label] Label to build within the subbuild.
#
# target_cpu (required)
# [string] Name of the CPU to build for.
#
# outputs (required)
# [string list] List of outputs generated by the subbuild. This
# is relative to the subbuild output directory.
#
# api_level (required)
# [string] API level to build for. Can be a specific level like "21" or
# the string "NEXT".
#
# extra_deps (required)
# [label list] List of labels to build before starting the subbuild.
#
# subbuild_dir_name (required)
# [string] The name of the build directory to create under the out directory.
#
# use_idk_tools_only (optional)
# [boolean] If set, depend only on the IDK build tools. This option can reduce the latency
# of starting IDK builds since they do not need to wait for all tools to build first, but
# other subbuilds may want to keep this unset so they can access all host tools during their
# build step.
# TODO(https://fxbug.dev/358439250): Create smaller-scoped sets for non-IDK builds.
#
# api_level_path (optional)
# [string] If set, output the API level to this file relative to the subbuild directory.
#
template("subbuild") {
assert(defined(invoker.target))
assert(defined(invoker.target_cpu))
assert(defined(invoker.outputs))
assert(defined(invoker.api_level))
assert(defined(invoker.extra_deps))
assert(defined(invoker.subbuild_dir_name))
_build_dir = root_build_dir + "/${invoker.subbuild_dir_name}"
# LINT.IfChange
_build_dir_stamp = _build_dir + ".stamp"
# LINT.ThenChange(//build/scripts/ninjatrace2json.py)
_target_dir = get_label_info(invoker.target, "dir")
_target_name = get_label_info(invoker.target, "name")
_target = "${_target_dir}:${_target_name}"
use_idk_tools_only =
defined(invoker.use_idk_tools_only) && invoker.use_idk_tools_only
action(target_name) {
script = "//build/subbuild.py"
outputs = [ _build_dir_stamp ]
mnemonic = "NINJA"
foreach(output, invoker.outputs) {
outputs += [ _build_dir + output ]
}
if (use_idk_tools_only) {
# //sdk:idk_build_tools contains the host tools that must be used in the
# subbuilds. Make sure those (and any extra deps) are built first.
deps = [ "//sdk:idk_build_tools" ]
tools_dir = rebase_path("$root_build_dir/sdk/idk_build_tools", _build_dir)
} else {
deps = [ "//bundles/tools" ]
tools_dir = rebase_path("$root_build_dir", _build_dir)
}
deps += invoker.extra_deps
args = [
"--stamp-file",
rebase_path(_build_dir_stamp, root_build_dir),
"--sdk-id",
sdk_id,
"--output-build-dir",
rebase_path(_build_dir, root_build_dir),
"--target-label",
_target,
"--target-cpu",
invoker.target_cpu,
"--api-level",
invoker.api_level,
"--fuchsia-dir",
rebase_path("//", root_build_dir),
"--prebuilt-host-tools-dir",
tools_dir,
"--compress-debuginfo",
compress_debuginfo,
"--compilation-mode",
compilation_mode,
]
if (sdk_sub_build_parallelism != "") {
args += [
"--parallelism",
"${sdk_sub_build_parallelism}",
]
}
if (sdk_sub_build_max_load_average != "") {
args += [
"--max-load-average",
"${sdk_sub_build_max_load_average}",
]
}
if (sdk_sub_build_verbose) {
args += [ "--verbose" ]
}
if (enable_jobserver) {
# Ensure the sub-builds use the jobserver pool setup by
# the top-level Ninja process.
args += [ "--use-jobserver" ]
}
if (cxx_rbe_enable) {
args += [
"--cxx-rbe-enable",
"--cxx-exec-strategy",
cxx_rbe_exec_strategy,
]
}
if (link_rbe_enable) {
args += [
"--link-rbe-enable",
"--link-exec-strategy",
link_rbe_exec_strategy,
]
}
if (rust_rbe_enable) {
args += [
"--rust-rbe-enable",
"--rust-exec-strategy",
rust_rbe_exec_strategy,
]
}
if (ninja_upload_build_events != "") {
args += [ "--upload-build-events=" + ninja_upload_build_events ]
}
if (defined(invoker.api_level_path)) {
args += [
"--api-level-path",
rebase_path(_build_dir, root_build_dir) + invoker.api_level_path,
]
}
if (update_goldens) {
args += [ "--update-goldens" ]
}
if (build_should_trace_actions) {
args += [ "--trace-build-actions" ]
}
# If jobserver mode is not enabled, limit the number of concurrent
# sub-builds that can run at the same time to avoid overloading the
# system with two many overall parallel tasks.
if (!enable_jobserver) {
pool = "//sdk:subbuild_pool"
}
# This script cannot be hermetic.
hermetic_deps = false
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
metadata = {
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
ninja_subbuilds = [
{
build_dir = rebase_path(_build_dir, root_build_dir)
},
]
}
}
}