Skip to content

Commit 26b0b77

Browse files
MaxDesiatovrauhul
andauthored
[5.9] fix apple and darwin triples disambiguation (#6722)
Cherry-pick of #6478. Fixes a bug where all triples with vendor `apple` are considered to be OS variants of `darwin`. This change special cases `apple-none` to not match `Triple.isDarwin()`, but all other "apple-*" triples will still be considered to be a variant `darwin`. Fixes some fallout of making the above change to check for the newly added `Triple.isApple()` instead of `Triple.isDarwin()` to allow the triple `apple-none` to opt into behaviors like dead stripping, using libtool, etc. As a result of these changes SwiftPM no longer appends a macOS version number to the `armv7em-apple-none-macho` triple (e.g. `armv7em-apple-none-macho10.13`) removing the work around of passing `-target armv7em-apple-none-macho` via cli args or via a `toolset.json`. Additionally SwiftPM no longer passes unexpected `-rpath` args to the linker when building a static binary for `armv7em-apple-none-macho`. # Conflicts: # Tests/BasicsTests/TripleTests.swift Co-authored-by: Rauhul Varma <[email protected]>
1 parent 316a3d2 commit 26b0b77

File tree

6 files changed

+81
-7
lines changed

6 files changed

+81
-7
lines changed

Sources/Basics/Triple.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,23 @@ public struct Triple: Encodable, Equatable, Sendable {
162162
return nil
163163
}
164164

165+
public func isApple() -> Bool {
166+
vendor == .apple
167+
}
168+
165169
public func isAndroid() -> Bool {
166170
os == .linux && abi == .android
167171
}
168172

169173
public func isDarwin() -> Bool {
170-
vendor == .apple || os == .macOS || os == .darwin
174+
switch (vendor, os) {
175+
case (.apple, .noneOS):
176+
return false
177+
case (.apple, _), (_, .macOS), (_, .darwin):
178+
return true
179+
default:
180+
return false
181+
}
171182
}
172183

173184
public func isLinux() -> Bool {

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
113113
case .debug:
114114
return []
115115
case .release:
116-
if self.buildParameters.triple.isDarwin() {
116+
if self.buildParameters.triple.isApple() {
117117
return ["-Xlinker", "-dead_strip"]
118118
} else if self.buildParameters.triple.isWindows() {
119119
return ["-Xlinker", "/OPT:REF"]
@@ -139,7 +139,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
139139
if triple.isWindows(), librarian.hasSuffix("link") || librarian.hasSuffix("link.exe") {
140140
return try [librarian, "/LIB", "/OUT:\(binaryPath.pathString)", "@\(self.linkFileListPath.pathString)"]
141141
}
142-
if triple.isDarwin(), librarian.hasSuffix("libtool") {
142+
if triple.isApple(), librarian.hasSuffix("libtool") {
143143
return try [librarian, "-static", "-o", binaryPath.pathString, "@\(self.linkFileListPath.pathString)"]
144144
}
145145
return try [librarian, "crs", binaryPath.pathString, "@\(self.linkFileListPath.pathString)"]

Sources/Build/BuildPlan.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension BuildParameters {
138138
/// Computes the linker flags to use in order to rename a module-named main function to 'main' for the target platform, or nil if the linker doesn't support it for the platform.
139139
func linkerFlagsForRenamingMainFunction(of target: ResolvedTarget) -> [String]? {
140140
let args: [String]
141-
if self.triple.isDarwin() {
141+
if self.triple.isApple() {
142142
args = ["-alias", "_\(target.c99name)_main", "_main"]
143143
}
144144
else if self.triple.isLinux() {

Sources/PackageModel/UserToolchain.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public final class UserToolchain: Toolchain {
137137
) throws
138138
-> AbsolutePath
139139
{
140-
let variable: String = triple.isDarwin() ? "LIBTOOL" : "AR"
140+
let variable: String = triple.isApple() ? "LIBTOOL" : "AR"
141141
let tool: String = {
142-
if triple.isDarwin() { return "libtool" }
142+
if triple.isApple() { return "libtool" }
143143
if triple.isWindows() {
144144
if let librarian: AbsolutePath =
145145
UserToolchain.lookup(

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public struct BuildParameters: Encodable {
380380
return nil
381381
}
382382

383-
if triple.isDarwin() {
383+
if triple.isApple() {
384384
return .swiftAST
385385
}
386386
return .modulewrap

Tests/BasicsTests/TripleTests.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,69 @@ import Basics
1414
import XCTest
1515

1616
final class TripleTests: XCTestCase {
17+
func testIsAppleIsDarwin() {
18+
func XCTAssertTriple(
19+
_ triple: String,
20+
isApple: Bool,
21+
isDarwin: Bool,
22+
file: StaticString = #filePath,
23+
line: UInt = #line
24+
) {
25+
guard let triple = try? Triple(triple) else {
26+
XCTFail(
27+
"Unknown triple '\(triple)'.",
28+
file: file,
29+
line: line)
30+
return
31+
}
32+
XCTAssert(
33+
isApple == triple.isApple(),
34+
"""
35+
Expected triple '\(triple.tripleString)' \
36+
\(isApple ? "" : " not") to be an Apple triple.
37+
""",
38+
file: file,
39+
line: line)
40+
XCTAssert(
41+
isDarwin == triple.isDarwin(),
42+
"""
43+
Expected triple '\(triple.tripleString)' \
44+
\(isDarwin ? "" : " not") to be a Darwin triple.
45+
""",
46+
file: file,
47+
line: line)
48+
}
49+
50+
XCTAssertTriple("x86_64-pc-linux-gnu", isApple: false, isDarwin: false)
51+
XCTAssertTriple("x86_64-pc-linux-musl", isApple: false, isDarwin: false)
52+
XCTAssertTriple("powerpc-bgp-linux", isApple: false, isDarwin: false)
53+
XCTAssertTriple("arm-none-none-eabi", isApple: false, isDarwin: false)
54+
XCTAssertTriple("arm-none-linux-musleabi", isApple: false, isDarwin: false)
55+
XCTAssertTriple("wasm32-unknown-wasi", isApple: false, isDarwin: false)
56+
XCTAssertTriple("riscv64-unknown-linux", isApple: false, isDarwin: false)
57+
XCTAssertTriple("mips-mti-linux-gnu", isApple: false, isDarwin: false)
58+
XCTAssertTriple("mipsel-img-linux-gnu", isApple: false, isDarwin: false)
59+
XCTAssertTriple("mips64-mti-linux-gnu", isApple: false, isDarwin: false)
60+
XCTAssertTriple("mips64el-img-linux-gnu", isApple: false, isDarwin: false)
61+
XCTAssertTriple("mips64el-img-linux-gnuabin32", isApple: false, isDarwin: false)
62+
XCTAssertTriple("mips64-unknown-linux-gnuabi64", isApple: false, isDarwin: false)
63+
XCTAssertTriple("mips64-unknown-linux-gnuabin32", isApple: false, isDarwin: false)
64+
XCTAssertTriple("mipsel-unknown-linux-gnu", isApple: false, isDarwin: false)
65+
XCTAssertTriple("mips-unknown-linux-gnu", isApple: false, isDarwin: false)
66+
XCTAssertTriple("arm-oe-linux-gnueabi", isApple: false, isDarwin: false)
67+
XCTAssertTriple("aarch64-oe-linux", isApple: false, isDarwin: false)
68+
XCTAssertTriple("armv7em-unknown-none-macho", isApple: false, isDarwin: false)
69+
XCTAssertTriple("armv7em-apple-none-macho", isApple: true, isDarwin: false)
70+
XCTAssertTriple("armv7em-apple-none", isApple: true, isDarwin: false)
71+
XCTAssertTriple("aarch64-apple-macosx", isApple: true, isDarwin: true)
72+
XCTAssertTriple("x86_64-apple-macosx", isApple: true, isDarwin: true)
73+
XCTAssertTriple("x86_64-apple-macosx10.15", isApple: true, isDarwin: true)
74+
XCTAssertTriple("x86_64h-apple-darwin", isApple: true, isDarwin: true)
75+
XCTAssertTriple("i686-pc-windows-msvc", isApple: false, isDarwin: false)
76+
XCTAssertTriple("i686-pc-windows-gnu", isApple: false, isDarwin: false)
77+
XCTAssertTriple("i686-pc-windows-cygnus", isApple: false, isDarwin: false)
78+
}
79+
1780
func testDescription() throws {
1881
let triple = try Triple("x86_64-pc-linux-gnu")
1982
XCTAssertEqual("foo \(triple) bar", "foo x86_64-pc-linux-gnu bar")

0 commit comments

Comments
 (0)