blob: 0f081f3a8be367823e33eb9393e85021d1a8f9f8 [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_test.gni")
# Defines a Rust cdylib library
#
# Parameters
#
# name
# 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 (optional)
# Edition of the Rust language to be used.
# Options are "2015" and "2018". Defaults to "2018".
#
# 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>",
#
# 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.
#
# 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'
#
# Example of usage:
#
# rustc_cdylib("foo") {
# deps = [
# "//garnet/public/rust/bar",
# "//third_party/rust_crates:clap",
# "//third_party/rust_crates:serde",
# "//third_party/rust_crates:slab",
# ]
# with_unit_tests = true
# }
template("rustc_cdylib") {
not_needed(invoker, [ "version" ])
# if "with_unit_tests" is set to true, generate an additional rust test target
# TODO(bwb) deprecate this. All tests should be declared independently
if (defined(invoker.with_unit_tests) && invoker.with_unit_tests == true) {
rustc_test("${target_name}_test") {
not_needed(invoker, [ "public" ])
name = invoker.target_name + "_cdylib_test"
if (defined(invoker.name)) {
name = invoker.name + "_cdylib_test"
}
forward_variables_from(invoker, "*", [ "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
}
shared_library(rustc_target) {
crate_root = source_root
output_name = output_name
if (defined(invoker.non_rust_deps)) {
data_deps = invoker.non_rust_deps
}
if (defined(invoker.edition) && invoker.edition == "2015") {
configs -= [ "//build/config:rust_edition_2018" ]
configs += [ "//build/config:rust_edition_2015" ]
}
forward_variables_from(invoker,
[
"deps",
"testonly",
"visibility",
])
}
public_hdrs = []
if (defined(invoker.public)) {
public_hdrs += invoker.public
}
header_target = "_${output_name}_cdylib_headers"
source_set(header_target) {
public = public_hdrs
visibility = [ ":$group_target" ]
deps = []
if (defined(invoker.non_rust_deps)) {
# TODO(43781): 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",
]
}
}