Skip to content

Commit

Permalink
Auto merge of rust-lang#137457 - JayAndJef:issue-132802-fix, r=<try>
Browse files Browse the repository at this point in the history
fix for issue 132802: x86 code in `wasm32-unknown-unknown` binaries

This is a direct fix for issue [132802](rust-lang#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
  • Loading branch information
bors committed Mar 3, 2025
2 parents 81d8edc + 63db26f commit ca07aa7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/bootstrap/src/core/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ than building it.
.entry(*target)
.or_insert_with(|| Target::from_triple(&target.triple));

// compiler-rt c fallbacks for wasm cannot be built with gcc
if target.contains("wasm") // bare metal targets without wasi sdk
&& (build.config.optimized_compiler_builtins(*target)
|| build.config.rust_std_features.contains("compiler-builtins-c"))
{
let is_clang = is_clang_compiler(build.cc(*target), build);
if !is_clang {
panic!("only clang supports building c code for wasm targets");
}
}

if (target.contains("-none-") || target.contains("nvptx"))
&& build.no_std(*target) == Some(false)
{
Expand Down Expand Up @@ -372,3 +383,16 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
cmd_finder.must_have(s);
}
}

/// checks if the compiler at `path` is clang by looking at defined macros
fn is_clang_compiler(path: PathBuf, build: &Build) -> bool {
let cc_output = command(&path)
.arg("-E") // preprocess only
.arg("-dM") // dump defines
.arg("-x")
.arg("c")
.arg("/dev/null")
.run_capture_stdout(build)
.stdout();
cc_output.contains("#define __clang__ 1")
}
4 changes: 4 additions & 0 deletions src/ci/docker/host-x86_64/dist-various-2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ ENV \
CXX_i686_unknown_uefi=clang++-11 \
CC_x86_64_unknown_uefi=clang-11 \
CXX_x86_64_unknown_uefi=clang++-11 \
CC_wasm32_unknown_unknown=clang-11 \
CXX_wasm32_unknown_unknown=clang++-11 \
CC_wasm32v1_none=clang-11 \
CXX_wasm32v1_none=clang++-11 \
CC=gcc-9 \
CXX=g++-9

Expand Down
13 changes: 10 additions & 3 deletions src/ci/docker/host-x86_64/test-various/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
clang-11 \
llvm-11 \
gcc-multilib \
g++ \
make \
ninja-build \
Expand Down Expand Up @@ -59,8 +60,8 @@ RUN curl -L https://github.com/bytecodealliance/wasmtime/releases/download/v19.0
tar -xJ
ENV PATH "$PATH:/wasmtime-v19.0.0-x86_64-linux"

ENV WASM_TARGETS=wasm32-wasip1
ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_TARGETS \
ENV WASM_WASIP_TARGET=wasm32-wasip1
ENV WASM_WASIP_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_WASIP_TARGET \
tests/run-make \
tests/ui \
tests/mir-opt \
Expand All @@ -69,6 +70,12 @@ ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_T
tests/assembly \
library/core

ENV WASM_BARE_TARGET=wasm32-unknown-unknown \
CC_wasm32_unknown_unknown=clang-11 \
CXX_wasm32_unknown_unknown=clang++-11
ENV WASM_BARE_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_BARE_TARGET \
tests/ui/wasm/wasm-builtins-no-linker-warning.rs

ENV NVPTX_TARGETS=nvptx64-nvidia-cuda
ENV NVPTX_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $NVPTX_TARGETS \
tests/run-make \
Expand All @@ -90,4 +97,4 @@ ENV UEFI_TARGETS=aarch64-unknown-uefi,i686-unknown-uefi,x86_64-unknown-uefi \
ENV UEFI_SCRIPT python3 /checkout/x.py --stage 2 build --host='' --target $UEFI_TARGETS && \
python3 -u /uefi_qemu_test/run.py

ENV SCRIPT $WASM_SCRIPT && $NVPTX_SCRIPT && $MUSL_SCRIPT && $UEFI_SCRIPT
ENV SCRIPT $WASM_BARE_SCRIPT && $WASM_WASIP_SCRIPT && $NVPTX_SCRIPT && $MUSL_SCRIPT && $UEFI_SCRIPT
3 changes: 3 additions & 0 deletions tests/run-make/wasm-builtins-no-linker-warning/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
()
}
7 changes: 7 additions & 0 deletions tests/run-make/wasm-builtins-no-linker-warning/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ only-wasm32-bare

use run_make_support::rustc;

fn main() {
rustc().input("foo.rs").target("wasm32-unknown-unknown").arg("-D").arg("linker-messages").run();
}
10 changes: 10 additions & 0 deletions tests/ui/wasm/wasm-builtins-no-linker-warning.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ only-wasm32-bare
//@ compile-flags: -Dlinker-messages
//@ build-pass
/// This test checks that linker messages due to gcc lacking
/// support for wasm32 messages are not emitted when the target
/// is wasm32-unknown-unknown or wasm32v1-none
fn main() {
()
}

0 comments on commit ca07aa7

Please sign in to comment.