Skip to content

rust: compile in gcc as the default linker (no cc in pkgs) - #1392

Merged
bryan-minimal merged 1 commit into
mainfrom
rust-default-linker-gcc
Nov 11, 2026
Merged

bryan-minimal merged 1 commit into
mainfrom
rust-default-linker-gcc

Conversation

@bryan-minimal

@bryan-minimal bryan-minimal commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Problem

In a session with toolchain + rust (+ llvm), a plain rustc hello.rs or a default cargo build fails:

error: linker `cc` not found

rustc's compiled-in default linker on *-linux-gnu is the literal cc, and pkgs ships no cc. 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 export RUSTFLAGS="-C linker=gcc", and zstd's build.sh notes "/usr/bin/cc does not exist in the build environment".

Fix

packages/rust/bootstrap.toml now sets default-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, with rust.default-linker as the fallback, is exported as CFG_DEFAULT_LINKER when 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.rs reads it into TargetOptions::default().linker = option_env!("CFG_DEFAULT_LINKER"). That is the default the shipped rustc uses when no -C linker is given.
  • The per-target key is keyed on the triple the compiler runs on, so each arch's rustc gets gcc. rust.lld = false stays, 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 an ld, and those come from toolchain. So clang saves nothing in closure size. gcc is also what the existing recipes pass (-C linker=gcc) and what toolchain provides as the C driver. A recipe that already passes -C linker=gcc is unaffected, because an explicit flag still wins.

Why not a global cc symlink 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 with cc. Making toolchain a runtime dep of rust would grow rust's closure for every consumer, and almost all Rust consumers already list toolchain as a build dep. So toolchain is only a test dep here. A session still needs toolchain installed to link, but with it, no flag is needed any more.

Tests

Two new standalone tests (test_deps bash-bootstrap + toolchain, no -C linker anywhere):

  • default_linker_rustc: rustc hello.rs -o h && ./h prints hello rust.
  • default_linker_cargo: cargo new --offline --vcs none hi, then an offline cargo build of that dependency-free crate, then ./target/debug/hi prints Hello, world! (CARGO_HOME is set to the test cwd).

Verification

  • Read the 1.99.1 bootstrap and rustc_target sources (above) to confirm default-linker is the compiled-in default and linker is not.
  • nickel format is clean.
  • Not built locally. The one local min package build --rebuild --verbose rust attempt died at sandbox spawn with No space left on device on 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 of rust, 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=gcc link exactly as before.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Rust builds targeting x86_64 and aarch64 Linux now use GCC as the default linker, allowing builds to proceed without manually specifying a linker.
  • Tests
    • Added checks covering standalone Rust compilation and offline Cargo builds on the updated targets.

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>
@bryan-minimal
bryan-minimal requested a review from a team as a code owner November 8, 2026 03:42
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Rust package sets GCC as the default linker for x86_64 and aarch64 Linux targets. It adds standalone rustc and Cargo tests that build and run programs without specifying a linker.

Changes: Rust default linker

Layer / File(s) Summary
Configure default linker
packages/rust/bootstrap.toml
The x86_64 and aarch64 Linux target configurations now set default-linker = "gcc".
Test linker selection
packages/rust/build.ncl
The build imports Test and bash-bootstrap. It adds a rustc test and an offline Cargo test that each build and run a program without specifying a linker. Both tests declare bash-bootstrap and toolchain as dependencies.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • gominimal/pkgs#805: This PR addresses the same Rust linker gap with a cc symlink to GCC and adds a standalone hello-world test.

Suggested reviewers: msample

Merge Risk: Minimal · up to 29a13

This change makes gcc rustc's default linker on two Linux targets and adds tests for it. No actionable merge-blocking issue remains. It requires a Rust rebuild and rebuilds of its dependents, as expected.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: gcc is set as rustc's compiled-in default linker. It also gives the reason, that no cc is shipped in pkgs. The raw summary and objectives both describe the bootstrap.t…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: gcc is set as rustc's compiled-in default linker. It also gives the reason, that no cc is shipped in pkgs. The raw summary and objectives both describe the bootstrap.toml default-linker change and the added tests, so the title matches the changeset. It omits the new standalone tests, which is acceptable because it does not need to cover every detail.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fd6a79e and 29a13a5.

Files selected for processing: 2

  • packages/rust/bootstrap.toml
  • packages/rust/build.ncl

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

@agent-137 agent-137 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: the lld = false comment 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 no cc: the symlink at packages/gcc/build.sh:80 is commented out. Without the compiled-in default they would fail with "linker cc not found".
  • Cross targets. CFG_DEFAULT_LINKER becomes the default for every target spec that does not name its own linker, not only the host. Thumb and wasm targets name rust-lld in their specs, so rust-arm-embedded is unaffected (its tests pass). A cross-gnu or musl target without a spec linker now gets host gcc instead of cc. That was wrong before too, so it is no regression. This rust ships only the host std.
  • Other linkers. lld = false is 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=gcc and the per-target linkers.
  • What it does not fix. The fix covers only rustc's own linker. Build scripts that use the cc crate still need CC. 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.

@agent-137 agent-137 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 29a13a5. No should-fix findings; the nits in the earlier comment review are optional.

@bryan-minimal
bryan-minimal added this pull request to the merge queue Nov 11, 2026
Merged via the queue into main with commit 9a76415 Nov 11, 2026
15 checks passed
@bryan-minimal
bryan-minimal deleted the rust-default-linker-gcc branch November 11, 2026 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants