From 527e2548938ac073d1ee4c0423e1198faaefdd5d Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Wed, 31 Jul 2019 20:41:11 -0700 Subject: [PATCH] [fuchsia] Kernel compiler is now ready (flutter/engine#10309) Also fixes the architecture of the bundled SO files. --- .../ci/licenses_golden/licenses_flutter | 1 + .../flutter/shell/platform/fuchsia/BUILD.gn | 1 + .../shell/platform/fuchsia/dart/BUILD.gn | 37 ++++++ .../shell/platform/fuchsia/dart/compiler.dart | 124 ++++++++++++++++++ .../tools/fuchsia/build_fuchsia_artifacts.py | 22 ++-- .../src/flutter/tools/fuchsia/common_libs.gni | 2 +- 6 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn create mode 100644 engine/src/flutter/shell/platform/fuchsia/dart/compiler.dart diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 59dffca2dd..5af616fd50 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -836,6 +836,7 @@ FILE: ../../../flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/natives.cc FILE: ../../../flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/natives.h FILE: ../../../flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/system.cc FILE: ../../../flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/system.h +FILE: ../../../flutter/shell/platform/fuchsia/dart/compiler.dart FILE: ../../../flutter/shell/platform/fuchsia/dart_runner/builtin_libraries.cc FILE: ../../../flutter/shell/platform/fuchsia/dart_runner/builtin_libraries.h FILE: ../../../flutter/shell/platform/fuchsia/dart_runner/dart_component_controller.cc diff --git a/engine/src/flutter/shell/platform/fuchsia/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/BUILD.gn index 3ca5996fb0..48da704a26 100644 --- a/engine/src/flutter/shell/platform/fuchsia/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/BUILD.gn @@ -17,6 +17,7 @@ if (using_fuchsia_sdk) { group("fuchsia") { deps = [ + "dart:kernel_compiler", "dart_runner:$dart_runner_target", "flutter:$flutter_runner_target", ] diff --git a/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn new file mode 100644 index 0000000000..2242fb4775 --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn @@ -0,0 +1,37 @@ +# 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. + +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", + ] + + dot_packages = rebase_path("//third_party/dart/.packages") + + training_args = [ "--train" ] + + kernel_compiler_files = + exec_script("//third_party/dart/tools/list_dart_files.py", + [ + "absolute", + rebase_path("."), + ], + "list lines") + + kernel_compiler_files += + exec_script("//third_party/dart/tools/list_dart_files.py", + [ + "absolute", + rebase_path("//third_party/dart/pkg"), + ], + "list lines") + + inputs = kernel_compiler_files +} diff --git a/engine/src/flutter/shell/platform/fuchsia/dart/compiler.dart b/engine/src/flutter/shell/platform/fuchsia/dart/compiler.dart new file mode 100644 index 0000000000..b276e311df --- /dev/null +++ b/engine/src/flutter/shell/platform/fuchsia/dart/compiler.dart @@ -0,0 +1,124 @@ +// 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. + +import 'dart:async'; +import 'dart:io'; + +import 'package:args/args.dart'; +import 'package:crypto/crypto.dart'; + +import 'package:vm/kernel_front_end.dart' + show createCompilerArgParser, runCompiler, successExitCode; + +final ArgParser _argParser = createCompilerArgParser() + ..addFlag('train', + help: 'Run through sample command line to produce snapshot', + negatable: false) + ..addOption('component-name', help: 'Name of the component') + ..addOption('data-dir', + help: 'Name of the subdirectory of //data for output files') + ..addOption('manifest', help: 'Path to output Fuchsia package manifest'); + +String _usage = ''' +Usage: compiler [options] input.dart + +Options: +${_argParser.usage} +'''; + +Future main(List args) async { + ArgResults options; + + try { + options = _argParser.parse(args); + + if (options['train']) { + final Directory temp = + Directory.systemTemp.createTempSync('train_kernel_compiler'); + try { + options = _argParser.parse([ + '--manifest=flutter', + '--data-dir=${temp.absolute}', + ]); + + await runCompiler(options, _usage); + return; + } finally { + temp.deleteSync(recursive: true); + } + } + + if (!options.rest.isNotEmpty) { + throw Exception('Must specify input.dart'); + } + } on Exception catch (error) { + print('ERROR: $error\n'); + print(_usage); + exitCode = 1; + return; + } + + final compilerExitCode = await runCompiler(options, _usage); + if (compilerExitCode != successExitCode) { + exitCode = compilerExitCode; + return; + } + + final String output = options['output']; + final String dataDir = options.options.contains('component-name') + ? options['component-name'] + : options['data-dir']; + final String manifestFilename = options['manifest']; + + if (manifestFilename != null) { + await createManifest(manifestFilename, dataDir, output); + } +} + +Future createManifest( + String packageManifestFilename, String dataDir, String output) async { + List packages = await File('$output-packages').readAsLines(); + + // Make sure the 'main' package is the last (convention with package loader). + packages.remove('main'); + packages.add('main'); + + final IOSink packageManifest = File(packageManifestFilename).openWrite(); + + final String kernelListFilename = '$packageManifestFilename.dilplist'; + final IOSink kernelList = File(kernelListFilename).openWrite(); + for (String package in packages) { + final String filenameInPackage = '$package.dilp'; + final String filenameInBuild = '$output-$package.dilp'; + packageManifest + .write('data/$dataDir/$filenameInPackage=$filenameInBuild\n'); + kernelList.write('$filenameInPackage\n'); + } + await kernelList.close(); + + final String frameworkVersionFilename = + '$packageManifestFilename.frameworkversion'; + final IOSink frameworkVersion = File(frameworkVersionFilename).openWrite(); + for (String package in [ + 'collection', + 'flutter', + 'meta', + 'typed_data', + 'vector_math' + ]) { + Digest digest; + if (packages.contains(package)) { + final filenameInBuild = '$output-$package.dilp'; + final bytes = await File(filenameInBuild).readAsBytes(); + digest = sha256.convert(bytes); + } + frameworkVersion.write('$package=$digest\n'); + } + await frameworkVersion.close(); + + packageManifest.write('data/$dataDir/app.dilplist=$kernelListFilename\n'); + packageManifest + .write('data/$dataDir/app.frameworkversion=$frameworkVersionFilename\n'); + await packageManifest.close(); +} diff --git a/engine/src/flutter/tools/fuchsia/build_fuchsia_artifacts.py b/engine/src/flutter/tools/fuchsia/build_fuchsia_artifacts.py index 734c568d89..1db6661bad 100755 --- a/engine/src/flutter/tools/fuchsia/build_fuchsia_artifacts.py +++ b/engine/src/flutter/tools/fuchsia/build_fuchsia_artifacts.py @@ -91,17 +91,22 @@ def FindFile(name, path): return os.path.join(root, name) +def FindFileAndCopyTo(file_name, source, dest_parent, dst_name=None): + found = FindFile(file_name, source) + if not dst_name: + dst_name = file_name + if found: + dst_path = os.path.join(dest_parent, dst_name) + CopyPath(found, dst_path) + + def CopyGenSnapshotIfExists(source, destination): source_root = os.path.join(_out_dir, source) destination_base = os.path.join(destination, 'dart_binaries') - gen_snapshot = FindFile('gen_snapshot', source_root) - gen_snapshot_product = FindFile('gen_snapshot_product', source_root) - if gen_snapshot: - dst_path = os.path.join(destination_base, 'gen_snapshot') - CopyPath(gen_snapshot, dst_path) - if gen_snapshot_product: - dst_path = os.path.join(destination_base, 'gen_snapshot_product') - CopyPath(gen_snapshot_product, dst_path) + FindFileAndCopyTo('gen_snapshot', source_root, destination_base) + FindFileAndCopyTo('gen_snapshot_product', source_root, destination_base) + FindFileAndCopyTo('kernel_compiler.dart.snapshot', source_root, + destination_base, 'kernel_compiler.snapshot') def CopyToBucketWithMode(source, destination, aot, product, runner_type): @@ -157,6 +162,7 @@ def ProcessCIPDPakcage(upload, engine_version): subprocess.check_call(command, cwd=_bucket_directory) + def GetRunnerTarget(runner_type, product, aot): base = 'flutter/shell/platform/fuchsia/%s:' % runner_type if 'dart' in runner_type: diff --git a/engine/src/flutter/tools/fuchsia/common_libs.gni b/engine/src/flutter/tools/fuchsia/common_libs.gni index c3f88fbb15..9edcd605d6 100644 --- a/engine/src/flutter/tools/fuchsia/common_libs.gni +++ b/engine/src/flutter/tools/fuchsia/common_libs.gni @@ -4,7 +4,7 @@ import("$flutter_root/tools/fuchsia/clang.gni") -fuchsia_sdk_base = "//fuchsia/sdk/$host_os/arch/$host_cpu" +fuchsia_sdk_base = "//fuchsia/sdk/$host_os/arch/$target_cpu" fuchsia_sdk_lib = "$fuchsia_sdk_base/lib" sysroot_lib = "$fuchsia_sdk_base/sysroot/lib" sysroot_dist_lib = "$fuchsia_sdk_base/sysroot/dist/lib"