Skip to content

Commit

Permalink
Pass a path to wasm-ld to wasm-component-ld
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Mar 18, 2024
1 parent 99ca952 commit 7284fd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions clang/lib/Driver/ToolChains/WebAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
llvm::sys::fs::can_execute(UseLinker))
return std::string(UseLinker);

// Accept 'lld', and 'ld' as aliases for the default linker
if (UseLinker != "lld" && UseLinker != "ld")
// Interpret 'lld' as explicitly requesting `wasm-ld`, so look for that
// linker. Note that for `wasm32-wasip2` this overrides the default linker
// of `wasm-component-ld`.
if (UseLinker == "lld") {
return ToolChain.GetProgramPath("wasm-ld");
}

// Allow 'ld' as an alias for the default linker
if (UseLinker != "ld")
ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
<< A->getAsString(Args);
}
Expand Down Expand Up @@ -73,6 +80,16 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_s))
CmdArgs.push_back("--strip-all");

// On `wasip2` the default linker is `wasm-component-ld` which wraps the
// execution of `wasm-ld`. Find `wasm-ld` and pass it as an argument of where
// to find it to avoid it needing to hunt and rediscover or search `PATH` for
// where it is.
if (llvm::sys::path::stem(Linker).ends_with_insensitive(
"wasm-component-ld")) {
CmdArgs.push_back("--wasm-ld-path");
CmdArgs.push_back(Args.MakeArgString(ToolChain.GetProgramPath("wasm-ld")));
}

Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});

ToolChain.AddFilePathLibArgs(Args, CmdArgs);
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/wasm-toolchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,19 @@
// RUN: | FileCheck -check-prefix=LINK_WASIP2 %s
// LINK_WASIP2: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
// LINK_WASIP2: wasm-component-ld{{.*}}" "-L/foo/lib/wasm32-wasip2" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"

// Test that on `wasm32-wasip2` the `wasm-component-ld` programs is told where
// to find `wasm-ld` by default.

// RUN: %clang -### -O2 --target=wasm32-wasip2 %s --sysroot /foo 2>&1 \
// RUN: | FileCheck -check-prefix=LINK_WASIP2_FIND_WASMLD %s
// LINK_WASIP2_FIND_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
// LINK_WASIP2_FIND_WASMLD: wasm-component-ld{{.*}}" {{.*}} "--wasm-ld-path" "{{.*}}wasm-ld{{.*}}" {{.*}} "[[temp]]" {{.*}}

// If `wasm32-wasip2` is configured with `wasm-ld` as a linker then don't pass
// the `--wasm-ld-path` flag.

// RUN: %clang -### -O2 --target=wasm32-wasip2 -fuse-ld=lld %s --sysroot /foo 2>&1 \
// RUN: | FileCheck -check-prefix=LINK_WASIP2_USE_WASMLD %s
// LINK_WASIP2_USE_WASMLD: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
// LINK_WASIP2_USE_WASMLD: wasm-ld{{.*}}" "-m" "wasm32" {{.*}} "[[temp]]" {{.*}}

0 comments on commit 7284fd4

Please sign in to comment.