Skip to content

Commit ec66198

Browse files
authored
Split the platform and cpuArch part of TargetPlatform (flutter#189479)
Convert `enum TargetPlatform` into a class with two fields: `TargetPlatformType` and `CpuArch`.
1 parent 2757a77 commit ec66198

141 files changed

Lines changed: 1435 additions & 1596 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/flutter_tools/bin/fuchsia_asset_builder.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Future<void> run(List<String> args) async {
6666
packageConfigPath:
6767
argResults[_kOptionPackages] as String? ??
6868
findPackageConfigFileOrDefault(globals.fs.currentDirectory).path,
69-
targetPlatform: TargetPlatform.fuchsia_arm64, // This is not arch specific.
69+
targetPlatform: const TargetPlatform(.fuchsia, .arm64), // This is not arch specific.
7070
);
7171

7272
if (assets == null) {

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

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,6 @@ class AndroidDevice extends Device {
190190
}
191191
}
192192

193-
@override
194-
late final Future<TargetPlatform> targetPlatform = () async {
195-
return switch (await cpuArch) {
196-
CpuArch.arm64 => TargetPlatform.android_arm64,
197-
CpuArch.armv7 => TargetPlatform.android_arm,
198-
CpuArch.x64 => TargetPlatform.android_x64,
199-
CpuArch.x86 || CpuArch.riscv64 || CpuArch.unknown => TargetPlatform.unsupported,
200-
};
201-
}();
202-
203193
@override
204194
late final Future<CpuArch> cpuArch = () async {
205195
// http://developer.android.com/ndk/guides/abis.html (x86, armeabi-v7a, ...)
@@ -227,26 +217,7 @@ class AndroidDevice extends Device {
227217

228218
@override
229219
Future<bool> supportsRuntimeMode(BuildMode buildMode) async {
230-
switch (await targetPlatform) {
231-
case TargetPlatform.android_arm:
232-
case TargetPlatform.android_arm64:
233-
case TargetPlatform.android_x64:
234-
return buildMode != BuildMode.jitRelease;
235-
case TargetPlatform.android:
236-
case TargetPlatform.darwin:
237-
case TargetPlatform.fuchsia_arm64:
238-
case TargetPlatform.fuchsia_x64:
239-
case TargetPlatform.ios:
240-
case TargetPlatform.linux_arm64:
241-
case TargetPlatform.linux_riscv64:
242-
case TargetPlatform.linux_x64:
243-
case TargetPlatform.tester:
244-
case TargetPlatform.web_javascript:
245-
case TargetPlatform.windows_x64:
246-
case TargetPlatform.windows_arm64:
247-
case TargetPlatform.unsupported:
248-
throw UnsupportedError('Invalid target platform for Android');
249-
}
220+
return buildMode != .jitRelease;
250221
}
251222

252223
@override
@@ -873,13 +844,9 @@ class AndroidDevice extends Device {
873844

874845
@override
875846
Future<bool> isSupported() async {
876-
final TargetPlatform platform = await targetPlatform;
877-
return switch (platform) {
878-
TargetPlatform.android ||
879-
TargetPlatform.android_arm ||
880-
TargetPlatform.android_arm64 ||
881-
TargetPlatform.android_x64 => true,
882-
_ => false,
847+
return switch (await cpuArch) {
848+
.arm64 || .armv7 || .x64 => true,
849+
.x86 || .riscv64 || .unknown => false,
883850
};
884851
}
885852

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '../base/io.dart';
1313
import '../base/logger.dart';
1414
import '../base/process.dart';
1515
import '../base/utils.dart';
16+
import '../build_info.dart';
1617
import '../device.dart';
1718
import '../emulator.dart';
1819
import 'android_sdk.dart';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,13 +1191,13 @@ void updateLocalProperties({
11911191
if (buildInfo != null) {
11921192
changeIfNecessary('flutter.buildMode', buildInfo.modeName);
11931193
final String? buildName = validatedBuildNameForPlatform(
1194-
TargetPlatform.android_arm,
1194+
PlatformType.android,
11951195
buildInfo.buildName ?? project.manifest.buildName,
11961196
globals.logger,
11971197
);
11981198
changeIfNecessary('flutter.versionName', buildName);
11991199
final String? buildNumber = validatedBuildNumberForPlatform(
1200-
TargetPlatform.android_arm,
1200+
PlatformType.android,
12011201
buildInfo.buildNumber ?? project.manifest.buildNumber,
12021202
globals.logger,
12031203
);

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

Lines changed: 75 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -197,35 +197,16 @@ enum HostArtifact {
197197

198198
// TODO(knopp): Remove once darwin artifacts are universal and moved out of darwin-x64
199199
String _enginePlatformDirectoryName(TargetPlatform platform) {
200-
if (platform == TargetPlatform.darwin) {
200+
if (platform.type == .macos) {
201201
return 'darwin-x64';
202202
}
203-
return platform.getName();
204-
}
205-
206-
// Remove android target platform type.
207-
TargetPlatform? _mapTargetPlatform(TargetPlatform? targetPlatform) {
208-
switch (targetPlatform) {
209-
case TargetPlatform.android:
210-
return TargetPlatform.android_arm64;
211-
case TargetPlatform.ios:
212-
case TargetPlatform.darwin:
213-
case TargetPlatform.linux_x64:
214-
case TargetPlatform.linux_arm64:
215-
case TargetPlatform.linux_riscv64:
216-
case TargetPlatform.windows_x64:
217-
case TargetPlatform.windows_arm64:
218-
case TargetPlatform.fuchsia_arm64:
219-
case TargetPlatform.fuchsia_x64:
220-
case TargetPlatform.tester:
221-
case TargetPlatform.web_javascript:
222-
case TargetPlatform.android_arm:
223-
case TargetPlatform.android_arm64:
224-
case TargetPlatform.android_x64:
225-
case TargetPlatform.unsupported:
226-
case null:
227-
return targetPlatform;
203+
// iOS engine artifacts live in a single `ios` directory regardless of the
204+
// target architecture, so the arch-qualified name from [getName] is not used
205+
// here.
206+
if (platform.type == .ios) {
207+
return 'ios';
228208
}
209+
return platform.getName();
229210
}
230211

231212
class EngineBuildPaths {
@@ -421,35 +402,34 @@ class CachedArtifacts implements Artifacts {
421402
BuildMode? mode,
422403
EnvironmentType? environmentType,
423404
}) {
424-
platform = _mapTargetPlatform(platform);
425-
switch (platform) {
426-
case TargetPlatform.android:
427-
case TargetPlatform.android_arm:
428-
case TargetPlatform.android_arm64:
429-
case TargetPlatform.android_x64:
430-
assert(platform != TargetPlatform.android);
431-
return _getAndroidArtifactPath(artifact, platform!, mode!);
432-
case TargetPlatform.ios:
405+
switch (platform?.type) {
406+
case .android:
407+
// A generic Android target has an unknown CPU architecture (e.g. the
408+
// architecture-independent patched SDK requested when compiling the
409+
// app's Dart kernel). Default to arm64 in that case, matching the
410+
// historical behavior of the removed `_mapTargetPlatform` helper.
411+
final TargetPlatform androidPlatform = platform!.cpuArch == .unknown
412+
? const TargetPlatform(.android, .arm64)
413+
: platform;
414+
return _getAndroidArtifactPath(artifact, androidPlatform, mode!);
415+
case .ios:
433416
return _getIosArtifactPath(artifact, platform!, mode, environmentType);
434-
case TargetPlatform.darwin:
435-
case TargetPlatform.linux_x64:
436-
case TargetPlatform.linux_arm64:
437-
case TargetPlatform.linux_riscv64:
438-
case TargetPlatform.windows_x64:
439-
case TargetPlatform.windows_arm64:
417+
case .macos:
418+
case .linux:
419+
case .windows:
440420
return _getDesktopArtifactPath(artifact, platform!, mode);
441-
case TargetPlatform.fuchsia_arm64:
442-
case TargetPlatform.fuchsia_x64:
421+
case .fuchsia:
443422
return _getFuchsiaArtifactPath(artifact, platform!, mode!);
444-
case TargetPlatform.tester:
445-
case TargetPlatform.web_javascript:
423+
case .tester:
424+
case .web:
425+
case .custom:
446426
case null:
447427
return _getHostArtifactPath(
448428
artifact,
449429
platform ?? _currentHostPlatform(_platform, _operatingSystemUtils),
450430
mode,
451431
);
452-
case TargetPlatform.unsupported:
432+
case .unsupported:
453433
TargetPlatform.throwUnsupportedTarget();
454434
}
455435
}
@@ -677,7 +657,11 @@ class CachedArtifacts implements Artifacts {
677657
case Artifact.genSnapshotX64:
678658
// For script snapshots any gen_snapshot binary will do. Returning gen_snapshot for
679659
// android_arm in profile mode because it is available on all supported host platforms.
680-
return _getAndroidArtifactPath(artifact, TargetPlatform.android_arm, BuildMode.profile);
660+
return _getAndroidArtifactPath(
661+
artifact,
662+
const TargetPlatform(.android, .armv7),
663+
BuildMode.profile,
664+
);
681665
case Artifact.frontendServerSnapshotForEngineDartSdk:
682666
return _fileSystem.path.join(
683667
_dartSdkPath(_cache),
@@ -785,13 +769,10 @@ class CachedArtifacts implements Artifacts {
785769
String? _getEngineArtifactsPath(TargetPlatform platform, [BuildMode? mode]) {
786770
final String engineDir = _cache.getArtifactDirectory('engine').path;
787771
final String platformName = _enginePlatformDirectoryName(platform);
788-
switch (platform) {
789-
case TargetPlatform.linux_x64:
790-
case TargetPlatform.linux_arm64:
791-
case TargetPlatform.linux_riscv64:
792-
case TargetPlatform.darwin:
793-
case TargetPlatform.windows_x64:
794-
case TargetPlatform.windows_arm64:
772+
switch (platform.type) {
773+
case .linux:
774+
case .macos:
775+
case .windows:
795776
// TODO(zanderso): remove once debug desktop artifacts are uploaded
796777
// under a separate directory from the host artifacts.
797778
// https://github.com/flutter/flutter/issues/38935
@@ -800,23 +781,25 @@ class CachedArtifacts implements Artifacts {
800781
}
801782
final suffix = mode != BuildMode.debug ? '-${kebabCase(mode.cliName)}' : '';
802783
return _fileSystem.path.join(engineDir, platformName + suffix);
803-
case TargetPlatform.fuchsia_arm64:
804-
case TargetPlatform.fuchsia_x64:
805-
case TargetPlatform.tester:
806-
case TargetPlatform.web_javascript:
784+
case .fuchsia:
785+
case .tester:
786+
case .web:
807787
assert(mode == null, 'Platform $platform does not support different build modes.');
808788
return _fileSystem.path.join(engineDir, platformName);
809-
case TargetPlatform.ios:
810-
case TargetPlatform.android_arm:
811-
case TargetPlatform.android_arm64:
812-
case TargetPlatform.android_x64:
789+
case .ios:
790+
assert(mode != null, 'Need to specify a build mode for platform $platform.');
791+
final suffix = mode != BuildMode.debug ? '-${kebabCase(mode!.cliName)}' : '';
792+
return _fileSystem.path.join(engineDir, platformName + suffix);
793+
case .android:
794+
assert(
795+
platform.cpuArch != .unknown,
796+
'cannot use a generic Android platform to look up artifacts',
797+
);
813798
assert(mode != null, 'Need to specify a build mode for platform $platform.');
814799
final suffix = mode != BuildMode.debug ? '-${kebabCase(mode!.cliName)}' : '';
815800
return _fileSystem.path.join(engineDir, platformName + suffix);
816-
case TargetPlatform.android:
817-
assert(false, 'cannot use TargetPlatform.android to look up artifacts');
818-
return null;
819-
case TargetPlatform.unsupported:
801+
case .custom:
802+
case .unsupported:
820803
TargetPlatform.throwUnsupportedTarget();
821804
}
822805
}
@@ -826,20 +809,15 @@ class CachedArtifacts implements Artifacts {
826809
}
827810

828811
TargetPlatform _currentHostPlatform(Platform platform, OperatingSystemUtils operatingSystemUtils) {
812+
final cpuArch = CpuArch.fromHostPlatform(operatingSystemUtils.hostPlatform);
829813
if (platform.isMacOS) {
830-
return TargetPlatform.darwin;
814+
return TargetPlatform(.macos, cpuArch);
831815
}
832816
if (platform.isLinux) {
833-
return switch (operatingSystemUtils.hostPlatform) {
834-
HostPlatform.linux_x64 => TargetPlatform.linux_x64,
835-
HostPlatform.linux_riscv64 => TargetPlatform.linux_riscv64,
836-
_ => TargetPlatform.linux_arm64,
837-
};
817+
return TargetPlatform(.linux, cpuArch);
838818
}
839819
if (platform.isWindows) {
840-
return operatingSystemUtils.hostPlatform == HostPlatform.windows_arm64
841-
? TargetPlatform.windows_arm64
842-
: TargetPlatform.windows_x64;
820+
return TargetPlatform(.windows, cpuArch);
843821
}
844822
throw UnimplementedError('Host OS not supported.');
845823
}
@@ -1061,7 +1039,6 @@ class CachedLocalEngineArtifacts implements Artifacts {
10611039
EnvironmentType? environmentType,
10621040
}) {
10631041
platform ??= _currentHostPlatform(_platform, _operatingSystemUtils);
1064-
platform = _mapTargetPlatform(platform);
10651042
final isDirectoryArtifact = artifact == Artifact.flutterPatchedSdkPath;
10661043
final String? artifactFileName = isDirectoryArtifact
10671044
? null
@@ -1073,7 +1050,7 @@ class CachedLocalEngineArtifacts implements Artifacts {
10731050
case Artifact.genSnapshotX64:
10741051
return _genSnapshotPath(artifact);
10751052
case Artifact.flutterTester:
1076-
return _flutterTesterPath(platform!);
1053+
return _flutterTesterPath(platform);
10771054
case Artifact.isolateSnapshotData:
10781055
case Artifact.vmSnapshotData:
10791056
return _fileSystem.path.join(
@@ -1089,7 +1066,7 @@ class CachedLocalEngineArtifacts implements Artifacts {
10891066
case Artifact.flutterMacOSXcframework:
10901067
return _fileSystem.path.join(localEngineInfo.targetOutPath, artifactFileName);
10911068
case Artifact.platformKernelDill:
1092-
if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
1069+
if (platform.type == .fuchsia) {
10931070
return _fileSystem.path.join(
10941071
localEngineInfo.targetOutPath,
10951072
'flutter_runner_patched_sdk',
@@ -1122,7 +1099,7 @@ class CachedLocalEngineArtifacts implements Artifacts {
11221099
// what was specified in [mode] argument because local engine will
11231100
// have only one flutter_patched_sdk in standard location, that
11241101
// is happen to be what debug(non-release) mode is using.
1125-
if (platform == TargetPlatform.fuchsia_x64 || platform == TargetPlatform.fuchsia_arm64) {
1102+
if (platform.type == .fuchsia) {
11261103
return _fileSystem.path.join(localEngineInfo.targetOutPath, 'flutter_runner_patched_sdk');
11271104
}
11281105
return _getFlutterPatchedSdkPath(BuildMode.debug);
@@ -1260,7 +1237,7 @@ class CachedLocalWebSdkArtifacts implements Artifacts {
12601237
BuildMode? mode,
12611238
EnvironmentType? environmentType,
12621239
}) {
1263-
if (platform == TargetPlatform.web_javascript) {
1240+
if (platform?.type == .web) {
12641241
switch (artifact) {
12651242
case Artifact.engineDartSdkPath:
12661243
return _getDartSdkPath();
@@ -1467,7 +1444,7 @@ class _TestArtifacts implements Artifacts {
14671444
final buffer = StringBuffer();
14681445
buffer.write(artifact);
14691446
if (platform != null) {
1470-
buffer.write('.$platform');
1447+
buffer.write('.${_enginePlatformDirectoryName(platform)}');
14711448
}
14721449
if (mode != null) {
14731450
buffer.write('.$mode');
@@ -1576,30 +1553,25 @@ String _getFlutterPrebuiltsPath(String baseOutPath, FileSystem fileSystem) {
15761553

15771554
String _getPrebuiltTarget(Platform platform, OperatingSystemUtils operatingSystemUtils) {
15781555
final TargetPlatform hostPlatform = _currentHostPlatform(platform, operatingSystemUtils);
1579-
switch (hostPlatform) {
1580-
case TargetPlatform.darwin:
1556+
switch (hostPlatform.type) {
1557+
case .macos:
15811558
return 'macos-x64';
1582-
case TargetPlatform.linux_riscv64:
1583-
return 'linux-riscv64';
1584-
case TargetPlatform.linux_arm64:
1585-
return 'linux-arm64';
1586-
case TargetPlatform.linux_x64:
1587-
return 'linux-x64';
1588-
case TargetPlatform.windows_x64:
1589-
return 'windows-x64';
1590-
case TargetPlatform.windows_arm64:
1591-
return 'windows-arm64';
1592-
case TargetPlatform.ios:
1593-
case TargetPlatform.android:
1594-
case TargetPlatform.android_arm:
1595-
case TargetPlatform.android_arm64:
1596-
case TargetPlatform.android_x64:
1597-
case TargetPlatform.fuchsia_arm64:
1598-
case TargetPlatform.fuchsia_x64:
1599-
case TargetPlatform.web_javascript:
1600-
case TargetPlatform.tester:
1559+
case .linux:
1560+
return switch (hostPlatform.cpuArch) {
1561+
.riscv64 => 'linux-riscv64',
1562+
.arm64 => 'linux-arm64',
1563+
_ => 'linux-x64',
1564+
};
1565+
case .windows:
1566+
return hostPlatform.cpuArch == .arm64 ? 'windows-arm64' : 'windows-x64';
1567+
case .ios:
1568+
case .android:
1569+
case .fuchsia:
1570+
case .web:
1571+
case .tester:
1572+
case .custom:
16011573
throwToolExit('Unsupported host platform: $hostPlatform');
1602-
case TargetPlatform.unsupported:
1574+
case .unsupported:
16031575
TargetPlatform.throwUnsupportedTarget();
16041576
}
16051577
}

0 commit comments

Comments
 (0)