blob: ed26599343d4913c0af1501995549fc603e949a4 [file] [log] [blame] [edit]
#!/usr/bin/env bash
# Copyright 2017 The Bazel Authors. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
@@GENERATED_MESSAGE@@
set -euo pipefail
RUNNER_LABEL=@@RUNNER_LABEL@@
GAZELLE_SHORT_PATH=@@GAZELLE_SHORT_PATH@@
GAZELLE_LABEL=@@GAZELLE_LABEL@@
ARGS=@@ARGS@@
GOTOOL=@@GOTOOL@@
# find_runfile prints the location of a runfile in the source workspace,
# either by reading the symbolic link or reading the runfiles manifest.
function find_runfile {
local runfile=$1
if [ -f "$runfile" ]; then
readlink "$runfile"
return
fi
runfile=$(echo "$runfile" | sed -e 's!^\(\.\./\|external/\)!!')
if grep -q "^$runfile" MANIFEST; then
grep "^$runfile" MANIFEST | head -n 1 | cut -d' ' -f2
return
fi
# printing nothing indicates failure
}
# bazel_build_get_path builds a given target and prints the absolute path
# to the generated binary. This only works for rules that produce a single file.
function bazel_build_get_path {
local build_log=$(mktemp gazelle_build.XXXX.json --tmpdir)
bazel build --build_event_json_file="$build_log" "$1"
grep "^{\"id\":{\"targetCompleted\":{\"label\":\"$1\"" "$build_log" | \
sed -e 's!^.*file://\([^"]*\).*$!\1!'
rm -f "$build_log"
}
# set_goroot attempts to set GOROOT to the SDK used by rules_go. gazelle
# invokes tools inside the Go SDK for dependency management. It's good to
# use the SDK used by the workspace in case the Go SDK is not installed
# on the host system or is a different version.
function set_goroot {
local gotool=$(find_runfile "$GOTOOL")
if [ -z "$gotool" ]; then
echo "$0: warning: could not locate GOROOT used by rules_go" >&2
return
fi
export GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
if type cygpath >/dev/null 2>&1; then
# On Windows, convert the path to something usable outside of bash.
GOROOT=$(cygpath -w "$GOROOT")
fi
}
# Check whether the script was executed by Bazel. The gazelle macro prepends
# an argument that tells us this.
is_bazel_run=false
if [ "${1-}" = "-bazel_run" ]; then
is_bazel_run=true
shift
fi
# If arguments were provided on the command line, either replace or augment
# the generated args.
if [ "${1-}" = "-args" ]; then
shift
ARGS+=("$@")
elif [ $# -ne 0 ]; then
ARGS=("$@")
fi
if [ "$is_bazel_run" = true ]; then
# If the script was invoked by "bazel run", jump out of the execroot, into
# the workspace before running Gazelle.
# TODO(jayconrod): detect when a command can't be run this way.
set_goroot
gazelle_short_path=$(find_runfile "$GAZELLE_SHORT_PATH")
if [ -z "$gazelle_short_path" ]; then
echo "error: could not locate gazelle binary" >&2
exit 1
fi
if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
echo "error: BUILD_WORKSPACE_DIRECOTRY not set" >&2
exit 1
fi
cd "$BUILD_WORKSPACE_DIRECTORY"
"$gazelle_short_path" "${ARGS[@]}"
else
# If the script was invoked directly, check whether the script is out of
# date before proceeding.
new_runner_script=$(bazel_build_get_path "$RUNNER_LABEL")
if ! diff "$new_runner_script" "$0" &>/dev/null; then
cat - >&2 <<EOF
error: $0: script is out of date. Refresh it with the command:
bazel build $RUNNER_LABEL && cp -fv "$new_runner_script" "$0"
EOF
exit 1
fi
# Rebuild and run Gazelle.
gazelle_exe=$(bazel_build_get_path "$GAZELLE_LABEL")
"$gazelle_exe" "${ARGS[@]}"
fi