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.

224 lines
5.5 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,
}:
{
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 ? { },
# Toggles whether a custom sysroot is created when the target is a .json file.
__internal_dontAddSysroot ? false,
# 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:
assert
cargoVendorDir == null && cargoDeps == null && cargoLock == null
->
!(args ? cargoSha256 && args.cargoSha256 != null) && !(args ? cargoHash && args.cargoHash != null)
-> throw "cargoHash, cargoVendorDir, cargoDeps, or cargoLock must be set";
2018-02-20 09:59:26 +00:00
let
cargoDeps' =
if cargoVendorDir != null then
null
else if cargoDeps != null then
cargoDeps
else if cargoLock != null then
importCargoLock cargoLock
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;
2020-10-17 00:45:27 -07:00
useSysroot = targetIsJSON && !__internal_dontAddSysroot;
sysroot = callPackage ./sysroot { } {
inherit target;
shortTarget = stdenv.hostPlatform.rust.cargoShortTarget;
RUSTFLAGS = args.RUSTFLAGS or "";
originalCargoToml = src + /Cargo.toml; # profile info is later extracted
};
in
2020-10-17 00:47:14 -07:00
# Tests don't currently work for `no_std`, and all custom sysroots are currently built without `std`.
# See https://os.phil-opp.com/testing/ for more information.
assert useSysroot -> !(args.doCheck or true);
stdenv.mkDerivation (
(removeAttrs args [
"depsExtraArgs"
"cargoUpdateHook"
"cargoDeps"
"cargoLock"
])
// lib.optionalAttrs useSysroot {
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
}
// lib.optionalAttrs (stdenv.hostPlatform.isDarwin && buildType == "debug") {
RUSTFLAGS =
"-C split-debuginfo=packed "
+ lib.optionalString useSysroot "--sysroot ${sysroot} "
+ (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
];
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;
};
}
)