blob: 43bc593f85fff5cd5de39641269aafebf8ea22e0 [file] [log] [blame]
Philipp Wollermanna5afe952016-06-21 14:58:09 +00001#!/bin/bash
Brian Silvermanba04b2d2016-01-19 16:46:10 +00002
3# Copyright 2015 The Bazel Authors. All rights reserved.
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
Philipp Wollermanna5afe952016-06-21 14:58:09 +000017set -eu
18
Brian Silvermanba04b2d2016-01-19 16:46:10 +000019# This is a script which is installed instead of the real Bazel binary.
20# It looks for a tools/bazel executable next to the containing WORKSPACE
21# file and runs that. If that's not found, it runs the real Bazel binary which
22# is installed next to this script as bazel-real.
23
Brian Silvermanba04b2d2016-01-19 16:46:10 +000024# `readlink -f` that works on OSX too.
25function get_realpath() {
26 if [ "$(uname -s)" == "Darwin" ]; then
27 local queue="$1"
28 if [[ "${queue}" != /* ]] ; then
29 # Make sure we start with an absolute path.
30 queue="${PWD}/${queue}"
31 fi
32 local current=""
33 while [ -n "${queue}" ]; do
34 # Removing a trailing /.
35 queue="${queue#/}"
36 # Pull the first path segment off of queue.
37 local segment="${queue%%/*}"
38 # If this is the last segment.
39 if [[ "${queue}" != */* ]] ; then
40 segment="${queue}"
41 queue=""
42 else
43 # Remove that first segment.
44 queue="${queue#*/}"
45 fi
46 local link="${current}/${segment}"
47 if [ -h "${link}" ] ; then
48 link="$(readlink "${link}")"
49 queue="${link}/${queue}"
50 if [[ "${link}" == /* ]] ; then
51 current=""
52 fi
53 else
54 current="${link}"
55 fi
56 done
57
58 echo "${current}"
59 else
60 readlink -f "$1"
61 fi
62}
63
Dan Fabulichccaee702016-12-02 16:23:06 +000064export BAZEL_REAL="$(dirname "$(get_realpath "${BASH_SOURCE[0]}")")/bazel-real"
65
66WORKSPACE_DIR="${PWD}"
67while [[ "${WORKSPACE_DIR}" != / ]]; do
68 if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
69 break;
70 fi
71 WORKSPACE_DIR="$(dirname "${WORKSPACE_DIR}")"
72done
73readonly WORKSPACE_DIR
74
75if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then
76 readonly WRAPPER="${WORKSPACE_DIR}/tools/bazel"
77
78 if [[ -x "${WRAPPER}" ]]; then
79 exec -a "$0" "${WRAPPER}" "$@"
80 fi
81fi
Brian Silvermanba04b2d2016-01-19 16:46:10 +000082
83if [[ ! -x "${BAZEL_REAL}" ]]; then
84 echo "Failed to find underlying Bazel executable at ${BAZEL_REAL}" >&2
85 exit 1
86fi
87
88exec -a "$0" "${BAZEL_REAL}" "$@"