Skip to content

Commit 185a84a

Browse files
committed
Allow linking _Concurrency for WASI with Embedded Swift (#1863)
Currently, when building packages for WASI with Embedded Swift, libraries such as `libswift_Concurrency.a` and `libswift_ConcurrencyDefaultExecutor.a` are not discoverable and require passing `-Xlinker <swift-sdk-path>/usr/lib/swift/embedded/wasm32-unknown-wasip1` option manually. This path can be inferred by the driver, which simplifies build invocations for users significantly, while the rest of linkage options (`-lswift_Concurrency` etc) can be specified in toolset files.
1 parent e9ca0c6 commit 185a84a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,17 @@ extension WebAssemblyToolchain {
125125
commandLine.appendPath(path)
126126
}
127127

128-
if !parsedOptions.isEmbeddedEnabled {
128+
let runtimeResourcePath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
129+
if parsedOptions.isEmbeddedEnabled {
130+
// Allow linking certain standard library modules (`_Concurrency` etc)
131+
let embeddedLibrariesPath: VirtualPath = runtimeResourcePath.appending(
132+
components: "embedded", targetTriple.triple
133+
)
134+
commandLine.append(.flag("-Xlinker"))
135+
commandLine.append(.joinedOptionAndPath("-L", embeddedLibrariesPath))
136+
} else {
129137
// Link the standard library and dependencies.
130-
let linkFilePath: VirtualPath =
131-
VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
138+
let linkFilePath: VirtualPath = runtimeResourcePath
132139
.appending(components: targetTriple.platformName() ?? "",
133140
"static-executable-args.lnk")
134141
guard try fileSystem.exists(linkFilePath) else {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6890,16 +6890,25 @@ final class SwiftDriverTests: XCTestCase {
68906890
do {
68916891
for tripleEnv in ["wasi", "wasi-wasm", "wasip1", "wasip1-wasm", "wasip1-threads"] {
68926892
var driver = try Driver(
6893-
args: ["swiftc", "-target", "wasm32-unknown-\(tripleEnv)", "test.o", "-enable-experimental-feature", "Embedded", "-wmo", "-o", "a.wasm"],
6893+
args: [
6894+
"swiftc", "-target", "wasm32-unknown-\(tripleEnv)",
6895+
"-resource-dir", "/usr/lib/swift",
6896+
"-enable-experimental-feature", "Embedded", "-wmo",
6897+
"test.o", "-o", "a.wasm"
6898+
],
68946899
env: env
68956900
)
68966901
let plannedJobs = try driver.planBuild()
68976902
XCTAssertEqual(plannedJobs.count, 1)
68986903
let linkJob = plannedJobs[0]
6904+
print(linkJob.commandLine)
68996905
XCTAssertFalse(linkJob.commandLine.contains(.flag("-force_load")))
69006906
XCTAssertFalse(linkJob.commandLine.contains(.flag("-rpath")))
69016907
XCTAssertFalse(linkJob.commandLine.contains(.flag("-lswiftCore")))
69026908
XCTAssertFalse(linkJob.commandLine.joinedUnresolvedArguments.contains("swiftrt.o"))
6909+
XCTAssertTrue(linkJob.commandLine.contains(
6910+
.joinedOptionAndPath("-L", try .init(path: "/usr/lib/swift/embedded/wasm32-unknown-\(tripleEnv)"))
6911+
))
69036912
}
69046913
}
69056914

0 commit comments

Comments
 (0)