2016-10-07 06:55:16 -07:00
|
|
|
|
# construct an executable file that wraps the actual executable
|
|
|
|
|
# makeWrapper EXECUTABLE ARGS
|
|
|
|
|
|
|
|
|
|
# ARGS:
|
|
|
|
|
# --argv0 NAME : set name of executed process to NAME
|
|
|
|
|
# (otherwise it’s called …-wrapped)
|
|
|
|
|
# --set VAR VAL : add VAR with value VAL to the executable’s environment
|
|
|
|
|
# --unset VAR : remove VAR from the environment
|
|
|
|
|
# --run COMMAND : run command before the executable
|
|
|
|
|
# The command can push extra flags to a magic list variable
|
|
|
|
|
# extraFlagsArray, which are then added to the invocation
|
|
|
|
|
# of the executable
|
|
|
|
|
# --add-flags FLAGS : add FLAGS to invocation of executable
|
|
|
|
|
|
|
|
|
|
# --prefix ENV SEP VAL : suffix/prefix ENV with VAL, separated by SEP
|
|
|
|
|
# --suffix
|
|
|
|
|
# --suffix-each ENV SEP VALS : like --suffix, but VALS is a list
|
|
|
|
|
# --prefix-contents ENV SEP FILES : like --suffix-each, but contents of FILES
|
|
|
|
|
# are read first and used as VALS
|
|
|
|
|
# --suffix-contents
|
2005-02-16 03:13:18 -08:00
|
|
|
|
makeWrapper() {
|
2008-01-18 03:28:41 -08:00
|
|
|
|
local original=$1
|
|
|
|
|
local wrapper=$2
|
|
|
|
|
local params varName value command separator n fileNames
|
2016-10-07 06:56:07 -07:00
|
|
|
|
local argv0 flagsBefore flags extraFlagsArray
|
2005-02-16 03:13:18 -08:00
|
|
|
|
|
2012-01-18 12:16:00 -08:00
|
|
|
|
mkdir -p "$(dirname $wrapper)"
|
2005-02-16 03:13:18 -08:00
|
|
|
|
|
|
|
|
|
echo "#! $SHELL -e" > $wrapper
|
|
|
|
|
|
|
|
|
|
params=("$@")
|
|
|
|
|
for ((n = 2; n < ${#params[*]}; n += 1)); do
|
|
|
|
|
p=${params[$n]}
|
|
|
|
|
|
2005-03-09 11:07:23 -08:00
|
|
|
|
if test "$p" = "--set"; then
|
|
|
|
|
varName=${params[$((n + 1))]}
|
|
|
|
|
value=${params[$((n + 2))]}
|
|
|
|
|
n=$((n + 2))
|
2016-06-19 04:38:46 -07:00
|
|
|
|
echo "export $varName=\"$value\"" >> $wrapper
|
2005-03-09 11:07:23 -08:00
|
|
|
|
fi
|
|
|
|
|
|
2016-02-28 05:27:43 -08:00
|
|
|
|
if test "$p" = "--unset"; then
|
|
|
|
|
varName=${params[$((n + 1))]}
|
|
|
|
|
n=$((n + 1))
|
|
|
|
|
echo "unset $varName" >> "$wrapper"
|
|
|
|
|
fi
|
|
|
|
|
|
2008-01-18 03:28:41 -08:00
|
|
|
|
if test "$p" = "--run"; then
|
|
|
|
|
command=${params[$((n + 1))]}
|
|
|
|
|
n=$((n + 1))
|
|
|
|
|
echo "$command" >> $wrapper
|
|
|
|
|
fi
|
|
|
|
|
|
2005-03-13 12:43:04 -08:00
|
|
|
|
if test "$p" = "--suffix" -o "$p" = "--prefix"; then
|
2005-02-16 03:13:18 -08:00
|
|
|
|
varName=${params[$((n + 1))]}
|
|
|
|
|
separator=${params[$((n + 2))]}
|
|
|
|
|
value=${params[$((n + 3))]}
|
|
|
|
|
n=$((n + 3))
|
2005-02-16 03:24:40 -08:00
|
|
|
|
if test -n "$value"; then
|
2005-03-13 12:43:04 -08:00
|
|
|
|
if test "$p" = "--suffix"; then
|
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> $wrapper
|
|
|
|
|
else
|
|
|
|
|
echo "export $varName=$value\${$varName:+$separator}\$$varName" >> $wrapper
|
|
|
|
|
fi
|
2005-02-16 03:24:40 -08:00
|
|
|
|
fi
|
2005-02-16 03:13:18 -08:00
|
|
|
|
fi
|
2005-02-16 03:38:52 -08:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--suffix-each"; then
|
|
|
|
|
varName=${params[$((n + 1))]}
|
|
|
|
|
separator=${params[$((n + 2))]}
|
|
|
|
|
values=${params[$((n + 3))]}
|
|
|
|
|
n=$((n + 3))
|
|
|
|
|
for value in $values; do
|
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> $wrapper
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
2009-07-16 04:22:37 -07:00
|
|
|
|
if test "$p" = "--suffix-contents" -o "$p" = "--prefix-contents"; then
|
2005-02-16 03:38:52 -08:00
|
|
|
|
varName=${params[$((n + 1))]}
|
|
|
|
|
separator=${params[$((n + 2))]}
|
|
|
|
|
fileNames=${params[$((n + 3))]}
|
|
|
|
|
n=$((n + 3))
|
|
|
|
|
for fileName in $fileNames; do
|
2009-07-16 04:22:37 -07:00
|
|
|
|
if test "$p" = "--suffix-contents"; then
|
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$(cat $fileName)" >> $wrapper
|
|
|
|
|
else
|
|
|
|
|
echo "export $varName=$(cat $fileName)\${$varName:+$separator}\$$varName" >> $wrapper
|
|
|
|
|
fi
|
2005-02-16 03:38:52 -08:00
|
|
|
|
done
|
|
|
|
|
fi
|
2008-06-14 13:45:58 -07:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--add-flags"; then
|
|
|
|
|
flags=${params[$((n + 1))]}
|
|
|
|
|
n=$((n + 1))
|
|
|
|
|
flagsBefore="$flagsBefore $flags"
|
|
|
|
|
fi
|
2015-08-30 08:41:43 -07:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--argv0"; then
|
|
|
|
|
argv0=${params[$((n + 1))]}
|
|
|
|
|
n=$((n + 1))
|
|
|
|
|
fi
|
2005-02-16 03:13:18 -08:00
|
|
|
|
done
|
|
|
|
|
|
2009-04-05 11:13:59 -07:00
|
|
|
|
# Note: extraFlagsArray is an array containing additional flags
|
|
|
|
|
# that may be set by --run actions.
|
2015-08-30 08:41:43 -07:00
|
|
|
|
echo exec ${argv0:+-a $argv0} "$original" \
|
|
|
|
|
$flagsBefore '"${extraFlagsArray[@]}"' '"$@"' >> $wrapper
|
2015-08-20 16:28:31 -07:00
|
|
|
|
|
2005-02-16 03:13:18 -08:00
|
|
|
|
chmod +x $wrapper
|
|
|
|
|
}
|
2005-02-16 03:38:52 -08:00
|
|
|
|
|
|
|
|
|
addSuffix() {
|
|
|
|
|
suffix=$1
|
|
|
|
|
shift
|
|
|
|
|
for name in "$@"; do
|
|
|
|
|
echo "$name$suffix"
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filterExisting() {
|
|
|
|
|
for fn in "$@"; do
|
|
|
|
|
if test -e "$fn"; then
|
|
|
|
|
echo "$fn"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
}
|
2008-01-18 02:29:58 -08:00
|
|
|
|
|
|
|
|
|
# Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...>
|
|
|
|
|
wrapProgram() {
|
|
|
|
|
local prog="$1"
|
2014-09-02 09:45:41 -07:00
|
|
|
|
local hidden="$(dirname "$prog")/.$(basename "$prog")"-wrapped
|
2008-01-18 02:29:58 -08:00
|
|
|
|
mv $prog $hidden
|
2015-08-30 08:41:43 -07:00
|
|
|
|
makeWrapper $hidden $prog --argv0 '"$0"' "$@"
|
2008-01-18 02:29:58 -08:00
|
|
|
|
}
|