blob: fe2a296b7b7bc450f0b774a43b76c8de2d597543 [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 Schuh137d9452018-07-07 15:42:36 -070027readonly VERSION="201807071512+aa7e972"
28#bazel_201807071512+aa7e972.xz
Brian Silvermand1c19fb2016-07-07 00:10:57 -070029
30readonly DOWNLOAD_DIR="$(dirname "${BASH_SOURCE[0]}")/../bazel-downloads"
31# Directory to unpack bazel into. This must change whenever bazel changes.
32readonly VERSION_DIR="${DOWNLOAD_DIR}/${VERSION}-v1"
33readonly VERSION_BAZEL="${VERSION_DIR}/usr/bin/bazel"
34
35# Creating might fail if another invocation is racing us.
36if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
37 mkdir "${DOWNLOAD_DIR}" || true
38fi
39if [[ ! -d "${DOWNLOAD_DIR}" ]]; then
40 echo "Failed to create ${DOWNLOAD_DIR}" >&2
41 exit 1
42fi
43
Austin Schuh137d9452018-07-07 15:42:36 -070044readonly INSTALLER_NAME="bazel_${VERSION}.xz"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070045readonly DOWNLOAD_URL="http://frc971.org/Build-Dependencies/${INSTALLER_NAME}"
46
47if [[ ! -d "${VERSION_DIR}" ]]; then
48 echo "Downloading Bazel version ${VERSION} from ${DOWNLOAD_URL}..." >&2
49
50 # A temporary directory which is definitely on the same filesystem as our final
51 # destination, which is important so we can atomically move it.
52 # If this move is non-atomic, then a concurrent Bazel command (like the verifier
53 # uses several of) could use a half-copied Bazel installation.
Philipp Schradere21f1582018-01-18 05:08:55 +000054 mkdir -p "${HOME}/.cache/bazel"
Austin Schuh6b046d42018-01-04 20:20:24 -080055 DOWNLOAD_TEMP_DIR="$(mktemp --directory --tmpdir="${HOME}/.cache/bazel")"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070056 TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")"
Austin Schuh6b046d42018-01-04 20:20:24 -080057 readonly DOWNLOAD_TEMP_DIR
Brian Silvermand1c19fb2016-07-07 00:10:57 -070058 readonly TEMP_DIR
59
Austin Schuh6b046d42018-01-04 20:20:24 -080060 ( cd "${DOWNLOAD_TEMP_DIR}"
61 # Now, download into the ~/.cache folder
62 if [ ! -e "${HOME}/.cache/bazel/${INSTALLER_NAME}" ];
63 then
Austin Schuh137d9452018-07-07 15:42:36 -070064 if [ -t 0 ] ; then
65 wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --show-progress
66 else
67 wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --quiet
68 fi
Austin Schuh6b046d42018-01-04 20:20:24 -080069 mv "${INSTALLER_NAME}" "${HOME}/.cache/bazel/${INSTALLER_NAME}"
70 fi
71 rm -rf "${DOWNLOAD_TEMP_DIR}"
72 )
73
Brian Silvermand1c19fb2016-07-07 00:10:57 -070074 ( cd "${TEMP_DIR}"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070075 echo "Unpacking Bazel version ${VERSION}..." >&2
Austin Schuh137d9452018-07-07 15:42:36 -070076 mkdir -p extracted/usr/bin
77 xz -d ${HOME}/.cache/bazel/${INSTALLER_NAME} -c > extracted/usr/bin/bazel-real
78 chmod a+x extracted/usr/bin/bazel-real
Brian Silvermand1c19fb2016-07-07 00:10:57 -070079 )
80
81 touch "${TEMP_DIR}/extracted/usr/bin/bazel.bazelrc"
82
83 # Careful: somebody else might have already done it. If they manage to make
84 # the move between our check and our move, then we'll end up with a random
85 # extracted directory which won't do anybody any harm. If somebody else does
86 # that first, then our move will fail.
87 if [[ ! -d "${VERSION_DIR}" ]]; then
88 mv "${TEMP_DIR}/extracted" "${VERSION_DIR}" || true
89 fi
90 if [[ ! -d "${VERSION_DIR}" ]]; then
91 echo "Failed to create ${VERSION_DIR}" >&2
92 exit 1
93 fi
94 rm -rf "${TEMP_DIR}"
95 echo "Done downloading Bazel version ${VERSION}"
96fi
97
Austin Schuh77cc2c32016-12-28 22:01:38 -080098ENVIRONMENT_VARIABLES=()
99ENVIRONMENT_VARIABLES+=(HOSTNAME="${HOSTNAME}")
100ENVIRONMENT_VARIABLES+=(SHELL="${SHELL}")
101ENVIRONMENT_VARIABLES+=(USER="${USER}")
Brian Silverman3983eb82018-08-05 11:42:36 -0700102ENVIRONMENT_VARIABLES+=(PATH="/usr/bin:/bin")
Austin Schuh77cc2c32016-12-28 22:01:38 -0800103ENVIRONMENT_VARIABLES+=(HOME="${HOME}")
104ENVIRONMENT_VARIABLES+=(LOGNAME="${LOGNAME}")
105ENVIRONMENT_VARIABLES+=(TERM="${TERM}")
Brian Silverman3983eb82018-08-05 11:42:36 -0700106ENVIRONMENT_VARIABLES+=(LANG="${LANG:-C}")
Brianfcf3ee72017-03-05 13:30:54 -0800107
Austin Schuh77cc2c32016-12-28 22:01:38 -0800108if [[ ! -z "${DISPLAY+x}" ]]; then
109 ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}")
110fi
111
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700112if [[ -x "${VERSION_BAZEL}-real" ]]; then
113 exec -a "${VERSION_BAZEL}" env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800114 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700115 "${VERSION_BAZEL}-real" "$@"
116fi
117if [[ -x "${VERSION_BAZEL}" ]]; then
118 exec env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800119 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700120 "${VERSION_BAZEL}" "$@"
121fi
122
123echo "Can't find the real bazel!" >&2
124exit 1