Skip to content

Commit 1785297

Browse files
authored
[5.10] Add and fix missing Triple tests (#6932)
Cherry-pick of #6843 Vendored Swift Driver triple was not checked against a few tests that weren't brought over from TSC after `TSC.Triple` type was deprecated. We should fix those tests, especially as they verified that per-component equality for triples worked instead of the current string-based equality check. (cherry picked from commit 8de70d3) ``` # Conflicts: # Tests/BasicsTests/TripleTests.swift ``` Related to rdar://113967401 Maybe related to rdar://115731621
1 parent 8f43950 commit 1785297

File tree

4 files changed

+95
-78
lines changed

4 files changed

+95
-78
lines changed

Sources/Basics/Triple+Basics.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,22 @@ extension Triple {
184184
}
185185
}
186186

187-
public func isRuntimeCompatible(with triple: Triple) -> Bool {
187+
public func isRuntimeCompatible(with triple: Triple) -> Bool {
188+
guard self != triple else {
189+
return true
190+
}
191+
188192
if
189193
self.arch == triple.arch &&
190194
self.vendor == triple.vendor &&
191195
self.os == triple.os &&
192-
self.environment == triple.environment
196+
self.environment == triple.environment,
197+
// If either of the triples have no `osVersion` specified, we can't determine compatibility and will
198+
// have to return `false`.
199+
let selfOSVersion = self.osVersion,
200+
let osVersion = triple.osVersion
193201
{
194-
return self.osVersion >= triple.osVersion
202+
return selfOSVersion >= osVersion
195203
} else {
196204
return false
197205
}
@@ -203,7 +211,11 @@ extension Triple: CustomStringConvertible {
203211
}
204212

205213
extension Triple: Equatable {
206-
public static func == (lhs: Self, rhs: Self) -> Bool {
207-
lhs.triple == rhs.triple
214+
public static func ==(lhs: Triple, rhs: Triple) -> Bool {
215+
lhs.arch == rhs.arch
216+
&& lhs.vendor == rhs.vendor
217+
&& lhs.os == rhs.os
218+
&& lhs.environment == rhs.environment
219+
&& lhs.osVersion == rhs.osVersion
208220
}
209221
}

Sources/Basics/Vendor/Triple+Platforms.swift

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===--------------- Triple+Platforms.swift - Swift Platform Triples ------===//
1+
//===----------------------------------------------------------------------===//
22
//
3-
// This source file is part of the Swift.org open source project
3+
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
8-
// See https://swift.org/LICENSE.txt for license information
9-
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -199,7 +199,7 @@ extension Triple {
199199
/// Not all combinations are valid; in particular, you cannot fetch a watchOS version
200200
/// from an iOS/tvOS triple or vice versa.
201201
public func version(for compatibilityPlatform: DarwinPlatform? = nil)
202-
-> Triple.Version
202+
-> Triple.Version?
203203
{
204204
switch compatibilityPlatform ?? darwinPlatform! {
205205
case .macOS:
@@ -239,53 +239,6 @@ extension Triple {
239239
}
240240
}
241241

242-
// The Darwin platform version used for linking.
243-
public var darwinLinkerPlatformVersion: Version {
244-
precondition(self.os?.isDarwin ?? false)
245-
switch darwinPlatform! {
246-
case .macOS:
247-
// The integrated driver falls back to `osVersion` for ivalid macOS
248-
// versions, this decision might be worth revisiting.
249-
let macVersion = _macOSVersion ?? osVersion
250-
// The first deployment of arm64 for macOS is version 11
251-
if macVersion.major < 11 && arch == .aarch64 {
252-
return Version(11, 0, 0)
253-
}
254-
255-
return macVersion
256-
case .iOS(.catalyst):
257-
// Mac Catalyst on arm was introduced with an iOS deployment target of
258-
// 14.0; the linker doesn't want to see a deployment target before that.
259-
if _iOSVersion.major < 14 && arch == .aarch64 {
260-
return Version(14, 0, 0)
261-
}
262-
263-
// Mac Catalyst was introduced with an iOS deployment target of 13.1;
264-
// the linker doesn't want to see a deployment target before that.
265-
if _iOSVersion.major < 13 {
266-
return Version(13, 1, 0)
267-
}
268-
269-
return _iOSVersion
270-
case .iOS(.device), .iOS(.simulator), .tvOS(_):
271-
// The first deployment of arm64 simulators is iOS/tvOS 14.0;
272-
// the linker doesn't want to see a deployment target before that.
273-
if _isSimulatorEnvironment && _iOSVersion.major < 14 && arch == .aarch64 {
274-
return Version(14, 0, 0)
275-
}
276-
277-
return _iOSVersion
278-
case .watchOS(_):
279-
// The first deployment of arm64 simulators is watchOS 7;
280-
// the linker doesn't want to see a deployment target before that.
281-
if _isSimulatorEnvironment && osVersion.major < 7 && arch == .aarch64 {
282-
return Version(7, 0, 0)
283-
}
284-
285-
return osVersion
286-
}
287-
}
288-
289242
/// The platform name, i.e. the name clang uses to identify this target in its
290243
/// resource directory.
291244
///
@@ -409,7 +362,8 @@ extension Triple {
409362
case .unavailable:
410363
return false
411364
case .available(let introducedVersion):
412-
return version(for: darwinPlatform) >= introducedVersion
365+
guard let tripleVersion = version(for: darwinPlatform) else { return false }
366+
return tripleVersion >= introducedVersion
413367
case .availableInAllVersions:
414368
return true
415369
}

Sources/Basics/Vendor/Triple.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//===--------------- Triple.swift - Swift Target Triples ------------------===//
1+
//===----------------------------------------------------------------------===//
22
//
3-
// This source file is part of the Swift.org open source project
3+
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
8-
// See https://swift.org/LICENSE.txt for license information
9-
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -72,14 +72,16 @@ public struct Triple {
7272

7373
/// Represents a version that may be present in the target triple.
7474
public struct Version: Equatable, Comparable, CustomStringConvertible {
75-
public static let zero = Version(0, 0, 0)
76-
7775
public var major: Int
7876
public var minor: Int
7977
public var micro: Int
8078

81-
public init<S: StringProtocol>(parse string: S) {
82-
let components = string.split(separator: ".", maxSplits: 3).map{ Int($0) ?? 0 }
79+
public init?(parse string: some StringProtocol) {
80+
guard !string.isEmpty else {
81+
return nil
82+
}
83+
84+
let components = string.split(separator: ".", maxSplits: 3).map { Int($0) ?? 0 }
8385
self.major = components.count > 0 ? components[0] : 0
8486
self.minor = components.count > 1 ? components[1] : 0
8587
self.micro = components.count > 2 ? components[2] : 0
@@ -162,10 +164,9 @@ public struct Triple {
162164

163165
// Now that we've parsed everything, we construct a normalized form of the
164166
// triple string.
165-
triple = parser.components.map({ $0.isEmpty ? "unknown" : $0 }).joined(separator: "-")
166-
}
167-
else {
168-
triple = string
167+
self.triple = parser.components.map { $0.isEmpty ? "unknown" : $0 }.joined(separator: "-")
168+
} else {
169+
self.triple = string
169170
}
170171

171172
// Unpack the parsed data into the fields. If no environment info was found,
@@ -1526,7 +1527,7 @@ extension Triple {
15261527
/// `darwin` OS version number is not adjusted to match the equivalent
15271528
/// `macosx` version number. It's usually better to use `version(for:)`
15281529
/// to get Darwin versions.
1529-
public var osVersion: Version {
1530+
public var osVersion: Version? {
15301531
var osName = self.osName[...]
15311532

15321533
// Assume that the OS portion of the triple starts with the canonical name.
@@ -1569,7 +1570,9 @@ extension Triple {
15691570
/// This accessor is semi-private; it's typically better to use `version(for:)` or
15701571
/// `Triple.FeatureAvailability`.
15711572
public var _macOSVersion: Version? {
1572-
var version = osVersion
1573+
guard var version = osVersion else {
1574+
return nil
1575+
}
15731576

15741577
switch os {
15751578
case .darwin:
@@ -1623,7 +1626,7 @@ extension Triple {
16231626
///
16241627
/// This accessor is semi-private; it's typically better to use `version(for:)` or
16251628
/// `Triple.FeatureAvailability`.
1626-
public var _iOSVersion: Version {
1629+
public var _iOSVersion: Version? {
16271630
switch os {
16281631
case .darwin, .macosx:
16291632
// Ignore the version from the triple. This is only handled because the
@@ -1632,7 +1635,7 @@ extension Triple {
16321635
// OS X.
16331636
return Version(5, 0, 0)
16341637
case .ios, .tvos:
1635-
var version = self.osVersion
1638+
guard var version = self.osVersion else { return nil }
16361639
// Default to 5.0 (or 7.0 for arm64).
16371640
if version.major == 0 {
16381641
version.major = arch == .aarch64 ? 7 : 5
@@ -1650,7 +1653,7 @@ extension Triple {
16501653
///
16511654
/// This accessor is semi-private; it's typically better to use `version(for:)` or
16521655
/// `Triple.FeatureAvailability`.
1653-
public var _watchOSVersion: Version {
1656+
public var _watchOSVersion: Version? {
16541657
switch os {
16551658
case .darwin, .macosx:
16561659
// Ignore the version from the triple. This is only handled because the
@@ -1659,7 +1662,7 @@ extension Triple {
16591662
// OS X.
16601663
return Version(2, 0, 0)
16611664
case .watchos:
1662-
var version = self.osVersion
1665+
guard var version = self.osVersion else { return nil }
16631666
if version.major == 0 {
16641667
version.major = 2
16651668
}

Tests/BasicsTests/TripleTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,54 @@ final class TripleTests: XCTestCase {
164164
XCTAssertTriple("aarch64-unknown-linux-android", matches: (.aarch64, nil, nil, .linux, .android, .elf))
165165
XCTAssertTriple("x86_64-unknown-windows-msvc", matches: (.x86_64, nil, nil, .win32, .msvc, .coff))
166166
XCTAssertTriple("wasm32-unknown-wasi", matches: (.wasm32, nil, nil, .wasi, nil, .wasm))
167+
}
168+
169+
func testTriple() {
170+
let linux = try? Triple("x86_64-unknown-linux-gnu")
171+
XCTAssertNotNil(linux)
172+
XCTAssertEqual(linux!.os, .linux)
173+
XCTAssertNil(linux!.osVersion)
174+
XCTAssertEqual(linux!.environment, .gnu)
175+
176+
let macos = try? Triple("x86_64-apple-macosx10.15")
177+
XCTAssertNotNil(macos!)
178+
XCTAssertEqual(macos!.osVersion, .init(parse: "10.15")!)
179+
let newVersion = "10.12"
180+
let tripleString = macos!.tripleString(forPlatformVersion: newVersion)
181+
XCTAssertEqual(tripleString, "x86_64-apple-macosx10.12")
182+
let macosNoX = try? Triple("x86_64-apple-macos12.2")
183+
XCTAssertNotNil(macosNoX!)
184+
XCTAssertEqual(macosNoX!.os, .macosx)
185+
XCTAssertEqual(macosNoX!.osVersion, .init(parse: "12.2")!)
186+
187+
let android = try? Triple("aarch64-unknown-linux-android24")
188+
XCTAssertNotNil(android)
189+
XCTAssertEqual(android!.os, .linux)
190+
XCTAssertEqual(android!.environment, .android)
191+
192+
let linuxWithABIVersion = try? Triple("x86_64-unknown-linux-gnu42")
193+
XCTAssertEqual(linuxWithABIVersion!.environment, .gnu)
194+
}
195+
196+
func testEquality() throws {
197+
let macOSTriple = try Triple("arm64-apple-macos")
198+
let macOSXTriple = try Triple("arm64-apple-macosx")
199+
XCTAssertEqual(macOSTriple, macOSXTriple)
200+
201+
let intelMacOSTriple = try Triple("x86_64-apple-macos")
202+
XCTAssertNotEqual(macOSTriple, intelMacOSTriple)
203+
204+
let linuxWithoutGNUABI = try Triple("x86_64-unknown-linux")
205+
let linuxWithGNUABI = try Triple("x86_64-unknown-linux-gnu")
206+
XCTAssertNotEqual(linuxWithoutGNUABI, linuxWithGNUABI)
207+
}
208+
209+
func testWASI() throws {
210+
let wasi = try Triple("wasm32-unknown-wasi")
211+
212+
// WASI dynamic libraries are only experimental,
213+
// but SwiftPM requires this property not to crash.
214+
_ = wasi.dynamicLibraryExtension
167215
}
168216

169217
func testIsRuntimeCompatibleWith() throws {

0 commit comments

Comments
 (0)