• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16# External `common.sh`
17
18# Keep in sync with tensorflow core and configure.py.
19# TODO(b/158448780): Guard bazel version with IfChangeThenChange.
20LATEST_BAZEL_VERSION=3.1.0
21
22# Run flaky functions with retries.
23# run_with_retry cmd
24function run_with_retry {
25  eval "$1"
26  # If the command fails retry again in 60 seconds.
27  if [[ $? -ne 0 ]]; then
28    sleep 60
29    eval "$1"
30  fi
31}
32
33function die() {
34  echo "$@" 1>&2 ; exit 1;
35}
36
37# A small utility to run the command and only print logs if the command fails.
38# On success, all logs are hidden.
39function readable_run {
40  # Disable debug mode to avoid printing of variables here.
41  set +x
42  result=$("$@" 2>&1) || die "$result"
43  echo "$@"
44  echo "Command completed successfully at $(date)"
45  set -x
46}
47
48# TODO(b/158448780): Guard bazel installation with IfChangeThenChange.
49function set_bazel_outdir {
50  mkdir -p /tmpfs/bazel_output
51  export TEST_TMPDIR=/tmpfs/bazel_output
52}
53
54# Downloads bazelisk to ~/bin as `bazel`.
55function install_bazelisk {
56  date
57  case "$(uname -s)" in
58    Darwin) local name=bazelisk-darwin-amd64 ;;
59    Linux)  local name=bazelisk-linux-amd64  ;;
60    *) die "Unknown OS: $(uname -s)" ;;
61  esac
62  mkdir -p "$HOME/bin"
63  wget --no-verbose -O "$HOME/bin/bazel" \
64      "https://github.com/bazelbuild/bazelisk/releases/download/v1.3.0/$name"
65  chmod u+x "$HOME/bin/bazel"
66  if [[ ! ":$PATH:" =~ :"$HOME"/bin/?: ]]; then
67    PATH="$HOME/bin:$PATH"
68  fi
69  set_bazel_outdir
70  which bazel
71  bazel version
72  date
73}
74
75# Install the given bazel version on linux
76function update_bazel_linux {
77  if [[ -z "$1" ]]; then
78    BAZEL_VERSION=${LATEST_BAZEL_VERSION}
79  else
80    BAZEL_VERSION=$1
81  fi
82  rm -rf ~/bazel
83  mkdir ~/bazel
84
85  pushd ~/bazel
86  readable_run wget https://github.com/bazelbuild/bazel/releases/download/"${BAZEL_VERSION}"/bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh
87  chmod +x bazel-*.sh
88  ./bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh --user
89  rm bazel-"${BAZEL_VERSION}"-installer-linux-x86_64.sh
90  popd
91
92  PATH="/home/kbuilder/bin:$PATH"
93  set_bazel_outdir
94  which bazel
95  bazel version
96}
97