Skip to content

Commit 437a63c

Browse files
MaxDesiatovrauhul
andauthored
[5.9] Fix triples parsed by Driver not accepted by SwiftPM (#6741)
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 ``` * Fix missing `import struct TSCBasic.AbsolutePath` --------- Co-authored-by: Rauhul Varma <[email protected]>
1 parent 184d396 commit 437a63c

File tree

12 files changed

+2465
-355
lines changed

12 files changed

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

0 commit comments

Comments
 (0)