Skip to content

Commit 8d46843

Browse files
authored
[flutter_tools] Move CpuArch to flutter_tools_core and define HostPlatform by osName and cpuArch (flutter#195841)
Moves `CpuArch` to `package:flutter_tools_core` as an extensible `@immutable final class` (re-exported from `build_info.dart`) and updates `HostPlatform` to be defined by `osName` and `cpuArch`, exposing `cpuArchName`, `cliName`, and `name` getters while validating single-segment lowercase alphanumeric OS and architecture identifiers. Replaces `CpuArch.fromHostPlatform` call sites with `HostPlatform.cpuArch`. ### Stack - flutter#195352 (merged) - **flutter#195841 (this PR)** - flutter#194588 Part of flutter#190755 Part of flutter#190754
1 parent f8bb491 commit 8d46843

18 files changed

Lines changed: 270 additions & 159 deletions

File tree

‎packages/flutter_tools/lib/src/android/android_device.dart‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ class AndroidDevice extends Device {
190190
@override
191191
late final Future<TargetPlatform> targetPlatform = () async {
192192
return switch (await cpuArch) {
193-
CpuArch.arm64 => TargetPlatform.android_arm64,
194-
CpuArch.armv7 => TargetPlatform.android_arm,
195-
CpuArch.x64 => TargetPlatform.android_x64,
196-
CpuArch.x86 || CpuArch.riscv64 || CpuArch.unknown => TargetPlatform.unsupported,
193+
.arm64 => TargetPlatform.android_arm64,
194+
.armv7 => TargetPlatform.android_arm,
195+
.x64 => TargetPlatform.android_x64,
196+
_ => TargetPlatform.unsupported,
197197
};
198198
}();
199199

@@ -532,14 +532,12 @@ class AndroidDevice extends Device {
532532
var builtPackage = package;
533533
final CpuArch cpuArch = await this.cpuArch;
534534
switch (cpuArch) {
535-
case CpuArch.armv7:
536-
case CpuArch.arm64:
537-
case CpuArch.x64:
535+
case .armv7:
536+
case .arm64:
537+
case .x64:
538538
break;
539-
case CpuArch.x86:
540-
case CpuArch.riscv64:
541-
case CpuArch.unknown:
542-
_logger.printError('Android platforms are only supported.');
539+
default:
540+
_logger.printError('Unsupported Android CPU architecture: $cpuArch');
543541
return LaunchResult.failed();
544542
}
545543

‎packages/flutter_tools/lib/src/build_info.dart‎

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
library;
77

88
import 'package:flutter_tools_core/flutter_tools_core.dart' as tools_core;
9+
import 'package:flutter_tools_core/flutter_tools_core.dart' show CpuArch;
910
import 'package:meta/meta.dart';
1011

1112
import 'package:package_config/package_config_types.dart';
@@ -20,6 +21,8 @@ import 'context/tool_context.dart';
2021
import 'convert.dart';
2122
import 'darwin/darwin.dart';
2223

24+
export 'package:flutter_tools_core/flutter_tools_core.dart' show CpuArch;
25+
2326
/// Whether icon font subsetting is enabled by default.
2427
const kIconTreeShakerEnabledDefault = true;
2528

@@ -576,84 +579,48 @@ bool isEmulatorBuildMode(BuildMode mode) {
576579
return mode == BuildMode.debug;
577580
}
578581

579-
/// Platform-agnostic CPU architecture type.
580-
enum CpuArch {
581-
/// Unknown architecture. Used when the architecture is not relevant.
582-
unknown,
583-
armv7,
584-
arm64,
585-
x86,
586-
x64,
587-
riscv64;
588-
589-
factory CpuArch.fromName(String name) {
590-
return switch (name) {
591-
'unknown' => .unknown,
592-
'armv7' => .armv7,
593-
'arm64' => .arm64,
594-
'x86' => .x86,
595-
'x64' || 'x86_64' => .x64,
596-
'riscv64' => .riscv64,
597-
_ => throw Exception('Unsupported CPU arch name "$name"'),
598-
};
599-
}
600-
601-
/// The [CpuArch] of the given [hostPlatform].
602-
factory CpuArch.fromHostPlatform(HostPlatform hostPlatform) {
603-
return switch (hostPlatform) {
604-
.darwin_x64 || .linux_x64 || .windows_x64 => .x64,
605-
.darwin_arm64 || .linux_arm64 || .windows_arm64 => .arm64,
606-
.linux_riscv64 => .riscv64,
607-
_ => CpuArch.fromName(hostPlatform.platformName),
608-
};
609-
}
610-
582+
/// Platform-specific naming helpers on [CpuArch] for built-in Flutter tool
583+
/// targets.
584+
extension CpuArchExtension on CpuArch {
611585
/// Returns the Dart SDK's name for the specified target architecture.
612586
///
613587
/// When building for Darwin platforms, the tool invokes architecture-specific
614588
/// variants of `gen_snapshot`, one for each target architecture. The output
615589
/// instructions are then built into architecture-specific binaries, which are
616590
/// merged into a universal binary using the `lipo` tool.
617-
String get dartName {
618-
return switch (this) {
619-
armv7 => 'armv7',
620-
arm64 => 'arm64',
621-
x86 => 'x86',
622-
x64 => 'x64',
623-
riscv64 => 'riscv64',
624-
unknown => throw UnsupportedError('Unexpected CPU arch $this'),
625-
};
626-
}
591+
String get dartName => switch (this) {
592+
.armv7 || .arm64 || .x86 || .x64 || .riscv64 => name,
593+
_ => throw UnsupportedError('Unexpected CPU arch $this'),
594+
};
627595

628596
/// The Apple architecture name for this architecture.
629597
///
630598
/// This is the name understood by the Darwin toolchain (e.g. `lipo`, `clang`,
631599
/// and the `-arch` flag) and used for architecture-specific build output
632-
/// directories on iOS and macOS. This differs from [dartName] for [x64],
633-
/// which maps to `x86_64` here.
600+
/// directories on iOS and macOS. This differs from [dartName] for
601+
/// [CpuArch.x64], which maps to `x86_64` here.
634602
String get darwinArchName => switch (this) {
635-
armv7 => 'armv7',
636-
arm64 => 'arm64',
637-
x64 => 'x86_64',
638-
x86 || riscv64 || unknown => throw UnsupportedError('Unexpected Darwin CPU arch $this'),
603+
.armv7 || .arm64 => name,
604+
.x64 => 'x86_64',
605+
_ => throw UnsupportedError('Unexpected Darwin CPU arch $this'),
639606
};
640607

641608
/// The name of the Android ABI (as used in `jniLibs` directories) for this
642609
/// architecture.
643610
String get androidArchName => switch (this) {
644-
armv7 => 'armeabi-v7a',
645-
arm64 => 'arm64-v8a',
646-
x64 => 'x86_64',
647-
x86 || riscv64 || unknown => throw UnsupportedError('Unexpected Android CPU arch $this'),
611+
.armv7 => 'armeabi-v7a',
612+
.arm64 => 'arm64-v8a',
613+
.x64 => 'x86_64',
614+
_ => throw UnsupportedError('Unexpected Android CPU arch $this'),
648615
};
649616

650617
/// The `TargetPlatform` name of the Android platform for this architecture.
651618
String get androidPlatformName => switch (this) {
652-
armv7 => 'android-arm',
653-
arm64 => 'android-arm64',
654-
x64 => 'android-x64',
655-
x86 || riscv64 || unknown => throw UnsupportedError('Unexpected Android CPU arch $this'),
656-
};
619+
.armv7 => TargetPlatform.android_arm,
620+
.arm64 => TargetPlatform.android_arm64,
621+
.x64 => TargetPlatform.android_x64,
622+
_ => throw UnsupportedError('Unexpected Android CPU arch $this'),
623+
}.getName();
657624
}
658625

659626
/// The operating system (or runtime environment) that a [TargetPlatform]
@@ -958,7 +925,7 @@ String getLinuxBuildDirectory(
958925
null => getCurrentHostPlatform(
959926
operatingSystemUtils: operatingSystemUtils,
960927
platform: platform,
961-
).platformName,
928+
).cpuArchName,
962929
};
963930
final String subDirs = fileSystem.path.joinAll(<String>[
964931
'linux',

‎packages/flutter_tools/lib/src/cache.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ class Cache {
643643
}
644644

645645
String getHostPlatformArchName() {
646-
return _osUtils.hostPlatform.platformName;
646+
return _osUtils.hostPlatform.cpuArchName;
647647
}
648648

649649
/// Return a directory in the cache dir. For `pkg`, this will return `bin/cache/pkg`.

‎packages/flutter_tools/lib/src/commands/build_linux.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class BuildLinuxCommand extends BuildSubCommand {
9595

9696
final BuildInfo buildInfo = await getBuildInfo();
9797
final TargetPlatform targetPlatform = getValue(_targetPlatformOption);
98-
final needCrossBuild = os.hostPlatform.platformName != targetPlatform.simpleName;
98+
final needCrossBuild = os.hostPlatform.cpuArchName != targetPlatform.simpleName;
9999

100100
if (!_featureFlags.isLinuxEnabled) {
101101
throwToolExit(

‎packages/flutter_tools/lib/src/experimental/extension_manager.dart‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ class ExtensionManager {
6868
List<ExtensionConnection> get connections => _discovery.connections;
6969

7070
/// The operating system name of the host platform (e.g. `'linux'`, `'macos'`, `'windows'`).
71-
String get hostPlatformName {
72-
final String os = hostPlatform.cliName.split('-').first;
73-
return os == 'darwin' ? 'macos' : os;
74-
}
71+
String get hostPlatformName => hostPlatform.osName == 'darwin' ? 'macos' : hostPlatform.osName;
7572

7673
/// Spawns entrypoints without host OS checks; disposes any extension that reports
7774
/// it does not support [hostPlatform].

‎packages/flutter_tools/lib/src/ios/mac.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ Future<XcodeBuildResult> buildXcodeProject({
484484
if (!hasWatchCompanion) {
485485
// ONLY_ACTIVE_ARCH specifies whether the product includes only code for
486486
// the native architecture.
487-
final HostPlatform hostPlatform = os.hostPlatform;
488-
final onlyActiveArch = activeArch == CpuArch.fromHostPlatform(hostPlatform);
487+
final onlyActiveArch = activeArch == os.hostPlatform.cpuArch;
489488

490489
buildCommands.add('ONLY_ACTIVE_ARCH=${onlyActiveArch ? 'YES' : 'NO'}');
491490
buildCommands.add('ARCHS=${activeArch.darwinArchName}');

‎packages/flutter_tools/lib/src/ios/simulators.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class IOSSimulatorUtils {
9898
}
9999

100100
final ToolContext(:Logger logger, :OperatingSystemUtils os) = _toolContext;
101-
final cpuArch = CpuArch.fromHostPlatform(os.hostPlatform);
101+
final CpuArch cpuArch = os.hostPlatform.cpuArch;
102102

103103
final List<BootedSimDevice> connected = await _simControl.getConnectedDevices();
104104
return connected

‎packages/flutter_tools/lib/src/isolated/native_assets/android/native_assets.dart‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ Future<List<File>> copyNativeCodeAssetsAndroid(
4949
/// Get the [Architecture] for [cpuArch].
5050
Architecture getNativeAndroidArchitecture(CpuArch cpuArch) {
5151
return switch (cpuArch) {
52-
CpuArch.armv7 => Architecture.arm,
53-
CpuArch.arm64 => Architecture.arm64,
54-
CpuArch.x64 => Architecture.x64,
55-
CpuArch.x86 ||
56-
CpuArch.riscv64 ||
57-
CpuArch.unknown => throwToolExit('Invalid Android arch: $cpuArch.'),
52+
.armv7 => Architecture.arm,
53+
.arm64 => Architecture.arm64,
54+
.x64 => Architecture.x64,
55+
_ => throwToolExit('Invalid Android arch: $cpuArch.'),
5856
};
5957
}
6058

‎packages/flutter_tools/lib/src/isolated/native_assets/ios/native_assets.dart‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ IOSSdk getIOSSdk(EnvironmentType environmentType) {
2525
/// Extract the [Architecture] from a [CpuArch].
2626
Architecture getNativeIOSArchitecture(CpuArch cpuArch) {
2727
return switch (cpuArch) {
28-
CpuArch.armv7 => Architecture.arm,
29-
CpuArch.arm64 => Architecture.arm64,
30-
CpuArch.x64 => Architecture.x64,
31-
CpuArch.x86 ||
32-
CpuArch.riscv64 ||
33-
CpuArch.unknown => throw Exception('Unknown iOS CPU arch: $cpuArch.'),
28+
.armv7 => Architecture.arm,
29+
.arm64 => Architecture.arm64,
30+
.x64 => Architecture.x64,
31+
_ => throw Exception('Unknown iOS CPU arch: $cpuArch.'),
3432
};
3533
}
3634

‎packages/flutter_tools/lib/src/isolated/native_assets/macos/native_assets.dart‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ const targetMacOSVersion = 13;
1818
/// Extract the [Architecture] from a [CpuArch].
1919
Architecture getNativeMacOSArchitecture(CpuArch cpuArch) {
2020
return switch (cpuArch) {
21-
CpuArch.arm64 => Architecture.arm64,
22-
CpuArch.x64 => Architecture.x64,
23-
CpuArch.armv7 ||
24-
CpuArch.x86 ||
25-
CpuArch.riscv64 ||
26-
CpuArch.unknown => throw Exception('Unknown macOS CPU arch: $cpuArch.'),
21+
.arm64 => Architecture.arm64,
22+
.x64 => Architecture.x64,
23+
_ => throw Exception('Unknown macOS CPU arch: $cpuArch.'),
2724
};
2825
}
2926

0 commit comments

Comments
 (0)