Skip to content

[5.9] Add and fix missing Triple tests, fix soundness checks #6843

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 2 commits into from
Aug 24, 2023
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
8 changes: 6 additions & 2 deletions Sources/Basics/Triple+Basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ extension Triple: CustomStringConvertible {
}

extension Triple: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.triple == rhs.triple
public static func ==(lhs: Triple, rhs: Triple) -> Bool {
lhs.arch == rhs.arch
&& lhs.vendor == rhs.vendor
&& lhs.os == rhs.os
&& lhs.environment == rhs.environment
&& lhs.osVersion == rhs.osVersion
}
}
62 changes: 8 additions & 54 deletions Sources/Basics/Vendor/Triple+Platforms.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//===--------------- Triple+Platforms.swift - Swift Platform Triples ------===//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
// This source file is part of the Swift open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

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

// The Darwin platform version used for linking.
public var darwinLinkerPlatformVersion: Version {
precondition(self.os?.isDarwin ?? false)
switch darwinPlatform! {
case .macOS:
// The integrated driver falls back to `osVersion` for ivalid macOS
// versions, this decision might be worth revisiting.
let macVersion = _macOSVersion ?? osVersion
// The first deployment of arm64 for macOS is version 11
if macVersion.major < 11 && arch == .aarch64 {
return Version(11, 0, 0)
}

return macVersion
case .iOS(.catalyst):
// Mac Catalyst on arm was introduced with an iOS deployment target of
// 14.0; the linker doesn't want to see a deployment target before that.
if _iOSVersion.major < 14 && arch == .aarch64 {
return Version(14, 0, 0)
}

// Mac Catalyst was introduced with an iOS deployment target of 13.1;
// the linker doesn't want to see a deployment target before that.
if _iOSVersion.major < 13 {
return Version(13, 1, 0)
}

return _iOSVersion
case .iOS(.device), .iOS(.simulator), .tvOS(_):
// The first deployment of arm64 simulators is iOS/tvOS 14.0;
// the linker doesn't want to see a deployment target before that.
if _isSimulatorEnvironment && _iOSVersion.major < 14 && arch == .aarch64 {
return Version(14, 0, 0)
}

return _iOSVersion
case .watchOS(_):
// The first deployment of arm64 simulators is watchOS 7;
// the linker doesn't want to see a deployment target before that.
if _isSimulatorEnvironment && osVersion.major < 7 && arch == .aarch64 {
return Version(7, 0, 0)
}

return osVersion
}
}

/// The platform name, i.e. the name clang uses to identify this target in its
/// resource directory.
///
Expand Down Expand Up @@ -409,7 +362,8 @@ extension Triple {
case .unavailable:
return false
case .available(let introducedVersion):
return version(for: darwinPlatform) >= introducedVersion
guard let tripleVersion = version(for: darwinPlatform) else { return false }
return tripleVersion >= introducedVersion
case .availableInAllVersions:
return true
}
Expand Down
41 changes: 22 additions & 19 deletions Sources/Basics/Vendor/Triple.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//===--------------- Triple.swift - Swift Target Triples ------------------===//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
// This source file is part of the Swift open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

