Skip to content

Commit 6715b84

Browse files
authored
Fix incorrect link job options for embedded WASI (#1841)
`WebAssemblyToolchain` didn't check for `parsedOptions.isEmbeddedEnabled` when forming `swiftrt.o` and `static-executable-args.lnk` options, and neither of these files exist when building with Embedded Swift for WASI.
1 parent 6ac8b54 commit 6715b84

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extension WebAssemblyToolchain {
9090
isShared: false
9191
)
9292

93-
if !parsedOptions.hasArgument(.nostartfiles) {
93+
if !parsedOptions.hasArgument(.nostartfiles) && !parsedOptions.isEmbeddedEnabled {
9494
let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
9595
.appending(
9696
components: targetTriple.platformName() ?? "",
@@ -125,15 +125,17 @@ extension WebAssemblyToolchain {
125125
commandLine.appendPath(path)
126126
}
127127

128-
// Link the standard library and dependencies.
129-
let linkFilePath: VirtualPath =
130-
VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
131-
.appending(components: targetTriple.platformName() ?? "",
132-
"static-executable-args.lnk")
133-
guard try fileSystem.exists(linkFilePath) else {
134-
throw Error.missingExternalDependency(linkFilePath.name)
128+
if !parsedOptions.isEmbeddedEnabled {
129+
// Link the standard library and dependencies.
130+
let linkFilePath: VirtualPath =
131+
VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
132+
.appending(components: targetTriple.platformName() ?? "",
133+
"static-executable-args.lnk")
134+
guard try fileSystem.exists(linkFilePath) else {
135+
throw Error.missingExternalDependency(linkFilePath.name)
136+
}
137+
commandLine.append(.responseFilePath(linkFilePath))
135138
}
136-
commandLine.append(.responseFilePath(linkFilePath))
137139

138140
// Pass down an optimization level
139141
if let optArg = mapOptimizationLevelToClangArg(from: &parsedOptions) {

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6861,6 +6861,23 @@ final class SwiftDriverTests: XCTestCase {
68616861
XCTAssertFalse(linkJob.commandLine.joinedUnresolvedArguments.contains("swiftrt.o"))
68626862
}
68636863

6864+
// Embedded WASI link job
6865+
do {
6866+
for tripleEnv in ["wasi", "wasi-wasm", "wasip1", "wasip1-wasm", "wasip1-threads"] {
6867+
var driver = try Driver(
6868+
args: ["swiftc", "-target", "wasm32-unknown-\(tripleEnv)", "test.o", "-enable-experimental-feature", "Embedded", "-wmo", "-o", "a.wasm"],
6869+
env: env
6870+
)
6871+
let plannedJobs = try driver.planBuild()
6872+
XCTAssertEqual(plannedJobs.count, 1)
6873+
let linkJob = plannedJobs[0]
6874+
XCTAssertFalse(linkJob.commandLine.contains(.flag("-force_load")))
6875+
XCTAssertFalse(linkJob.commandLine.contains(.flag("-rpath")))
6876+
XCTAssertFalse(linkJob.commandLine.contains(.flag("-lswiftCore")))
6877+
XCTAssertFalse(linkJob.commandLine.joinedUnresolvedArguments.contains("swiftrt.o"))
6878+
}
6879+
}
6880+
68646881
do {
68656882
let diags = DiagnosticsEngine()
68666883
var driver = try Driver(args: ["swiftc", "-target", "arm64-apple-macosx10.13", "test.swift", "-enable-experimental-feature", "Embedded", "-parse-as-library", "-wmo", "-o", "a.out", "-module-name", "main", "-enable-library-evolution"], diagnosticsEngine: diags)
@@ -6874,7 +6891,7 @@ final class SwiftDriverTests: XCTestCase {
68746891
XCTAssertEqual(diags.diagnostics.first!.message.text, Diagnostic.Message.error_need_wmo_embedded.text)
68756892
} catch _ { }
68766893
do {
6877-
var environment = ProcessEnv.vars
6894+
var environment = ProcessEnv.block
68786895
environment["SDKROOT"] = nil
68796896

68806897
// Indexing embedded Swift code should not require WMO

0 commit comments

Comments
 (0)