Import Dart rules from buildroot, to make them available in Fuchsia. (flutter/engine#3246)

This also removes a reference to mojo_gni.
This commit is contained in:
P.Y. Laligand
2016-11-18 15:24:14 -08:00
committed by GitHub
parent 6e603372ba
commit 3a1caa55b3
5 changed files with 572 additions and 2 deletions

View File

@@ -0,0 +1,239 @@
# 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("//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/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) {
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
}
}
}

View File

@@ -0,0 +1,38 @@
#!/usr/bin/python
# 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 script outputs the package name specified in the pubspec.yaml"""
import argparse
import os
import sys
# TODO(johnmccutchan): Use the yaml package to parse.
def PackageName(line):
assert line.startswith("name:")
return line.split(":")[1].strip()
def main(pubspec_file):
source_file = open(pubspec_file, "r")
for line in source_file:
if line.startswith("name:"):
print(PackageName(line))
return 0
source_file.close()
# Couldn't find it.
return -1
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="This script outputs the package name specified in the"
"pubspec.yaml")
parser.add_argument("--pubspec",
dest="pubspec_file",
metavar="<pubspec-file>",
type=str,
required=True,
help="Path to pubspec file")
args = parser.parse_args()
sys.exit(main(args.pubspec_file))

View File

@@ -0,0 +1,293 @@
#!/usr/bin/env python
#
# 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.
"""Utility for dart_pkg and dart_pkg_app rules"""
import argparse
import errno
import json
import os
import shutil
import subprocess
import sys
USE_LINKS = sys.platform != "win32"
DART_ANALYZE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"dart_analyze.py")
def dart_filter(path):
if os.path.isdir(path):
return True
_, ext = os.path.splitext(path)
# .dart includes '.mojom.dart'
return ext == '.dart'
def ensure_dir_exists(path):
abspath = os.path.abspath(path)
if not os.path.exists(abspath):
os.makedirs(abspath)
def has_pubspec_yaml(paths):
for path in paths:
_, filename = os.path.split(path)
if 'pubspec.yaml' == filename:
return True
return False
def link(from_root, to_root):
ensure_dir_exists(os.path.dirname(to_root))
try:
os.unlink(to_root)
except OSError as e:
if e.errno == errno.ENOENT:
pass
try:
os.symlink(from_root, to_root)
except OSError as e:
if e.errno == errno.EEXIST:
pass
def copy(from_root, to_root, filter_func=None):
if not os.path.exists(from_root):
return
if os.path.isfile(from_root):
ensure_dir_exists(os.path.dirname(to_root))
shutil.copy(from_root, to_root)
return
ensure_dir_exists(to_root)
for root, dirs, files in os.walk(from_root):
# filter_func expects paths not names, so wrap it to make them absolute.
wrapped_filter = None
if filter_func:
wrapped_filter = lambda name: filter_func(os.path.join(root, name))
for name in filter(wrapped_filter, files):
from_path = os.path.join(root, name)
root_rel_path = os.path.relpath(from_path, from_root)
to_path = os.path.join(to_root, root_rel_path)
to_dir = os.path.dirname(to_path)
if not os.path.exists(to_dir):
os.makedirs(to_dir)
shutil.copy(from_path, to_path)
dirs[:] = filter(wrapped_filter, dirs)
def copy_or_link(from_root, to_root, filter_func=None):
if USE_LINKS:
link(from_root, to_root)
else:
copy(from_root, to_root, filter_func)
def link_if_possible(from_root, to_root):
if USE_LINKS:
link(from_root, to_root)
def remove_if_exists(path):
try:
os.remove(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise
def list_files(from_root, filter_func=None):
file_list = []
for root, dirs, files in os.walk(from_root):
# filter_func expects paths not names, so wrap it to make them absolute.
wrapped_filter = None
if filter_func:
wrapped_filter = lambda name: filter_func(os.path.join(root, name))
for name in filter(wrapped_filter, files):
path = os.path.join(root, name)
file_list.append(path)
dirs[:] = filter(wrapped_filter, dirs)
return file_list
def remove_broken_symlink(path):
try:
link_path = os.readlink(path)
except OSError as e:
# Path was not a symlink.
if e.errno == errno.EINVAL:
pass
else:
if not os.path.exists(link_path):
remove_if_exists(path)
def remove_broken_symlinks(root_dir):
for current_dir, _, child_files in os.walk(root_dir):
for filename in child_files:
path = os.path.join(current_dir, filename)
remove_broken_symlink(path)
def analyze_entrypoints(dart_sdk, package_root, entrypoints):
cmd = [ "python", DART_ANALYZE ]
cmd.append("--dart-sdk")
cmd.append(dart_sdk)
cmd.append("--entrypoints")
cmd.extend(entrypoints)
cmd.append("--package-root")
cmd.append(package_root)
cmd.append("--no-hints")
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('Failed analyzing %s' % entrypoints)
print(e.output)
return e.returncode
return 0
def main():
parser = argparse.ArgumentParser(description='Generate a dart-pkg')
parser.add_argument('--dart-sdk',
action='store',
metavar='dart_sdk',
help='Path to the Dart SDK.')
parser.add_argument('--package-name',
action='store',
metavar='package_name',
help='Name of package',
required=True)
parser.add_argument('--pkg-directory',
metavar='pkg_directory',
help='Directory where dart_pkg should go',
required=True)
parser.add_argument('--package-root',
metavar='package_root',
help='packages/ directory',
required=True)
parser.add_argument('--stamp-file',
metavar='stamp_file',
help='timestamp file',
required=True)
parser.add_argument('--entries-file',
metavar='entries_file',
help='script entries file',
required=True)
parser.add_argument('--package-sources',
metavar='package_sources',
help='Package sources',
nargs='+')
parser.add_argument('--package-entrypoints',
metavar='package_entrypoints',
help='Package entry points for analyzer',
nargs='*',
default=[])
parser.add_argument('--sdk-ext-directories',
metavar='sdk_ext_directories',
help='Directory containing .dart sources',
nargs='*',
default=[])
parser.add_argument('--sdk-ext-files',
metavar='sdk_ext_files',
help='List of .dart files that are part of of sdk_ext.',
nargs='*',
default=[])
parser.add_argument('--sdk-ext-mappings',
metavar='sdk_ext_mappings',
help='Mappings for SDK extension libraries.',
nargs='*',
default=[])
parser.add_argument('--read_only',
action='store_true',
dest='read_only',
help='Package is a read only package.',
default=False)
args = parser.parse_args()
# We must have a pubspec.yaml.
assert has_pubspec_yaml(args.package_sources)
target_dir = os.path.join(args.pkg_directory, args.package_name)
target_packages_dir = os.path.join(target_dir, 'packages')
lib_path = os.path.join(target_dir, "lib")
ensure_dir_exists(lib_path)
mappings = {}
for mapping in args.sdk_ext_mappings:
library, path = mapping.split(',', 1)
mappings[library] = '../sdk_ext/%s' % path
sdkext_path = os.path.join(lib_path, '_sdkext')
if mappings:
with open(sdkext_path, 'w') as stream:
json.dump(mappings, stream, sort_keys=True,
indent=2, separators=(',', ': '))
else:
remove_if_exists(sdkext_path)
# Copy or symlink package sources into pkg directory.
common_source_prefix = os.path.dirname(os.path.commonprefix(
args.package_sources))
for source in args.package_sources:
relative_source = os.path.relpath(source, common_source_prefix)
target = os.path.join(target_dir, relative_source)
copy_or_link(source, target)
entrypoint_targets = []
for source in args.package_entrypoints:
relative_source = os.path.relpath(source, common_source_prefix)
target = os.path.join(target_dir, relative_source)
copy_or_link(source, target)
entrypoint_targets.append(target)
# Copy sdk-ext sources into pkg directory
sdk_ext_dir = os.path.join(target_dir, 'sdk_ext')
for directory in args.sdk_ext_directories:
sdk_ext_sources = list_files(directory, dart_filter)
common_prefix = os.path.commonprefix(sdk_ext_sources)
for source in sdk_ext_sources:
relative_source = os.path.relpath(source, common_prefix)
target = os.path.join(sdk_ext_dir, relative_source)
copy_or_link(source, target)
common_source_prefix = os.path.dirname(os.path.commonprefix(
args.sdk_ext_files))
for source in args.sdk_ext_files:
relative_source = os.path.relpath(source, common_source_prefix)
target = os.path.join(sdk_ext_dir, relative_source)
copy_or_link(source, target)
# Symlink packages/
package_path = os.path.join(args.package_root, args.package_name)
link(lib_path, package_path)
# Link dart-pkg/$package/packages to dart-pkg/packages
link_if_possible(args.package_root, target_packages_dir)
# Remove any broken symlinks in target_dir and package root.
remove_broken_symlinks(target_dir)
remove_broken_symlinks(args.package_root)
# If any entrypoints are defined, write them to disk so that the analyzer
# test can find them.
with open(args.entries_file, 'w') as f:
for entrypoint in entrypoint_targets:
f.write(entrypoint + '\n')
# Write stamp file.
with open(args.stamp_file, 'w'):
pass
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -3,10 +3,10 @@
# found in the LICENSE file.
import("//dart/sdk/lib/rules.gni")
import("//flutter/build/dart/rules.gni")
import("//flutter/lib/jni/dart_jni.gni")
import("//flutter/lib/ui/dart_ui.gni")
import("//flutter/sky/engine/core/core.gni")
import("//build/dart/rules.gni")
copy("copy_sky_engine_authors") {
sources = [

View File

@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/dart/rules.gni")
import("//flutter/build/dart/rules.gni")
copy("copy_sky_services_license") {
sources = [