Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # The bazel script calls this instead of the bazel-real binary which is |
| 4 | # installed next to it. This script downloads a specific version of Bazel and |
| 5 | # then calls that. |
| 6 | |
| 7 | # Alternatively, if the environment variable BAZEL_OVERRIDE is set, that will be |
| 8 | # run directly (after printing a message). That is intended for testing only. |
| 9 | |
| 10 | # This script operates based on the assumption that any directory of the correct |
| 11 | # name is a fully extracted, valid Bazel installation. It is careful to avoid |
| 12 | # putting an invalid directory at that name at any point. |
| 13 | |
| 14 | set -e |
| 15 | set -u |
| 16 | set -o pipefail |
| 17 | |
| 18 | if [[ -n "${BAZEL_OVERRIDE+x}" ]]; then |
| 19 | tput setaf 1 >&2 |
| 20 | echo -n "Actually calling " >&2 |
| 21 | tput setaf 3 >&2 |
| 22 | echo "${BAZEL_OVERRIDE}" >&2 |
| 23 | tput sgr0 >&2 |
| 24 | exec "${BAZEL_OVERRIDE}" "$@" |
| 25 | fi |
| 26 | |
| 27 | readonly VERSION="201607070016+7a0d360" |
| 28 | |
| 29 | readonly DOWNLOAD_DIR="$(dirname "${BASH_SOURCE[0]}")/../bazel-downloads" |
| 30 | # Directory to unpack bazel into. This must change whenever bazel changes. |
| 31 | readonly VERSION_DIR="${DOWNLOAD_DIR}/${VERSION}-v1" |
| 32 | readonly VERSION_BAZEL="${VERSION_DIR}/usr/bin/bazel" |
| 33 | |
| 34 | # Creating might fail if another invocation is racing us. |
| 35 | if [[ ! -d "${DOWNLOAD_DIR}" ]]; then |
| 36 | mkdir "${DOWNLOAD_DIR}" || true |
| 37 | fi |
| 38 | if [[ ! -d "${DOWNLOAD_DIR}" ]]; then |
| 39 | echo "Failed to create ${DOWNLOAD_DIR}" >&2 |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | readonly INSTALLER_NAME="bazel_${VERSION}_amd64.deb" |
| 44 | readonly DOWNLOAD_URL="http://frc971.org/Build-Dependencies/${INSTALLER_NAME}" |
| 45 | |
| 46 | if [[ ! -d "${VERSION_DIR}" ]]; then |
| 47 | echo "Downloading Bazel version ${VERSION} from ${DOWNLOAD_URL}..." >&2 |
| 48 | |
| 49 | # A temporary directory which is definitely on the same filesystem as our final |
| 50 | # destination, which is important so we can atomically move it. |
| 51 | # If this move is non-atomic, then a concurrent Bazel command (like the verifier |
| 52 | # uses several of) could use a half-copied Bazel installation. |
| 53 | TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")" |
| 54 | readonly TEMP_DIR |
| 55 | |
| 56 | ( cd "${TEMP_DIR}" |
| 57 | wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --show-progress |
| 58 | echo "Unpacking Bazel version ${VERSION}..." >&2 |
| 59 | dpkg-deb -x "${INSTALLER_NAME}" extracted |
| 60 | ) |
| 61 | |
| 62 | touch "${TEMP_DIR}/extracted/usr/bin/bazel.bazelrc" |
| 63 | |
| 64 | # Careful: somebody else might have already done it. If they manage to make |
| 65 | # the move between our check and our move, then we'll end up with a random |
| 66 | # extracted directory which won't do anybody any harm. If somebody else does |
| 67 | # that first, then our move will fail. |
| 68 | if [[ ! -d "${VERSION_DIR}" ]]; then |
| 69 | mv "${TEMP_DIR}/extracted" "${VERSION_DIR}" || true |
| 70 | fi |
| 71 | if [[ ! -d "${VERSION_DIR}" ]]; then |
| 72 | echo "Failed to create ${VERSION_DIR}" >&2 |
| 73 | exit 1 |
| 74 | fi |
| 75 | rm -rf "${TEMP_DIR}" |
| 76 | echo "Done downloading Bazel version ${VERSION}" |
| 77 | fi |
| 78 | |
| 79 | if [[ -x "${VERSION_BAZEL}-real" ]]; then |
| 80 | exec -a "${VERSION_BAZEL}" env -i \ |
| 81 | HOSTNAME="${HOSTNAME}" \ |
| 82 | SHELL="${SHELL}" \ |
| 83 | USER="${USER}" \ |
| 84 | PATH="${PATH}" \ |
| 85 | LANG="${LANG}" \ |
| 86 | HOME="${HOME}" \ |
| 87 | LOGNAME="${LOGNAME}" \ |
| 88 | TERM="${TERM}" \ |
Austin Schuh | 3f3deb7 | 2016-07-09 13:02:45 -0700 | [diff] [blame] | 89 | DISPLAY="${DISPLAY}" \ |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 90 | "${VERSION_BAZEL}-real" "$@" |
| 91 | fi |
| 92 | if [[ -x "${VERSION_BAZEL}" ]]; then |
| 93 | exec env -i \ |
| 94 | HOSTNAME="${HOSTNAME}" \ |
| 95 | SHELL="${SHELL}" \ |
| 96 | USER="${USER}" \ |
| 97 | PATH="${PATH}" \ |
| 98 | LANG="${LANG}" \ |
| 99 | HOME="${HOME}" \ |
| 100 | LOGNAME="${LOGNAME}" \ |
| 101 | TERM="${TERM}" \ |
Austin Schuh | 3f3deb7 | 2016-07-09 13:02:45 -0700 | [diff] [blame] | 102 | DISPLAY="${DISPLAY}" \ |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 103 | "${VERSION_BAZEL}" "$@" |
| 104 | fi |
| 105 | |
| 106 | echo "Can't find the real bazel!" >&2 |
| 107 | exit 1 |