blob: 7dda21b12933aa40df0e37db906313765004f2ae [file] [log] [blame]
# Copyright 2021 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/rustc_library.gni")
template("_fidl_banjo_c") {
c_target_name = "${target_name}_banjo_c"
c_fidlgen_target_name = "$c_target_name.gen"
c_config_target_name = "$c_target_name.config"
ir_target_name = get_label_info(invoker.fidl_library, "name")
ir_gen_dir = get_label_info(invoker.fidl_library, "target_gen_dir")
ir_file = "$ir_gen_dir/$ir_target_name.fidl.json"
c_header_path = string_replace(invoker.library_name, ".", "/")
c_header_path = string_replace(c_header_path, "_", "-")
c_header_include_base = "$target_gen_dir/$c_target_name"
c_header_file = "$c_header_include_base/$c_header_path/c/banjo.h"
# Run fidlgen_banjo on the IR and generate C code.
compiled_action(c_fidlgen_target_name) {
forward_variables_from(invoker, [ "testonly" ])
tool = "//src/devices/tools/fidlgen_banjo:bin"
tool_output_name = "fidlgen_banjo"
inputs = [ ir_file ]
outputs = [ c_header_file ]
deps = [ invoker.fidl_library ]
args = [
"--ir",
rebase_path(ir_file, root_build_dir),
"--output",
rebase_path(c_header_file, root_build_dir),
"--backend",
"c",
]
}
# This config ensures dependents can find the generated header within the
# output directory.
config(c_config_target_name) {
include_dirs = [ c_header_include_base ]
}
# Exposes the bindings as C sources to the rest of the build.
# Eventually this target will be surfaced via the `fidl` template.
source_set(c_target_name) {
forward_variables_from(invoker, [ "testonly" ])
public = [ c_header_file ]
public_configs = [ ":$c_config_target_name" ]
deps = [ ":$c_fidlgen_target_name" ]
if (defined(invoker.deps)) {
deps += invoker.deps
}
public_deps = []
if (defined(invoker.public_deps)) {
foreach(dep, invoker.public_deps) {
_name = get_label_info(dep, "name")
_dir = get_label_info(dep, "dir")
public_deps += [ "$_dir:${_name}_banjo_c" ]
}
}
}
}
template("_fidl_banjo_cpp") {
c_target_name = "${target_name}_banjo_c"
cpp_target_name = "${target_name}_banjo_cpp"
cpp_mock_target_name = "${target_name}_banjo_cpp_mock"
cpp_public_fidlgen_target_name = "$cpp_target_name.public.gen"
cpp_private_fidlgen_target_name = "$cpp_target_name.private.gen"
cpp_mock_fidlgen_target_name = "$cpp_target_name.mock.gen"
ir_target_name = get_label_info(invoker.fidl_library, "name")
ir_gen_dir = get_label_info(invoker.fidl_library, "target_gen_dir")
ir_file = "$ir_gen_dir/$ir_target_name.fidl.json"
cpp_header_path = string_replace(invoker.library_name, ".", "/")
cpp_header_path = string_replace(cpp_header_path, "_", "-")
# Note: C++ headers are explicitly installed in the same location as their C
# counterparts as they expect to be colocated.
cpp_header_include_base = "$target_gen_dir/$c_target_name"
cpp_public_file = "$cpp_header_include_base/$cpp_header_path/cpp/banjo.h"
cpp_private_file =
"$cpp_header_include_base/$cpp_header_path/cpp/banjo-internal.h"
cpp_mock_file = "$cpp_header_include_base/$cpp_header_path/cpp/banjo-mock.h"
# Run fidlgen_banjo on the IR and generate C++ code.
compiled_action(cpp_public_fidlgen_target_name) {
forward_variables_from(invoker, [ "testonly" ])
tool = "//src/devices/tools/fidlgen_banjo:bin"
tool_output_name = "fidlgen_banjo"
inputs = [ ir_file ]
outputs = [ cpp_public_file ]
deps = [ invoker.fidl_library ]
args = [
"--ir",
rebase_path(ir_file, root_build_dir),
"--output",
rebase_path(cpp_public_file, root_build_dir),
"--backend",
"cpp",
]
}
compiled_action(cpp_private_fidlgen_target_name) {
forward_variables_from(invoker, [ "testonly" ])
tool = "//src/devices/tools/fidlgen_banjo:bin"
tool_output_name = "fidlgen_banjo"
inputs = [ ir_file ]
outputs = [ cpp_private_file ]
deps = [ invoker.fidl_library ]
args = [
"--ir",
rebase_path(ir_file, root_build_dir),
"--output",
rebase_path(cpp_private_file, root_build_dir),
"--backend",
"cpp_internal",
]
}
compiled_action(cpp_mock_fidlgen_target_name) {
forward_variables_from(invoker, [ "testonly" ])
tool = "//src/devices/tools/fidlgen_banjo:bin"
tool_output_name = "fidlgen_banjo"
inputs = [ ir_file ]
outputs = [ cpp_mock_file ]
deps = [ invoker.fidl_library ]
args = [
"--ir",
rebase_path(ir_file, root_build_dir),
"--output",
rebase_path(cpp_mock_file, root_build_dir),
"--backend",
"cpp_mock",
]
}
source_set(cpp_target_name) {
forward_variables_from(invoker, [ "testonly" ])
public = [ cpp_public_file ]
sources = [ cpp_private_file ]
# Let dependencies use `#include "$file_stem.h"`.
public_configs = [ "//build/c:banjo_gen_config" ]
deps = [
":$cpp_private_fidlgen_target_name",
":$cpp_public_fidlgen_target_name",
"//src/zircon/lib/zircon",
]
if (defined(invoker.deps)) {
deps += invoker.deps
}
public_deps = [
":$c_target_name",
# The generated headers #include <ddk/*.h> and #include <ddktl/*.h>
# files from the libraries (that aren't generated).
"//src/lib/ddk",
"//src/lib/ddktl",
"//zircon/system/ulib/zx",
]
if (defined(invoker.public_deps)) {
foreach(dep, invoker.public_deps) {
_name = get_label_info(dep, "name")
_dir = get_label_info(dep, "dir")
deps += [ "$_dir:${_name}_banjo_cpp" ]
}
}
}
source_set(cpp_mock_target_name) {
testonly = true
public = [ cpp_mock_file ]
deps = [ ":$cpp_mock_fidlgen_target_name" ]
if (defined(invoker.deps)) {
deps += invoker.deps
}
public_deps = [
":$cpp_target_name",
"//zircon/system/ulib/fbl",
"//zircon/system/ulib/mock-function",
"//zircon/system/ulib/zxtest",
]
if (defined(invoker.public_deps)) {
foreach(dep, invoker.public_deps) {
_name = get_label_info(dep, "name")
_dir = get_label_info(dep, "dir")
deps += [ "$_dir:${_name}_banjo_cpp_mock" ]
}
}
}
}
template("_fidl_banjo_rust") {
rust_target_name = "${target_name}_banjo_rust"
rust_fidlgen_target_name = "$rust_target_name.gen"
ir_target_name = get_label_info(invoker.fidl_library, "name")
ir_gen_dir = get_label_info(invoker.fidl_library, "target_gen_dir")
ir_file = "$ir_gen_dir/$ir_target_name.fidl.json"
rust_library_name = "banjo_" + string_replace(invoker.library_name, ".", "_")
rust_file_name = "$rust_library_name.rs"
rust_source_file = "$target_gen_dir/$rust_target_name/$rust_file_name"
# Run fidlgen_banjo on the IR and generate Rust code.
compiled_action(rust_fidlgen_target_name) {
forward_variables_from(invoker, [ "testonly" ])
tool = "//src/devices/tools/fidlgen_banjo:bin"
tool_output_name = "fidlgen_banjo"
inputs = [ ir_file ]
outputs = [ rust_source_file ]
deps = [ invoker.fidl_library ]
args = [
"--ir",
rebase_path(ir_file, root_build_dir),
"--output",
rebase_path(rust_source_file, root_build_dir),
"--backend",
"rust",
]
}
rustc_library(rust_target_name) {
forward_variables_from(invoker, [ "testonly" ])
name = rust_library_name
version = "0.1.0"
edition = "2018"
source_root = rebase_path(rust_source_file)
sources = [ rust_source_file ]
output_dir = "$target_out_dir/banjo/$rust_target_name"
non_rust_deps = [ ":$rust_fidlgen_target_name" ]
if (defined(invoker.deps)) {
non_rust_deps += invoker.deps
}
deps = [
"//src/lib/fuchsia-async",
"//src/lib/zircon/rust:fuchsia-zircon",
"//third_party/rust_crates:futures",
]
configs -= [ "//build/config/rust:allow_unused_results" ]
if (defined(invoker.public_deps)) {
foreach(dep, invoker.public_deps) {
_name = get_label_info(dep, "name")
_dir = get_label_info(dep, "dir")
deps += [ "$_dir:${_name}_banjo_rust" ]
}
}
}
}
template("fidl_banjo") {
not_needed(invoker, "*")
params = {
forward_variables_from(invoker,
[
"deps",
"public_deps",
"testonly",
])
}
params.fidl_library = ":$target_name($fidl_toolchain)"
params.library_name = target_name
if (defined(invoker.name)) {
params.library_name = invoker.name
}
_fidl_banjo_c(target_name) {
forward_variables_from(params, "*")
}
_fidl_banjo_cpp(target_name) {
forward_variables_from(params, "*")
}
_fidl_banjo_rust(target_name) {
forward_variables_from(params, "*")
}
}