blob: 167518839ddad663c762acbac3a95e5ea3727623 [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 Schuhe18baff2018-10-20 17:40:42 -070027readonly VERSION="0.19.0rc4-201810201638+ac88041"
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 Schuh137d9452018-07-07 15:42:36 -070043readonly INSTALLER_NAME="bazel_${VERSION}.xz"
johnp8a384f92019-01-03 13:10:00 -080044readonly DOWNLOAD_URL="http://www.frc971.org/Build-Dependencies/${INSTALLER_NAME}"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070045
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.
Philipp Schradere21f1582018-01-18 05:08:55 +000053 mkdir -p "${HOME}/.cache/bazel"
Austin Schuh6b046d42018-01-04 20:20:24 -080054 DOWNLOAD_TEMP_DIR="$(mktemp --directory --tmpdir="${HOME}/.cache/bazel")"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070055 TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")"
Austin Schuh6b046d42018-01-04 20:20:24 -080056 readonly DOWNLOAD_TEMP_DIR
Brian Silvermand1c19fb2016-07-07 00:10:57 -070057 readonly TEMP_DIR
58
Austin Schuh6b046d42018-01-04 20:20:24 -080059 ( cd "${DOWNLOAD_TEMP_DIR}"
60 # Now, download into the ~/.cache folder
61 if [ ! -e "${HOME}/.cache/bazel/${INSTALLER_NAME}" ];
62 then
Austin Schuh137d9452018-07-07 15:42:36 -070063 if [ -t 0 ] ; then
64 wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --show-progress
65 else
66 wget "${DOWNLOAD_URL}" -O "${INSTALLER_NAME}" --no-verbose --quiet
67 fi
Austin Schuh6b046d42018-01-04 20:20:24 -080068 mv "${INSTALLER_NAME}" "${HOME}/.cache/bazel/${INSTALLER_NAME}"
69 fi
70 rm -rf "${DOWNLOAD_TEMP_DIR}"
71 )
72
Brian Silvermand1c19fb2016-07-07 00:10:57 -070073 ( cd "${TEMP_DIR}"
Brian Silvermand1c19fb2016-07-07 00:10:57 -070074 echo "Unpacking Bazel version ${VERSION}..." >&2
Austin Schuh137d9452018-07-07 15:42:36 -070075 mkdir -p extracted/usr/bin
76 xz -d ${HOME}/.cache/bazel/${INSTALLER_NAME} -c > extracted/usr/bin/bazel-real
77 chmod a+x extracted/usr/bin/bazel-real
Brian Silvermand1c19fb2016-07-07 00:10:57 -070078 )
79
80 touch "${TEMP_DIR}/extracted/usr/bin/bazel.bazelrc"
81
82 # Careful: somebody else might have already done it. If they manage to make
83 # the move between our check and our move, then we'll end up with a random
84 # extracted directory which won't do anybody any harm. If somebody else does
85 # that first, then our move will fail.
86 if [[ ! -d "${VERSION_DIR}" ]]; then
87 mv "${TEMP_DIR}/extracted" "${VERSION_DIR}" || true
88 fi
89 if [[ ! -d "${VERSION_DIR}" ]]; then
90 echo "Failed to create ${VERSION_DIR}" >&2
91 exit 1
92 fi
93 rm -rf "${TEMP_DIR}"
94 echo "Done downloading Bazel version ${VERSION}"
95fi
96
Austin Schuh77cc2c32016-12-28 22:01:38 -080097ENVIRONMENT_VARIABLES=()
98ENVIRONMENT_VARIABLES+=(HOSTNAME="${HOSTNAME}")
99ENVIRONMENT_VARIABLES+=(SHELL="${SHELL}")
100ENVIRONMENT_VARIABLES+=(USER="${USER}")
Brian Silverman3983eb82018-08-05 11:42:36 -0700101ENVIRONMENT_VARIABLES+=(PATH="/usr/bin:/bin")
Austin Schuh77cc2c32016-12-28 22:01:38 -0800102ENVIRONMENT_VARIABLES+=(HOME="${HOME}")
103ENVIRONMENT_VARIABLES+=(LOGNAME="${LOGNAME}")
104ENVIRONMENT_VARIABLES+=(TERM="${TERM}")
Brian Silverman3983eb82018-08-05 11:42:36 -0700105ENVIRONMENT_VARIABLES+=(LANG="${LANG:-C}")
Brianfcf3ee72017-03-05 13:30:54 -0800106
Austin Schuh77cc2c32016-12-28 22:01:38 -0800107if [[ ! -z "${DISPLAY+x}" ]]; then
108 ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}")
109fi
110
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700111if [[ -x "${VERSION_BAZEL}-real" ]]; then
112 exec -a "${VERSION_BAZEL}" env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800113 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700114 "${VERSION_BAZEL}-real" "$@"
115fi
116if [[ -x "${VERSION_BAZEL}" ]]; then
117 exec env -i \
Austin Schuh77cc2c32016-12-28 22:01:38 -0800118 "${ENVIRONMENT_VARIABLES[@]}" \
Brian Silvermand1c19fb2016-07-07 00:10:57 -0700119 "${VERSION_BAZEL}" "$@"
120fi
121
122echo "Can't find the real bazel!" >&2
123exit 1