blob: 8eba73984e413536f98e0a8af811a9fee6ace9c9 [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/dart/dart_library.gni")
import("//build/fidl/toolchain.gni")
# Declares a source_set that contains code generated by fidlmerge from a
# template and a FIDL JSON file.
#
# Parameters
#
# fidl_target (required)
# Specifies the fidl target from which to read fidl json. For example,
# "//sdk/fidl/fuchsia.sys" for fuchsia.sys.
#
# template_path (required)
# Specifies the template to use to generate the source code for the
# source_set. For example, "//src/lib/fostr/build/fostr.fidlmerge".
#
# generated_source_base (required)
# The base file name from which the source_set's 'source' file names are
# generated. For example, "formatting".
#
# options (optional)
# A single string with comma-separated key=value pairs.
#
# amendments_path (optional)
# Specifies a JSON file that contains amendments to be made to the fidl
# model before the template is applied.
# See the fidlmerge README for details.
#
# deps, public_deps, test_only, visibility (optional)
# These parameters are forwarded to the source_set.
#
template("fidlmerge_dart") {
assert(defined(invoker.fidl_target),
"fidlmerge_dart requires parameter fidl_target.")
assert(defined(invoker.template_path),
"fidlmerge_dart requires parameter template_path.")
assert(defined(invoker.generated_source_base),
"fidlmerge_dart requires parameter generated_source_base.")
fidl_target = invoker.fidl_target
template_path = invoker.template_path
source_base = invoker.generated_source_base
generation_target_name = "${target_name}_generate"
fidl_target_gen_dir =
get_label_info("$fidl_target($fidl_toolchain)", "target_gen_dir")
fidl_target_name = get_path_info(fidl_target_gen_dir, "file")
json_representation = "$fidl_target_gen_dir/$fidl_target_name.fidl.json"
# Subtle: rebase dance, so that we allow paths up to and including the build
# dir to contain periods:
include_stem =
string_replace(rebase_path(target_gen_dir, root_build_dir), ".", "/")
file_stem = "$root_build_dir/$include_stem/$source_base"
compiled_action(generation_target_name) {
forward_variables_from(invoker, [ "testonly" ])
visibility = [ ":*" ]
tool = "//tools/fidl/fidlmerge"
inputs = [
json_representation,
template_path,
]
outputs = [ "$file_stem.dart" ]
args = [
"--template",
rebase_path(template_path, root_build_dir),
"--json",
rebase_path(json_representation, root_build_dir),
"--output-base",
rebase_path(file_stem, root_build_dir),
]
if (defined(invoker.options)) {
args += [
"--options",
invoker.options,
]
}
if (defined(invoker.amendments_path)) {
args += [
"--amend",
rebase_path(invoker.amendments_path, root_build_dir),
]
}
deps = [ "$fidl_target($fidl_toolchain)" ]
if (defined(invoker.deps)) {
deps += invoker.deps
}
}
copy_pubspec_target_name = "${target_name}_dart_pubspec"
copy_options_target_name = "${target_name}_dart_options"
copy_source_target_name = "${target_name}_dart_sources"
library_name = target_name
root_dir = "$target_gen_dir/${library_name}_package"
copy(copy_pubspec_target_name) {
sources = [ "//build/dart/empty_pubspec.yaml" ]
outputs = [ "$root_dir/pubspec.yaml" ]
}
copy(copy_source_target_name) {
sources = [ "$file_stem.dart" ]
outputs = [ "$root_dir/lib/$source_base.dart" ]
deps = [ ":$generation_target_name" ]
}
dart_library(library_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
package_root = root_dir
package_name = "fidl_" + string_replace(library_name, ".", "_")
sources = [ "$source_base.dart" ]
deps = [
":$copy_pubspec_target_name",
":$copy_source_target_name",
":$generation_target_name",
]
if (defined(invoker.deps)) {
deps += invoker.deps
}
if (defined(invoker.public_deps)) {
deps += invoker.public_deps
}
}
}