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
|
|
|
|
, cargo
|
|
|
|
, diffutils
|
|
|
|
, fetchCargoTarball
|
2020-11-30 08:33:03 +00:00
|
|
|
, runCommandNoCC
|
|
|
|
, rustPlatform
|
|
|
|
, callPackage
|
|
|
|
, remarshal
|
2020-05-15 21:57:56 +00:00
|
|
|
, git
|
|
|
|
, rust
|
|
|
|
, rustc
|
|
|
|
, 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 ? ""
|
|
|
|
, cargoBuildFlags ? []
|
|
|
|
, buildType ? "release"
|
|
|
|
, meta ? {}
|
2020-11-30 08:33:03 +00:00
|
|
|
, target ? rust.toRustTargetSpec stdenv.hostPlatform
|
2020-04-24 23:36:52 +00:00
|
|
|
, cargoVendorDir ? null
|
2020-06-18 07:06:33 +00:00
|
|
|
, checkType ? buildType
|
2020-09-25 04:45:31 +00:00
|
|
|
, depsExtraArgs ? {}
|
|
|
|
, cargoParallelTestThreads ? true
|
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
|
|
|
|
|
|
|
# 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.
|
|
|
|
setupVendorDir = if cargoVendorDir == null
|
|
|
|
then (''
|
|
|
|
unpackFile "$cargoDeps"
|
|
|
|
cargoDepsCopy=$(stripHash $cargoDeps)
|
|
|
|
'')
|
|
|
|
else ''
|
|
|
|
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
|
|
|
|
'';
|
|
|
|
|
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
|
|
|
|
|
|
|
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-11-30 08:33:03 +00:00
|
|
|
releaseDir = "target/${shortTarget}/${buildType}";
|
2020-08-20 17:08:02 +00:00
|
|
|
tmpDir = "${releaseDir}-tmp";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-05-15 21:57:56 +00: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-18 07:06:33 +00: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-15 21:57:56 +00:00
|
|
|
|
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 "");
|
|
|
|
} // {
|
2020-04-24 23:36:52 +00:00
|
|
|
inherit cargoDeps;
|
|
|
|
|
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
|
|
|
nativeBuildInputs = nativeBuildInputs ++ [ cacert git cargo rustc ];
|
2021-02-05 17:12:51 +00:00
|
|
|
buildInputs = buildInputs ++ lib.optional 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"
|
|
|
|
|
|
|
|
${setupVendorDir}
|
|
|
|
|
|
|
|
mkdir .cargo
|
|
|
|
config="$(pwd)/$cargoDepsCopy/.cargo/config";
|
|
|
|
if [[ ! -e $config ]]; then
|
|
|
|
config=${./fetchcargo-default-config.toml};
|
|
|
|
fi;
|
|
|
|
substitute $config .cargo/config \
|
|
|
|
--subst-var-by vendor "$(pwd)/$cargoDepsCopy"
|
|
|
|
|
|
|
|
cat >> .cargo/config <<'EOF'
|
|
|
|
[target."${rust.toRustTarget stdenv.buildPlatform}"]
|
|
|
|
"linker" = "${ccForBuild}"
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
2020-11-30 08:33:03 +00:00
|
|
|
[target."${shortTarget}"]
|
2020-04-24 23:36:52 +00:00
|
|
|
"linker" = "${ccForHost}"
|
|
|
|
${# https://github.com/rust-lang/rust/issues/46651#issuecomment-433611633
|
2021-02-05 17:12:51 +00:00
|
|
|
lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
"rustflags" = [ "-C", "target-feature=+crt-static", "-C", "link-arg=-lgcc" ]
|
|
|
|
''}
|
|
|
|
''}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
export RUST_LOG=${logLevel}
|
|
|
|
'' + (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-02-05 17:12:51 +00:00
|
|
|
postPatch = (args.postPatch or "") + lib.optionalString validateCargoDeps ''
|
2020-04-24 23:36:52 +00:00
|
|
|
cargoDepsLockfile=$NIX_BUILD_TOP/$cargoDepsCopy/Cargo.lock
|
|
|
|
srcLockfile=$NIX_BUILD_TOP/$sourceRoot/Cargo.lock
|
|
|
|
|
|
|
|
echo "Validating consistency between $srcLockfile and $cargoDepsLockfile"
|
2020-05-15 21:57:56 +00:00
|
|
|
if ! ${diff} $srcLockfile $cargoDepsLockfile; then
|
2020-04-24 23:36:52 +00: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-18 07:06:33 +00:00
|
|
|
echo "Hint: You can use the cargoPatches attribute to add a Cargo.lock manually to the build."
|
2020-04-24 23:36:52 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -e $cargoDepsLockfile ]; then
|
|
|
|
echo "ERROR: Missing lockfile from cargo vendor. Expected to find it at: $cargoDepsLockfile"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "ERROR: cargoSha256 is out of date"
|
|
|
|
echo
|
|
|
|
echo "Cargo.lock is not the same in $cargoDepsCopy"
|
|
|
|
echo
|
|
|
|
echo "To fix the issue:"
|
|
|
|
echo '1. Use "0000000000000000000000000000000000000000000000000000" as the cargoSha256 value'
|
2020-09-25 04:45:31 +00:00
|
|
|
echo "2. Build the derivation and wait for it to fail with a hash mismatch"
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "3. Copy the 'got: sha256:' value back into the cargoSha256 field"
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'' + ''
|
|
|
|
unset cargoDepsCopy
|
|
|
|
'';
|
|
|
|
|
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = with builtins; args.buildPhase or ''
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook preBuild
|
|
|
|
|
|
|
|
(
|
|
|
|
set -x
|
|
|
|
env \
|
|
|
|
"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-09-25 04:45:31 +00:00
|
|
|
cargo build -j $NIX_BUILD_CORES \
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (buildType == "release") "--release"} \
|
2020-11-30 08:33:03 +00:00
|
|
|
--target ${target} \
|
2020-04-24 23:36:52 +00:00
|
|
|
--frozen ${concatStringsSep " " cargoBuildFlags}
|
|
|
|
)
|
|
|
|
|
|
|
|
runHook postBuild
|
2020-06-18 07:06:33 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "popd"}
|
2020-06-18 07:06:33 +00: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-08-20 17:08:02 +00:00
|
|
|
mkdir -p $tmpDir
|
|
|
|
cp -r $releaseDir/* $tmpDir/
|
|
|
|
bins=$(find $tmpDir \
|
2020-06-18 07:06:33 +00:00
|
|
|
-maxdepth 1 \
|
|
|
|
-type f \
|
|
|
|
-executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \))
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
checkPhase = args.checkPhase or (let
|
2021-02-05 17:12:51 +00:00
|
|
|
argstr = "${lib.optionalString (checkType == "release") "--release"} --target ${target} --frozen";
|
2020-09-25 04:45:31 +00:00
|
|
|
threads = if cargoParallelTestThreads then "$NIX_BUILD_CORES" else "1";
|
2020-06-18 07:06:33 +00:00
|
|
|
in ''
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "pushd ${buildAndTestSubdir}"}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook preCheck
|
2020-06-18 07:06:33 +00:00
|
|
|
echo "Running cargo test ${argstr} -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}"
|
2020-09-25 04:45:31 +00:00
|
|
|
cargo test -j $NIX_BUILD_CORES ${argstr} -- --test-threads=${threads} ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"}
|
2020-04-24 23:36:52 +00:00
|
|
|
runHook postCheck
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (buildAndTestSubdir != null) "popd"}
|
2020-06-18 07:06:33 +00:00
|
|
|
'');
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
|
|
|
strictDeps = true;
|
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
inherit releaseDir tmpDir;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
installPhase = args.installPhase or ''
|
|
|
|
runHook preInstall
|
2020-06-18 07:06:33 +00:00
|
|
|
|
|
|
|
# rename the output dir to a architecture independent one
|
2020-08-20 17:08:02 +00:00
|
|
|
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep '${tmpDir}$')
|
2020-06-18 07:06:33 +00:00
|
|
|
for target in "''${targets[@]}"; do
|
|
|
|
rm -rf "$target/../../${buildType}"
|
|
|
|
ln -srf "$target" "$target/../../"
|
|
|
|
done
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p $out/bin $out/lib
|
|
|
|
|
2020-06-18 07:06:33 +00:00
|
|
|
xargs -r cp -t $out/bin <<< $bins
|
2020-08-20 17:08:02 +00:00
|
|
|
find $tmpDir \
|
2020-04-24 23:36:52 +00:00
|
|
|
-maxdepth 1 \
|
|
|
|
-regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \
|
|
|
|
-print0 | xargs -r -0 cp -t $out/lib
|
|
|
|
rmdir --ignore-fail-on-non-empty $out/lib $out/bin
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
passthru = { inherit cargoDeps; } // (args.passthru or {});
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
# default to Rust's platforms
|
|
|
|
platforms = rustc.meta.platforms;
|
|
|
|
} // meta;
|
|
|
|
})
|