Skip to content

Commit dd69590

Browse files
committed
refactor: Replace get hostPlatform with Dart's Abi.current (#184358)
1 parent d69ed56 commit dd69590

1 file changed

Lines changed: 18 additions & 47 deletions

File tree

  • packages/flutter_tools/lib/src/base

packages/flutter_tools/lib/src/base/os.dart

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,13 @@ class _PosixUtils extends OperatingSystemUtils {
265265
@override
266266
HostPlatform get hostPlatform {
267267
if (_hostPlatform == null) {
268-
final RunResult hostPlatformCheck = _processUtils.runSync(<String>['uname', '-m']);
269-
// On x64 stdout is "uname -m: x86_64"
270-
// On arm64 stdout is "uname -m: aarch64, arm64_v8a"
271-
if (hostPlatformCheck.exitCode != 0) {
272-
_hostPlatform = HostPlatform.linux_x64;
273-
_logger.printError(
274-
'Encountered an error trying to run "uname -m":\n'
275-
' exit code: ${hostPlatformCheck.exitCode}\n'
276-
' stdout: ${hostPlatformCheck.stdout.trimRight()}\n'
277-
' stderr: ${hostPlatformCheck.stderr.trimRight()}\n'
278-
'Assuming host platform is ${getNameForHostPlatform(_hostPlatform!)}.',
279-
);
280-
} else if (hostPlatformCheck.stdout.trim().endsWith('x86_64')) {
281-
_hostPlatform = HostPlatform.linux_x64;
282-
} else if (hostPlatformCheck.stdout.trim().endsWith('riscv64')) {
283-
_hostPlatform = HostPlatform.linux_riscv64;
284-
} else {
285-
// We default to ARM if it's not x86_64 and we did not get an error.
286-
_hostPlatform = HostPlatform.linux_arm64;
287-
}
268+
final abi = Abi.current();
269+
_hostPlatform = switch (abi) {
270+
Abi.linuxArm64 || Abi.fuchsiaArm64 || Abi.androidArm64 => HostPlatform.linux_arm64,
271+
Abi.linuxX64 || Abi.fuchsiaX64 || Abi.androidX64 => HostPlatform.linux_x64,
272+
Abi.linuxRiscv64 || Abi.fuchsiaRiscv64 || Abi.androidRiscv64 => HostPlatform.linux_riscv64,
273+
_ => throwToolExit('Host platform and architecture "$abi" not supported for Posix.'),
274+
};
288275
}
289276
return _hostPlatform!;
290277
}
@@ -390,33 +377,15 @@ class _MacOSUtils extends _PosixUtils {
390377
return _name!;
391378
}
392379

393-
// On ARM returns arm64, even when this process is running in Rosetta.
394380
@override
395381
HostPlatform get hostPlatform {
396382
if (_hostPlatform == null) {
397-
String? sysctlPath;
398-
if (which('sysctl') == null) {
399-
// Fallback to known install locations.
400-
for (final path in <String>['/usr/sbin/sysctl', '/sbin/sysctl']) {
401-
if (_fileSystem.isFileSync(path)) {
402-
sysctlPath = path;
403-
}
404-
}
405-
} else {
406-
sysctlPath = 'sysctl';
407-
}
408-
409-
if (sysctlPath == null) {
410-
throwToolExit('sysctl not found. Try adding it to your PATH environment variable.');
411-
}
412-
final RunResult arm64Check = _processUtils.runSync(<String>[sysctlPath, 'hw.optional.arm64']);
413-
// On arm64 stdout is "sysctl hw.optional.arm64: 1"
414-
// On x86 hw.optional.arm64 is unavailable and exits with 1.
415-
if (arm64Check.exitCode == 0 && arm64Check.stdout.trim().endsWith('1')) {
416-
_hostPlatform = HostPlatform.darwin_arm64;
417-
} else {
418-
_hostPlatform = HostPlatform.darwin_x64;
419-
}
383+
final abi = Abi.current();
384+
_hostPlatform = switch (abi) {
385+
Abi.macosArm64 => HostPlatform.darwin_arm64,
386+
Abi.macosX64 => HostPlatform.darwin_x64,
387+
_ => throwToolExit('Host platform and architecture "$abi" not supported for MacOS.'),
388+
};
420389
}
421390
return _hostPlatform!;
422391
}
@@ -480,9 +449,11 @@ class _WindowsUtils extends OperatingSystemUtils {
480449
HostPlatform get hostPlatform {
481450
if (_hostPlatform == null) {
482451
final abi = Abi.current();
483-
_hostPlatform = (abi == Abi.windowsArm64)
484-
? HostPlatform.windows_arm64
485-
: HostPlatform.windows_x64;
452+
_hostPlatform = switch (abi) {
453+
Abi.windowsArm64 => HostPlatform.windows_arm64,
454+
Abi.windowsX64 => HostPlatform.windows_x64,
455+
_ => throwToolExit('Host platform and architecture "$abi" not supported for Windows.'),
456+
};
486457
}
487458
return _hostPlatform!;
488459
}

0 commit comments

Comments
 (0)