Skip to content

Commit d18cdb4

Browse files
Use SwiftDriver's Triple parser (#72)
SwiftDriver's `Triple` parser is more robust than our bespoke one, and it's already tested. * Replace all use of `Triple` with the SwiftDriver's one * "uname -m" and "sw_vers" commands are replaced with libc `uname` * Darwin version -> macOS version conversion is now done by the `Triple`
1 parent d877b72 commit d18cdb4

16 files changed

+1855
-199
lines changed

Sources/GeneratorCLI/GeneratorCLI.swift

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ struct GeneratorCLI: AsyncParsableCommand {
2323
defaultSubcommand: MakeLinuxSDK.self
2424
)
2525

26-
static func run<Recipe: SwiftSDKRecipe>(recipe: Recipe, options: GeneratorOptions) async throws {
26+
static func run<Recipe: SwiftSDKRecipe>(
27+
recipe: Recipe,
28+
hostTriple: Triple,
29+
targetTriple: Triple,
30+
options: GeneratorOptions
31+
) async throws {
2732
let elapsed = try await ContinuousClock().measure {
2833
let logger = Logger(label: "org.swift.swift-sdk-generator")
29-
30-
let (hostTriple, targetTriple) = try await options.deriveTriples()
3134
let generator = try await SwiftSDKGenerator(
3235
bundleVersion: options.bundleVersion,
3336
hostTriple: hostTriple,
@@ -56,7 +59,12 @@ struct GeneratorCLI: AsyncParsableCommand {
5659
}
5760
}
5861

59-
extension Triple.CPU: ExpressibleByArgument {}
62+
extension Triple.Arch: ExpressibleByArgument {}
63+
extension Triple: ExpressibleByArgument {
64+
public init?(argument: String) {
65+
self.init(argument, normalizing: true)
66+
}
67+
}
6068

6169
extension GeneratorCLI {
6270
struct GeneratorOptions: ParsableArguments {
@@ -94,29 +102,26 @@ extension GeneratorCLI {
94102
help: """
95103
CPU architecture of the host triple of the bundle. Defaults to a triple of the machine this generator is \
96104
running on if unspecified. Available options: \(
97-
Triple.CPU.allCases.map { "`\($0.rawValue)`" }.joined(separator: ", ")
105+
Triple.Arch.allCases.map { "`\($0.rawValue)`" }.joined(separator: ", ")
98106
).
99107
"""
100108
)
101-
var hostArch: Triple.CPU? = nil
109+
var hostArch: Triple.Arch? = nil
102110

103111
@Option(
104112
help: """
105113
CPU architecture of the target triple of the bundle. Same as the host triple CPU architecture if unspecified. \
106-
Available options: \(Triple.CPU.allCases.map { "`\($0.rawValue)`" }.joined(separator: ", ")).
114+
Available options: \(Triple.Arch.allCases.map { "`\($0.rawValue)`" }.joined(separator: ", ")).
107115
"""
108116
)
109-
var targetArch: Triple.CPU? = nil
110-
111-
func deriveTriples() async throws -> (hostTriple: Triple, targetTriple: Triple) {
112-
let hostTriple = try await SwiftSDKGenerator.getHostTriple(explicitArch: hostArch, isVerbose: verbose)
113-
let targetTriple = Triple(
114-
cpu: targetArch ?? hostTriple.cpu,
115-
vendor: .unknown,
116-
os: .linux,
117-
environment: .gnu
118-
)
119-
return (hostTriple, targetTriple)
117+
var targetArch: Triple.Arch? = nil
118+
119+
func deriveHostTriple() throws -> Triple {
120+
let current = try SwiftSDKGenerator.getCurrentTriple(isVerbose: verbose)
121+
if let explicitArch = hostArch {
122+
return Triple(arch: explicitArch, vendor: current.vendor, os: current.os!)
123+
}
124+
return current
120125
}
121126
}
122127

@@ -155,6 +160,10 @@ extension GeneratorCLI {
155160
)
156161
var linuxDistributionVersion: String?
157162

163+
func deriveTargetTriple(hostTriple: Triple) -> Triple {
164+
Triple(arch: self.generatorOptions.targetArch ?? hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu)
165+
}
166+
158167
func run() async throws {
159168
if isInvokedAsDefaultSubcommand() {
160169
print("deprecated: Please explicity specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk")
@@ -167,7 +176,8 @@ extension GeneratorCLI {
167176
}
168177
let linuxDistributionVersion = self.linuxDistributionVersion ?? linuxDistributionDefaultVersion
169178
let linuxDistribution = try LinuxDistribution(name: linuxDistributionName, version: linuxDistributionVersion)
170-
let (_, targetTriple) = try await generatorOptions.deriveTriples()
179+
let hostTriple = try self.generatorOptions.deriveHostTriple()
180+
let targetTriple = self.deriveTargetTriple(hostTriple: hostTriple)
171181

172182
let recipe = try LinuxRecipe(
173183
targetTriple: targetTriple,
@@ -178,7 +188,7 @@ extension GeneratorCLI {
178188
withDocker: withDocker,
179189
fromContainerImage: fromContainerImage
180190
)
181-
try await GeneratorCLI.run(recipe: recipe, options: generatorOptions)
191+
try await GeneratorCLI.run(recipe: recipe, hostTriple: hostTriple, targetTriple: targetTriple, options: generatorOptions)
182192
}
183193

184194
func isInvokedAsDefaultSubcommand() -> Bool {

Sources/Helpers/Vendor/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The `Triple.swift` file in this directory has been vendored from the
2+
[swift-driver](https://github.com/apple/swift-driver) project. The purpose of
3+
this vendoring is not to bring the entire swift-driver package as a dependency.

0 commit comments

Comments
 (0)