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.
This commit is contained in:
Alyssa Ross 2024-02-28 23:10:07 +01:00 committed by GitHub
parent 5134b74edf
commit 79156bf13a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 49 deletions

View File

@ -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 = {

View File

@ -13,7 +13,7 @@ for arg; do
esac
done
extraBefore=("${defaultSysroot[@]}")
extraBefore=(@defaultArgs@ "${defaultSysroot[@]}")
extraAfter=($NIX_RUSTFLAGS)
# Optionally print debug info.

View File

@ -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 <<EOF >> .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;

View File

@ -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.