Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca07aa7

Browse files
committedMar 3, 2025
Auto merge of #137457 - JayAndJef:issue-132802-fix, r=<try>
fix for issue 132802: x86 code in `wasm32-unknown-unknown` binaries This is a direct fix for issue [132802](#132802). Followed the outline as follows: > * give a hard error in bootstrap when using gcc to compile for wasm > * change our CI to use clang instead of gcc > * add a test that compiling a sample program for wasm32-unknown doesn't give any linker warnings The `test-various` ci job was also changed. try-job: test-various try-job: dist-various-2
2 parents 81d8edc + 63db26f commit ca07aa7

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed
 

‎src/bootstrap/src/core/sanity.rs

+24
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,17 @@ than building it.
310310
.entry(*target)
311311
.or_insert_with(|| Target::from_triple(&target.triple));
312312

313+
// compiler-rt c fallbacks for wasm cannot be built with gcc
314+
if target.contains("wasm") // bare metal targets without wasi sdk
315+
&& (build.config.optimized_compiler_builtins(*target)
316+
|| build.config.rust_std_features.contains("compiler-builtins-c"))
317+
{
318+
let is_clang = is_clang_compiler(build.cc(*target), build);
319+
if !is_clang {
320+
panic!("only clang supports building c code for wasm targets");
321+
}
322+
}
323+
313324
if (target.contains("-none-") || target.contains("nvptx"))
314325
&& build.no_std(*target) == Some(false)
315326
{
@@ -372,3 +383,16 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
372383
cmd_finder.must_have(s);
373384
}
374385
}
386+
387+
/// checks if the compiler at `path` is clang by looking at defined macros
388+
fn is_clang_compiler(path: PathBuf, build: &Build) -> bool {
389+
let cc_output = command(&path)
390+
.arg("-E") // preprocess only
391+
.arg("-dM") // dump defines
392+
.arg("-x")
393+
.arg("c")
394+
.arg("/dev/null")
395+
.run_capture_stdout(build)
396+
.stdout();
397+
cc_output.contains("#define __clang__ 1")
398+
}

‎src/ci/docker/host-x86_64/dist-various-2/Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ ENV \
6565
CXX_i686_unknown_uefi=clang++-11 \
6666
CC_x86_64_unknown_uefi=clang-11 \
6767
CXX_x86_64_unknown_uefi=clang++-11 \
68+
CC_wasm32_unknown_unknown=clang-11 \
69+
CXX_wasm32_unknown_unknown=clang++-11 \
70+
CC_wasm32v1_none=clang-11 \
71+
CXX_wasm32v1_none=clang++-11 \
6872
CC=gcc-9 \
6973
CXX=g++-9
7074

‎src/ci/docker/host-x86_64/test-various/Dockerfile

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ARG DEBIAN_FRONTEND=noninteractive
44
RUN apt-get update && apt-get install -y --no-install-recommends \
55
clang-11 \
66
llvm-11 \
7+
gcc-multilib \
78
g++ \
89
make \
910
ninja-build \
@@ -59,8 +60,8 @@ RUN curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v19.0
5960
tar -xJ
6061
ENV PATH "$PATH:/wasmtime-v19.0.0-x86_64-linux"
6162

62-
ENV WASM_TARGETS=wasm32-wasip1
63-
ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_TARGETS \
63+
ENV WASM_WASIP_TARGET=wasm32-wasip1
64+
ENV WASM_WASIP_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_WASIP_TARGET \
6465
tests/run-make \
6566
tests/ui \
6667
tests/mir-opt \
@@ -69,6 +70,12 @@ ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_T
6970
tests/assembly \
7071
library/core
7172

73+
ENV WASM_BARE_TARGET=wasm32-unknown-unknown \
74+
CC_wasm32_unknown_unknown=clang-11 \
75+
CXX_wasm32_unknown_unknown=clang++-11
76+
ENV WASM_BARE_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_BARE_TARGET \
77+
tests/ui/wasm/wasm-builtins-no-linker-warning.rs
78+
7279
ENV NVPTX_TARGETS=nvptx64-nvidia-cuda
7380
ENV NVPTX_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $NVPTX_TARGETS \
7481
tests/run-make \
@@ -90,4 +97,4 @@ ENV UEFI_TARGETS=aarch64-unknown-uefi,i686-unknown-uefi,x86_64-unknown-uefi \
9097
ENV UEFI_SCRIPT python3 /checkout/x.py --stage 2 build --host='' --target $UEFI_TARGETS && \
9198
python3 -u /uefi_qemu_test/run.py
9299

93-
ENV SCRIPT $WASM_SCRIPT && $NVPTX_SCRIPT && $MUSL_SCRIPT && $UEFI_SCRIPT
100+
ENV SCRIPT $WASM_BARE_SCRIPT && $WASM_WASIP_SCRIPT && $NVPTX_SCRIPT && $MUSL_SCRIPT && $UEFI_SCRIPT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
()
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//@ only-wasm32-bare
2+
3+
use run_make_support::rustc;
4+
5+
fn main() {
6+
rustc().input("foo.rs").target("wasm32-unknown-unknown").arg("-D").arg("linker-messages").run();
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ only-wasm32-bare
2+
//@ compile-flags: -Dlinker-messages
3+
//@ build-pass
4+
/// This test checks that linker messages due to gcc lacking
5+
/// support for wasm32 messages are not emitted when the target
6+
/// is wasm32-unknown-unknown or wasm32v1-none
7+
8+
fn main() {
9+
()
10+
}

0 commit comments

Comments
 (0)
Failed to load comments.