|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022-2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import AsyncHTTPClient |
| 14 | +import GeneratorEngine |
| 15 | +import struct SystemPackage.FilePath |
| 16 | + |
| 17 | +public struct WebAssemblyRecipe: SwiftSDKRecipe { |
| 18 | + let hostSwiftPackagePath: FilePath |
| 19 | + let targetSwiftPackagePath: FilePath |
| 20 | + let wasiSysroot: FilePath |
| 21 | + let swiftVersion: String |
| 22 | + |
| 23 | + public init( |
| 24 | + hostSwiftPackagePath: FilePath, |
| 25 | + targetSwiftPackagePath: FilePath, |
| 26 | + wasiSysroot: FilePath, |
| 27 | + swiftVersion: String |
| 28 | + ) { |
| 29 | + self.hostSwiftPackagePath = hostSwiftPackagePath |
| 30 | + self.targetSwiftPackagePath = targetSwiftPackagePath |
| 31 | + self.wasiSysroot = wasiSysroot |
| 32 | + self.swiftVersion = swiftVersion |
| 33 | + } |
| 34 | + |
| 35 | + public var defaultArtifactID: String { |
| 36 | + "\(self.swiftVersion)_wasm" |
| 37 | + } |
| 38 | + |
| 39 | + public func applyPlatformOptions(toolset: inout Toolset) { |
| 40 | + // We only support static linking for WebAssembly for now, so make it the default. |
| 41 | + toolset.swiftCompiler = Toolset.ToolProperties(extraCLIOptions: ["-static-stdlib"]) |
| 42 | + } |
| 43 | + |
| 44 | + public func makeSwiftSDK( |
| 45 | + generator: SwiftSDKGenerator, |
| 46 | + engine: Engine, |
| 47 | + httpClient: HTTPClient |
| 48 | + ) async throws -> SwiftSDKProduct { |
| 49 | + let pathsConfiguration = generator.pathsConfiguration |
| 50 | + logGenerationStep("Copying Swift binaries for the host triple...") |
| 51 | + try await generator.rsync(from: self.hostSwiftPackagePath.appending("usr"), to: pathsConfiguration.toolchainDirPath) |
| 52 | + try await self.copyTargetSwift(from: self.targetSwiftPackagePath.appending("usr/lib"), generator: generator) |
| 53 | + |
| 54 | + let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract") |
| 55 | + |
| 56 | + // WebAssembly object file requires `swift-autolink-extract` |
| 57 | + if await !generator.doesFileExist(at: autolinkExtractPath) { |
| 58 | + logGenerationStep("Fixing `swift-autolink-extract` symlink...") |
| 59 | + try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") |
| 60 | + } |
| 61 | + |
| 62 | + // Copy the WASI sysroot into the SDK bundle. |
| 63 | + let sdkDirPath = pathsConfiguration.swiftSDKRootPath.appending("WASI.sdk") |
| 64 | + try await generator.rsyncContents(from: self.wasiSysroot, to: sdkDirPath) |
| 65 | + |
| 66 | + return SwiftSDKProduct(sdkDirPath: sdkDirPath) |
| 67 | + } |
| 68 | + |
| 69 | + func copyTargetSwift(from distributionPath: FilePath, generator: SwiftSDKGenerator) async throws { |
| 70 | + let pathsConfiguration = generator.pathsConfiguration |
| 71 | + logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") |
| 72 | + for (pathWithinPackage, pathWithinSwiftSDK) in [ |
| 73 | + ("clang", pathsConfiguration.toolchainDirPath.appending("usr/lib")), |
| 74 | + ("swift/wasi", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift")), |
| 75 | + ("swift_static/wasi", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), |
| 76 | + ("swift_static/shims", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), |
| 77 | + ("swift_static/CoreFoundation", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static")), |
| 78 | + ] { |
| 79 | + try await generator.rsync(from: distributionPath.appending(pathWithinPackage), to: pathWithinSwiftSDK) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments