about summary refs log tree commit diff
path: root/pkgs/build-support/rust
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2024-02-28 23:10:07 +0100
committerGitHub <noreply@github.com>2024-02-28 23:10:07 +0100
commit79156bf13ae76389684d4f3bd94a88639d835213 (patch)
treef24cadde9586bdabeb5b55fe6392e5c92158a753 /pkgs/build-support/rust
parent5134b74edfb4fd8aab15254c42dc8c362a1505b7 (diff)
rustc: move crt-static default override to wrapper (#291829)
Previously, when cross compiling from non-musl to musl, the crt-static
default override wouldn't be applied, because the compiler wouldn't
have been built with it due to fastCross.  Moving it to the wrapper
fixes this without having to introduce extra compiler rebuilds.  And
because the wrapper is applied even to the bootstrap rustc, we no
longer need special handling of crt-static in the Cargo expression.

Unlike --sysroot, rustc allows -C target-feature= to be passed
multiple times, with later instances taking precedence over earlier
ones.  This means that it's very easy to set the default in the
wrapper, just by our overridden default before any other arguments.

This fixes pkgsCross.aarch64-multiplatform-musl.mesa from x86_64-linux.
Diffstat (limited to 'pkgs/build-support/rust')
-rw-r--r--pkgs/build-support/rust/rustc-wrapper/default.nix20
-rw-r--r--pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh2
2 files changed, 21 insertions, 1 deletions
diff --git a/pkgs/build-support/rust/rustc-wrapper/default.nix b/pkgs/build-support/rust/rustc-wrapper/default.nix
index d6034c08af47d..2b4a84e72b240 100644
--- a/pkgs/build-support/rust/rustc-wrapper/default.nix
+++ b/pkgs/build-support/rust/rustc-wrapper/default.nix
@@ -8,6 +8,26 @@ runCommand "${rustc-unwrapped.pname}-wrapper-${rustc-unwrapped.version}" {
   env = {
     prog = "${rustc-unwrapped}/bin/rustc";
     sysroot = lib.optionalString (sysroot != null) "--sysroot ${sysroot}";
+
+    # Upstream rustc still assumes that musl = static[1].  The fix for
+    # this is to disable crt-static by default for non-static musl
+    # targets.
+    #
+    # Even though Cargo will build build.rs files for the build platform,
+    # cross-compiling _from_ musl appears to work fine, so we only need
+    # to do this when rustc's target platform is dynamically linked musl.
+    #
+    # [1]: https://github.com/rust-lang/compiler-team/issues/422
+    #
+    # WARNING: using defaultArgs is dangerous, as it will apply to all
+    # targets used by this compiler (host and target).  This means
+    # that it can't be used to set arguments that should only be
+    # applied to the target.  It's fine to do this for -crt-static,
+    # because rustc does not support +crt-static host platforms
+    # anyway.
+    defaultArgs = lib.optionalString
+      (with rustc-unwrapped.stdenv.targetPlatform; isMusl && !isStatic)
+      "-C target-feature=-crt-static";
   };
 
   passthru = {
diff --git a/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh b/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
index 4a90e30652fea..63e18be2e1310 100644
--- a/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
+++ b/pkgs/build-support/rust/rustc-wrapper/rustc-wrapper.sh
@@ -13,7 +13,7 @@ for arg; do
     esac
 done
 
-extraBefore=("${defaultSysroot[@]}")
+extraBefore=(@defaultArgs@ "${defaultSysroot[@]}")
 extraAfter=($NIX_RUSTFLAGS)
 
 # Optionally print debug info.