From 8dddd0b5e555706a7c2f14fc36b331f036c438d0 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 6 Mar 2025 21:37:47 -0500 Subject: [PATCH 1/7] Default to enabling host toolchain on macOS - It can still be disabled, but most people would want to include it when just building a Swift SDK with minimal params. --- Sources/GeneratorCLI/GeneratorCLI.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index a322f10..2bef784 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -125,7 +125,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: """ @@ -154,6 +154,15 @@ extension GeneratorCLI { @Option(help: "Deprecated. Use `--target` instead") 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 From 003ef465904fda8d419f3f207ef5445763b11002 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 7 Mar 2025 00:41:10 -0500 Subject: [PATCH 2/7] Add documentation for more common generator options most might want to use --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index d456d4d..c4b37a2 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,40 @@ 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` flag: + +```bash +swift run swift-sdk-generator make-linux-sdk --target x86_64-unknown-linux-gnu +``` + +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 --linux-distribution-name ubuntu --linux-distribution-version noble +``` + +### 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 x86_64-unknown-linux-gnu +``` + +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 aarch64-unknown-linux-gnu +``` + ## Building an SDK from a container image You can base your SDK on a container image, such as one of the From 302fa1061b30163f3b0f58bd7037acd43cb02e6e Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 11 Mar 2025 14:26:23 -0400 Subject: [PATCH 3/7] Add missing return for target when parsed from --target-arch --- Sources/GeneratorCLI/GeneratorCLI.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 21f3668..2280b92 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -229,6 +229,7 @@ extension GeneratorCLI { let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu) appLogger.warning( "deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`") + return target } return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu) } From 51a57303adbe3aaa2838c3a7d392b0eb7b995146 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 11 Mar 2025 20:56:51 -0400 Subject: [PATCH 4/7] Idea: recommend using --target-arch in README.md, with option to use --target for different platforms + environments --- README.md | 11 +++++++---- Sources/GeneratorCLI/GeneratorCLI.swift | 17 +++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c4b37a2..a4f4baf 100644 --- a/README.md +++ b/README.md @@ -120,12 +120,15 @@ to the `--experimental-swift-sdk` option. 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` flag: +the default target architecture by passing the `--target-arch` flag: ```bash -swift run swift-sdk-generator make-linux-sdk --target x86_64-unknown-linux-gnu +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 @@ -141,13 +144,13 @@ and are likely not using the Swift OSS toolchain to build and run Swift projects 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 x86_64-unknown-linux-gnu +swift run swift-sdk-generator make-linux-sdk --no-host-toolchain --target 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 aarch64-unknown-linux-gnu +swift run swift-sdk-generator make-linux-sdk --host-toolchain --target aarch64 ``` ## Building an SDK from a container image diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 2280b92..3c607dc 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -148,15 +148,19 @@ 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 `-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 @@ -228,7 +232,8 @@ 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) From c1e4bf3e3b95b713fab86e014f9d34bcc1bbdc9e Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 12 Mar 2025 11:21:16 +0000 Subject: [PATCH 5/7] Clean up formatting in `GeneratorCLI.swift` --- Sources/GeneratorCLI/GeneratorCLI.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 3c607dc..520d362 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -149,7 +149,8 @@ extension GeneratorCLI { @Option( help: - "The target triple of the bundle. The default depends on a recipe used for SDK generation.") + "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") From 60aabbe48afc357a8be6dc47fb0ab86f7858dfe2 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Wed, 12 Mar 2025 07:27:48 -0400 Subject: [PATCH 6/7] Rename --linux-distribution* to just --distribution*, fixup README and typo in GeneratorError.unknownLinuxDistribution --- README.md | 12 +++++----- Sources/GeneratorCLI/GeneratorCLI.swift | 22 +++++++++---------- .../SystemUtils/GeneratorError.swift | 2 +- .../EndToEndTests.swift | 4 ++-- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a4f4baf..d3d9b39 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ platforms and environments, supply the `--target` flag with the full target trip 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 --linux-distribution-name ubuntu --linux-distribution-version noble +swift run swift-sdk-generator make-linux-sdk --distribution-name ubuntu --distribution-version 24.04 ``` ### Host Toolchain @@ -144,13 +144,13 @@ and are likely not using the Swift OSS toolchain to build and run Swift projects 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 x86_64 +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 aarch64 +swift run swift-sdk-generator make-linux-sdk --host-toolchain --target-arch aarch64 ``` ## Building an SDK from a container image @@ -162,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: @@ -175,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 diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 3c607dc..0f94501 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -214,16 +214,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 { @@ -245,17 +245,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) diff --git a/Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift b/Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift index e7706de..a5959da 100644 --- a/Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift +++ b/Sources/SwiftSDKGenerator/SystemUtils/GeneratorError.swift @@ -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): diff --git a/Tests/SwiftSDKGeneratorTests/EndToEndTests.swift b/Tests/SwiftSDKGeneratorTests/EndToEndTests.swift index e18f543..f93b20b 100644 --- a/Tests/SwiftSDKGeneratorTests/EndToEndTests.swift +++ b/Tests/SwiftSDKGeneratorTests/EndToEndTests.swift @@ -138,7 +138,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.") @@ -198,7 +198,7 @@ struct SDKConfiguration { "--swift-version \(swiftVersion)-RELEASE", testLinuxSwiftSDKs ? "--host \(hostArch!)-unknown-linux-gnu" : nil, "--target \(architecture)-unknown-linux-gnu", - "--linux-distribution-name \(linuxDistributionName)", + "--distribution-name \(linuxDistributionName)", ].compactMap { $0 }.joined(separator: " ") } } From bc0661bfb1530c76a70589f7e06e3ec551e2ff67 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 12 Mar 2025 11:34:59 +0000 Subject: [PATCH 7/7] Fix formatting in `GeneratorCLI.swift` --- Sources/GeneratorCLI/GeneratorCLI.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 377e180..570d994 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -150,7 +150,7 @@ extension GeneratorCLI { @Option( 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")