Skip to content

Commit 7ffcd3d

Browse files
authored
Extract KernelCompiler class (#16937)
Wraps the compile function in a class injected via the global context, which makes it easier to mock in unit tests -- specifically tests for AOT snapshotting, which already require pretty significant amounts of mock inputs.
1 parent a21c93d commit 7ffcd3d

File tree

5 files changed

+86
-77
lines changed

5 files changed

+86
-77
lines changed

packages/flutter_tools/lib/src/bundle.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Future<void> build({
106106
String kernelBinaryFilename;
107107
if (needBuild) {
108108
ensureDirectoryExists(applicationKernelFilePath);
109-
final CompilerOutput compilerOutput = await compile(
109+
final CompilerOutput compilerOutput = await kernelCompiler.compile(
110110
sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
111111
incrementalCompilerByteStorePath: fs.path.absolute(getIncrementalCompilerByteStoreDirectory()),
112112
mainPath: fs.file(mainPath).absolute.path,

packages/flutter_tools/lib/src/commands/build_aot.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Future<String> _buildAotSnapshot(
366366
}
367367

368368
if (previewDart2) {
369-
final CompilerOutput compilerOutput = await compile(
369+
final CompilerOutput compilerOutput = await kernelCompiler.compile(
370370
sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
371371
mainPath: mainPath,
372372
outputFilePath: kApplicationKernelPath,

packages/flutter_tools/lib/src/compile.dart

Lines changed: 79 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import 'package:usage/uuid/uuid.dart';
99

1010
import 'artifacts.dart';
1111
import 'base/common.dart';
12+
import 'base/context.dart';
1213
import 'base/io.dart';
1314
import 'base/process_manager.dart';
1415
import 'globals.dart';
1516

17+
KernelCompiler get kernelCompiler => context[KernelCompiler];
18+
1619
typedef void CompilerMessageConsumer(String message);
1720

1821
class CompilerOutput {
@@ -59,7 +62,10 @@ class _StdoutHandler {
5962
}
6063
}
6164

62-
Future<CompilerOutput> compile(
65+
class KernelCompiler {
66+
const KernelCompiler();
67+
68+
Future<CompilerOutput> compile(
6369
{String sdkRoot,
6470
String mainPath,
6571
String outputFilePath,
@@ -73,80 +79,81 @@ Future<CompilerOutput> compile(
7379
String packagesPath,
7480
List<String> fileSystemRoots,
7581
String fileSystemScheme}) async {
76-
final String frontendServer = artifacts.getArtifactPath(
77-
Artifact.frontendServerSnapshotForEngineDartSdk
78-
);
79-
80-
// This is a URI, not a file path, so the forward slash is correct even on Windows.
81-
if (!sdkRoot.endsWith('/'))
82-
sdkRoot = '$sdkRoot/';
83-
final String engineDartPath = artifacts.getArtifactPath(Artifact.engineDartBinary);
84-
if (!processManager.canRun(engineDartPath)) {
85-
throwToolExit('Unable to find Dart binary at $engineDartPath');
86-
}
87-
final List<String> command = <String>[
88-
engineDartPath,
89-
frontendServer,
90-
'--sdk-root',
91-
sdkRoot,
92-
'--strong',
93-
'--target=flutter',
94-
];
95-
if (trackWidgetCreation)
96-
command.add('--track-widget-creation');
97-
if (!linkPlatformKernelIn)
98-
command.add('--no-link-platform');
99-
if (aot) {
100-
command.add('--aot');
101-
command.add('--tfa');
102-
}
103-
if (entryPointsJsonFiles != null) {
104-
for (String entryPointsJson in entryPointsJsonFiles) {
105-
command.addAll(<String>['--entry-points', entryPointsJson]);
82+
final String frontendServer = artifacts.getArtifactPath(
83+
Artifact.frontendServerSnapshotForEngineDartSdk
84+
);
85+
86+
// This is a URI, not a file path, so the forward slash is correct even on Windows.
87+
if (!sdkRoot.endsWith('/'))
88+
sdkRoot = '$sdkRoot/';
89+
final String engineDartPath = artifacts.getArtifactPath(Artifact.engineDartBinary);
90+
if (!processManager.canRun(engineDartPath)) {
91+
throwToolExit('Unable to find Dart binary at $engineDartPath');
10692
}
107-
}
108-
if (incrementalCompilerByteStorePath != null) {
109-
command.add('--incremental');
110-
}
111-
if (packagesPath != null) {
112-
command.addAll(<String>['--packages', packagesPath]);
113-
}
114-
if (outputFilePath != null) {
115-
command.addAll(<String>['--output-dill', outputFilePath]);
116-
}
117-
if (depFilePath != null && (fileSystemRoots == null || fileSystemRoots.isEmpty)) {
118-
command.addAll(<String>['--depfile', depFilePath]);
119-
}
120-
if (fileSystemRoots != null) {
121-
for (String root in fileSystemRoots) {
122-
command.addAll(<String>['--filesystem-root', root]);
93+
final List<String> command = <String>[
94+
engineDartPath,
95+
frontendServer,
96+
'--sdk-root',
97+
sdkRoot,
98+
'--strong',
99+
'--target=flutter',
100+
];
101+
if (trackWidgetCreation)
102+
command.add('--track-widget-creation');
103+
if (!linkPlatformKernelIn)
104+
command.add('--no-link-platform');
105+
if (aot) {
106+
command.add('--aot');
107+
command.add('--tfa');
123108
}
124-
}
125-
if (fileSystemScheme != null) {
126-
command.addAll(<String>['--filesystem-scheme', fileSystemScheme]);
127-
}
109+
if (entryPointsJsonFiles != null) {
110+
for (String entryPointsJson in entryPointsJsonFiles) {
111+
command.addAll(<String>['--entry-points', entryPointsJson]);
112+
}
113+
}
114+
if (incrementalCompilerByteStorePath != null) {
115+
command.add('--incremental');
116+
}
117+
if (packagesPath != null) {
118+
command.addAll(<String>['--packages', packagesPath]);
119+
}
120+
if (outputFilePath != null) {
121+
command.addAll(<String>['--output-dill', outputFilePath]);
122+
}
123+
if (depFilePath != null && (fileSystemRoots == null || fileSystemRoots.isEmpty)) {
124+
command.addAll(<String>['--depfile', depFilePath]);
125+
}
126+
if (fileSystemRoots != null) {
127+
for (String root in fileSystemRoots) {
128+
command.addAll(<String>['--filesystem-root', root]);
129+
}
130+
}
131+
if (fileSystemScheme != null) {
132+
command.addAll(<String>['--filesystem-scheme', fileSystemScheme]);
133+
}
134+
135+
if (extraFrontEndOptions != null)
136+
command.addAll(extraFrontEndOptions);
137+
command.add(mainPath);
138+
printTrace(command.join(' '));
139+
final Process server = await processManager
140+
.start(command)
141+
.catchError((dynamic error, StackTrace stack) {
142+
printError('Failed to start frontend server $error, $stack');
143+
});
128144

129-
if (extraFrontEndOptions != null)
130-
command.addAll(extraFrontEndOptions);
131-
command.add(mainPath);
132-
printTrace(command.join(' '));
133-
final Process server = await processManager
134-
.start(command)
135-
.catchError((dynamic error, StackTrace stack) {
136-
printError('Failed to start frontend server $error, $stack');
137-
});
138-
139-
final _StdoutHandler stdoutHandler = new _StdoutHandler();
140-
141-
server.stderr
142-
.transform(utf8.decoder)
143-
.listen((String s) { printError('compiler message: $s'); });
144-
server.stdout
145-
.transform(utf8.decoder)
146-
.transform(const LineSplitter())
147-
.listen(stdoutHandler.handler);
148-
final int exitCode = await server.exitCode;
149-
return exitCode == 0 ? stdoutHandler.compilerOutput.future : null;
145+
final _StdoutHandler stdoutHandler = new _StdoutHandler();
146+
147+
server.stderr
148+
.transform(utf8.decoder)
149+
.listen((String s) { printError('compiler message: $s'); });
150+
server.stdout
151+
.transform(utf8.decoder)
152+
.transform(const LineSplitter())
153+
.listen(stdoutHandler.handler);
154+
final int exitCode = await server.exitCode;
155+
return exitCode == 0 ? stdoutHandler.compilerOutput.future : null;
156+
}
150157
}
151158

152159
/// Wrapper around incremental frontend server compiler, that communicates with

packages/flutter_tools/lib/src/context_runner.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'base/platform.dart';
2222
import 'base/port_scanner.dart';
2323
import 'base/utils.dart';
2424
import 'cache.dart';
25+
import 'compile.dart';
2526
import 'devfs.dart';
2627
import 'device.dart';
2728
import 'doctor.dart';
@@ -63,6 +64,7 @@ Future<T> runInContext<T>(
6364
IMobileDevice: () => const IMobileDevice(),
6465
IOSSimulatorUtils: () => new IOSSimulatorUtils(),
6566
IOSWorkflow: () => const IOSWorkflow(),
67+
KernelCompiler: () => const KernelCompiler(),
6668
Logger: () => platform.isWindows ? new WindowsStdoutLogger() : new StdoutLogger(),
6769
OperatingSystemUtils: () => new OperatingSystemUtils(),
6870
PortScanner: () => const HostPortScanner(),

packages/flutter_tools/test/compile_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void main() {
4646
'result abc\nline1\nline2\nabc /path/to/main.dart.dill 0'
4747
))
4848
));
49-
final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot',
49+
final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot',
5050
mainPath: '/path/to/main.dart'
5151
);
5252
expect(mockFrontendServerStdIn.getAndClear(), isEmpty);
@@ -66,7 +66,7 @@ void main() {
6666
))
6767
));
6868

69-
final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot',
69+
final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot',
7070
mainPath: '/path/to/main.dart'
7171
);
7272
expect(mockFrontendServerStdIn.getAndClear(), isEmpty);
@@ -88,7 +88,7 @@ void main() {
8888
))
8989
));
9090

91-
final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot',
91+
final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot',
9292
mainPath: '/path/to/main.dart'
9393
);
9494
expect(mockFrontendServerStdIn.getAndClear(), isEmpty);

0 commit comments

Comments
 (0)