Added Posix Shell implementation of Runfiles. (#49)
This change adds `runfiles.sh`, the runfiles library in POSIX shell, and
makes `runfiles.bash` a thin wrapper that sources it. One implementation
serves both the existing `bazel_tools/tools/bash/runfiles/runfiles.bash`
snippets and scripts that run under dash, ash or busybox sh. The public
API is unchanged: `rlocation`, `runfiles_export_envvars`,
`runfiles_current_repository` and `runfiles_rlocation_checked`.
## Under bash
Nothing changes for callers. The caller's repository comes from
`BASH_SOURCE`, the functions are exported with `export -f`, and the
`use_bash_launcher` launcher keeps working.
## Under any other shell
Every script sources the library itself, since functions cannot be
exported across `exec`, and `runfiles_current_repository "$0"` takes the
script's path, since there is no `BASH_SOURCE`. A launcher that runs
under a non-bash `sh_toolchain` follows in #78.
## Initialization snippet v4
One snippet body serves both entry points: it resolves the library's
location before sourcing it instead of sourcing each candidate in turn,
and aborts the script if the library fails to load rather than leaving
it without `rlocation`. The bash and POSIX versions differ only in the
path they look for and in `pipefail`, which not every shell has. The
launcher uses the bash one; v3 keeps working.
## Performance
Lookups against a runfiles directory are one `stat`. Lookups against a
manifest use one `awk` process per lookup when awk is available (`AWK`
names the program), so the cost does not grow with the manifest; without
awk, or with `RULES_SHELL_RUNFILES_USE_AWK=0`, a shell loop searches
instead and nothing is forked. The snippets find the library the same
way. `shell/runfiles/README.md` has the details and a table produced by
`bazel run //tests/runfiles:benchmark`.
## Compatibility
The library needs Bazel 7.4 for its root symlinks; on Bazel 7 the
`//shell/runfiles` alias still resolves to Bazel's bundled library.
---
# Usage
## 1. Depend on the runfiles library from your build rule
```python
sh_binary(
name = "my_binary",
# ...
deps = ["@rules_shell//shell/runfiles"],
)
```
## 2. Source the runfiles library
The library defines `rlocation`, which you would need to look up its own
location, so insert a snippet at the top of your main script. Bash
scripts:
```shell
# --- begin runfiles.bash initialization v4 ---
# Copy-pasted from the Bazel Bash runfiles library v4.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash; _rf_p=; _rf_a=
if [ -n "${AWK:-}" ] || [ "${RULES_SHELL_RUNFILES_USE_AWK:-}" != 0 ]; then _rf_a=1; fi
_rf_d() { [ -f "$1/$f" ] && _rf_p="$1/$f"; }
_rf_m() { [ -f "$1" ] || return 1; if [ -n "$_rf_a" ]; then \
_rf_p=$(${AWK:-awk} -v k="$f " 'index($0,k)==1{print substr($0,length(k)+1);m=1;exit}END{exit !m}' "$1" 2>/dev/null) && return; fi; \
while IFS= read -r _rf_l || [ -n "$_rf_l" ]; do \
case "$_rf_l" in "$f "*) _rf_p="${_rf_l#"$f "}"; return;; esac; done < "$1"; return 1; }
_rf_d "${RUNFILES_DIR:-/dev/null}" || _rf_m "${RUNFILES_MANIFEST_FILE:-/dev/null}" || \
_rf_d "$0.runfiles" || _rf_m "$0.runfiles_manifest" || _rf_m "$0.exe.runfiles_manifest" || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }
# shellcheck disable=SC1090
. "$_rf_p" || { echo>&2 "ERROR: cannot source $_rf_p"; exit 1; }; f=; unset -f _rf_d _rf_m; unset _rf_l _rf_p _rf_a; set -e
# --- end runfiles.bash initialization v4 ---
```
POSIX shell scripts:
```shell
# --- begin runfiles.sh initialization v4 ---
# Copy-pasted from the Bazel POSIX shell runfiles library v4.
set -u; set +e; f=rules_shell/shell/runfiles/runfiles.sh; _rf_p=; _rf_a=
if [ -n "${AWK:-}" ] || [ "${RULES_SHELL_RUNFILES_USE_AWK:-}" != 0 ]; then _rf_a=1; fi
_rf_d() { [ -f "$1/$f" ] && _rf_p="$1/$f"; }
_rf_m() { [ -f "$1" ] || return 1; if [ -n "$_rf_a" ]; then \
_rf_p=$(${AWK:-awk} -v k="$f " 'index($0,k)==1{print substr($0,length(k)+1);m=1;exit}END{exit !m}' "$1" 2>/dev/null) && return; fi; \
while IFS= read -r _rf_l || [ -n "$_rf_l" ]; do \
case "$_rf_l" in "$f "*) _rf_p="${_rf_l#"$f "}"; return;; esac; done < "$1"; return 1; }
_rf_d "${RUNFILES_DIR:-/dev/null}" || _rf_m "${RUNFILES_MANIFEST_FILE:-/dev/null}" || \
_rf_d "$0.runfiles" || _rf_m "$0.runfiles_manifest" || _rf_m "$0.exe.runfiles_manifest" || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }
# shellcheck disable=SC1090
. "$_rf_p" || { echo>&2 "ERROR: cannot source $_rf_p"; exit 1; }; f=; unset -f _rf_d _rf_m; unset _rf_l _rf_p _rf_a; set -e
# --- end runfiles.sh initialization v4 ---
```
## 3. Use `rlocation` to look up runfile paths
```shell
cat "$(rlocation my_workspace/path/to/my/data.txt)"
```
---------
Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
14 files changed