Unverified Commit 4dffc851 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Detect ARM macOS arch with sysctl hw.optional.arm64 (#67970)

parent 0343555a
......@@ -328,7 +328,7 @@ abstract class IOSApp extends ApplicationPackage {
}
static Future<IOSApp> fromIosProject(IosProject project, BuildInfo buildInfo) {
if (getCurrentHostPlatform() != HostPlatform.darwin_x64) {
if (!globals.platform.isMacOS) {
return null;
}
if (!project.exists) {
......
......@@ -7,6 +7,7 @@ import 'package:file/file.dart';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../build_info.dart';
import '../globals.dart' as globals;
import 'common.dart';
import 'file_system.dart';
......@@ -29,6 +30,13 @@ abstract class OperatingSystemUtils {
platform: platform,
processManager: processManager,
);
} else if (platform.isMacOS) {
return _MacOSUtils(
fileSystem: fileSystem,
logger: logger,
platform: platform,
processManager: processManager,
);
} else {
return _PosixUtils(
fileSystem: fileSystem,
......@@ -112,6 +120,8 @@ abstract class OperatingSystemUtils {
return osNames.containsKey(osName) ? osNames[osName] : osName;
}
HostPlatform get hostPlatform;
List<File> _which(String execName, { bool all = false });
/// Returns the separator between items in the PATH environment variable.
......@@ -246,30 +256,63 @@ class _PosixUtils extends OperatingSystemUtils {
return _fileSystem.file(path);
}
@override
String get pathVarSeparator => ':';
@override
HostPlatform hostPlatform = HostPlatform.linux_x64;
}
class _MacOSUtils extends _PosixUtils {
_MacOSUtils({
@required FileSystem fileSystem,
@required Logger logger,
@required Platform platform,
@required ProcessManager processManager,
}) : super(
fileSystem: fileSystem,
logger: logger,
platform: platform,
processManager: processManager,
);
String _name;
@override
String get name {
if (_name == null) {
if (_platform.isMacOS) {
final List<RunResult> results = <RunResult>[
_processUtils.runSync(<String>['sw_vers', '-productName']),
_processUtils.runSync(<String>['sw_vers', '-productVersion']),
_processUtils.runSync(<String>['sw_vers', '-buildVersion']),
_processUtils.runSync(<String>['uname', '-m']),
];
if (results.every((RunResult result) => result.exitCode == 0)) {
_name = '${results[0].stdout.trim()} ${results[1].stdout
.trim()} ${results[2].stdout.trim()} ${results[3].stdout.trim()}';
}
_name =
'${results[0].stdout.trim()} ${results[1].stdout.trim()} ${results[2].stdout.trim()} ${getNameForHostPlatform(hostPlatform)}';
}
_name ??= super.name;
}
return _name;
}
HostPlatform _hostPlatform;
// On ARM returns arm64, even when this process is running in Rosetta.
@override
String get pathVarSeparator => ':';
HostPlatform get hostPlatform {
if (_hostPlatform == null) {
final RunResult arm64Check =
_processUtils.runSync(<String>['sysctl', 'hw.optional.arm64']);
// hw.optional.arm64 is unavailable on < macOS 11 and exits with 1, assume x86 on failure.
// On arm64 stdout is "sysctl hw.optional.arm64: 1"
if (arm64Check.exitCode == 0 && arm64Check.stdout.trim().endsWith('1')) {
_hostPlatform = HostPlatform.darwin_arm;
} else {
_hostPlatform = HostPlatform.darwin_x64;
}
}
return _hostPlatform;
}
}
class _WindowsUtils extends OperatingSystemUtils {
......@@ -285,6 +328,9 @@ class _WindowsUtils extends OperatingSystemUtils {
processManager: processManager,
);
@override
HostPlatform hostPlatform = HostPlatform.windows_x64;
@override
void makeExecutable(File file) {}
......
......@@ -398,6 +398,7 @@ bool isEmulatorBuildMode(BuildMode mode) {
enum HostPlatform {
darwin_x64,
darwin_arm,
linux_x64,
windows_x64,
}
......@@ -406,6 +407,8 @@ String getNameForHostPlatform(HostPlatform platform) {
switch (platform) {
case HostPlatform.darwin_x64:
return 'darwin-x64';
case HostPlatform.darwin_arm:
return 'darwin-arm';
case HostPlatform.linux_x64:
return 'linux-x64';
case HostPlatform.windows_x64:
......@@ -418,6 +421,7 @@ String getNameForHostPlatform(HostPlatform platform) {
enum TargetPlatform {
android,
ios,
// darwin_arm64 not yet supported, macOS desktop targets run in Rosetta as x86.
darwin_x64,
linux_x64,
windows_x64,
......
......@@ -17,6 +17,7 @@ import 'package:flutter_tools/src/base/signals.dart';
import 'package:flutter_tools/src/base/template.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/base/time.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/isolated/mustache_template.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
......@@ -294,6 +295,9 @@ class FakeOperatingSystemUtils implements OperatingSystemUtils {
@override
ProcessResult makeExecutable(File file) => null;
@override
HostPlatform hostPlatform = HostPlatform.linux_x64;
@override
void chmod(FileSystemEntity entity, String mode) { }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment