Skip to content

Simplify some code by introducing an enum for SDK platform kind. NFC #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions Sources/SwiftDriver/Jobs/PrebuiltModulesJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,35 +233,22 @@ public struct SDKPrebuiltModuleInputsCollector {

// Returns a target triple that's proper to use with the given SDK path.
public var targetTriple: String {
let canonicalName = sdkInfo.canonicalName
func extractVersion(_ platform: String) -> Substring? {
if canonicalName.starts(with: platform) {
let versionStartIndex = canonicalName.index(canonicalName.startIndex,
offsetBy: platform.count)
let delimiterRange = canonicalName.range(of: "internal", options: .backwards)
let versionEndIndex = delimiterRange == nil ? canonicalName.endIndex : delimiterRange!.lowerBound
return canonicalName[versionStartIndex..<versionEndIndex]
}
return nil
}

if let version = extractVersion("macosx") {
let version = sdkInfo.versionString
switch sdkInfo.platformKind {
case .macosx:
return "arm64-apple-macosx\(version)"
} else if let version = extractVersion("iphoneos") {
case .iphoneos:
return "arm64-apple-ios\(version)"
} else if let version = extractVersion("iphonesimulator") {
case .iphonesimulator:
return "arm64-apple-ios\(version)-simulator"
} else if let version = extractVersion("watchos") {
case .watchos:
return "armv7k-apple-watchos\(version)"
} else if let version = extractVersion("watchsimulator") {
case .watchsimulator:
return "arm64-apple-watchos\(version)-simulator"
} else if let version = extractVersion("appletvos") {
case .appletvos:
return "arm64-apple-tvos\(version)"
} else if let version = extractVersion("appletvsimulator") {
case .appletvsimulator:
return "arm64-apple-tvos\(version)-simulator"
} else {
diagEngine.emit(error: "unhandled platform name: \(canonicalName)")
return ""
}
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ public final class DarwinToolchain: Toolchain {
case canonicalName = "CanonicalName"
}

public enum SDKPlatformKind: String {
case macosx
case iphoneos
case iphonesimulator
case watchos
case watchsimulator
case appletvos
case appletvsimulator
}

struct VersionMap: Decodable {
private enum CodingKeys: String, CodingKey {
case macOSToCatalystMapping = "macOS_iOSMac"
Expand Down Expand Up @@ -269,6 +279,7 @@ public final class DarwinToolchain: Toolchain {
}
}
public let versionString: String
public let platformKind: SDKPlatformKind
private var version: Version
private var versionMap: VersionMap
let canonicalName: String
Expand All @@ -277,6 +288,8 @@ public final class DarwinToolchain: Toolchain {

self.versionString = try keyedContainer.decode(String.self, forKey: .version)
self.canonicalName = try keyedContainer.decode(String.self, forKey: .canonicalName)
let verRange = canonicalName.range(of: versionString)!
self.platformKind = SDKPlatformKind(rawValue: String(canonicalName[..<verRange.lowerBound]))!
guard let version = try? Version(versionString: versionString, usesLenientParsing: true) else {
throw DecodingError.dataCorruptedError(forKey: .version,
in: keyedContainer,
Expand All @@ -290,6 +303,7 @@ public final class DarwinToolchain: Toolchain {
}
}


func sdkVersion(for triple: Triple) -> Version {
if triple.isMacCatalyst {
// For the Mac Catalyst environment, we have a macOS SDK with a macOS
Expand Down
2 changes: 1 addition & 1 deletion TestInputs/SDKChecks/iPhoneOS.sdk/SDKSettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Version": "13.00",
"Version": "13.0",
"CanonicalName": "iphoneos13.0"
}