Skip to content

Commit d1a1b22

Browse files
committed
Pass a path to wasm-ld to wasm-component-ld
This commit adds an explicit argument that's passed to `wasm-component-ld` containing the location of `wasm-ld` itself. This enables `wasm-component-ld` to avoid hunting around looking for it and instead use the install that's paired with Clang itself. Additionally this reinterprets the `-fuse-ld=lld` argument to explicitly requesting the `wasm-ld` linker flavor, even on `wasm32-wasip2` targets.
1 parent 99ca952 commit d1a1b22

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

clang/lib/Driver/ToolChains/WebAssembly.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
4444
llvm::sys::fs::can_execute(UseLinker))
4545
return std::string(UseLinker);
4646

47-
// Accept 'lld', and 'ld' as aliases for the default linker
48-
if (UseLinker != "lld" && UseLinker != "ld")
47+
// Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
48+
// linker. Note that for `wasm32-wasip2` this overrides the default linker
49+
// of `wasm-component-ld`.
50+
if (UseLinker == "lld") {
51+
return ToolChain.GetProgramPath("wasm-ld");
52+
}
53+
54+
// Allow 'ld' as an alias for the default linker
55+
if (UseLinker != "ld")
4956
ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
5057
<< A->getAsString(Args);
5158
}
@@ -73,6 +80,15 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
7380
if (Args.hasArg(options::OPT_s))
7481
CmdArgs.push_back("--strip-all");
7582

83+
// On `wasip2` the default linker is `wasm-component-ld` which wraps the
84+
// execution of `wasm-ld`. Find `wasm-ld` and pass it as an argument of where
85+
// to find it to avoid it needing to hunt and rediscover or search `PATH` for
86+
// where it is.
87+
if (llvm::sys::path::stem(Linker).ends_with_insensitive("wasm-component-ld")) {
88+
CmdArgs.push_back("--wasm-ld-path");
89+
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetProgramPath("wasm-ld")));
90+
}
91+
7692
Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
7793

7894
ToolChain.AddFilePathLibArgs(Args, CmdArgs);

clang/test/Driver/wasm-toolchain.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,19 @@
205205
// RUN: | FileCheck -check-prefix=LINK_WASIP2 %s
206206
// LINK_WASIP2: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
207207
// LINK_WASIP2: wasm-component-ld{{.*}}" "-L/foo/lib/wasm32-wasip2" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
208+
209+
// Test that on `wasm32-wasip2` the `wasm-component-ld` programs is told where
210+
// to find `wasm-ld` by default.
211+
212+
// RUN: %clang -### -O2 --target=wasm32-wasip2 %s --sysroot /foo 2>&1 \
213+
// RUN: | FileCheck -check-prefix=LINK_WASIP2_FIND_WASMLD %s
214+
// LINK_WASIP2_FIND_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
215+
// LINK_WASIP2_FIND_WASMLD: wasm-component-ld{{.*}}" {{.*}} "--wasm-ld-path" "{{.*}}wasm-ld{{.*}}" {{.*}} "[[temp]]" {{.*}}
216+
217+
// If `wasm32-wasip2` is configured with `wasm-ld` as a linker then don't pass
218+
// the `--wasm-ld-path` flag.
219+
220+
// RUN: %clang -### -O2 --target=wasm32-wasip2 -fuse-ld=lld %s --sysroot /foo 2>&1 \
221+
// RUN: | FileCheck -check-prefix=LINK_WASIP2_USE_WASMLD %s
222+
// LINK_WASIP2_USE_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
223+
// LINK_WASIP2_USE_WASMLD: wasm-ld{{.*}}" "-m" "wasm32" {{.*}} "[[temp]]" {{.*}}

0 commit comments

Comments
 (0)