Use Dart's new direct ELF generator to package AOT blobs as shared libraries in Android APKs (#33611)

This is a replacement for the old implementation of --build-shared-library
that emits an AOT assembly snapshot and feeds it to the Android NDK toolchain.
This commit is contained in:
Jason Simmons
2019-05-30 16:07:22 -07:00
committed by GitHub
parent c2ed91478f
commit e810512b32
2 changed files with 7 additions and 41 deletions

View File

@@ -429,7 +429,7 @@ Future<void> _buildGradleProjectV2(
command.add('-Pfilesystem-roots=${buildInfo.fileSystemRoots.join('|')}');
if (buildInfo.fileSystemScheme != null)
command.add('-Pfilesystem-scheme=${buildInfo.fileSystemScheme}');
if (buildInfo.buildSharedLibrary && androidSdk.ndk != null) {
if (buildInfo.buildSharedLibrary) {
command.add('-Pbuild-shared-library=true');
}
if (buildInfo.targetPlatform != null)

View File

@@ -6,7 +6,6 @@ import 'dart:async';
import 'package:meta/meta.dart';
import '../android/android_sdk.dart';
import '../artifacts.dart';
import '../build_info.dart';
import '../bundle.dart';
@@ -101,19 +100,6 @@ class AOTSnapshotter {
if (platform == TargetPlatform.ios)
buildSharedLibrary = false;
if (buildSharedLibrary && androidSdk.ndk == null) {
final String explanation = AndroidNdk.explainMissingNdk(androidSdk.directory);
printError(
'Could not find NDK in Android SDK at ${androidSdk.directory}:\n'
'\n'
' $explanation\n'
'\n'
'Unable to build with --build-shared-library\n'
'To install the NDK, see instructions at https://developer.android.com/ndk/guides/'
);
return 1;
}
final PackageMap packageMap = PackageMap(packagesPath);
final String packageMapError = packageMap.checkValid();
if (packageMapError != null) {
@@ -141,11 +127,16 @@ class AOTSnapshotter {
}
final String assembly = fs.path.join(outputDir.path, 'snapshot_assembly.S');
if (buildSharedLibrary || platform == TargetPlatform.ios) {
if (platform == TargetPlatform.ios) {
// Assembly AOT snapshot.
outputPaths.add(assembly);
genSnapshotArgs.add('--snapshot_kind=app-aot-assembly');
genSnapshotArgs.add('--assembly=$assembly');
} else if (buildSharedLibrary) {
final String aotSharedLibrary = fs.path.join(outputDir.path, 'app.so');
outputPaths.add(aotSharedLibrary);
genSnapshotArgs.add('--snapshot_kind=app-aot-elf');
genSnapshotArgs.add('--assembly=$aotSharedLibrary');
} else {
// Blob AOT snapshot.
final String vmSnapshotData = fs.path.join(outputDir.path, 'vm_snapshot_data');
@@ -224,12 +215,6 @@ class AOTSnapshotter {
final RunResult result = await _buildIosFramework(iosArch: iosArch, assemblyPath: assembly, outputPath: outputDir.path);
if (result.exitCode != 0)
return result.exitCode;
} else if (buildSharedLibrary) {
final RunResult result = await _buildAndroidSharedLibrary(assemblyPath: assembly, outputPath: outputDir.path);
if (result.exitCode != 0) {
printError('Failed to build AOT snapshot. Compiler terminated with exit code ${result.exitCode}');
return result.exitCode;
}
}
// Compute and record build fingerprint.
@@ -273,25 +258,6 @@ class AOTSnapshotter {
return linkResult;
}
/// Builds an Android shared library at [outputPath]/app.so from the assembly
/// source at [assemblyPath].
Future<RunResult> _buildAndroidSharedLibrary({
@required String assemblyPath,
@required String outputPath,
}) async {
// A word of warning: Instead of compiling via two steps, to a .o file and
// then to a .so file we use only one command. When using two commands
// gcc will end up putting a .eh_frame and a .debug_frame into the shared
// library. Without stripping .debug_frame afterwards, unwinding tools
// based upon libunwind use just one and ignore the contents of the other
// (which causes it to not look into the other section and therefore not
// find the correct unwinding information).
final String assemblySo = fs.path.join(outputPath, 'app.so');
return await runCheckedAsync(<String>[androidSdk.ndk.compiler]
..addAll(androidSdk.ndk.compilerArgs)
..addAll(<String>[ '-shared', '-nostdlib', '-o', assemblySo, assemblyPath ]));
}
/// Compiles a Dart file to kernel.
///
/// Returns the output kernel file path, or null on failure.