blob: ca97e72d3ec52f8a19e61ed9c51dc748cfdf2cb4 [file] [log] [blame]
Philipp Wollermanna5afe952016-06-21 14:58:09 +00001#!/bin/bash
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +00002
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2015 The Bazel Authors. All rights reserved.
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +00004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Philipp Wollermanna5afe952016-06-21 14:58:09 +000017set -e
18
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000019# Bazel self-extractable installer
20
21# Installation and etc prefix can be overriden from command line
22install_prefix=${1:-"/usr/local"}
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000023
24progname="$0"
25
26echo "Bazel installer"
27echo "---------------"
28echo
Damien Martin-Guillerez3dff9a32016-07-04 13:38:20 +000029echo "Bazel is bundled with software licensed under the GPLv2 with Classpath exception."
30echo "You can find the sources next to the installer on our release page:"
31echo " https://github.com/bazelbuild/bazel/releases"
32echo
33
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000034cat <<'EOF'
35%release_info%
36EOF
37
Damien Martin-Guillerez3dff9a32016-07-04 13:38:20 +000038usage() {
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000039 echo "Usage: $progname [options]" >&2
40 echo "Options are:" >&2
41 echo " --prefix=/some/path set the prefix path (default=/usr/local)." >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000042 echo " --bin= set the binary folder path (default=%prefix%/bin)." >&2
43 echo " --base= set the base install path (default=%prefix%/lib/bazel)." >&2
Damien Martin-Guillerezdef8ce32016-02-15 12:09:09 +000044 echo " --user configure for user install, expands to:" >&2
Philipp Wollermann72056112017-05-08 05:17:06 -040045 echo ' --bin=$HOME/bin --base=$HOME/.bazel' >&2
andy g scott ?4dd6dac2018-06-18 09:05:31 -070046 echo " --skip-uncompress skip uncompressing the base image until the" >&2
47 echo " first bazel invocation" >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000048 exit 1
49}
50
51prefix="/usr/local"
52bin="%prefix%/bin"
53base="%prefix%/lib/bazel"
andy g scott ?4dd6dac2018-06-18 09:05:31 -070054should_uncompress=true
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000055
56for opt in "${@}"; do
57 case $opt in
58 --prefix=*)
59 prefix="$(echo "$opt" | cut -d '=' -f 2-)"
60 ;;
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000061 --bin=*)
62 bin="$(echo "$opt" | cut -d '=' -f 2-)"
63 ;;
64 --base=*)
65 base="$(echo "$opt" | cut -d '=' -f 2-)"
66 ;;
67 --user)
68 bin="$HOME/bin"
69 base="$HOME/.bazel"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000070 ;;
andy g scott ?4dd6dac2018-06-18 09:05:31 -070071 --skip-uncompress)
72 should_uncompress=false
73 ;;
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000074 *)
75 usage
76 ;;
77 esac
78done
79
80bin="${bin//%prefix%/${prefix}}"
81base="${base//%prefix%/${prefix}}"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000082
Damien Martin-Guillerez3dff9a32016-07-04 13:38:20 +000083test_write() {
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000084 local file="$1"
85 while [ "$file" != "/" ] && [ -n "${file}" ] && [ ! -e "$file" ]; do
86 file="$(dirname "${file}")"
87 done
88 [ -w "${file}" ] || {
89 echo >&2
90 echo "The Bazel installer must have write access to $1!" >&2
John Cater1c4ae472016-07-20 17:31:00 +000091 echo "Consider using the --user flag to install Bazel under $HOME/bin instead." >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000092 echo >&2
93 usage
94 }
95}
96
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +000097# Test for dependencies
98# unzip
99if ! which unzip >/dev/null; then
100 echo >&2
101 echo "unzip not found, please install the corresponding package." >&2
Damien Martin-Guillerez4885eef2016-10-28 12:02:50 +0000102 echo "See http://bazel.build/docs/install.html for more information on" >&2
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000103 echo "dependencies of Bazel." >&2
104 exit 1
105fi
106
107# java
108if [ -z "${JAVA_HOME-}" ]; then
109 case "$(uname -s | tr 'A-Z' 'a-z')" in
110 linux)
111 JAVA_HOME="$(readlink -f $(which javac) 2>/dev/null | sed 's_/bin/javac__')" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000112 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000113 ;;
114 freebsd)
115 JAVA_HOME="/usr/local/openjdk8"
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000116 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000117 ;;
118 darwin)
119 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000120 BASHRC="~/.bash_profile"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000121 ;;
122 esac
123fi
philwobaca6e42017-05-15 22:00:47 +0200124
125# Only check for an installed JDK if this version of Bazel does not contain a
126# bundled JDK.
127case "$0" in
128 *without-jdk*)
129 if [ ! -x "${JAVA_HOME}/bin/javac" ]; then
130 echo >&2
131 echo "Java not found, please install the corresponding package." >&2
132 echo "See http://bazel.build/docs/install.html for more information on" >&2
133 echo "dependencies of Bazel." >&2
134 exit 1
135 fi
136 ;;
137esac
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000138
139# Test for write access
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000140test_write "${bin}"
141test_write "${base}"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000142
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000143# Do the actual installation
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000144echo -n "Uncompressing."
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000145
146# Cleaning-up, with some guards.
Damien Martin-Guillerezcab6e872016-05-02 11:39:17 +0000147rm -f "${bin}/bazel"
Damien Martin-Guillerez2c2e70d2015-09-08 17:23:02 +0000148if [ -d "${base}" -a -x "${base}/bin/bazel" ]; then
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000149 rm -fr "${base}"
150fi
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000151
Kristina Chodorow84450b82016-01-28 15:16:02 +0000152mkdir -p ${bin} ${base} ${base}/bin ${base}/etc
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000153echo -n .
154
Keith Smiley74c274a2019-01-11 09:04:36 -0800155unzip -q "${BASH_SOURCE[0]}" bazel bazel-real bazel-complete.bash _bazel -d "${base}/bin"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000156echo -n .
Brian Silvermanba04b2d2016-01-19 16:46:10 +0000157chmod 0755 "${base}/bin/bazel" "${base}/bin/bazel-real"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000158echo -n .
159chmod -R og-w "${base}"
160chmod -R og+rX "${base}"
161chmod -R u+rwX "${base}"
162echo -n .
163
164ln -s "${base}/bin/bazel" "${bin}/bazel"
165echo -n .
166
andy g scott ?4dd6dac2018-06-18 09:05:31 -0700167if [ "${should_uncompress}" = true ] && [ "${UID}" -ne 0 ]; then
Kristina Chodorow00ed6fe2016-03-03 17:16:45 +0000168 # Uncompress the bazel base install for faster startup time
169 "${bin}/bazel" help >/dev/null
Damien Martin-Guillerezfa465f62016-02-15 14:43:02 +0000170fi
Kristina Chodorow84450b82016-01-28 15:16:02 +0000171echo .
172
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000173cat <<EOF
174
175Bazel is now installed!
176
177Make sure you have "${bin}" in your path. You can also activate bash
178completion by adding the following line to your ${BASHRC}:
179 source ${base}/bin/bazel-complete.bash
180
Damien Martin-Guillerez4885eef2016-10-28 12:02:50 +0000181See http://bazel.build/docs/getting-started.html to start a new project!
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000182EOF
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000183exit 0