8.35. rustc 1.95.0

Build and install the final Rust compiler and Cargo toolchain inside the target chroot.

Input assumption: rustc-1.95.0-src.tar.xz is already present in /sources from the chapter 4 source staging step.

Source URL: https://static.rust-lang.org/dist/rustc-1.95.0-src.tar.xz

Upstream build note: the 1.95.0 source tree uses bootstrap.toml as the primary bootstrap configuration file. Upstream's bootstrap documentation describes ./x.py install as the source-install command, and the local src/etc/xhelp documents -j, --jobs for parallelism.

musl target note: compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs still sets base.crt_static_default = true in this release. This section patches that default to false so the installed musl target produces dynamically linked binaries unless a build explicitly asks for static CRT linkage.

Licenses:

Dependencies:

rustc is a compiler for the Rust programming language. we need it to provide the final target rustc compiler, standard library, and Rust documentation support for packages that build Rust code.

cargo is Rust's package manager and build driver. we need it to build Rust packages and drive Rust dependency builds in the final target environment.

Extract and Enter the Source Tree

cd /sources
rm -rf rustc-1.95.0-src
tar -xf rustc-1.95.0-src.tar.xz
cd rustc-1.95.0-src

Patch the musl Target Default

cat > /tmp/rustc-1.95.0-musl-dynamic-link.patch <<'EOF'
--- a/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs
+++ b/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs
@@ -18,6 +18,6 @@ pub(crate) fn target() -> Target {
     base.supports_xray = true;
     // FIXME(compiler-team#422): musl targets should be dynamically linked by default.
-    base.crt_static_default = true;
+    base.crt_static_default = false;

     Target {
         llvm_target: "x86_64-unknown-linux-musl".into(),
EOF

patch -Np1 -i /tmp/rustc-1.95.0-musl-dynamic-link.patch

Create bootstrap.toml

cat > bootstrap.toml <<EOF
change-id = 148671

[build]
build = "x86_64-unknown-linux-musl"
host = ["x86_64-unknown-linux-musl"]
target = ["x86_64-unknown-linux-musl"]
vendor = true
submodules = false
extended = true
tools = ["cargo", "rustdoc"]
jobs = $LWI_MAKE_JOBS

[install]
prefix = "/system"
sysconfdir = "/system/configuration"
docdir = "documentation/rust"
bindir = "binaries"
libdir = "libraries"
mandir = "documentation/man-pages"
datadir = "share"

[llvm]
download-ci-llvm = false
use-libcxx = true

[rust]
channel = "stable"
download-rustc = false
default-linker = "/system/binaries/cc"
lld = false
bootstrap-override-lld = false
rpath = false
jemalloc = false
codegen-tests = false
llvm-tools = false

[target.x86_64-unknown-linux-musl]
cc = "/system/binaries/cc"
cxx = "/system/binaries/c++"
linker = "/system/binaries/cc"
ar = "/system/binaries/llvm-ar"
ranlib = "/system/binaries/llvm-ranlib"
llvm-config = "/system/binaries/llvm-config"
llvm-has-rust-patches = false
crt-static = false
EOF

Build and Install rustc

./x.py install -j "$LWI_MAKE_JOBS"

Verify rustc

rustc --version
cargo --version
rustdoc --version

Check that the default musl target now links dynamically.

cat > /tmp/lbi-rust-test.rs <<'EOF'
fn main() {
    println!("rust ok");
}
EOF

rustc /tmp/lbi-rust-test.rs -o /tmp/lbi-rust-test
/tmp/lbi-rust-test
readelf -l /tmp/lbi-rust-test | grep 'Requesting program interpreter'

The Rust test should print:

rust ok

The readelf command should show the musl dynamic loader instead of reporting no program interpreter.

After this step is complete, you can remove the extracted source directory and source tarball from /sources if you do not plan to rebuild rustc again.

Command Explanations