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

@ -123,7 +123,7 @@ release_archive="$${RELEASE}"
# TODO: If a release is set, we assume it's set to a branch name.
# thus we default the archive value to a commit. This is likely
# only appropriate when publishing on the branch in specified
# and a smarter solution should be found to avoid unexpected behavior.
# and a smarter solution should be found to avoid unexpected behavior.
if [[ -n "\\$${RELEASE:-}" ]]; then
release="\\$${RELEASE}"
release_archive="\\$${commit}"

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,19 +42,23 @@ done < <(find . -name "*${SHARED_LIB_SUFFIX}" -print0)
# Add paths to shared library directories to SHARED_LIBS_DIRS_ARRAY
SHARED_LIBS_DIRS_ARRAY=()
for lib in "${SHARED_LIBS_ARRAY[@]}"; do
SHARED_LIBS_DIRS_ARRAY+=($(dirname $(realpath $lib)))
done
if [ ${#SHARED_LIBS_ARRAY[@]} -ne 0 ]; then
for lib in "${SHARED_LIBS_ARRAY[@]}"; do
SHARED_LIBS_DIRS_ARRAY+=($(dirname $(realpath $lib)))
done
fi
# Remove duplicates from array
IFS=" " read -r -a SHARED_LIBS_DIRS_ARRAY <<< "$(tr ' ' '\n' <<< "${SHARED_LIBS_DIRS_ARRAY[@]}" | sort -u | tr '\n' ' ')"
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' ' ')"
# Allow unbound variable here, in case LD_LIBRARY_PATH or similar is not already set
set +u
for dir in "${SHARED_LIBS_DIRS_ARRAY[@]}"; do
export ${LIB_PATH_VAR}="$dir":"${!LIB_PATH_VAR}"
done
set -u
# Allow unbound variable here, in case LD_LIBRARY_PATH or similar is not already set
set +u
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} "$@"