Philipp Wollermann | a5afe95 | 2016-06-21 14:58:09 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Brian Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 2 | |
| 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 Wollermann | a5afe95 | 2016-06-21 14:58:09 +0000 | [diff] [blame] | 17 | set -eu |
| 18 | |
Brian Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 19 | # 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 Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 24 | # `readlink -f` that works on OSX too. |
| 25 | function 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 Fabulich | ccaee70 | 2016-12-02 16:23:06 +0000 | [diff] [blame] | 64 | export BAZEL_REAL="$(dirname "$(get_realpath "${BASH_SOURCE[0]}")")/bazel-real" |
| 65 | |
| 66 | WORKSPACE_DIR="${PWD}" |
| 67 | while [[ "${WORKSPACE_DIR}" != / ]]; do |
| 68 | if [[ -e "${WORKSPACE_DIR}/WORKSPACE" ]]; then |
| 69 | break; |
| 70 | fi |
| 71 | WORKSPACE_DIR="$(dirname "${WORKSPACE_DIR}")" |
| 72 | done |
| 73 | readonly WORKSPACE_DIR |
| 74 | |
| 75 | if [[ -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 |
| 81 | fi |
Brian Silverman | ba04b2d | 2016-01-19 16:46:10 +0000 | [diff] [blame] | 82 | |
| 83 | if [[ ! -x "${BAZEL_REAL}" ]]; then |
| 84 | echo "Failed to find underlying Bazel executable at ${BAZEL_REAL}" >&2 |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | exec -a "$0" "${BAZEL_REAL}" "$@" |