1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-26 19:16:47 +03:00
nixpkgs/pkgs/build-support/rust/build-rust-package/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
4.7 KiB
Nix
Raw Normal View History

{
lib,
importCargoLock,
fetchCargoTarball,
fetchCargoVendor,
stdenv,
callPackage,
cargoBuildHook,
cargoCheckHook,
cargoInstallHook,
cargoNextestHook,
cargoSetupHook,
cargo,
cargo-auditable,
buildPackages,
rustc,
2021-05-07 23:36:21 +02:00
libiconv,
windows,
}:
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
excludeDrvArgNames = [
"depsExtraArgs"
"cargoUpdateHook"
"cargoDeps"
"cargoLock"
];
extendDrvArgs =
finalAttrs:
{
name ? "${args.pname}-${args.version}",
# Name for the vendored dependencies tarball
cargoDepsName ? name,
src ? null,
srcs ? null,
preUnpack ? null,
unpackPhase ? null,
postUnpack ? null,
cargoPatches ? [ ],
patches ? [ ],
sourceRoot ? null,
cargoRoot ? null,
logLevel ? "",
buildInputs ? [ ],
2018-11-21 01:47:45 +00:00
nativeBuildInputs ? [ ],
cargoUpdateHook ? "",
cargoDepsHook ? "",
buildType ? "release",
meta ? { },
useFetchCargoVendor ? false,
cargoDeps ? null,
cargoLock ? null,
2018-02-20 09:59:26 +00:00
cargoVendorDir ? null,
checkType ? buildType,
2021-10-26 22:41:37 -04:00
buildNoDefaultFeatures ? false,
checkNoDefaultFeatures ? buildNoDefaultFeatures,
buildFeatures ? [ ],
checkFeatures ? buildFeatures,
useNextest ? false,
auditable ? !cargo-auditable.meta.broken,
2020-09-23 06:01:05 -04:00
depsExtraArgs ? { },
# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
# Otherwise, everything from the tarball would've been built/tested.
buildAndTestSubdir ? null,
...
}@args:
let
cargoDeps' =
if cargoVendorDir != null then
null
else if cargoDeps != null then
cargoDeps
else if cargoLock != null then
importCargoLock cargoLock
else if (args.cargoHash or null == null) && (args.cargoSha256 or null == null) then
throw "cargoHash, cargoVendorDir, cargoDeps, or cargoLock must be set"
else if useFetchCargoVendor then
fetchCargoVendor (
{
inherit
src
srcs
sourceRoot
cargoRoot
preUnpack
unpackPhase
postUnpack
;
name = cargoDepsName;
patches = cargoPatches;
hash = args.cargoHash;
}
// depsExtraArgs
)
else
fetchCargoTarball (
{
inherit
src
srcs
sourceRoot
cargoRoot
preUnpack
unpackPhase
postUnpack
cargoUpdateHook
;
name = cargoDepsName;
patches = cargoPatches;
}
// lib.optionalAttrs (args ? cargoHash) {
hash = args.cargoHash;
}
// lib.optionalAttrs (args ? cargoSha256) {
sha256 = lib.warn "cargoSha256 is deprecated. Please use cargoHash with SRI hash instead" args.cargoSha256;
}
// depsExtraArgs
);
2018-02-20 09:59:26 +00:00
target = stdenv.hostPlatform.rust.rustcTargetSpec;
2021-01-24 01:40:18 +01:00
targetIsJSON = lib.hasSuffix ".json" target;
in
lib.optionalAttrs (stdenv.hostPlatform.isDarwin && buildType == "debug") {
RUSTFLAGS = "-C split-debuginfo=packed " + (args.RUSTFLAGS or "");
}
// {
cargoDeps = cargoDeps';
inherit buildAndTestSubdir;
cargoBuildType = buildType;
cargoCheckType = checkType;
2021-10-26 22:41:37 -04:00
cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;
2021-10-26 22:41:37 -04:00
cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;
2021-10-26 22:41:37 -04:00
cargoBuildFeatures = buildFeatures;
2021-10-26 22:41:37 -04:00
cargoCheckFeatures = checkFeatures;
2022-12-07 00:18:58 -05:00
nativeBuildInputs =
nativeBuildInputs
++ lib.optionals auditable [
(buildPackages.cargo-auditable-cargo-wrapper.override {
inherit cargo cargo-auditable;
2022-12-07 00:18:58 -05:00
})
]
++ [
cargoBuildHook
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
rustc
rust: fix splicing for rust hooks This fixes a long standing issue where rust hooks behave differently when used inside buildRustPackage vs inside mkDerivation, which lead to surprising behavior, like for example the package being built for the wrong paltform or the linker not being found especially in cross compilation scenarios. The reason for this inconsitency was, that buildRustPackage consumed the hooks in a non-spliced form, via [this inherit statement](https://github.com/NixOS/nixpkgs/blob/4506ece030a0c82d078edd0360ea8af6b4d94035/pkgs/development/compilers/rust/make-rust-platform.nix#L60), and therefore the usual platform shift on the hooks introduced by putting them in `nativeBuildInputs` was not applied here. Thoug whenever the hook was used inside other builders like `mkDerivation` the platform shift did apply correctly as the hook was consumed via the spliced package set, introducing the inconsitecy. Because of the wrong (non-spliced) use in buildRustPackage, most rust hooks have been designed with the wrong build/host/target shift in mind which is fixed by this change. Due to the inconsitent behavior between different builders, workarounds like `rust.envVars`, which were previously introduced, likely become obsolete by this change. This likely fixes a bunch of cross compilation issues for rust packages that are not based on `buildRustPackage` but instead consume the hooks directly. Done: - ensure that `buildRustPackage` consumes spliced hooks by using makeScopeWithSplicing' in make-rust-platform.nix. - refactor hooks to make them refer to correct build/host/target packages. - remove `rust.envVars` workaround from all rust hooks - implement tests for most rust hooks in /pkgs/test/rut-hooks The newly added tests can be executed for native as well as cross compilation via: ``` nix-build -A tests.rust-hooks -A pkgsCross.riscv64.tests.rust-hooks ```
2025-01-01 02:40:34 +07:00
cargo
];
2021-05-07 23:36:21 +02:00
buildInputs =
buildInputs
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
2021-05-07 23:36:21 +02:00
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];
patches = cargoPatches ++ patches;
PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
postUnpack =
''
eval "$cargoDepsHook"
export RUST_LOG=${logLevel}
''
+ (args.postUnpack or "");
configurePhase =
args.configurePhase or ''
runHook preConfigure
2018-11-21 01:47:45 +00:00
runHook postConfigure
'';
doCheck = args.doCheck or true;
strictDeps = true;
meta = meta // {
badPlatforms = meta.badPlatforms or [ ] ++ rustc.badTargetPlatforms;
# default to Rust's platforms
platforms = lib.intersectLists meta.platforms or lib.platforms.all rustc.targetPlatforms;
};
};
}