nixpkgs/pkgs/build-support/make-wrapper/make-wrapper.sh
Eelco Dolstra b00839299f * makeWrapper: new function that transparently takes care of creating
a wrapper script.  E.g.

    wrapProgram $out/bin/prog --set FOO bar

  will create a wrapper around "prog" so that the environment variable
  FOO is set to bar.  The original program is renamed to
  $out/bin/.wrapped-prog.
  
* Function makeSetupHook to convert a script into a setup hook which
  can be included in buildInputs.  This makes it easier to use scripts
  like makeWrapper.

* Added Bazaar, yet another distributed version management system.

svn path=/nixpkgs/trunk/; revision=10211
2008-01-18 10:29:58 +00:00

83 lines
2.2 KiB
Bash

makeWrapper() {
original=$1
wrapper=$2
ensureDir "$(dirname $wrapper)"
echo "#! $SHELL -e" > $wrapper
params=("$@")
for ((n = 2; n < ${#params[*]}; n += 1)); do
p=${params[$n]}
if test "$p" = "--set"; then
varName=${params[$((n + 1))]}
value=${params[$((n + 2))]}
n=$((n + 2))
echo "export $varName=$value" >> $wrapper
fi
if test "$p" = "--suffix" -o "$p" = "--prefix"; then
varName=${params[$((n + 1))]}
separator=${params[$((n + 2))]}
value=${params[$((n + 3))]}
n=$((n + 3))
if test -n "$value"; then
if test "$p" = "--suffix"; then
echo "export $varName=\$$varName\${$varName:+$separator}$value" >> $wrapper
else
echo "export $varName=$value\${$varName:+$separator}\$$varName" >> $wrapper
fi
fi
fi
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
if test "$p" = "--suffix-contents"; then
varName=${params[$((n + 1))]}
separator=${params[$((n + 2))]}
fileNames=${params[$((n + 3))]}
n=$((n + 3))
for fileName in $fileNames; do
echo "export $varName=\$$varName\${$varName:+$separator}$(cat $fileName)" >> $wrapper
done
fi
done
echo "exec \"$original\" \"\$@\"" >> $wrapper
chmod +x $wrapper
}
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
}
# Syntax: wrapProgram <PROGRAM> <MAKE-WRAPPER FLAGS...>
wrapProgram() {
local prog="$1"
local hidden="$(dirname "$prog")/.wrapped-$(basename "$prog")"
mv $prog $hidden
makeWrapper $hidden $prog "$@"
}