blob: dcafa6caf46170d1c8d6548f16ba3e16dd861ab8 [file] [log] [blame] [edit]
# Copyright 2013 The Flutter 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("//build/module_args/dart.gni")
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"
entries_file = "$root_gen_dir/dart-pkg/${package_name}.entries"
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
}
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("//flutter/build/dart/tools/dart_pkg.py", ".", "//")
entrypoints = []
if (defined(invoker.apps)) {
foreach(app, invoker.apps) {
entrypoints += [ app[1] ]
}
}
if (defined(invoker.libs)) {
entrypoints += invoker.libs
}
sources = entrypoints
extra_flags = []
if (defined(invoker.sources)) {
sources += invoker.sources
} else if (defined(invoker.pkg_dir)) {
list_script = rebase_path("//flutter/build/dart/tools/ls.py", ".", "//")
extra_flags += [ "--read_only" ]
ls_sources = exec_script(list_script,
[
"--target-directory",
rebase_path(invoker.pkg_dir),
],
"list lines")
sources += ls_sources
}
# 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 = [
"$root_gen_dir/dart-pkg/${package_name}",
"$root_gen_dir/dart-pkg/${package_name}/pubspec.yaml",
"$root_gen_dir/dart-pkg/packages/${package_name}",
stamp_file,
]
inputs = [ script ] + rebase_path(sources)
args = [
"--package-name",
package_name,
"--dart-sdk",
rebase_path(dart_sdk_root),
"--pkg-directory",
pkg_directory,
"--package-root",
package_root,
"--stamp-file",
rebase_path(stamp_file),
"--entries-file",
rebase_path(entries_file),
"--package-sources",
] + rebase_path(sources) + [ "--package-entrypoints" ] +
rebase_path(entrypoints) + [ "--sdk-ext-directories" ] +
rebase_path(sdk_ext_directory) + [ "--sdk-ext-files" ] +
rebase_path(sdk_ext_files) + [ "--sdk-ext-mappings" ] +
sdk_ext_mappings + extra_flags
}
}
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("//flutter/build/dart/tools/dart_package_name.py", ".", "//")
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_helper_output_dir = "$root_gen_dir/dart-pkg/${dart_package_name}"
foreach(app, invoker.apps) {
app_name = app[0]
app_entrypoint = app[1]
dartx_output_name = app_name
dartx_application("${app_name}_dart_app") {
output_name = dartx_output_name
main_dart = rebase_path(app_entrypoint, "", pkg_helper_output_dir)
sources = [ "$root_gen_dir/dart-pkg/${dart_package_name}.stamp" ]
deps = [ ":$dart_pkg_target_name" ]
deps += invoker.deps
if (defined(invoker.strict)) {
strict = invoker.strict
}
}
}
}
group(target_name) {
public_deps = [ ":$dart_pkg_target_name" ]
if (defined(invoker.apps)) {
foreach(app, invoker.apps) {
app_name = app[0]
dartx_target_name = "${app_name}_dart_app"
deps += [ ":$dartx_target_name" ]
}
}
}
}
# Used to create dart_pkgs from a directory populated by fetch_dart_packages.py
#
# This build rule will result in a many packages under $root_gen_dir/dart-pkg/.
#
# base_dir (optional)
# Directory populated by fetch_dart_packages.py. Defaults to BUILD.gn
# directory.
template("dart_packages") {
base_dir = "."
if (defined(invoker.base_dir)) {
base_dir = invoker.base_dir
}
# Determine list of packages.
list_script =
rebase_path("build/dart/dart/tools/fetch_dart_packages.py", ".", "//")
packages = exec_script(list_script,
[
"--directory",
rebase_path(base_dir),
"--list",
],
"list lines",
[
rebase_path("pubspec.yaml"),
rebase_path("pubspec.lock"),
])
# Generate dart_pkg for each package.
foreach(package_dir, packages) {
dart_pkg("$package_dir") {
pkg_dir = rebase_path("$package_dir")
}
}
# Generate target group.
group(target_name) {
deps = []
foreach(package_dir, packages) {
deps += [ ":$package_dir" ]
}
if (defined(invoker.deps)) {
deps += invoker.deps
}
}
}