aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/bash
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-04-23 07:56:37 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-23 07:58:18 -0700
commit87621656da2377f5b59b0ee84ecaf54ea77b7bd8 (patch)
tree849d6fe1cd76b45162d96cab68444902b240a942 /tools/bash
parent27b0bcfda2ef8496fd83733d7a7392a7e04218fa (diff)
Bash,runfiles: add tests
The runfiles library can now discover where the runfiles are. Also remove the shebang line from the runfiles library. This script should never by executed on its own but be sourced by other Bash scripts. See https://github.com/bazelbuild/bazel/issues/4460 RELNOTES[NEW]: Bash,runfiles: use the new platform-independent library in `@bazel_tools//tools/bash/runfiles` to access runfiles (data-dependencies). See https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash for usage information. Closes #5071. Change-Id: I94557133e76ecc927646d109e2611d711c363cdc PiperOrigin-RevId: 193923870
Diffstat (limited to 'tools/bash')
-rw-r--r--tools/bash/runfiles/BUILD.tools7
-rw-r--r--tools/bash/runfiles/runfiles.bash40
-rwxr-xr-x[-rw-r--r--]tools/bash/runfiles/runfiles_test.bash0
3 files changed, 43 insertions, 4 deletions
diff --git a/tools/bash/runfiles/BUILD.tools b/tools/bash/runfiles/BUILD.tools
index 9fcf6ea6ae..3b43a75f9f 100644
--- a/tools/bash/runfiles/BUILD.tools
+++ b/tools/bash/runfiles/BUILD.tools
@@ -1,2 +1,5 @@
-# This package will host the Bash runfiles library.
-# See https://github.com/bazelbuild/bazel/issues/4460
+sh_library(
+ name = "runfiles",
+ srcs = ["runfiles.bash"],
+ visibility = ["//visibility:public"],
+)
diff --git a/tools/bash/runfiles/runfiles.bash b/tools/bash/runfiles/runfiles.bash
index d56a835bfa..6894a8bc15 100644
--- a/tools/bash/runfiles/runfiles.bash
+++ b/tools/bash/runfiles/runfiles.bash
@@ -1,5 +1,3 @@
-#!/bin/bash
-#
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,6 +22,44 @@
# to the absolute path of the runfiles manifest. RUNFILES_DIR may be unset in
# this case.
# - If RUNFILES_LIB_DEBUG=1 is set, the script will print errors to stderr.
+#
+# USAGE:
+# 1. Depend on this runfiles library from your build rule:
+#
+# sh_binary(
+# name = "my_binary",
+# ...
+# deps = ["@bazel_tools//tools/bash/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:
+#
+# set -euo pipefail
+# # --- begin runfiles.bash initialization ---
+# if [[ "${RUNFILES_MANIFEST_ONLY:-}" != 1 && -z "${RUNFILES_DIR:-}" ]]; then
+# if [[ -f "$0.runfiles_manifest" ]]; then
+# export RUNFILES_MANIFEST_ONLY=1
+# export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+# elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+# export RUNFILES_MANIFEST_ONLY=1
+# export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+# elif [[ -d "$0.runfiles" ]]; then
+# export RUNFILES_DIR="$0.runfiles"
+# fi
+# fi
+# if [[ "${RUNFILES_MANIFEST_ONLY:-}" == 1 && -f "${RUNFILES_MANIFEST_FILE:-}" ]]; then
+# source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+# "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+# elif [[ -n "${RUNFILES_DIR:-}" && -d "${RUNFILES_DIR}" ]]; then
+# source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+# else
+# echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+# exit 1
+# fi
+# # --- end runfiles.bash initialization ---
case "$(uname -s | tr [:upper:] [:lower:])" in
msys*|mingw*|cygwin*)
diff --git a/tools/bash/runfiles/runfiles_test.bash b/tools/bash/runfiles/runfiles_test.bash
index 3d6dd8885c..3d6dd8885c 100644..100755
--- a/tools/bash/runfiles/runfiles_test.bash
+++ b/tools/bash/runfiles/runfiles_test.bash