Skip to content

Commit

Permalink
rust: support structuredAttrs in setup hooks (#340862)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilazy authored Sep 10, 2024
2 parents 04e6186 + d5013e9 commit bf5f0b6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 73 deletions.
42 changes: 18 additions & 24 deletions pkgs/build-support/rust/hooks/cargo-build-hook.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare -a cargoBuildFlags
# shellcheck shell=bash disable=SC2154,SC2164

cargoBuildHook() {
echo "Executing cargoBuildHook"
Expand All @@ -9,44 +9,38 @@ cargoBuildHook() {
# separateDebugInfo.
export "CARGO_PROFILE_${cargoBuildType@U}_STRIP"=false

if [ ! -z "${buildAndTestSubdir-}" ]; then
if [ -n "${buildAndTestSubdir-}" ]; then
# ensure the output doesn't end up in the subdirectory
export CARGO_TARGET_DIR="$(pwd)/target"
CARGO_TARGET_DIR="$(pwd)/target"
export CARGO_TARGET_DIR

pushd "${buildAndTestSubdir}"
fi

local flagsArray=(
"-j" "$NIX_BUILD_CORES"
"--target" "@rustHostPlatformSpec@"
"--offline"
)

if [ "${cargoBuildType}" != "debug" ]; then
cargoBuildProfileFlag="--profile ${cargoBuildType}"
flagsArray+=("--profile" "${cargoBuildType}")
fi

if [ -n "${cargoBuildNoDefaultFeatures-}" ]; then
cargoBuildNoDefaultFeaturesFlag=--no-default-features
flagsArray+=("--no-default-features")
fi

if [ -n "${cargoBuildFeatures-}" ]; then
if [ -n "$__structuredAttrs" ]; then
OLDIFS="$IFS"
IFS=','; cargoBuildFeaturesFlag="--features=${cargoBuildFeatures[*]}"
IFS="$OLDIFS"
unset OLDIFS
else
cargoBuildFeaturesFlag="--features=${cargoBuildFeatures// /,}"
fi
flagsArray+=("--features=$(concatStringsSep "," cargoBuildFeatures)")
fi

(
set -x
@setEnv@ cargo build -j $NIX_BUILD_CORES \
--target @rustHostPlatformSpec@ \
--offline \
${cargoBuildProfileFlag} \
${cargoBuildNoDefaultFeaturesFlag} \
${cargoBuildFeaturesFlag} \
${cargoBuildFlags}
)
concatTo flagsArray cargoBuildFlags

echoCmd 'cargoBuildHook flags' "${flagsArray[@]}"
@setEnv@ cargo build "${flagsArray[@]}"

if [ ! -z "${buildAndTestSubdir-}" ]; then
if [ -n "${buildAndTestSubdir-}" ]; then
popd
fi

Expand Down
35 changes: 17 additions & 18 deletions pkgs/build-support/rust/hooks/cargo-check-hook.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
declare -a checkFlags
declare -a cargoTestFlags
# shellcheck shell=bash disable=SC2154,SC2164

cargoCheckHook() {
echo "Executing cargoCheckHook"
Expand All @@ -10,37 +9,37 @@ cargoCheckHook() {
pushd "${buildAndTestSubdir}"
fi

local flagsArray=("-j" "$NIX_BUILD_CORES")

if [[ -z ${dontUseCargoParallelTests-} ]]; then
threads=$NIX_BUILD_CORES
prependToVar checkFlags "--test-threads=$NIX_BUILD_CORES"
else
threads=1
prependToVar checkFlags "--test-threads=1"
fi

if [ "${cargoCheckType}" != "debug" ]; then
cargoCheckProfileFlag="--profile ${cargoCheckType}"
flagsArray+=("--profile" "${cargoCheckType}")
fi

if [ -n "${cargoCheckNoDefaultFeatures-}" ]; then
cargoCheckNoDefaultFeaturesFlag=--no-default-features
flagsArray+=("--no-default-features")
fi

if [ -n "${cargoCheckFeatures-}" ]; then
cargoCheckFeaturesFlag="--features=${cargoCheckFeatures// /,}"
flagsArray+=("--features=$(concatStringsSep "," cargoCheckFeatures)")
fi

argstr="${cargoCheckProfileFlag} ${cargoCheckNoDefaultFeaturesFlag} ${cargoCheckFeaturesFlag}
--target @rustHostPlatformSpec@ --offline ${cargoTestFlags}"

(
set -x
cargo test \
-j $NIX_BUILD_CORES \
${argstr} -- \
--test-threads=${threads} \
${checkFlags} \
${checkFlagsArray+"${checkFlagsArray[@]}"}
flagsArray+=(
"--target" "@rustHostPlatformSpec@"
"--offline"
)

prependToVar checkFlags "--"
concatTo flagsArray cargoTestFlags checkFlags checkFlagsArray

echoCmd 'cargoCheckHook flags' "${flagsArray[@]}"
cargo test "${flagsArray[@]}"

if [[ -n "${buildAndTestSubdir-}" ]]; then
popd
fi
Expand Down
32 changes: 15 additions & 17 deletions pkgs/build-support/rust/hooks/cargo-nextest-hook.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
declare -a checkFlags
declare -a cargoTestFlags
# shellcheck shell=bash disable=SC2154,SC2164

cargoNextestHook() {
echo "Executing cargoNextestHook"
Expand All @@ -10,35 +9,34 @@ cargoNextestHook() {
pushd "${buildAndTestSubdir}"
fi

local flagsArray=(
"--target" "@rustHostPlatformSpec@"
"--offline"
)

if [[ -z ${dontUseCargoParallelTests-} ]]; then
threads=$NIX_BUILD_CORES
flagsArray+=("-j" "$NIX_BUILD_CORES")
else
threads=1
flagsArray+=("-j" "1")
fi

if [ "${cargoCheckType}" != "debug" ]; then
cargoCheckProfileFlag="--cargo-profile ${cargoCheckType}"
flagsArray+=("--cargo-profile" "${cargoCheckType}")
fi

if [ -n "${cargoCheckNoDefaultFeatures-}" ]; then
cargoCheckNoDefaultFeaturesFlag=--no-default-features
flagsArray+=("--no-default-features")
fi

if [ -n "${cargoCheckFeatures-}" ]; then
cargoCheckFeaturesFlag="--features=${cargoCheckFeatures// /,}"
flagsArray+=("--features=$(concatStringsSep "," cargoCheckFeatures)")
fi

argstr="${cargoCheckProfileFlag} ${cargoCheckNoDefaultFeaturesFlag} ${cargoCheckFeaturesFlag}
--target @rustHostPlatformSpec@ --offline ${cargoTestFlags}"
prependToVar checkFlags "--"
concatTo flagsArray cargoTestFlags checkFlags checkFlagsArray

(
set -x
cargo nextest run \
-j ${threads} \
${argstr} -- \
${checkFlags} \
${checkFlagsArray+"${checkFlagsArray[@]}"}
)
echoCmd 'cargoNextestHook flags' "${flagsArray[@]}"
cargo nextest run "${flagsArray[@]}"

if [[ -n "${buildAndTestSubdir-}" ]]; then
popd
Expand Down
30 changes: 17 additions & 13 deletions pkgs/build-support/rust/hooks/maturin-build-hook.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# shellcheck shell=bash disable=SC2154,SC2164

maturinBuildHook() {
echo "Executing maturinBuildHook"

Expand All @@ -6,24 +8,26 @@ maturinBuildHook() {
# Put the wheel to dist/ so that regular Python tooling can find it.
local dist="$PWD/dist"

if [ ! -z "${buildAndTestSubdir-}" ]; then
if [ -n "${buildAndTestSubdir-}" ]; then
pushd "${buildAndTestSubdir}"
fi

(
set -x
@setEnv@ maturin build \
--jobs=$NIX_BUILD_CORES \
--offline \
--target @rustTargetPlatformSpec@ \
--manylinux off \
--strip \
--release \
--out "$dist" \
${maturinBuildFlags-}
local flagsArray=(
"--jobs=$NIX_BUILD_CORES"
"--offline"
"--target" "@rustTargetPlatformSpec@"
"--manylinux" "off"
"--strip"
"--release"
"--out" "$dist"
)

if [ ! -z "${buildAndTestSubdir-}" ]; then
concatTo flagsArray maturinBuildFlags

echoCmd 'maturinBuildHook flags' "${flagsArray[@]}"
@setEnv@ maturin build "${flagsArray[@]}"

if [ -n "${buildAndTestSubdir-}" ]; then
popd
fi

Expand Down
2 changes: 1 addition & 1 deletion pkgs/development/tools/rust/rust-analyzer/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rustPlatform.buildRustPackage rec {

buildFeatures = lib.optional useMimalloc "mimalloc";

CFG_RELEASE = version;
env.CFG_RELEASE = version;

inherit doCheck;
preCheck = lib.optionalString doCheck ''
Expand Down

0 comments on commit bf5f0b6

Please sign in to comment.