blob: ae332af6f6fbf5ffc88f4b4274574866f7fab5a6 [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
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000046 exit 1
47}
48
49prefix="/usr/local"
50bin="%prefix%/bin"
51base="%prefix%/lib/bazel"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000052
53for opt in "${@}"; do
54 case $opt in
55 --prefix=*)
56 prefix="$(echo "$opt" | cut -d '=' -f 2-)"
57 ;;
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000058 --bin=*)
59 bin="$(echo "$opt" | cut -d '=' -f 2-)"
60 ;;
61 --base=*)
62 base="$(echo "$opt" | cut -d '=' -f 2-)"
63 ;;
64 --user)
65 bin="$HOME/bin"
66 base="$HOME/.bazel"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000067 ;;
68 *)
69 usage
70 ;;
71 esac
72done
73
74bin="${bin//%prefix%/${prefix}}"
75base="${base//%prefix%/${prefix}}"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000076
Damien Martin-Guillerez3dff9a32016-07-04 13:38:20 +000077test_write() {
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000078 local file="$1"
79 while [ "$file" != "/" ] && [ -n "${file}" ] && [ ! -e "$file" ]; do
80 file="$(dirname "${file}")"
81 done
82 [ -w "${file}" ] || {
83 echo >&2
84 echo "The Bazel installer must have write access to $1!" >&2
John Cater1c4ae472016-07-20 17:31:00 +000085 echo "Consider using the --user flag to install Bazel under $HOME/bin instead." >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000086 echo >&2
87 usage
88 }
89}
90
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +000091# Test for dependencies
92# unzip
93if ! which unzip >/dev/null; then
94 echo >&2
95 echo "unzip not found, please install the corresponding package." >&2
Damien Martin-Guillerez4885eef2016-10-28 12:02:50 +000096 echo "See http://bazel.build/docs/install.html for more information on" >&2
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +000097 echo "dependencies of Bazel." >&2
98 exit 1
99fi
100
101# java
102if [ -z "${JAVA_HOME-}" ]; then
103 case "$(uname -s | tr 'A-Z' 'a-z')" in
104 linux)
105 JAVA_HOME="$(readlink -f $(which javac) 2>/dev/null | sed 's_/bin/javac__')" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000106 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000107 ;;
108 freebsd)
109 JAVA_HOME="/usr/local/openjdk8"
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000110 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000111 ;;
112 darwin)
113 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000114 BASHRC="~/.bash_profile"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000115 ;;
116 esac
117fi
philwobaca6e42017-05-15 22:00:47 +0200118
119# Only check for an installed JDK if this version of Bazel does not contain a
120# bundled JDK.
121case "$0" in
122 *without-jdk*)
123 if [ ! -x "${JAVA_HOME}/bin/javac" ]; then
124 echo >&2
125 echo "Java not found, please install the corresponding package." >&2
126 echo "See http://bazel.build/docs/install.html for more information on" >&2
127 echo "dependencies of Bazel." >&2
128 exit 1
129 fi
130 ;;
131esac
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000132
133# Test for write access
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000134test_write "${bin}"
135test_write "${base}"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000136
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000137# Do the actual installation
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000138echo -n "Uncompressing."
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000139
140# Cleaning-up, with some guards.
Damien Martin-Guillerezcab6e872016-05-02 11:39:17 +0000141rm -f "${bin}/bazel"
Damien Martin-Guillerez2c2e70d2015-09-08 17:23:02 +0000142if [ -d "${base}" -a -x "${base}/bin/bazel" ]; then
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000143 rm -fr "${base}"
144fi
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000145
Kristina Chodorow84450b82016-01-28 15:16:02 +0000146mkdir -p ${bin} ${base} ${base}/bin ${base}/etc
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000147echo -n .
148
Brian Silvermanba04b2d2016-01-19 16:46:10 +0000149unzip -q "${BASH_SOURCE[0]}" bazel bazel-real bazel-complete.bash -d "${base}/bin"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000150echo -n .
Brian Silvermanba04b2d2016-01-19 16:46:10 +0000151chmod 0755 "${base}/bin/bazel" "${base}/bin/bazel-real"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000152echo -n .
153chmod -R og-w "${base}"
154chmod -R og+rX "${base}"
155chmod -R u+rwX "${base}"
156echo -n .
157
158ln -s "${base}/bin/bazel" "${bin}/bazel"
159echo -n .
160
Philipp Wollermann72056112017-05-08 05:17:06 -0400161if [ "${UID}" -ne 0 ]; then
Kristina Chodorow00ed6fe2016-03-03 17:16:45 +0000162 # Uncompress the bazel base install for faster startup time
163 "${bin}/bazel" help >/dev/null
Damien Martin-Guillerezfa465f62016-02-15 14:43:02 +0000164fi
Kristina Chodorow84450b82016-01-28 15:16:02 +0000165echo .
166
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000167cat <<EOF
168
169Bazel is now installed!
170
171Make sure you have "${bin}" in your path. You can also activate bash
172completion by adding the following line to your ${BASHRC}:
173 source ${base}/bin/bazel-complete.bash
174
Damien Martin-Guillerez4885eef2016-10-28 12:02:50 +0000175See http://bazel.build/docs/getting-started.html to start a new project!
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000176EOF
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000177exit 0