Skip to content

Commit eb28e5e

Browse files
committed
stdenv: log build hooks as they run
1 parent d49be0b commit eb28e5e

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

doc/stdenv/stdenv.chapter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ The propagated equivalent of `depsTargetTarget`. This is prefixed for the same r
432432

433433
#### `NIX_DEBUG` {#var-stdenv-NIX_DEBUG}
434434

435-
A number between 0 and 7 indicating how much information to log. If set to 1 or higher, `stdenv` will print moderate debugging information during the build. In particular, the `gcc` and `ld` wrapper scripts will print out the complete command line passed to the wrapped tools. If set to 6 or higher, the `stdenv` setup script will be run with `set -x` tracing. If set to 7 or higher, the `gcc` and `ld` wrapper scripts will also be run with `set -x` tracing.
435+
A number between 0 and 7 indicating how much information to log. If set to 1 or higher, `stdenv` will print moderate debugging information during the build. In particular, the `gcc` and `ld` wrapper scripts will print out the complete command line passed to the wrapped tools. If set to 2 or higher, multiline build hooks will be printed literally. If set to 6 or higher, the `stdenv` setup script will be run with `set -x` tracing. If set to 7 or higher, the `gcc` and `ld` wrapper scripts will also be run with `set -x` tracing.
436436

437437
### Attributes affecting build properties {#attributes-affecting-build-properties}
438438

pkgs/stdenv/generic/setup.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ getAllOutputNames() {
5050
######################################################################
5151
# Hook handling.
5252

53+
# Log a hook, to be run before the hook is actually called.
54+
# logging for "implicit" hooks -- the ones specified directly
55+
# in derivation's arguments -- is done in _callImplicitHook instead.
56+
_logHook() {
57+
local hookKind="$1"
58+
local hookExpr="$2"
59+
shift 2
60+
61+
if declare -F "$hookExpr" > /dev/null 2>&1; then
62+
echo "calling '$hookKind' function hook '$hookExpr'" "$@"
63+
elif type -p "$hookExpr" > /dev/null; then
64+
echo "sourcing '$hookKind' script hook '$hookExpr'"
65+
elif [[ "$hookExpr" != "_callImplicitHook"* ]]; then
66+
# Here we have a string hook to eval.
67+
# Join lines onto one with literal \n characters unless NIX_DEBUG >= 2.
68+
local exprToOutput
69+
if (( "${NIX_DEBUG:-0}" >= 2 )); then
70+
exprToOutput="$hookExpr"
71+
else
72+
while IFS= read -r hookExprLine; do
73+
# These lines often have indentation,
74+
# so let's removing leading whitespace.
75+
hookExprLine="${hookExprLine#"${hookExprLine%%[![:space:]]*}"}"
76+
# If this line wasn't entirely whitespace,
77+
# then add it to our output.
78+
if [[ -n "$hookExprLine" ]]; then
79+
exprToOutput+="$hookExprLine\\n "
80+
fi
81+
done <<< "$hookExpr"
82+
83+
# And then remove the final, unnecessary, \n
84+
exprToOutput="${exprToOutput%%\\n }"
85+
fi
86+
echo "evaling '$hookKind' string hook '$exprToOutput'"
87+
fi
88+
}
5389

5490
# Run all hooks with the specified name in the order in which they
5591
# were added, stopping if any fails (returns a non-zero exit
@@ -64,6 +100,7 @@ runHook() {
64100
# Hack around old bash being bad and thinking empty arrays are
65101
# undefined.
66102
for hook in "_callImplicitHook 0 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do
103+
_logHook "$hookName" "$hook" "$@"
67104
_eval "$hook" "$@"
68105
done
69106

@@ -81,6 +118,7 @@ runOneHook() {
81118
local hook ret=1
82119
# Hack around old bash like above
83120
for hook in "_callImplicitHook 1 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do
121+
_logHook "$hookName" "$hook" "$@"
84122
if _eval "$hook" "$@"; then
85123
ret=0
86124
break
@@ -100,10 +138,13 @@ _callImplicitHook() {
100138
local def="$1"
101139
local hookName="$2"
102140
if declare -F "$hookName" > /dev/null; then
141+
echo "calling implicit '$hookName' function hook"
103142
"$hookName"
104143
elif type -p "$hookName" > /dev/null; then
144+
echo "sourcing implicit '$hookName' script hook"
105145
source "$hookName"
106146
elif [ -n "${!hookName:-}" ]; then
147+
echo "evaling implicit '$hookName' string hook"
107148
eval "${!hookName}"
108149
else
109150
return "$def"
@@ -644,6 +685,7 @@ activatePackage() {
644685
(( hostOffset <= targetOffset )) || exit 1
645686

646687
if [ -f "$pkg" ]; then
688+
echo "sourcing setup hook '$pkg'"
647689
source "$pkg"
648690
fi
649691

@@ -667,6 +709,7 @@ activatePackage() {
667709
fi
668710

669711
if [[ -f "$pkg/nix-support/setup-hook" ]]; then
712+
echo "sourcing setup hook '$pkg/nix-support/setuphook'"
670713
source "$pkg/nix-support/setup-hook"
671714
fi
672715
}

0 commit comments

Comments
 (0)