Skip to content

Added Posix Shell implementation of Runfiles.#49

Open
UebelAndre wants to merge 4 commits intobazel-contrib:mainfrom
UebelAndre:shell
Open

Added Posix Shell implementation of Runfiles.#49
UebelAndre wants to merge 4 commits intobazel-contrib:mainfrom
UebelAndre:shell

Conversation

@UebelAndre
Copy link
Copy Markdown
Contributor

This change implements the runfiles library in pure Posix Shell to improve compatibility on systems that don't have bash.

Users who want to try this implementation can either explicitly use the @rules_shell//shell/runfiles:runfiles_sh target or set --@rules_shell//shell/settings:experimental_use_shell_runfiles in a project's .bazelrc file when consuming @rules_shell//shell/runfiles


USAGE:

1. Depend on this runfiles library from your build rule:

sh_binary(
    name = "my_binary",
    #...
    deps = ["@rules_shell//shell/runfiles"],
)

2. Source the runfiles library.

The runfiles library itself defines rlocation which you would need to
look up the library's runtime location, thus we have a chicken-and-egg
problem. Insert the following code snippet to the top of your main
script:

# --- begin runfiles.sh initialization v1 ---
# Copy-pasted from the Bazel POSIX shell runfiles library v1.
set +e; f=shell/runfiles/runfiles.sh
_rf_s() { [ -f "$1" ] || return 1; while IFS= read -r _rf_l; do \
  case "$_rf_l" in "$f "*) . "${_rf_l#"$f "}"; return $?;; esac; \
  done < "$1"; return 1; }
# shellcheck disable=SC1090
. "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
  _rf_s "${RUNFILES_MANIFEST_FILE:-/dev/null}" 2>/dev/null || \
  . "$0.runfiles/$f" 2>/dev/null || \
  _rf_s "$0.runfiles_manifest" 2>/dev/null || \
  _rf_s "$0.exe.runfiles_manifest" 2>/dev/null || \
  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
unset -f _rf_s 2>/dev/null; unset _rf_l 2>/dev/null
# --- end runfiles.sh initialization v1 ---

3. Use rlocation to look up runfile paths.

cat "$(rlocation my_workspace/path/to/my/data.txt)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant