Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 3 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 4 | # |
| 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 |
| 20 | install_prefix=${1:-"/usr/local"} |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 21 | # TODO(kchodorow): delete by April 2016. |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 22 | bazelrc=${2:-"/etc/bazel.bazelrc"} |
| 23 | |
| 24 | progname="$0" |
| 25 | |
| 26 | echo "Bazel installer" |
| 27 | echo "---------------" |
| 28 | echo |
| 29 | cat <<'EOF' |
| 30 | %release_info% |
| 31 | EOF |
| 32 | |
| 33 | function 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-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 37 | 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-Guillerez | def8ce3 | 2016-02-15 12:09:09 +0000 | [diff] [blame] | 39 | 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-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 42 | exit 1 |
| 43 | } |
| 44 | |
| 45 | prefix="/usr/local" |
| 46 | bin="%prefix%/bin" |
| 47 | base="%prefix%/lib/bazel" |
| 48 | bazelrc="/etc/bazel.bazelrc" |
| 49 | |
| 50 | for 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 |
| 73 | done |
| 74 | |
| 75 | bin="${bin//%prefix%/${prefix}}" |
| 76 | base="${base//%prefix%/${prefix}}" |
| 77 | bazelrc="${bazelrc//%prefix%/${prefix}}" |
| 78 | |
| 79 | function 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-Guillerez | a1d9396 | 2015-08-31 12:23:50 +0000 | [diff] [blame] | 92 | # Test for dependencies |
| 93 | # unzip |
| 94 | if ! 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 |
| 100 | fi |
| 101 | |
| 102 | # java |
| 103 | if [ -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-Guillerez | e8f8dc5 | 2015-09-04 15:27:45 +0000 | [diff] [blame] | 107 | BASHRC="~/.bashrc" |
Damien Martin-Guillerez | a1d9396 | 2015-08-31 12:23:50 +0000 | [diff] [blame] | 108 | ;; |
| 109 | freebsd) |
| 110 | JAVA_HOME="/usr/local/openjdk8" |
Damien Martin-Guillerez | e8f8dc5 | 2015-09-04 15:27:45 +0000 | [diff] [blame] | 111 | BASHRC="~/.bashrc" |
Damien Martin-Guillerez | a1d9396 | 2015-08-31 12:23:50 +0000 | [diff] [blame] | 112 | ;; |
| 113 | darwin) |
| 114 | JAVA_HOME="$(/usr/libexec/java_home -v ${JAVA_VERSION}+ 2> /dev/null)" || true |
Damien Martin-Guillerez | e8f8dc5 | 2015-09-04 15:27:45 +0000 | [diff] [blame] | 115 | BASHRC="~/.bash_profile" |
Damien Martin-Guillerez | a1d9396 | 2015-08-31 12:23:50 +0000 | [diff] [blame] | 116 | ;; |
| 117 | esac |
| 118 | fi |
| 119 | if [ ! -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 |
| 125 | fi |
| 126 | |
| 127 | # Test for write access |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 128 | test_write "${bin}" |
| 129 | test_write "${base}" |
| 130 | test_write "${bazelrc}" |
| 131 | |
Damien Martin-Guillerez | a1d9396 | 2015-08-31 12:23:50 +0000 | [diff] [blame] | 132 | # Do the actual installation |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 133 | echo -n "Uncompressing." |
Damien Martin-Guillerez | 2984f1c | 2015-09-04 13:41:54 +0000 | [diff] [blame] | 134 | |
| 135 | # Cleaning-up, with some guards. |
| 136 | if [ -f "${bin}/bazel" ]; then |
| 137 | rm -f "${bin}/bazel" |
| 138 | fi |
Damien Martin-Guillerez | 2c2e70d | 2015-09-08 17:23:02 +0000 | [diff] [blame] | 139 | if [ -d "${base}" -a -x "${base}/bin/bazel" ]; then |
Damien Martin-Guillerez | 2984f1c | 2015-09-04 13:41:54 +0000 | [diff] [blame] | 140 | rm -fr "${base}" |
| 141 | fi |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 142 | |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 143 | mkdir -p ${bin} ${base} ${base}/bin ${base}/etc |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 144 | echo -n . |
| 145 | |
Brian Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 146 | unzip -q "${BASH_SOURCE[0]}" bazel bazel-real bazel-complete.bash -d "${base}/bin" |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 147 | echo -n . |
Brian Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 148 | chmod 0755 "${base}/bin/bazel" "${base}/bin/bazel-real" |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 149 | echo -n . |
| 150 | chmod -R og-w "${base}" |
| 151 | chmod -R og+rX "${base}" |
| 152 | chmod -R u+rwX "${base}" |
| 153 | echo -n . |
| 154 | |
| 155 | ln -s "${base}/bin/bazel" "${bin}/bazel" |
| 156 | echo -n . |
| 157 | |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 158 | if [ -f "${bazelrc}" ]; then |
| 159 | echo |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 160 | echo "${bazelrc} already exists, moving it to ${bazelrc}.bak." |
| 161 | mv "${bazelrc}" "${bazelrc}.bak" |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 162 | fi |
| 163 | |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 164 | # Not necessary, but this way it matches the Debian package. |
| 165 | touch "${bazelrc}" |
Kristina Chodorow | 00ed6fe | 2016-03-03 17:16:45 +0000 | [diff] [blame] | 166 | if [ "${UID}" -eq 0 ]; then |
Damien Martin-Guillerez | fa465f6 | 2016-02-15 14:43:02 +0000 | [diff] [blame] | 167 | chmod 0644 "${bazelrc}" |
Kristina Chodorow | 00ed6fe | 2016-03-03 17:16:45 +0000 | [diff] [blame] | 168 | else |
| 169 | # Uncompress the bazel base install for faster startup time |
| 170 | "${bin}/bazel" help >/dev/null |
Damien Martin-Guillerez | fa465f6 | 2016-02-15 14:43:02 +0000 | [diff] [blame] | 171 | fi |
Kristina Chodorow | 84450b8 | 2016-01-28 15:16:02 +0000 | [diff] [blame] | 172 | echo . |
| 173 | |
Damien Martin-Guillerez | e8f8dc5 | 2015-09-04 15:27:45 +0000 | [diff] [blame] | 174 | cat <<EOF |
| 175 | |
| 176 | Bazel is now installed! |
| 177 | |
| 178 | Make sure you have "${bin}" in your path. You can also activate bash |
| 179 | completion by adding the following line to your ${BASHRC}: |
| 180 | source ${base}/bin/bazel-complete.bash |
| 181 | |
| 182 | See http://bazel.io/docs/getting-started.html to start a new project! |
| 183 | EOF |
Damien Martin-Guillerez | ab13f68 | 2015-07-28 08:19:32 +0000 | [diff] [blame] | 184 | exit 0 |