2014-06-27 11:33:05 +00:00
|
|
|
# This setup hook causes the fixup phase to rewrite all script
|
|
|
|
# interpreter file names (`#! /path') to paths found in $PATH. E.g.,
|
|
|
|
# /bin/sh will be rewritten to /nix/store/<hash>-some-bash/bin/sh.
|
|
|
|
# /usr/bin/env gets special treatment so that ".../bin/env python" is
|
|
|
|
# rewritten to /nix/store/<hash>/bin/python. Interpreters that are
|
|
|
|
# already in the store are left untouched.
|
|
|
|
|
2018-10-31 20:17:24 +00:00
|
|
|
fixupOutputHooks+=(patchShebangsAuto)
|
|
|
|
|
|
|
|
# Run patch shebangs on a directory.
|
|
|
|
# patchShebangs [--build | --host] directory
|
|
|
|
|
|
|
|
# Flags:
|
|
|
|
# --build : Lookup commands available at build-time
|
|
|
|
# --host : Lookup commands available at runtime
|
|
|
|
|
|
|
|
# Example use cases,
|
|
|
|
# $ patchShebangs --host /nix/store/...-hello-1.0/bin
|
|
|
|
# $ patchShebangs --build configure
|
2014-06-27 11:33:05 +00:00
|
|
|
|
|
|
|
patchShebangs() {
|
2018-10-31 20:17:24 +00:00
|
|
|
local pathName
|
|
|
|
|
|
|
|
if [ "$1" = "--host" ]; then
|
|
|
|
pathName=HOST_PATH
|
|
|
|
shift
|
|
|
|
elif [ "$1" = "--build" ]; then
|
|
|
|
pathName=PATH
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2014-06-27 11:33:05 +00:00
|
|
|
local dir="$1"
|
2018-10-31 20:17:24 +00:00
|
|
|
|
2014-06-27 11:33:05 +00:00
|
|
|
header "patching script interpreter paths in $dir"
|
|
|
|
local f
|
|
|
|
local oldPath
|
|
|
|
local newPath
|
|
|
|
local arg0
|
|
|
|
local args
|
|
|
|
local oldInterpreterLine
|
|
|
|
local newInterpreterLine
|
|
|
|
|
2018-09-28 16:21:51 +00:00
|
|
|
[ -e "$dir" ] || return 0
|
|
|
|
|
|
|
|
local f
|
|
|
|
while IFS= read -r -d $'\0' f; do
|
2018-09-28 16:17:33 +00:00
|
|
|
isScript "$f" || continue
|
2014-06-27 11:33:05 +00:00
|
|
|
|
2015-11-07 03:45:26 +00:00
|
|
|
oldInterpreterLine=$(head -1 "$f" | tail -c+3)
|
2014-06-27 11:33:05 +00:00
|
|
|
read -r oldPath arg0 args <<< "$oldInterpreterLine"
|
|
|
|
|
2018-10-31 20:17:24 +00:00
|
|
|
if [ -z "$pathName" ]; then
|
|
|
|
if [ -n "$strictDeps" ] && [[ "$f" = "$NIX_STORE"* ]]; then
|
|
|
|
pathName=HOST_PATH
|
|
|
|
else
|
|
|
|
pathName=PATH
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-06-27 11:33:05 +00:00
|
|
|
if $(echo "$oldPath" | grep -q "/bin/env$"); then
|
|
|
|
# Check for unsupported 'env' functionality:
|
|
|
|
# - options: something starting with a '-'
|
|
|
|
# - environment variables: foo=bar
|
|
|
|
if $(echo "$arg0" | grep -q -- "^-.*\|.*=.*"); then
|
2018-08-28 09:06:04 +00:00
|
|
|
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)"
|
2014-06-27 11:33:05 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2018-10-31 20:17:24 +00:00
|
|
|
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)"
|
2014-06-27 11:33:05 +00:00
|
|
|
else
|
|
|
|
if [ "$oldPath" = "" ]; then
|
|
|
|
# If no interpreter is specified linux will use /bin/sh. Set
|
|
|
|
# oldpath="/bin/sh" so that we get /nix/store/.../sh.
|
|
|
|
oldPath="/bin/sh"
|
|
|
|
fi
|
2018-10-31 20:17:24 +00:00
|
|
|
|
|
|
|
newPath="$(PATH="${!pathName}" command -v "$(basename "$oldPath")" || true)"
|
|
|
|
|
2014-06-27 11:33:05 +00:00
|
|
|
args="$arg0 $args"
|
|
|
|
fi
|
|
|
|
|
2016-07-07 20:35:08 +00:00
|
|
|
# Strip trailing whitespace introduced when no arguments are present
|
2016-07-09 00:05:11 +00:00
|
|
|
newInterpreterLine="$(echo "$newPath $args" | sed 's/[[:space:]]*$//')"
|
2014-06-27 11:33:05 +00:00
|
|
|
|
|
|
|
if [ -n "$oldPath" -a "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then
|
|
|
|
if [ -n "$newPath" -a "$newPath" != "$oldPath" ]; then
|
|
|
|
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
|
|
|
|
# escape the escape chars so that sed doesn't interpret them
|
|
|
|
escapedInterpreterLine=$(echo "$newInterpreterLine" | sed 's|\\|\\\\|g')
|
2018-01-01 17:05:46 +00:00
|
|
|
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
|
|
|
|
touch -r "$f" "$f.timestamp"
|
2014-06-27 11:33:05 +00:00
|
|
|
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
|
2018-01-01 17:05:46 +00:00
|
|
|
touch -r "$f.timestamp" "$f"
|
|
|
|
rm "$f.timestamp"
|
2014-06-27 11:33:05 +00:00
|
|
|
fi
|
|
|
|
fi
|
2018-09-28 16:21:51 +00:00
|
|
|
done < <(find "$dir" -type f -perm -0100 -print0)
|
2014-06-27 11:33:05 +00:00
|
|
|
|
|
|
|
stopNest
|
|
|
|
}
|
2018-10-31 20:17:24 +00:00
|
|
|
|
|
|
|
patchShebangsAuto () {
|
|
|
|
if [ -z "$dontPatchShebangs" -a -e "$prefix" ]; then
|
|
|
|
|
|
|
|
# Dev output will end up being run on the build platform. An
|
|
|
|
# example case of this is sdl2-config. Otherwise, we can just
|
|
|
|
# use the runtime path (--host).
|
|
|
|
if [ "$output" != out ] && [ "$output" = "${!outputDev}" ]; then
|
|
|
|
patchShebangs --build "$prefix"
|
|
|
|
else
|
|
|
|
patchShebangs --host "$prefix"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|