2019-06-16 19:59:06 +00:00
|
|
|
{ stdenv, cacert, git, cargo, rustc, fetchcargo, buildPackages }:
|
2018-11-21 12:38:49 +00:00
|
|
|
|
2019-03-02 02:45:12 +00:00
|
|
|
{ name ? "${args.pname}-${args.version}"
|
|
|
|
, cargoSha256 ? "unset"
|
2015-05-29 17:35:31 +00:00
|
|
|
, src ? null
|
|
|
|
, srcs ? null
|
2018-08-13 05:44:30 +00:00
|
|
|
, cargoPatches ? []
|
|
|
|
, patches ? []
|
2015-05-29 17:35:31 +00:00
|
|
|
, sourceRoot ? null
|
2016-05-28 13:03:59 +00:00
|
|
|
, logLevel ? ""
|
2015-05-29 17:35:31 +00:00
|
|
|
, buildInputs ? []
|
2018-11-21 01:47:45 +00:00
|
|
|
, nativeBuildInputs ? []
|
2015-05-29 17:35:31 +00:00
|
|
|
, cargoUpdateHook ? ""
|
2016-12-03 22:36:48 +00:00
|
|
|
, cargoDepsHook ? ""
|
2017-04-15 09:14:55 +00:00
|
|
|
, cargoBuildFlags ? []
|
2019-02-26 05:52:01 +00:00
|
|
|
, buildType ? "release"
|
2018-02-20 09:59:26 +00:00
|
|
|
|
|
|
|
, cargoVendorDir ? null
|
2015-05-29 17:35:31 +00:00
|
|
|
, ... } @ args:
|
2014-10-10 14:59:37 +00:00
|
|
|
|
2018-04-07 21:44:21 +00:00
|
|
|
assert cargoVendorDir == null -> cargoSha256 != "unset";
|
2019-02-26 05:52:01 +00:00
|
|
|
assert buildType == "release" || buildType == "debug";
|
2018-02-20 09:59:26 +00:00
|
|
|
|
2014-10-10 14:59:37 +00:00
|
|
|
let
|
2018-02-20 09:59:26 +00:00
|
|
|
cargoDeps = if cargoVendorDir == null
|
|
|
|
then fetchcargo {
|
|
|
|
inherit name src srcs sourceRoot cargoUpdateHook;
|
2018-08-13 05:44:30 +00:00
|
|
|
patches = cargoPatches;
|
2018-02-20 09:59:26 +00: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 14:59:37 +00:00
|
|
|
|
2018-11-21 01:47:45 +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++";
|
2019-02-26 05:52:01 +00:00
|
|
|
releaseDir = "target/${stdenv.hostPlatform.config}/${buildType}";
|
2018-11-21 01:47:45 +00:00
|
|
|
|
2014-10-10 14:59:37 +00:00
|
|
|
in stdenv.mkDerivation (args // {
|
2017-08-05 14:38:48 +00:00
|
|
|
inherit cargoDeps;
|
2014-10-10 14:59:37 +00:00
|
|
|
|
2015-04-23 14:37:52 +00:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
2018-11-21 01:47:45 +00:00
|
|
|
nativeBuildInputs = [ cargo rustc git cacert ] ++ nativeBuildInputs;
|
|
|
|
inherit buildInputs;
|
2014-10-10 14:59:37 +00:00
|
|
|
|
2018-08-13 05:44:30 +00:00
|
|
|
patches = cargoPatches ++ patches;
|
|
|
|
|
2019-03-12 13:07:36 +00:00
|
|
|
PKG_CONFIG_ALLOW_CROSS =
|
|
|
|
if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
|
|
|
|
|
2014-10-10 14:59:37 +00:00
|
|
|
postUnpack = ''
|
2016-12-03 22:36:48 +00:00
|
|
|
eval "$cargoDepsHook"
|
|
|
|
|
2018-02-20 09:59:26 +00:00
|
|
|
${setupVendorDir}
|
2017-11-20 17:02:01 +00:00
|
|
|
|
2017-08-05 14:38:48 +00:00
|
|
|
mkdir .cargo
|
2018-09-09 13:54:00 +00:00
|
|
|
config="$(pwd)/$cargoDepsCopy/.cargo/config";
|
|
|
|
if [[ ! -e $config ]]; then
|
|
|
|
config=${./fetchcargo-default-config.toml};
|
|
|
|
fi;
|
|
|
|
substitute $config .cargo/config \
|
2018-11-21 01:47:45 +00:00
|
|
|
--subst-var-by vendor "$(pwd)/$cargoDepsCopy"
|
2015-04-23 13:14:34 +00:00
|
|
|
|
2017-11-20 17:02:01 +00:00
|
|
|
unset cargoDepsCopy
|
|
|
|
|
2016-05-28 10:46:16 +00:00
|
|
|
export RUST_LOG=${logLevel}
|
2014-10-10 14:59:37 +00:00
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
2018-11-21 01:47:45 +00:00
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
mkdir -p .cargo
|
|
|
|
cat >> .cargo/config <<'EOF'
|
|
|
|
[target."${stdenv.buildPlatform.config}"]
|
|
|
|
"linker" = "${ccForBuild}"
|
|
|
|
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
|
|
|
|
[target."${stdenv.hostPlatform.config}"]
|
|
|
|
"linker" = "${ccForHost}"
|
|
|
|
''}
|
|
|
|
EOF
|
|
|
|
cat .cargo/config
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2017-04-15 09:14:55 +00:00
|
|
|
buildPhase = with builtins; args.buildPhase or ''
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook preBuild
|
2019-02-22 06:16:20 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
set -x
|
2018-11-21 01:47:45 +00:00
|
|
|
env \
|
|
|
|
"CC_${stdenv.buildPlatform.config}"="${ccForBuild}" \
|
|
|
|
"CXX_${stdenv.buildPlatform.config}"="${cxxForBuild}" \
|
|
|
|
"CC_${stdenv.hostPlatform.config}"="${ccForHost}" \
|
|
|
|
"CXX_${stdenv.hostPlatform.config}"="${cxxForHost}" \
|
|
|
|
cargo build \
|
2019-02-26 05:52:01 +00:00
|
|
|
--${buildType} \
|
2018-11-21 01:47:45 +00:00
|
|
|
--target ${stdenv.hostPlatform.config} \
|
|
|
|
--frozen ${concatStringsSep " " cargoBuildFlags}
|
2019-02-22 06:16:20 +00: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 05:52:01 +00:00
|
|
|
rm -rf "$target/../../${buildType}"
|
2019-02-22 06:16:20 +00:00
|
|
|
ln -srf "$target" "$target/../../"
|
|
|
|
done
|
|
|
|
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook postBuild
|
2014-10-10 14:59:37 +00:00
|
|
|
'';
|
|
|
|
|
2015-04-21 18:34:26 +00:00
|
|
|
checkPhase = args.checkPhase or ''
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook preCheck
|
2015-04-21 18:34:26 +00:00
|
|
|
echo "Running cargo test"
|
|
|
|
cargo test
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook postCheck
|
2015-04-21 18:34:26 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2018-11-21 01:47:45 +00:00
|
|
|
inherit releaseDir;
|
|
|
|
|
2014-10-10 14:59:37 +00:00
|
|
|
installPhase = args.installPhase or ''
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook preInstall
|
2018-09-30 14:03:35 +00:00
|
|
|
mkdir -p $out/bin $out/lib
|
2018-11-21 01:47:45 +00: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 14:03:35 +00:00
|
|
|
rmdir --ignore-fail-on-non-empty $out/lib $out/bin
|
2017-04-15 10:42:09 +00:00
|
|
|
runHook postInstall
|
2014-10-10 14:59:37 +00:00
|
|
|
'';
|
2017-08-05 14:38:48 +00:00
|
|
|
|
2017-10-26 16:43:17 +00:00
|
|
|
passthru = { inherit cargoDeps; } // (args.passthru or {});
|
2014-10-10 14:59:37 +00:00
|
|
|
})
|