Skip to content

Commit b8b0dc8

Browse files
rauhulMaxDesiatov
authored andcommitted
[5.9] Fix triples parsed by Driver not accepted by SwiftPM
Cherry-pick of #6627 Removes and replaces the custom LLVM Triple type from the Basics module with the implementation from the Swift Driver. This change allows SwiftPM to understand all triples that the Swift Driver already does and resolves some issues surrounding incorrect triple parsing. For example the Basics.Triple was not able to parse 'armv7em-apple-none-eabihf-macho' where as SwiftDriver.Triple can. (cherry picked from commit 4e6501b) ``` # Conflicts: # Sources/Basics/CMakeLists.txt # Sources/Basics/Triple.swift ```
1 parent 184d396 commit b8b0dc8

File tree

12 files changed

+2464
-355
lines changed

12 files changed

+2464
-355
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ let package = Package(
192192
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
193193
.product(name: "SystemPackage", package: "swift-system"),
194194
],
195-
exclude: ["CMakeLists.txt"]
195+
exclude: ["CMakeLists.txt", "Vendor/README.md"]
196196
),
197197

198198
.target(

Sources/Basics/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ add_library(Basics
5252
String+Extensions.swift
5353
SwiftVersion.swift
5454
SQLiteBackedCache.swift
55-
Triple.swift
55+
Triple+Basics.swift
5656
Version+Extensions.swift
57-
WritableByteStream+Extensions.swift)
57+
WritableByteStream+Extensions.swift
58+
Vendor/Triple.swift
59+
Vendor/Triple+Platforms.swift)
5860
target_link_libraries(Basics PUBLIC
5961
SwiftCollections::DequeModule
6062
SwiftCollections::OrderedCollections

Sources/Basics/Triple+Basics.swift

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import enum TSCBasic.JSON
14+
import class TSCBasic.Process
15+
16+
extension Triple {
17+
public init(_ description: String) throws {
18+
self.init(description, normalizing: false)
19+
}
20+
}
21+
22+
extension Triple {
23+
public static let macOS = try! Self("x86_64-apple-macosx")
24+
}
25+
26+
extension Triple {
27+
public func isApple() -> Bool {
28+
vendor == .apple
29+
}
30+
31+
public func isAndroid() -> Bool {
32+
os == .linux && environment == .android
33+
}
34+
35+
public func isDarwin() -> Bool {
36+
switch (vendor, os) {
37+
case (.apple, .noneOS):
38+
return false
39+
case (.apple, _), (_, .macosx), (_, .darwin):
40+
return true
41+
default:
42+
return false
43+
}
44+
}
45+
46+
public func isLinux() -> Bool {
47+
os == .linux
48+
}
49+
50+
public func isWindows() -> Bool {
51+
os == .win32
52+
}
53+
54+
public func isWASI() -> Bool {
55+
os == .wasi
56+
}
57+
58+
public func isOpenBSD() -> Bool {
59+
os == .openbsd
60+
}
61+
62+
/// Returns the triple string for the given platform version.
63+
///
64+
/// This is currently meant for Apple platforms only.
65+
public func tripleString(forPlatformVersion version: String) -> String {
66+
precondition(isDarwin())
67+
// This function did not handle triples with a specific environments and
68+
// object formats previously to using SwiftDriver.Triple and still does
69+
// not.
70+
return """
71+
\(self.archName)-\
72+
\(self.vendorName)-\
73+
\(self.osNameUnversioned)\(version)
74+
"""
75+
}
76+
77+
public var tripleString: String {
78+
self.triple
79+
}
80+
81+
/// Determine the versioned host triple using the Swift compiler.
82+
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
83+
// Call the compiler to get the target info JSON.
84+
let compilerOutput: String
85+
do {
86+
let result = try Process.popen(args: swiftCompiler.pathString, "-print-target-info")
87+
compilerOutput = try result.utf8Output().spm_chomp()
88+
} catch {
89+
throw InternalError("Failed to get target info (\(error.interpolationDescription))")
90+
}
91+
// Parse the compiler's JSON output.
92+
let parsedTargetInfo: JSON
93+
do {
94+
parsedTargetInfo = try JSON(string: compilerOutput)
95+
} catch {
96+
throw InternalError(
97+
"Failed to parse target info (\(error.interpolationDescription)).\nRaw compiler output: \(compilerOutput)"
98+
)
99+
}
100+
// Get the triple string from the parsed JSON.
101+
let tripleString: String
102+
do {
103+
tripleString = try parsedTargetInfo.get("target").get("triple")
104+
} catch {
105+
throw InternalError(
106+
"Target info does not contain a triple string (\(error.interpolationDescription)).\nTarget info: \(parsedTargetInfo)"
107+
)
108+
}
109+
110+
// Parse the triple string.
111+
do {
112+
return try Triple(tripleString)
113+
} catch {
114+
throw InternalError(
115+
"Failed to parse triple string (\(error.interpolationDescription)).\nTriple string: \(tripleString)"
116+
)
117+
}
118+
}
119+
}
120+
121+
extension Triple {
122+
/// The file prefix for dynamic libraries
123+
public var dynamicLibraryPrefix: String {
124+
switch os {
125+
case .win32:
126+
return ""
127+
default:
128+
return "lib"
129+
}
130+
}
131+
132+
/// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`)
133+
public var dynamicLibraryExtension: String {
134+
guard let os = self.os else {
135+
fatalError("Cannot create dynamic libraries unknown os.")
136+
}
137+
138+
switch os {
139+
case .darwin, .macosx:
140+
return ".dylib"
141+
case .linux, .openbsd:
142+
return ".so"
143+
case .win32:
144+
return ".dll"
145+
case .wasi:
146+
return ".wasm"
147+
default:
148+
fatalError("Cannot create dynamic libraries for os \"\(os)\".")
149+
}
150+
}
151+
152+
public var executableExtension: String {
153+
guard let os = self.os else {
154+
return ""
155+
}
156+
157+
switch os {
158+
case .darwin, .macosx:
159+
return ""
160+
case .linux, .openbsd:
161+
return ""
162+
case .wasi:
163+
return ".wasm"
164+
case .win32:
165+
return ".exe"
166+
case .noneOS:
167+
return ""
168+
default:
169+
return ""
170+
}
171+
}
172+
173+
/// The file extension for static libraries.
174+
public var staticLibraryExtension: String {
175+
".a"
176+
}
177+
178+
/// The file extension for Foundation-style bundle.
179+
public var nsbundleExtension: String {
180+
switch os {
181+
case .darwin, .macosx:
182+
return ".bundle"
183+
default:
184+
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md
185+
return ".resources"
186+
}
187+
}
188+
}
189+
190+
extension Triple: CustomStringConvertible {
191+
public var description: String { tripleString }
192+
}
193+
194+
extension Triple: Equatable {
195+
public static func == (lhs: Self, rhs: Self) -> Bool {
196+
lhs.triple == rhs.triple
197+
}
198+
}

0 commit comments

Comments
 (0)