Skip to content

Commit 710b623

Browse files
authored
Always parse the ABI component of target triples (#330)
Previously, we were ignoring the ABI component unless it started with the literal string "android". This seems incorrect, we should be including any type of ABI component if it is present in a triple.
1 parent fec67a0 commit 710b623

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

Sources/TSCUtility/Triple.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,41 @@ public struct Triple: Encodable, Equatable {
6767
case openbsd
6868
}
6969

70-
public enum ABI: String, Encodable {
70+
public enum ABI: Encodable, Equatable, RawRepresentable {
7171
case unknown
7272
case android
73+
case other(name: String)
74+
75+
public init?(rawValue: String) {
76+
if rawValue.hasPrefix(ABI.android.rawValue) {
77+
self = .android
78+
} else if let version = rawValue.firstIndex(where: { $0.isNumber }) {
79+
self = .other(name: String(rawValue[..<version]))
80+
} else {
81+
self = .other(name: rawValue)
82+
}
83+
}
84+
85+
public var rawValue: String {
86+
switch self {
87+
case .android: return "android"
88+
case .other(let name): return name
89+
case .unknown: return "unknown"
90+
}
91+
}
92+
93+
public static func ==(lhs: ABI, rhs: ABI) -> Bool {
94+
switch (lhs, rhs) {
95+
case (.unknown, .unknown):
96+
return true
97+
case (.android, .android):
98+
return true
99+
case let (.other(lhsName), .other(rhsName)):
100+
return lhsName == rhsName
101+
default:
102+
return false
103+
}
104+
}
73105
}
74106

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

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

94-
let abi = components.count > 3 ? Triple.parseABI(components[3]) : nil
126+
let abi = components.count > 3 ? Triple.ABI(rawValue: components[3]) : nil
95127
let abiVersion = components.count > 3 ? Triple.parseVersion(components[3]) : nil
96128

97129
self.tripleString = string
@@ -119,13 +151,6 @@ public struct Triple: Encodable, Equatable {
119151
return nil
120152
}
121153

122-
fileprivate static func parseABI(_ string: String) -> ABI? {
123-
if string.hasPrefix(ABI.android.rawValue) {
124-
return ABI.android
125-
}
126-
return nil
127-
}
128-
129154
public func isAndroid() -> Bool {
130155
return os == .linux && abi == .android
131156
}

Tests/TSCUtilityTests/TripleTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class TripleTests : XCTestCase {
1717
XCTAssertNotNil(linux)
1818
XCTAssertEqual(linux!.os, .linux)
1919
XCTAssertNil(linux!.osVersion)
20+
XCTAssertEqual(linux!.abi, .other(name: "gnu"))
21+
XCTAssertNil(linux!.abiVersion)
2022

2123
let macos = try? Triple("x86_64-apple-macosx10.15")
2224
XCTAssertNotNil(macos!)
@@ -32,7 +34,12 @@ class TripleTests : XCTestCase {
3234
let android = try? Triple("aarch64-unknown-linux-android24")
3335
XCTAssertNotNil(android)
3436
XCTAssertEqual(android!.os, .linux)
37+
XCTAssertEqual(android!.abi, .android)
3538
XCTAssertEqual(android!.abiVersion, "24")
39+
40+
let linuxWithABIVersion = try? Triple("x86_64-unknown-linux-gnu42")
41+
XCTAssertEqual(linuxWithABIVersion!.abi, .other(name: "gnu"))
42+
XCTAssertEqual(linuxWithABIVersion!.abiVersion, "42")
3643
}
3744

3845
func testEquality() throws {
@@ -42,6 +49,10 @@ class TripleTests : XCTestCase {
4249

4350
let intelMacOSTriple = try Triple("x86_64-apple-macos")
4451
XCTAssertNotEqual(macOSTriple, intelMacOSTriple)
52+
53+
let linuxWithoutGNUABI = try Triple("x86_64-unknown-linux")
54+
let linuxWithGNUABI = try Triple("x86_64-unknown-linux-gnu")
55+
XCTAssertNotEqual(linuxWithoutGNUABI, linuxWithGNUABI)
4556
}
4657

4758
func testWASI() throws {

0 commit comments

Comments
 (0)