Skip to content

Commit

Permalink
Don't use Dart's application_snapshot() rule (flutter#28361)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderso authored Sep 16, 2021
1 parent 86d06cc commit e0cbd61
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 119 deletions.
178 changes: 127 additions & 51 deletions build/dart/rules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import("//flutter/common/config.gni")
import("//third_party/dart/build/dart/dart_action.gni")

# Creates a dart kernel (dill) file suitable for use with gen_snapshot, as well
# as the app-jit, aot-elf, or aot-assembly snapshot for Android or iOS.
# as the app-jit, aot-elf, or aot-assembly snapshot for targeting Flutter on
# Android or iOS.
#
# Invoker must supply dart_main and package_config. Invoker may optionally
# supply aot as a boolean and product as a boolean.
template("dart_snapshot") {
template("flutter_snapshot") {
assert(!is_fuchsia)
assert(defined(invoker.main_dart), "main_dart is a required parameter.")
assert(defined(invoker.package_config),
Expand Down Expand Up @@ -168,7 +169,129 @@ template("dart_snapshot") {
}
}

template("dart_pkg_helper") {
# Creates an app-jit snapshot for a command-line Dart program based on a
# training run.
#
# Parameters:
# main_dart (required):
# The entrypoint to the Dart application.
#
# training_args (required):
# Arguments to pass to the Dart application for the training run.
#
# vm_args (optional):
# Additional arguments to the Dart VM.
#
# deps (optional):
# Any build dependencies.
#
# package_config (required):
# The .packages file for the app. Defaults to the $_dart_root/.packages.
#
# output (optional):
# Overrides the full output path.
#
# snapshot_kind (optional)
# Either an "app-jit" snapshot (default) or a "kernel" snapshot
template("application_snapshot") {
assert(defined(invoker.main_dart), "Must specify 'main_dart'")
assert(defined(invoker.training_args), "Must specify 'training_args'")
assert(defined(invoker.package_config), "Must specify 'package_config'")

main_dart = invoker.main_dart
training_args = invoker.training_args
package_config = rebase_path(invoker.package_config)
name = target_name

extra_deps = []
if (defined(invoker.deps)) {
extra_deps += invoker.deps
}
extra_inputs = [ main_dart ]
if (defined(invoker.inputs)) {
extra_inputs += invoker.inputs
}
output = "$root_gen_dir/$name.dart.snapshot"
if (defined(invoker.output)) {
output = invoker.output
}

depfile = output + ".d"
abs_depfile = rebase_path(depfile)
abs_output = rebase_path(output)
rel_output = rebase_path(output, root_build_dir)
snapshot_vm_args = [
"--disable-dart-dev",
"--deterministic",
"--packages=$package_config",
"--snapshot=$abs_output",
"--snapshot-depfile=$abs_depfile",
"--depfile-output-filename=$rel_output",
]
if (defined(invoker.vm_args)) {
snapshot_vm_args += invoker.vm_args
}

snapshot_kind = "app-jit"
if (target_cpu != host_cpu) {
snapshot_kind = "kernel"
}
if (defined(invoker.snapshot_kind)) {
snapshot_kind = invoker.snapshot_kind
}
if (snapshot_kind == "kernel") {
snapshot_vm_args += [ "--snapshot-kind=kernel" ]
} else if (snapshot_kind == "app-jit") {
snapshot_vm_args += [ "--snapshot-kind=app-jit" ]
} else {
assert(false, "Bad snapshot_kind: '$snapshot_kind'")
}

if (flutter_prebuilt_dart_sdk) {
action(target_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
deps = extra_deps + [ "//flutter:dart_sdk" ]
script = "//build/gn_run_binary.py"
inputs = extra_inputs
outputs = [ output ]
depfile = depfile
pool = "//flutter/build/dart:dart_pool"

ext = ""
if (is_win) {
ext = ".exe"
}
dart = rebase_path("$host_prebuilt_dart_sdk/bin/dart$ext", root_out_dir)

args = [ dart ]
args += snapshot_vm_args
args += [ rebase_path(main_dart) ]
args += training_args
}
} else {
dart_action(target_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
script = main_dart
pool = "//flutter/build/dart:dart_pool"
deps = extra_deps
inputs = extra_inputs
outputs = [ output ]
depfile = depfile
vm_args = snapshot_vm_args
args = training_args
}
}
}

template("_dart_pkg_helper") {
assert(defined(invoker.package_name))
package_name = invoker.package_name
pkg_directory = rebase_path("$root_gen_dir/dart-pkg")
Expand Down Expand Up @@ -284,7 +407,7 @@ template("dart_pkg") {
[ pubspec_yaml_path ])

dart_pkg_target_name = "${target_name}_pkg_helper"
dart_pkg_helper(dart_pkg_target_name) {
_dart_pkg_helper(dart_pkg_target_name) {
package_name = dart_package_name
if (defined(invoker.sources)) {
sources = invoker.sources
Expand Down Expand Up @@ -345,50 +468,3 @@ template("dart_pkg") {
}
}
}

# 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
}
}
}
5 changes: 2 additions & 3 deletions flutter_frontend_server/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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("//third_party/dart/utils/application_snapshot.gni")
import("//flutter/build/dart/rules.gni")

frontend_server_files =
exec_script("//third_party/dart/tools/list_dart_files.py",
Expand All @@ -23,9 +23,8 @@ frontend_server_files +=
application_snapshot("frontend_server") {
main_dart = "bin/starter.dart"
deps = [ "//flutter/lib/snapshot:kernel_platform_files" ]
pool = "//flutter/build/dart:dart_pool"

dot_packages = rebase_path(".dart_tool/package_config.json")
package_config = rebase_path(".dart_tool/package_config.json")
flutter_patched_sdk = rebase_path("$root_out_dir/flutter_patched_sdk")
training_args = [
"--train",
Expand Down
15 changes: 8 additions & 7 deletions flutter_frontend_server/test/to_string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import 'package:litetest/litetest.dart';
import 'package:path/path.dart' as path;

Future<void> main(List<String> args) async {
if (args.length != 2) {
stderr.writeln('The first argument must be the path to the frontend server dill.');
stderr.writeln('The second argument must be the path to the flutter_patched_sdk');
if (args.length != 3) {
stderr.writeln('The first argument must be the path to the build output.');
stderr.writeln('The second argument must be the path to the frontend server dill.');
stderr.writeln('The third argument must be the path to the flutter_patched_sdk');
exit(-1);
}

final String dart = Platform.resolvedExecutable;
final String frontendServer = args[0];
final String sdkRoot = args[1];
final String buildDir = args[0];
final String frontendServer = args[1];
final String sdkRoot = args[2];
final String basePath = path.canonicalize(path.join(path.dirname(Platform.script.path), '..'));
final String fixtures = path.join(basePath, 'test', 'fixtures');
final String mainDart = path.join(fixtures, 'lib', 'main.dart');
final String packageConfig = path.join(fixtures, '.dart_tool', 'package_config.json');
final String regularDill = path.join(fixtures, 'toString.dill');
final String transformedDill = path.join(fixtures, 'toStringTransformed.dill');


void _checkProcessResult(ProcessResult result) {
if (result.exitCode != 0) {
stdout.writeln(result.stdout);
Expand All @@ -45,7 +46,7 @@ Future<void> main(List<String> args) async {
final ProcessResult runResult = Process.runSync(dart, <String>[regularDill]);
_checkProcessResult(runResult);
String paintString = '"Paint.toString":"Paint(Color(0xffffffff))"';
if (const bool.fromEnvironment('dart.vm.product', defaultValue: false)) {
if (buildDir.contains('release')) {
paintString = '"Paint.toString":"Instance of \'Paint\'"';
}

Expand Down
26 changes: 17 additions & 9 deletions shell/platform/fuchsia/dart/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//flutter/build/dart/rules.gni")
import("//flutter/tools/fuchsia/dart/dart_library.gni")
import("//third_party/dart/build/dart/dart_action.gni")
import("//third_party/dart/utils/application_snapshot.gni")
import("//third_party/dart/utils/compile_platform.gni")

application_snapshot("kernel_compiler") {
main_dart = "compiler.dart"

deps = [ "../flutter/kernel:kernel_platform_files($host_toolchain)" ]

dot_packages =
package_config =
rebase_path("//third_party/dart/.dart_tool/package_config.json")

training_args = [
Expand All @@ -39,14 +37,24 @@ application_snapshot("kernel_compiler") {
inputs = kernel_compiler_files
}

application_snapshot("_list_libraries_kernel") {
visibility = [ ":*" ]
snapshot_kind = "kernel"
main_dart = "//third_party/dart/pkg/vm/bin/list_libraries.dart"
package_config =
rebase_path("//third_party/dart/.dart_tool/package_config.json")
training_args = []
output = "$target_gen_dir/list_libraries.dart.dill"
}

application_snapshot("list_libraries") {
deps = [ ":_list_libraries_kernel" ]
main_dart = "//third_party/dart/pkg/vm/bin/list_libraries.dart"
package_config =
rebase_path("//third_party/dart/.dart_tool/package_config.json")
training_args = [
# train against the dill file which is generated for this snapshot. If this build file
# changes location this path will need to be updated.
rebase_path(
root_gen_dir +
"/flutter/shell/platform/fuchsia/dart/list_libraries.dart.dill"),
# train against the dill file which is generated for this snapshot.
rebase_path("$target_gen_dir/list_libraries.dart.dill"),
]
}

Expand Down
2 changes: 1 addition & 1 deletion testing/android_background_image/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import("//flutter/testing/rules/runtime_mode.gni")

assert(is_android)

dart_snapshot("android_background_image_snapshot") {
flutter_snapshot("android_background_image_snapshot") {
main_dart = "lib/main.dart"
package_config = ".dart_tool/package_config.json"
}
Expand Down
Loading

0 comments on commit e0cbd61

Please sign in to comment.