@@ -17,7 +17,9 @@ import struct SystemPackage.FilePath
17
17
18
18
package struct WebAssemblyRecipe : SwiftSDKRecipe {
19
19
let hostSwiftPackage : HostToolchainPackage ?
20
- let targetSwiftPackagePath : FilePath
20
+
21
+ /// Optional to allow creating WebAssembly Swift SDKs that don't include Swift support and therefore can only target C/C++.
22
+ let targetSwiftPackagePath : FilePath ?
21
23
let wasiSysroot : FilePath
22
24
let swiftVersion : String
23
25
package let logger : Logger
@@ -34,7 +36,7 @@ package struct WebAssemblyRecipe: SwiftSDKRecipe {
34
36
35
37
package init (
36
38
hostSwiftPackage: HostToolchainPackage ? ,
37
- targetSwiftPackagePath: FilePath ,
39
+ targetSwiftPackagePath: FilePath ? ,
38
40
wasiSysroot: FilePath ,
39
41
swiftVersion: String ,
40
42
logger: Logger
@@ -47,7 +49,10 @@ package struct WebAssemblyRecipe: SwiftSDKRecipe {
47
49
}
48
50
49
51
package var defaultArtifactID : String {
50
- " \( self . swiftVersion) _wasm "
52
+ if hostSwiftPackage == nil && targetSwiftPackagePath == nil {
53
+ return " wasm "
54
+ }
55
+ return " \( self . swiftVersion) _wasm "
51
56
}
52
57
53
58
package let shouldSupportEmbeddedSwift = true
@@ -134,74 +139,74 @@ package struct WebAssemblyRecipe: SwiftSDKRecipe {
134
139
httpClient: some HTTPClientProtocol
135
140
) async throws -> SwiftSDKProduct {
136
141
let pathsConfiguration = generator. pathsConfiguration
137
- let targetSwiftLibPath = self . targetSwiftPackagePath. appending ( " usr/lib " )
138
-
139
- logger. info ( " Copying Swift binaries for the host triple... " )
140
142
var hostTriples : [ Triple ] ? = nil
141
- if let hostSwiftPackage {
142
- hostTriples = hostSwiftPackage. triples
143
- try await generator. rsync (
144
- from: hostSwiftPackage. path. appending ( " usr " ) ,
145
- to: pathsConfiguration. toolchainDirPath
146
- )
143
+ if let targetSwiftLibPath = self . targetSwiftPackagePath? . appending ( " usr/lib " ) {
144
+ logger. info ( " Copying Swift binaries for the host triple... " )
145
+ if let hostSwiftPackage {
146
+ hostTriples = hostSwiftPackage. triples
147
+ try await generator. rsync (
148
+ from: hostSwiftPackage. path. appending ( " usr " ) ,
149
+ to: pathsConfiguration. toolchainDirPath
150
+ )
151
+
152
+ logger. info ( " Removing unused toolchain components... " )
153
+ let liblldbNames : [ String ] = try await {
154
+ let libDirPath = pathsConfiguration. toolchainDirPath. appending ( " usr/lib " )
155
+ guard await generator. doesFileExist ( at: libDirPath) else {
156
+ return [ ]
157
+ }
158
+ return try await generator. contentsOfDirectory ( at: libDirPath) . filter { dirEntry in
159
+ // liblldb is version suffixed: liblldb.so.17.0.0
160
+ dirEntry. hasPrefix ( " liblldb " )
161
+ }
162
+ } ( )
163
+ try await generator. removeToolchainComponents (
164
+ pathsConfiguration. toolchainDirPath,
165
+ platforms: unusedTargetPlatforms,
166
+ libraries: unusedHostLibraries + liblldbNames,
167
+ binaries: unusedHostBinaries + [ " lldb " , " lldb-argdumper " , " lldb-server " ]
168
+ )
169
+ // Merge target Swift package with the host package.
170
+ try await self . mergeTargetSwift ( from: targetSwiftLibPath, generator: generator)
171
+ } else {
172
+ // Simply copy the target Swift package into the Swift SDK bundle when building host-agnostic Swift SDK.
173
+ try await generator. createDirectoryIfNeeded (
174
+ at: pathsConfiguration. toolchainDirPath. appending ( " usr " )
175
+ )
176
+ try await generator. copy (
177
+ from: targetSwiftLibPath,
178
+ to: pathsConfiguration. toolchainDirPath. appending ( " usr/lib " )
179
+ )
180
+ }
147
181
148
- logger. info ( " Removing unused toolchain components... " )
149
- let liblldbNames : [ String ] = try await {
150
- let libDirPath = pathsConfiguration. toolchainDirPath. appending ( " usr/lib " )
151
- guard await generator. doesFileExist ( at: libDirPath) else {
152
- return [ ]
153
- }
154
- return try await generator. contentsOfDirectory ( at: libDirPath) . filter { dirEntry in
155
- // liblldb is version suffixed: liblldb.so.17.0.0
156
- dirEntry. hasPrefix ( " liblldb " )
157
- }
158
- } ( )
159
- try await generator. removeToolchainComponents (
160
- pathsConfiguration. toolchainDirPath,
161
- platforms: unusedTargetPlatforms,
162
- libraries: unusedHostLibraries + liblldbNames,
163
- binaries: unusedHostBinaries + [ " lldb " , " lldb-argdumper " , " lldb-server " ]
164
- )
165
- // Merge target Swift package with the host package.
166
- try await self . mergeTargetSwift ( from: targetSwiftLibPath, generator: generator)
167
- } else {
168
- // Simply copy the target Swift package into the SDK bundle when building host-agnostic SDK.
169
- try await generator. createDirectoryIfNeeded (
170
- at: pathsConfiguration. toolchainDirPath. appending ( " usr " )
182
+ let autolinkExtractPath = pathsConfiguration. toolchainBinDirPath. appending (
183
+ " swift-autolink-extract "
171
184
)
172
- try await generator. copy (
173
- from: targetSwiftLibPath,
174
- to: pathsConfiguration. toolchainDirPath. appending ( " usr/lib " )
175
- )
176
- }
177
185
178
- let autolinkExtractPath = pathsConfiguration. toolchainBinDirPath. appending (
179
- " swift-autolink-extract "
180
- )
181
-
182
- // WebAssembly object file requires `swift-autolink-extract`
183
- if await !generator. doesFileExist ( at: autolinkExtractPath) ,
184
- await generator. doesFileExist (
185
- at: generator. pathsConfiguration. toolchainBinDirPath. appending ( " swift " )
186
- )
187
- {
188
- logger. info ( " Fixing `swift-autolink-extract` symlink... " )
189
- try await generator. createSymlink ( at: autolinkExtractPath, pointingTo: " swift " )
190
- }
186
+ // WebAssembly object file requires `swift-autolink-extract`
187
+ if await !generator. doesFileExist ( at: autolinkExtractPath) ,
188
+ await generator. doesFileExist (
189
+ at: generator. pathsConfiguration. toolchainBinDirPath. appending ( " swift " )
190
+ )
191
+ {
192
+ logger. info ( " Fixing `swift-autolink-extract` symlink... " )
193
+ try await generator. createSymlink ( at: autolinkExtractPath, pointingTo: " swift " )
194
+ }
191
195
192
- // TODO: Remove this once we drop support for Swift 6.2
193
- // Embedded Swift looks up clang compiler-rt in a different path.
194
- let embeddedCompilerRTPath = pathsConfiguration. toolchainDirPath. appending (
195
- " usr/lib/swift/clang/lib/wasip1 "
196
- )
197
- if await !generator. doesFileExist ( at: embeddedCompilerRTPath) {
198
- try await generator. createSymlink (
199
- at: embeddedCompilerRTPath,
200
- pointingTo: " ../../../swift_static/clang/lib/wasi "
196
+ // TODO: Remove this once we drop support for Swift 6.2
197
+ // Embedded Swift looks up clang compiler-rt in a different path.
198
+ let embeddedCompilerRTPath = pathsConfiguration. toolchainDirPath. appending (
199
+ " usr/lib/swift/clang/lib/wasip1 "
201
200
)
201
+ if await !generator. doesFileExist ( at: embeddedCompilerRTPath) {
202
+ try await generator. createSymlink (
203
+ at: embeddedCompilerRTPath,
204
+ pointingTo: " ../../../swift_static/clang/lib/wasi "
205
+ )
206
+ }
202
207
}
203
208
204
- // Copy the WASI sysroot into the SDK bundle.
209
+ // Copy the WASI sysroot into the Swift SDK bundle.
205
210
let sdkDirPath = pathsConfiguration. swiftSDKRootPath. appending ( " WASI.sdk " )
206
211
try await generator. rsyncContents ( from: self . wasiSysroot, to: sdkDirPath)
207
212
0 commit comments