From 79156bf13ae76389684d4f3bd94a88639d835213 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Wed, 28 Feb 2024 23:10:07 +0100 Subject: [PATCH] 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. --- .../rust/rustc-wrapper/default.nix | 20 +++++++++++ .../rust/rustc-wrapper/rustc-wrapper.sh | 2 +- pkgs/development/compilers/rust/cargo.nix | 36 ------------------- pkgs/development/compilers/rust/rustc.nix | 12 ------- 4 files changed, 21 insertions(+), 49 deletions(-) diff --git a/pkgs/build-support/rust/rustc-wrapper/default.nix b/pkgs/build-support/rust/rustc-wrapper/default.nix index d6034c08af47..2b4a84e72b24 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 4a90e30652fe..63e18be2e131 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. diff --git a/pkgs/development/compilers/rust/cargo.nix b/pkgs/development/compilers/rust/cargo.nix index 1377cfad6c6c..b57ad0c811c2 100644 --- a/pkgs/development/compilers/rust/cargo.nix +++ b/pkgs/development/compilers/rust/cargo.nix @@ -24,42 +24,6 @@ rustPlatform.buildRustPackage.override { inherit (rustc.unwrapped) tests; }; - # Upstream rustc still assumes that musl = static[1]. The fix for - # this is to disable crt-static by default for non-static musl - # targets. - # - # For every package apart from Cargo, we can fix this by just - # patching rustc to not have crt-static by default. But Cargo is - # built with the upstream bootstrap binary for rustc, which we can't - # easily patch. This means we need to find another way to make sure - # crt-static is not used during the build of pkgsMusl.cargo. - # - # By default, Cargo doesn't apply RUSTFLAGS when building build.rs - # if --target is passed, so the only good way to set -crt-static for - # build.rs files used in the Cargo build is to use the unstable - # -Zhost-config Cargo feature. This allows us to specify flags that - # should be passed to rustc when building for the build platform. - # We also need to use -Ztarget-applies-to-host, because using - # -Zhost-config requires it. - # - # When doing this, we also have to specify the linker, or cargo - # won't pass a -C linker= argument to rustc. This will make rustc - # try to use its default value of "cc", which won't be available - # when cross-compiling. - # - # [1]: https://github.com/rust-lang/compiler-team/issues/422 - postPatch = lib.optionalString (with stdenv.buildPlatform; isMusl && !isStatic) '' - mkdir -p .cargo - cat <> .cargo/config - [host] - rustflags = "-C target-feature=-crt-static" - linker = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}cc" - [unstable] - host-config = true - target-applies-to-host = true - EOF - ''; - # changes hash of vendor directory otherwise dontUpdateAutotoolsGnuConfigScripts = true; diff --git a/pkgs/development/compilers/rust/rustc.nix b/pkgs/development/compilers/rust/rustc.nix index 6e0afa1b8f57..9235519eee1f 100644 --- a/pkgs/development/compilers/rust/rustc.nix +++ b/pkgs/development/compilers/rust/rustc.nix @@ -197,18 +197,6 @@ in stdenv.mkDerivation (finalAttrs: { # Useful debugging parameter # export VERBOSE=1 - '' + lib.optionalString (stdenv.targetPlatform.isMusl && !stdenv.targetPlatform.isStatic) '' - # 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 - substituteInPlace compiler/rustc_target/src/spec/base/linux_musl.rs \ - --replace "base.crt_static_default = true" "base.crt_static_default = false" '' + lib.optionalString (stdenv.isDarwin && stdenv.isx86_64) '' # See https://github.com/jemalloc/jemalloc/issues/1997 # Using a value of 48 should work on both emulated and native x86_64-darwin.