buildRustPackage: factor out install phase to cargoInstallHook

This commit is contained in:
Daniël de Kok 2021-02-15 06:54:18 +01:00
parent 056f697397
commit 9757c7101a
6 changed files with 69 additions and 41 deletions

View file

@ -293,6 +293,8 @@ attributes can also be used:
variable `buildAndTestSubdir` can be used to build a crate in a
Cargo workspace. Additional maturin flags can be passed through
`maturinBuildFlags`.
* `cargoInstallHook`: install binaries and static/shared libraries
that were built using `cargoBuildHook`.
### Examples

View file

@ -4,6 +4,7 @@
, cacert
, cargo
, cargoBuildHook
, cargoInstallHook
, cargoSetupHook
, fetchCargoTarball
, runCommandNoCC
@ -87,9 +88,6 @@ let
originalCargoToml = src + /Cargo.toml; # profile info is later extracted
};
releaseDir = "target/${shortTarget}/${buildType}";
tmpDir = "${releaseDir}-tmp";
in
# Tests don't currently work for `no_std`, and all custom sysroots are currently built without `std`.
@ -99,16 +97,16 @@ assert useSysroot -> !(args.doCheck or true);
stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs useSysroot {
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
} // {
inherit buildAndTestSubdir cargoDeps releaseDir tmpDir;
inherit buildAndTestSubdir cargoDeps;
cargoBuildFlags = lib.concatStringsSep " " cargoBuildFlags;
cargoBuildType = "--${buildType}";
cargoBuildType = buildType;
patchRegistryDeps = ./patch-registry-deps;
nativeBuildInputs = nativeBuildInputs ++
[ cacert git cargo cargoBuildHook cargoSetupHook rustc ];
[ cacert git cargo cargoBuildHook cargoInstallHook cargoSetupHook rustc ];
buildInputs = buildInputs ++ lib.optional stdenv.hostPlatform.isMinGW windows.pthreads;
@ -144,36 +142,6 @@ stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs u
strictDeps = true;
installPhase = args.installPhase or ''
runHook preInstall
# 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.
mkdir -p $tmpDir
cp -r $releaseDir/* $tmpDir/
bins=$(find $tmpDir \
-maxdepth 1 \
-type f \
-executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \))
# rename the output dir to a architecture independent one
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep '${tmpDir}$')
for target in "''${targets[@]}"; do
rm -rf "$target/../../${buildType}"
ln -srf "$target" "$target/../../"
done
mkdir -p $out/bin $out/lib
xargs -r cp -t $out/bin <<< $bins
find $tmpDir \
-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 = {

View file

@ -17,16 +17,16 @@ cargoBuildHook() {
cargo build -j $NIX_BUILD_CORES \
--target @rustTargetPlatformSpec@ \
--frozen \
${cargoBuildType} \
--${cargoBuildType} \
${cargoBuildFlags}
)
runHook postBuild
if [ ! -z "${buildAndTestSubdir-}" ]; then
popd
fi
runHook postBuild
echo "Finished cargoBuildHook"
}

View file

@ -0,0 +1,49 @@
cargoInstallPostBuildHook() {
echo "Executing cargoInstallPostBuildHook"
releaseDir=target/@shortTarget@/$cargoBuildType
tmpDir="${releaseDir}-tmp";
mkdir -p $tmpDir
cp -r ${releaseDir}/* $tmpDir/
bins=$(find $tmpDir \
-maxdepth 1 \
-type f \
-executable ! \( -regex ".*\.\(so.[0-9.]+\|so\|a\|dylib\)" \))
echo "Finished cargoInstallPostBuildHook"
}
cargoInstallHook() {
echo "Executing cargoInstallHook"
runHook preInstall
# rename the output dir to a architecture independent one
releaseDir=target/@shortTarget@/$cargoBuildType
tmpDir="${releaseDir}-tmp";
mapfile -t targets < <(find "$NIX_BUILD_TOP" -type d | grep "${tmpDir}$")
for target in "${targets[@]}"; do
rm -rf "$target/../../${cargoBuildType}"
ln -srf "$target" "$target/../../"
done
mkdir -p $out/bin $out/lib
xargs -r cp -t $out/bin <<< $bins
find $tmpDir \
-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
echo "Finished cargoInstallHook"
}
if [ -z "${installPhase-}" ]; then
installPhase=cargoInstallHook
postBuildHooks+=(cargoInstallPostBuildHook)
fi

View file

@ -36,6 +36,15 @@ in {
};
} ./cargo-build-hook.sh) {};
cargoInstallHook = callPackage ({ }:
makeSetupHook {
name = "cargo-install-hook.sh";
deps = [ ];
substitutions = {
inherit shortTarget;
};
} ./cargo-install-hook.sh) {};
cargoSetupHook = callPackage ({ }:
makeSetupHook {
name = "cargo-setup-hook.sh";

View file

@ -12,7 +12,7 @@ rec {
};
buildRustPackage = callPackage ../../../build-support/rust {
inherit rustc cargo cargoBuildHook cargoSetupHook fetchCargoTarball;
inherit rustc cargo cargoBuildHook cargoInstallHook cargoSetupHook fetchCargoTarball;
};
rustcSrc = callPackage ./rust-src.nix {
@ -26,5 +26,5 @@ rec {
# Hooks
inherit (callPackage ../../../build-support/rust/hooks {
inherit cargo;
}) cargoBuildHook cargoSetupHook maturinBuildHook;
}) cargoBuildHook cargoInstallHook cargoSetupHook maturinBuildHook;
}