blob: 3224a16dcdd0d87784dfc90da41435087e6fd281 [file] [log] [blame]
# Copyright 2018 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/compiled_action.gni")
import("//build/fidl/toolchain.gni")
import("//build/rust/config.gni")
import("//build/rust/rustc_library.gni")
# Generates Rust bindings for a FIDL library.
#
# Parameters (see //build/fidl/fidl.gni for descriptions):
#
# name
# public_deps
# testonly
# visibility
#
template("fidl_rust") {
not_needed(invoker, [ "sdk_category" ])
rust_target_name = "${target_name}_rust"
generation_target_name = "${rust_target_name}_generate"
library_name = target_name
if (defined(invoker.name)) {
library_name = invoker.name
}
crate_name = "fidl_" + string_replace(library_name, ".", "_")
fidl_target_gen_dir =
get_label_info(":bogus($fidl_toolchain)", "target_gen_dir")
file_stem = "$fidl_target_gen_dir/$target_name/$crate_name"
if (current_toolchain == fidl_toolchain) {
not_needed(invoker,
[
"public_deps",
"visibility",
])
filename = "$file_stem.rs"
json_representation = "$fidl_target_gen_dir/$target_name.fidl.json"
json_representation_target = ":${target_name}"
compiled_action(generation_target_name) {
forward_variables_from(invoker, [ "testonly" ])
visibility = [
":*",
"//tools/fidl/fidlgen_rust:*",
]
tool = "//tools/fidl/fidlgen_rust"
inputs = [
json_representation,
"//rustfmt.toml",
"$rustc_prefix/bin/rustfmt",
rustc_version_file,
]
outputs = [ filename ]
args = [
"--json",
rebase_path(json_representation, root_build_dir),
"--output-filename",
rebase_path(filename, root_build_dir),
"--rustfmt",
"$rebased_rustc_prefix/bin/rustfmt",
"--rustfmt-config",
rebase_path("//rustfmt.toml", root_build_dir),
]
deps = [ json_representation_target ]
metadata = {
generated_sources = rebase_path(outputs, root_build_dir)
}
}
} else {
rustc_library(rust_target_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
name = crate_name
output_dir = "$target_out_dir/$target_name"
version = "0.1.0"
edition = "2018"
disable_clippy = true
deps = [
"//src/lib/fidl/rust/fidl",
"//src/lib/zircon/rust:fuchsia-zircon-status",
"//third_party/rust_crates:bitflags",
"//third_party/rust_crates:futures",
]
# TODO(https://fxbug.dev/104019): Avoid this suppression. At the time of
# writing it is needed because this target might depend on another FIDL
# library *only* for type aliases; the generated Rust code will not
# reference this external alias, producing an unused crate dependency. We
# cannot know here whether that's the case or not, so we just suppress
# the warning.
configs += [ "//build/config/rust/lints:allow_unused_crate_dependencies" ]
if (is_fuchsia) {
deps += [ "//src/lib/zircon/rust:fuchsia-zircon" ]
}
if (defined(invoker.public_deps)) {
foreach(dep, invoker.public_deps) {
label = get_label_info(dep, "label_no_toolchain")
deps += [ "${label}_rust" ]
}
}
non_rust_deps = [ ":$generation_target_name($fidl_toolchain)" ]
source_root = "$file_stem.rs"
sources = [ "$file_stem.rs" ]
configs -= [ "//build/config/rust/lints:allow_unused_results" ]
}
}
}