2016-10-07 15:55:16 +02: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 12:13:18 +01:00
|
|
|
|
makeWrapper() {
|
2017-03-05 11:56:30 +01:00
|
|
|
|
local original="$1"
|
|
|
|
|
local wrapper="$2"
|
2008-01-18 12:28:41 +01:00
|
|
|
|
local params varName value command separator n fileNames
|
2017-03-05 11:56:30 +01:00
|
|
|
|
local argv0 flagsBefore flags
|
2005-02-16 12:13:18 +01:00
|
|
|
|
|
2017-03-05 11:56:30 +01:00
|
|
|
|
mkdir -p "$(dirname "$wrapper")"
|
2005-02-16 12:13:18 +01:00
|
|
|
|
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "#! $SHELL -e" > "$wrapper"
|
2005-02-16 12:13:18 +01:00
|
|
|
|
|
|
|
|
|
params=("$@")
|
|
|
|
|
for ((n = 2; n < ${#params[*]}; n += 1)); do
|
2017-03-05 11:56:30 +01:00
|
|
|
|
p="${params[$n]}"
|
2005-02-16 12:13:18 +01:00
|
|
|
|
|
2005-03-09 20:07:23 +01:00
|
|
|
|
if test "$p" = "--set"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
varName="${params[$((n + 1))]}"
|
|
|
|
|
value="${params[$((n + 2))]}"
|
2005-03-09 20:07:23 +01:00
|
|
|
|
n=$((n + 2))
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=\"$value\"" >> "$wrapper"
|
2005-03-09 20:07:23 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2016-02-28 14:27:43 +01:00
|
|
|
|
if test "$p" = "--unset"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
varName="${params[$((n + 1))]}"
|
2016-02-28 14:27:43 +01:00
|
|
|
|
n=$((n + 1))
|
|
|
|
|
echo "unset $varName" >> "$wrapper"
|
|
|
|
|
fi
|
|
|
|
|
|
2008-01-18 12:28:41 +01:00
|
|
|
|
if test "$p" = "--run"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
command="${params[$((n + 1))]}"
|
2008-01-18 12:28:41 +01:00
|
|
|
|
n=$((n + 1))
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "$command" >> "$wrapper"
|
2008-01-18 12:28:41 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2005-03-13 21:43:04 +01:00
|
|
|
|
if test "$p" = "--suffix" -o "$p" = "--prefix"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
varName="${params[$((n + 1))]}"
|
|
|
|
|
separator="${params[$((n + 2))]}"
|
|
|
|
|
value="${params[$((n + 3))]}"
|
2005-02-16 12:13:18 +01:00
|
|
|
|
n=$((n + 3))
|
2005-02-16 12:24:40 +01:00
|
|
|
|
if test -n "$value"; then
|
2005-03-13 21:43:04 +01:00
|
|
|
|
if test "$p" = "--suffix"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> "$wrapper"
|
2005-03-13 21:43:04 +01:00
|
|
|
|
else
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=$value\${$varName:+$separator}\$$varName" >> "$wrapper"
|
2005-03-13 21:43:04 +01:00
|
|
|
|
fi
|
2005-02-16 12:24:40 +01:00
|
|
|
|
fi
|
2005-02-16 12:13:18 +01:00
|
|
|
|
fi
|
2005-02-16 12:38:52 +01:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--suffix-each"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
varName="${params[$((n + 1))]}"
|
|
|
|
|
separator="${params[$((n + 2))]}"
|
|
|
|
|
values="${params[$((n + 3))]}"
|
2005-02-16 12:38:52 +01:00
|
|
|
|
n=$((n + 3))
|
|
|
|
|
for value in $values; do
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> "$wrapper"
|
2005-02-16 12:38:52 +01:00
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
2009-07-16 13:22:37 +02:00
|
|
|
|
if test "$p" = "--suffix-contents" -o "$p" = "--prefix-contents"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
varName="${params[$((n + 1))]}"
|
|
|
|
|
separator="${params[$((n + 2))]}"
|
|
|
|
|
fileNames="${params[$((n + 3))]}"
|
2005-02-16 12:38:52 +01:00
|
|
|
|
n=$((n + 3))
|
|
|
|
|
for fileName in $fileNames; do
|
2009-07-16 13:22:37 +02:00
|
|
|
|
if test "$p" = "--suffix-contents"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=\$$varName\${$varName:+$separator}$(cat "$fileName")" >> "$wrapper"
|
2009-07-16 13:22:37 +02:00
|
|
|
|
else
|
2017-03-05 11:56:30 +01:00
|
|
|
|
echo "export $varName=$(cat "$fileName")\${$varName:+$separator}\$$varName" >> "$wrapper"
|
2009-07-16 13:22:37 +02:00
|
|
|
|
fi
|
2005-02-16 12:38:52 +01:00
|
|
|
|
done
|
|
|
|
|
fi
|
2008-06-14 22:45:58 +02:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--add-flags"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
flags="${params[$((n + 1))]}"
|
2008-06-14 22:45:58 +02:00
|
|
|
|
n=$((n + 1))
|
|
|
|
|
flagsBefore="$flagsBefore $flags"
|
|
|
|
|
fi
|
2015-08-30 17:41:43 +02:00
|
|
|
|
|
|
|
|
|
if test "$p" = "--argv0"; then
|
2017-03-05 11:56:30 +01:00
|
|
|
|
argv0="${params[$((n + 1))]}"
|
2015-08-30 17:41:43 +02:00
|
|
|
|
n=$((n + 1))
|
|
|
|
|
fi
|
2005-02-16 12:13:18 +01:00
|
|
|
|
done
|
|
|
|
|
|
2009-04-05 20:13:59 +02:00
|
|
|
|
# Note: extraFlagsArray is an array containing additional flags
|
|
|
|
|
# that may be set by --run actions.
|
2017-03-05 11:56:30 +01:00
|
|
|
|
# Silence warning about unexpanded extraFlagsArray:
|
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
|
echo exec ${argv0:+-a \"$argv0\"} \""$original"\" \
|
|
|
|
|
"$flagsBefore" '"${extraFlagsArray[@]}"' '"$@"' >> "$wrapper"
|
2015-08-21 01:28:31 +02:00
|
|
|
|
|
2017-03-05 11:56:30 +01:00
|
|
|
|
chmod +x "$wrapper"
|
2005-02-16 12:13:18 +01:00
|
|
|
|
}
|
2005-02-16 12:38:52 +01:00
|
|
|
|
|
|
|
|
|
addSuffix() {
|
2017-03-05 11:56:30 +01:00
|
|
|
|
suffix="$1"
|
2005-02-16 12:38:52 +01:00
|
|
|
|
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 11:29:58 +01:00
|
|
|
|
|
|
|
|
|
# Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...>
|
|
|
|
|
wrapProgram() {
|
|
|
|
|
local prog="$1"
|
2017-03-05 11:56:30 +01:00
|
|
|
|
local hidden
|
|
|
|
|
hidden="$(dirname "$prog")/.$(basename "$prog")"-wrapped
|
|
|
|
|
mv "$prog" "$hidden"
|
|
|
|
|
# Silence warning about unexpanded $0:
|
|
|
|
|
# shellcheck disable=SC2016
|
|
|
|
|
makeWrapper "$hidden" "$prog" --argv0 '$0' "$@"
|
2008-01-18 11:29:58 +01:00
|
|
|
|
}
|