From edb7463125a0f8febe716c3717c416e33e10b1de Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 26 Feb 2024 16:27:10 +0000 Subject: [PATCH 1/2] Passthrough `-tools-directory` as `-B` to clang linker for Wasm target This change makes the clang linker driver for Wasm targets respect the `-tools-directory` option. This is necessary to use linker tools from Swift SDK toolset. --- .../WebAssemblyToolchain+LinkerSupport.swift | 4 ++++ Tests/SwiftDriverTests/SwiftDriverTests.swift | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift index 397ee24e1..6214fee4d 100644 --- a/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift +++ b/Sources/SwiftDriver/Jobs/WebAssemblyToolchain+LinkerSupport.swift @@ -73,6 +73,10 @@ extension WebAssemblyToolchain { if let tool = lookupExecutablePath(filename: "clang", searchPaths: [toolsDir]) { clangPath = tool } + + // Look for binutils in the toolchain folder. + commandLine.appendFlag("-B") + commandLine.appendPath(toolsDir) } guard !parsedOptions.hasArgument(.noStaticStdlib, .noStaticExecutable) else { diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 72856f91c..68dd2321c 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -4367,6 +4367,26 @@ final class SwiftDriverTests: XCTestCase { XCTAssertEqual(frontendJobs.count, 2) XCTAssertEqual(frontendJobs[1].kind, .link) XCTAssertEqual(frontendJobs[1].tool.absolutePath!.pathString, ld.pathString) + + // WebAssembly toolchain + do { + try withTemporaryDirectory { resourceDir in + try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) { + $0.send("garbage") + } + var driver = try Driver(args: ["swiftc", + "-target", "wasm32-unknown-wasi", + "-resource-dir", resourceDir.pathString, + "-tools-directory", tmpDir.pathString, + "foo.swift"], + env: env) + let frontendJobs = try driver.planBuild().removingAutolinkExtractJobs() + XCTAssertEqual(frontendJobs.count, 2) + XCTAssertTrue(frontendJobs[1].commandLine.contains(subsequence: [ + .flag("-B"), .path(.absolute(tmpDir)) + ])) + } + } } } From d65f2647b59f7568067ac2957ddee8e7e5ff85a1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 27 Feb 2024 01:42:40 +0900 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Max Desiatov --- Tests/SwiftDriverTests/SwiftDriverTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 68dd2321c..d60058862 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -4368,7 +4368,7 @@ final class SwiftDriverTests: XCTestCase { XCTAssertEqual(frontendJobs[1].kind, .link) XCTAssertEqual(frontendJobs[1].tool.absolutePath!.pathString, ld.pathString) - // WebAssembly toolchain + // WASI toolchain do { try withTemporaryDirectory { resourceDir in try localFileSystem.writeFileContents(resourceDir.appending(components: "wasi", "static-executable-args.lnk")) {