[flutter_tools] Add ARM64 Linux host and cross-building option support (#61221)

This commit is contained in:
Hidenori Matsubayashi
2021-02-12 14:25:30 -08:00
committed by GitHub
parent 97a9f2ae93
commit 4cc0ab2d19
40 changed files with 794 additions and 150 deletions

View File

@@ -33,6 +33,9 @@ Future<void> buildLinux(
BuildInfo buildInfo, {
String target = 'lib/main.dart',
SizeAnalyzer sizeAnalyzer,
bool needCrossBuild = false,
TargetPlatform targetPlatform = TargetPlatform.linux_x64,
String targetSysroot = '/',
}) async {
if (!linuxProject.cmakeFile.existsSync()) {
throwToolExit('No Linux desktop project configured. See '
@@ -68,14 +71,16 @@ Future<void> buildLinux(
);
try {
final String buildModeName = getNameForBuildMode(buildInfo.mode ?? BuildMode.release);
final Directory buildDirectory = globals.fs.directory(getLinuxBuildDirectory()).childDirectory(buildModeName);
await _runCmake(buildModeName, linuxProject.cmakeFile.parent, buildDirectory);
final Directory buildDirectory =
globals.fs.directory(getLinuxBuildDirectory(targetPlatform)).childDirectory(buildModeName);
await _runCmake(buildModeName, linuxProject.cmakeFile.parent, buildDirectory,
needCrossBuild, targetPlatform, targetSysroot);
await _runBuild(buildDirectory);
} finally {
status.cancel();
}
if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
final String arch = getNameForTargetPlatform(TargetPlatform.linux_x64);
final String arch = getNameForTargetPlatform(targetPlatform);
final File codeSizeFile = globals.fs.directory(buildInfo.codeSizeDirectory)
.childFile('snapshot.$arch.json');
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
@@ -84,7 +89,7 @@ Future<void> buildLinux(
aotSnapshot: codeSizeFile,
// This analysis is only supported for release builds.
outputDirectory: globals.fs.directory(
globals.fs.path.join(getLinuxBuildDirectory(), 'release', 'bundle'),
globals.fs.path.join(getLinuxBuildDirectory(targetPlatform), 'release', 'bundle'),
),
precompilerTrace: precompilerTrace,
type: 'linux',
@@ -109,12 +114,15 @@ Future<void> buildLinux(
}
}
Future<void> _runCmake(String buildModeName, Directory sourceDir, Directory buildDir) async {
Future<void> _runCmake(String buildModeName, Directory sourceDir, Directory buildDir,
bool needCrossBuild, TargetPlatform targetPlatform, String targetSysroot) async {
final Stopwatch sw = Stopwatch()..start();
await buildDir.create(recursive: true);
final String buildFlag = toTitleCase(buildModeName);
final bool needCrossBuildOptionsForArm64 = needCrossBuild
&& targetPlatform == TargetPlatform.linux_arm64;
int result;
try {
result = await globals.processUtils.stream(
@@ -123,6 +131,15 @@ Future<void> _runCmake(String buildModeName, Directory sourceDir, Directory buil
'-G',
'Ninja',
'-DCMAKE_BUILD_TYPE=$buildFlag',
'-DFLUTTER_TARGET_PLATFORM=' + getNameForTargetPlatform(targetPlatform),
// Support cross-building for arm64 targets on x64 hosts.
// (Cross-building for x64 on arm64 hosts isn't supported now.)
if (needCrossBuild)
'-DFLUTTER_TARGET_PLATFORM_SYSROOT=$targetSysroot',
if (needCrossBuildOptionsForArm64)
'-DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu',
if (needCrossBuildOptionsForArm64)
'-DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu',
sourceDir.path,
],
workingDirectory: buildDir.path,

View File

@@ -27,15 +27,20 @@ class LinuxDevice extends DesktopDevice {
@required Logger logger,
@required FileSystem fileSystem,
@required OperatingSystemUtils operatingSystemUtils,
}) : super(
'linux',
platformType: PlatformType.linux,
ephemeral: false,
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
operatingSystemUtils: operatingSystemUtils,
);
}) : _operatingSystemUtils = operatingSystemUtils,
super(
'linux',
platformType: PlatformType.linux,
ephemeral: false,
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
operatingSystemUtils: operatingSystemUtils,
);
final OperatingSystemUtils _operatingSystemUtils;
TargetPlatform _targetPlatform;
@override
bool isSupported() => true;
@@ -44,7 +49,17 @@ class LinuxDevice extends DesktopDevice {
String get name => 'Linux';
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.linux_x64;
Future<TargetPlatform> get targetPlatform async {
if (_targetPlatform == null) {
if (_operatingSystemUtils.hostPlatform == HostPlatform.linux_x64) {
_targetPlatform = TargetPlatform.linux_x64;
} else {
_targetPlatform = TargetPlatform.linux_arm64;
}
}
return _targetPlatform;
}
@override
bool isSupportedForProject(FlutterProject flutterProject) {
@@ -61,6 +76,7 @@ class LinuxDevice extends DesktopDevice {
FlutterProject.current().linux,
buildInfo,
target: mainPath,
targetPlatform: _targetPlatform,
);
}