Skip to content

[5.7] Always parse the ABI component of target triples #331

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
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
43 changes: 34 additions & 9 deletions Sources/TSCUtility/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,41 @@ public struct Triple: Encodable, Equatable {
case openbsd
}

public enum ABI: String, Encodable {
public enum ABI: Encodable, Equatable, RawRepresentable {
case unknown
case android
case other(name: String)

public init?(rawValue: String) {
if rawValue.hasPrefix(ABI.android.rawValue) {
self = .android
} else if let version = rawValue.firstIndex(where: { $0.isNumber }) {
self = .other(name: String(rawValue[..<version]))
} else {
self = .other(name: rawValue)
}
}

public var rawValue: String {
switch self {
case .android: return "android"
case .other(let name): return name
case .unknown: return "unknown"
}
}

public static func ==(lhs: ABI, rhs: ABI) -> Bool {
switch (lhs, rhs) {
case (.unknown, .unknown):
return true
case (.android, .android):
return true
case let (.other(lhsName), .other(rhsName)):
return lhsName == rhsName
default:
return false
}
}
}

public init(_ string: String) throws {
Expand All @@ -91,7 +123,7 @@ public struct Triple: Encodable, Equatable {

let osVersion = Triple.parseVersion(components[2])

let abi = components.count > 3 ? Triple.parseABI(components[3]) : nil
let abi = components.count > 3 ? Triple.ABI(rawValue: components[3]) : nil
let abiVersion = components.count > 3 ? Triple.parseVersion(components[3]) : nil

self.tripleString = string
Expand Down Expand Up @@ -119,13 +151,6 @@ public struct Triple: Encodable, Equatable {
return nil
}

fileprivate static func parseABI(_ string: String) -> ABI? {
if string.hasPrefix(ABI.android.rawValue) {
return ABI.android
}
return nil
}

public func isAndroid() -> Bool {
return os == .linux && abi == .android
}
Expand Down
11 changes: 11 additions & 0 deletions Tests/TSCUtilityTests/TripleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TripleTests : XCTestCase {
XCTAssertNotNil(linux)
XCTAssertEqual(linux!.os, .linux)
XCTAssertNil(linux!.osVersion)
XCTAssertEqual(linux!.abi, .other(name: "gnu"))
XCTAssertNil(linux!.abiVersion)

let macos = try? Triple("x86_64-apple-macosx10.15")
XCTAssertNotNil(macos!)
Expand All @@ -32,7 +34,12 @@ class TripleTests : XCTestCase {
let android = try? Triple("aarch64-unknown-linux-android24")
XCTAssertNotNil(android)
XCTAssertEqual(android!.os, .linux)
XCTAssertEqual(android!.abi, .android)
XCTAssertEqual(android!.abiVersion, "24")

let linuxWithABIVersion = try? Triple("x86_64-unknown-linux-gnu42")
XCTAssertEqual(linuxWithABIVersion!.abi, .other(name: "gnu"))
XCTAssertEqual(linuxWithABIVersion!.abiVersion, "42")
}

func testEquality() throws {
Expand All @@ -42,5 +49,9 @@ class TripleTests : XCTestCase {

let intelMacOSTriple = try Triple("x86_64-apple-macos")
XCTAssertNotEqual(macOSTriple, intelMacOSTriple)

let linuxWithoutGNUABI = try Triple("x86_64-unknown-linux")
let linuxWithGNUABI = try Triple("x86_64-unknown-linux-gnu")
XCTAssertNotEqual(linuxWithoutGNUABI, linuxWithGNUABI)
}
}