Skip to content

Commit 6e55a2f

Browse files
Add --{host,target}-swift-package-path to use self-built toolchains (#75)
Generalized `--{host,target}-swift-package-path` options to be common generator options based on PR feedback #74 (comment)
1 parent 452c88c commit 6e55a2f

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

Sources/GeneratorCLI/GeneratorCLI.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ extension GeneratorCLI {
9898
@Option(help: "Version of Swift to supply in the bundle.")
9999
var swiftVersion = "5.9.2-RELEASE"
100100

101+
@Option(
102+
help: """
103+
Path to the Swift toolchain package containing the Swift compiler that runs on the host platform.
104+
"""
105+
)
106+
var hostSwiftPackagePath: String? = nil
107+
108+
@Option(
109+
help: """
110+
Path to the Swift toolchain package containing the Swift standard library that runs on the target platform.
111+
"""
112+
)
113+
var targetSwiftPackagePath: String? = nil
114+
101115
@Option(
102116
help: """
103117
The host triple of the bundle. Defaults to a triple of the machine this generator is \
@@ -203,7 +217,9 @@ extension GeneratorCLI {
203217
swiftBranch: generatorOptions.swiftBranch,
204218
lldVersion: lldVersion,
205219
withDocker: withDocker,
206-
fromContainerImage: fromContainerImage
220+
fromContainerImage: fromContainerImage,
221+
hostSwiftPackagePath: generatorOptions.hostSwiftPackagePath,
222+
targetSwiftPackagePath: generatorOptions.targetSwiftPackagePath
207223
)
208224
try await GeneratorCLI.run(recipe: recipe, hostTriple: hostTriple, targetTriple: targetTriple, options: generatorOptions)
209225
}

Sources/SwiftSDKGenerator/Artifacts/DownloadableArtifacts.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,12 @@ struct DownloadableArtifacts: Sendable {
4040
private(set) var hostLLVM: Item
4141
let targetSwift: Item
4242

43-
private let shouldUseDocker: Bool
44-
var allItems: [Item] {
45-
if self.shouldUseDocker {
46-
[self.hostSwift, self.hostLLVM]
47-
} else {
48-
[self.hostSwift, self.hostLLVM, self.targetSwift]
49-
}
50-
}
51-
5243
private let versions: VersionsConfiguration
5344
private let paths: PathsConfiguration
5445

5546
init(
5647
hostTriple: Triple,
5748
targetTriple: Triple,
58-
shouldUseDocker: Bool,
5949
_ versions: VersionsConfiguration,
6050
_ paths: PathsConfiguration
6151
) throws {
@@ -94,8 +84,6 @@ struct DownloadableArtifacts: Sendable {
9484
.appending("target_swift_\(versions.swiftVersion)_\(targetTriple.triple).tar.gz"),
9585
isPrebuilt: true
9686
)
97-
98-
self.shouldUseDocker = shouldUseDocker
9987
}
10088

10189
mutating func useLLVMSources() {

Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ let byteCountFormatter = ByteCountFormatter()
2929
extension SwiftSDKGenerator {
3030
func downloadArtifacts(
3131
_ client: HTTPClient, _ engine: Engine,
32-
downloadableArtifacts: inout DownloadableArtifacts
32+
downloadableArtifacts: inout DownloadableArtifacts,
33+
itemsToDownload: @Sendable (DownloadableArtifacts) -> [DownloadableArtifacts.Item]
3334
) async throws {
3435
logGenerationStep("Downloading required toolchain packages...")
3536
var headRequest = HTTPClientRequest(url: downloadableArtifacts.hostLLVM.remoteURL.absoluteString)
@@ -43,7 +44,7 @@ extension SwiftSDKGenerator {
4344
}
4445

4546
let results = try await withThrowingTaskGroup(of: FileCacheRecord.self) { group in
46-
for item in downloadableArtifacts.allItems {
47+
for item in itemsToDownload(downloadableArtifacts) {
4748
group.addTask {
4849
try await engine[DownloadArtifactQuery(artifact: item)]
4950
}

Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ import struct SystemPackage.FilePath
1818
public struct LinuxRecipe: SwiftSDKRecipe {
1919
public enum TargetSwiftSource: Sendable {
2020
case docker(baseSwiftDockerImage: String)
21-
case tarball
21+
case localPackage(FilePath)
22+
case remoteTarball
23+
}
24+
25+
public enum HostSwiftSource: Sendable {
26+
case localPackage(FilePath)
27+
case remoteTarball
2228
}
2329

2430
let mainTargetTriple: Triple
2531
let linuxDistribution: LinuxDistribution
2632
let targetSwiftSource: TargetSwiftSource
33+
let hostSwiftSource: HostSwiftSource
2734
let versionsConfiguration: VersionsConfiguration
2835

2936
var shouldUseDocker: Bool {
@@ -40,7 +47,9 @@ public struct LinuxRecipe: SwiftSDKRecipe {
4047
swiftBranch: String?,
4148
lldVersion: String,
4249
withDocker: Bool,
43-
fromContainerImage: String?
50+
fromContainerImage: String?,
51+
hostSwiftPackagePath: String?,
52+
targetSwiftPackagePath: String?
4453
) throws {
4554
let versionsConfiguration = try VersionsConfiguration(
4655
swiftVersion: swiftVersion,
@@ -51,17 +60,28 @@ public struct LinuxRecipe: SwiftSDKRecipe {
5160
)
5261

5362
let targetSwiftSource: LinuxRecipe.TargetSwiftSource
54-
if withDocker {
55-
let imageName = fromContainerImage ?? versionsConfiguration.swiftBaseDockerImage
56-
targetSwiftSource = .docker(baseSwiftDockerImage: imageName)
63+
if let targetSwiftPackagePath {
64+
targetSwiftSource = .localPackage(FilePath(targetSwiftPackagePath))
65+
} else {
66+
if withDocker {
67+
let imageName = fromContainerImage ?? versionsConfiguration.swiftBaseDockerImage
68+
targetSwiftSource = .docker(baseSwiftDockerImage: imageName)
69+
} else {
70+
targetSwiftSource = .remoteTarball
71+
}
72+
}
73+
let hostSwiftSource: HostSwiftSource
74+
if let hostSwiftPackagePath {
75+
hostSwiftSource = .localPackage(FilePath(hostSwiftPackagePath))
5776
} else {
58-
targetSwiftSource = .tarball
77+
hostSwiftSource = .remoteTarball
5978
}
6079

6180
self.init(
6281
mainTargetTriple: targetTriple,
6382
linuxDistribution: linuxDistribution,
6483
targetSwiftSource: targetSwiftSource,
84+
hostSwiftSource: hostSwiftSource,
6585
versionsConfiguration: versionsConfiguration
6686
)
6787
}
@@ -70,11 +90,13 @@ public struct LinuxRecipe: SwiftSDKRecipe {
7090
mainTargetTriple: Triple,
7191
linuxDistribution: LinuxDistribution,
7292
targetSwiftSource: TargetSwiftSource,
93+
hostSwiftSource: HostSwiftSource,
7394
versionsConfiguration: VersionsConfiguration
7495
) {
7596
self.mainTargetTriple = mainTargetTriple
7697
self.linuxDistribution = linuxDistribution
7798
self.targetSwiftSource = targetSwiftSource
99+
self.hostSwiftSource = hostSwiftSource
78100
self.versionsConfiguration = versionsConfiguration
79101
}
80102

@@ -112,12 +134,29 @@ public struct LinuxRecipe: SwiftSDKRecipe {
112134
var downloadableArtifacts = try DownloadableArtifacts(
113135
hostTriple: generator.hostTriple,
114136
targetTriple: generator.targetTriple,
115-
shouldUseDocker: shouldUseDocker,
116137
versionsConfiguration,
117138
generator.pathsConfiguration
118139
)
119140

120-
try await generator.downloadArtifacts(client, engine, downloadableArtifacts: &downloadableArtifacts)
141+
try await generator.downloadArtifacts(
142+
client,
143+
engine,
144+
downloadableArtifacts: &downloadableArtifacts,
145+
itemsToDownload: { artifacts in
146+
var items = [artifacts.hostLLVM]
147+
switch targetSwiftSource {
148+
case .remoteTarball:
149+
items.append(artifacts.targetSwift)
150+
case .docker, .localPackage: break
151+
}
152+
switch hostSwiftSource {
153+
case .remoteTarball:
154+
items.append(artifacts.hostSwift)
155+
case .localPackage: break
156+
}
157+
return items
158+
}
159+
)
121160

122161
if !self.shouldUseDocker {
123162
guard case let .ubuntu(version) = linuxDistribution else {
@@ -134,9 +173,16 @@ public struct LinuxRecipe: SwiftSDKRecipe {
134173
)
135174
}
136175

137-
try await generator.unpackHostSwift(
138-
hostSwiftPackagePath: downloadableArtifacts.hostSwift.localPath
139-
)
176+
switch self.hostSwiftSource {
177+
case .localPackage(let filePath):
178+
try await generator.rsync(
179+
from: filePath.appending("usr"), to: generator.pathsConfiguration.toolchainDirPath
180+
)
181+
case .remoteTarball:
182+
try await generator.unpackHostSwift(
183+
hostSwiftPackagePath: downloadableArtifacts.hostSwift.localPath
184+
)
185+
}
140186

141187
switch self.targetSwiftSource {
142188
case let .docker(baseSwiftDockerImage):
@@ -145,7 +191,11 @@ public struct LinuxRecipe: SwiftSDKRecipe {
145191
baseDockerImage: baseSwiftDockerImage,
146192
sdkDirPath: sdkDirPath
147193
)
148-
case .tarball:
194+
case .localPackage(let filePath):
195+
try await generator.copyTargetSwift(
196+
from: filePath.appending("usr/lib"), sdkDirPath: sdkDirPath
197+
)
198+
case .remoteTarball:
149199
try await generator.unpackTargetSwiftPackage(
150200
targetSwiftPackagePath: downloadableArtifacts.targetSwift.localPath,
151201
relativePathToRoot: [FilePath.Component(versionsConfiguration.swiftDistributionName())!],

Tests/SwiftSDKGeneratorTests/ArchitectureMappingTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ final class ArchitectureMappingTests: XCTestCase {
5151
swiftBranch: nil,
5252
lldVersion: "16.0.4",
5353
withDocker: false,
54-
fromContainerImage: nil
54+
fromContainerImage: nil,
55+
hostSwiftPackagePath: nil,
56+
targetSwiftPackagePath: nil
5557
)
5658
// LocalSwiftSDKGenerator constructs URLs and paths which depend on architectures
5759
let sdk = try await SwiftSDKGenerator(
@@ -76,7 +78,6 @@ final class ArchitectureMappingTests: XCTestCase {
7678
let artifacts = try await DownloadableArtifacts(
7779
hostTriple: sdk.hostTriple,
7880
targetTriple: sdk.targetTriple,
79-
shouldUseDocker: recipe.shouldUseDocker,
8081
recipe.versionsConfiguration,
8182
sdk.pathsConfiguration
8283
)

0 commit comments

Comments
 (0)