runfiles done right

This commit is contained in:
Stuart Axelbrooke 2025-04-18 11:40:54 -07:00
parent daba2e2b12
commit 8ca2b4a00b
No known key found for this signature in database
GPG key ID: 1B0A848C29D46A35
3 changed files with 43 additions and 33 deletions

View file

@ -1,6 +1,14 @@
#!/bin/bash
set -e
# TODO should this be extracted to shared init script
# Get the directory where the script is located
if [[ -z "${RUNFILES_DIR:-}" ]]; then
SCRIPT_DIR="$(readlink -f "${BASH_SOURCE[0]}")"
# Set RUNFILES_DIR relative to the script location
export RUNFILES_DIR="${SCRIPT_DIR}.runfiles"
fi
# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
@ -12,8 +20,6 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---
CONFIGURE_BINARY="%{CONFIGURE_PATH}"
# Set up JAVA_RUNFILES if not already set
if [[ -z "${JAVA_RUNFILES:-}" ]]; then
if [[ -d "$0.runfiles" ]]; then
@ -23,16 +29,7 @@ if [[ -z "${JAVA_RUNFILES:-}" ]]; then
fi
fi
if [[ ! -x "$CONFIGURE_BINARY" ]]; then
CONFIGURE_BASENAME=$(basename "$CONFIGURE_BINARY")
if [[ ! -x "$CONFIGURE_BASENAME" ]]; then
echo "Error: CONFIGURE binary not found or not executable at $CONFIGURE_BINARY and $CONFIGURE_BASENAME" >&2
exit 1
else
CONFIGURE_BINARY=./$CONFIGURE_BASENAME
fi
fi
CONFIGURE_BINARY="$(rlocation "_main/$(basename "%{CONFIGURE_PATH}")")"
# Run the configuration
exec "${CONFIGURE_BINARY}" "$@"

View file

@ -1,32 +1,39 @@
#!/bin/bash
set -e
EXECUTE_BINARY="%{EXECUTE_PATH}"
JQ="%{JQ_PATH}"
# TODO should this be extracted to shared init script
# Get the directory where the script is located
if [[ -z "${RUNFILES_DIR:-}" ]]; then
SCRIPT_DIR="$(readlink -f "${BASH_SOURCE[0]}")"
# Set RUNFILES_DIR relative to the script location
export RUNFILES_DIR="${SCRIPT_DIR}.runfiles"
fi
if [[ ! -x "$EXECUTE_BINARY" ]]; then
EXECUTE_BASENAME=$(basename "$EXECUTE_BINARY")
if [[ ! -x "$EXECUTE_BASENAME" ]]; then
echo "Error: Execute binary not found or not executable at $EXECUTE_BINARY and $EXECUTE_BASENAME" >&2
exit 1
else
EXECUTE_BINARY=./$EXECUTE_BASENAME
# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---
# Set up JAVA_RUNFILES if not already set
if [[ -z "${JAVA_RUNFILES:-}" ]]; then
if [[ -d "$0.runfiles" ]]; then
export JAVA_RUNFILES="$0.runfiles"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-}" ]]; then
export JAVA_RUNFILES="$(dirname "${RUNFILES_MANIFEST_FILE}")"
fi
fi
# Ensure the binaries exist and are executable
if [[ ! -x "$JQ" ]]; then
JQ_PATH="../databuild+/runtime/$(basename $JQ)"
if [[ ! -x "$JQ_PATH" ]]; then
echo "Error: JQ binary not found or not executable at $JQ or $JQ_PATH" >&2
exit 1
else
JQ=$JQ_PATH
fi
fi
EXECUTE_BINARY="$(rlocation "_main/$(basename "%{EXECUTE_PATH}")")"
JQ="$(rlocation "databuild+/runtime/$(basename "%{JQ_PATH}")")"
# First argument should be the path to a config file
CONFIG_FILE="$1"
CONFIG_FILE=${1:-}
# Create a temporary file for stdin if needed
if [[ -z "$CONFIG_FILE" ]] || [[ "$CONFIG_FILE" == "-" ]]; then

View file

@ -106,7 +106,9 @@ def _databuild_job_exec_impl(ctx):
runfiles = ctx.runfiles(
files = [jq_file, execute_file],
).merge(ctx.attr.execute.default_runfiles).merge(ctx.attr._jq.default_runfiles)
).merge(ctx.attr.execute.default_runfiles).merge(ctx.attr._jq.default_runfiles).merge(
ctx.attr._bash_runfiles.default_runfiles,
)
return [
DefaultInfo(
@ -156,6 +158,10 @@ _databuild_job_exec_rule = rule(
executable = True,
cfg = "exec",
),
"_bash_runfiles": attr.label(
default = Label("@bazel_tools//tools/bash/runfiles"),
allow_files = True,
),
},
executable = True,
)