blob: 8d69c443b6b6a930b3717c99fa173c8036eb860f [file]
# Copyright 2020 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/bazel/bazel_host_tool.gni")
import("//build/bazel/bazel_inputs.gni")
import("//build/host.gni")
import("//build/rust/rustc_binary.gni")
import("//build/sdk/sdk_atom.gni")
import("//build/testing/host_test_data.gni")
import("//sdk/categories/compatibility.gni")
import("//src/developer/ffx/build/ffx_action.gni")
import("//src/developer/ffx/config.gni")
import("//src/developer/ffx/lib/version/build/ffx_apply_version.gni")
# Registers an in-tree standalone ffx subtool in the ffx subtool manifest
# (`ffx_tools.json`, see `//src/developer/ffx:tool_manifest`).
#
# Shared by `ffx_tool` and `bazel_ffx_tool`.
#
# Parameters
#
# output_name (required)
# Name of the versioned subtool binary installed in `host_tools_dir`.
#
# sdk_category (optional)
# Subtool category. Defaults to `"internal"`, which marks subtools that
# are not in the IDK (see //src/developer/ffx/tests/cli-goldens).
#
# testonly, visibility (optional)
# Usual GN meaning.
#
template("_ffx_tool_manifest_entry") {
group(target_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
_output_name = invoker.output_name
metadata = {
# LINT.IfChange(clidoc_subtool_manifest)
# this is a comment to remember to change clidoc.
# nested IfThisThenThat are not allowed.
# nor multiple files in the then clause.
# LINT.ThenChange(//tools/clidoc/src/ffx_doc.rs:clidoc_subtool_manifest)
# LINT.IfChange(subtool_manifest)
ffx_tool = [
{
name = _output_name
category = "internal"
if (defined(invoker.sdk_category)) {
category = invoker.sdk_category
}
executable =
rebase_path("$host_tools_dir/$_output_name", root_build_dir)
executable_metadata =
rebase_path("$host_tools_dir/${_output_name}.json",
root_build_dir)
},
]
# LINT.ThenChange(//src/developer/ffx/lib/fho/search/src/lib.rs:subtool_manifest)
}
}
}
# Defines an ffx-compatible subtool in the form of a rust binary target
#
# All parameters to `rustc_binary` are available, along with the additional
# parameters below.
#
# Additional Parameters
#
# sdk_target_name (required if `sdk_category` is set)
# The target name to generate for an sdk atom definition. If not
# set, no sdk target will be generated.
#
# sdk_category (optional)
# The sdk category this will be installed to.
# See `sdk_atom`'s `category` argument for more information.
#
# sdk_area (optional; only allowed if `sdk_category` is set))
# [string] The API area responsible for maintaining this library.
# See //build/sdk/sdk_atom.gni.
#
# sdk_deps (optional; only allowed if `sdk_category` is set))
# Dependencies for inclusion in the sdk. See `sdk_atom` for more
# information on this.
#
# The following targets will be produced, in addition to any from
# `rustc_binary`:
#
# `<target_name>`
# The rust binary before applying build version information to it.
# Output: `<output_name>_unversioned`
#
# `<target_name>_versioned`
# The rust binary after applying build version information to it.
# Output: `<output_name>`
#
# `<target_name>_metadata`
# The json metadata for this tool that allows ffx to discover it
# and determine compatibility information.
# Output: `<output_name>.json`
#
# `<target_name>_host_tool`
# Installation of the versioned binary and metadata as a host tool.
# Output: `host-tools/<output_name>`, `host-tools/<output_name>.json`
#
# `<target_name>_test_data`
# A `host_test_data` target that includes the files necessary to make sure
# this tool is available to tests.
#
# The following target will be produced only if `sdk_category` is set:
#
# `<sdk_target_name>`
# Include the tool in the sdk as a host tool with metadata attached.
# Output: (see `sdk_atom` for more information)
#
template("ffx_tool") {
assert(
!defined(invoker.sdk_category) || defined(invoker.sdk_target_name),
"Must specify an SDK target name when generating an SDK atom for ffx tool")
if (!defined(invoker.output_name)) {
output_name = target_name
} else {
output_name = invoker.output_name
}
# targets that are part of the 'public interface' of ffx_tool
target_versioned = "${target_name}_versioned"
target_unversioned = target_name
target_metadata = "${target_name}_metadata"
target_host_tool = "${target_name}_host_tool"
target_test_data = "${target_name}_test_data"
# targets that are part of the private interface of ffx_tool
# (prefixed with _ to differentiate).
target_versioned_bin = "_${target_name}_versioned_bin"
output_versioned = output_name
output_unversioned = "${output_name}_unversioned"
output_metadata = "${output_name}.json"
# Make the rust binary
rustc_binary(target_unversioned) {
output_name = output_unversioned
forward_variables_from(invoker,
"*",
[
"output_name",
"sdk_target_name",
"sdk_category",
"sdk_area",
"sdk_deps",
])
# As these are large libraries that are slow to compile on RBE workers,
# switch them to the larger RBE workers.
_concurrent_jobs = concurrent_jobs.rust_highmem
configs += _concurrent_jobs.configs
forward_variables_from(_concurrent_jobs.vars, "*")
}
# Replace the special linker sections containing the version information with
# the real values
ffx_apply_version(target_versioned_bin) {
output_name = output_versioned
ffx_unversioned_binary_path = "${root_out_dir}/${output_unversioned}"
runtime_files = [ "$root_out_dir/$output_metadata" ]
deps = [
":$target_metadata",
":$target_unversioned",
]
}
# internal target to create metadata about this tool for use by
# ffx when running hermetically.
tool_list_metadata_target = "${output_name}-ffx-tool-metadata"
# Create the metadata file for in-tree discovery
ffx_action(target_metadata) {
# The ffx_action() template will convert these names to the correct ones by
# adding the '_unversioned' suffix to the 'output_name'.
ffx_tool = ":$target_unversioned"
ffx_tool_output_name = output_versioned # without _unversioned suffix
inputs = []
outputs = [ "$root_out_dir/$output_metadata" ]
args = [
"metadata",
rebase_path("$root_out_dir/$output_metadata", root_build_dir),
]
}
# Combine those into a target that represents both the versioned binary
# and the metadata.
group(target_versioned) {
public_deps = [
":$target_metadata",
":$target_versioned_bin",
]
deps = [ ":${tool_list_metadata_target}" ]
}
# Install host tool
install_host_tools(target_host_tool) {
deps = [ ":$target_versioned" ]
outputs = [
output_name,
output_metadata,
]
}
# This is used to generate metadata about in-tree standalone ffx subtools.
_ffx_tool_manifest_entry(tool_list_metadata_target) {
forward_variables_from(invoker, [ "sdk_category" ])
output_name = output_name
}
# Install host tool and metadata as test data
host_test_data(target_test_data) {
forward_variables_from(invoker,
[
"assert_no_deps",
"testonly",
"visibility",
])
data_deps = [ ":$target_host_tool" ]
sources = [
"${host_tools_dir}/${output_metadata}",
"${host_tools_dir}/${output_name}",
]
}
# Expose the installed host tool and metadata JSON to Bazel host tests via
# @gn_targets//toolchain_host_<cpu>/<package>:<target_name>_test_data.
if (current_toolchain == host_toolchain &&
current_toolchain == toolchain_variant.base) {
bazel_input_file("${target_test_data}.bazel_input") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
generator = ":$target_host_tool"
outputs = [
"${host_tools_dir}/${output_name}",
"${host_tools_dir}/${output_metadata}",
]
gn_targets_name = target_test_data
}
}
if (defined(invoker.sdk_category)) {
# Add to the sdk
file_base_root = "tools/ffx_tools"
if (host_os == "linux" || host_os == "mac") {
file_base_root = "tools/$current_cpu/ffx_tools"
}
file_base = "$file_base_root/$output_name"
file_base_metadata = "$file_base_root/$output_metadata"
# for the moment this goes directly to sdk_atom because this is a 'weird'
# host tool that includes a json metadata file.
sdk_atom(invoker.sdk_target_name) {
forward_variables_from(invoker,
[
"assert_no_deps",
"sdk_area",
])
category = invoker.sdk_category
id = "sdk://$file_base"
_target_files = {
executable = file_base
executable_metadata = file_base_metadata
}
meta = {
dest = "$file_base-meta.json"
type = "ffx_tool"
value = {
type = "ffx_tool"
name = output_name
root = "tools"
files = {
}
target_files = {
}
if (current_cpu == "arm64") {
target_files.arm64 = _target_files
} else if (current_cpu == "x64") {
target_files.x64 = _target_files
} else if (current_cpu == "riscv64") {
target_files.riscv64 = _target_files
} else {
assert(false, "Unknown CPU type: $current_cpu")
}
}
}
files = [
{
source = "$root_out_dir/$output_versioned"
dest = file_base
},
{
source = "$root_out_dir/$output_metadata"
dest = file_base_metadata
},
]
if (defined(invoker.sdk_deps)) {
deps = invoker.sdk_deps
}
non_sdk_deps = [
":$target_metadata",
":$target_versioned",
]
# Ensure all FIDL dependencies meet the compatibility and stability
# requirements.
# There are currently exceptions - see https://fxbug.dev/42081073.
if (!defined(assert_no_deps)) {
assert_no_deps = []
}
assert(invoker.sdk_category == "partner",
"Unexpected SDK category '${invoker.sdk_category}'.")
# Host tools in the IDK can depend on SDK categories up to "host_tool".
assert_no_deps += markers_partner_idk_host_tools_must_not_depend_on
}
}
}
# Bridges a Bazel-defined `ffx_tool` or `idk_ffx_tool` target into the GN
# build graph as a host tool and registers it in the ffx subtool manifest.
#
# The following targets will be produced:
#
# `<target_name>`
# The unversioned binary (`<output_name>_unversioned`), for use by
# `ffx_action()`.
#
# `<target_name>_versioned`
# The versioned binary (`<output_name>`) and its metadata
# (`<output_name>.json`), also installed in `host_tools_dir`.
#
# `<target_name>_host_tool`
# Alias of `<target_name>_versioned`, for consistency with `ffx_tool`.
#
# Unlike `ffx_tool`, no `_test_data` target is produced: Bazel `ffx_tool`
# already provides `<name>_test_data` for tests.
#
# Parameters
#
# bazel_target (optional)
# The base Bazel `ffx_tool` target name (without `_versioned`).
# Defaults to `":${target_name}"`.
#
# output_name (optional)
# Name of the output binary. Defaults to `target_name`.
#
# sdk_category (optional)
# Subtool category recorded in `ffx_tools.json`. Defaults to `"internal"`.
#
# deps, testonly, visibility (optional)
# Usual GN meaning.
#
template("bazel_ffx_tool") {
output_name = target_name
if (defined(invoker.output_name)) {
output_name = invoker.output_name
}
tool_list_metadata_target = "${output_name}-ffx-tool-metadata"
_bazel_target = ":${target_name}"
if (defined(invoker.bazel_target)) {
_bazel_target = invoker.bazel_target
}
_forwarded_args = [
"testonly",
"visibility",
]
bazel_host_tool(target_name) {
forward_variables_from(invoker, _forwarded_args + [ "deps" ])
bazel_target = _bazel_target
bazel_output_path = "{{BAZEL_TARGET_OUT_DIR}}/${output_name}_unversioned"
output_name = "${output_name}_unversioned"
}
bazel_host_tool("${target_name}_versioned") {
forward_variables_from(invoker, _forwarded_args)
bazel_target = "${_bazel_target}_versioned"
bazel_output_path = "{{BAZEL_TARGET_OUT_DIR}}/${output_name}"
output_name = output_name
companion_files = [ "${output_name}.json" ]
install_host_tool = true
deps = [ ":${tool_list_metadata_target}" ]
if (defined(invoker.deps)) {
deps += invoker.deps
}
}
# `<target_name>_versioned` already installs the tool in `host_tools_dir`.
_versioned_target = ":${target_name}_versioned"
group("${target_name}_host_tool") {
forward_variables_from(invoker, _forwarded_args)
public_deps = [ _versioned_target ]
}
_ffx_tool_manifest_entry(tool_list_metadata_target) {
forward_variables_from(invoker, _forwarded_args + [ "sdk_category" ])
output_name = output_name
}
}