Skip to content

Commit 7e05186

Browse files
Enable host toolchain on macOS by default, improve README.md (#196)
It adds some documentation to help with common issues people run into. But primarily, this PR sets `--host-toolchain` on macOS by default, since most people are using Xcode and not the Swift OSS toolchain to run the generator and build using generated Swift SDKs. --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent bb99c98 commit 7e05186

File tree

4 files changed

+76
-24
lines changed

4 files changed

+76
-24
lines changed

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,43 @@ for Ubuntu Jammy and Swift 5.9 this would be `swift:5.9-jammy-slim`. If you'd li
116116
an arbitrary Ubuntu Jammy system, make sure you pass `--static-swift-stdlib` flag to `swift build`, in addition
117117
to the `--experimental-swift-sdk` option.
118118

119+
## Common Generator Options
120+
121+
By default, on macOS hosts running on Apple Silicon, the Swift SDK Generator will create Swift SDKs
122+
for Ubuntu Jammy on aarch64, which matches the CPU architecture of the host. However, it is possible to change
123+
the default target architecture by passing the `--target-arch` flag:
124+
125+
```bash
126+
swift run swift-sdk-generator make-linux-sdk --target-arch x86_64
127+
```
128+
129+
This will default to building the Swift SDK for `x86_64-unknown-linux-gnu`. To build for other
130+
platforms and environments, supply the `--target` flag with the full target triple instead.
131+
132+
The Linux distribution name and version can also be passed to change from the default of Ubuntu Jammy:
133+
134+
```bash
135+
swift run swift-sdk-generator make-linux-sdk --distribution-name ubuntu --distribution-version 24.04
136+
```
137+
138+
### Host Toolchain
139+
140+
The host toolchain is not included in the generated Swift SDK by default on Linux to match the behavior
141+
of the [Static Linux Swift SDKs](https://www.swift.org/documentation/articles/static-linux-getting-started.html)
142+
downloadable from [swift.org](https://www.swift.org/install/). However, on macOS, since most users are using Xcode
143+
and are likely not using the Swift OSS toolchain to build and run Swift projects, the Swift host toolchain
144+
is included by *default*. This default behavior can be changed by passing `--no-host-toolchain`:
145+
146+
```bash
147+
swift run swift-sdk-generator make-linux-sdk --no-host-toolchain --target-arch x86_64
148+
```
149+
150+
To generate the Swift SDK on Linux with the host toolchain included, add `--host-toolchain`:
151+
152+
```bash
153+
swift run swift-sdk-generator make-linux-sdk --host-toolchain --target-arch aarch64
154+
```
155+
119156
## Building an SDK from a container image
120157

121158
You can base your SDK on a container image, such as one of the
@@ -125,10 +162,10 @@ Jammy image:
125162
```
126163
swift run swift-sdk-generator make-linux-sdk --with-docker
127164
```
128-
To build a RHEL images, use the `--linux-distribution-name` option.
165+
To build a RHEL images, use the `--distribution-name` option.
129166
The following command will build a `ubi9`-based image:
130167
```
131-
swift run swift-sdk-generator make-linux-sdk --with-docker --linux-distribution-name rhel
168+
swift run swift-sdk-generator make-linux-sdk --with-docker --distribution-name rhel
132169
```
133170

134171
You can also specify the base container image by name:
@@ -138,7 +175,7 @@ swift run swift-sdk-generator make-linux-sdk --from-container-image swift:5.9-ja
138175
```
139176

140177
```
141-
swift run swift-sdk-generator make-linux-sdk --with-docker --linux-distribution-name rhel --from-container-image swift:5.9-rhel-ubi9
178+
swift run swift-sdk-generator make-linux-sdk --with-docker --distribution-name rhel --from-container-image swift:5.9-rhel-ubi9
142179
```
143180

144181
### Including extra Linux libraries

Sources/GeneratorCLI/GeneratorCLI.swift

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension GeneratorCLI {
131131
but requires exactly the same version of the swift.org toolchain to be installed for it to work.
132132
"""
133133
)
134-
var hostToolchain: Bool = false
134+
var hostToolchain: Bool = hostToolchainDefault
135135

136136
@Option(
137137
help: """
@@ -149,17 +149,31 @@ extension GeneratorCLI {
149149
var host: Triple? = nil
150150

151151
@Option(
152-
help: """
153-
The target triple of the bundle. The default depends on a recipe used for SDK generation. Pass `--help` to a specific recipe subcommand for more details.
154-
"""
152+
help:
153+
"The target triple of the bundle. The default depends on a recipe used for SDK generation."
155154
)
156155
var target: Triple? = nil
157156

158157
@Option(help: "Deprecated. Use `--host` instead")
159158
var hostArch: Triple.Arch? = nil
160-
@Option(help: "Deprecated. Use `--target` instead")
159+
@Option(
160+
help: """
161+
The target arch of the bundle. The default depends on a recipe used for SDK generation. \
162+
If this is passed, the target triple will default to `<target-arch>-unknown-linux-gnu`. \
163+
Use the `--target` param to pass the full target triple if needed.
164+
"""
165+
)
161166
var targetArch: Triple.Arch? = nil
162167

168+
/// Default to adding host toolchain when building on macOS
169+
static var hostToolchainDefault: Bool {
170+
#if os(macOS)
171+
true
172+
#else
173+
false
174+
#endif
175+
}
176+
163177
func deriveHostTriple() throws -> Triple {
164178
if let host {
165179
return host
@@ -204,7 +218,7 @@ extension GeneratorCLI {
204218
""",
205219
transform: LinuxDistribution.Name.init(nameString:)
206220
)
207-
var linuxDistributionName = LinuxDistribution.Name.ubuntu
221+
var distributionName = LinuxDistribution.Name.ubuntu
208222

209223
@Option(
210224
help: """
@@ -214,7 +228,7 @@ extension GeneratorCLI {
214228
- Available options for RHEL: `ubi9` (default when `--distribution-name` is `rhel`).
215229
"""
216230
)
217-
var linuxDistributionVersion: String?
231+
var distributionVersion: String?
218232

219233
func deriveTargetTriple(hostTriple: Triple) -> Triple {
220234
if let target = generatorOptions.target {
@@ -223,8 +237,9 @@ extension GeneratorCLI {
223237
if let arch = generatorOptions.targetArch {
224238
let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu)
225239
appLogger.warning(
226-
"deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`"
240+
"Using `--target-arch \(arch)` defaults to `\(target.triple)`. Use `--target` if you want to pass the full target triple."
227241
)
242+
return target
228243
}
229244
return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu)
230245
}
@@ -235,20 +250,20 @@ extension GeneratorCLI {
235250
"deprecated: Please explicitly specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk"
236251
)
237252
}
238-
let linuxDistributionDefaultVersion: String
239-
switch self.linuxDistributionName {
253+
let distributionDefaultVersion: String
254+
switch self.distributionName {
240255
case .rhel:
241-
linuxDistributionDefaultVersion = "ubi9"
256+
distributionDefaultVersion = "ubi9"
242257
case .ubuntu:
243-
linuxDistributionDefaultVersion = "22.04"
258+
distributionDefaultVersion = "22.04"
244259
case .debian:
245-
linuxDistributionDefaultVersion = "12"
260+
distributionDefaultVersion = "12"
246261
}
247-
let linuxDistributionVersion =
248-
self.linuxDistributionVersion ?? linuxDistributionDefaultVersion
262+
let distributionVersion =
263+
self.distributionVersion ?? distributionDefaultVersion
249264
let linuxDistribution = try LinuxDistribution(
250-
name: linuxDistributionName,
251-
version: linuxDistributionVersion
265+
name: distributionName,
266+
version: distributionVersion
252267
)
253268
let hostTriple = try self.generatorOptions.deriveHostTriple()
254269
let targetTriple = self.deriveTargetTriple(hostTriple: hostTriple)

Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension GeneratorError: CustomStringConvertible {
4141
return "Process launched with \(commandInfo) failed with exit code \(exitCode)"
4242
case let .unknownLinuxDistribution(name, version):
4343
return
44-
"Linux distribution `\(name)`\(version.map { " with version \($0)" } ?? "")` is not supported by this generator."
44+
"Linux distribution `\(name)`\(version.map { " with version `\($0)`" } ?? "") is not supported by this generator."
4545
case let .unknownMacOSVersion(version):
4646
return "macOS version `\(version)` is not supported by this generator."
4747
case let .unknownCPUArchitecture(cpu):

Tests/SwiftSDKGeneratorTests/EndToEndTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ final class RepeatedBuildTests: XCTestCase {
140140
do {
141141
try await Shell.run("docker ps")
142142
possibleArguments.append(
143-
"--with-docker --linux-distribution-name rhel --linux-distribution-version ubi9"
143+
"--with-docker --distribution-name rhel --distribution-version ubi9"
144144
)
145145
} catch {
146146
self.logger.warning(
@@ -223,8 +223,8 @@ struct SDKConfiguration {
223223
"--swift-version \(swiftVersion)-RELEASE",
224224
testLinuxSwiftSDKs ? "--host \(hostArch!)-unknown-linux-gnu" : nil,
225225
"--target \(architecture)-unknown-linux-gnu",
226-
"--linux-distribution-name \(linuxDistributionName)",
227-
"--linux-distribution-version \(linuxDistributionVersion)",
226+
"--distribution-name \(linuxDistributionName)",
227+
"--distribution-version \(linuxDistributionVersion)",
228228
].compactMap { $0 }.joined(separator: " ")
229229
}
230230
}

0 commit comments

Comments
 (0)