From 2c0329a103860cb2c14eead0d4e1aafc8e5f9c1b Mon Sep 17 00:00:00 2001 From: Stanislav Baranov Date: Thu, 16 Aug 2018 08:46:57 -0700 Subject: [PATCH] Replace flutter --build-snapshot with --precompile that takes input (#20574) --- packages/flutter_tools/gradle/flutter.gradle | 16 ++++++++-------- .../flutter_tools/lib/src/android/gradle.dart | 4 ++-- packages/flutter_tools/lib/src/base/build.dart | 5 +++-- packages/flutter_tools/lib/src/build_info.dart | 8 ++++---- packages/flutter_tools/lib/src/bundle.dart | 15 ++++++++------- .../lib/src/commands/build_bundle.dart | 13 ++++++++----- packages/flutter_tools/lib/src/commands/run.dart | 11 +++++++---- .../lib/src/runner/flutter_command.dart | 15 ++++++++++++--- packages/flutter_tools/test/base/build_test.dart | 9 +++++++++ 9 files changed, 61 insertions(+), 35 deletions(-) diff --git a/packages/flutter_tools/gradle/flutter.gradle b/packages/flutter_tools/gradle/flutter.gradle index 5b25cc91fb..f2e9280979 100644 --- a/packages/flutter_tools/gradle/flutter.gradle +++ b/packages/flutter_tools/gradle/flutter.gradle @@ -300,9 +300,9 @@ class FlutterPlugin implements Plugin { if (project.hasProperty('track-widget-creation')) { trackWidgetCreationValue = project.property('track-widget-creation').toBoolean() } - Boolean buildSnapshotValue = false - if (project.hasProperty('build-snapshot')) { - buildSnapshotValue = project.property('build-snapshot').toBoolean() + String compilationTraceFilePathValue = null + if (project.hasProperty('precompile')) { + compilationTraceFilePathValue = project.property('precompile') } String extraFrontEndOptionsValue = null if (project.hasProperty('extra-front-end-options')) { @@ -346,7 +346,7 @@ class FlutterPlugin implements Plugin { fileSystemRoots fileSystemRootsValue fileSystemScheme fileSystemSchemeValue trackWidgetCreation trackWidgetCreationValue - buildSnapshot buildSnapshotValue + compilationTraceFilePath compilationTraceFilePathValue buildSharedLibrary buildSharedLibraryValue targetPlatform targetPlatformValue sourceDir project.file(project.flutter.source) @@ -396,7 +396,7 @@ abstract class BaseFlutterTask extends DefaultTask { @Optional @Input Boolean trackWidgetCreation @Optional @Input - Boolean buildSnapshot + String compilationTraceFilePath @Optional @Input Boolean buildSharedLibrary @Optional @Input @@ -500,8 +500,8 @@ abstract class BaseFlutterTask extends DefaultTask { if (trackWidgetCreation) { args "--track-widget-creation" } - if (buildSnapshot) { - args "--build-snapshot" + if (compilationTraceFilePath != null) { + args "--precompile", compilationTraceFilePath } if (extraFrontEndOptions != null) { args "--extra-front-end-options", "${extraFrontEndOptions}" @@ -549,7 +549,7 @@ class FlutterTask extends BaseFlutterTask { include "flutter_assets/**" // the working dir and its files - if (buildMode != 'debug' || buildSnapshot) { + if (buildMode != 'debug' || compilationTraceFilePath) { if (buildSharedLibrary) { include "app.so" } else { diff --git a/packages/flutter_tools/lib/src/android/gradle.dart b/packages/flutter_tools/lib/src/android/gradle.dart index e48809d262..979ea36719 100644 --- a/packages/flutter_tools/lib/src/android/gradle.dart +++ b/packages/flutter_tools/lib/src/android/gradle.dart @@ -357,8 +357,8 @@ Future _buildGradleProjectV2( command.add('-Ppreview-dart-2=true'); if (buildInfo.trackWidgetCreation) command.add('-Ptrack-widget-creation=true'); - if (buildInfo.buildSnapshot) - command.add('-Pbuild-snapshot=true'); + if (buildInfo.compilationTraceFilePath != null) + command.add('-Pprecompile=${buildInfo.compilationTraceFilePath}'); if (buildInfo.extraFrontEndOptions != null) command.add('-Pextra-front-end-options=${buildInfo.extraFrontEndOptions}'); if (buildInfo.extraGenSnapshotOptions != null) diff --git a/packages/flutter_tools/lib/src/base/build.dart b/packages/flutter_tools/lib/src/base/build.dart index 10e75f0fd3..47866fe2aa 100644 --- a/packages/flutter_tools/lib/src/base/build.dart +++ b/packages/flutter_tools/lib/src/base/build.dart @@ -427,6 +427,7 @@ class CoreJITSnapshotter { @required String mainPath, @required String packagesPath, @required String outputPath, + @required String compilationTraceFilePath, List extraGenSnapshotOptions = const [], }) async { if (!_isValidCoreJitPlatform(platform)) { @@ -437,7 +438,7 @@ class CoreJITSnapshotter { final Directory outputDir = fs.directory(outputPath); outputDir.createSync(recursive: true); - final List inputPaths = [mainPath]; + final List inputPaths = [mainPath, compilationTraceFilePath]; final Set outputPaths = new Set(); final String depfilePath = fs.path.join(outputDir.path, 'snapshot.d'); @@ -466,7 +467,7 @@ class CoreJITSnapshotter { '--isolate_snapshot_data=$isolateSnapshotData', '--vm_snapshot_instructions=$vmSnapshotInstructions', '--isolate_snapshot_instructions=$isolateSnapshotInstructions', - '--load_compilation_trace=trace.txt', + '--load_compilation_trace=$compilationTraceFilePath', ]); if (platform == TargetPlatform.android_arm) { diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index 28a4033242..50238a1547 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -13,7 +13,7 @@ class BuildInfo { const BuildInfo(this.mode, this.flavor, { this.previewDart2 = false, this.trackWidgetCreation = false, - this.buildSnapshot = false, + this.compilationTraceFilePath, this.extraFrontEndOptions, this.extraGenSnapshotOptions, this.buildSharedLibrary, @@ -43,8 +43,8 @@ class BuildInfo { /// Whether the build should track widget creation locations. final bool trackWidgetCreation; - /// Whether the build should create VM snapshot instead of using prebuilt one from engine. - final bool buildSnapshot; + /// Dart compilation trace file to use for JIT VM snapshot. + final String compilationTraceFilePath; /// Extra command-line options for front-end. final String extraFrontEndOptions; @@ -101,7 +101,7 @@ class BuildInfo { new BuildInfo(mode, flavor, previewDart2: previewDart2, trackWidgetCreation: trackWidgetCreation, - buildSnapshot: buildSnapshot, + compilationTraceFilePath: compilationTraceFilePath, extraFrontEndOptions: extraFrontEndOptions, extraGenSnapshotOptions: extraGenSnapshotOptions, buildSharedLibrary: buildSharedLibrary, diff --git a/packages/flutter_tools/lib/src/bundle.dart b/packages/flutter_tools/lib/src/bundle.dart index 898ccfbf46..513f606eff 100644 --- a/packages/flutter_tools/lib/src/bundle.dart +++ b/packages/flutter_tools/lib/src/bundle.dart @@ -47,7 +47,7 @@ Future build({ bool precompiledSnapshot = false, bool reportLicensedPackages = false, bool trackWidgetCreation = false, - bool buildSnapshot = false, + String compilationTraceFilePath, List extraFrontEndOptions = const [], List extraGenSnapshotOptions = const [], List fileSystemRoots, @@ -84,7 +84,7 @@ Future build({ ensureDirectoryExists(applicationKernelFilePath); final CompilerOutput compilerOutput = await kernelCompiler.compile( sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), - incrementalCompilerByteStorePath: buildSnapshot ? null : + incrementalCompilerByteStorePath: compilationTraceFilePath != null ? null : fs.path.absolute(getIncrementalCompilerByteStoreDirectory()), mainPath: fs.file(mainPath).absolute.path, outputFilePath: applicationKernelFilePath, @@ -94,7 +94,7 @@ Future build({ fileSystemRoots: fileSystemRoots, fileSystemScheme: fileSystemScheme, packagesPath: packagesPath, - linkPlatformKernelIn: buildSnapshot, + linkPlatformKernelIn: compilationTraceFilePath != null, ); if (compilerOutput?.outputFilename == null) { throwToolExit('Compiler failed on $mainPath'); @@ -104,7 +104,7 @@ Future build({ await fs.directory(getBuildDirectory()).childFile('frontend_server.d') .writeAsString('frontend_server.d: ${artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk)}\n'); - if (buildSnapshot) { + if (compilationTraceFilePath != null) { final CoreJITSnapshotter snapshotter = new CoreJITSnapshotter(); final int snapshotExitCode = await snapshotter.build( platform: platform, @@ -112,6 +112,7 @@ Future build({ mainPath: applicationKernelFilePath, outputPath: getBuildDirectory(), packagesPath: packagesPath, + compilationTraceFilePath: compilationTraceFilePath, extraGenSnapshotOptions: extraGenSnapshotOptions, ); if (snapshotExitCode != 0) { @@ -135,7 +136,7 @@ Future build({ snapshotFile: snapshotFile, privateKeyPath: privateKeyPath, assetDirPath: assetDirPath, - buildSnapshot: buildSnapshot, + compilationTraceFilePath: compilationTraceFilePath, ); } @@ -171,14 +172,14 @@ Future assemble({ File dylibFile, String privateKeyPath = defaultPrivateKeyPath, String assetDirPath, - bool buildSnapshot, + String compilationTraceFilePath, }) async { assetDirPath ??= getAssetBuildDirectory(); printTrace('Building bundle'); final Map assetEntries = new Map.from(assetBundle.entries); if (kernelContent != null) { - if (buildSnapshot) { + if (compilationTraceFilePath != null) { final String vmSnapshotData = fs.path.join(getBuildDirectory(), _kVMSnapshotData); final String vmSnapshotInstr = fs.path.join(getBuildDirectory(), _kVMSnapshotInstr); final String isolateSnapshotData = fs.path.join(getBuildDirectory(), _kIsolateSnapshotData); diff --git a/packages/flutter_tools/lib/src/commands/build_bundle.dart b/packages/flutter_tools/lib/src/commands/build_bundle.dart index 59c86a4d48..24c162e244 100644 --- a/packages/flutter_tools/lib/src/commands/build_bundle.dart +++ b/packages/flutter_tools/lib/src/commands/build_bundle.dart @@ -37,11 +37,14 @@ class BuildBundleCommand extends BuildSubCommand { hide: !verboseHelp, help: 'Track widget creation locations. Requires Dart 2.0 functionality.', ) - ..addFlag('build-snapshot', + ..addOption('precompile', hide: !verboseHelp, - defaultsTo: false, - help: 'Build and use application-specific VM snapshot instead of\n' - 'prebuilt one provided by the engine.', + help: 'Precompile functions specified in input file. This flag is only\n' + 'allowed when using --dynamic. It takes a Dart compilation trace\n' + 'file produced by the training run of the application. With this\n' + 'flag, instead of using default Dart VM snapshot provided by the\n' + 'engine, the application will use its own snapshot that includes\n' + 'additional functions.' ) ..addMultiOption(FlutterOptions.kExtraFrontEndOptions, splitCommas: true, @@ -106,7 +109,7 @@ class BuildBundleCommand extends BuildSubCommand { precompiledSnapshot: argResults['precompiled'], reportLicensedPackages: argResults['report-licensed-packages'], trackWidgetCreation: argResults['track-widget-creation'], - buildSnapshot: argResults['build-snapshot'], + compilationTraceFilePath: argResults['precompile'], extraFrontEndOptions: argResults[FlutterOptions.kExtraFrontEndOptions], extraGenSnapshotOptions: argResults[FlutterOptions.kExtraGenSnapshotOptions], fileSystemScheme: argResults['filesystem-scheme'], diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 213bbcf38a..359fd54532 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -124,11 +124,14 @@ class RunCommand extends RunCommandBase { hide: !verboseHelp, help: 'Preview Dart 2.0 functionality.', ) - ..addFlag('build-snapshot', + ..addOption('precompile', hide: !verboseHelp, - defaultsTo: false, - help: 'Build and use application-specific VM snapshot instead of\n' - 'prebuilt one provided by the engine.', + help: 'Precompile functions specified in input file. This flag is only\n' + 'allowed when using --dynamic. It takes a Dart compilation trace\n' + 'file produced by the training run of the application. With this\n' + 'flag, instead of using default Dart VM snapshot provided by the\n' + 'engine, the application will use its own snapshot that includes\n' + 'additional functions.' ) ..addFlag('track-widget-creation', hide: !verboseHelp, diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index a0e23e00fc..255756d16f 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -224,9 +224,9 @@ abstract class FlutterCommand extends Command { : null, previewDart2: previewDart2, trackWidgetCreation: trackWidgetCreation, - buildSnapshot: argParser.options.containsKey('build-snapshot') - ? argResults['build-snapshot'] - : false, + compilationTraceFilePath: argParser.options.containsKey('precompile') + ? argResults['precompile'] + : null, extraFrontEndOptions: argParser.options.containsKey(FlutterOptions.kExtraFrontEndOptions) ? argResults[FlutterOptions.kExtraFrontEndOptions] : null, @@ -474,6 +474,15 @@ abstract class FlutterCommand extends Command { if (!fs.isFileSync(targetPath)) throw new ToolExit('Target file "$targetPath" not found.'); } + + final bool dynamicFlag = argParser.options.containsKey('dynamic') + ? argResults['dynamic'] : false; + final String compilationTraceFilePath = argParser.options.containsKey('precompile') + ? argResults['precompile'] : null; + if (compilationTraceFilePath != null && getBuildMode() == BuildMode.debug) + throw new ToolExit('Error: --precompile is not allowed when --debug is specified.'); + if (compilationTraceFilePath != null && !dynamicFlag) + throw new ToolExit('Error: --precompile is allowed only when --dynamic is specified.'); } ApplicationPackageStore applicationPackages; diff --git a/packages/flutter_tools/test/base/build_test.dart b/packages/flutter_tools/test/base/build_test.dart index e7617c4bb0..c14ae5e097 100644 --- a/packages/flutter_tools/test/base/build_test.dart +++ b/packages/flutter_tools/test/base/build_test.dart @@ -813,6 +813,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ), isNot(equals(0))); }, overrides: contextOverrides); @@ -836,6 +837,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0); @@ -880,6 +882,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0); @@ -910,6 +913,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ), isNot(equals(0))); }, overrides: contextOverrides); @@ -933,6 +937,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0); @@ -976,6 +981,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0); @@ -1005,6 +1011,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ), isNot(equals(0))); }, overrides: contextOverrides); @@ -1028,6 +1035,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0); @@ -1071,6 +1079,7 @@ void main() { mainPath: 'main.dill', packagesPath: '.packages', outputPath: outputPath, + compilationTraceFilePath: kTrace, ); expect(genSnapshotExitCode, 0);