diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index c53818f81570..8451c126410d 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -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 diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index 19ec71261be9..b7d6cb522bc5 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 55585233413e..85a3827dc103 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 000000000000..e6ffa3007063 --- /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 d4b2cc156059..b43f83acda0b 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"; diff --git a/pkgs/development/compilers/rust/make-rust-platform.nix b/pkgs/development/compilers/rust/make-rust-platform.nix index 1ea97b48c447..e963c4631357 100644 --- a/pkgs/development/compilers/rust/make-rust-platform.nix +++ b/pkgs/development/compilers/rust/make-rust-platform.nix @@ -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; }