blob: 9df3a6b46c4689abb400fa0dd245ddea3b38a107 [file] [log] [blame]
# Copyright 2024 The Fuchsia Authors.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/rust/rustc_library.gni")
# Defines a `rustc_library` target for each specified feature set.
#
# Parameters
#
# feature_sets (required)
# List of scopes defining distinct variants of this crate.
# Type: list(scope)
# Example: [
# {
# features = []
# },
# {
# target_name = "crate-instrumented"
# features = [ "instrumented" ]
# deps += [ ":dep-instrumented" ]
# },
# ]
#
# A list of scopes, each of which defines a named "feature set": a variant
# of this `rustc_library` with a particular set of features enabled (could
# be none), along with any other properties you might set on a
# `rustc_library` (for example, `deps`): all variables in a given scope will
# be forwarded to the generated `rustc_library` corresponding to that
# feature set. Note that the scopes inherit from their parent scope, but
# variables set in the parent scope can be overridden/modified in the
# feature set scope. This allows callers to add/remove deps relative to the
# deps declared in the parent scope, for example.
#
# A feature set that does not specify a `target_name`, or specifies an empty
# `target_name`, will correspond to a generated `rustc_library` with a
# target name that is the same as this target's name. Any other feature set
# will use the `target_name` specified in the feature set as the generated
# target's name. Note, though, that the name of the crate (not the GN
# target) will be the same for all generated crates.
#
# All other parameters will be forwarded to the generated rustc_library
# targets.
template("rustc_library_with_features") {
assert(defined(invoker.feature_sets), "Must define supported feature sets.")
foreach(feature_set, invoker.feature_sets) {
if (defined(invoker.name)) {
_library_name = invoker.name
} else {
_library_name = target_name
}
_target_name = target_name
if (defined(feature_set.target_name) && feature_set.target_name != "") {
_target_name = feature_set.target_name
}
rustc_library(_target_name) {
# Use the same crate name for each declared variant of the crate to make
# it easier for dependencies to use the crate with whatever combination of
# features enabled that they require.
name = _library_name
if (defined(feature_set.target_name) && feature_set.target_name != "") {
output_dir = target_out_dir + "/${feature_set.target_name}"
} else {
# Disable rustdoc generation for non-default feature sets in order not
# to generate duplicate rustdocs for each variant of the crate.
disable_rustdoc = true
}
# Note that callers of this template can specify variables either in the
# invoker's scope or the scope of a particular feature set; the feature
# set scopes inherit from the outer scope, but any changes in the scope of
# a feature set take final precedence for that crate.
forward_variables_from(invoker, "*", [ "name" ])
forward_variables_from(feature_set,
"*",
[
"name",
"target_name",
])
}
}
}