Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable host toolchain on macOS by default, improve README.md #196

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ for Ubuntu Jammy and Swift 5.9 this would be `swift:5.9-jammy-slim`. If you'd li
an arbitrary Ubuntu Jammy system, make sure you pass `--static-swift-stdlib` flag to `swift build`, in addition
to the `--experimental-swift-sdk` option.

## Common Generator Options

By default, on macOS hosts running on Apple Silicon, the Swift SDK Generator will create Swift SDKs
for Ubuntu Jammy on aarch64, which matches the arch of the host. However, it is possible to change
the default target architecture by passing the `--target-arch` flag:

```bash
swift run swift-sdk-generator make-linux-sdk --target-arch x86_64
```

This will default to building the Swift SDK for `x86_64-unknown-linux-gnu`. To build for other
platforms and environments, supply the `--target` flag with the full target triple instead.

The Linux distribution name and version can also be passed to change from the default of Ubuntu Jammy:

```bash
swift run swift-sdk-generator make-linux-sdk --distribution-name ubuntu --distribution-version 24.04
```

### Host Toolchain

The host toolchain is not included in the generated Swift SDK by default on Linux to match the behavior
of the [Static Linux Swift SDKs](https://www.swift.org/documentation/articles/static-linux-getting-started.html)
downloadable from [swift.org](https://www.swift.org/install/). However, on macOS, since most users are using Xcode
and are likely not using the Swift OSS toolchain to build and run Swift projects, the Swift host toolchain
is included by *default*. This default behavior can be changed by passing `--no-host-toolchain`:

```bash
swift run swift-sdk-generator make-linux-sdk --no-host-toolchain --target-arch x86_64
```

Or, if on Linux, and desiring to generate the Swift SDK with the host toolchain included, add `--host-toolchain`:

```bash
swift run swift-sdk-generator make-linux-sdk --host-toolchain --target-arch aarch64
```

## Building an SDK from a container image

You can base your SDK on a container image, such as one of the
Expand All @@ -125,10 +162,10 @@ Jammy image:
```
swift run swift-sdk-generator make-linux-sdk --with-docker
```
To build a RHEL images, use the `--linux-distribution-name` option.
To build a RHEL images, use the `--distribution-name` option.
The following command will build a `ubi9`-based image:
```
swift run swift-sdk-generator make-linux-sdk --with-docker --linux-distribution-name rhel
swift run swift-sdk-generator make-linux-sdk --with-docker --distribution-name rhel
```

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

```
swift run swift-sdk-generator make-linux-sdk --with-docker --linux-distribution-name rhel --from-container-image swift:5.9-rhel-ubi9
swift run swift-sdk-generator make-linux-sdk --with-docker --distribution-name rhel --from-container-image swift:5.9-rhel-ubi9
```

### Including extra Linux libraries
Expand Down
50 changes: 33 additions & 17 deletions Sources/GeneratorCLI/GeneratorCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension GeneratorCLI {
but requires exactly the same version of the swift.org toolchain to be installed for it to work.
"""
)
var hostToolchain: Bool = false
var hostToolchain: Bool = hostToolchainDefault

@Option(
help: """
Expand All @@ -148,17 +148,31 @@ extension GeneratorCLI {
var host: Triple? = nil

@Option(
help: """
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.
"""
help:
"The target triple of the bundle. The default depends on a recipe used for SDK generation."
)
var target: Triple? = nil

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

/// Default to adding host toolchain when building on macOS
static var hostToolchainDefault: Bool {
#if os(macOS)
true
#else
false
#endif
}

func deriveHostTriple() throws -> Triple {
if let host {
return host
Expand Down Expand Up @@ -201,16 +215,16 @@ extension GeneratorCLI {
""",
transform: LinuxDistribution.Name.init(nameString:)
)
var linuxDistributionName = LinuxDistribution.Name.ubuntu
var distributionName = LinuxDistribution.Name.ubuntu

@Option(
help: """
Version of the Linux distribution used as a target platform.
Available options for Ubuntu: `20.04`, `22.04` (default when `--linux-distribution-name` is `ubuntu`), `24.04`.
Available options for RHEL: `ubi9` (default when `--linux-distribution-name` is `rhel`).
Available options for Ubuntu: `20.04`, `22.04` (default when `--distribution-name` is `ubuntu`), `24.04`.
Available options for RHEL: `ubi9` (default when `--distribution-name` is `rhel`).
"""
)
var linuxDistributionVersion: String?
var distributionVersion: String?

func deriveTargetTriple(hostTriple: Triple) -> Triple {
if let target = generatorOptions.target {
Expand All @@ -219,7 +233,9 @@ extension GeneratorCLI {
if let arch = generatorOptions.targetArch {
let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu)
appLogger.warning(
"deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`")
"Using `--target-arch \(arch)` defaults to `\(target.triple)`. Use `--target` if you want to pass the full target triple."
)
return target
}
return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu)
}
Expand All @@ -230,17 +246,17 @@ extension GeneratorCLI {
"deprecated: Please explicitly specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk"
)
}
let linuxDistributionDefaultVersion: String
switch self.linuxDistributionName {
let distributionDefaultVersion: String
switch self.distributionName {
case .rhel:
linuxDistributionDefaultVersion = "ubi9"
distributionDefaultVersion = "ubi9"
case .ubuntu:
linuxDistributionDefaultVersion = "22.04"
distributionDefaultVersion = "22.04"
}
let linuxDistributionVersion =
self.linuxDistributionVersion ?? linuxDistributionDefaultVersion
let distributionVersion =
self.distributionVersion ?? distributionDefaultVersion
let linuxDistribution = try LinuxDistribution(
name: linuxDistributionName, version: linuxDistributionVersion)
name: distributionName, version: distributionVersion)
let hostTriple = try self.generatorOptions.deriveHostTriple()
let targetTriple = self.deriveTargetTriple(hostTriple: hostTriple)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension GeneratorError: CustomStringConvertible {
return "Process launched with \(commandInfo) failed with exit code \(exitCode)"
case let .unknownLinuxDistribution(name, version):
return
"Linux distribution `\(name)`\(version.map { " with version \($0)" } ?? "")` is not supported by this generator."
"Linux distribution `\(name)`\(version.map { " with version `\($0)`" } ?? "") is not supported by this generator."
case let .unknownMacOSVersion(version):
return "macOS version `\(version)` is not supported by this generator."
case let .unknownCPUArchitecture(cpu):
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftSDKGeneratorTests/EndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ final class RepeatedBuildTests: XCTestCase {
do {
try await Shell.run("docker ps")
possibleArguments.append(
"--with-docker --linux-distribution-name rhel --linux-distribution-version ubi9")
"--with-docker --distribution-name rhel --distribution-version ubi9")
} catch {
self.logger.warning(
"Docker CLI does not seem to be working, skipping tests that involve Docker.")
Expand Down Expand Up @@ -211,8 +211,8 @@ struct SDKConfiguration {
"--swift-version \(swiftVersion)-RELEASE",
testLinuxSwiftSDKs ? "--host \(hostArch!)-unknown-linux-gnu" : nil,
"--target \(architecture)-unknown-linux-gnu",
"--linux-distribution-name \(linuxDistributionName)",
"--linux-distribution-version \(linuxDistributionVersion)",
"--distribution-name \(linuxDistributionName)",
"--distribution-version \(linuxDistributionVersion)",
].compactMap { $0 }.joined(separator: " ")
}
}
Expand Down