Skip to content

Commit d0bca40

Browse files
authored
Merge pull request #229 from 3405691582/UnversionTriples
Use the (versioned) triple from swiftc instead.
2 parents c796272 + 9db7a27 commit d0bca40

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Sources/TSCUtility/Triple.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public struct Triple: Encodable, Equatable {
2727
public let vendor: Vendor
2828
public let os: OS
2929
public let abi: ABI
30+
public let osVersion: String?
31+
public let abiVersion: String?
3032

3133
public enum Error: Swift.Error {
3234
case badFormat
@@ -85,13 +87,18 @@ public struct Triple: Encodable, Equatable {
8587
throw Error.unknownOS
8688
}
8789

90+
let osVersion = Triple.parseVersion(components[2])
91+
8892
let abi = components.count > 3 ? Triple.parseABI(components[3]) : nil
93+
let abiVersion = components.count > 3 ? Triple.parseVersion(components[3]) : nil
8994

9095
self.tripleString = string
9196
self.arch = arch
9297
self.vendor = vendor
9398
self.os = os
99+
self.osVersion = osVersion
94100
self.abi = abi ?? .unknown
101+
self.abiVersion = abiVersion
95102
}
96103

97104
fileprivate static func parseOS(_ string: String) -> OS? {
@@ -102,6 +109,15 @@ public struct Triple: Encodable, Equatable {
102109
return nil
103110
}
104111

112+
fileprivate static func parseVersion(_ string: String) -> String? {
113+
let candidate = String(string.drop(while: { $0.isLetter }))
114+
if candidate != string && !candidate.isEmpty {
115+
return candidate
116+
}
117+
118+
return nil
119+
}
120+
105121
fileprivate static func parseABI(_ string: String) -> ABI? {
106122
if string.hasPrefix(ABI.android.rawValue) {
107123
return ABI.android
@@ -138,18 +154,18 @@ public struct Triple: Encodable, Equatable {
138154
/// This is currently meant for Apple platforms only.
139155
public func tripleString(forPlatformVersion version: String) -> String {
140156
precondition(isDarwin())
141-
return self.tripleString + version
157+
return String(self.tripleString.dropLast(self.osVersion?.count ?? 0)) + version
142158
}
143159

144160
public static let macOS = try! Triple("x86_64-apple-macosx")
145161

146-
/// Determine the host triple using the Swift compiler.
162+
/// Determine the versioned host triple using the Swift compiler.
147163
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) -> Triple {
148164
do {
149165
let result = try Process.popen(args: swiftCompiler.pathString, "-print-target-info")
150166
let output = try result.utf8Output().spm_chomp()
151167
let targetInfo = try JSON(string: output)
152-
let tripleString: String = try targetInfo.get("target").get("unversionedTriple")
168+
let tripleString: String = try targetInfo.get("target").get("triple")
153169
return try Triple(tripleString)
154170
} catch {
155171
// FIXME: Remove the macOS special-casing once the latest version of Xcode comes with
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import TSCUtility
12+
import XCTest
13+
14+
class TripleTests : XCTestCase {
15+
func testTriple() {
16+
let linux = try? Triple("x86_64-unknown-linux-gnu")
17+
XCTAssertNotNil(linux)
18+
XCTAssertEqual(linux!.os, .linux)
19+
XCTAssertNil(linux!.osVersion)
20+
21+
let macos = try? Triple("x86_64-apple-macosx10.15")
22+
XCTAssertNotNil(macos!)
23+
XCTAssertEqual(macos!.osVersion, "10.15")
24+
let newVersion = "10.12"
25+
let tripleString = macos!.tripleString(forPlatformVersion: newVersion)
26+
XCTAssertEqual(tripleString, "x86_64-apple-macosx10.12")
27+
28+
let android = try? Triple("aarch64-unknown-linux-android24")
29+
XCTAssertNotNil(android)
30+
XCTAssertEqual(android!.os, .linux)
31+
XCTAssertEqual(android!.abiVersion, "24")
32+
}
33+
}

0 commit comments

Comments
 (0)