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 | |
Austin Schuh | 8f99c82 | 2024-05-05 22:43:40 -0700 | [diff] [blame^] | 27 | readonly VERSION="7.1.1" |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 28 | |
Austin Schuh | e3ce272 | 2019-02-17 15:09:46 -0800 | [diff] [blame] | 29 | readonly DOWNLOAD_DIR="${HOME}/.cache/bazel" |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 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 |
Austin Schuh | bf85279 | 2019-02-18 11:06:41 -0800 | [diff] [blame] | 36 | mkdir -p "${DOWNLOAD_DIR}" || true |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 37 | fi |
| 38 | if [[ ! -d "${DOWNLOAD_DIR}" ]]; then |
| 39 | echo "Failed to create ${DOWNLOAD_DIR}" >&2 |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
Austin Schuh | 137d945 | 2018-07-07 15:42:36 -0700 | [diff] [blame] | 43 | readonly INSTALLER_NAME="bazel_${VERSION}.xz" |
Maxwell Henderson | 7a93a65 | 2023-06-24 15:17:09 -0700 | [diff] [blame] | 44 | readonly DOWNLOAD_URL="https://software.frc971.org/Build-Dependencies/${INSTALLER_NAME}" |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 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. |
Philipp Schrader | e21f158 | 2018-01-18 05:08:55 +0000 | [diff] [blame] | 53 | mkdir -p "${HOME}/.cache/bazel" |
Austin Schuh | 6b046d4 | 2018-01-04 20:20:24 -0800 | [diff] [blame] | 54 | DOWNLOAD_TEMP_DIR="$(mktemp --directory --tmpdir="${HOME}/.cache/bazel")" |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 55 | TEMP_DIR="$(mktemp --directory --tmpdir="${DOWNLOAD_DIR}")" |
Austin Schuh | 6b046d4 | 2018-01-04 20:20:24 -0800 | [diff] [blame] | 56 | readonly DOWNLOAD_TEMP_DIR |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 57 | readonly TEMP_DIR |
| 58 | |
Austin Schuh | 6b046d4 | 2018-01-04 20:20:24 -0800 | [diff] [blame] | 59 | ( cd "${DOWNLOAD_TEMP_DIR}" |
| 60 | # Now, download into the ~/.cache folder |
| 61 | if [ ! -e "${HOME}/.cache/bazel/${INSTALLER_NAME}" ]; |
| 62 | then |
Brian Silverman | 3464d53 | 2019-12-18 20:15:20 -0800 | [diff] [blame] | 63 | NOISINESS=--silent |
Austin Schuh | 137d945 | 2018-07-07 15:42:36 -0700 | [diff] [blame] | 64 | if [ -t 0 ] ; then |
Brian Silverman | 3464d53 | 2019-12-18 20:15:20 -0800 | [diff] [blame] | 65 | echo on terminal |
| 66 | NOISINESS= |
Austin Schuh | 137d945 | 2018-07-07 15:42:36 -0700 | [diff] [blame] | 67 | fi |
Brian Silverman | 3464d53 | 2019-12-18 20:15:20 -0800 | [diff] [blame] | 68 | curl --output "${INSTALLER_NAME}" "${DOWNLOAD_URL}" ${NOISINESS} |
Austin Schuh | 6b046d4 | 2018-01-04 20:20:24 -0800 | [diff] [blame] | 69 | mv "${INSTALLER_NAME}" "${HOME}/.cache/bazel/${INSTALLER_NAME}" |
| 70 | fi |
| 71 | rm -rf "${DOWNLOAD_TEMP_DIR}" |
| 72 | ) |
| 73 | |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 74 | ( cd "${TEMP_DIR}" |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 75 | echo "Unpacking Bazel version ${VERSION}..." >&2 |
Austin Schuh | 137d945 | 2018-07-07 15:42:36 -0700 | [diff] [blame] | 76 | 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 Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 79 | ) |
| 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}" |
| 96 | fi |
| 97 | |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 98 | ENVIRONMENT_VARIABLES=() |
| 99 | ENVIRONMENT_VARIABLES+=(HOSTNAME="${HOSTNAME}") |
| 100 | ENVIRONMENT_VARIABLES+=(SHELL="${SHELL}") |
| 101 | ENVIRONMENT_VARIABLES+=(USER="${USER}") |
Brian Silverman | 3983eb8 | 2018-08-05 11:42:36 -0700 | [diff] [blame] | 102 | ENVIRONMENT_VARIABLES+=(PATH="/usr/bin:/bin") |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 103 | ENVIRONMENT_VARIABLES+=(HOME="${HOME}") |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 104 | ENVIRONMENT_VARIABLES+=(TERM="${TERM}") |
Brian Silverman | 3983eb8 | 2018-08-05 11:42:36 -0700 | [diff] [blame] | 105 | ENVIRONMENT_VARIABLES+=(LANG="${LANG:-C}") |
Philipp Schrader | 394e567 | 2022-02-15 22:09:42 -0800 | [diff] [blame] | 106 | ENVIRONMENT_VARIABLES+=(BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1) |
Brian | fcf3ee7 | 2017-03-05 13:30:54 -0800 | [diff] [blame] | 107 | |
Adam Snaider | 770b97b | 2023-08-04 21:07:48 -0700 | [diff] [blame] | 108 | if [[ ! -z "${CARGO_BAZEL_REPIN+x}" ]]; then |
| 109 | ENVIRONMENT_VARIABLES+=(CARGO_BAZEL_REPIN="${CARGO_BAZEL_REPIN}") |
| 110 | fi |
| 111 | |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 112 | if [[ ! -z "${DISPLAY+x}" ]]; then |
| 113 | ENVIRONMENT_VARIABLES+=(DISPLAY="${DISPLAY}") |
| 114 | fi |
| 115 | |
Austin Schuh | 47d8bef | 2020-08-26 21:54:38 -0700 | [diff] [blame] | 116 | if [[ ! -z "${SSH_AUTH_SOCK+x}" ]]; then |
| 117 | ENVIRONMENT_VARIABLES+=(SSH_AUTH_SOCK="${SSH_AUTH_SOCK}") |
| 118 | fi |
| 119 | |
Lee Mracek | f8d73cb | 2019-01-04 03:26:24 -0800 | [diff] [blame] | 120 | if [[ ! -z "${LOGNAME+x}" ]]; then |
| 121 | ENVIRONMENT_VARIABLES+=(LOGNAME="${LOGNAME}") |
| 122 | fi |
| 123 | |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 124 | if [[ -x "${VERSION_BAZEL}-real" ]]; then |
| 125 | exec -a "${VERSION_BAZEL}" env -i \ |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 126 | "${ENVIRONMENT_VARIABLES[@]}" \ |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 127 | "${VERSION_BAZEL}-real" "$@" |
| 128 | fi |
| 129 | if [[ -x "${VERSION_BAZEL}" ]]; then |
| 130 | exec env -i \ |
Austin Schuh | 77cc2c3 | 2016-12-28 22:01:38 -0800 | [diff] [blame] | 131 | "${ENVIRONMENT_VARIABLES[@]}" \ |
Brian Silverman | d1c19fb | 2016-07-07 00:10:57 -0700 | [diff] [blame] | 132 | "${VERSION_BAZEL}" "$@" |
| 133 | fi |
| 134 | |
| 135 | echo "Can't find the real bazel!" >&2 |
| 136 | exit 1 |