2018-12-21 01:28:09 +01:00
|
|
|
{ stdenv, cacert, git, rust, cargo, rustc, fetchcargo, buildPackages, windows }:
|
2018-11-21 13:38:49 +01:00
|
|
|
|
2019-03-02 03:45:12 +01:00
|
|
|
{ name ? "${args.pname}-${args.version}"
|
|
|
|
, cargoSha256 ? "unset"
|
2015-05-29 19:35:31 +02:00
|
|
|
, src ? null
|
|
|
|
, srcs ? 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-08-24 14:29:47 +02:00
|
|
|
, # Set to true to verify if the cargo dependencies are up to date.
|
|
|
|
# This will change the value of cargoSha256.
|
|
|
|
verifyCargoDeps ? false
|
2019-02-26 06:52:01 +01:00
|
|
|
, buildType ? "release"
|
2019-07-21 07:00:00 +02:00
|
|
|
, meta ? {}
|
2019-10-24 12:39:08 +02:00
|
|
|
, target ? null
|
2018-02-20 10:59:26 +01:00
|
|
|
|
|
|
|
, cargoVendorDir ? null
|
2015-05-29 19:35:31 +02:00
|
|
|
, ... } @ args:
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2018-04-07 23:44:21 +02:00
|
|
|
assert cargoVendorDir == null -> cargoSha256 != "unset";
|
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
|
2018-02-20 10:59:26 +01:00
|
|
|
cargoDeps = if cargoVendorDir == null
|
|
|
|
then fetchcargo {
|
|
|
|
inherit name src srcs sourceRoot cargoUpdateHook;
|
2019-08-24 14:29:47 +02:00
|
|
|
copyLockfile = verifyCargoDeps;
|
2018-08-13 07:44:30 +02:00
|
|
|
patches = cargoPatches;
|
2018-02-20 10:59:26 +01:00
|
|
|
sha256 = cargoSha256;
|
|
|
|
}
|
|
|
|
else null;
|
|
|
|
|
|
|
|
setupVendorDir = if cargoVendorDir == null
|
|
|
|
then ''
|
|
|
|
unpackFile "$cargoDeps"
|
|
|
|
cargoDepsCopy=$(stripHash $(basename $cargoDeps))
|
|
|
|
chmod -R +w "$cargoDepsCopy"
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
|
|
|
|
'';
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2018-12-21 01:28:09 +01:00
|
|
|
rustTarget = if target == null then rust.toRustTarget stdenv.hostPlatform else target;
|
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++";
|
2019-10-24 12:39:08 +02:00
|
|
|
releaseDir = "target/${rustTarget}/${buildType}";
|
2019-08-14 11:13:19 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
stdenv.mkDerivation (args // {
|
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 ];
|
|
|
|
buildInputs = buildInputs ++ stdenv.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}"
|
|
|
|
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
2019-10-24 12:39:08 +02:00
|
|
|
[target."${rustTarget}"]
|
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
|
|
|
|
stdenv.lib.optionalString (stdenv.hostPlatform.isMusl && stdenv.hostPlatform.isAarch64) ''
|
|
|
|
"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
|
|
|
|
|
|
|
unset cargoDepsCopy
|
|
|
|
export RUST_LOG=${logLevel}
|
2019-08-24 14:29:47 +02:00
|
|
|
'' + stdenv.lib.optionalString verifyCargoDeps ''
|
|
|
|
if ! diff source/Cargo.lock $cargoDeps/Cargo.lock ; then
|
|
|
|
echo
|
|
|
|
echo "ERROR: cargoSha256 is out of date."
|
|
|
|
echo
|
|
|
|
echo "Cargo.lock is not the same in $cargoDeps."
|
|
|
|
echo
|
|
|
|
echo "To fix the issue:"
|
|
|
|
echo '1. Use "1111111111111111111111111111111111111111111111111111" as the cargoSha256 value'
|
|
|
|
echo "2. Build the derivation and wait it to fail with a hash mismatch"
|
|
|
|
echo "3. Copy the 'got: sha256:' value back into the cargoSha256 field"
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-07-25 18:48:18 +02:00
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
|
|
|
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 ''
|
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}" \
|
2018-11-21 02:47:45 +01:00
|
|
|
cargo build \
|
2019-07-31 14:19:01 +02:00
|
|
|
${stdenv.lib.optionalString (buildType == "release") "--release"} \
|
2019-10-24 12:39:08 +02:00
|
|
|
--target ${rustTarget} \
|
2018-11-21 02:47:45 +01:00
|
|
|
--frozen ${concatStringsSep " " cargoBuildFlags}
|
2019-02-22 07:16:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# rename the output dir to a architecture independent one
|
|
|
|
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep '${releaseDir}$')
|
|
|
|
for target in "''${targets[@]}"; do
|
2019-02-26 06:52:01 +01:00
|
|
|
rm -rf "$target/../../${buildType}"
|
2019-02-22 07:16:20 +01:00
|
|
|
ln -srf "$target" "$target/../../"
|
|
|
|
done
|
|
|
|
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postBuild
|
2014-10-10 16:59:37 +02:00
|
|
|
'';
|
|
|
|
|
2015-04-21 20:34:26 +02:00
|
|
|
checkPhase = args.checkPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preCheck
|
2019-07-25 12:34:22 +02:00
|
|
|
echo "Running cargo cargo test -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}"
|
|
|
|
cargo test -- ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"}
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postCheck
|
2015-04-21 20:34:26 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2018-11-21 02:47:45 +01:00
|
|
|
inherit releaseDir;
|
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
installPhase = args.installPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preInstall
|
2018-09-30 16:03:35 +02:00
|
|
|
mkdir -p $out/bin $out/lib
|
2018-11-21 02:47:45 +01:00
|
|
|
|
|
|
|
find $releaseDir \
|
|
|
|
-maxdepth 1 \
|
|
|
|
-type f \
|
|
|
|
-executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \) \
|
|
|
|
-print0 | xargs -r -0 cp -t $out/bin
|
|
|
|
find $releaseDir \
|
|
|
|
-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
|
|
|
})
|