2021-10-20 01:59:28 +02:00
|
|
|
{ lib
|
|
|
|
, importCargoLock
|
|
|
|
, fetchCargoTarball
|
|
|
|
, rust
|
|
|
|
, stdenv
|
|
|
|
, callPackage
|
2020-05-07 03:03:41 +02:00
|
|
|
, cacert
|
2021-10-20 01:59:28 +02:00
|
|
|
, git
|
2021-02-11 17:32:47 +01:00
|
|
|
, cargoBuildHook
|
2021-02-15 10:26:40 +01:00
|
|
|
, cargoCheckHook
|
2021-02-15 06:54:18 +01:00
|
|
|
, cargoInstallHook
|
2021-02-09 11:38:25 +01:00
|
|
|
, cargoSetupHook
|
2020-05-07 03:03:41 +02:00
|
|
|
, rustc
|
2021-05-07 23:36:21 +02:00
|
|
|
, libiconv
|
2020-05-07 03:03:41 +02:00
|
|
|
, windows
|
|
|
|
}:
|
2018-11-21 13:38:49 +01:00
|
|
|
|
2019-03-02 03:45:12 +01:00
|
|
|
{ name ? "${args.pname}-${args.version}"
|
2020-11-08 08:47:12 +01:00
|
|
|
|
2021-02-15 07:06:31 +01:00
|
|
|
# Name for the vendored dependencies tarball
|
|
|
|
, cargoDepsName ? name
|
|
|
|
|
2015-05-29 19:35:31 +02:00
|
|
|
, src ? null
|
|
|
|
, srcs ? null
|
2019-12-02 22:24:40 +01:00
|
|
|
, unpackPhase ? null
|
2018-08-13 07:44:30 +02:00
|
|
|
, cargoPatches ? []
|
|
|
|
, patches ? []
|
2015-05-29 19:35:31 +02:00
|
|
|
, sourceRoot ? null
|
2016-05-28 15:03:59 +02:00
|
|
|
, logLevel ? ""
|
2015-05-29 19:35:31 +02:00
|
|
|
, buildInputs ? []
|
2018-11-21 02:47:45 +01:00
|
|
|
, nativeBuildInputs ? []
|
2015-05-29 19:35:31 +02:00
|
|
|
, cargoUpdateHook ? ""
|
2016-12-03 23:36:48 +01:00
|
|
|
, cargoDepsHook ? ""
|
2019-02-26 06:52:01 +01:00
|
|
|
, buildType ? "release"
|
2019-07-21 07:00:00 +02:00
|
|
|
, meta ? {}
|
2021-05-08 07:44:31 +02:00
|
|
|
, cargoLock ? null
|
2018-02-20 10:59:26 +01:00
|
|
|
, cargoVendorDir ? null
|
2020-05-13 01:15:23 +02:00
|
|
|
, checkType ? buildType
|
2021-10-27 04:41:37 +02:00
|
|
|
, buildNoDefaultFeatures ? false
|
|
|
|
, checkNoDefaultFeatures ? buildNoDefaultFeatures
|
|
|
|
, buildFeatures ? [ ]
|
|
|
|
, checkFeatures ? buildFeatures
|
2020-09-23 12:01:05 +02:00
|
|
|
, depsExtraArgs ? {}
|
2020-09-09 13:39:23 +02:00
|
|
|
|
2020-10-08 23:32:49 +02:00
|
|
|
# Toggles whether a custom sysroot is created when the target is a .json file.
|
|
|
|
, __internal_dontAddSysroot ? false
|
|
|
|
|
2020-05-13 01:28:24 +02:00
|
|
|
# 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
|
2015-05-29 19:35:31 +02:00
|
|
|
, ... } @ args:
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2021-10-19 01:21:43 +02:00
|
|
|
assert cargoVendorDir == null && cargoLock == null -> !(args ? cargoSha256) && !(args ? cargoHash)
|
2021-05-08 07:44:31 +02:00
|
|
|
-> throw "cargoSha256, cargoHash, cargoVendorDir, or cargoLock must be set";
|
2019-02-26 06:52:01 +01:00
|
|
|
assert buildType == "release" || buildType == "debug";
|
2018-02-20 10:59:26 +01:00
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
let
|
2020-01-12 17:21:23 +01:00
|
|
|
|
2021-05-08 07:44:31 +02:00
|
|
|
cargoDeps =
|
|
|
|
if cargoVendorDir == null
|
|
|
|
then if cargoLock != null then importCargoLock cargoLock
|
|
|
|
else fetchCargoTarball ({
|
|
|
|
inherit src srcs sourceRoot unpackPhase cargoUpdateHook;
|
|
|
|
name = cargoDepsName;
|
|
|
|
patches = cargoPatches;
|
2021-10-19 01:21:43 +02:00
|
|
|
} // lib.optionalAttrs (args ? cargoHash) {
|
|
|
|
hash = args.cargoHash;
|
|
|
|
} // lib.optionalAttrs (args ? cargoSha256) {
|
|
|
|
sha256 = args.cargoSha256;
|
2021-05-08 07:44:31 +02:00
|
|
|
} // depsExtraArgs)
|
2018-02-20 10:59:26 +01:00
|
|
|
else null;
|
|
|
|
|
2020-03-19 01:43:07 +01:00
|
|
|
# If we have a cargoSha256 fixed-output derivation, validate it at build time
|
|
|
|
# against the src fixed-output derivation to check consistency.
|
2021-10-19 01:21:43 +02:00
|
|
|
validateCargoDeps = args ? cargoHash || args ? cargoSha256;
|
2020-01-12 17:21:23 +01:00
|
|
|
|
2021-02-11 17:32:47 +01:00
|
|
|
target = rust.toRustTargetSpec stdenv.hostPlatform;
|
2021-01-24 01:40:18 +01:00
|
|
|
targetIsJSON = lib.hasSuffix ".json" target;
|
2020-10-17 09:45:27 +02:00
|
|
|
useSysroot = targetIsJSON && !__internal_dontAddSysroot;
|
2020-10-08 23:32:49 +02:00
|
|
|
|
|
|
|
# see https://github.com/rust-lang/cargo/blob/964a16a28e234a3d397b2a7031d4ab4a428b1391/src/cargo/core/compiler/compile_kind.rs#L151-L168
|
|
|
|
# the "${}" is needed to transform the path into a /nix/store path before baseNameOf
|
|
|
|
shortTarget = if targetIsJSON then
|
2021-01-24 01:40:18 +01:00
|
|
|
(lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
|
2020-10-08 23:32:49 +02:00
|
|
|
else target;
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2021-10-20 01:59:28 +02:00
|
|
|
sysroot = callPackage ./sysroot { } {
|
2020-10-08 23:32:49 +02:00
|
|
|
inherit target shortTarget;
|
|
|
|
RUSTFLAGS = args.RUSTFLAGS or "";
|
|
|
|
originalCargoToml = src + /Cargo.toml; # profile info is later extracted
|
|
|
|
};
|
2019-08-14 11:13:19 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
|
2020-10-17 09:47:14 +02: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);
|
|
|
|
|
2021-05-08 07:44:31 +02:00
|
|
|
stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoLock" ]) // lib.optionalAttrs useSysroot {
|
2020-10-17 09:48:38 +02:00
|
|
|
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
|
|
|
|
} // {
|
2021-02-15 06:54:18 +01:00
|
|
|
inherit buildAndTestSubdir cargoDeps;
|
2021-02-11 17:32:47 +01:00
|
|
|
|
2021-02-15 06:54:18 +01:00
|
|
|
cargoBuildType = buildType;
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2021-02-26 11:51:31 +01:00
|
|
|
cargoCheckType = checkType;
|
|
|
|
|
2021-10-27 04:41:37 +02:00
|
|
|
cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;
|
|
|
|
|
|
|
|
cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;
|
|
|
|
|
|
|
|
cargoBuildFeatures = buildFeatures;
|
|
|
|
|
|
|
|
cargoCheckFeatures = checkFeatures;
|
|
|
|
|
2015-04-23 16:37:52 +02:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
2021-02-15 10:26:40 +01:00
|
|
|
nativeBuildInputs = nativeBuildInputs ++ [
|
|
|
|
cacert
|
|
|
|
git
|
|
|
|
cargoBuildHook
|
|
|
|
cargoCheckHook
|
|
|
|
cargoInstallHook
|
|
|
|
cargoSetupHook
|
|
|
|
rustc
|
|
|
|
];
|
2021-02-11 17:32:47 +01:00
|
|
|
|
2021-05-07 23:36:21 +02:00
|
|
|
buildInputs = buildInputs
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2018-08-13 07:44:30 +02:00
|
|
|
patches = cargoPatches ++ patches;
|
|
|
|
|
2019-03-12 14:07:36 +01:00
|
|
|
PKG_CONFIG_ALLOW_CROSS =
|
|
|
|
if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
|
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
postUnpack = ''
|
2016-12-03 23:36:48 +01:00
|
|
|
eval "$cargoDepsHook"
|
|
|
|
|
2019-07-25 18:48:18 +02:00
|
|
|
export RUST_LOG=${logLevel}
|
2020-02-16 08:33:02 +01:00
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
2019-07-25 18:48:18 +02:00
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
2018-11-21 02:47:45 +01:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2015-04-21 20:34:26 +02:00
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2020-03-18 14:50:12 +01:00
|
|
|
strictDeps = true;
|
|
|
|
|
2017-10-26 18:43:17 +02:00
|
|
|
passthru = { inherit cargoDeps; } // (args.passthru or {});
|
2019-07-21 07:00:00 +02:00
|
|
|
|
|
|
|
meta = {
|
|
|
|
# default to Rust's platforms
|
|
|
|
platforms = rustc.meta.platforms;
|
|
|
|
} // meta;
|
2014-10-10 16:59:37 +02:00
|
|
|
})
|