blob: ed506d28c2ba6125480fc0164135c65a48daf6aa [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001#!/bin/bash
2
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00003# Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01004#
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
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000017# This script bootstraps building a Bazel binary without Bazel then
18# use this compiled Bazel to bootstrap Bazel itself. It can also
19# be provided with a previous version of Bazel to bootstrap Bazel
20# itself.
21# The resulting binary can be found at output/bazel.
David Mankin5cafe132015-05-28 20:20:56 +000022
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023set -o errexit
24
Laszlo Csomorf5525e82017-01-30 18:11:58 +000025# Check that the bintools can be found, otherwise we would see very confusing
26# error messages.
Laszlo Csomore79a1102017-10-06 16:45:05 +020027# For example on Windows we would find "FIND.EXE" instead of "/usr/bin/find"
28# when running "find".
Klaus Aehlig27db2d82017-08-28 12:41:52 +020029hash tr >&/dev/null || {
30 echo >&2 "ERROR: cannot locate GNU coreutils; check your PATH."
L?szl? Csomor90ecf932017-12-06 06:53:13 -080031 echo >&2 " You may need to run the following command:"
32 echo >&2 " export PATH=\"/bin:/usr/bin:\$PATH\""
Laszlo Csomorf5525e82017-01-30 18:11:58 +000033 exit 1
34}
35
Laszlo Csomore79a1102017-10-06 16:45:05 +020036# Ensure Python is on the PATH on Windows,otherwise we would see
37# "LAUNCHER ERROR" messages from py_binary exe launchers.
Laszlo Csomor4cb8cb82018-01-30 06:59:49 -080038case "$(uname -s | tr "[:upper:]" "[:lower:]")" in
Laszlo Csomore79a1102017-10-06 16:45:05 +020039msys*|mingw*|cygwin*)
Laszlo Csomor4cb8cb82018-01-30 06:59:49 -080040 # Ensure Python is on the PATH, otherwise the bootstrapping fails later.
Laszlo Csomore79a1102017-10-06 16:45:05 +020041 which python.exe >&/dev/null || {
42 echo >&2 "ERROR: cannot locate python.exe; check your PATH."
L?szl? Csomor90ecf932017-12-06 06:53:13 -080043 echo >&2 " You may need to run the following command, or something"
44 echo >&2 " similar, depending on where you installed Python:"
45 echo >&2 " export PATH=\"/c/Python27:\$PATH\""
Laszlo Csomore79a1102017-10-06 16:45:05 +020046 exit 1
47 }
Laszlo Csomor4cb8cb82018-01-30 06:59:49 -080048 # Ensure TMPDIR uses the user-specified TMPDIR or TMP or TEMP.
49 # This is necessary to avoid overly longs paths during bootstrapping, see for
50 # example https://github.com/bazelbuild/bazel/issues/4536
51 export TMPDIR="${TMPDIR:-${TMP:-${TEMP:-}}}"
Laszlo Csomore79a1102017-10-06 16:45:05 +020052esac
53
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054cd "$(dirname "$0")"
55
Julio Merino5909d9d2016-02-25 21:44:31 +000056# Set the default verbose mode in buildenv.sh so that we do not display command
57# output unless there is a failure. We do this conditionally to offer the user
58# a chance of overriding this in case they want to do so.
59: ${VERBOSE:=no}
60
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000061source scripts/bootstrap/buildenv.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000063mkdir -p output
dmarting03976a52017-08-29 12:42:58 +020064: ${BAZEL:=}
Kristina Chodorowac271142015-03-06 16:00:46 +000065
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000066#
67# Create an initial binary so we can host ourself
68#
69if [ ! -x "${BAZEL}" ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000070 new_step 'Building Bazel from scratch'
71 source scripts/bootstrap/compile.sh
Daniel Wagner-Hall13ba3312015-03-20 05:45:45 +000072fi
73
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000074#
75# Bootstrap bazel using the previous bazel binary = release binary
76#
Damien Martin-Guillereza377af02015-06-24 12:04:27 +000077if [ "${EMBED_LABEL-x}" = "x" ]; then
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +000078 # Add a default label when unspecified
79 git_sha1=$(git_sha1)
Damien Martin-Guillerez28e67b52016-03-14 11:01:31 +000080 EMBED_LABEL="$(get_last_version) (@${git_sha1:-non-git})"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081fi
82
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +000083if [[ $PLATFORM == "darwin" ]] && \
84 xcodebuild -showsdks 2> /dev/null | grep -q '\-sdk iphonesimulator'; then
Damien Martin-Guillerez1540d8f2016-09-07 11:49:39 +000085 EXTRA_BAZEL_ARGS="${EXTRA_BAZEL_ARGS-} --define IPHONE_SDK=1"
Dmitry Lomovac6ed792015-12-22 19:24:00 +000086fi
Yun Pengcb8a5e22017-03-08 14:30:49 +000087
Damien Martin-Guillerez3d796fe2016-01-11 10:07:57 +000088source scripts/bootstrap/bootstrap.sh
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089
dmarting03976a52017-08-29 12:42:58 +020090new_step 'Building Bazel with Bazel'
91display "."
92log "Building output/bazel"
cpeyserd852e482017-09-07 22:16:06 +020093# We set host and target platform directly since the defaults in @bazel_tools
94# have not yet been generated.
Liam Miller-Cushon7c49bd92018-06-18 09:29:52 -070095bazel_build "src:bazel_nojdk${EXE_EXT}" \
John Cater32c5add2018-03-19 08:51:46 -070096 --host_platform=@bazel_tools//platforms:host_platform \
97 --platforms=@bazel_tools//platforms:target_platform \
dmarting03976a52017-08-29 12:42:58 +020098 || fail "Could not build Bazel"
Liam Miller-Cushon7c49bd92018-06-18 09:29:52 -070099bazel_bin_path="$(get_bazel_bin_path)/src/bazel_nojdk${EXE_EXT}"
dmarting03976a52017-08-29 12:42:58 +0200100[ -e "$bazel_bin_path" ] \
101 || fail "Could not find freshly built Bazel binary at '$bazel_bin_path'"
102cp -f "$bazel_bin_path" "output/bazel${EXE_EXT}" \
103 || fail "Could not copy '$bazel_bin_path' to 'output/bazel${EXE_EXT}'"
104chmod 0755 "output/bazel${EXE_EXT}"
105BAZEL="$(pwd)/output/bazel${EXE_EXT}"
Damien Martin-Guillerezd6f48082015-06-05 10:50:43 +0000106
Damien Martin-Guillerezd47a8ef2015-06-10 11:54:50 +0000107clear_log
108display "Build successful! Binary is here: ${BAZEL}"