blob: f8c7ccb7a4c43dd1a6fb942644c75d46fe2423c4 [file] [log] [blame]
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This file has rules for making Dart packages and Dart-based Mojo applications.
# The entrypoint is the dart_pkg rule.
import("../mojo.gni")
import("//build/module_args/mojo.gni")
import("//build/module_args/dart.gni")
template("dartx") {
bundle_prefix = target_name
bundle = "$target_gen_dir/${bundle_prefix}.dartx"
snapshot = "$target_gen_dir/${bundle_prefix}_snapshot.bin"
if (mojo_use_prebuilt_dart_snapshotter) {
dart_snapshotter_path =
rebase_path("mojo/public/tools:copy_dart_snapshotter", ".", mojo_root)
dart_snapshotter_rule = "$dart_snapshotter_path($host_toolchain)"
} else {
dart_snapshotter_rule = dart_snapshotter_bin
}
dart_snapshotter_dir =
get_label_info("$dart_snapshotter_rule", "root_out_dir")
dart_snapshotter = "$dart_snapshotter_dir/dart_snapshotter"
action("gen_${bundle_prefix}_snapshot") {
main_dart = invoker.main_dart
inputs = [
dart_snapshotter,
main_dart,
]
outputs = [
snapshot,
]
if (defined(invoker.sources)) {
inputs += invoker.sources
}
script =
rebase_path("mojo/public/tools/dart_snapshotter.py", ".", mojo_sdk_root)
args = [
rebase_path(dart_snapshotter),
rebase_path(main_dart),
"--package-root",
rebase_path("$root_gen_dir/dart-pkg/packages"),
"--snapshot",
rebase_path(snapshot),
]
deps = [
dart_snapshotter_rule,
]
if (defined(invoker.deps)) {
deps += invoker.deps
}
}
action("gen_${bundle_prefix}_bundle") {
sources = [
rebase_path("mojo/public/tools/dartx.py", ".", mojo_sdk_root),
snapshot,
]
outputs = [
bundle,
]
script = rebase_path("mojo/public/tools/dartx.py", ".", mojo_sdk_root)
args = [
"--snapshot",
rebase_path(snapshot),
"--output",
rebase_path(bundle),
]
deps = [
":gen_${bundle_prefix}_snapshot",
]
}
group(target_name) {
deps = [
":gen_${bundle_prefix}_bundle",
]
}
}
template("dartx_application") {
dartx_name = "${target_name}_dartx"
dartx(dartx_name) {
main_dart = invoker.main_dart
if (defined(invoker.sources)) {
sources = invoker.sources
}
if (defined(invoker.deps)) {
deps = invoker.deps
}
}
if (defined(invoker.output_name)) {
mojo_output = "$root_out_dir/" + invoker.output_name + ".mojo"
} else {
mojo_output = "$root_out_dir/" + target_name + ".mojo"
}
action(target_name) {
script = rebase_path("mojo/public/tools/prepend.py", ".", mojo_sdk_root)
input = "$target_gen_dir/${dartx_name}.dartx"
inputs = [
input,
]
output = mojo_output
outputs = [
output,
]
deps = [
":$dartx_name",
]
if (defined(invoker.deps)) {
deps += invoker.deps
}
line = "#!mojo mojo:dart_content_handler"
if (is_debug || (defined(invoker.strict) && invoker.strict == true)) {
line = "#!mojo mojo:dart_content_handler?strict=true"
}
rebase_input = rebase_path(input, root_build_dir)
rebase_output = rebase_path(output, root_build_dir)
args = [
"--input=$rebase_input",
"--output=$rebase_output",
"--line=$line",
]
}
}
template("dart_pkg_helper") {
assert(defined(invoker.package_name))
package_name = invoker.package_name
pkg_directory = rebase_path("$root_gen_dir/dart-pkg")
package_root = rebase_path("$root_gen_dir/dart-pkg/packages")
stamp_file = "$root_gen_dir/dart-pkg/${package_name}.stamp"
assert(defined(invoker.sources) || defined(invoker.apps) ||
defined(invoker.libs) || defined(invoker.pkg_dir))
action(target_name) {
deps = []
if (defined(invoker.deps)) {
deps += invoker.deps
}
datadeps = []
if (defined(invoker.datadeps)) {
datadeps += invoker.datadeps
}
list_mojoms_script =
rebase_path("mojo/public/tools/dart_list_mojoms.py", ".", mojo_sdk_root)
mojom_sources = []
foreach(d, deps) {
root_prefix = rebase_path(".", "", mojo_sdk_root)
source_directory = rebase_path(get_label_info(d, "dir"))
mojom_sources += exec_script(list_mojoms_script,
[
source_directory,
root_prefix,
],
"list lines")
}
sdk_ext_directory = []
if (defined(invoker.sdk_ext_directory)) {
sdk_ext_directory += [ invoker.sdk_ext_directory ]
}
sdk_ext_files = []
if (defined(invoker.sdk_ext_files)) {
sdk_ext_files += invoker.sdk_ext_files
}
sdk_ext_mappings = []
if (defined(invoker.sdk_ext_mappings)) {
sdk_ext_mappings += invoker.sdk_ext_mappings
}
script = rebase_path("mojo/public/tools/dart_pkg.py", ".", mojo_sdk_root)
entrypoints = []
if (defined(invoker.apps)) {
entrypoints += invoker.apps
}
if (defined(invoker.libs)) {
entrypoints += invoker.libs
}
sources = entrypoints
pkg_sources = entrypoints
if (defined(invoker.sources)) {
sources += invoker.sources
pkg_sources += invoker.sources
} else if (defined(invoker.pkg_dir)) {
list_script = rebase_path("build/ls.py", ".", mojo_sdk_root)
ls_sources = exec_script(list_script,
[
"--target-directory",
rebase_path(invoker.pkg_dir),
],
"list lines")
sources += ls_sources
pkg_sources += rebase_path(ls_sources, rebase_path(invoker.pkg_dir))
}
# We have to use foreach to set up outputs instead of rebase_path because
# GN doesn't like assignments to outputs that aren't obviously under
# $root_gen_dir somewhere.
outputs = []
foreach(s, pkg_sources) {
outputs += [ "$root_gen_dir/dart-pkg/${package_name}/$s" ]
}
outputs += [ stamp_file ]
inputs = [
list_mojoms_script,
script,
] + rebase_path(sources)
args = [
"--package-name",
package_name,
"--dart-sdk",
rebase_path(dart_sdk_root),
"--gen-directory",
rebase_path("$root_gen_dir/dart-gen"),
"--pkg-directory",
pkg_directory,
"--package-root",
package_root,
"--stamp-file",
rebase_path(stamp_file),
"--package-sources",
] + rebase_path(sources) + [ "--package-entrypoints" ] +
rebase_path(entrypoints) + [ "--mojom-sources" ] +
rebase_path(mojom_sources, "", mojo_sdk_root) +
[ "--sdk-ext-directories" ] + rebase_path(sdk_ext_directory) +
[ "--sdk-ext-files" ] + rebase_path(sdk_ext_files) +
[ "--sdk-ext-mappings" ] + sdk_ext_mappings
}
}
# This is the entrypoint for organizing Dart code for Mojo.
#
# For each file in |apps|, it makes a .mojo Mojo application using the dartx
# format as well as an assemblage of the app under $root_gen_dir/part-pkg for
# use in local development.
#
# For each file in |libs|, it invokes the Dart analyzer. All other sources go in
# |sources|. This should at least contain a 'pubspec.yaml' file. If no |apps|
# are defined, this rule makes the library package available to applications.
# The name of the package is taken from the 'pubspec.yaml' file. Even if a
# package will not be uploaded to pub, an attempt should be made not to conflict
# with the names of existing pub packages, for example by using the prefix
# 'mojo_dart_'.
#
# sources
# List of non-app and non-lib sources to include in the package. This
# should at least contain the pubspec.yaml for the package.
#
# apps (optional)
# List of Mojo application entrypoints containing a main() function.
# Each of these entrypoints will result in a .mojo Mojo application.
# See |output_name| for how this application is named.
#
# libs (optional)
# List of package entrypoints to pass to the analyzer. If none are
# defined, the analyzer is not run.
#
# strict (optional)
# If |apps| are specified, |strict| can be set to true to
# instruct the content handler to run the apps in Dart VM's strict
# compilation mode (with assertions and type-checks, etc.).
#
# app_name_override (optional)
# When |apps| are specified, this is the prefix to use for the
# name of the assembled .mojo file. The target name is used by default.
# For each entrypoint, the result is |output_name|_|entrypoint|.mojo. If
# the entrypoint is main.dart, the result is simply |output_name|.mojo.
#
# deps (optional)
# List of other dart_pkg targets for Dart packages imported by this
# dart_pkg, as well as the mojom targets needed by this dart_pkg.
#
# pkg_dir (optional)
# Directory containing the package sources. This overrides sources and
# entrypoints. The analyzer will not be run.
#
# datadeps (optional)
#
# sdk_ext_directory (optional)
# Directory containing sdk-ext .dart sources.
#
# sdk_ext_files (optional)
# List of sources to include in sdk-ext.
#
# sdk_ext_mappings (optional)
# Mappings for dart libraries that are part of of sdk_ext.
template("dart_pkg") {
if (defined(invoker.pkg_dir)) {
pubspec_yaml_path = rebase_path("pubspec.yaml", "", invoker.pkg_dir)
} else {
pubspec_yaml_path = rebase_path("pubspec.yaml")
}
dart_package_name_script =
rebase_path("mojo/public/tools/dart_package_name.py", ".", mojo_sdk_root)
dart_package_name = exec_script(dart_package_name_script,
[
"--pubspec",
pubspec_yaml_path,
],
"trim string",
[ pubspec_yaml_path ])
dart_pkg_target_name = "${target_name}_pkg_helper"
dart_pkg_helper(dart_pkg_target_name) {
package_name = dart_package_name
if (defined(invoker.sources)) {
sources = invoker.sources
}
if (defined(invoker.apps)) {
apps = invoker.apps
}
if (defined(invoker.libs)) {
libs = invoker.libs
}
if (defined(invoker.pkg_dir)) {
pkg_dir = invoker.pkg_dir
}
if (defined(invoker.deps)) {
deps = invoker.deps
}
if (defined(invoker.datadeps)) {
datadeps = invoker.datadeps
}
if (defined(invoker.sdk_ext_directory)) {
sdk_ext_directory = invoker.sdk_ext_directory
}
if (defined(invoker.sdk_ext_files)) {
sdk_ext_files = invoker.sdk_ext_files
}
if (defined(invoker.sdk_ext_mappings)) {
sdk_ext_mappings = invoker.sdk_ext_mappings
}
}
if (defined(invoker.apps)) {
pkg_name = target_name
if (defined(invoker.app_name_override)) {
pkg_name = invoker.app_name_override
}
pkg_helper_output_dir = "$root_gen_dir/dart-pkg/${dart_package_name}"
foreach(entrypoint, invoker.apps) {
entrypoint_name = get_path_info(entrypoint, "name")
dartx_target_name = "${pkg_name}_${entrypoint_name}"
dartx_output_name = dartx_target_name
if (entrypoint_name == "main") {
dartx_output_name = pkg_name
}
dartx_application(dartx_target_name) {
output_name = dartx_output_name
main_dart = rebase_path(entrypoint, "", pkg_helper_output_dir)
sources = rebase_path(invoker.sources, "", pkg_helper_output_dir)
deps = [
":$dart_pkg_target_name",
]
deps += invoker.deps
if (defined(invoker.strict)) {
strict = invoker.strict
}
}
}
}
group(target_name) {
deps = [
":$dart_pkg_target_name",
]
if (defined(invoker.apps)) {
pkg_name = target_name
if (defined(invoker.app_name_override)) {
pkg_name = invoker.app_name_override
}
foreach(entrypoint, invoker.apps) {
entrypoint_name = get_path_info(entrypoint, "name")
dartx_target_name = "${pkg_name}_${entrypoint_name}"
deps += [ ":$dartx_target_name" ]
}
}
}
}