[clang][WebAssembly] WASI preview targets can't find compiler-rt builtins in flat library layout
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
`ToolChain::getOSLibName()` returns the raw OS component of the target triple for WASI preview targets (e.g. "`wasip1`" for `wasm32-wasip1`) rather than the canonical "`wasi`" directory name that compiler-rt uses when built with `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF`.
This causes clang to search for `[...]/lib/wasip1/libclang_rt.builtins-wasm32.a` instead of `[...]/lib/wasi/libclang_rt.builtins-wasm32.a`.
This bug was originally reported in [Ubuntu](https://bugs.launchpad.net/ubuntu/+source/llvm-toolchain-22/+bug/2152147) but is reproducible in both Debian and a toolchain built from source.
## Steps to reproduce
The following steps were done in a fresh Ubuntu 26.04 LTS installation.
Start by building a toolchain capable of building for WebAssembly targets:
```
# apt update
# apt install -y build-essential cmake ninja-build python3 git
$ git clone --depth 1 -b main https://github.com/llvm/llvm-project.git
$ cmake -G Ninja llvm-project/llvm \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/opt/llvm \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_TARGETS_TO_BUILD="WebAssembly" \
-DLLVM_INCLUDE_TESTS=OFF
$ ninja install
$ mkdir build-rt && cd build-rt
```
Next, build the compiler-rt builtins for wasm. Setting `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF` in the compiler-rt configuration enables the directory layout which triggers the bug:
```
$ cmake -G Ninja ../llvm-project/compiler-rt \
-DCMAKE_BUILD_TYPE=Release \
-DCOMPILER_RT_INSTALL_PATH=$(/opt/llvm/bin/clang --print-resource-dir) \
-DCMAKE_C_COMPILER=/opt/llvm/bin/clang \
-DCMAKE_C_COMPILER_TARGET=wasm32-wasip1 \
-DCMAKE_CXX_COMPILER=/opt/llvm/bin/clang++ \
-DCMAKE_CXX_COMPILER_TARGET=wasm32-wasip1 \
-DCMAKE_SYSTEM_NAME=WASI \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCOMPILER_RT_BAREMETAL_BUILD=ON \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
-DCOMPILER_RT_BUILD_CRT=OFF \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF
$ ninja install
$ touch test.c
```
Finally, attempt to build a dummy program:
```
$ /opt/llvm/bin/clang --target=wasm32-wasip1 test.c
$ /opt/llvm/bin/clang --target=wasm32-wasip2 -fuse-ld=/opt/llvm/bin/wasm-ld test.c
$ /opt/llvm/bin/clang --target=wasm32-wasip3 -fuse-ld=/opt/llvm/bin/wasm-ld test.c
```
The output of the final three commands should contain the following error:
```
wasm-ld: error: cannot open /opt/llvm/lib/clang/23/lib/wasm32-unknown-wasip{1,2,3}/libclang_rt.builtins.a: No such file or directory
```
In reality, the library was installed to `/opt/llvm/lib/clang/23/lib/wasi/libclang_rt.builtins-wasm32.a`, but the toolchain was unable to find it.
Contributor guide
Assessment
This issue has not been assessed yet.