Skip to content

Commit 0c6b0fd

Browse files
authored
[flutter_tools] Replace TargetPlatform.simpleName with cpuArch (flutter#196264)
Replaces the ambiguously named `TargetPlatform.simpleName` getter with a strongly typed `CpuArch get cpuArch` getter on `TargetPlatform` (following up on review feedback from flutter#195841). Updates `getLinuxBuildDirectory`, `getWindowsBuildDirectory`, and `BuildLinuxCommand` to resolve architectures through `TargetPlatform.cpuArch` and `HostPlatform.cpuArch`, and throws `UnsupportedError` on multi-architecture, architecture-independent, or non-native platforms (including removing the legacy `TargetPlatform.darwin => 'x64'` mapping). Part of flutter#190755 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 04cb5e2 commit 0c6b0fd

3 files changed

Lines changed: 86 additions & 25 deletions

File tree

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -749,20 +749,19 @@ enum TargetPlatform {
749749
.unsupported => throw UnsupportedError('Unexpected target platform $this'),
750750
};
751751

752-
String get simpleName => switch (this) {
753-
linux_x64 || darwin || windows_x64 => 'x64',
754-
linux_arm64 || windows_arm64 => 'arm64',
755-
linux_riscv64 => 'riscv64',
756-
android ||
757-
android_arm ||
758-
android_arm64 ||
759-
android_x64 ||
760-
fuchsia_arm64 ||
761-
fuchsia_x64 ||
762-
ios ||
763-
tester ||
764-
web_javascript ||
765-
unsupported => throw UnsupportedError('Unexpected target platform $this'),
752+
/// The [CpuArch] for architecture-specific target platforms.
753+
///
754+
/// Throws an [UnsupportedError] for multi-architecture,
755+
/// architecture-independent, or non-native target platforms ([android],
756+
/// [darwin], [ios], [tester], [web_javascript], and [unsupported]).
757+
CpuArch get cpuArch => switch (this) {
758+
android_arm => .armv7,
759+
android_arm64 || fuchsia_arm64 || linux_arm64 || windows_arm64 => .arm64,
760+
android_x64 || fuchsia_x64 || linux_x64 || windows_x64 => .x64,
761+
linux_riscv64 => .riscv64,
762+
android || darwin || ios || tester || web_javascript || unsupported => throw UnsupportedError(
763+
'Target platform $this does not specify a CPU architecture.',
764+
),
766765
};
767766

768767
static Never throwUnsupportedTarget() =>
@@ -920,16 +919,15 @@ String getLinuxBuildDirectory(
920919
os: OperatingSystemUtils operatingSystemUtils,
921920
:Platform platform,
922921
) = toolContext;
923-
final String arch = switch (targetPlatform) {
924-
final TargetPlatform target => target.simpleName,
925-
null => getCurrentHostPlatform(
926-
operatingSystemUtils: operatingSystemUtils,
927-
platform: platform,
928-
).cpuArchName,
929-
};
922+
final CpuArch cpuArch =
923+
targetPlatform?.cpuArch ??
924+
getCurrentHostPlatform(
925+
operatingSystemUtils: operatingSystemUtils,
926+
platform: platform,
927+
).cpuArch;
930928
final String subDirs = fileSystem.path.joinAll(<String>[
931929
'linux',
932-
arch,
930+
cpuArch.name,
933931
if (flavor != null && flavor.isNotEmpty) flavor,
934932
]);
935933
return fileSystem.path.join(getBuildDirectory(config, fileSystem), subDirs);
@@ -945,10 +943,9 @@ String getWindowsBuildDirectory(
945943
required FileSystem fileSystem,
946944
String? flavor,
947945
}) {
948-
final String arch = targetPlatform.simpleName;
949946
final String subDirs = fileSystem.path.joinAll(<String>[
950947
'windows',
951-
arch,
948+
targetPlatform.cpuArch.name,
952949
if (flavor != null && flavor.isNotEmpty) flavor,
953950
]);
954951
return fileSystem.path.join(getBuildDirectory(config, fileSystem), subDirs);

‎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.cpuArchName != targetPlatform.simpleName;
98+
final needCrossBuild = os.hostPlatform.cpuArch != targetPlatform.cpuArch;
9999

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

‎packages/flutter_tools/test/general.shard/build_info_test.dart‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,43 @@ void main() {
168168
}
169169
});
170170

171+
testWithoutContext(
172+
'TargetPlatform.cpuArch maps architecture-specific platforms and throws for others',
173+
() {
174+
const expected = <TargetPlatform, CpuArch>{
175+
.android_arm: .armv7,
176+
.android_arm64: .arm64,
177+
.android_x64: .x64,
178+
.fuchsia_arm64: .arm64,
179+
.fuchsia_x64: .x64,
180+
.linux_arm64: .arm64,
181+
.linux_riscv64: .riscv64,
182+
.linux_x64: .x64,
183+
.windows_arm64: .arm64,
184+
.windows_x64: .x64,
185+
};
186+
const unsupported = <TargetPlatform>{
187+
.android,
188+
.darwin,
189+
.ios,
190+
.tester,
191+
.web_javascript,
192+
.unsupported,
193+
};
194+
// Every TargetPlatform value must be covered by this test.
195+
expect(<TargetPlatform>{
196+
...expected.keys,
197+
...unsupported,
198+
}, containsAll(TargetPlatform.values));
199+
for (final MapEntry(:TargetPlatform key, :CpuArch value) in expected.entries) {
200+
expect(key.cpuArch, value, reason: '$key');
201+
}
202+
for (final platform in unsupported) {
203+
expect(() => platform.cpuArch, throwsUnsupportedError, reason: '$platform');
204+
}
205+
},
206+
);
207+
171208
testUsingContext(
172209
'defaultIOSArchsForEnvironment',
173210
() {
@@ -684,6 +721,13 @@ void main() {
684721
),
685722
fileSystem.path.join('custom_build_out', 'linux', 'arm64', 'prod'),
686723
);
724+
expect(
725+
getLinuxBuildDirectory(
726+
TargetPlatform.linux_riscv64,
727+
toolContext: FakeToolContext(config: config, fs: fileSystem),
728+
),
729+
fileSystem.path.join('custom_build_out', 'linux', 'riscv64'),
730+
);
687731
expect(
688732
getLinuxBuildDirectory(
689733
null,
@@ -705,6 +749,26 @@ void main() {
705749
),
706750
fileSystem.path.join('custom_build_out', 'windows', 'x64', 'prod'),
707751
);
752+
expect(
753+
getWindowsBuildDirectory(
754+
TargetPlatform.windows_arm64,
755+
config: config,
756+
fileSystem: fileSystem,
757+
),
758+
fileSystem.path.join('custom_build_out', 'windows', 'arm64'),
759+
);
760+
expect(
761+
() => getLinuxBuildDirectory(
762+
TargetPlatform.darwin,
763+
toolContext: FakeToolContext(config: config, fs: fileSystem),
764+
),
765+
throwsUnsupportedError,
766+
);
767+
expect(
768+
() =>
769+
getWindowsBuildDirectory(TargetPlatform.darwin, config: config, fileSystem: fileSystem),
770+
throwsUnsupportedError,
771+
);
708772
});
709773
});
710774

0 commit comments

Comments
 (0)