[native_assets] Use kernel concatenation (#147158)

Uses kernel concatenation to concatenate the `program.dill` and `native_assets.dill`.

This will enable compiling `native_assets.dill` after `program.dill` so that tree-shaking information can be used.

Issue:

* https://github.com/flutter/flutter/issues/146270

## Implementation choices

* We can either run the frontend_server twice as a process invocation (current PR), or keep the process running after the dart compilation and then shut it down after native assets compilation to kernel. Keeping the process running would require more coordination between different `Target`s.
* We can either chose to only do separate kernel file compilation in release (AOT) builds, or do it both for JIT and AOT (current PR). Doing it differently in JIT/AOT means more conditionals in lib/src/build_system/targets/, doing it single-shot in JIT might be more performant though.

Update: Both of these are mitigated by not running the kernel compiler process if there are no native assets at all.

## Implementation notes

This only updates `flutter assemble`.

The kernel compilation calls in `flutter run` do not require kernel concatenation. The native-assets-kernel-mapping in `flutter run` contains results from `--dry-run` invocations of `hook/build.dart` (and in the future `hook/link.dart`). These `--dry-run` invocations do not get access to tree-shaking information, so the `link` hook to be introduced later can be run before Dart compilation.

## Tests

* Updated the `Target`s tests to reflect the new implementation.

## Related CLs

* frontend_server support: https://dart-review.googlesource.com/c/sdk/+/363567
* Same PR for Dart standalone: https://dart-review.googlesource.com/c/sdk/+/362384
This commit is contained in:
Daco Harkes
2024-04-25 13:22:17 +02:00
committed by GitHub
parent d2631b552c
commit e410b7cd5e
4 changed files with 269 additions and 99 deletions

View File

@@ -64,10 +64,10 @@ void main() {
iosEnvironment.buildDir.createSync(recursive: true);
});
testWithoutContext('KernelSnapshot throws error if missing build mode', () async {
testWithoutContext('KernelSnapshotProgram throws error if missing build mode', () async {
androidEnvironment.defines.remove(kBuildMode);
expect(
const KernelSnapshot().build(androidEnvironment),
const KernelSnapshotProgram().build(androidEnvironment),
throwsA(isA<MissingDefineException>()));
});
@@ -79,13 +79,22 @@ format-version:
native-assets: {}
''';
testWithoutContext('KernelSnapshot handles null result from kernel compilation', () async {
const String nonEmptyNativeAssets = '''
format-version:
- 1
- 0
- 0
native-assets:
macos_arm64:
package:my_package/my_package_bindings_generated.dart:
- absolute
- my_package.framework/my_package
''';
testWithoutContext('KernelSnapshotProgram handles null result from kernel compilation', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -110,27 +119,22 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'file:///lib/main.dart',
], exitCode: 1),
]);
await expectLater(() => const KernelSnapshot().build(androidEnvironment), throwsException);
await expectLater(() => const KernelSnapshotProgram().build(androidEnvironment), throwsException);
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot does use track widget creation on profile builds', () async {
testWithoutContext('KernelSnapshotProgram does use track widget creation on profile builds', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -155,28 +159,23 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot().build(androidEnvironment);
await const KernelSnapshotProgram().build(androidEnvironment);
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot correctly handles an empty string in ExtraFrontEndOptions', () async {
testWithoutContext('KernelSnapshotProgram correctly handles an empty string in ExtraFrontEndOptions', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -201,29 +200,24 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot()
await const KernelSnapshotProgram()
.build(androidEnvironment..defines[kExtraFrontEndOptions] = '');
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot correctly forwards FrontendServerStarterPath', () async {
testWithoutContext('KernelSnapshotProgram correctly forwards FrontendServerStarterPath', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -248,29 +242,24 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot()
await const KernelSnapshotProgram()
.build(androidEnvironment..defines[kFrontendServerStarterPath] = 'path/to/frontend_server_starter.dart');
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot correctly forwards ExtraFrontEndOptions', () async {
testWithoutContext('KernelSnapshotProgram correctly forwards ExtraFrontEndOptions', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -295,31 +284,27 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'foo',
'bar',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot()
await const KernelSnapshotProgram()
.build(androidEnvironment..defines[kExtraFrontEndOptions] = 'foo,bar');
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot can disable track-widget-creation on debug builds', () async {
testWithoutContext('KernelSnapshotProgram can disable track-widget-creation on debug builds', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -340,33 +325,28 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--incremental',
'--initialize-from-dill',
'$build/app.dill',
'--native-assets',
'$build/native_assets.yaml',
'$build/program.dill',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot().build(androidEnvironment
await const KernelSnapshotProgram().build(androidEnvironment
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kTrackWidgetCreation] = 'false');
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot forces platform linking on debug for darwin target platforms', () async {
testWithoutContext('KernelSnapshotProgram forces platform linking on debug for darwin target platforms', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -386,20 +366,18 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--incremental',
'--initialize-from-dill',
'$build/app.dill',
'--native-assets',
'$build/native_assets.yaml',
'$build/program.dill',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/program.dill 0\n'),
]);
await const KernelSnapshot().build(androidEnvironment
await const KernelSnapshotProgram().build(androidEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.darwin)
..defines[kBuildMode] = BuildMode.debug.cliName
..defines[kTrackWidgetCreation] = 'false'
@@ -408,7 +386,7 @@ native-assets: {}
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('KernelSnapshot does use track widget creation on debug builds', () async {
testWithoutContext('KernelSnapshotProgram does use track widget creation on debug builds', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
@@ -423,9 +401,6 @@ native-assets: {}
fileSystem: fileSystem,
logger: logger,
);
testEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(emptyNativeAssets);
final String build = testEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
@@ -447,24 +422,72 @@ native-assets: {}
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/app.dill',
'$build/program.dill',
'--depfile',
'$build/kernel_snapshot.d',
'--incremental',
'--initialize-from-dill',
'$build/app.dill',
'--native-assets',
'$build/native_assets.yaml',
'$build/program.dill',
'--verbosity=error',
'file:///lib/main.dart',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey /build/653e11a8e6908714056a57cd6b4f602a/app.dill 0\n'),
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey /build/653e11a8e6908714056a57cd6b4f602a/program.dill 0\n'),
]);
await const KernelSnapshot().build(testEnvironment);
await const KernelSnapshotProgram().build(testEnvironment);
expect(processManager, hasNoRemainingExpectations);
});
for (final BuildMode buildMode in <BuildMode>[BuildMode.debug, BuildMode.release]) {
for (final bool empty in <bool>[true, false]) {
final String testName = empty ? 'empty' : 'non empty';
testWithoutContext('KernelSnapshotNativeAssets ${buildMode.name} $testName', () async {
fileSystem.file('.dart_tool/package_config.json')
..createSync(recursive: true)
..writeAsStringSync('{"configVersion": 2, "packages":[]}');
androidEnvironment.buildDir.childFile('native_assets.yaml')
..createSync(recursive: true)
..writeAsStringSync(empty ? emptyNativeAssets : nonEmptyNativeAssets);
final String build = androidEnvironment.buildDir.path;
final String flutterPatchedSdkPath = artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
platform: TargetPlatform.darwin,
mode: buildMode,
);
processManager.addCommands(<FakeCommand>[
if (!empty)
FakeCommand(command: <String>[
artifacts.getArtifactPath(Artifact.engineDartAotRuntime),
'--disable-dart-dev',
artifacts.getArtifactPath(Artifact.frontendServerSnapshotForEngineDartSdk),
'--sdk-root',
'$flutterPatchedSdkPath/',
'--target=flutter',
'--no-print-incremental-dependencies',
...buildModeOptions(buildMode, <String>[]),
'--no-link-platform',
if (buildMode == BuildMode.release) ...<String>['--aot', '--tfa'],
'--packages',
'/.dart_tool/package_config.json',
'--output-dill',
'$build/native_assets.dill',
'--native-assets',
'$build/native_assets.yaml',
'--verbosity=error',
'--native-assets-only',
], stdout: 'result $kBoundaryKey\n$kBoundaryKey\n$kBoundaryKey $build/app.dill 0\n'),
]);
await const KernelSnapshotNativeAssets().build(androidEnvironment
..defines[kTargetPlatform] = getNameForTargetPlatform(TargetPlatform.darwin)
..defines[kBuildMode] = buildMode.cliName
);
expect(processManager, hasNoRemainingExpectations);
});
}
}
testUsingContext('AotElfProfile Produces correct output directory', () async {
final String build = androidEnvironment.buildDir.path;
processManager.addCommands(<FakeCommand>[