blob: 5b1cfa943c9e64771782a7f720540386529495d5 [file] [log] [blame]
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +00001#!/bin/bash -e
2
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
17# Bazel self-extractable installer
18
19# Installation and etc prefix can be overriden from command line
20install_prefix=${1:-"/usr/local"}
Kristina Chodorow84450b82016-01-28 15:16:02 +000021# TODO(kchodorow): delete by April 2016.
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000022bazelrc=${2:-"/etc/bazel.bazelrc"}
23
24progname="$0"
25
26echo "Bazel installer"
27echo "---------------"
28echo
29cat <<'EOF'
30%release_info%
31EOF
32
33function usage() {
34 echo "Usage: $progname [options]" >&2
35 echo "Options are:" >&2
36 echo " --prefix=/some/path set the prefix path (default=/usr/local)." >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000037 echo " --bin= set the binary folder path (default=%prefix%/bin)." >&2
38 echo " --base= set the base install path (default=%prefix%/lib/bazel)." >&2
Damien Martin-Guillerezdef8ce32016-02-15 12:09:09 +000039 echo " --bazelrc= set the path to bazelrc (default=/etc/bazelrc)." >&2
40 echo " --user configure for user install, expands to:" >&2
41 echo ' --bin=$HOME/bin --base=$HOME/.bazel --bazelrc=$HOME/.bazelrc' >&2
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +000042 exit 1
43}
44
45prefix="/usr/local"
46bin="%prefix%/bin"
47base="%prefix%/lib/bazel"
48bazelrc="/etc/bazel.bazelrc"
49
50for opt in "${@}"; do
51 case $opt in
52 --prefix=*)
53 prefix="$(echo "$opt" | cut -d '=' -f 2-)"
54 ;;
55 --bazelrc=*)
56 bazelrc="$(echo "$opt" | cut -d '=' -f 2-)"
57 ;;
58 --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"
67 bazelrc="$HOME/.bazelrc"
68 ;;
69 *)
70 usage
71 ;;
72 esac
73done
74
75bin="${bin//%prefix%/${prefix}}"
76base="${base//%prefix%/${prefix}}"
77bazelrc="${bazelrc//%prefix%/${prefix}}"
78
79function test_write() {
80 local file="$1"
81 while [ "$file" != "/" ] && [ -n "${file}" ] && [ ! -e "$file" ]; do
82 file="$(dirname "${file}")"
83 done
84 [ -w "${file}" ] || {
85 echo >&2
86 echo "The Bazel installer must have write access to $1!" >&2
87 echo >&2
88 usage
89 }
90}
91
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +000092# Test for dependencies
93# unzip
94if ! which unzip >/dev/null; then
95 echo >&2
96 echo "unzip not found, please install the corresponding package." >&2
97 echo "See http://bazel.io/docs/install.html for more information on" >&2
98 echo "dependencies of Bazel." >&2
99 exit 1
100fi
101
102# java
103if [ -z "${JAVA_HOME-}" ]; then
104 case "$(uname -s | tr 'A-Z' 'a-z')" in
105 linux)
106 JAVA_HOME="$(readlink -f $(which javac) 2>/dev/null | sed 's_/bin/javac__')" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000107 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000108 ;;
109 freebsd)
110 JAVA_HOME="/usr/local/openjdk8"
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000111 BASHRC="~/.bashrc"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000112 ;;
113 darwin)
114 JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" || true
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000115 BASHRC="~/.bash_profile"
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000116 ;;
117 esac
118fi
119if [ ! -x "${JAVA_HOME}/bin/javac" ]; then
120 echo >&2
121 echo "Java not found, please install the corresponding package" >&2
122 echo "See http://bazel.io/docs/install.html for more information on" >&2
123 echo "dependencies of Bazel." >&2
124 exit 1
125fi
126
127# Test for write access
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000128test_write "${bin}"
129test_write "${base}"
130test_write "${bazelrc}"
131
Damien Martin-Guillereza1d93962015-08-31 12:23:50 +0000132# Do the actual installation
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000133echo -n "Uncompressing."
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000134
135# Cleaning-up, with some guards.
Damien Martin-Guillerezcab6e872016-05-02 11:39:17 +0000136rm -f "${bin}/bazel"
Damien Martin-Guillerez2c2e70d2015-09-08 17:23:02 +0000137if [ -d "${base}" -a -x "${base}/bin/bazel" ]; then
Damien Martin-Guillerez2984f1c2015-09-04 13:41:54 +0000138 rm -fr "${base}"
139fi
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000140
Kristina Chodorow84450b82016-01-28 15:16:02 +0000141mkdir -p ${bin} ${base} ${base}/bin ${base}/etc
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000142echo -n .
143
Brian Silvermanba04b2d2016-01-19 16:46:10 +0000144unzip -q "${BASH_SOURCE[0]}" bazel bazel-real bazel-complete.bash -d "${base}/bin"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000145echo -n .
Brian Silvermanba04b2d2016-01-19 16:46:10 +0000146chmod 0755 "${base}/bin/bazel" "${base}/bin/bazel-real"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000147echo -n .
148chmod -R og-w "${base}"
149chmod -R og+rX "${base}"
150chmod -R u+rwX "${base}"
151echo -n .
152
153ln -s "${base}/bin/bazel" "${bin}/bazel"
154echo -n .
155
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000156if [ -f "${bazelrc}" ]; then
157 echo
Kristina Chodorow84450b82016-01-28 15:16:02 +0000158 echo "${bazelrc} already exists, moving it to ${bazelrc}.bak."
159 mv "${bazelrc}" "${bazelrc}.bak"
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000160fi
161
Kristina Chodorow84450b82016-01-28 15:16:02 +0000162# Not necessary, but this way it matches the Debian package.
163touch "${bazelrc}"
Kristina Chodorow00ed6fe2016-03-03 17:16:45 +0000164if [ "${UID}" -eq 0 ]; then
Damien Martin-Guillerezfa465f62016-02-15 14:43:02 +0000165 chmod 0644 "${bazelrc}"
Kristina Chodorow00ed6fe2016-03-03 17:16:45 +0000166else
167 # Uncompress the bazel base install for faster startup time
168 "${bin}/bazel" help >/dev/null
Damien Martin-Guillerezfa465f62016-02-15 14:43:02 +0000169fi
Kristina Chodorow84450b82016-01-28 15:16:02 +0000170echo .
171
Damien Martin-Guillereze8f8dc52015-09-04 15:27:45 +0000172cat <<EOF
173
174Bazel is now installed!
175
176Make sure you have "${bin}" in your path. You can also activate bash
177completion by adding the following line to your ${BASHRC}:
178 source ${base}/bin/bazel-complete.bash
179
180See http://bazel.io/docs/getting-started.html to start a new project!
181EOF
Damien Martin-Guillerezab13f682015-07-28 08:19:32 +0000182exit 0