Increase resiliency of `runnable_binary` (#1134)

This commit is contained in:
Misha Zharov 2024-02-01 12:21:01 -08:00 committed by GitHub
parent d873cace68
commit 4831827e29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 20 deletions

View File

@ -1,13 +1,5 @@
#!/usr/bin/env bash
# RUN_UNDER_RUNFILES is set in the "bazel test" environment, where all transitive runfiles are placed into one directory
# Otherwise, first cd to the runfiles dir for the wrapped executable before searching for shared libraries for the wrapped executable
if [[ -z $RUN_UNDER_RUNFILES ]]; then
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
RUNFILES_DIR=${SCRIPT_DIR}/SH_BINARY_FILENAME.runfiles
fi
cd ${RUNFILES_DIR}
# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2. (@bazel_tools//tools/bash/runfiles)
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
@ -19,6 +11,15 @@ source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---
if [[ ! -d "${RUNFILES_DIR}" ]]; then
>&2 echo "RUNFILES_DIR is set to '${RUNFILES_DIR}' which does not exist";
exit 1;
fi
RUNFILES_DIR=$( cd "${RUNFILES_DIR}" ; pwd -P )
cd "${RUNFILES_DIR}"
EXE=EXECUTABLE
EXE_PATH=$(rlocation "${EXE#external/}")
@ -41,10 +42,13 @@ done < <(find . -name "*${SHARED_LIB_SUFFIX}" -print0)
# Add paths to shared library directories to SHARED_LIBS_DIRS_ARRAY
SHARED_LIBS_DIRS_ARRAY=()
if [ ${#SHARED_LIBS_ARRAY[@]} -ne 0 ]; then
for lib in "${SHARED_LIBS_ARRAY[@]}"; do
SHARED_LIBS_DIRS_ARRAY+=($(dirname $(realpath $lib)))
done
fi
if [ ${#SHARED_LIBS_DIRS_ARRAY[@]} -ne 0 ]; then
# Remove duplicates from array
IFS=" " read -r -a SHARED_LIBS_DIRS_ARRAY <<< "$(tr ' ' '\n' <<< "${SHARED_LIBS_DIRS_ARRAY[@]}" | sort -u | tr '\n' ' ')"
@ -54,6 +58,7 @@ for dir in "${SHARED_LIBS_DIRS_ARRAY[@]}"; do
export ${LIB_PATH_VAR}="$dir":"${!LIB_PATH_VAR}"
done
set -u
fi
cd - &> /dev/null
exec ${EXE_PATH} "$@"