1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-12-25 03:17:13 +00:00
nixpkgs/pkgs/build-support/rust/default.nix

183 lines
5.5 KiB
Nix
Raw Normal View History

{ stdenv, cacert, git, rust, cargo, rustc, fetchcargo, buildPackages, windows }:
{ name ? "${args.pname}-${args.version}"
, cargoSha256 ? "unset"
, src ? null
, srcs ? null
, unpackPhase ? null
, cargoPatches ? []
, patches ? []
, sourceRoot ? null
, logLevel ? ""
, buildInputs ? []
2018-11-21 01:47:45 +00:00
, nativeBuildInputs ? []
, cargoUpdateHook ? ""
, cargoDepsHook ? ""
2017-04-15 10:14:55 +01:00
, cargoBuildFlags ? []
, # Set to true to verify if the cargo dependencies are up to date.
# This will change the value of cargoSha256.
verifyCargoDeps ? false
, buildType ? "release"
, meta ? {}
2019-10-24 11:39:08 +01:00
, target ? null
2018-02-20 09:59:26 +00:00
, cargoVendorDir ? null
, ... } @ args:
assert cargoVendorDir == null -> cargoSha256 != "unset";
assert buildType == "release" || buildType == "debug";
2018-02-20 09:59:26 +00:00
let
2018-02-20 09:59:26 +00:00
cargoDeps = if cargoVendorDir == null
then fetchcargo {
inherit name src srcs sourceRoot unpackPhase cargoUpdateHook;
copyLockfile = verifyCargoDeps;
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}"
'';
rustTarget = if target == null then rust.toRustTarget stdenv.hostPlatform else target;
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-10-24 11:39:08 +01:00
releaseDir = "target/${rustTarget}/${buildType}";
in
stdenv.mkDerivation (args // {
inherit cargoDeps;
patchRegistryDeps = ./patch-registry-deps;
nativeBuildInputs = nativeBuildInputs ++ [ cacert git cargo rustc ];
buildInputs = buildInputs ++ stdenv.lib.optional stdenv.hostPlatform.isMinGW windows.pthreads;
patches = cargoPatches ++ patches;
PKG_CONFIG_ALLOW_CROSS =
if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;
postUnpack = ''
eval "$cargoDepsHook"
2018-02-20 09:59:26 +00:00
${setupVendorDir}
mkdir .cargo
2018-09-09 14:54:00 +01: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"
2018-11-21 01:47:45 +00:00
cat >> .cargo/config <<'EOF'
[target."${rust.toRustTarget stdenv.buildPlatform}"]
2018-11-21 01:47:45 +00:00
"linker" = "${ccForBuild}"
${stdenv.lib.optionalString (stdenv.buildPlatform.config != stdenv.hostPlatform.config) ''
2019-10-24 11:39:08 +01:00
[target."${rustTarget}"]
2018-11-21 01:47:45 +00:00
"linker" = "${ccForHost}"
${# 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 01:47:45 +00:00
''}
EOF
unset cargoDepsCopy
export RUST_LOG=${logLevel}
'' + 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
'' + (args.postUnpack or "");
configurePhase = args.configurePhase or ''
runHook preConfigure
2018-11-21 01:47:45 +00:00
runHook postConfigure
'';
2017-04-15 10:14:55 +01:00
buildPhase = with builtins; args.buildPhase or ''
runHook preBuild
(
set -x
2018-11-21 01:47:45 +00:00
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}" \
2018-11-21 01:47:45 +00:00
cargo build \
${stdenv.lib.optionalString (buildType == "release") "--release"} \
2019-10-24 11:39:08 +01:00
--target ${rustTarget} \
2018-11-21 01:47:45 +00:00
--frozen ${concatStringsSep " " cargoBuildFlags}
)
# 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
rm -rf "$target/../../${buildType}"
ln -srf "$target" "$target/../../"
done
runHook postBuild
'';
checkPhase = args.checkPhase or ''
runHook preCheck
echo "Running cargo cargo test -- ''${checkFlags} ''${checkFlagsArray+''${checkFlagsArray[@]}}"
cargo test -- ''${checkFlags} ''${checkFlagsArray+"''${checkFlagsArray[@]}"}
runHook postCheck
'';
doCheck = args.doCheck or true;
2018-11-21 01:47:45 +00:00
inherit releaseDir;
installPhase = args.installPhase or ''
runHook preInstall
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
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;
})