blob: 40f828d92da9715ddae5cc83a73a18fbd9455b75 [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/c/fidl_c.gni")
import("//build/cpp/fidl_cpp.gni")
import("//build/dart/fidl_dart.gni")
import("//build/dart/toolchain.gni")
import("//build/fidl/fidl_library.gni")
import("//build/fidl/toolchain.gni")
import("//build/go/fidl_go.gni")
import("//build/go/toolchain.gni")
import("//zircon/tools/zither/zither_library.gni")
if (support_rust) {
import("//build/banjo/fidl_banjo.gni")
import("//build/bind/fidl_bind_library.gni")
import("//build/rust/fidl_rust.gni")
}
# Declares a FIDL library.
#
# Supported backends: rust, hlcpp, llcpp, banjo_{c,cpp,rust}, bindlib, dart,
# go, and zither.
#
# Subtargets:
# * `${target_name}`: available only in the FIDL toolchain
# * `${target_name}_dart`: available only in the dart toolchain
# * `${target_name}_go`: available only in the go toolchain
# * `${target_name}_${backend_name}`: available in every toolchain but dart
# and go.
#
# Parameters
#
# sources (required)
# List of paths to library source files.
#
# name (optional)
# Name of the library.
# Defaults to the target's name.
#
# sdk_category (optional)
# Publication level of the library in SDKs.
# See //build/sdk/sdk_atom.gni.
#
# api (optional)
# Path to the file representing the API of this library.
# This file is used to ensure modifications to the library's API are
# explicitly acknowledged. It is mandatory for publication categories of
# "partner" or "public".
# Defaults to "<target name>.api".
#
# excluded_checks (optional)
# A list of fidl-lint check IDs to ignore (by passing the command line flag
# "-e some-check-id" for each value).
#
# experimental_checks (optional)
# A list of fidl-lint check IDs to include (by passing the command line flag
# "-x some-check-id" for each value).
#
# fuzzers (optional)
# Protocol/methods for which to generate LibFuzzer fuzz targets. Example:
# [
# {
# # Required:
# protocol = "fully.qualified.FIDLProtocolName"
# # Optional. Default: All methods in protocol.
# methods = [ "MethodName1", "MethodName2", ... ]
# },
# ...
# ]
#
# golden_fuzzer (optional)
# Boolean flag to generate a LibFuzzer fuzz target for all protocols, used
# to ensure fuzzers for golden libraries compile successfully.
#
# experimental_flags (optional)
# A list of experimental fidlc features to enable.
#
# goldens_dir (optional, default "//sdk/history")
# The directory containing golden files for this FIDL API, per API level.
# Should not contain a trailing slash. This is only used if the API is
# publishable in an SDK.
#
# non_fidl_deps (optional)
# A list of non-FIDL dependency targets, i.e. targets that don't contribute
# FIDL artifacts, but should be built before this target regardless. This is
# typically used when `sources` contains files generated by another target.
#
# dart_analysis (optional, boolean, default false)
# If set to true, dart analysis is run on generated Dart bindings.
# Dart analysis can take tens of seconds to finish, so running it for all
# generated Dart bindings is wasteful. This parameter allows us to only run
# analysis on goldens, and skip for other generated bindings. See fxb/82975
# for details.
#
# contains_drivers (optional, boolean, default false)
# Indicates if any of the FIDL files contain the driver transport or
# references to the driver transport.
#
# enable_hlcpp (optional flag, default to false)
# Set to true to enable legacy HLCPP bindings for this library
#
# enable_banjo (optional flag, default to false)
# Set to true to enable Banjo bindings for this library.
#
# enable_zither (optional flag, default to false)
# Set to true to enable Zither bindings for this library.
# See //zircon/tools/zither/README.md for details.
#
# public_deps
# testonly
# visibility
#
template("fidl") {
assert(defined(invoker.sources), "A FIDL library requires some sources.")
assert(!defined(invoker.deps),
"All FIDL dependencies are inherently " +
"public, use 'public_deps' instead of 'deps'.")
extra_parameters = {
library_name = target_name
if (defined(invoker.name)) {
library_name = invoker.name
}
fidl_target = target_name
fidl_gen_dir = get_label_info(":$fidl_target($fidl_toolchain)",
"target_gen_dir") + "/$fidl_target"
# The FIDL IR target, to be forwarded to each backend as a dependency.
fidl_ir_target = ":${target_name}_compile_fidlc($fidl_toolchain)"
fidl_ir_json = get_label_info(fidl_ir_target, "target_gen_dir") +
"/${target_name}.fidl.json"
}
enable_banjo = defined(invoker.enable_banjo) && invoker.enable_banjo
enable_hlcpp = defined(invoker.enable_hlcpp) && invoker.enable_hlcpp
enable_zither = defined(invoker.enable_zither) && invoker.enable_zither
not_needed([
"enable_banjo",
"enable_hlcpp",
"enable_zither",
])
is_go = current_toolchain == go_toolchain
is_dart = current_toolchain == dart_toolchain
# Allow generated targets visibility to their dependant generated targets
if (defined(invoker.visibility)) {
invoker.visibility += [ ":*" ]
}
fidl_only = [
"api",
"excluded_checks",
"experimental_checks",
"experimental_flags",
"goldens_dir",
"non_fidl_deps",
]
if (current_toolchain == fidl_toolchain) {
fidl_library(target_name) {
forward_variables_from(invoker,
fidl_only + [
"testonly",
"visibility",
"public_deps",
"sdk_category",
"sources",
])
forward_variables_from(extra_parameters,
[
"library_name",
"fidl_ir_json",
])
}
# Check that the IR JSON is indeed an output of the purported IR target.
fidlc_outputs = get_target_outputs(extra_parameters.fidl_ir_target)
assert(
[ extra_parameters.fidl_ir_json ] + fidlc_outputs - fidlc_outputs == [])
} else {
not_needed(invoker, fidl_only)
}
dart_only = [ "dart_analysis" ]
if (is_dart) {
fidl_dart("${target_name}_dart") {
forward_variables_from(invoker,
dart_only + [
"testonly",
"visibility",
"public_deps",
"sdk_category",
])
forward_variables_from(extra_parameters, "*")
fidl_gen_dir += "/dart"
}
} else {
not_needed(invoker, dart_only + [ "sources" ])
}
if (is_go) {
fidl_go("${target_name}_go") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
"public_deps",
])
forward_variables_from(extra_parameters, "*", [ "fidl_target" ])
fidl_gen_dir += "/go"
}
not_needed(invoker,
[
"sdk_category",
"sources",
])
}
non_dart_go_only = [
"contains_drivers",
"fuzzers",
"golden_fuzzer",
]
if (!is_dart && !is_go) {
fidl_cpp_family(target_name) {
forward_variables_from(invoker,
non_dart_go_only + [
"testonly",
"visibility",
"public_deps",
"sdk_category",
])
forward_variables_from(extra_parameters, "*")
enable_hlcpp = enable_hlcpp
}
if (support_rust) {
fidl_rust("${target_name}_rust") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
"public_deps",
])
forward_variables_from(extra_parameters, "*", [ "fidl_target" ])
fidl_gen_dir += "/rust"
}
fidl_bind_library("${target_name}_bindlib") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
forward_variables_from(extra_parameters, "*", [ "fidl_target" ])
fidl_gen_dir += "/bindlib"
}
}
if (enable_banjo && support_rust) {
fidl_banjo("${target_name}_banjo") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
"public_deps",
])
forward_variables_from(extra_parameters, "*")
fidl_gen_dir += "/banjo"
}
}
if (enable_zither) {
zither_library("${target_name}_zither") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
"sources",
])
forward_variables_from(extra_parameters, "*")
fidl_gen_dir += "/zither"
}
} else {
not_needed(invoker, [ "sources" ])
}
fidl_tables("${target_name}_tables") {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
forward_variables_from(extra_parameters, [ "fidl_target" ])
}
} else {
not_needed(invoker, non_dart_go_only + [ "sources" ])
}
}