blob: f393b19118f26bf32887aa525a44bb9f09b28d9c [file] [log] [blame]
# 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/rust/rustc_artifact.gni")
import("//build/rust/rustc_test.gni")
# Defines a Rust cdylib library
#
# Parameters
#
# output_name (optional)
# Name of the crate as defined in its manifest file. If not specified, it is
# assumed to be the same as the target name.
#
# version
# Semver version of the crate as seen on crates.io.
#
# edition
# Edition of the Rust language to be used. See
# https://doc.rust-lang.org/edition-guide/editions/index.html for more info on rust editions.
#
# configs (optional)
# A list of config labels applying to this target.
#
# deps (optional)
# List of rust_library GN targets on which this crate depends.
# Third party crates can be included through paths like
# "//third_party/rust_crates:<cratename>",
#
# enforce_source_listing (optional)
# When true, enforces that any source files used by the Rust compiler are
# listed in `sources`. Defaults to true.
#
# sources (optional)
# List of source files which this crate is allowed to compile. Only
# allowed when `enforce_source_listing = true`.
# The Rust compiler discovers source files by following `mod` declarations
# starting at the `source_root`. The discovered source files must match this
# list.
#
# test_deps (optional)
# List of rust_library GN targets on which this crate's tests depend.
#
# non_rust_deps (optional)
# List of non-rust_library GN targets on which this crate depends.
# Obsolete. Please use deps instead.
#
# data_deps (optional)
# List of GN targets that are only needed at runtime.
#
# with_unit_tests (optional)
# Builds unit tests associated with the binary. This will create a
# `<name>-bin-unit-test` test file in the output directory.
#
# source_root (optional)
# Location of the crate root (e.g. `src/main.rs` or `src/lib.rs`).
# This defaults to `./src/main.rs` for binaries and `./src/lib.rs` for libraries,
# and should only be changed when absolutely necessary
# (such as in the case of generated code).
#
# output_name (optional)
# Name of the output file.
#
# 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"'
#
# rustenv (optional)
# A list of environment variables that will be set when running the rust
# compiler. These can be accessed at compile time with
# [`std::env!`](https://doc.rust-lang.org/stable/std/macro.env.html)
#
# output_dir (optional)
# Directory that the resulting library should be placed in.
# See: `gn help output_dir`
#
# disable_rbe (optional)
# Set to true to force this target to build locally, overriding the global
# `rust_rbe_enable`.
#
# disable_clippy (optional)
# Don't run clippy on this target.
#
# original_target_name (optional)
# The name of the target as it appears in the BUILD file. Enables tooling
# to find the template invocation in a BUILD file where this target was defined.
#
# exclude_toolchain_tags (optional)
# [strings] A list of tags that corresponds to toolchain variants that
# this target should not be built with.
# See: //docs/development/build/concepts/build_system/internals/toolchains/build_variants.md.
#
# Example of usage:
#
# rustc_cdylib("foo") {
# deps = [
# "//garnet/public/rust/bar",
# "//third_party/rust_crates:argh",
# "//third_party/rust_crates:serde",
# "//third_party/rust_crates:slab",
# ]
# with_unit_tests = true
# }
template("rustc_cdylib") {
not_needed(invoker, [ "version" ])
if (defined(invoker.original_target_name)) {
_original_target_name = invoker.original_target_name
} else {
_original_target_name = target_name
}
# if "with_unit_tests" is set to true, generate an additional rust test target
# TODO(https://fxbug.dev/42152447): accept a string.
if (defined(invoker.with_unit_tests) && invoker.with_unit_tests) {
rustc_test("${target_name}_test") {
not_needed(invoker, [ "public" ])
output_name = invoker.target_name + "_cdylib_test"
if (defined(invoker.output_name)) {
output_name = invoker.output_name + "_cdylib_test"
}
forward_variables_from(invoker, "*", [ "output_name" ])
original_target_name = _original_target_name
}
}
rustc_target = "_${target_name}_rustc"
rustc_target = string_replace(rustc_target, "-", "_")
group_target = "${target_name}"
if (!defined(invoker.source_root)) {
source_root = "src/lib.rs"
} else {
source_root = invoker.source_root
}
if (!defined(invoker.output_name)) {
_lib_output_name = "${target_name}"
} else {
_lib_output_name = invoker.output_name
}
rustc_artifact(rustc_target) {
target_type = "shared_library"
crate_root = source_root
clippy_crate_type = "cdylib"
crate_name = rustc_target
pass_through = {
forward_variables_from(invoker,
[
"data_deps",
"exclude_toolchain_tags",
"output_dir",
"runtime_deps",
])
crate_type = "cdylib"
output_name = _lib_output_name
}
original_target_name = _original_target_name
deps = []
if (defined(invoker.deps)) {
deps += invoker.deps
}
# TODO(https://fxbug.dev/42120123) remove "non_rust_deps"
if (defined(invoker.non_rust_deps)) {
deps += invoker.non_rust_deps
}
configs = []
configs = invoker.configs
forward_variables_from(invoker,
[
"disable_clippy",
"edition",
"enforce_source_listing",
"inputs",
"rustenv",
"sources",
"testonly",
"visibility",
])
if (defined(invoker.disable_rbe)) {
disable_rbe = invoker.disable_rbe
}
}
public_hdrs = []
if (defined(invoker.public)) {
public_hdrs += invoker.public
}
header_target = "_${_lib_output_name}_cdylib_headers"
source_set(header_target) {
public = public_hdrs
visibility = [ ":$group_target" ]
deps = []
if (defined(invoker.non_rust_deps)) {
# TODO(https://fxbug.dev/42120123): This allows propagating non rust dependencies through a
# single level of indirection, but does not handle propagating
# dependencies through an intermediate Rust target.
deps += invoker.non_rust_deps
}
}
group(target_name) {
public_deps = [
":$header_target",
":$rustc_target",
]
}
}