aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/get_project_paths.sh
blob: 327e2b2c7a5f655c4a99b78a3875d16e65315053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# Copyright 2015 Google Inc. 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.
#
# Gets all libraries needed for IDE support of a Bazel workspace

set -eu

# Build everything
bazel build ${TARGET} >&2 || exit $?

function query() {
    bazel query "$@"
}

# Find the bazel-workspaceName link
EXECUTION_ROOT_PATH=$(bazel info execution_root)
WORKSPACE_PATH=$(bazel info workspace)
for i in ${WORKSPACE_PATH}/bazel-*; do
  if [[ "$(readlink $i)" == "${EXECUTION_ROOT_PATH}" ]]; then
    EXECUTION_ROOT=$(basename $i)
  fi
done

# Do a bazel query and replace the result by paths relative to the workspace.
#   @repo//package:target will be replaced by
#                         bazel-%workspaceName%/external/repo/package/target
#   //package:target will be replaced by package/target
function query_to_path() {
   query "$1" | sed 's|:|/|' \
     | sed 's|@\(.*\)///\{0,1\}|'"${EXECUTION_ROOT}"'/external/\1/|' \
     | sed 's|^//||' | sort -u
}

# Now for java each targets, find all sources and all jars
PATHS=$(query_to_path 'filter("\.java$",
                    deps(kind("(java_binary|java_library|java_test|java_plugin)",
                         deps('"$TARGET"')))
                    except deps(//tools/...))')
# Java Files:
JAVA_PATHS=$(echo "$PATHS" | sed 's_\(/java\(tests\)\{0,1\}\)/.*$_\1_' | sort -u)

# Java plugins
PLUGIN_PATHS=$(query_to_path 'filter("\.jar$",
                                     deps(kind(java_import,
                                               deps(kind(java_plugin,
                                                         deps('"$TARGET"')))))
                                     except deps(//tools/...))')
# Jar Files:
JAR_FILES=$(query_to_path 'filter("\.jar$", deps(kind(java_import, deps('"$TARGET"')))
                                            except deps(//tools/...))')

# Generated files are direct dependencies of java rules that are not java rules,
# filegroup, binaries or external dependencies.
# We also handle genproto separately it is output in bazel-genfiles not in
# bazel-bin.
# We suppose that all files are generated in the same package than the library.
GEN_LIBS=$(query 'let gendeps = kind(rule, deps(kind(java_*, deps('"$TARGET"')), 1))
                              - kind("(java_.*|filegroup|.*_binary|genproto|bind)", deps('"$TARGET"'))
                              - deps(//tools/...)
                  in rdeps('"$TARGET"', set($gendeps), 1) - set($gendeps)' \
    | sed 's|^//\(.*\):\(.*\)|bazel-bin/\1/lib\2.jar:bazel-genfiles/\1|')

# Hack for genproto
PROTOBUFS=$(query 'kind(genproto, deps('"$TARGET"'))' \
    | sed 's|^//\(.*\):\(.*\)$|bazel-bin/\1/lib\2.jar:bazel-bin/\1/lib\2.jar.proto_output/|')
LIB_PATHS="${JAR_FILES} ${PROTOBUFS} ${GEN_LIBS}"