From 9757c7101a0527c001fa9e9832764d5dc106ff25 Mon Sep 17 00:00:00 2001 From: Daniƫl de Kok Date: Mon, 15 Feb 2021 06:54:18 +0100 Subject: buildRustPackage: factor out install phase to cargoInstallHook --- pkgs/build-support/rust/default.nix | 40 ++---------------- pkgs/build-support/rust/hooks/cargo-build-hook.sh | 6 +-- .../build-support/rust/hooks/cargo-install-hook.sh | 49 ++++++++++++++++++++++ pkgs/build-support/rust/hooks/default.nix | 9 ++++ 4 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 pkgs/build-support/rust/hooks/cargo-install-hook.sh (limited to 'pkgs/build-support/rust') 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"; -- cgit 1.4.1