blob: c45b050a4cac24fdf100e149c4fd5f3193129348 [file] [log] [blame]
#!/usr/bin/env bash
# Top-level 'universal' shell library for hscloud. All sh_binary targets should
# depend on this and import it as follows:
#
# #!/usr/bin/env bash
# source tools/hscloud/lib.sh || exit 1
#
# And by specifying deps = [ "//tools/hscloud:shell" ] in sh_binary.
set -e -u -o pipefail
function hscloud::_prepare_runfiles() {
if [[ $(type -t rlocation) == function ]]; then
return
fi
# --- begin runfiles.bash initialization v2 ---
# Mostly copy-pasted from the Bazel Bash runfiles library v2.
local 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 - are you in a valid checkout and running the script via bazel?"; exit 1; }
# --- end runfiles.bash initialization v2 ---
}
# Return an absolute path to a built //tools/hscloud binary, ready to run.
#
# This will fail if we're not in a hscloud checkout.
function hscloud::tool_location() {
rloc="$(hscloud::rlocation "hscloud/tools/hscloud/hscloud_/hscloud")"
if [ -z "$rloc" ]; then
echo "Could not find location of hscloud - are you in a valid checkout and running the script via bazel?" >&2
exit 1
fi
echo "$rloc"
}
# Return an absolute path to the hscloud checkout.
function hscloud::workspace_location() {
$(hscloud::tool_location) workspace
}
# Return an absolute path to a runfile, eg. a data dependency in sh_binary.
function hscloud::rlocation() {
hscloud::_prepare_runfiles
echo "$(rlocation "$1")"
}
# Return an absolute path to a runfile, eg. a data dependency in sh_binary.
#
# This will fail if the runfile is not found.
function hscloud::must_rlocation() {
rloc="$(hscloud::rlocation $1)"
if [ -z "$rloc" ]; then
echo "Could not find runfile $1" >&2
if [ ! -z "${RUNFILES_MANIFEST_FILE:-}" ]; then
echo "Manifest file: $RUNFILES_MANIFEST_FILE" >&2
fi
exit 1
fi
echo "$rloc"
}