Repository: rust
Issue: Unable to compile rust for riscv32imc-unknown-none-elf with clang 19.0.0git 
Labels: E-needs-test,T-bootstrap,C-bug,O-riscv

We're running into an issue on Fuchsia where we fail to compile tip-of-tree rust targeting `riscv32imc-unknown-none-elf` while using a nightly build of clang 19.0.0git, which is at the moment:

```
clang --version
Fuchsia clang version 19.0.0git (https://llvm.googlesource.com/llvm-project 1e7d5871eed1bdf1eeb4b50eb9b911774f420ca1)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/etryzelaar/src/fuchsia-rust/clang/bin
```

Rust compilation will fail trying to compile `compiler-builtins` with this error:

```
Building stage2 library artifacts (aarch64-unknown-linux-gnu -> riscv32imc-unknown-none-elf)
   Compiling core v0.0.0 (/b/s/w/ir/x/w/fuchsia-third_party-rust/library/core)
   Compiling compiler_builtins v0.1.108
The following warnings were emitted during compilation:

warning: compiler_builtins@0.1.108: error: unknown target triple 'riscv32imc-unknown-none-elf'

error: failed to run custom build command for `compiler_builtins v0.1.108`

Caused by:
  process didn't exit successfully: `/b/s/w/ir/x/w/staging/build/fuchsia-build/aarch64-unknown-linux-gnu/stage2-std/release/build/compiler_builtins-67cabcbd74a02cc3/build-script-build` (exit status: 1)

...
  running: "/b/s/w/ir/x/w/cipd/bin/clang" "-O3" "-ffunction-sections" "-fdata-sections" "-g" "-fno-omit-frame-pointer" "--target=riscv32imc-unknown-none-elf" "-ffunction-sections" "-fdata-sections" "--target=riscv32imc-unknown-none-elf" "-fno-builtin" "-fvisibility=hidden" "-ffreestanding" "-DVISIBILITY_HIDDEN" "-o" "/b/s/w/ir/x/w/staging/build/fuchsia-build/aarch64-unknown-linux-gnu/stage2-std/riscv32imc-unknown-none-elf/release/build/compiler_builtins-70bd6bc9d3d89298/out/fdce3f58404f6d87-absvdi2.o" "-c" "/b/s/w/ir/x/w/fuchsia-third_party-rust/src/llvm-project/compiler-rt/lib/builtins/absvdi2.c"
  cargo:warning=error: unknown target triple 'riscv32imc-unknown-none-elf'
```

The full build logs can be seen [here](https://ci.chromium.org/raw/build/logs.chromium.org/fuchsia/led/etryzelaar_google.com/df7e3c80ca74cf52991047c117e2b415151a59f25d8ee7a47fb5cbdffce5af60/+/build.proto?server=chromium-swarm.appspot.com), with the [compilation logs](https://logs.chromium.org/logs/fuchsia/led/etryzelaar_google.com/df7e3c80ca74cf52991047c117e2b415151a59f25d8ee7a47fb5cbdffce5af60/+/u/rust_install/stdout?format=raw), our [config.toml](https://logs.chromium.org/logs/fuchsia/led/etryzelaar_google.com/df7e3c80ca74cf52991047c117e2b415151a59f25d8ee7a47fb5cbdffce5af60/+/u/generate_config.toml/raw_io.output_text), and our [environment variables](https://logs.chromium.org/logs/fuchsia/led/etryzelaar_google.com/df7e3c80ca74cf52991047c117e2b415151a59f25d8ee7a47fb5cbdffce5af60/+/u/generate_environment/raw_io.output_text_environment_).

According to that [target's spec](https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/targets/riscv32imc_unknown_none_elf.rs), it should be using the `riscv32` target with llvm and clang.

Tracing through the commits, it appears the compilation failure was introduced in https://github.com/rust-lang/compiler-builtins/pull/563, when `compiler-builtins` added support to fall back to `compiler-rt` for riscv for some intrinsics. However I'm not sure if it's a `compiler-builtins` issue. According to the build logs and my investigation, it looks like the rustc build system is explicitly setting setting the environment variable that's being used by `compiler-builtins` when it tries to compile the intrinsics:

```
  CFLAGS_riscv32imc_unknown_none_elf = Some("-ffunction-sections -fdata-sections --target=riscv32imc-unknown-none-elf")
```

Unfortunately I haven't been able to figure out yet why rust is using the wrong llvm target here. However, we've figured out a workaround in this [patch](https://fuchsia-review.git.corp.google.com/c/infra/recipes/+/993903) where we're setting the environment variable to this to use the `--target=riscv32`.

### Meta

This was compiled against the rust-lang/rust commit 2b43e75c98cc5ae32328c8b49657bcd882eb5e75.