From 25dbee26b11c6adf1ca416590e992ff8414cdaa4 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 26 Dec 2025 16:24:34 +0900 Subject: [PATCH] Wasm: Use `-sysroot` value as the clang linker sysroot if provided For other Unix-like platforms, the driver already respects `-sysroot` over `-sdk` as the native sysroot (not Swift "SDK"). See the following PRs for reference: * https://github.com/swiftlang/swift-driver/pull/1811 * https://github.com/swiftlang/swift/pull/72352 For WebAssembly, this change makes the driver behave consistently with other platforms. This consistency is important for supporting swiftc as a swift-build's underlying linker driver for Wasm targets, as UnixLd.xcspec now uses `-sysroot` by default: https://github.com/swiftlang/swift-build/blob/02e9f6778b621375b5a5a2fe40f9a4b55bb44ba0/Sources/SWBGenericUnixPlatform/Specs/UnixLd.xcspec#L61-L68 --- .../WebAssemblyToolchain+LinkerSupport.swift | 6 +++++- Tests/SwiftDriverTests/SwiftDriverTests.swift | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift index 0e4e31e02..d9a45cc97 100644 --- a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift @@ -114,7 +114,11 @@ extension WebAssemblyToolchain { } commandLine.append(contentsOf: inputFiles) - if let path = targetInfo.sdkPath?.path { + // Prefer -sysroot as the native sysroot path if provided. + if let sysroot = parsedOptions.getLastArgument(.sysroot)?.asSingle { + commandLine.appendFlag("--sysroot") + try commandLine.appendPath(VirtualPath(path: sysroot)) + } else if let path = targetInfo.sdkPath?.path { commandLine.appendFlag("--sysroot") commandLine.appendPath(VirtualPath.lookup(path)) } diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 457ecc560..d4b555ae0 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -2790,6 +2790,23 @@ final class SwiftDriverTests: XCTestCase { } } + do { + // -sysroot is preferred over -sdk as the sysroot passed to the clang linker + try withTemporaryDirectory { path in + try localFileSystem.writeFileContents(path.appending(components: "wasi", "static-executable-args.lnk")) { + $0.send("garbage") + } + var driver = try Driver(args: commonArgs + ["-emit-executable", "-Ounchecked", + "-target", "wasm32-unknown-wasi", + "-resource-dir", path.pathString, + "-sysroot", "/sysroot/path", + "-sdk", "/sdk/path"], env: env) + let plannedJobs = try driver.planBuild() + let cmd = plannedJobs.last!.commandLine + XCTAssertTrue(cmd.contains(subsequence: ["--sysroot", .path(.absolute(try .init(validating: "/sysroot/path")))])) + } + } + do { // Linker flags with and without space var driver = try Driver(args: commonArgs + ["-lsomelib","-l","otherlib"], env: env)