Allow passing arguments to hooks

This allows envHooks and crossEnvHooks to be handled using the regular
hook mechanism.
This commit is contained in:
Eelco Dolstra 2014-07-08 14:14:28 +02:00
parent 2def8e7499
commit 11dc9036d0
1 changed files with 21 additions and 20 deletions

View File

@ -13,11 +13,12 @@ set -e
# <hookName>, and the values of the shell array <hookName>Hooks. # <hookName>, and the values of the shell array <hookName>Hooks.
runHook() { runHook() {
local hookName="$1" local hookName="$1"
shift
local var="$hookName" local var="$hookName"
if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi
eval "local -a dummy=(\"\${$var[@]}\")" eval "local -a dummy=(\"\${$var[@]}\")"
for hook in "_callImplicitHook 0 $hookName" "${dummy[@]}"; do for hook in "_callImplicitHook 0 $hookName" "${dummy[@]}"; do
if ! _eval "$hook"; then return 1; fi if ! _eval "$hook" "$@"; then return 1; fi
done done
return 0 return 0
} }
@ -27,11 +28,12 @@ runHook() {
# zero exit code). If none succeed, return a non-zero exit code. # zero exit code). If none succeed, return a non-zero exit code.
runOneHook() { runOneHook() {
local hookName="$1" local hookName="$1"
shift
local var="$hookName" local var="$hookName"
if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi if [[ "$hookName" =~ Hook$ ]]; then var+=s; else var+=Hooks; fi
eval "local -a dummy=(\"\${$var[@]}\")" eval "local -a dummy=(\"\${$var[@]}\")"
for hook in "_callImplicitHook 1 $hookName" "${dummy[@]}"; do for hook in "_callImplicitHook 1 $hookName" "${dummy[@]}"; do
if _eval "$hook"; then if _eval "$hook" "$@"; then
return 0 return 0
fi fi
done done
@ -60,7 +62,12 @@ _callImplicitHook() {
# hooks exits the hook, not the caller. # hooks exits the hook, not the caller.
_eval() { _eval() {
local code="$1" local code="$1"
eval "$code" shift
if [ "$(type -t $code)" = function ]; then
eval "$code \"\$@\""
else
eval "$code"
fi
} }
@ -175,10 +182,6 @@ export CONFIG_SHELL="$SHELL"
if [ -z "$shell" ]; then export shell=$SHELL; fi if [ -z "$shell" ]; then export shell=$SHELL; fi
envHooks=()
crossEnvHooks=()
# Allow the caller to augment buildInputs (it's not always possible to # Allow the caller to augment buildInputs (it's not always possible to
# do this before the call to setup.sh, since the PATH is empty at that # do this before the call to setup.sh, since the PATH is empty at that
# point; here we have a basic Unix environment). # point; here we have a basic Unix environment).
@ -235,9 +238,7 @@ _addToNativeEnv() {
fi fi
# Run the package-specific hooks set by the setup-hook scripts. # Run the package-specific hooks set by the setup-hook scripts.
for i in "${envHooks[@]}"; do runHook envHook "$pkg"
$i $pkg
done
} }
for i in $nativePkgs; do for i in $nativePkgs; do
@ -255,9 +256,7 @@ _addToCrossEnv() {
fi fi
# Run the package-specific hooks set by the setup-hook scripts. # Run the package-specific hooks set by the setup-hook scripts.
for i in "${crossEnvHooks[@]}"; do runHook crossEnvHook "$pkg"
$i $pkg
done
} }
for i in $crossPkgs; do for i in $crossPkgs; do
@ -459,22 +458,24 @@ stripHash() {
unpackCmdHooks+=(_defaultUnpack) unpackCmdHooks+=(_defaultUnpack)
_defaultUnpack() { _defaultUnpack() {
if [ -d "$curSrc" ]; then local fn="$1"
stripHash "$curSrc" if [ -d "$fn" ]; then
cp -prd --no-preserve=timestamps "$curSrc" $strippedName
stripHash "$fn"
cp -prd --no-preserve=timestamps "$fn" $strippedName
else else
case "$curSrc" in case "$fn" in
*.tar.xz | *.tar.lzma) *.tar.xz | *.tar.lzma)
# Don't rely on tar knowing about .xz. # Don't rely on tar knowing about .xz.
xz -d < "$curSrc" | tar xf - xz -d < "$fn" | tar xf -
;; ;;
*.tar | *.tar.* | *.tgz | *.tbz2) *.tar | *.tar.* | *.tgz | *.tbz2)
# GNU tar can automatically select the decompression method # GNU tar can automatically select the decompression method
# (info "(tar) gzip"). # (info "(tar) gzip").
tar xf "$curSrc" tar xf "$fn"
;; ;;
*) *)
return 1 return 1
@ -488,7 +489,7 @@ _defaultUnpack() {
unpackFile() { unpackFile() {
curSrc="$1" curSrc="$1"
header "unpacking source archive $curSrc" 3 header "unpacking source archive $curSrc" 3
if ! runOneHook unpackCmd; then if ! runOneHook unpackCmd "$curSrc"; then
echo "do not know how to unpack source archive $curSrc" echo "do not know how to unpack source archive $curSrc"
exit 1 exit 1
fi fi