| # 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 |
| # |
| # * library_name |
| # - Required: The name of the FIDL library. |
| # - Type: string |
| # |
| # * fidl_target |
| # - Required: The name of the associated fidl() target. |
| # - Type: string |
| # |
| # * fidl_ir_json |
| # - Required: The path to the associated FIDL IR JSON file. |
| # - Type: path |
| # |
| # * fidl_ir_target |
| # - Required: The label of the target that generates the FIDL IR JSON file. |
| # - Type: label |
| # |
| # * sdk_cateogry, dart_analysis |
| # - Optional: See //build/fidl/fidl.gni for a description. |
| # - Type: string |
| # |
| # * testonly, visibility, public_deps |
| # - Optional: Usual GN meanings. |
| # |
| template("fidl_dart") { |
| assert( |
| current_toolchain == dart_toolchain, |
| "This template can only be used in the Dart toolchain $dart_toolchain.") |
| |
| assert(defined(invoker.library_name), |
| "fidl_dart(\"$target_name\") must define `library_name`") |
| assert(defined(invoker.fidl_target), |
| "fidl_dart(\"$target_name\") must define `fidl_target`") |
| assert(defined(invoker.fidl_gen_dir), |
| "fidl_dart(\"$target_name\") must define `fidl_gen_dir`") |
| assert(defined(invoker.fidl_ir_json), |
| "fidl_dart(\"$target_name\") must define `fidl_ir_json`") |
| assert(defined(invoker.fidl_ir_target), |
| "fidl_dart(\"$target_name\") must define `fidl_ir_target`") |
| |
| main_target = target_name |
| generation_target = "${target_name}_generate" |
| copy_pubspec_target = "_fidl_dart.${target_name}.pubspec" |
| |
| forward_variables_from(invoker, |
| [ |
| "library_name", |
| "fidl_gen_dir", |
| ]) |
| root_dir = fidl_gen_dir |
| 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) { |
| forward_variables_from(invoker, |
| [ |
| "testonly", |
| "fidl_ir_target", |
| "fidl_ir_json", |
| ]) |
| |
| visibility = [ |
| ":*", |
| "//tools/fidl/fidlgen_dart:*", |
| ] |
| |
| tool = "//tools/fidl/fidlgen_dart" |
| |
| inputs = [ fidl_ir_json ] |
| |
| outputs = [ |
| async_bindings_file, |
| test_bindings_file, |
| ] |
| |
| args = [ |
| "--json", |
| rebase_path(fidl_ir_json, root_build_dir), |
| "--output-async", |
| rebase_path(async_bindings_file, root_build_dir), |
| "--output-test", |
| rebase_path(test_bindings_file, root_build_dir), |
| "--dart", |
| rebase_path(prebuilt_dart, root_build_dir), |
| ] |
| deps = [ fidl_ir_target ] + dart_sdk_deps |
| |
| metadata = { |
| generated_sources = rebase_path(outputs, root_build_dir) |
| } |
| } |
| |
| copy(copy_pubspec_target) { |
| sources = [ "//build/dart/empty_pubspec.yaml" ] |
| |
| outputs = [ "$root_dir/pubspec.yaml" ] |
| } |
| |
| dart_library(main_target) { |
| 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", |
| ":$generation_target", |
| "//sdk/dart/fidl", |
| "//sdk/dart/zircon", |
| "//third_party/dart-pkg/pub/meta", |
| ] |
| |
| if (defined(invoker.public_deps)) { |
| foreach(dep, invoker.public_deps) { |
| deps += [ get_label_info(dep, "label_no_toolchain") + "_dart" ] |
| } |
| } |
| } |
| |
| 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 = "${main_target}_sdk" |
| sdk_atom_alias(sdk_target_name) { |
| atom = ":${invoker.fidl_target}_sdk($fidl_toolchain)" |
| } |
| } else { |
| not_needed(invoker, [ "fidl_target" ]) |
| } |
| } |