From ab4c35982269e8f1f15e07afa7fedf65c749f276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 4 Aug 2020 06:39:51 +0100 Subject: [PATCH] stdenv/patchShebangs: use builtins where possible builtins for small input sizes should be faster due to less forking. --- pkgs/build-support/setup-hooks/patch-shebangs.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkgs/build-support/setup-hooks/patch-shebangs.sh b/pkgs/build-support/setup-hooks/patch-shebangs.sh index b48b0c50f577..fa90a6fd8054 100644 --- a/pkgs/build-support/setup-hooks/patch-shebangs.sh +++ b/pkgs/build-support/setup-hooks/patch-shebangs.sh @@ -50,8 +50,8 @@ patchShebangs() { while IFS= read -r -d $'\0' f; do isScript "$f" || continue - oldInterpreterLine=$(head -1 "$f" | tail -c+3) - read -r oldPath arg0 args <<< "$oldInterpreterLine" + read -r oldInterpreterLine < "$f" + read -r oldPath arg0 args <<< "${oldInterpreterLine:3}" if [ -z "$pathName" ]; then if [ -n "$strictDeps" ] && [[ "$f" = "$NIX_STORE"* ]]; then @@ -61,11 +61,11 @@ patchShebangs() { fi fi - if $(echo "$oldPath" | grep -q "/bin/env$"); then + if [[ "$oldPath" == *"/bin/env" ]]; then # Check for unsupported 'env' functionality: # - options: something starting with a '-' # - 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 exit 1 fi @@ -84,13 +84,15 @@ patchShebangs() { fi # 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 "$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') + escapedInterpreterLine=${newInterpreterLine//\\/\\\\} + # Preserve times, see: https://github.com/NixOS/nixpkgs/pull/33281 timestamp=$(mktemp) touch -r "$f" "$timestamp"