Unverified Commit f9d45513 authored by Kevin Moore's avatar Kevin Moore Committed by GitHub

tool: replace top-level functions with enum properties (#126167)

parent 201f7311
......@@ -119,7 +119,7 @@ Iterable<String> _apkFilesFor(AndroidBuildInfo androidBuildInfo) {
final String flavorString = productFlavor.isEmpty ? '' : '-$productFlavor';
if (androidBuildInfo.splitPerAbi) {
return androidBuildInfo.targetArchs.map<String>((AndroidArch arch) {
final String abi = getNameForAndroidArch(arch);
final String abi = arch.archName;
return 'app$flavorString-$abi-$buildType.apk';
});
}
......@@ -347,7 +347,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
} else if (androidBuildInfo.targetArchs.isNotEmpty) {
final String targetPlatforms = androidBuildInfo
.targetArchs
.map(getPlatformNameForAndroidArch).join(',');
.map((AndroidArch e) => e.platformName).join(',');
command.add('-Ptarget-platform=$targetPlatforms');
}
command.add('-Ptarget=$target');
......@@ -560,7 +560,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
logger: _logger,
flutterUsage: _usage,
);
final String archName = getNameForAndroidArch(androidBuildInfo.targetArchs.single);
final String archName = androidBuildInfo.targetArchs.single.archName;
final BuildInfo buildInfo = androidBuildInfo.buildInfo;
final File aotSnapshot = _fileSystem.directory(buildInfo.codeSizeDirectory)
.childFile('snapshot.$archName.json');
......@@ -688,7 +688,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
localEngineInfo.engineOutPath)}');
} else if (androidBuildInfo.targetArchs.isNotEmpty) {
final String targetPlatforms = androidBuildInfo.targetArchs
.map(getPlatformNameForAndroidArch).join(',');
.map((AndroidArch e) => e.platformName).join(',');
command.add('-Ptarget-platform=$targetPlatforms');
}
......@@ -939,7 +939,7 @@ Iterable<String> listApkPaths(
for (AndroidArch androidArch in androidBuildInfo.targetArchs)
<String>[
'app',
getNameForAndroidArch(androidArch),
androidArch.archName,
...apkPartialName,
].join('-'),
];
......
......@@ -583,7 +583,7 @@ class CachedArtifacts implements Artifacts {
final String root = _fileSystem.path.join(
_cache.getArtifactDirectory('flutter_runner').path,
'flutter',
fuchsiaArchForTargetPlatform(platform),
platform.fuchsiaArchForTargetPlatform,
mode.isRelease ? 'release' : mode.toString(),
);
final String runtime = mode.isJit ? 'jit' : 'aot';
......
......@@ -69,7 +69,7 @@ class GenSnapshot {
// one for the target architecture in question.
if (snapshotType.platform == TargetPlatform.ios ||
snapshotType.platform == TargetPlatform.darwin) {
snapshotterPath += '_${getDartNameForDarwinArch(darwinArch!)}';
snapshotterPath += '_${darwinArch!.dartName}';
}
return _processUtils.stream(
......@@ -259,7 +259,7 @@ class AOTSnapshotter {
required bool stripAfterBuild,
required bool extractAppleDebugSymbols
}) async {
final String targetArch = getNameForDarwinArch(appleArch);
final String targetArch = appleArch.name;
if (!quiet) {
_logger.printStatus('Building App.framework for $targetArch...');
}
......
......@@ -607,7 +607,17 @@ enum HostPlatform {
darwin_arm64,
linux_x64,
linux_arm64,
windows_x64,
windows_x64;
String get platformName {
return switch (this) {
HostPlatform.darwin_x64 => 'x64',
HostPlatform.darwin_arm64 => 'arm64',
HostPlatform.linux_x64 => 'x64',
HostPlatform.linux_arm64 => 'arm64',
HostPlatform.windows_x64 => 'x64'
};
}
}
String getNameForHostPlatform(HostPlatform platform) {
......
......@@ -545,7 +545,51 @@ enum TargetPlatform {
android_arm,
android_arm64,
android_x64,
android_x86,
android_x86;
String get fuchsiaArchForTargetPlatform {
switch (this) {
case TargetPlatform.fuchsia_arm64:
return 'arm64';
case TargetPlatform.fuchsia_x64:
return 'x64';
case TargetPlatform.android:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.darwin:
case TargetPlatform.ios:
case TargetPlatform.linux_arm64:
case TargetPlatform.linux_x64:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
case TargetPlatform.windows_x64:
throw UnsupportedError('Unexpected Fuchsia platform $this');
}
}
String get simpleName {
switch (this) {
case TargetPlatform.linux_x64:
case TargetPlatform.darwin:
case TargetPlatform.windows_x64:
return 'x64';
case TargetPlatform.linux_arm64:
return 'arm64';
case TargetPlatform.android:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.ios:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
throw UnsupportedError('Unexpected target platform $this');
}
}
}
/// iOS and macOS target device architecture.
......@@ -554,7 +598,21 @@ enum TargetPlatform {
enum DarwinArch {
armv7, // Deprecated. Used to display 32-bit unsupported devices.
arm64,
x86_64,
x86_64;
/// Returns the Dart SDK's name for the specified target architecture.
///
/// When building for Darwin platforms, the tool invokes architecture-specific
/// variants of `gen_snapshot`, one for each target architecture. The output
/// instructions are then built into architecture-specific binaries, which are
/// merged into a universal binary using the `lipo` tool.
String get dartName {
return switch (this) {
DarwinArch.armv7 => 'armv7',
DarwinArch.arm64 => 'arm64',
DarwinArch.x86_64 => 'x64'
};
}
}
// TODO(zanderso): replace all android TargetPlatform usage with AndroidArch.
......@@ -562,7 +620,25 @@ enum AndroidArch {
armeabi_v7a,
arm64_v8a,
x86,
x86_64,
x86_64;
String get archName {
return switch (this) {
AndroidArch.armeabi_v7a => 'armeabi-v7a',
AndroidArch.arm64_v8a => 'arm64-v8a',
AndroidArch.x86_64 => 'x86_64',
AndroidArch.x86 => 'x86'
};
}
String get platformName {
return switch (this) {
AndroidArch.armeabi_v7a => 'android-arm',
AndroidArch.arm64_v8a => 'android-arm64',
AndroidArch.x86_64 => 'android-x64',
AndroidArch.x86 => 'android-x86'
};
}
}
/// The default set of iOS device architectures to build for.
......@@ -607,36 +683,6 @@ List<DarwinArch> defaultMacOSArchsForEnvironment(Artifacts artifacts) {
];
}
// Returns the Dart SDK's name for the specified target architecture.
//
// When building for Darwin platforms, the tool invokes architecture-specific
// variants of `gen_snapshot`, one for each target architecture. The output
// instructions are then built into architecture-specific binaries, which are
// merged into a universal binary using the `lipo` tool.
String getDartNameForDarwinArch(DarwinArch arch) {
return switch (arch) {
DarwinArch.armv7 => 'armv7',
DarwinArch.arm64 => 'arm64',
DarwinArch.x86_64 => 'x64'
};
}
// Returns Apple's name for the specified target architecture.
//
// When invoking Apple tools such as `xcodebuild` or `lipo`, the tool often
// passes one or more target architectures as parameters. The names returned by
// this function reflect Apple's name for the specified architecture.
//
// For consistency with developer expectations, Flutter outputs also use these
// architecture names in its build products for Darwin target platforms.
String getNameForDarwinArch(DarwinArch arch) {
return switch (arch) {
DarwinArch.armv7 => 'armv7',
DarwinArch.arm64 => 'arm64',
DarwinArch.x86_64 => 'x86_64'
};
}
DarwinArch getIOSArchForName(String arch) {
switch (arch) {
case 'armv7':
......@@ -674,12 +720,12 @@ String getNameForTargetPlatform(TargetPlatform platform, {DarwinArch? darwinArch
return 'android-x86';
case TargetPlatform.ios:
if (darwinArch != null) {
return 'ios-${getNameForDarwinArch(darwinArch)}';
return 'ios-${darwinArch.name}';
}
return 'ios';
case TargetPlatform.darwin:
if (darwinArch != null) {
return 'darwin-${getNameForDarwinArch(darwinArch)}';
return 'darwin-${darwinArch.name}';
}
return 'darwin';
case TargetPlatform.linux_x64:
......@@ -751,46 +797,6 @@ AndroidArch getAndroidArchForName(String platform) {
throw Exception('Unsupported Android arch name "$platform"');
}
String getNameForAndroidArch(AndroidArch arch) {
return switch (arch) {
AndroidArch.armeabi_v7a => 'armeabi-v7a',
AndroidArch.arm64_v8a => 'arm64-v8a',
AndroidArch.x86_64 => 'x86_64',
AndroidArch.x86 => 'x86'
};
}
String getPlatformNameForAndroidArch(AndroidArch arch) {
return switch (arch) {
AndroidArch.armeabi_v7a => 'android-arm',
AndroidArch.arm64_v8a => 'android-arm64',
AndroidArch.x86_64 => 'android-x64',
AndroidArch.x86 => 'android-x86'
};
}
String fuchsiaArchForTargetPlatform(TargetPlatform targetPlatform) {
switch (targetPlatform) {
case TargetPlatform.fuchsia_arm64:
return 'arm64';
case TargetPlatform.fuchsia_x64:
return 'x64';
case TargetPlatform.android:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.darwin:
case TargetPlatform.ios:
case TargetPlatform.linux_arm64:
case TargetPlatform.linux_x64:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
case TargetPlatform.windows_x64:
throw UnsupportedError('Unexpected Fuchsia platform $targetPlatform');
}
}
HostPlatform getCurrentHostPlatform() {
if (globals.platform.isMacOS) {
return HostPlatform.darwin_x64;
......@@ -862,7 +868,7 @@ String getWebBuildDirectory([bool isWasm = false]) {
String getLinuxBuildDirectory([TargetPlatform? targetPlatform]) {
final String arch = (targetPlatform == null) ?
_getCurrentHostPlatformArchName() :
getNameForTargetPlatformArch(targetPlatform);
targetPlatform.simpleName;
final String subDirs = 'linux/$arch';
return globals.fs.path.join(getBuildDirectory(), subDirs);
}
......@@ -1019,39 +1025,7 @@ enum NullSafetyMode {
String _getCurrentHostPlatformArchName() {
final HostPlatform hostPlatform = getCurrentHostPlatform();
return getNameForHostPlatformArch(hostPlatform);
}
String getNameForTargetPlatformArch(TargetPlatform platform) {
switch (platform) {
case TargetPlatform.linux_x64:
case TargetPlatform.darwin:
case TargetPlatform.windows_x64:
return 'x64';
case TargetPlatform.linux_arm64:
return 'arm64';
case TargetPlatform.android:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.android_x86:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.ios:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
throw UnsupportedError('Unexpected target platform $platform');
}
}
String getNameForHostPlatformArch(HostPlatform platform) {
return switch (platform) {
HostPlatform.darwin_x64 => 'x64',
HostPlatform.darwin_arm64 => 'arm64',
HostPlatform.linux_x64 => 'x64',
HostPlatform.linux_arm64 => 'arm64',
HostPlatform.windows_x64 => 'x64'
};
return hostPlatform.platformName;
}
String? _uncapitalize(String? s) {
......
......@@ -158,8 +158,7 @@ class AndroidAot extends AotElfBase {
/// The name of the produced Android ABI.
String get _androidAbiName {
return getNameForAndroidArch(
getAndroidArchForName(getNameForTargetPlatform(targetPlatform)));
return getAndroidArchForName(getNameForTargetPlatform(targetPlatform)).archName;
}
@override
......@@ -286,8 +285,7 @@ class AndroidAotBundle extends Target {
/// The name of the produced Android ABI.
String get _androidAbiName {
return getNameForAndroidArch(
getAndroidArchForName(getNameForTargetPlatform(dependency.targetPlatform)));
return getAndroidArchForName(getNameForTargetPlatform(dependency.targetPlatform)).archName;
}
@override
......@@ -373,8 +371,7 @@ class AndroidAotDeferredComponentsBundle extends Target {
/// The name of the produced Android ABI.
String get _androidAbiName {
return getNameForAndroidArch(
getAndroidArchForName(getNameForTargetPlatform(dependency.targetPlatform)));
return getAndroidArchForName(getNameForTargetPlatform(dependency.targetPlatform)).archName;
}
@override
......
......@@ -407,7 +407,7 @@ abstract final class Lipo {
environment.fileSystem.directory(resultPath).parent.createSync(recursive: true);
Iterable<String> inputPaths = darwinArchs.map(
(DarwinArch iosArch) => environment.fileSystem.path.join(inputDir, getNameForDarwinArch(iosArch), relativePath)
(DarwinArch iosArch) => environment.fileSystem.path.join(inputDir, iosArch.name, relativePath)
);
if (skipMissingInputs) {
inputPaths = inputPaths.where(environment.fileSystem.isFileSync);
......
......@@ -41,7 +41,7 @@ class DeferredComponentsGenSnapshotValidatorTarget extends Target {
for (final AndroidAotDeferredComponentsBundle target in deferredComponentsDependencies) {
if (deferredComponentsTargets.contains(target.name)) {
abis.add(
getNameForAndroidArch(getAndroidArchForName(getNameForTargetPlatform(target.dependency.targetPlatform)))
getAndroidArchForName(getNameForTargetPlatform(target.dependency.targetPlatform)).archName
);
}
}
......
......@@ -88,10 +88,10 @@ abstract class AotAssemblyBase extends Target {
if (codeSizeDirectory != null) {
final File codeSizeFile = environment.fileSystem
.directory(codeSizeDirectory)
.childFile('snapshot.${getNameForDarwinArch(darwinArch)}.json');
.childFile('snapshot.${darwinArch.name}.json');
final File precompilerTraceFile = environment.fileSystem
.directory(codeSizeDirectory)
.childFile('trace.${getNameForDarwinArch(darwinArch)}.json');
.childFile('trace.${darwinArch.name}.json');
archExtraGenSnapshotOptions.add('--write-v8-snapshot-profile-to=${codeSizeFile.path}');
archExtraGenSnapshotOptions.add('--trace-precompiler-to=${precompilerTraceFile.path}');
}
......@@ -99,7 +99,7 @@ abstract class AotAssemblyBase extends Target {
platform: targetPlatform,
buildMode: buildMode,
mainPath: environment.buildDir.childFile('app.dill').path,
outputPath: environment.fileSystem.path.join(buildOutputPath, getNameForDarwinArch(darwinArch)),
outputPath: environment.fileSystem.path.join(buildOutputPath, darwinArch.name),
darwinArch: darwinArch,
sdkRoot: sdkRoot,
quiet: true,
......
......@@ -191,7 +191,7 @@ class DebugMacOSFramework extends Target {
?? <DarwinArch>[DarwinArch.x86_64, DarwinArch.arm64];
final Iterable<String> darwinArchArguments =
darwinArchs.expand((DarwinArch arch) => <String>['-arch', getNameForDarwinArch(arch)]);
darwinArchs.expand((DarwinArch arch) => <String>['-arch', arch.name]);
outputFile.createSync(recursive: true);
final File debugApp = environment.buildDir.childFile('debug_app.cc')
......@@ -277,10 +277,10 @@ class CompileMacOSFramework extends Target {
if (codeSizeDirectory != null) {
final File codeSizeFile = environment.fileSystem
.directory(codeSizeDirectory)
.childFile('snapshot.${getNameForDarwinArch(darwinArch)}.json');
.childFile('snapshot.${darwinArch.name}.json');
final File precompilerTraceFile = environment.fileSystem
.directory(codeSizeDirectory)
.childFile('trace.${getNameForDarwinArch(darwinArch)}.json');
.childFile('trace.${darwinArch.name}.json');
extraGenSnapshotOptions.add('--write-v8-snapshot-profile-to=${codeSizeFile.path}');
extraGenSnapshotOptions.add('--trace-precompiler-to=${precompilerTraceFile.path}');
}
......@@ -288,7 +288,7 @@ class CompileMacOSFramework extends Target {
pending.add(snapshotter.build(
buildMode: buildMode,
mainPath: environment.buildDir.childFile('app.dill').path,
outputPath: environment.fileSystem.path.join(buildOutputPath, getNameForDarwinArch(darwinArch)),
outputPath: environment.fileSystem.path.join(buildOutputPath, darwinArch.name),
platform: TargetPlatform.darwin,
darwinArch: darwinArch,
splitDebugInfo: splitDebugInfo,
......
......@@ -19,7 +19,6 @@ import 'base/os.dart' show OperatingSystemUtils;
import 'base/platform.dart';
import 'base/terminal.dart';
import 'base/user_messages.dart';
import 'build_info.dart';
import 'convert.dart';
import 'features.dart';
......@@ -537,7 +536,7 @@ class Cache {
}
String getHostPlatformArchName() {
return getNameForHostPlatformArch(_osUtils.hostPlatform);
return _osUtils.hostPlatform.platformName;
}
/// Return a directory in the cache dir. For `pkg`, this will return `bin/cache/pkg`.
......
......@@ -673,7 +673,7 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand {
appFilenamePattern: 'App'
);
// Only support 64bit iOS code size analysis.
final String arch = getNameForDarwinArch(DarwinArch.arm64);
final String arch = DarwinArch.arm64.name;
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
.childFile('snapshot.$arch.json');
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
......
......@@ -428,7 +428,7 @@ end
kTargetFile: targetFile,
kTargetPlatform: getNameForTargetPlatform(TargetPlatform.ios),
kIosArchs: defaultIOSArchsForEnvironment(sdkType, globals.artifacts!)
.map(getNameForDarwinArch)
.map((DarwinArch e) => e.name)
.join(' '),
kSdkRoot: await globals.xcode!.sdkLocation(sdkType),
...buildInfo.toBuildSystemEnvironment(),
......
......@@ -62,8 +62,8 @@ class BuildLinuxCommand extends BuildSubCommand {
final TargetPlatform targetPlatform =
getTargetPlatformForName(stringArg('target-platform')!);
final bool needCrossBuild =
getNameForHostPlatformArch(_operatingSystemUtils.hostPlatform)
!= getNameForTargetPlatformArch(targetPlatform);
_operatingSystemUtils.hostPlatform.platformName
!= targetPlatform.simpleName;
if (!featureFlags.isLinuxEnabled) {
throwToolExit('"build linux" is not currently supported. To enable, run "flutter config --enable-linux-desktop".');
......
......@@ -195,7 +195,7 @@ end
kTargetFile: targetFile,
kTargetPlatform: getNameForTargetPlatform(TargetPlatform.darwin),
kDarwinArchs: defaultMacOSArchsForEnvironment(globals.artifacts!)
.map(getNameForDarwinArch)
.map((DarwinArch e) => e.name)
.join(' '),
...buildInfo.toBuildSystemEnvironment(),
},
......
......@@ -296,7 +296,7 @@ Future<XcodeBuildResult> buildXcodeProject({
}
if (activeArch != null) {
final String activeArchName = getNameForDarwinArch(activeArch);
final String activeArchName = activeArch.name;
buildCommands.add('ONLY_ACTIVE_ARCH=YES');
// Setting ARCHS to $activeArchName will break the build if a watchOS companion app exists,
// as it cannot be build for the architecture of the Flutter app.
......
......@@ -141,7 +141,7 @@ Future<void> buildMacOS({
throwToolExit('Build process failed');
}
if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
final String arch = getNameForDarwinArch(DarwinArch.x86_64);
final String arch = DarwinArch.x86_64.name;
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
.childFile('snapshot.$arch.json');
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
......
......@@ -632,7 +632,7 @@ class XCDevice {
}
_logger.printWarning(
'Unknown architecture $architecture, defaulting to '
'${getNameForDarwinArch(cpuArchitecture)}',
'${cpuArchitecture.name}',
);
}
}
......
......@@ -56,8 +56,7 @@ void main() {
TargetPlatform.android_x64,
]) {
testWithoutContext('AndroidDevice.startApp allows release builds on $targetPlatform', () async {
final String arch = getNameForAndroidArch(
getAndroidArchForName(getNameForTargetPlatform(targetPlatform)));
final String arch = getAndroidArchForName(getNameForTargetPlatform(targetPlatform)).archName;
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel',
fileSystem: fileSystem,
processManager: processManager,
......
......@@ -83,15 +83,15 @@ void main() {
});
testWithoutContext('getDartNameForDarwinArch returns name used in Dart SDK', () {
expect(getDartNameForDarwinArch(DarwinArch.armv7), 'armv7');
expect(getDartNameForDarwinArch(DarwinArch.arm64), 'arm64');
expect(getDartNameForDarwinArch(DarwinArch.x86_64), 'x64');
expect(DarwinArch.armv7.dartName, 'armv7');
expect(DarwinArch.arm64.dartName, 'arm64');
expect(DarwinArch.x86_64.dartName, 'x64');
});
testWithoutContext('getNameForDarwinArch returns Apple names', () {
expect(getNameForDarwinArch(DarwinArch.armv7), 'armv7');
expect(getNameForDarwinArch(DarwinArch.arm64), 'arm64');
expect(getNameForDarwinArch(DarwinArch.x86_64), 'x86_64');
expect(DarwinArch.armv7.name, 'armv7');
expect(DarwinArch.arm64.name, 'arm64');
expect(DarwinArch.x86_64.name, 'x86_64');
});
testWithoutContext('getNameForTargetPlatform on Darwin arches', () {
......
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