Expand Down Expand Up @@ -72,14 +72,16 @@ public struct Triple {

/// Represents a version that may be present in the target triple.
public struct Version: Equatable, Comparable, CustomStringConvertible {
public static let zero = Version(0, 0, 0)

public var major: Int
public var minor: Int
public var micro: Int

public init<S: StringProtocol>(parse string: S) {
let components = string.split(separator: ".", maxSplits: 3).map{ Int($0) ?? 0 }
public init?(parse string: some StringProtocol) {
guard !string.isEmpty else {
return nil
}

let components = string.split(separator: ".", maxSplits: 3).map { Int($0) ?? 0 }
self.major = components.count > 0 ? components[0] : 0
self.minor = components.count > 1 ? components[1] : 0
self.micro = components.count > 2 ? components[2] : 0
Expand Down Expand Up @@ -162,10 +164,9 @@ public struct Triple {

// Now that we've parsed everything, we construct a normalized form of the
// triple string.
triple = parser.components.map({ $0.isEmpty ? "unknown" : $0 }).joined(separator: "-")
}
else {
triple = string
self.triple = parser.components.map { $0.isEmpty ? "unknown" : $0 }.joined(separator: "-")
} else {
self.triple = string
}

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

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

switch os {
case .darwin:
Expand Down Expand Up @@ -1623,7 +1626,7 @@ extension Triple {
///
/// This accessor is semi-private; it's typically better to use `version(for:)` or
/// `Triple.FeatureAvailability`.
public var _iOSVersion: Version {
public var _iOSVersion: Version? {
switch os {
case .darwin, .macosx:
// Ignore the version from the triple. This is only handled because the
Expand All @@ -1632,7 +1635,7 @@ extension Triple {
// OS X.
return Version(5, 0, 0)
case .ios, .tvos:
var version = self.osVersion
guard var version = self.osVersion else { return nil }
// Default to 5.0 (or 7.0 for arm64).
if version.major == 0 {
version.major = arch == .aarch64 ? 7 : 5
Expand All @@ -1650,7 +1653,7 @@ extension Triple {
///
/// This accessor is semi-private; it's typically better to use `version(for:)` or
/// `Triple.FeatureAvailability`.
public var _watchOSVersion: Version {
public var _watchOSVersion: Version? {
switch os {
case .darwin, .macosx:
// Ignore the version from the triple. This is only handled because the
Expand All @@ -1659,7 +1662,7 @@ extension Triple {
// OS X.
return Version(2, 0, 0)
case .watchos:
var version = self.osVersion
guard var version = self.osVersion else { return nil }
if version.major == 0 {
version.major = 2
}
Expand Down
48 changes: 48 additions & 0 deletions Tests/BasicsTests/TripleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,53 @@ final class TripleTests: XCTestCase {
XCTAssertTriple("aarch64-unknown-linux-android", matches: (.aarch64, nil, nil, .linux, .android, .elf))
XCTAssertTriple("x86_64-unknown-windows-msvc", matches: (.x86_64, nil, nil, .win32, .msvc, .coff))
XCTAssertTriple("wasm32-unknown-wasi", matches: (.wasm32, nil, nil, .wasi, nil, .wasm))
}

func testTriple() {
let linux = try? Triple("x86_64-unknown-linux-gnu")
XCTAssertNotNil(linux)
XCTAssertEqual(linux!.os, .linux)
XCTAssertNil(linux!.osVersion)
XCTAssertEqual(linux!.environment, .gnu)

let macos = try? Triple("x86_64-apple-macosx10.15")
XCTAssertNotNil(macos!)
XCTAssertEqual(macos!.osVersion, .init(parse: "10.15")!)
let newVersion = "10.12"
let tripleString = macos!.tripleString(forPlatformVersion: newVersion)
XCTAssertEqual(tripleString, "x86_64-apple-macosx10.12")
let macosNoX = try? Triple("x86_64-apple-macos12.2")
XCTAssertNotNil(macosNoX!)
XCTAssertEqual(macosNoX!.os, .macosx)
XCTAssertEqual(macosNoX!.osVersion, .init(parse: "12.2")!)

let android = try? Triple("aarch64-unknown-linux-android24")
XCTAssertNotNil(android)
XCTAssertEqual(android!.os, .linux)
XCTAssertEqual(android!.environment, .android)

let linuxWithABIVersion = try? Triple("x86_64-unknown-linux-gnu42")
XCTAssertEqual(linuxWithABIVersion!.environment, .gnu)
}

func testEquality() throws {
let macOSTriple = try Triple("arm64-apple-macos")
let macOSXTriple = try Triple("arm64-apple-macosx")
XCTAssertEqual(macOSTriple, macOSXTriple)

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)
}

func testWASI() throws {
let wasi = try Triple("wasm32-unknown-wasi")

// WASI dynamic libraries are only experimental,
// but SwiftPM requires this property not to crash.
_ = wasi.dynamicLibraryExtension
}
}
2 changes: 1 addition & 1 deletion Utilities/soundness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ for language in swift-or-c bash python; do
swift-or-c)
exceptions=(
-name "Package.swift"
-o -path "./Sources/PackageSigning/embedded_resources.swift"
-o -path "./Sources/*/embedded_resources.swift"
-o -path "./Examples/*"
-o -path "./Fixtures/*"
-o -path "./IntegrationTests/*"
Expand Down