From 8e1e16fdcb3ee87ba1308f0ba045c5a2d29c41b5 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Thu, 4 Jan 2024 09:43:06 +0000 Subject: [PATCH] rustc: Fix building cross-compilers for no_std targets. When building a cross-compiler, the rustc derivation does some tricks to only build the standard library and reuse the host's compiler, leading to much faster build time. Unfortunately, the way the build system was invoked, it would always build the `std` crate, whether or not the target supports it. Some bare-metal targets only support building the `core` and `alloc` crates. By being more vague about the build command, using `library` instead of `library/std`, Rust's build system is able to figure out exactly which crates to build: https://github.com/rust-lang/rust/blob/1.74.1/src/bootstrap/compile.rs#L370-L412 Oddly enough, the install command still needs to use `library/std`, even if building just a subset: https://github.com/rust-lang/rust/blob/1.74.1/src/bootstrap/install.rs#L207 The following command was used to reproduce the original issue. Without this patch, it leads to a build failure when trying to compile one of std's dependencies. With the patch it completes succesfully and produces a working cross-compiler. nix build --impure --expr '(import ./. { crossSystem = { config = "riscv32-none-elf"; rustc.config = "riscv32imc-unknown-none-elf"; }; }).buildPackages.rustc' --- pkgs/development/compilers/rust/rustc.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index 7e365f52ef30..7fe33a4011e5 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -159,7 +159,7 @@ in stdenv.mkDerivation (finalAttrs: { ln -s ${rustc.unwrapped}/bin/rustc build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/rustc-main touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-std/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.libstd.stamp touch build/${stdenv.hostPlatform.rust.rustcTargetSpec}/stage0-rustc/${stdenv.hostPlatform.rust.rustcTargetSpec}/release/.librustc.stamp - python ./x.py --keep-stage=0 --stage=1 build library/std + python ./x.py --keep-stage=0 --stage=1 build library runHook postBuild " else null;