Merge pull request #94642 from Mic92/patch-shebangs

This commit is contained in:
Jörg Thalheim 2021-02-04 21:20:40 +00:00 committed by GitHub
commit 2dcc858efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,10 +24,10 @@ fixupOutputHooks+=(patchShebangsAuto)
patchShebangs() { patchShebangs() {
local pathName local pathName
if [ "$1" = "--host" ]; then if [[ "$1" == "--host" ]]; then
pathName=HOST_PATH pathName=HOST_PATH
shift shift
elif [ "$1" = "--build" ]; then elif [[ "$1" == "--build" ]]; then
pathName=PATH pathName=PATH
shift shift
fi fi
@ -41,7 +41,7 @@ patchShebangs() {
local oldInterpreterLine local oldInterpreterLine
local newInterpreterLine local newInterpreterLine
if [ $# -eq 0 ]; then if [[ $# -eq 0 ]]; then
echo "No arguments supplied to patchShebangs" >&2 echo "No arguments supplied to patchShebangs" >&2
return 0 return 0
fi fi
@ -50,29 +50,29 @@ patchShebangs() {
while IFS= read -r -d $'\0' f; do while IFS= read -r -d $'\0' f; do
isScript "$f" || continue isScript "$f" || continue
oldInterpreterLine=$(head -1 "$f" | tail -c+3) read -r oldInterpreterLine < "$f"
read -r oldPath arg0 args <<< "$oldInterpreterLine" read -r oldPath arg0 args <<< "${oldInterpreterLine:3}"
if [ -z "$pathName" ]; then if [[ -z "$pathName" ]]; then
if [ -n "$strictDeps" ] && [[ "$f" = "$NIX_STORE"* ]]; then if [[ -n $strictDeps && $f == "$NIX_STORE"* ]]; then
pathName=HOST_PATH pathName=HOST_PATH
else else
pathName=PATH pathName=PATH
fi fi
fi fi
if $(echo "$oldPath" | grep -q "/bin/env$"); then if [[ "$oldPath" == *"/bin/env" ]]; then
# Check for unsupported 'env' functionality: # Check for unsupported 'env' functionality:
# - options: something starting with a '-' # - options: something starting with a '-'
# - environment variables: foo=bar # - environment variables: foo=bar
if $(echo "$arg0" | grep -q -- "^-.*\|.*=.*"); then if [[ $arg0 == "-"* || $arg0 == *"="* ]]; then
echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2 echo "$f: unsupported interpreter directive \"$oldInterpreterLine\" (set dontPatchShebangs=1 and handle shebang patching yourself)" >&2
exit 1 exit 1
fi fi
newPath="$(PATH="${!pathName}" command -v "$arg0" || true)" newPath="$(PATH="${!pathName}" command -v "$arg0" || true)"
else else
if [ "$oldPath" = "" ]; then if [[ -z $oldPath ]]; then
# If no interpreter is specified linux will use /bin/sh. Set # If no interpreter is specified linux will use /bin/sh. Set
# oldpath="/bin/sh" so that we get /nix/store/.../sh. # oldpath="/bin/sh" so that we get /nix/store/.../sh.
oldPath="/bin/sh" oldPath="/bin/sh"
@ -84,19 +84,19 @@ patchShebangs() {
fi fi
# Strip trailing whitespace introduced when no arguments are present # Strip trailing whitespace introduced when no arguments are present
newInterpreterLine="$(echo "$newPath $args" | sed 's/[[:space:]]*$//')" newInterpreterLine="$newPath $args"
newInterpreterLine=${newInterpreterLine%${newInterpreterLine##*[![:space:]]}}
if [ -n "$oldPath" -a "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then if [[ -n "$oldPath" && "${oldPath:0:${#NIX_STORE}}" != "$NIX_STORE" ]]; then
if [ -n "$newPath" -a "$newPath" != "$oldPath" ]; then if [[ -n "$newPath" && "$newPath" != "$oldPath" ]]; then
echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\"" echo "$f: interpreter directive changed from \"$oldInterpreterLine\" to \"$newInterpreterLine\""
# escape the escape chars so that sed doesn't interpret them # escape the escape chars so that sed doesn't interpret them
escapedInterpreterLine=$(echo "$newInterpreterLine" | sed 's|\\|\\\\|g') escapedInterpreterLine=${newInterpreterLine//\\/\\\\}
# Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281 # Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281
timestamp=$(mktemp) timestamp=$(stat --printf "%y" "$f")
touch -r "$f" "$timestamp"
sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f" sed -i -e "1 s|.*|#\!$escapedInterpreterLine|" "$f"
touch -r "$timestamp" "$f" touch --date "$timestamp" "$f"
rm "$timestamp"
fi fi
fi fi
done < <(find "$@" -type f -perm -0100 -print0) done < <(find "$@" -type f -perm -0100 -print0)
@ -105,12 +105,12 @@ patchShebangs() {
} }
patchShebangsAuto () { patchShebangsAuto () {
if [ -z "${dontPatchShebangs-}" -a -e "$prefix" ]; then if [[ -z "${dontPatchShebangs-}" && -e "$prefix" ]]; then
# Dev output will end up being run on the build platform. An # Dev output will end up being run on the build platform. An
# example case of this is sdl2-config. Otherwise, we can just # example case of this is sdl2-config. Otherwise, we can just
# use the runtime path (--host). # use the runtime path (--host).
if [ "$output" != out ] && [ "$output" = "$outputDev" ]; then if [[ "$output" != out && "$output" = "$outputDev" ]]; then
patchShebangs --build "$prefix" patchShebangs --build "$prefix"
else else
patchShebangs --host "$prefix" patchShebangs --host "$prefix"