blob: 0bdc79b5f637db02e554d49a169112d15ddccaff [file] [log] [blame]
# Copyright 2016 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.
declare_args() {
# Whether to run analysis on packages as part of the build.
run_dart_analysis = false
}
# Defines a Dart package
#
# Parameters
#
# package_name (optional)
# Name of the dart package. This is used as an identifier in code that
# depends on this package. In general, this should correspond to the path to
# the package. I.e. a dart package in //foo/bar/baz should have the package
# name "foo.bar.baz"
#
# infer_package_name (optional)
# Infer the package name based on the path to the package.
#
# NOTE: Either package_name or infer_package_name must be set.
#
# source_dir (optional)
# Directory containing the package sources. Defaults to "lib".
#
# deps (optional)
# List of labels for Dart packages this package depends on.
#
# non_dart_deps (optional)
# List of labels this package depends on that are not Dart packages. This
# includes things like actions that generate Dart code. It typically doesn't
# need to be set.
#
# analysis_options (optional)
# By default, a script to run the analyzer on the contents of the package is
# generated in the output directory. This parameter contains the path to an
# analysis options file for this target; otherwise a default set of options
# is used.
#
# disable_analysis (optional)
# Prevents the analysis script from being generated.
#
# Example of usage:
#
# dart_package("baz") {
# package_name = "foo.bar.baz"
# deps = [
# "//foo/bar/owl"
# ]
# }
template("dart_package") {
if (defined(invoker.package_name)) {
package_name = invoker.package_name
} else if (defined(invoker.infer_package_name) && invoker.infer_package_name) {
target_label = get_label_info(":$target_name", "label_no_toolchain")
package_name = exec_script("//build/dart/label_to_package_name.py",
[ target_label ],
"trim string")
} else {
assert(false, "Must specify either a package_name or infer_package_name")
}
dart_deps = []
if (defined(invoker.deps)) {
foreach(dep, invoker.deps) {
dart_deps += [ get_label_info(dep, "label_no_toolchain") ]
}
}
dot_packages_file = "$target_gen_dir/$target_name.packages"
source_dir = "lib"
if (defined(invoker.source_dir)) {
source_dir = invoker.source_dir
}
with_analysis = !defined(invoker.disable_analysis) || !invoker.disable_analysis
if (with_analysis) {
dot_packages_target_name = "${target_name}_dot_packages"
} else {
dot_packages_target_name = target_name
}
action(dot_packages_target_name) {
script = "//build/dart/gen_dot_packages.py"
deps = dart_deps
if (defined(invoker.non_dart_deps)) {
deps += invoker.non_dart_deps
}
outputs = [
dot_packages_file,
]
depfile = "$dot_packages_file.d"
args = [
"--out",
rebase_path(dot_packages_file, root_build_dir),
"--source-dir",
rebase_path(source_dir),
"--root-build-dir",
rebase_path(root_build_dir),
"--root-gen-dir",
rebase_path(root_gen_dir),
"--package-name",
package_name,
"--depfile",
rebase_path(depfile),
"--deps",
] + dart_deps
}
if (with_analysis) {
invocation_file = "$target_gen_dir/$target_name.analyzer.sh"
if (run_dart_analysis) {
invocation_target_name = "${target_name}_invocation"
} else {
invocation_target_name = target_name
}
options_file = "//build/dart/default_analysis_options"
if (defined(invoker.analysis_options)) {
options_file = invoker.analysis_options
}
dart_sdk_label = "//dart:create_sdk($host_toolchain)"
dart_sdk_out_dir = get_label_info(dart_sdk_label, "root_out_dir")
dart_sdk_dir = "$dart_sdk_out_dir/dart-sdk"
dart_analyzer_binary = "$dart_sdk_dir/bin/dartanalyzer"
action(invocation_target_name) {
script = "//build/dart/gen_analyzer_invocation.py"
deps = dart_deps + [
dart_sdk_label,
":$dot_packages_target_name",
]
# Technically this is not strictly needed as even if the contents of the
# options file change the analysis script should remain unchanged.
# However, this is a sanity check to ensure at build time that this
# attribute points to a valid file.
inputs = [
options_file,
]
outputs = [
invocation_file,
]
args = [
"--out",
rebase_path(invocation_file),
"--source-dir",
rebase_path(source_dir),
"--dot-packages",
rebase_path(dot_packages_file),
"--dartanalyzer",
rebase_path(dart_analyzer_binary),
"--package-name",
package_name,
"--dart-sdk",
rebase_path(dart_sdk_dir),
"--options",
rebase_path(options_file),
]
}
if (run_dart_analysis) {
action(target_name) {
script = "//build/dart/run_analysis.py"
depfile = "$target_gen_dir/$target_name.analysis.d"
output_file = "$target_gen_dir/$target_name.analyzed"
inputs = [
options_file,
]
outputs = [
output_file,
]
args = [
"--source-dir",
rebase_path(source_dir),
"--dot-packages",
rebase_path(dot_packages_file),
"--dartanalyzer",
rebase_path(dart_analyzer_binary),
"--dart-sdk",
rebase_path(dart_sdk_dir),
"--options",
rebase_path(options_file),
"--stamp",
rebase_path(output_file),
"--depname",
rebase_path(output_file, root_build_dir),
"--depfile",
rebase_path(depfile),
]
deps = [
":$invocation_target_name",
]
}
}
}
}