2020-05-15 21:57:56 +00:00
|
|
|
{ stdenv
|
2021-02-05 17:12:51 +00:00
|
|
|
, lib
|
2020-05-15 21:57:56 +00:00
|
|
|
, buildPackages
|
|
|
|
, cacert
|
2021-03-09 03:18:52 +00:00
|
|
|
, cargoBuildHook
|
|
|
|
, cargoCheckHook
|
|
|
|
, cargoInstallHook
|
|
|
|
, cargoSetupHook
|
2020-05-15 21:57:56 +00:00
|
|
|
, fetchCargoTarball
|
2020-11-30 08:33:03 +00:00
|
|
|
, runCommandNoCC
|
|
|
|
, rustPlatform
|
|
|
|
, callPackage
|
|
|
|
, remarshal
|
2020-05-15 21:57:56 +00:00
|
|
|
, git
|
|
|
|
, rust
|
|
|
|
, rustc
|
2021-05-28 09:39:13 +00:00
|
|
|
, libiconv
|
2020-05-15 21:57:56 +00:00
|
|
|
, windows
|
|
|
|
}:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
{ name ? "${args.pname}-${args.version}"
|
2021-01-05 17:05:55 +00:00
|
|
|
|
|
|
|
# SRI hash
|
|
|
|
, cargoHash ? ""
|
|
|
|
|
|
|
|
# Legacy hash
|
|
|
|
, cargoSha256 ? ""
|
|
|
|
|
2021-02-24 18:30:23 +00:00
|
|
|
# Name for the vendored dependencies tarball
|
|
|
|
, cargoDepsName ? name
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
, src ? null
|
|
|
|
, srcs ? null
|
|
|
|
, unpackPhase ? null
|
|
|
|
, cargoPatches ? []
|
|
|
|
, patches ? []
|
|
|
|
, sourceRoot ? null
|
|
|
|
, logLevel ? ""
|
|
|
|
, buildInputs ? []
|
|
|
|
, nativeBuildInputs ? []
|
|
|
|
, cargoUpdateHook ? ""
|
|
|
|
, cargoDepsHook ? ""
|
|
|
|
, buildType ? "release"
|
|
|
|
, meta ? {}
|
|
|
|
, cargoVendorDir ? null
|
2020-06-18 07:06:33 +00:00
|
|
|
, checkType ? buildType
|
2020-09-25 04:45:31 +00:00
|
|
|
, depsExtraArgs ? {}
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2020-11-30 08:33:03 +00:00
|
|
|
# Toggles whether a custom sysroot is created when the target is a .json file.
|
|
|
|
, __internal_dontAddSysroot ? false
|
|
|
|
|
2020-06-18 07:06:33 +00: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
|
2020-04-24 23:36:52 +00:00
|
|
|
, ... } @ args:
|
|
|
|
|
2021-01-05 17:05:55 +00:00
|
|
|
assert cargoVendorDir == null -> !(cargoSha256 == "" && cargoHash == "");
|
2020-04-24 23:36:52 +00:00
|
|
|
assert buildType == "release" || buildType == "debug";
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cargoDeps = if cargoVendorDir == null
|
2020-09-25 04:45:31 +00:00
|
|
|
then fetchCargoTarball ({
|
2021-02-24 18:30:23 +00:00
|
|
|
inherit src srcs sourceRoot unpackPhase cargoUpdateHook;
|
|
|
|
name = cargoDepsName;
|
2021-01-05 17:05:55 +00:00
|
|
|
hash = cargoHash;
|
2020-04-24 23:36:52 +00:00
|
|
|
patches = cargoPatches;
|
|
|
|
sha256 = cargoSha256;
|
2020-09-25 04:45:31 +00:00
|
|
|
} // depsExtraArgs)
|
2020-04-24 23:36:52 +00:00
|
|
|
else null;
|
|
|
|
|
|
|
|
# If we have a cargoSha256 fixed-output derivation, validate it at build time
|
|
|
|
# against the src fixed-output derivation to check consistency.
|
2021-01-05 17:05:55 +00:00
|
|
|
validateCargoDeps = !(cargoHash == "" && cargoSha256 == "");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
target = rust.toRustTargetSpec stdenv.hostPlatform;
|
2021-02-05 17:12:51 +00:00
|
|
|
targetIsJSON = lib.hasSuffix ".json" target;
|
2020-11-30 08:33:03 +00:00
|
|
|
useSysroot = targetIsJSON && !__internal_dontAddSysroot;
|
|
|
|
|
|
|
|
# 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-02-05 17:12:51 +00:00
|
|
|
(lib.removeSuffix ".json" (builtins.baseNameOf "${target}"))
|
2020-11-30 08:33:03 +00:00
|
|
|
else target;
|
|
|
|
|
|
|
|
sysroot = (callPackage ./sysroot {}) {
|
|
|
|
inherit target shortTarget;
|
|
|
|
RUSTFLAGS = args.RUSTFLAGS or "";
|
|
|
|
originalCargoToml = src + /Cargo.toml; # profile info is later extracted
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
|
2020-11-30 08:33:03 +00: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-02-05 17:12:51 +00:00
|
|
|
stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs useSysroot {
|
2020-11-30 08:33:03 +00:00
|
|
|
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
|
|
|
|
} // {
|
2021-03-09 03:18:52 +00:00
|
|
|
inherit buildAndTestSubdir cargoDeps;
|
|
|
|
|
|
|
|
cargoBuildType = buildType;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-03-19 17:17:44 +00:00
|
|
|
cargoCheckType = checkType;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
nativeBuildInputs = nativeBuildInputs ++ [
|
|
|
|
cacert
|
|
|
|
git
|
|
|
|
cargoBuildHook
|
|
|
|
cargoCheckHook
|
|
|
|
cargoInstallHook
|
|
|
|
cargoSetupHook
|
|
|
|
rustc
|
|
|
|
];
|
|
|
|
|
2021-05-28 09:39:13 +00:00
|
|
|
buildInputs = buildInputs
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
|
|
|
|
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
|
|
|
strictDeps = true;
|
|
|
|
|
|
|
|
passthru = { inherit cargoDeps; } // (args.passthru or {});
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
# default to Rust's platforms
|
|
|
|
platforms = rustc.meta.platforms;
|
|
|
|
} // meta;
|
|
|
|
})
|