rust: fix overriding rust flags on musl
If RUSTFLAGS is set in the environment, Cargo will ignore rustflags
settings in its TOML configuration. So setting RUSTFLAGS=-g (like
separateDebugInfo does) to generate debug info breaks
dynamically-linked Rust packages on musl. This breakage is visible
for any packages that call into C dynamic libraries. If the binary is
linked directly to a C dynamic library, it will fail to build, and if
it depends on a Rust library which links a C dynamic library, it will
segfault at runtime when it tries to call a function from the C
library. I noticed this because pkgsMusl.crosvm is broken for this
reason, since it sets separateDebugInfo = true.
It shouldn't be possible to end up with broken binaries just by using
RUSTFLAGS to do something innocuous like enable debug info, so I think
that, even though we liked the approach of modiyfing .cargo/config
better at the time, it's become clear that it's too brittle, and we
should bite the bullet and patch the compiler instead when targetting
musl. It does not appear to be necessary to modify the compiler at
all when cross-compiling /from/ dynamically-linked Musl to another
target, so I'm only checking whether the target system is
dynamically-linked Musl when deciding whether to make the modification
to the compiler.
This reverts commit c2eaaae50d66a933e38256bdf5a3ae43430592a3
("cargoSetupHook: pass host config flags"), and implements the
compiler patching approach instead.
2023-03-16 00:09:35 +00:00
|
|
|
{ lib, stdenv, pkgsBuildHost, pkgsHostHost
|
2021-09-10 09:26:04 +00:00
|
|
|
, file, curl, pkg-config, python3, openssl, cmake, zlib
|
2023-04-22 19:53:23 +00:00
|
|
|
, installShellFiles, makeWrapper, rustPlatform, rust, rustc
|
2022-12-15 23:38:33 +00:00
|
|
|
, CoreFoundation, Security
|
2023-04-22 19:52:06 +00:00
|
|
|
, auditable ? !cargo-auditable.meta.broken
|
2023-03-27 00:34:07 +00:00
|
|
|
, cargo-auditable
|
2023-04-22 19:53:23 +00:00
|
|
|
, pkgsBuildBuild
|
2018-11-21 01:47:45 +00:00
|
|
|
}:
|
2017-11-05 10:13:49 +00:00
|
|
|
|
2023-04-22 19:53:23 +00:00
|
|
|
rustPlatform.buildRustPackage.override {
|
2023-03-27 00:34:07 +00:00
|
|
|
cargo-auditable = cargo-auditable.bootstrap;
|
2023-04-22 19:53:23 +00:00
|
|
|
} ({
|
2022-03-01 10:52:52 +00:00
|
|
|
pname = "cargo";
|
2018-11-21 01:47:45 +00:00
|
|
|
inherit (rustc) version src;
|
2016-06-07 18:42:51 +00:00
|
|
|
|
2018-02-20 09:59:26 +00:00
|
|
|
# the rust source tarball already has all the dependencies vendored, no need to fetch them again
|
2019-01-19 14:47:18 +00:00
|
|
|
cargoVendorDir = "vendor";
|
2020-05-12 23:28:24 +00:00
|
|
|
buildAndTestSubdir = "src/tools/cargo";
|
2016-06-07 18:42:51 +00:00
|
|
|
|
2022-12-05 01:52:06 +00:00
|
|
|
inherit auditable;
|
|
|
|
|
2022-08-23 01:10:09 +00:00
|
|
|
passthru = {
|
|
|
|
rustc = rustc;
|
|
|
|
inherit (rustc) tests;
|
|
|
|
};
|
2016-06-07 18:42:51 +00:00
|
|
|
|
rust: fix overriding rust flags on musl
If RUSTFLAGS is set in the environment, Cargo will ignore rustflags
settings in its TOML configuration. So setting RUSTFLAGS=-g (like
separateDebugInfo does) to generate debug info breaks
dynamically-linked Rust packages on musl. This breakage is visible
for any packages that call into C dynamic libraries. If the binary is
linked directly to a C dynamic library, it will fail to build, and if
it depends on a Rust library which links a C dynamic library, it will
segfault at runtime when it tries to call a function from the C
library. I noticed this because pkgsMusl.crosvm is broken for this
reason, since it sets separateDebugInfo = true.
It shouldn't be possible to end up with broken binaries just by using
RUSTFLAGS to do something innocuous like enable debug info, so I think
that, even though we liked the approach of modiyfing .cargo/config
better at the time, it's become clear that it's too brittle, and we
should bite the bullet and patch the compiler instead when targetting
musl. It does not appear to be necessary to modify the compiler at
all when cross-compiling /from/ dynamically-linked Musl to another
target, so I'm only checking whether the target system is
dynamically-linked Musl when deciding whether to make the modification
to the compiler.
This reverts commit c2eaaae50d66a933e38256bdf5a3ae43430592a3
("cargoSetupHook: pass host config flags"), and implements the
compiler patching approach instead.
2023-03-16 00:09:35 +00:00
|
|
|
# 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
|
|
|
|
'';
|
|
|
|
|
2018-08-14 07:40:47 +00:00
|
|
|
# changes hash of vendor directory otherwise
|
|
|
|
dontUpdateAutotoolsGnuConfigScripts = true;
|
2018-02-22 11:46:30 +00:00
|
|
|
|
2021-09-10 09:26:04 +00:00
|
|
|
nativeBuildInputs = [
|
|
|
|
pkg-config cmake installShellFiles makeWrapper
|
|
|
|
(lib.getDev pkgsHostHost.curl)
|
2023-01-11 20:06:22 +00:00
|
|
|
zlib
|
2021-09-10 09:26:04 +00:00
|
|
|
];
|
2023-01-31 14:28:06 +00:00
|
|
|
buildInputs = [ file curl python3 openssl zlib ]
|
2022-12-15 23:38:33 +00:00
|
|
|
++ lib.optionals stdenv.isDarwin [ CoreFoundation Security ];
|
2016-08-08 13:55:26 +00:00
|
|
|
|
2020-02-12 16:00:19 +00:00
|
|
|
# cargo uses git-rs which is made for a version of libgit2 from recent master that
|
|
|
|
# is not compatible with the current version in nixpkgs.
|
|
|
|
#LIBGIT2_SYS_USE_PKG_CONFIG = 1;
|
2016-06-07 18:42:51 +00:00
|
|
|
|
2018-09-11 21:20:20 +00:00
|
|
|
# fixes: the cargo feature `edition` requires a nightly version of Cargo, but this is the `stable` channel
|
2019-03-12 01:19:48 +00:00
|
|
|
RUSTC_BOOTSTRAP = 1;
|
2018-09-11 21:20:20 +00:00
|
|
|
|
2016-06-07 18:42:51 +00:00
|
|
|
postInstall = ''
|
2023-01-31 14:28:06 +00:00
|
|
|
wrapProgram "$out/bin/cargo" --suffix PATH : "${rustc}/bin"
|
2020-02-11 17:52:11 +00:00
|
|
|
|
|
|
|
installManPage src/tools/cargo/src/etc/man/*
|
2020-06-26 22:56:11 +00:00
|
|
|
|
|
|
|
installShellCompletion --bash --name cargo \
|
|
|
|
src/tools/cargo/src/etc/cargo.bashcomp.sh
|
|
|
|
|
|
|
|
installShellCompletion --zsh src/tools/cargo/src/etc/_cargo
|
2016-06-07 18:42:51 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
checkPhase = ''
|
|
|
|
# Disable cross compilation tests
|
|
|
|
export CFG_DISABLE_CROSS_TESTS=1
|
|
|
|
cargo test
|
|
|
|
'';
|
|
|
|
|
2016-12-29 07:56:19 +00:00
|
|
|
# Disable check phase as there are failures (4 tests fail)
|
2016-06-07 18:42:51 +00:00
|
|
|
doCheck = false;
|
|
|
|
|
2021-09-10 09:26:04 +00:00
|
|
|
doInstallCheck = !stdenv.hostPlatform.isStatic &&
|
|
|
|
stdenv.hostPlatform.parsed.kernel.execFormat == lib.systems.parse.execFormats.elf;
|
|
|
|
installCheckPhase = ''
|
|
|
|
runHook preInstallCheck
|
|
|
|
readelf -a $out/bin/.cargo-wrapped | grep -F 'Shared library: [libcurl.so'
|
|
|
|
runHook postInstallCheck
|
|
|
|
'';
|
|
|
|
|
2021-01-22 11:25:31 +00:00
|
|
|
meta = with lib; {
|
2020-04-01 01:11:51 +00:00
|
|
|
homepage = "https://crates.io";
|
2016-06-07 18:42:51 +00:00
|
|
|
description = "Downloads your Rust project's dependencies and builds your project";
|
2023-04-03 00:24:55 +00:00
|
|
|
maintainers = teams.rust.members;
|
2016-06-07 18:42:51 +00:00
|
|
|
license = [ licenses.mit licenses.asl20 ];
|
2018-02-03 11:51:03 +00:00
|
|
|
platforms = platforms.unix;
|
2016-06-07 18:42:51 +00:00
|
|
|
};
|
|
|
|
}
|
2023-04-22 19:53:23 +00:00
|
|
|
// lib.optionalAttrs (rust.toRustTarget stdenv.buildPlatform != rust.toRustTarget stdenv.hostPlatform) {
|
|
|
|
HOST_PKG_CONFIG_PATH="${pkgsBuildBuild.pkg-config}/bin/pkg-config";
|
|
|
|
})
|