blob: bad5f8e500bf5727d1eda3e8e1ae6566179cc609 [file] [log] [blame]
Serge Bazanskif369cfa2020-05-22 18:36:42 +02001#!/usr/bin/env bash
2
3set -euo pipefail
4
5# Most of this comes from github.com/bazelbuild/bazel-gazelle/internal/gazelle.bash.in.
6# It's very hacky.
7
8# Copyright 2017 The Bazel Authors. All rights reserved.
9#
10# Licensed under the Apache License, Version 2.0 (the "License");
11# you may not use this file except in compliance with the License.
12# You may obtain a copy of the License at
13#
14# http://www.apache.org/licenses/LICENSE-2.0
15#
16# Unless required by applicable law or agreed to in writing, software
17# distributed under the License is distributed on an "AS IS" BASIS,
18# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19# See the License for the specific language governing permissions and
20# limitations under the License.
21
22FIETSJE_SHORT_PATH=@@FIETSJE_SHORT_PATH@@
23GOTOOL=@@GOTOOL@@
24
25# find_runfile prints the location of a runfile in the source workspace,
26# either by reading the symbolic link or reading the runfiles manifest.
27function find_runfile {
28 local runfile=$1
29 if [ -f "$runfile" ]; then
30 readlink "$runfile"
31 return
32 fi
33 runfile=$(echo "$runfile" | sed -e 's!^\(\.\./\|external/\)!!')
34 if grep -q "^$runfile" MANIFEST; then
35 grep "^$runfile" MANIFEST | head -n 1 | cut -d' ' -f2
36 return
37 fi
38 # printing nothing indicates failure
39}
40
41# bazel_build_get_path builds a given target and prints the absolute path
42# to the generated binary. This only works for rules that produce a single file.
43function bazel_build_get_path {
44 local build_log=$(mktemp fietsje_build.XXXX.json --tmpdir)
45 bazel build --build_event_json_file="$build_log" "$1"
46 grep "^{\"id\":{\"targetCompleted\":{\"label\":\"$1\"" "$build_log" | \
47 sed -e 's!^.*file://\([^"]*\).*$!\1!'
48 rm -f "$build_log"
49}
50
51# set_goroot attempts to set GOROOT to the SDK used by rules_go. fietsje
52# invokes tools inside the Go SDK for dependency management. It's good to
53# use the SDK used by the workspace in case the Go SDK is not installed
54# on the host system or is a different version.
55function set_goroot {
56 local gotool=$(find_runfile "$GOTOOL")
57 if [ -z "$gotool" ]; then
58 echo "$0: warning: could not locate GOROOT used by rules_go" >&2
59 return
60 fi
61 export GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
62 if type cygpath >/dev/null 2>&1; then
63 # On Windows, convert the path to something usable outside of bash.
64 GOROOT=$(cygpath -w "$GOROOT")
65 fi
66}
67
68set_goroot
69fietsje_short_path=$(find_runfile "$FIETSJE_SHORT_PATH")
70if [ -z "$fietsje_short_path" ]; then
71 echo "error: could not locate fietsje binary" >&2
72 exit 1
73fi
74if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
75 echo "error: BUILD_WORKSPACE_DIRECOTRY not set" >&2
76 exit 1
77fi
78cd "$BUILD_WORKSPACE_DIRECTORY"
79"$fietsje_short_path" -shelf_path third_party/go/shelf.pb.text -repositories_bzl third_party/go/repositories.bzl