Repository navigation
rust: compile in gcc as the default linker (no cc in pkgs) - #1392
Conversation
rustc's default linker on linux-gnu is the literal `cc`, which pkgs does not ship, so a plain `rustc hello.rs` or a default `cargo build` in a session fails with "linker `cc` not found". Set `default-linker = "gcc"` for both host triples in bootstrap.toml; bootstrap exports it as CFG_DEFAULT_LINKER, the target-spec default compiled into the shipped rustc. (`target.<t>.linker` is only what x.py links the build itself with.) Adds standalone tests that compile and run a hello world with rustc and with cargo, with no -C linker, against toolchain as a test dep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
agent-137
left a comment
There was a problem hiding this comment.
Review on Karl's behalf. This is a comment, not an approval. Nothing was built for it. The findings come from reading the declarations and fetching upstream sources, and each one says how it was checked. The CI rollup on this head is now SUCCESS.
CI: all green, including minimal build. rust passed on both arches, including the new default_linker_rustc and default_linker_cargo tests. Its importers that were rebuilt (codex, probe-rs, ripgrep, ruby, rust-arm-embedded, uv) also passed.
Should-fix: none.
Nit:
packages/rust/bootstrap.toml:30: thelld = falsecomment still says built binaries "keep linking through cc rather than a rust-lld". The PR makes it gcc, so the comment now states the wrong default.
Checked:
- Mechanism. bootstrap rejects unknown keys in
[target.*], so a successful rust build on both arches shows the key is valid in 1.99.1. Both tests are meaningful, because toolchain ships nocc: the symlink atpackages/gcc/build.sh:80is commented out. Without the compiled-in default they would fail with "linkerccnot found". - Cross targets.
CFG_DEFAULT_LINKERbecomes the default for every target spec that does not name its own linker, not only the host. Thumb and wasm targets namerust-lldin their specs, so rust-arm-embedded is unaffected (its tests pass). A cross-gnu or musl target without a spec linker now gets hostgccinstead ofcc. That was wrong before too, so it is no regression. This rust ships only the host std. - Other linkers.
lld = falseis kept, so the linker flavor is unchanged. An explicit linker still wins:- 68 files pass
-C linker=gcc. - amaro-wasm and kani set
CARGO_TARGET_*_LINKER=gcc. - bun sets
CARGO_TARGET_*_LINKER=clang. - The rust stack sets
CC=gccand the per-target linkers.
- 68 files pass
- What it does not fix. The fix covers only rustc's own linker. Build scripts that use the
cccrate still needCC. The stack and recipes already set it, and the new tests use a crate with no dependencies, so they would not show this. That is acceptable for the PR's stated scope.
Problem
In a session with
toolchain+rust(+llvm), a plainrustc hello.rsor a defaultcargo buildfails:rustc's compiled-in default linker on
*-linux-gnuis the literalcc, and pkgs ships nocc. Reproduces on amd64 and arm64, predates #1339, and was seen on fd6a79e and 23dd58c. Recipes work around it one at a time: about 65 Rust recipes exportRUSTFLAGS="-C linker=gcc", and zstd's build.sh notes "/usr/bin/cc does not exist in the build environment".Fix
packages/rust/bootstrap.tomlnow setsdefault-linker = "gcc"in both[target.x86_64-unknown-linux-gnu]and[target.aarch64-unknown-linux-gnu].Which knob, checked against rust 1.99.1's bootstrap:
target.<triple>.linker(already"clang") is only what x.py uses to link the build itself. It does not reach the shipped rustc.target.<triple>.default-linker, withrust.default-linkeras the fallback, is exported asCFG_DEFAULT_LINKERwhen rustc_target is compiled (src/bootstrap/src/core/build_steps/compile.rs, "Prefer the current target's own default_linker, else a globally specified one").compiler/rustc_target/src/spec/mod.rsreads it intoTargetOptions::default().linker = option_env!("CFG_DEFAULT_LINKER"). That is the default the shipped rustc uses when no-C linkeris given.rust.lld = falsestays, so the linker-flavor override (default-linker-linux-override) is unaffected.Why gcc and not clang. clang is already in rust's closure through
llvm. But clang cannot link a Rust binary on its own either: it still needs glibc's crt/libc, gcc's crtbegin/libgcc and anld, and those come fromtoolchain. So clang saves nothing in closure size. gcc is also what the existing recipes pass (-C linker=gcc) and whattoolchainprovides as the C driver. A recipe that already passes-C linker=gccis unaffected, because an explicit flag still wins.Why not a global
ccsymlink in toolchain. Every C build would see it and pick it up through autoconf/cmake probing. That is a much wider change than making rustc's default correct.runtime_deps unchanged. Linking needs the full
toolchain, as it did before withcc. Makingtoolchaina runtime dep of rust would grow rust's closure for every consumer, and almost all Rust consumers already listtoolchainas a build dep. Sotoolchainis only a test dep here. A session still needstoolchaininstalled to link, but with it, no flag is needed any more.Tests
Two new standalone tests (test_deps
bash-bootstrap+toolchain, no-C linkeranywhere):default_linker_rustc:rustc hello.rs -o h && ./hprintshello rust.default_linker_cargo:cargo new --offline --vcs none hi, then an offlinecargo buildof that dependency-free crate, then./target/debug/hiprintsHello, world!(CARGO_HOMEis set to the test cwd).Verification
default-linkeris the compiled-in default andlinkeris not.nickel formatis clean.min package build --rebuild --verbose rustattempt died at sandbox spawn withNo space left on deviceon the minimald VM (shared disk). It never reached build.sh, and the session was destroyed. The build and both new tests are left to build-bot CI.Impact
This changes
bootstrap.toml, a hashed input ofrust, so it rebuilds rust (~16 min per arch) and every package that build-depends on it: 74 packages that import it directly plus their dependents, a near-world rebuild of the Rust stack. The shipped bits change only in rustc's default-linker string. Recipes passing-C linker=gcclink exactly as before.🤖 Generated with Claude Code
Summary by CodeRabbit