blob: 75faa0fb27af2d37d4d3f47f1c97959e8403ecce [file] [log] [blame]
Brian Silvermand1c19fb2016-07-07 00:10:57 -07001#!/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
14set -e
15set -u
16set -o pipefail
17
18if [[ -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}" "$@"
25fi
26
Austin Schuhbe8c9b12017-11-25 15:53:12 -080027readonly VERSION="201711232100+e0fe5d9"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070028
29readonly DOWNLOAD_DIR="$(dirname "${BASH_SOURCE[0]}")/../bazel-downloads"
30# Directory to unpack bazel into. This must change whenever bazel changes.
31readonly VERSION_DIR="${DOWNLOAD_DIR}/${VERSION}-v1"
32readonly VERSION_BAZEL="${VERSION_DIR}/usr/bin/bazel"
33
34# Creating might fail if another invocation is racing us.
35if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
36 mkdir "${DOWNLOAD_DIR}" || true
37fi
38if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
39 echo "Failed to create ${DOWNLOAD_DIR}" >&2
40 exit 1
41fi
42
Austin Schuh9d92e6b2017-10-17 01:19:38 -070043readonly INSTALLER_NAME="bazel_${VERSION}-linux-x86_64.deb"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070044readonly DOWNLOAD_URL="http://frc971.org/Build-Dependencies/${INSTALLER_NAME}"
45
46if [[ ! -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.
Austin Schuh6b046d42018-01-04 20:20:24 -080053 DOWNLOAD_TEMP_DIR="$(mktemp --directory --tmpdir="${HOME}/.cache/bazel")"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070054 TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")"
Austin Schuh6b046d42018-01-04 20:20:24 -080055 readonly DOWNLOAD_TEMP_DIR
Brian Silvermand1c19fb2016-07-07 00:10:57 -070056 readonly TEMP_DIR
57
Austin Schuh6b046d42018-01-04 20:20:24 -080058 ( cd "${DOWNLOAD_TEMP_DIR}"
59 # Now, download into the ~/.cache folder
60 if [ ! -e "${HOME}/.cache/bazel/${INSTALLER_NAME}" ];
61 then
62 wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --show-progress
63 mv "${INSTALLER_NAME}" "${HOME}/.cache/bazel/${INSTALLER_NAME}"
64 fi
65 rm -rf "${DOWNLOAD_TEMP_DIR}"
66 )
67
Brian Silvermand1c19fb2016-07-07 00:10:57 -070068 ( cd "${TEMP_DIR}"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070069 echo "Unpacking Bazel version ${VERSION}..." >&2
Austin Schuh6b046d42018-01-04 20:20:24 -080070 dpkg-deb -x "${HOME}/.cache/bazel/${INSTALLER_NAME}" extracted
Brian Silvermand1c19fb2016-07-07 00:10:57 -070071 )
72
73 touch "${TEMP_DIR}/extracted/usr/bin/bazel.bazelrc"
74
75 # Careful: somebody else might have already done it. If they manage to make
76 # the move between our check and our move, then we'll end up with a random
77 # extracted directory which won't do anybody any harm. If somebody else does
78 # that first, then our move will fail.
79 if [[ ! -d "${VERSION_DIR}" ]]; then
80 mv "${TEMP_DIR}/extracted" "${VERSION_DIR}" || true
81 fi
82 if [[ ! -d "${VERSION_DIR}" ]]; then
83 echo "Failed to create ${VERSION_DIR}" >&2
84 exit 1
85 fi
86 rm -rf "${TEMP_DIR}"
87 echo "Done downloading Bazel version ${VERSION}"
88fi
89
Austin Schuh77cc2c32016-12-28 22:01:38 -080090ENVIRONMENT_VARIABLES=()
91ENVIRONMENT_VARIABLES+=(HOSTNAME="${HOSTNAME}")
92ENVIRONMENT_VARIABLES+=(SHELL="${SHELL}")
93ENVIRONMENT_VARIABLES+=(USER="${USER}")
94ENVIRONMENT_VARIABLES+=(PATH="${PATH}")
Austin Schuh77cc2c32016-12-28 22:01:38 -080095ENVIRONMENT_VARIABLES+=(HOME="${HOME}")
96ENVIRONMENT_VARIABLES+=(LOGNAME="${LOGNAME}")
97ENVIRONMENT_VARIABLES+=(TERM="${TERM}")
98
Brianfcf3ee72017-03-05 13:30:54 -080099if [[ ! -z "${LANG+x}" ]]; then
100 ENVIRONMENT_VARIABLES+=(LANG="${LANG}")
101fi
102
Austin Schuh77cc2c32016-12-28 22:01:38 -0800103if [[ ! -z "${DISPLAY+x}" ]]; then
104 ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}")
105fi
106
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700107if [[ -x "${VERSION_BAZEL}-real" ]]; then
108 exec -a "${VERSION_BAZEL}" env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800109 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700110 "${VERSION_BAZEL}-real" "$@"
111fi
112if [[ -x "${VERSION_BAZEL}" ]]; then
113 exec env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800114 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700115 "${VERSION_BAZEL}" "$@"
116fi
117
118echo "Can't find the real bazel!" >&2
119exit 1