Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase resiliency of runnable_binary #1134

Merged
Merged
2 changes: 1 addition & 1 deletion docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
47 changes: 26 additions & 21 deletions foreign_cc/private/runnable_binary_wrapper.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}"
mishazharov marked this conversation as resolved.
Show resolved Hide resolved

EXE=EXECUTABLE
EXE_PATH=$(rlocation "${EXE#external/}")
mishazharov marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -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

# 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
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' ' ')"

# 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} "$@"
Loading