2020-05-07 03:03:41 +02:00
|
|
|
{ stdenv
|
2021-01-24 01:40:18 +01:00
|
|
|
, lib
|
2020-05-07 03:03:41 +02:00
|
|
|
, buildPackages
|
|
|
|
, cacert
|
|
|
|
, cargo
|
|
|
|
, diffutils
|
|
|
|
, fetchCargoTarball
|
2020-10-08 23:32:49 +02:00
|
|
|
, runCommandNoCC
|
|
|
|
, rustPlatform
|
|
|
|
, callPackage
|
|
|
|
, remarshal
|
2020-05-07 03:03:41 +02:00
|
|
|
, git
|
|
|
|
, rust
|
|
|
|
, rustc
|
|
|
|
, 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
|
|
|
|
|
|
|
# SRI hash
|
|
|
|
, cargoHash ? ""
|
|
|
|
|
|
|
|
# Legacy hash
|
|
|
|
, cargoSha256 ? ""
|
|
|
|
|
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 ? ""
|
2017-04-15 11:14:55 +02:00
|
|
|
, cargoBuildFlags ? []
|
2019-02-26 06:52:01 +01:00
|
|
|
, buildType ? "release"
|
2019-07-21 07:00:00 +02:00
|
|
|
, meta ? {}
|
2020-10-14 05:37:29 +02:00
|
|
|
, target ? rust.toRustTargetSpec stdenv.hostPlatform
|
2018-02-20 10:59:26 +01:00
|
|
|
, cargoVendorDir ? null
|
2020-05-13 01:15:23 +02:00
|
|
|
, checkType ? buildType
|
2020-09-23 12:01:05 +02:00
|
|
|
, depsExtraArgs ? {}
|
2020-09-09 13:39:23 +02:00
|
|
|
, cargoParallelTestThreads ? true
|
|
|
|
|
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
|
|
|
|
2020-11-08 08:47:12 +01:00
|
|
|
assert cargoVendorDir == null -> !(cargoSha256 == "" && cargoHash == "");
|
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
|
|
|
|
2018-02-20 10:59:26 +01:00
|
|
|
cargoDeps = if cargoVendorDir == null
|
2020-09-23 12:01:05 +02:00
|
|
|
then fetchCargoTarball ({
|
2019-12-02 22:24:40 +01:00
|
|
|
inherit name src srcs sourceRoot unpackPhase cargoUpdateHook;
|
2020-11-08 08:47:12 +01:00
|
|
|
hash = cargoHash;
|
2018-08-13 07:44:30 +02:00
|
|
|
patches = cargoPatches;
|
2018-02-20 10:59:26 +01:00
|
|
|
sha256 = cargoSha256;
|
2020-09-23 12:01:05 +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.
|
2020-11-08 08:47:12 +01:00
|
|
|
validateCargoDeps = !(cargoHash == "" && cargoSha256 == "");
|
2020-01-12 17:21:23 +01:00
|
|
|
|
2020-02-15 01:51:05 +01:00
|
|
|
# Some cargo builds include build hooks that modify their own vendor
|
|
|
|
# dependencies. This copies the vendor directory into the build tree and makes
|
|
|
|
# it writable. If we're using a tarball, the unpackFile hook already handles
|
|
|
|
# this for us automatically.
|
2018-02-20 10:59:26 +01:00
|
|
|
setupVendorDir = if cargoVendorDir == null
|
2020-02-15 01:51:05 +01:00
|
|
|
then (''
|
2018-02-20 10:59:26 +01:00
|
|
|
unpackFile "$cargoDeps"
|
2020-01-12 17:21:23 +01:00
|
|
|
cargoDepsCopy=$(stripHash $cargoDeps)
|
2020-02-15 01:51:05 +01:00
|
|
|
'')
|
2018-02-20 10:59:26 +01:00
|
|
|
else ''
|
|
|
|
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
|
|
|
|
'';
|
2020-10-13 03:41:13 +02:00
|
|
|
|
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
|
|
|
|
2020-10-08 23:32:49 +02:00
|
|
|
sysroot = (callPackage ./sysroot {}) {
|
|
|
|
inherit target shortTarget;
|
|
|
|
RUSTFLAGS = args.RUSTFLAGS or "";
|
|
|
|
originalCargoToml = src + /Cargo.toml; # profile info is later extracted
|
|
|
|
};
|
2019-08-14 11:13:19 +02:00
|
|
|
|
2018-11-21 02:47:45 +01:00
|
|
|
ccForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc";
|
|
|
|
cxxForBuild="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}c++";
|
|
|
|
ccForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
|
|
|
|
cxxForHost="${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++";
|
2020-10-08 23:32:49 +02:00
|
|
|
releaseDir = "target/${shortTarget}/${buildType}";
|
2020-07-14 17:32:06 +02:00
|
|
|
tmpDir = "${releaseDir}-tmp";
|
2020-01-12 17:21:23 +01:00
|
|
|
|
2020-05-07 03:03:41 +02:00
|
|
|
# Specify the stdenv's `diff` by abspath to ensure that the user's build
|
|
|
|
# inputs do not cause us to find the wrong `diff`.
|
2020-06-07 19:51:13 +02:00
|
|
|
# The `.nativeDrv` stanza works like nativeBuildInputs and ensures cross-compiling has the right version available.
|
|
|
|
diff = "${diffutils.nativeDrv or diffutils}/bin/diff";
|
2020-05-07 03:03:41 +02:00
|
|
|
|
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-01-24 01:40:18 +01:00
|
|
|
stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs useSysroot {
|
2020-10-17 09:48:38 +02:00
|
|
|
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
|
|
|
|
} // {
|
2017-08-05 16:38:48 +02:00
|
|
|
inherit cargoDeps;
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2015-04-23 16:37:52 +02:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
2019-08-14 11:50:05 +02:00
|
|
|
nativeBuildInputs = nativeBuildInputs ++ [ cacert git cargo rustc ];
|
2021-01-24 01:40:18 +01:00
|
|
|
buildInputs = buildInputs ++ lib.optional 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"
|
|
|
|
|
2018-02-20 10:59:26 +01:00
|
|
|
${setupVendorDir}
|
2017-11-20 18:02:01 +01:00
|
|
|
|
2017-08-05 16:38:48 +02:00
|
|
|
mkdir .cargo
|
2018-09-09 15:54:00 +02:00
|
|
|
config="$(pwd)/$cargoDepsCopy/.cargo/config";
|
|
|
|
if [[ ! -e $config ]]; then
|
|
|
|
config=${./fetchcargo-default-config.toml};
|
|
|
|
fi;
|
|
|
|
substitute $config .cargo/config \
|
2018-11-21 02:47:45 +01:00
|
|
|
--subst-var-by vendor "$(pwd)/$cargoDepsCopy"
|
2015-04-23 15:14:34 +02:00
|
|
|
|
2018-11-21 02:47:45 +01:00
|
|
|
cat >> .cargo/config <<'EOF'
|
2018-12-21 01:28:09 +01:00
|
|
|
[target."${rust.toRustTarget stdenv.buildPlatform}"]
|
2018-11-21 02:47:45 +01:00
|
|
|
"linker" = "${ccForBuild}"
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
2020-10-08 23:32:49 +02:00
|
|
|
[target."${shortTarget}"]
|
2018-11-21 02:47:45 +01:00
|
|
|
"linker" = "${ccForHost}"
|
2019-08-14 13:59:41 +02:00
|
|
|
${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
|
2021-01-24 01:40:18 +01:00
|
|
|
lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
|
2019-08-14 13:59:41 +02:00
|
|
|
"rustflags" = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ]
|
|
|
|
''}
|
2018-11-21 02:47:45 +01:00
|
|
|
''}
|
|
|
|
EOF
|
2019-07-25 18:48:18 +02:00
|
|
|
|
|
|
|
export RUST_LOG=${logLevel}
|
2020-02-16 08:33:02 +01:00
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
|
|
|
# After unpacking and applying patches, check that the Cargo.lock matches our
|
|
|
|
# src package. Note that we do this after the patchPhase, because the
|
|
|
|
# patchPhase may create the Cargo.lock if upstream has not shipped one.
|
2021-01-24 01:40:18 +01:00
|
|
|
postPatch = (args.postPatch or "") + lib.optionalString validateCargoDeps ''
|
2020-02-16 08:33:02 +01:00
|
|
|
cargoDepsLockfile=$NIX_BUILD_TOP/$cargoDepsCopy/Cargo.lock
|
|
|
|
srcLockfile=$NIX_BUILD_TOP/$sourceRoot/Cargo.lock
|
|
|
|
|
|
|
|
echo "Validating consistency between $srcLockfile and $cargoDepsLockfile"
|
2020-05-07 03:03:41 +02:00
|
|
|
if ! ${diff} $srcLockfile $cargoDepsLockfile; then
|
2020-02-16 08:33:02 +01:00
|
|
|
|
|
|
|
# If the diff failed, first double-check that the file exists, so we can
|
|
|
|
# give a friendlier error msg.
|
|
|
|
if ! [ -e $srcLockfile ]; then
|
|
|
|
echo "ERROR: Missing Cargo.lock from src. Expected to find it at: $srcLockfile"
|
2020-06-05 09:10:53 +02:00
|
|
|
echo "Hint: You can use the cargoPatches attribute to add a Cargo.lock manually to the build."
|
2020-02-16 08:33:02 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -e $cargoDepsLockfile ]; then
|
|
|
|
echo "ERROR: Missing lockfile from cargo vendor. Expected to find it at: $cargoDepsLockfile"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-08-24 14:29:47 +02:00
|
|
|
echo
|
2020-01-12 18:19:17 +01:00
|
|
|
echo "ERROR: cargoSha256 is out of date"
|
2019-08-24 14:29:47 +02:00
|
|
|
echo
|
2020-01-12 17:21:23 +01:00
|
|
|
echo "Cargo.lock is not the same in $cargoDepsCopy"
|
2019-08-24 14:29:47 +02:00
|
|
|
echo
|
|
|
|
echo "To fix the issue:"
|
2020-02-16 08:33:02 +01:00
|
|
|
echo '1. Use "0000000000000000000000000000000000000000000000000000" as the cargoSha256 value'
|
2020-09-05 23:04:32 +02:00
|
|
|
echo "2. Build the derivation and wait for it to fail with a hash mismatch"
|
2019-08-24 14:29:47 +02:00
|
|
|
echo "3. Copy the 'got: sha256:' value back into the cargoSha256 field"
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-01-12 17:21:23 +01:00
|
|
|
'' + ''
|
|
|
|
unset cargoDepsCopy
|
2020-02-16 08:33:02 +01:00
|
|
|
'';
|
2019-07-25 18:48:18 +02:00
|
|
|
|
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
2018-11-21 02:47:45 +01:00
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2017-04-15 11:14:55 +02:00
|
|
|
buildPhase = with builtins; args.buildPhase or ''
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preBuild
|
2019-02-22 07:16:20 +01:00
|
|
|
|
|
|
|
(
|
|
|
|
set -x
|
2018-11-21 02:47:45 +01:00
|
|
|
env \
|
2018-12-21 01:28:09 +01:00
|
|
|
"CC_${rust.toRustTarget stdenv.buildPlatform}"="${ccForBuild}" \
|
|
|
|
"CXX_${rust.toRustTarget stdenv.buildPlatform}"="${cxxForBuild}" \
|
|
|
|
"CC_${rust.toRustTarget stdenv.hostPlatform}"="${ccForHost}" \
|
|
|
|
"CXX_${rust.toRustTarget stdenv.hostPlatform}"="${cxxForHost}" \
|
2020-10-17 09:48:38 +02:00
|
|
|
cargo build -j $NIX_BUILD_CORES \
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (buildType == "release") "--release"} \
|
2020-10-08 23:32:49 +02:00
|
|
|
--target ${target} \
|
2018-11-21 02:47:45 +01:00
|
|
|
--frozen ${concatStringsSep " " cargoBuildFlags}
|
2019-02-22 07:16:20 +01:00
|
|
|
)
|
|
|
|
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postBuild
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "popd"}
|
2020-03-24 18:32:59 +01:00
|
|
|
|
|
|
|
# This needs to be done after postBuild: packages like `cargo` do a pushd/popd in
|
|
|
|
# the pre/postBuild-hooks that need to be taken into account before gathering
|
|
|
|
# all binaries to install.
|
2020-07-14 17:32:06 +02:00
|
|
|
mkdir -p $tmpDir
|
|
|
|
cp -r $releaseDir/* $tmpDir/
|
|
|
|
bins=$(find $tmpDir \
|
2020-03-24 18:32:59 +01:00
|
|
|
-maxdepth 1 \
|
|
|
|
-type f \
|
|
|
|
-executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \))
|
|
|
|
'';
|
|
|
|
|
2020-07-14 14:39:13 +02:00
|
|
|
checkPhase = args.checkPhase or (let
|
2021-01-24 01:40:18 +01:00
|
|
|
argstr = "${lib.optionalString (checkType == "release") "--release"} --target ${target} --frozen";
|
2020-09-09 13:39:23 +02:00
|
|
|
threads = if cargoParallelTestThreads then "$NIX_BUILD_CORES" else "1";
|
2020-02-25 21:21:04 +01:00
|
|
|
in ''
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preCheck
|
2020-05-06 23:25:01 +02:00
|
|
|
echo "Running cargo test ${argstr} -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}"
|
2020-09-09 13:39:23 +02:00
|
|
|
cargo test -j $NIX_BUILD_CORES ${argstr} -- --test-threads=${threads} ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"}
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postCheck
|
2021-01-24 01:40:18 +01:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "popd"}
|
2020-02-25 21:21:04 +01:00
|
|
|
'');
|
2015-04-21 20:34:26 +02:00
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2020-03-18 14:50:12 +01:00
|
|
|
strictDeps = true;
|
|
|
|
|
2020-07-14 17:32:06 +02:00
|
|
|
inherit releaseDir tmpDir;
|
2018-11-21 02:47:45 +01:00
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
installPhase = args.installPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preInstall
|
2020-02-25 21:21:04 +01:00
|
|
|
|
|
|
|
# rename the output dir to a architecture independent one
|
2020-07-14 17:32:06 +02:00
|
|
|
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep '${tmpDir}$')
|
2020-02-25 21:21:04 +01:00
|
|
|
for target in "''${targets[@]}"; do
|
|
|
|
rm -rf "$target/../../${buildType}"
|
|
|
|
ln -srf "$target" "$target/../../"
|
|
|
|
done
|
2018-09-30 16:03:35 +02:00
|
|
|
mkdir -p $out/bin $out/lib
|
2018-11-21 02:47:45 +01:00
|
|
|
|
2020-03-24 18:32:59 +01:00
|
|
|
xargs -r cp -t $out/bin <<< $bins
|
2020-07-14 17:32:06 +02:00
|
|
|
find $tmpDir \
|
2018-11-21 02:47:45 +01:00
|
|
|
-maxdepth 1 \
|
|
|
|
-regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \
|
|
|
|
-print0 | xargs -r -0 cp -t $out/lib
|
2018-09-30 16:03:35 +02:00
|
|
|
rmdir --ignore-fail-on-non-empty $out/lib $out/bin
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postInstall
|
2014-10-10 16:59:37 +02:00
|
|
|
'';
|
2017-08-05 16:38:48 +02:00
|
|
|
|
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
|
|
|
})
|