-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebAssembly SDK recipe and
make-wasm-sdk
subcommand (#74)
* Add WebAssembly SDK recipe and `make-wasm-sdk` subcommand This patch introduces an experimental WebAssembly SDK recipe and `make-wasm-sdk` subcommand. The make command takes host Swift toolchain package, target Swift stdlib (`lib/swift{,_static}`), and WASI sysroot like follows: ```console swift run swift-sdk-generator make-wasm-sdk \ --target wasm32-unknown-wasi \ --host-swift-package-path Downloads/swift-DEVELOPMENT-SNAPSHOT-2024-01-12-a \ --target-swift-package-path Downloads/swift-wasm-DEVELOPMENT-SNAPSHOT-2024-01-12-a \ --wasi-sysroot Downloads/wasi-sysroot ``` * Use `GeneratorOptions`'s common fields * rsync `lib/clang` into the SDK bundle The directory was missed to be taken from target toolchain but it wasn't revealed on local testing since the host toolchain unintentionally contained the directory. --------- Co-authored-by: Max Desiatov <[email protected]>
- Loading branch information
1 parent
cf9e76a
commit de216d1
Showing
3 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import AsyncHTTPClient | ||
import GeneratorEngine | ||
import struct SystemPackage.FilePath | ||
|
||
public struct WebAssemblyRecipe: SwiftSDKRecipe { | ||
let hostSwiftPackagePath: FilePath | ||
let targetSwiftPackagePath: FilePath | ||
let wasiSysroot: FilePath | ||
let swiftVersion: String | ||
|
||
public init( | ||
hostSwiftPackagePath: FilePath, | ||
targetSwiftPackagePath: FilePath, | ||
wasiSysroot: FilePath, | ||
swiftVersion: String | ||
) { | ||
self.hostSwiftPackagePath = hostSwiftPackagePath | ||
self.targetSwiftPackagePath = targetSwiftPackagePath | ||
self.wasiSysroot = wasiSysroot | ||
self.swiftVersion = swiftVersion | ||
} | ||
|
||
public var defaultArtifactID: String { | ||
"\(self.swiftVersion)_wasm" | ||
} | ||
|
||
public func applyPlatformOptions(toolset: inout Toolset) { | ||
// We only support static linking for WebAssembly for now, so make it the default. | ||
toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-static-stdlib"]) | ||
} | ||
|
||
public func makeSwiftSDK( | ||
generator: SwiftSDKGenerator, | ||
engine: Engine, | ||
httpClient: HTTPClient | ||
) async throws -> SwiftSDKProduct { | ||
let pathsConfiguration = generator.pathsConfiguration | ||
logGenerationStep("Copying Swift binaries for the host triple...") | ||
try await generator.rsync(from: self.hostSwiftPackagePath.appending("usr"), to: pathsConfiguration.toolchainDirPath) | ||
try await self.copyTargetSwift(from: self.targetSwiftPackagePath.appending("usr/lib"), generator: generator) | ||
|
||
let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract") | ||
|
||
// WebAssembly object file requires `swift-autolink-extract` | ||
if await !generator.doesFileExist(at: autolinkExtractPath) { | ||
logGenerationStep("Fixing `swift-autolink-extract` symlink...") | ||
try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") | ||
} | ||
|
||
// Copy the WASI sysroot into the SDK bundle. | ||
let sdkDirPath = pathsConfiguration.swiftSDKRootPath.appending("WASI.sdk") | ||
try await generator.rsyncContents(from: self.wasiSysroot, to: sdkDirPath) | ||
|
||
return SwiftSDKProduct(sdkDirPath: sdkDirPath) | ||
} | ||
|
||
func copyTargetSwift(from distributionPath: FilePath, generator: SwiftSDKGenerator) async throws { | ||
let pathsConfiguration = generator.pathsConfiguration | ||
logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") | ||
for (pathWithinPackage, pathWithinSwiftSDK) in [ | ||
("clang", pathsConfiguration.toolchainDirPath.appending("usr/lib")), | ||
("swift/wasi", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift")), | ||
("swift_static/wasi", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), | ||
("swift_static/shims", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), | ||
("swift_static/CoreFoundation", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), | ||
] { | ||
try await generator.rsync(from: distributionPath.appending(pathWithinPackage), to: pathWithinSwiftSDK) | ||
} | ||
} | ||
} |