From 79a985f9fd41b7ff51f780a9c3a552d7406d5911 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 22 Oct 2019 20:35:05 -0700 Subject: [PATCH] Add compiler configuration to support dartdevc target (#43281) --- packages/flutter_tools/lib/src/artifacts.dart | 8 + packages/flutter_tools/lib/src/compile.dart | 215 +++++++++++------- packages/flutter_tools/lib/src/features.dart | 14 ++ .../lib/src/resident_runner.dart | 15 +- .../test/general.shard/compile_test.dart | 4 + .../general.shard/resident_runner_test.dart | 28 +++ packages/flutter_tools/test/src/testbed.dart | 6 + 7 files changed, 209 insertions(+), 81 deletions(-) diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart index 1f9a8a3f10..c989236776 100644 --- a/packages/flutter_tools/lib/src/artifacts.dart +++ b/packages/flutter_tools/lib/src/artifacts.dart @@ -40,6 +40,8 @@ enum Artifact { kernelWorkerSnapshot, /// The root of the web implementation of the dart SDK. flutterWebSdk, + /// The summary dill for the dartdevc target. + webPlatformKernelDill, iosDeploy, ideviceinfo, ideviceId, @@ -126,6 +128,8 @@ String _artifactToFileName(Artifact artifact, [ TargetPlatform platform, BuildMo return 'sky_engine'; case Artifact.flutterMacOSPodspec: return 'FlutterMacOS.podspec'; + case Artifact.webPlatformKernelDill: + return 'flutter_ddc_sdk.dill'; } assert(false, 'Invalid artifact $artifact.'); return null; @@ -290,6 +294,8 @@ class CachedArtifacts extends Artifacts { return _getFlutterPatchedSdkPath(mode); case Artifact.flutterWebSdk: return _getFlutterWebSdkPath(); + case Artifact.webPlatformKernelDill: + return fs.path.join(_getFlutterWebSdkPath(), 'kernel', _artifactToFileName(artifact)); case Artifact.dart2jsSnapshot: return fs.path.join(dartSdkPath, 'bin', 'snapshots', _artifactToFileName(artifact)); case Artifact.dartdevcSnapshot: @@ -422,6 +428,8 @@ class LocalEngineArtifacts extends Artifacts { return fs.path.join(_hostEngineOutPath, 'gen', 'dart-pkg', artifactFileName); case Artifact.flutterMacOSPodspec: return fs.path.join(_hostEngineOutPath, _artifactToFileName(artifact)); + case Artifact.webPlatformKernelDill: + return fs.path.join(_getFlutterWebSdkPath(), 'kernel', _artifactToFileName(artifact)); } assert(false, 'Invalid artifact $artifact.'); return null; diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index e8bc5a846c..f44dce594d 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -52,6 +52,8 @@ class TargetModel { return flutterRunner; case 'vm': return vm; + case 'dartdevc': + return dartdevc; } assert(false); return null; @@ -68,6 +70,9 @@ class TargetModel { /// The Dart vm. static const TargetModel vm = TargetModel._('vm'); + /// The development compiler for JavaScript. + static const TargetModel dartdevc = TargetModel._('dartdevc'); + final String _value; @override @@ -383,9 +388,9 @@ abstract class _CompilationRequest { Completer completer; - Future _run(ResidentCompiler compiler); + Future _run(DefaultResidentCompiler compiler); - Future run(ResidentCompiler compiler) async { + Future run(DefaultResidentCompiler compiler) async { completer.complete(await _run(compiler)); } } @@ -405,7 +410,7 @@ class _RecompileRequest extends _CompilationRequest { String packagesFilePath; @override - Future _run(ResidentCompiler compiler) async => + Future _run(DefaultResidentCompiler compiler) async => compiler._recompile(this); } @@ -428,7 +433,7 @@ class _CompileExpressionRequest extends _CompilationRequest { bool isStatic; @override - Future _run(ResidentCompiler compiler) async => + Future _run(DefaultResidentCompiler compiler) async => compiler._compileExpression(this); } @@ -436,7 +441,7 @@ class _RejectRequest extends _CompilationRequest { _RejectRequest(Completer completer) : super(completer); @override - Future _run(ResidentCompiler compiler) async => + Future _run(DefaultResidentCompiler compiler) async => compiler._reject(); } @@ -445,55 +450,22 @@ class _RejectRequest extends _CompilationRequest { /// /// The wrapper is intended to stay resident in memory as user changes, reloads, /// restarts the Flutter app. -class ResidentCompiler { - ResidentCompiler( - this._sdkRoot, { +abstract class ResidentCompiler { + factory ResidentCompiler(String sdkRoot, { @required BuildMode buildMode, - bool causalAsyncStacks = true, - bool trackWidgetCreation = false, + bool causalAsyncStacks, + bool trackWidgetCreation, String packagesPath, List fileSystemRoots, String fileSystemScheme, - CompilerMessageConsumer compilerMessageConsumer = printError, + CompilerMessageConsumer compilerMessageConsumer, String initializeFromDill, - TargetModel targetModel = TargetModel.flutter, + TargetModel targetModel, bool unsafePackageSerialization, List experimentalFlags, - }) : assert(_sdkRoot != null), - _buildMode = buildMode, - _causalAsyncStacks = causalAsyncStacks, - _trackWidgetCreation = trackWidgetCreation, - _packagesPath = packagesPath, - _fileSystemRoots = fileSystemRoots, - _fileSystemScheme = fileSystemScheme, - _targetModel = targetModel, - _stdoutHandler = StdoutHandler(consumer: compilerMessageConsumer), - _controller = StreamController<_CompilationRequest>(), - _initializeFromDill = initializeFromDill, - _unsafePackageSerialization = unsafePackageSerialization, - _experimentalFlags = experimentalFlags { - // This is a URI, not a file path, so the forward slash is correct even on Windows. - if (!_sdkRoot.endsWith('/')) { - _sdkRoot = '$_sdkRoot/'; - } - } + String platformDill, + }) = DefaultResidentCompiler; - final BuildMode _buildMode; - final bool _causalAsyncStacks; - final bool _trackWidgetCreation; - final String _packagesPath; - final TargetModel _targetModel; - final List _fileSystemRoots; - final String _fileSystemScheme; - String _sdkRoot; - Process _server; - final StdoutHandler _stdoutHandler; - final String _initializeFromDill; - final bool _unsafePackageSerialization; - final List _experimentalFlags; - bool _compileRequestNeedsConfirmation = false; - - final StreamController<_CompilationRequest> _controller; /// If invoked for the first time, it compiles Dart script identified by /// [mainPath], [invalidatedFiles] list is ignored. @@ -502,6 +474,89 @@ class ResidentCompiler { /// point that is used for recompilation. /// Binary file name is returned if compilation was successful, otherwise /// null is returned. + Future recompile( + String mainPath, + List invalidatedFiles, { + @required String outputPath, + String packagesFilePath, + }); + + Future compileExpression( + String expression, + List definitions, + List typeDefinitions, + String libraryUri, + String klass, + bool isStatic, + ); + + /// Should be invoked when results of compilation are accepted by the client. + /// + /// Either [accept] or [reject] should be called after every [recompile] call. + void accept(); + + /// Should be invoked when results of compilation are rejected by the client. + /// + /// Either [accept] or [reject] should be called after every [recompile] call. + Future reject(); + + /// Should be invoked when frontend server compiler should forget what was + /// accepted previously so that next call to [recompile] produces complete + /// kernel file. + void reset(); + + Future shutdown(); +} + +@visibleForTesting +class DefaultResidentCompiler implements ResidentCompiler { + DefaultResidentCompiler( + String sdkRoot, { + @required this.buildMode, + this.causalAsyncStacks = true, + this.trackWidgetCreation = false, + this.packagesPath, + this.fileSystemRoots, + this.fileSystemScheme, + CompilerMessageConsumer compilerMessageConsumer = printError, + this.initializeFromDill, + this.targetModel = TargetModel.flutter, + this.unsafePackageSerialization, + this.experimentalFlags, + this.platformDill, + }) : assert(sdkRoot != null), + _stdoutHandler = StdoutHandler(consumer: compilerMessageConsumer), + // This is a URI, not a file path, so the forward slash is correct even on Windows. + sdkRoot = sdkRoot.endsWith('/') ? sdkRoot : '$sdkRoot/'; + + final BuildMode buildMode; + final bool causalAsyncStacks; + final bool trackWidgetCreation; + final String packagesPath; + final TargetModel targetModel; + final List fileSystemRoots; + final String fileSystemScheme; + final String initializeFromDill; + final bool unsafePackageSerialization; + final List experimentalFlags; + + /// The path to the root of the Dart SDK used to compile. + /// + /// This is used to resolve the [platformDill]. + final String sdkRoot; + + /// The path to the platform dill file. + /// + /// This does not need to be provided for the normal Flutter workflow. + final String platformDill; + + Process _server; + final StdoutHandler _stdoutHandler; + bool _compileRequestNeedsConfirmation = false; + + final StreamController<_CompilationRequest> _controller = StreamController<_CompilationRequest>(); + + @override Future recompile( String mainPath, List invalidatedFiles, { @@ -526,12 +581,12 @@ class ResidentCompiler { // First time recompile is called we actually have to compile the app from // scratch ignoring list of invalidated files. PackageUriMapper packageUriMapper; - if (request.packagesFilePath != null || _packagesPath != null) { + if (request.packagesFilePath != null || packagesPath != null) { packageUriMapper = PackageUriMapper( request.mainPath, - request.packagesFilePath ?? _packagesPath, - _fileSystemScheme, - _fileSystemRoots, + request.packagesFilePath ?? packagesPath, + fileSystemScheme, + fileSystemRoots, ); } @@ -541,7 +596,7 @@ class ResidentCompiler { return _compile( _mapFilename(request.mainPath, packageUriMapper), request.outputPath, - _mapFilename(request.packagesFilePath ?? _packagesPath, /* packageUriMapper= */ null), + _mapFilename(request.packagesFilePath ?? packagesPath, /* packageUriMapper= */ null), ); } @@ -589,11 +644,11 @@ class ResidentCompiler { artifacts.getArtifactPath(Artifact.engineDartBinary), frontendServer, '--sdk-root', - _sdkRoot, + sdkRoot, '--incremental', '--strong', - '--target=$_targetModel', - '-Ddart.developer.causal_async_stacks=$_causalAsyncStacks', + '--target=$targetModel', + '-Ddart.developer.causal_async_stacks=$causalAsyncStacks', if (outputPath != null) ...[ '--output-dill', outputPath, @@ -601,28 +656,32 @@ class ResidentCompiler { if (packagesFilePath != null) ...[ '--packages', packagesFilePath, - ] else if (_packagesPath != null) ...[ + ] else if (packagesPath != null) ...[ '--packages', - _packagesPath, + packagesPath, ], - ..._buildModeOptions(_buildMode), - if (_trackWidgetCreation) '--track-widget-creation', - if (_fileSystemRoots != null) - for (String root in _fileSystemRoots) ...[ + ..._buildModeOptions(buildMode), + if (trackWidgetCreation) '--track-widget-creation', + if (fileSystemRoots != null) + for (String root in fileSystemRoots) ...[ '--filesystem-root', root, ], - if (_fileSystemScheme != null) ...[ + if (fileSystemScheme != null) ...[ '--filesystem-scheme', - _fileSystemScheme, + fileSystemScheme, ], - if (_initializeFromDill != null) ...[ + if (initializeFromDill != null) ...[ '--initialize-from-dill', - _initializeFromDill, + initializeFromDill, ], - if (_unsafePackageSerialization == true) '--unsafe-package-serialization', - if ((_experimentalFlags != null) && _experimentalFlags.isNotEmpty) - '--enable-experiment=${_experimentalFlags.join(',')}', + if (platformDill != null) ...[ + '--platform', + platformDill, + ], + if (unsafePackageSerialization == true) '--unsafe-package-serialization', + if ((experimentalFlags != null) && experimentalFlags.isNotEmpty) + '--enable-experiment=${experimentalFlags.join(',')}', ]; printTrace(command.join(' ')); _server = await processManager.start(command); @@ -657,6 +716,7 @@ class ResidentCompiler { return _stdoutHandler.compilerOutput.future; } + @override Future compileExpression( String expression, List definitions, @@ -700,9 +760,7 @@ class ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - /// Should be invoked when results of compilation are accepted by the client. - /// - /// Either [accept] or [reject] should be called after every [recompile] call. + @override void accept() { if (_compileRequestNeedsConfirmation) { _server.stdin.writeln('accept'); @@ -711,9 +769,7 @@ class ResidentCompiler { _compileRequestNeedsConfirmation = false; } - /// Should be invoked when results of compilation are rejected by the client. - /// - /// Either [accept] or [reject] should be called after every [recompile] call. + @override Future reject() { if (!_controller.hasListener) { _controller.stream.listen(_handleCompilationRequest); @@ -735,9 +791,7 @@ class ResidentCompiler { return _stdoutHandler.compilerOutput.future; } - /// Should be invoked when frontend server compiler should forget what was - /// accepted previously so that next call to [recompile] produces complete - /// kernel file. + @override void reset() { _server?.stdin?.writeln('reset'); printTrace('<- reset'); @@ -765,21 +819,22 @@ class ResidentCompiler { } } - if (_fileSystemRoots != null) { - for (String root in _fileSystemRoots) { + if (fileSystemRoots != null) { + for (String root in fileSystemRoots) { if (filename.startsWith(root)) { return Uri( - scheme: _fileSystemScheme, path: filename.substring(root.length)) + scheme: fileSystemScheme, path: filename.substring(root.length)) .toString(); } } } - if (platform.isWindows && _fileSystemRoots != null && _fileSystemRoots.length > 1) { + if (platform.isWindows && fileSystemRoots != null && fileSystemRoots.length > 1) { return Uri.file(filename, windows: platform.isWindows).toString(); } return null; } + @override Future shutdown() async { // Server was never successfully created. if (_server == null) { diff --git a/packages/flutter_tools/lib/src/features.dart b/packages/flutter_tools/lib/src/features.dart index d3237d70c4..95c0248e84 100644 --- a/packages/flutter_tools/lib/src/features.dart +++ b/packages/flutter_tools/lib/src/features.dart @@ -39,6 +39,9 @@ class FeatureFlags { /// Whether the Android embedding V2 is enabled. bool get isAndroidEmbeddingV2Enabled => isEnabled(flutterAndroidEmbeddingV2Feature); + /// Whether the web incremental compiler is enabled. + bool get isWebIncrementalCompilerEnabled => isEnabled(flutterWebIncrementalCompiler); + /// Whether a particular feature is enabled for the current channel. /// /// Prefer using one of the specific getters above instead of this API. @@ -72,6 +75,7 @@ const List allFeatures = [ flutterWindowsDesktopFeature, flutterBuildPluginAsAarFeature, flutterAndroidEmbeddingV2Feature, + flutterWebIncrementalCompiler, ]; /// The [Feature] for flutter web. @@ -143,6 +147,16 @@ const Feature flutterAndroidEmbeddingV2Feature = Feature( ), ); +/// The [Feature] for using the incremental compiler instead of build runner. +const Feature flutterWebIncrementalCompiler = Feature( + name: 'Enable the incremental compiler for web builds', + configSetting: 'enable-web-incremental-compiler', + master: FeatureChannelSetting( + available: true, + enabledByDefault: false, + ), +); + /// A [Feature] is a process for conditionally enabling tool features. /// /// All settings are optional, and if not provided will generally default to diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index e36ee037cd..6de86aa63e 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -22,6 +22,7 @@ import 'compile.dart'; import 'dart/package_map.dart'; import 'devfs.dart'; import 'device.dart'; +import 'features.dart'; import 'globals.dart'; import 'project.dart'; import 'run_cold.dart'; @@ -65,7 +66,19 @@ class FlutterDevice { ResidentCompiler generator, }) async { ResidentCompiler generator; - if (flutterProject.hasBuilders) { + if (featureFlags.isWebIncrementalCompilerEnabled && + await device.targetPlatform == TargetPlatform.web_javascript) { + generator = ResidentCompiler( + artifacts.getArtifactPath(Artifact.flutterWebSdk, mode: buildMode), + buildMode: buildMode, + trackWidgetCreation: trackWidgetCreation, + fileSystemRoots: fileSystemRoots, + fileSystemScheme: fileSystemScheme, + targetModel: TargetModel.dartdevc, + experimentalFlags: experimentalFlags, + platformDill: artifacts.getArtifactPath(Artifact.webPlatformKernelDill, mode: buildMode), + ); + } else if (flutterProject.hasBuilders) { generator = await CodeGeneratingResidentCompiler.create( buildMode: buildMode, flutterProject: flutterProject, diff --git a/packages/flutter_tools/test/general.shard/compile_test.dart b/packages/flutter_tools/test/general.shard/compile_test.dart index cfaa288cac..9026c99fb3 100644 --- a/packages/flutter_tools/test/general.shard/compile_test.dart +++ b/packages/flutter_tools/test/general.shard/compile_test.dart @@ -37,6 +37,10 @@ void main() { expect(TargetModel('flutter_runner'), TargetModel.flutterRunner); expect(TargetModel.flutterRunner.toString(), 'flutter_runner'); + + expect(TargetModel('dartdevc'), TargetModel.dartdevc); + expect(TargetModel.dartdevc.toString(), 'dartdevc'); + expect(() => TargetModel('foobar'), throwsA(isInstanceOf())); }); } diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 87f7972eca..2ed75524b6 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -5,14 +5,18 @@ import 'dart:async'; import 'package:file/memory.dart'; +import 'package:flutter_tools/src/artifacts.dart'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/context.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/build_info.dart'; +import 'package:flutter_tools/src/compile.dart'; import 'package:flutter_tools/src/devfs.dart'; import 'package:flutter_tools/src/device.dart'; +import 'package:flutter_tools/src/features.dart'; import 'package:flutter_tools/src/globals.dart'; +import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/resident_runner.dart'; import 'package:flutter_tools/src/run_cold.dart'; @@ -599,6 +603,29 @@ void main() { expect(await fs.file('foo').readAsString(), testUri.toString()); })); + + test('FlutterDevice uses dartdevc configuration when targeting web', () => testbed.run(() async { + final MockDevice mockDevice = MockDevice(); + when(mockDevice.targetPlatform).thenAnswer((Invocation invocation) async { + return TargetPlatform.web_javascript; + }); + + final DefaultResidentCompiler residentCompiler = (await FlutterDevice.create( + mockDevice, + buildMode: BuildMode.debug, + flutterProject: FlutterProject.current(), + target: null, + trackWidgetCreation: true, + )).generator; + + expect(residentCompiler.targetModel, TargetModel.dartdevc); + expect(residentCompiler.sdkRoot, + artifacts.getArtifactPath(Artifact.flutterWebSdk, mode: BuildMode.debug) + '/'); + expect(residentCompiler.platformDill, + artifacts.getArtifactPath(Artifact.webPlatformKernelDill, mode: BuildMode.debug)); + }, overrides: { + FeatureFlags: () => TestFeatureFlags(isWebIncrementalCompilerEnabled: true), + })); } class MockFlutterDevice extends Mock implements FlutterDevice {} @@ -608,6 +635,7 @@ class MockDevFS extends Mock implements DevFS {} class MockIsolate extends Mock implements Isolate {} class MockDevice extends Mock implements Device {} class MockUsage extends Mock implements Usage {} +class MockProcessManager extends Mock implements ProcessManager {} class MockServiceEvent extends Mock implements ServiceEvent {} class TestFlutterDevice extends FlutterDevice { TestFlutterDevice(Device device, this.views) diff --git a/packages/flutter_tools/test/src/testbed.dart b/packages/flutter_tools/test/src/testbed.dart index c7298ab5e6..4469127882 100644 --- a/packages/flutter_tools/test/src/testbed.dart +++ b/packages/flutter_tools/test/src/testbed.dart @@ -700,6 +700,7 @@ class TestFeatureFlags implements FeatureFlags { this.isWebEnabled = false, this.isWindowsEnabled = false, this.isAndroidEmbeddingV2Enabled = false, + this.isWebIncrementalCompilerEnabled = false, }); @override @@ -717,6 +718,9 @@ class TestFeatureFlags implements FeatureFlags { @override final bool isAndroidEmbeddingV2Enabled; + @override + final bool isWebIncrementalCompilerEnabled; + @override bool isEnabled(Feature feature) { switch (feature) { @@ -730,6 +734,8 @@ class TestFeatureFlags implements FeatureFlags { return isWindowsEnabled; case flutterAndroidEmbeddingV2Feature: return isAndroidEmbeddingV2Enabled; + case flutterWebIncrementalCompiler: + return isWebIncrementalCompilerEnabled; } return false; }