summary refs log tree commit diff
path: root/pkgs/build-support/rust
diff options
context:
space:
mode:
authorDaniël de Kok <me@danieldk.eu>2021-02-15 06:54:18 +0100
committerDaniël de Kok <me@danieldk.eu>2021-02-15 12:17:18 +0100
commit9757c7101a0527c001fa9e9832764d5dc106ff25 (patch)
tree84e0fae7e564134fe879e732b0444f03adcc3ddf /pkgs/build-support/rust
parent056f697397f0971aac7f3e022f9b90dc80d194e9 (diff)
downloadnixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar.gz
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar.bz2
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar.lz
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar.xz
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.tar.zst
nixpkgs-9757c7101a0527c001fa9e9832764d5dc106ff25.zip
buildRustPackage: factor out install phase to cargoInstallHook
Diffstat (limited to 'pkgs/build-support/rust')
-rw-r--r--pkgs/build-support/rust/default.nix40
-rw-r--r--pkgs/build-support/rust/hooks/cargo-build-hook.sh6
-rw-r--r--pkgs/build-support/rust/hooks/cargo-install-hook.sh49
-rw-r--r--pkgs/build-support/rust/hooks/default.nix9
4 files changed, 65 insertions, 39 deletions
diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix
index 19ec71261be..b7d6cb522bc 100644
--- a/pkgs/build-support/rust/default.nix
+++ b/pkgs/build-support/rust/default.nix
@@ -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 = {
diff --git a/pkgs/build-support/rust/hooks/cargo-build-hook.sh b/pkgs/build-support/rust/hooks/cargo-build-hook.sh
index 55585233413..85a3827dc10 100644
--- a/pkgs/build-support/rust/hooks/cargo-build-hook.sh
+++ b/pkgs/build-support/rust/hooks/cargo-build-hook.sh
@@ -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"
 }
 
diff --git a/pkgs/build-support/rust/hooks/cargo-install-hook.sh b/pkgs/build-support/rust/hooks/cargo-install-hook.sh
new file mode 100644
index 00000000000..e6ffa300706
--- /dev/null
+++ b/pkgs/build-support/rust/hooks/cargo-install-hook.sh
@@ -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
diff --git a/pkgs/build-support/rust/hooks/default.nix b/pkgs/build-support/rust/hooks/default.nix
index d4b2cc15605..b43f83acda0 100644
--- a/pkgs/build-support/rust/hooks/default.nix
+++ b/pkgs/build-support/rust/hooks/default.nix
@@ -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";