| # 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/dart/toolchain.gni") |
| import("//build/fidl/toolchain.gni") |
| import("//build/sdk/sdk_atom_alias.gni") |
| |
| # Generates Dart bindings for a FIDL library. |
| # |
| # Parameters (see //build/fidl/fidl.gni for descriptions): |
| # |
| # dart_analysis |
| # name |
| # public_deps |
| # sdk_category |
| # testonly |
| # visibility |
| # |
| template("fidl_dart") { |
| assert( |
| current_toolchain == dart_toolchain, |
| "This template can only be used in the Dart toolchain $dart_toolchain.") |
| |
| generation_target_name = "${target_name}_dart_generate" |
| |
| library_name = target_name |
| if (defined(invoker.name)) { |
| library_name = invoker.name |
| } |
| |
| json_representation_target = ":${target_name}($fidl_toolchain)" |
| fidl_target_gen_dir = |
| get_label_info(json_representation_target, "target_gen_dir") |
| json_representation = "$fidl_target_gen_dir/$target_name.fidl.json" |
| |
| root_dir = "$fidl_target_gen_dir/${library_name}_package" |
| if (library_name != target_name) { |
| root_dir = "$fidl_target_gen_dir/${target_name}_${library_name}_package" |
| } |
| bindings_dir = "$root_dir/lib" |
| async_bindings_file = "$bindings_dir/fidl_async.dart" |
| test_bindings_file = "$bindings_dir/fidl_test.dart" |
| |
| compiled_action(generation_target_name) { |
| forward_variables_from(invoker, [ "testonly" ]) |
| |
| visibility = [ |
| ":*", |
| "//tools/fidl/fidlgen_dart:*", |
| ] |
| |
| tool = "//tools/fidl/fidlgen_dart" |
| |
| inputs = [ json_representation ] |
| |
| outputs = [ |
| async_bindings_file, |
| test_bindings_file, |
| ] |
| |
| args = [ |
| "--json", |
| rebase_path(json_representation, root_build_dir), |
| "--output-async", |
| rebase_path(async_bindings_file, root_build_dir), |
| "--output-test", |
| rebase_path(test_bindings_file, root_build_dir), |
| ] |
| |
| deps = [ json_representation_target ] |
| |
| # Don't run the formatter if we are using a custom-built Dart SDK in a |
| # cross-build. |
| if (host_cpu == target_cpu) { |
| args += [ |
| "--dart", |
| rebase_path(prebuilt_dart, root_build_dir), |
| ] |
| deps += dart_sdk_deps |
| } |
| |
| metadata = { |
| generated_sources = rebase_path(outputs, root_build_dir) |
| } |
| } |
| |
| copy_pubspec_target_name = "${target_name}_dart_pubspec" |
| |
| copy(copy_pubspec_target_name) { |
| sources = [ "//build/dart/empty_pubspec.yaml" ] |
| |
| outputs = [ "$root_dir/pubspec.yaml" ] |
| } |
| |
| dart_library(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "visibility", |
| ]) |
| |
| # Dart analysis can take tens of second, so skip by default to save build |
| # time. |
| disable_analysis = !defined(invoker.dart_analysis) || !invoker.dart_analysis |
| |
| package_root = root_dir |
| |
| package_name = "fidl_" + string_replace(library_name, ".", "_") |
| |
| null_safe = true |
| |
| sources = [ |
| rebase_path(async_bindings_file, bindings_dir), |
| rebase_path(test_bindings_file, bindings_dir), |
| ] |
| |
| deps = [ |
| ":$copy_pubspec_target_name", |
| ":$generation_target_name", |
| "//sdk/dart/fidl", |
| "//sdk/dart/zircon", |
| "//third_party/dart-pkg/pub/meta", |
| ] |
| |
| if (defined(invoker.public_deps)) { |
| deps += invoker.public_deps |
| } |
| } |
| |
| if (defined(invoker.sdk_category) && invoker.sdk_category != "excluded") { |
| # Instead of depending on the generated bindings, set up a dependency on the |
| # original library. |
| sdk_target_name = "${target_name}_sdk" |
| sdk_atom_alias(sdk_target_name) { |
| atom = ":$sdk_target_name($fidl_toolchain)" |
| } |
| } |
| } |