blob: 58af1bb9518ed9356011bc15939d3930f59d8b07 [file] [log] [blame] [edit]
# Copyright 2022 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/rust/clippy.gni")
import("//build/toolchain/rbe.gni")
# Common logic for implementing the other rustc_* templates in this directory.
# Should not be called directly outside of //build/rust.
#
# Parameters
#
# target_type
# Name of the template or rule to wrap, as a string.
#
# crate_name
# Name of the crate as passed to rustc. All dashes will be replaced
# with underscores in the crate name: <name_underscored>
#
# crate_root
# Location of the crate root (e.g. `src/main.rs` or `src/lib.rs`).
#
# edition (optional)
# Edition of the Rust language to be used.
# Options are "2015" and "2018". Defaults to "2018".
#
# rustflags
# Extra rust compiler flags passed directly to rustc.
#
# features (optional)
# A list of conditional compilation flags to enable. This can be used to set features for crates
# built in-tree which are also published to crates.io. This would be passed to rustc as
# '--cfg feature="XXX"'
#
# configs (optional)
# A list of config labels applying to this target.
#
# deps
# List of GN targets on which this crate depends.
#
# pass_through
# A scope of arguments to pass directly to the underlying wrapped target.
# Only the primary target generated will receive these arguments.
#
# metadata (optional)
# Metadata to apply to the primary wrapped target.
#
# inputs (optional)
# List of additional non-source files read by the compiler. These are typically
# configuration or test-data files included in the build with the `include_str!`
# macro.
#
# sources
# List of source files which this crate is allowed to compile.
# The Rust compiler discovers source files by following `mod` declarations
# starting at the `crate_root`. The discovered source files must match this
# list.
#
# disable_rbe (optional)
# Set to true to force this target to build locally, overriding the global `enable_rbe`.
# disable_clippy (optional)
# Don't run clippy on this target.
#
# clippy_crate_type
# Usually GN handles this internally for rust targets, but we have to set it explicitly
# for targets that generate a corresponding clippy target so that clippy has access to it.
# See https://doc.rust-lang.org/reference/linkage.html for possible values
#
template("rustc_artifact") {
# rustc does not support dashes in crate names
_crate_name = string_replace(invoker.crate_name, "-", "_")
_crate_root = invoker.crate_root
_remote_inputs = []
if (defined(invoker.inputs)) {
_remote_inputs = invoker.inputs
}
_local_inputs = _remote_inputs
# enable these features for the target
_rustflags = []
if (defined(invoker.features)) {
foreach(i, invoker.features) {
_rustflags += [ "--cfg=feature=\"${i}\"" ]
}
}
if (defined(invoker.rustflags)) {
_rustflags += invoker.rustflags
}
_disable_clippy = defined(invoker.disable_clippy) && invoker.disable_clippy
if (!_disable_clippy) {
clippy("$target_name.clippy") {
forward_variables_from(invoker,
[
"edition",
"configs",
])
rustflags = _rustflags
sources = invoker.sources
deps = invoker.deps
crate_root = _crate_root
clippy_crate_type = invoker.clippy_crate_type
}
} else {
if (defined(invoker.clippy_crate_type)) {
not_needed(invoker, [ "clippy_crate_type" ])
}
}
_use_rbe = enable_rbe
if (defined(invoker.disable_rbe) && invoker.disable_rbe) {
_use_rbe = false
}
if (enable_rbe && !_use_rbe) {
# Disable RBE for this target through a fake rustflag,
# that is intercepted by rustc-remote-wrapper.sh.
_rustflags += [ "--remote-disable" ]
}
if (_use_rbe) {
# Depend on Rust/RBE scripts and tools
_local_inputs += rust_rbe_deps
}
target(invoker.target_type, target_name) {
crate_root = _crate_root
crate_name = _crate_name
rustflags = []
if (enable_rbe) {
rustflags +=
[ "--remote-flag=--label='" +
get_label_info(":$target_name", "label_with_toolchain") + "'" ]
}
if (_use_rbe && _remote_inputs != []) {
# Signal to rustc-remote-wrapper.sh that there are additional
# inputs to upload. This flag is stripped away from the eventual
# rustc command.
rustflags += [
"--remote-inputs",
string_join(",", rebase_path(_remote_inputs, root_build_dir)),
]
}
rustflags += _rustflags
configs = []
configs = invoker.configs
deps = invoker.deps
inputs = _local_inputs
sources = invoker.sources
# in-tree default is 2018 edition
if (defined(invoker.edition)) {
assert(invoker.edition == "2015" || invoker.edition == "2018",
"Only editions 2015 and 2018 are supported")
if (invoker.edition == "2015") {
configs -= [ "//build/config/rust:edition_2018" ]
configs += [ "//build/config/rust:edition_2015" ]
}
}
if (!_disable_clippy) {
gen_deps = [ ":$target_name.clippy" ]
}
# _pass_through and not_needed are to workaround https://crbug.com/gn/10
_pass_through = invoker.pass_through
assert(!defined(_pass_through.metadata))
not_needed([ "_pass_through" ])
metadata = {
if (defined(invoker.metadata)) {
forward_variables_from(invoker.metadata, "*")
}
if (!_disable_clippy) {
_outputs = get_target_outputs(":$target_name.clippy")
rust_source_map = [
{
clippy =
get_label_info(":$target_name.clippy", "label_with_toolchain")
output = rebase_path(_outputs[0], root_build_dir)
src = []
foreach(s, sources) {
src += [ rebase_path(s, root_build_dir) ]
}
},
]
}
}
# pass through these variables unmodified
forward_variables_from(invoker.pass_through, "*")
}
}