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)