[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

@@ -16,6 +16,7 @@ import '../base/os.dart';
import '../base/platform.dart';
import '../base/user_messages.dart' hide userMessages;
import '../base/version.dart';
import '../build_info.dart';
import '../convert.dart';
import '../doctor.dart';
import '../features.dart';
@@ -45,14 +46,19 @@ class AndroidWorkflow implements Workflow {
AndroidWorkflow({
@required AndroidSdk androidSdk,
@required FeatureFlags featureFlags,
@required OperatingSystemUtils operatingSystemUtils,
}) : _androidSdk = androidSdk,
_featureFlags = featureFlags;
_featureFlags = featureFlags,
_operatingSystemUtils = operatingSystemUtils;
final AndroidSdk _androidSdk;
final FeatureFlags _featureFlags;
final OperatingSystemUtils _operatingSystemUtils;
@override
bool get appliesToHostPlatform => _featureFlags.isAndroidEnabled;
bool get appliesToHostPlatform => _featureFlags.isAndroidEnabled
// Android Studio is not currently supported on Linux Arm64 Hosts.
&& _operatingSystemUtils.hostPlatform != HostPlatform.linux_arm64;
@override
bool get canListDevices => _androidSdk != null

View File

@@ -101,6 +101,7 @@ class ApplicationPackageFactory {
}
return WebApplicationPackage(FlutterProject.current());
case TargetPlatform.linux_x64:
case TargetPlatform.linux_arm64:
return applicationBinary == null
? LinuxApp.fromLinuxProject(FlutterProject.current().linux)
: LinuxApp.fromPrebuiltApp(applicationBinary);

View File

@@ -9,6 +9,7 @@ import 'package:process/process.dart';
import 'base/common.dart';
import 'base/file_system.dart';
import 'base/os.dart';
import 'base/platform.dart';
import 'base/utils.dart';
import 'build_info.dart';
@@ -219,6 +220,7 @@ abstract class Artifacts {
fileSystem: globals.fs,
processManager: globals.processManager,
platform: globals.platform,
operatingSystemUtils: globals.os,
);
}
@@ -245,13 +247,16 @@ class CachedArtifacts implements Artifacts {
@required FileSystem fileSystem,
@required Platform platform,
@required Cache cache,
@required OperatingSystemUtils operatingSystemUtils,
}) : _fileSystem = fileSystem,
_platform = platform,
_cache = cache;
_cache = cache,
_operatingSystemUtils = operatingSystemUtils;
final FileSystem _fileSystem;
final Platform _platform;
final Cache _cache;
final OperatingSystemUtils _operatingSystemUtils;
@override
String getArtifactPath(
@@ -270,6 +275,7 @@ class CachedArtifacts implements Artifacts {
return _getIosArtifactPath(artifact, platform, mode, environmentType);
case TargetPlatform.darwin_x64:
case TargetPlatform.linux_x64:
case TargetPlatform.linux_arm64:
case TargetPlatform.windows_x64:
return _getDesktopArtifactPath(artifact, platform, mode);
case TargetPlatform.fuchsia_arm64:
@@ -278,7 +284,7 @@ class CachedArtifacts implements Artifacts {
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
default: // could be null, but that can't be specified as a case.
return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform), mode);
return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
}
}
@@ -294,7 +300,7 @@ class CachedArtifacts implements Artifacts {
final String engineDir = _getEngineArtifactsPath(platform, mode);
return _fileSystem.path.join(engineDir, _artifactToFileName(artifact));
}
return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform), mode);
return _getHostArtifactPath(artifact, platform ?? _currentHostPlatform(_platform, _operatingSystemUtils), mode);
}
String _getAndroidArtifactPath(Artifact artifact, TargetPlatform platform, BuildMode mode) {
@@ -480,6 +486,7 @@ class CachedArtifacts implements Artifacts {
final String platformName = getNameForTargetPlatform(platform);
switch (platform) {
case TargetPlatform.linux_x64:
case TargetPlatform.linux_arm64:
case TargetPlatform.darwin_x64:
case TargetPlatform.windows_x64:
// TODO(jonahwilliams): remove once debug desktop artifacts are uploaded
@@ -516,12 +523,13 @@ class CachedArtifacts implements Artifacts {
bool get isLocalEngine => false;
}
TargetPlatform _currentHostPlatform(Platform platform) {
TargetPlatform _currentHostPlatform(Platform platform, OperatingSystemUtils operatingSystemUtils) {
if (platform.isMacOS) {
return TargetPlatform.darwin_x64;
}
if (platform.isLinux) {
return TargetPlatform.linux_x64;
return operatingSystemUtils.hostPlatform == HostPlatform.linux_x64 ?
TargetPlatform.linux_x64 : TargetPlatform.linux_arm64;
}
if (platform.isWindows) {
return TargetPlatform.windows_x64;
@@ -529,19 +537,6 @@ TargetPlatform _currentHostPlatform(Platform platform) {
throw UnimplementedError('Host OS not supported.');
}
HostPlatform _currentHostPlatformAsHost(Platform platform) {
if (platform.isMacOS) {
return HostPlatform.darwin_x64;
}
if (platform.isLinux) {
return HostPlatform.linux_x64;
}
if (platform.isWindows) {
return HostPlatform.windows_x64;
}
throw UnimplementedError('Host OS not supported.');
}
String _getIosEngineArtifactPath(String engineDirectory,
EnvironmentType environmentType, FileSystem fileSystem) {
final Directory xcframeworkDirectory = fileSystem
@@ -583,10 +578,12 @@ class LocalEngineArtifacts implements Artifacts {
@required Cache cache,
@required ProcessManager processManager,
@required Platform platform,
@required OperatingSystemUtils operatingSystemUtils,
}) : _fileSystem = fileSystem,
_cache = cache,
_processManager = processManager,
_platform = platform;
_platform = platform,
_operatingSystemUtils = operatingSystemUtils;
final String engineOutPath; // TODO(goderbauer): This should be private.
final String _hostEngineOutPath;
@@ -594,6 +591,7 @@ class LocalEngineArtifacts implements Artifacts {
final Cache _cache;
final ProcessManager _processManager;
final Platform _platform;
final OperatingSystemUtils _operatingSystemUtils;
@override
String getArtifactPath(
@@ -602,7 +600,7 @@ class LocalEngineArtifacts implements Artifacts {
BuildMode mode,
EnvironmentType environmentType,
}) {
platform ??= _currentHostPlatform(_platform);
platform ??= _currentHostPlatform(_platform, _operatingSystemUtils);
final bool isDirectoryArtifact = artifact == Artifact.flutterWebSdk || artifact == Artifact.flutterPatchedSdkPath;
final String artifactFileName = isDirectoryArtifact ? null : _artifactToFileName(artifact, platform, mode);
switch (artifact) {
@@ -745,12 +743,11 @@ class LocalEngineArtifacts implements Artifacts {
}
String _flutterTesterPath(TargetPlatform platform) {
final HostPlatform hostPlatform = _currentHostPlatformAsHost(_platform);
if (hostPlatform == HostPlatform.linux_x64) {
if (_platform.isLinux) {
return _fileSystem.path.join(engineOutPath, _artifactToFileName(Artifact.flutterTester));
} else if (hostPlatform == HostPlatform.darwin_x64) {
} else if (_platform.isMacOS) {
return _fileSystem.path.join(engineOutPath, 'flutter_tester');
} else if (hostPlatform == HostPlatform.windows_x64) {
} else if (_platform.isWindows) {
return _fileSystem.path.join(engineOutPath, 'flutter_tester.exe');
}
throw Exception('Unsupported platform $platform.');

View File

@@ -313,6 +313,7 @@ class AOTSnapshotter {
TargetPlatform.ios,
TargetPlatform.darwin_x64,
TargetPlatform.linux_x64,
TargetPlatform.linux_arm64,
TargetPlatform.windows_x64,
].contains(platform);
}

View File

@@ -261,8 +261,30 @@ class _PosixUtils extends OperatingSystemUtils {
@override
String get pathVarSeparator => ':';
HostPlatform _hostPlatform;
@override
HostPlatform hostPlatform = HostPlatform.linux_x64;
HostPlatform get hostPlatform {
if (_hostPlatform == null) {
final RunResult hostPlatformCheck =
_processUtils.runSync(<String>['uname', '-m']);
// On x64 stdout is "uname -m: x86_64"
// On arm64 stdout is "uname -m: aarch64, arm64_v8a"
if (hostPlatformCheck.exitCode != 0) {
_logger.printError(
'Error trying to run uname -m'
'\nstdout: ${hostPlatformCheck.stdout}'
'\nstderr: ${hostPlatformCheck.stderr}',
);
_hostPlatform = HostPlatform.linux_x64;
} else if (hostPlatformCheck.stdout.trim().endsWith('x86_64')) {
_hostPlatform = HostPlatform.linux_x64;
} else {
_hostPlatform = HostPlatform.linux_arm64;
}
}
return _hostPlatform;
}
}
class _MacOSUtils extends _PosixUtils {
@@ -297,8 +319,6 @@ class _MacOSUtils extends _PosixUtils {
return _name;
}
HostPlatform _hostPlatform;
// On ARM returns arm64, even when this process is running in Rosetta.
@override
HostPlatform get hostPlatform {

View File

@@ -456,6 +456,7 @@ enum HostPlatform {
darwin_x64,
darwin_arm,
linux_x64,
linux_arm64,
windows_x64,
}
@@ -467,6 +468,8 @@ String getNameForHostPlatform(HostPlatform platform) {
return 'darwin-arm';
case HostPlatform.linux_x64:
return 'linux-x64';
case HostPlatform.linux_arm64:
return 'linux-arm64';
case HostPlatform.windows_x64:
return 'windows-x64';
}
@@ -480,6 +483,7 @@ enum TargetPlatform {
// darwin_arm64 not yet supported, macOS desktop targets run in Rosetta as x86.
darwin_x64,
linux_x64,
linux_arm64,
windows_x64,
fuchsia_arm64,
fuchsia_x64,
@@ -574,6 +578,8 @@ String getNameForTargetPlatform(TargetPlatform platform, {DarwinArch darwinArch}
return 'darwin-x64';
case TargetPlatform.linux_x64:
return 'linux-x64';
case TargetPlatform.linux_arm64:
return 'linux-arm64';
case TargetPlatform.windows_x64:
return 'windows-x64';
case TargetPlatform.fuchsia_arm64:
@@ -613,6 +619,8 @@ TargetPlatform getTargetPlatformForName(String platform) {
return TargetPlatform.darwin_x64;
case 'linux-x64':
return TargetPlatform.linux_x64;
case 'linux-arm64':
return TargetPlatform.linux_arm64;
case 'windows-x64':
return TargetPlatform.windows_x64;
case 'web-javascript':
@@ -683,7 +691,8 @@ HostPlatform getCurrentHostPlatform() {
return HostPlatform.darwin_x64;
}
if (globals.platform.isLinux) {
return HostPlatform.linux_x64;
// support x64 and arm64 architecture.
return globals.os.hostPlatform;
}
if (globals.platform.isWindows) {
return HostPlatform.windows_x64;
@@ -747,8 +756,12 @@ String getWebBuildDirectory() {
}
/// Returns the Linux build output directory.
String getLinuxBuildDirectory() {
return globals.fs.path.join(getBuildDirectory(), 'linux');
String getLinuxBuildDirectory([TargetPlatform targetPlatform]) {
final String arch = (targetPlatform == null) ?
_getCurrentHostPlatformArchName() :
getNameForTargetPlatformArch(targetPlatform);
final String subDirs = 'linux/' + arch;
return globals.fs.path.join(getBuildDirectory(), subDirs);
}
/// Returns the Windows build output directory.
@@ -813,3 +826,40 @@ enum NullSafetyMode {
/// The null safety mode was not detected. Only supported for 'flutter test'.
autodetect,
}
String _getCurrentHostPlatformArchName() {
final HostPlatform hostPlatform = getCurrentHostPlatform();
return getNameForHostPlatformArch(hostPlatform);
}
String getNameForTargetPlatformArch(TargetPlatform platform) {
switch (platform) {
case TargetPlatform.linux_x64:
case TargetPlatform.darwin_x64:
case TargetPlatform.windows_x64:
return 'x64';
case TargetPlatform.linux_arm64:
return 'arm64';
default:
break;
}
assert(false);
return null;
}
String getNameForHostPlatformArch(HostPlatform platform) {
switch (platform) {
case HostPlatform.darwin_x64:
return 'x64';
case HostPlatform.darwin_arm:
return 'arm';
case HostPlatform.linux_x64:
return 'x64';
case HostPlatform.linux_arm64:
return 'arm64';
case HostPlatform.windows_x64:
return 'x64';
}
assert(false);
return null;
}

View File

@@ -26,7 +26,9 @@ const String _kLinuxDepfile = 'linux_engine_sources.d';
/// Copies the Linux desktop embedding files to the copy directory.
class UnpackLinux extends Target {
const UnpackLinux();
const UnpackLinux(this.targetPlatform);
final TargetPlatform targetPlatform;
@override
String get name => 'unpack_linux';
@@ -52,13 +54,13 @@ class UnpackLinux extends Target {
.getArtifactPath(
Artifact.linuxDesktopPath,
mode: buildMode,
platform: TargetPlatform.linux_x64,
platform: targetPlatform,
);
final String headersPath = environment.artifacts
.getArtifactPath(
Artifact.linuxHeaders,
mode: buildMode,
platform: TargetPlatform.linux_x64,
platform: targetPlatform,
);
final Directory outputDirectory = environment.fileSystem.directory(
environment.fileSystem.path.join(
@@ -75,7 +77,7 @@ class UnpackLinux extends Target {
clientSourcePaths: <String>[headersPath],
icuDataPath: environment.artifacts.getArtifactPath(
Artifact.icuData,
platform: TargetPlatform.linux_x64,
platform: targetPlatform,
)
);
final DepfileService depfileService = DepfileService(
@@ -91,12 +93,14 @@ class UnpackLinux extends Target {
/// Creates a bundle for the Linux desktop target.
abstract class BundleLinuxAssets extends Target {
const BundleLinuxAssets();
const BundleLinuxAssets(this.targetPlatform);
final TargetPlatform targetPlatform;
@override
List<Target> get dependencies => const <Target>[
KernelSnapshot(),
UnpackLinux(),
List<Target> get dependencies => <Target>[
const KernelSnapshot(),
UnpackLinux(targetPlatform),
];
@override
@@ -132,7 +136,7 @@ abstract class BundleLinuxAssets extends Target {
final Depfile depfile = await copyAssets(
environment,
outputDirectory,
targetPlatform: TargetPlatform.linux_x64,
targetPlatform: targetPlatform,
additionalContent: <String, DevFSContent>{
'version.json': DevFSStringContent(versionInfo),
}
@@ -186,10 +190,10 @@ class LinuxAotBundle extends Target {
}
class DebugBundleLinuxAssets extends BundleLinuxAssets {
const DebugBundleLinuxAssets();
const DebugBundleLinuxAssets(TargetPlatform targetPlatform) : super(targetPlatform);
@override
String get name => 'debug_bundle_linux_assets';
String get name => 'debug_bundle_${getNameForTargetPlatform(targetPlatform)}_assets';
@override
List<Source> get inputs => <Source>[
@@ -203,10 +207,10 @@ class DebugBundleLinuxAssets extends BundleLinuxAssets {
}
class ProfileBundleLinuxAssets extends BundleLinuxAssets {
const ProfileBundleLinuxAssets();
const ProfileBundleLinuxAssets(TargetPlatform targetPlatform) : super(targetPlatform);
@override
String get name => 'profile_bundle_linux_assets';
String get name => 'profile_bundle_${getNameForTargetPlatform(targetPlatform)}_assets';
@override
List<Source> get outputs => const <Source>[];
@@ -214,15 +218,15 @@ class ProfileBundleLinuxAssets extends BundleLinuxAssets {
@override
List<Target> get dependencies => <Target>[
...super.dependencies,
const LinuxAotBundle(AotElfProfile(TargetPlatform.linux_x64)),
LinuxAotBundle(AotElfProfile(targetPlatform)),
];
}
class ReleaseBundleLinuxAssets extends BundleLinuxAssets {
const ReleaseBundleLinuxAssets();
const ReleaseBundleLinuxAssets(TargetPlatform targetPlatform) : super(targetPlatform);
@override
String get name => 'release_bundle_linux_assets';
String get name => 'release_bundle_${getNameForTargetPlatform(targetPlatform)}_assets';
@override
List<Source> get outputs => const <Source>[];
@@ -230,6 +234,6 @@ class ReleaseBundleLinuxAssets extends BundleLinuxAssets {
@override
List<Target> get dependencies => <Target>[
...super.dependencies,
const LinuxAotBundle(AotElfRelease(TargetPlatform.linux_x64)),
LinuxAotBundle(AotElfRelease(targetPlatform)),
];
}

View File

@@ -23,6 +23,7 @@ import 'base/os.dart' show OperatingSystemUtils;
import 'base/platform.dart';
import 'base/process.dart';
import 'base/user_messages.dart';
import 'build_info.dart';
import 'convert.dart';
import 'dart/package_map.dart';
import 'dart/pub.dart';
@@ -430,6 +431,10 @@ class Cache {
}
}
String getHostPlatformArchName() {
return getNameForHostPlatformArch(_osUtils.hostPlatform);
}
/// Return a directory in the cache dir. For `pkg`, this will return `bin/cache/pkg`.
Directory getCacheDir(String name) {
final Directory dir = _fileSystem.directory(_fileSystem.path.join(getRoot().path, name));
@@ -996,12 +1001,14 @@ class FlutterSdk extends EngineCachedArtifact {
@override
List<List<String>> getBinaryDirs() {
// Currently only Linux supports both arm64 and x64.
final String arch = cache.getHostPlatformArchName();
return <List<String>>[
<String>['common', 'flutter_patched_sdk.zip'],
<String>['common', 'flutter_patched_sdk_product.zip'],
if (cache.includeAllPlatforms) ...<List<String>>[
<String>['windows-x64', 'windows-x64/artifacts.zip'],
<String>['linux-x64', 'linux-x64/artifacts.zip'],
<String>['linux-$arch', 'linux-$arch/artifacts.zip'],
<String>['darwin-x64', 'darwin-x64/artifacts.zip'],
]
else if (_platform.isWindows)
@@ -1009,7 +1016,7 @@ class FlutterSdk extends EngineCachedArtifact {
else if (_platform.isMacOS)
<String>['darwin-x64', 'darwin-x64/artifacts.zip']
else if (_platform.isLinux)
<String>['linux-x64', 'linux-x64/artifacts.zip'],
<String>['linux-$arch', 'linux-$arch/artifacts.zip'],
];
}
@@ -1091,7 +1098,12 @@ class LinuxEngineArtifacts extends EngineCachedArtifact {
@override
List<List<String>> getBinaryDirs() {
if (_platform.isLinux || ignorePlatformFiltering) {
return _linuxDesktopBinaryDirs;
final String arch = cache.getHostPlatformArchName();
return <List<String>>[
<String>['linux-$arch', 'linux-$arch/linux-$arch-flutter-gtk.zip'],
<String>['linux-$arch-profile', 'linux-$arch-profile/linux-$arch-flutter-gtk.zip'],
<String>['linux-$arch-release', 'linux-$arch-release/linux-$arch-flutter-gtk.zip'],
];
}
return const <List<String>>[];
}
@@ -1480,9 +1492,11 @@ class FontSubsetArtifacts extends EngineCachedArtifact {
@override
List<List<String>> getBinaryDirs() {
const Map<String, List<String>> artifacts = <String, List<String>> {
// Currently only Linux supports both arm64 and x64.
final String arch = cache.getHostPlatformArchName();
final Map<String, List<String>> artifacts = <String, List<String>> {
'macos': <String>['darwin-x64', 'darwin-x64/$artifactName.zip'],
'linux': <String>['linux-x64', 'linux-x64/$artifactName.zip'],
'linux': <String>['linux-$arch', 'linux-$arch/$artifactName.zip'],
'windows': <String>['windows-x64', 'windows-x64/$artifactName.zip'],
};
if (cache.includeAllPlatforms) {
@@ -1613,12 +1627,6 @@ const List<List<String>> _windowsDesktopBinaryDirs = <List<String>>[
<String>['windows-x64-release', 'windows-x64-release/windows-x64-flutter.zip'],
];
const List<List<String>> _linuxDesktopBinaryDirs = <List<String>>[
<String>['linux-x64', 'linux-x64/linux-x64-flutter-gtk.zip'],
<String>['linux-x64-profile', 'linux-x64-profile/linux-x64-flutter-gtk.zip'],
<String>['linux-x64-release', 'linux-x64-release/linux-x64-flutter-gtk.zip'],
];
const List<List<String>> _macOSDesktopBinaryDirs = <List<String>>[
<String>['darwin-x64', 'darwin-x64/FlutterMacOS.framework.zip'],
<String>['darwin-x64-profile', 'darwin-x64-profile/FlutterMacOS.framework.zip'],

View File

@@ -41,9 +41,12 @@ const List<Target> _kDefaultTargets = <Target>[
ProfileMacOSBundleFlutterAssets(),
ReleaseMacOSBundleFlutterAssets(),
// Linux targets
DebugBundleLinuxAssets(),
ProfileBundleLinuxAssets(),
ReleaseBundleLinuxAssets(),
DebugBundleLinuxAssets(TargetPlatform.linux_x64),
DebugBundleLinuxAssets(TargetPlatform.linux_arm64),
ProfileBundleLinuxAssets(TargetPlatform.linux_x64),
ProfileBundleLinuxAssets(TargetPlatform.linux_arm64),
ReleaseBundleLinuxAssets(TargetPlatform.linux_x64),
ReleaseBundleLinuxAssets(TargetPlatform.linux_arm64),
// Web targets
WebServiceWorker(),
ReleaseAndroidApplication(),

View File

@@ -35,7 +35,10 @@ class BuildCommand extends FlutterCommand {
addSubcommand(BuildBundleCommand(verboseHelp: verboseHelp));
addSubcommand(BuildWebCommand(verboseHelp: verboseHelp));
addSubcommand(BuildMacosCommand(verboseHelp: verboseHelp));
addSubcommand(BuildLinuxCommand(verboseHelp: verboseHelp));
addSubcommand(BuildLinuxCommand(
operatingSystemUtils: globals.os,
verboseHelp: verboseHelp
));
addSubcommand(BuildWindowsCommand(verboseHelp: verboseHelp));
addSubcommand(BuildFuchsiaCommand(verboseHelp: verboseHelp));
}

View File

@@ -34,6 +34,7 @@ class BuildBundleCommand extends BuildSubCommand {
'ios',
'darwin-x64',
'linux-x64',
'linux-arm64',
'windows-x64',
],
)

View File

@@ -4,8 +4,11 @@
// @dart = 2.8
import 'package:meta/meta.dart';
import '../base/analyze_size.dart';
import '../base/common.dart';
import '../base/os.dart';
import '../build_info.dart';
import '../cache.dart';
import '../features.dart';
@@ -17,10 +20,29 @@ import 'build.dart';
/// A command to build a linux desktop target through a build shell script.
class BuildLinuxCommand extends BuildSubCommand {
BuildLinuxCommand({ bool verboseHelp = false }) {
BuildLinuxCommand({
@required OperatingSystemUtils operatingSystemUtils,
bool verboseHelp = false,
}) : _operatingSystemUtils = operatingSystemUtils {
addCommonDesktopBuildOptions(verboseHelp: verboseHelp);
final String defaultTargetPlatform =
(_operatingSystemUtils.hostPlatform == HostPlatform.linux_arm64) ?
'linux-arm64' : 'linux-x64';
argParser.addOption('target-platform',
defaultsTo: defaultTargetPlatform,
allowed: <String>['linux-arm64', 'linux-x64'],
help: 'The target platform for which the app is compiled.',
);
argParser.addOption('target-sysroot',
defaultsTo: '/',
help: 'The root filesystem path of target platform for which '
'the app is compiled. This option is valid only '
'if the current host and target architectures are different.',
);
}
final OperatingSystemUtils _operatingSystemUtils;
@override
final String name = 'linux';
@@ -39,12 +61,29 @@ class BuildLinuxCommand extends BuildSubCommand {
Future<FlutterCommandResult> runCommand() async {
final BuildInfo buildInfo = await getBuildInfo();
final FlutterProject flutterProject = FlutterProject.current();
final TargetPlatform targetPlatform =
getTargetPlatformForName(stringArg('target-platform'));
final bool needCrossBuild =
getNameForHostPlatformArch(_operatingSystemUtils.hostPlatform)
!= getNameForTargetPlatformArch(targetPlatform);
if (!featureFlags.isLinuxEnabled) {
throwToolExit('"build linux" is not currently supported.');
}
if (!globals.platform.isLinux) {
throwToolExit('"build linux" only supported on Linux hosts.');
}
// Cross-building for x64 targets on arm64 hosts is not supported.
if (_operatingSystemUtils.hostPlatform != HostPlatform.linux_x64 &&
targetPlatform != TargetPlatform.linux_arm64) {
throwToolExit('"cross-building" only supported on Linux x64 hosts.');
}
// TODO(fujino): https://github.com/flutter/flutter/issues/74929
if (_operatingSystemUtils.hostPlatform == HostPlatform.linux_x64 &&
targetPlatform == TargetPlatform.linux_arm64) {
throwToolExit(
'Cross-build from Linux x64 host to Linux arm64 target is not currently supported.');
}
displayNullSafetyMode(buildInfo);
await buildLinux(
flutterProject.linux,
@@ -55,6 +94,9 @@ class BuildLinuxCommand extends BuildSubCommand {
logger: globals.logger,
flutterUsage: globals.flutterUsage,
),
needCrossBuild: needCrossBuild,
targetPlatform: targetPlatform,
targetSysroot: stringArg('target-sysroot'),
);
return FlutterCommandResult.success();
}

View File

@@ -105,6 +105,7 @@ Future<T> runInContext<T>(
AndroidWorkflow: () => AndroidWorkflow(
androidSdk: globals.androidSdk,
featureFlags: featureFlags,
operatingSystemUtils: globals.os,
),
ApplicationPackageFactory: () => ApplicationPackageFactory(
userMessages: globals.userMessages,
@@ -117,6 +118,7 @@ Future<T> runInContext<T>(
fileSystem: globals.fs,
cache: globals.cache,
platform: globals.platform,
operatingSystemUtils: globals.os,
),
AssetBundleFactory: () {
return AssetBundleFactory.defaultInstance(

View File

@@ -394,6 +394,7 @@ class FlutterDeviceManager extends DeviceManager {
config: config,
logger: logger,
artifacts: artifacts,
operatingSystemUtils: operatingSystemUtils,
),
MacOSDevices(
processManager: processManager,

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,
);
}

View File

@@ -56,9 +56,20 @@ class CmakeCustomCommandMigration extends ProjectMigrator {
final String addCustomCommandReplacement = '$addCustomCommandOriginal\n VERBATIM';
newProjectContents = newProjectContents.replaceAll(addCustomCommandOriginal, addCustomCommandReplacement);
}
// CMake's add_custom_command() should add FLUTTER_TARGET_PLATFORM to support multi-architecture.
// However, developers would get the following warning every time if we do nothing.
// ------------------------------
// CMake Warning:
// Manually-specified variables were not used by the project:
// FLUTTER_TARGET_PLATFORM
// ------------------------------
if (addCustomCommandOriginal?.contains('linux-x64') == true) {
newProjectContents = newProjectContents.replaceAll('linux-x64', r'${FLUTTER_TARGET_PLATFORM}');
}
}
if (originalProjectContents != newProjectContents) {
logger.printStatus('add_custom_command() missing VERBATIM, updating.');
logger.printStatus('add_custom_command() missing VERBATIM or FLUTTER_TARGET_PLATFORM, updating.');
_cmakeFile.writeAsStringSync(newProjectContents.toString());
}
return true;

View File

@@ -1410,6 +1410,7 @@ DevelopmentArtifact _artifactFromTargetPlatform(TargetPlatform targetPlatform) {
}
return null;
case TargetPlatform.linux_x64:
case TargetPlatform.linux_arm64:
if (featureFlags.isLinuxEnabled) {
return DevelopmentArtifact.linux;
}

View File

@@ -15,6 +15,7 @@ import '../base/config.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/os.dart';
import '../build_info.dart';
import '../bundle.dart';
import '../convert.dart';
@@ -55,12 +56,14 @@ class FlutterTesterDevice extends Device {
@required String buildDirectory,
@required FileSystem fileSystem,
@required Artifacts artifacts,
@required OperatingSystemUtils operatingSystemUtils,
}) : _processManager = processManager,
_flutterVersion = flutterVersion,
_logger = logger,
_buildDirectory = buildDirectory,
_fileSystem = fileSystem,
_artifacts = artifacts,
_operatingSystemUtils = operatingSystemUtils,
super(
deviceId,
platformType: null,
@@ -74,6 +77,7 @@ class FlutterTesterDevice extends Device {
final String _buildDirectory;
final FileSystem _fileSystem;
final Artifacts _artifacts;
final OperatingSystemUtils _operatingSystemUtils;
Process _process;
final DevicePortForwarder _portForwarder = const NoOpDevicePortForwarder();
@@ -162,7 +166,7 @@ class FlutterTesterDevice extends Device {
mainPath: mainPath,
applicationKernelFilePath: applicationKernelFilePath,
trackWidgetCreation: buildInfo.trackWidgetCreation,
platform: getTargetPlatformForName(getNameForHostPlatform(getCurrentHostPlatform())),
platform: getTargetPlatformForName(getNameForHostPlatform(_operatingSystemUtils.hostPlatform)),
treeShakeIcons: buildInfo.treeShakeIcons,
);
@@ -270,6 +274,7 @@ class FlutterTesterDevices extends PollingDeviceDiscovery {
@required Logger logger,
@required FlutterVersion flutterVersion,
@required Config config,
@required OperatingSystemUtils operatingSystemUtils,
}) : _testerDevice = FlutterTesterDevice(
kTesterDeviceId,
fileSystem: fileSystem,
@@ -278,6 +283,7 @@ class FlutterTesterDevices extends PollingDeviceDiscovery {
buildDirectory: getBuildDirectory(config, fileSystem),
logger: logger,
flutterVersion: flutterVersion,
operatingSystemUtils: operatingSystemUtils,
),
super('Flutter tester');