Repository: rust
Issue: "cannot produce dylib for `rustc_driver v0.0.0 ...` as the target `riscv64gc-unknown-linux-musl` does not support these crate types"
Labels: A-linkage,C-enhancement,T-bootstrap,T-cargo,O-musl

I'm cross-compiling Rust for `riscv64gc-unknown-linux-musl` with the following `config.toml`:

```
profile = "codegen"
changelog-seen = 2

[build]
docs = false
compiler-docs = false
extended = true

[target.riscv64gc-unknown-linux-musl]
ar = "/home/scratch/riscv/bin/riscv64-unknown-linux-musl-ar"
linker = "/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc"
cc = "/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc"
cxx = "/home/scratch/riscv/bin/riscv64-unknown-linux-musl-g++"
musl-root = "/home/scratch/riscv/sysroot"

[llvm]
download-ci-llvm = false
link-shared = false
```
Executing:
`RUSTFLAGS="-Clink-args=-lgcc" ./x.py build --host riscv64gc-unknown-linux-musl --target riscv64gc-unknown-linux-musl  --verbose`

the output is:
```
[snip]
Building compiler artifacts (stage1:x86_64-unknown-linux-gnu -> stage2:riscv64gc-unknown-linux-musl)
running: cd "/home/scratch/rust" && AR_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-ar" CARGO_INCREMENTAL="1" CARGO_PROFILE_RELEASE_DEBUG="0" CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false" CARGO_TARGET_DIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1-rustc" CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_MUSL_LINKER="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc" CC_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc" CFG_COMPILER_HOST_TRIPLE="riscv64gc-unknown-linux-musl" CFG_DEFAULT_CODEGEN_BACKEND="llvm" CFG_LIBDIR_RELATIVE="lib" CFG_RELEASE="1.71.0-dev" CFG_RELEASE_CHANNEL="dev" CFG_VERSION="1.71.0-dev" CFLAGS_riscv64gc_unknown_linux_musl="-ffunction-sections -fdata-sections -fPIC -march=rv64gc -mabi=lp64d -mcmodel=medany -static" CXXFLAGS_riscv64gc_unknown_linux_musl="-ffunction-sections -fdata-sections -fPIC -march=rv64gc -mabi=lp64d -mcmodel=medany -static" CXX_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-g++" LIBC_CHECK_CFG="1" LLVM_CONFIG="/home/scratch/rust/build/x86_64-unknown-linux-gnu/llvm/build/bin/llvm-config" LLVM_NDEBUG="1" LLVM_RUSTLLVM="1" RANLIB_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-ar s" REAL_LIBRARY_PATH_VAR="LD_LIBRARY_PATH" RUSTBUILD_NATIVE_DIR="/home/scratch/rust/build/riscv64gc-unknown-linux-musl/native" RUSTC="/home/scratch/rust/build/bootstrap/debug/rustc" RUSTC_BACKTRACE_ON_ICE="1" RUSTC_BOOTSTRAP="1" RUSTC_BREAK_ON_ICE="1" RUSTC_ERROR_METADATA_DST="/home/scratch/rust/build/tmp/extended-error-metadata" RUSTC_FORCE_UNSTABLE="1" RUSTC_INSTALL_BINDIR="bin" RUSTC_LIBDIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/lib" RUSTC_LINT_FLAGS="-Wrust_2018_idioms -Wunused_lifetimes -Wsemicolon_in_expressions_from_macros -Dwarnings" RUSTC_REAL="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" RUSTC_SNAPSHOT="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" RUSTC_SNAPSHOT_LIBDIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/lib" RUSTC_STAGE="1" RUSTC_SYSROOT="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1" RUSTC_TLS_MODEL_INITIAL_EXEC="1" RUSTC_VERBOSE="1" RUSTDOC="/home/scratch/rust/build/bootstrap/debug/rustdoc" RUSTDOCFLAGS="-Clink-args=-lgcc --cfg=windows_raw_dylib -Csymbol-mangling-version=v0 -Zunstable-options --check-cfg=values(bootstrap) --check-cfg=values(parallel_compiler) --check-cfg=values(no_btreemap_remove_entry) --check-cfg=values(crossbeam_loom) --check-cfg=values(span_locations) --check-cfg=values(rustix_use_libc) --check-cfg=values(emulate_second_only_system) --check-cfg=values(windows_raw_dylib) -Dwarnings -Wrustdoc::invalid_codeblock_attributes --crate-version 1.71.0-dev" RUSTDOC_REAL="/path/to/nowhere/rustdoc/not/required" RUSTFLAGS="-Clink-args=-lgcc --cfg=windows_raw_dylib -Csymbol-mangling-version=v0 -Zunstable-options --check-cfg=values(bootstrap) --check-cfg=values(parallel_compiler) --check-cfg=values(no_btreemap_remove_entry) --check-cfg=values(crossbeam_loom) --check-cfg=values(span_locations) --check-cfg=values(rustix_use_libc) --check-cfg=values(emulate_second_only_system) --check-cfg=values(windows_raw_dylib) -Zdual-proc-macros -Zmacro-backtrace -Clink-args=-Wl,-z,origin -Clink-args=-Wl,-rpath,$ORIGIN/../lib -Csplit-debuginfo=off -Zunstable-options -Wrustc::internal -Cprefer-dynamic -Cllvm-args=-import-instr-limit=10 -Clto=off" RUST_TEST_THREADS="32" WINAPI_NO_BUNDLED_LIBRARIES="1" __CARGO_DEFAULT_LIB_METADATA="dev" "/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "riscv64gc-unknown-linux-musl" "--release" "-Zcheck-cfg=names,values,output,features" "-Zdual-proc-macros" "-Zbinary-dep-depinfo" "-j" "32" "-v" "--features" "llvm" "--manifest-path" "/home/scratch/rust/compiler/rustc/Cargo.toml" "--message-format" "json-render-diagnostics"
error: cannot produce dylib for `rustc_driver v0.0.0 (/home/scratch/rust/compiler/rustc_driver)` as the target `riscv64gc-unknown-linux-musl` does not support these crate types
command did not execute successfully: cd "/home/scratch/rust" && AR_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-ar" CARGO_INCREMENTAL="1" CARGO_PROFILE_RELEASE_DEBUG="0" CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false" CARGO_TARGET_DIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1-rustc" CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_MUSL_LINKER="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc" CC_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-gcc" CFG_COMPILER_HOST_TRIPLE="riscv64gc-unknown-linux-musl" CFG_DEFAULT_CODEGEN_BACKEND="llvm" CFG_LIBDIR_RELATIVE="lib" CFG_RELEASE="1.71.0-dev" CFG_RELEASE_CHANNEL="dev" CFG_VERSION="1.71.0-dev" CFLAGS_riscv64gc_unknown_linux_musl="-ffunction-sections -fdata-sections -fPIC -march=rv64gc -mabi=lp64d -mcmodel=medany -static" CXXFLAGS_riscv64gc_unknown_linux_musl="-ffunction-sections -fdata-sections -fPIC -march=rv64gc -mabi=lp64d -mcmodel=medany -static" CXX_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-g++" LIBC_CHECK_CFG="1" LLVM_CONFIG="/home/scratch/rust/build/x86_64-unknown-linux-gnu/llvm/build/bin/llvm-config" LLVM_NDEBUG="1" LLVM_RUSTLLVM="1" RANLIB_riscv64gc_unknown_linux_musl="/home/scratch/riscv/bin/riscv64-unknown-linux-musl-ar s" REAL_LIBRARY_PATH_VAR="LD_LIBRARY_PATH" RUSTBUILD_NATIVE_DIR="/home/scratch/rust/build/riscv64gc-unknown-linux-musl/native" RUSTC="/home/scratch/rust/build/bootstrap/debug/rustc" RUSTC_BACKTRACE_ON_ICE="1" RUSTC_BOOTSTRAP="1" RUSTC_BREAK_ON_ICE="1" RUSTC_ERROR_METADATA_DST="/home/scratch/rust/build/tmp/extended-error-metadata" RUSTC_FORCE_UNSTABLE="1" RUSTC_INSTALL_BINDIR="bin" RUSTC_LIBDIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/lib" RUSTC_LINT_FLAGS="-Wrust_2018_idioms -Wunused_lifetimes -Wsemicolon_in_expressions_from_macros -Dwarnings" RUSTC_REAL="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" RUSTC_SNAPSHOT="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/bin/rustc" RUSTC_SNAPSHOT_LIBDIR="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1/lib" RUSTC_STAGE="1" RUSTC_SYSROOT="/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage1" RUSTC_TLS_MODEL_INITIAL_EXEC="1" RUSTC_VERBOSE="1" RUSTDOC="/home/scratch/rust/build/bootstrap/debug/rustdoc" RUSTDOCFLAGS="-Clink-args=-lgcc --cfg=windows_raw_dylib -Csymbol-mangling-version=v0 -Zunstable-options --check-cfg=values(bootstrap) --check-cfg=values(parallel_compiler) --check-cfg=values(no_btreemap_remove_entry) --check-cfg=values(crossbeam_loom) --check-cfg=values(span_locations) --check-cfg=values(rustix_use_libc) --check-cfg=values(emulate_second_only_system) --check-cfg=values(windows_raw_dylib) -Dwarnings -Wrustdoc::invalid_codeblock_attributes --crate-version 1.71.0-dev" RUSTDOC_REAL="/path/to/nowhere/rustdoc/not/required" RUSTFLAGS="-Clink-args=-lgcc --cfg=windows_raw_dylib -Csymbol-mangling-version=v0 -Zunstable-options --check-cfg=values(bootstrap) --check-cfg=values(parallel_compiler) --check-cfg=values(no_btreemap_remove_entry) --check-cfg=values(crossbeam_loom) --check-cfg=values(span_locations) --check-cfg=values(rustix_use_libc) --check-cfg=values(emulate_second_only_system) --check-cfg=values(windows_raw_dylib) -Zdual-proc-macros -Zmacro-backtrace -Clink-args=-Wl,-z,origin -Clink-args=-Wl,-rpath,$ORIGIN/../lib -Csplit-debuginfo=off -Zunstable-options -Wrustc::internal -Cprefer-dynamic -Cllvm-args=-import-instr-limit=10 -Clto=off" RUST_TEST_THREADS="32" WINAPI_NO_BUNDLED_LIBRARIES="1" __CARGO_DEFAULT_LIB_METADATA="dev" "/home/scratch/rust/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "riscv64gc-unknown-linux-musl" "--release" "-Zcheck-cfg=names,values,output,features" "-Zdual-proc-macros" "-Zbinary-dep-depinfo" "-j" "32" "-v" "--features" "llvm" "--manifest-path" "/home/scratch/rust/compiler/rustc/Cargo.toml" "--message-format" "json-render-diagnostics"
expected success, got: exit status: 101
Traceback (most recent call last):
  File "/home/scratch/rust/./x.py", line 29, in <module>
    bootstrap.main()
  File "/home/scratch/rust/src/bootstrap/bootstrap.py", line 951, in main
    bootstrap(args)
  File "/home/scratch/rust/src/bootstrap/bootstrap.py", line 926, in bootstrap
    run(args, env=env, verbose=build.verbose, is_bootstrap=True)
  File "/home/scratch/rust/src/bootstrap/bootstrap.py", line 167, in run
    raise RuntimeError(err)
RuntimeError: failed to run: /home/scratch/rust/build/bootstrap/debug/bootstrap build --host riscv64gc-unknown-linux-musl --target riscv64gc-unknown-linux-musl --verbose
```
If I edit the `compiler/rustc_driver/Cargo.toml` file as suggested [here](https://users.rust-lang.org/t/how-to-build-a-statically-linked-rustc/83024), it works.

I filed a separate bug, #110509, about the error message, but even aside from the diagnostics, this behavior seems wrong. My understanding of what the error means is "we can't build the extended Rust tool set, because that requires a dynamically linked `rustc_driver` crate, which can't be built for a target with a statically linked C runtime" (if my understanding is wrong, let me know.) That seems like a condition that should be detected at config time, not midway through the build. Moreover, if this really is an intrinsic limitation of compiling Rust for statically linked targets, it seems weird to me that the `compiler/rustc_driver/Cargo.toml` patch would fix it. On the other hand, if it's not an intrinsic limitation, then manually patching this file shouldn't be necessary and the preferred crate type for `rustc_driver` should be auto-detected based on the target and target options.

Hope this makes sense, but if further clarification is needed, let me know!