blob: 327e2b2c7a5f655c4a99b78a3875d16e65315053 [file] [log] [blame]
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +00001#!/bin/bash
2# Copyright 2015 Google Inc. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Gets all libraries needed for IDE support of a Bazel workspace
17
18set -eu
19
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000020# Build everything
21bazel build ${TARGET} >&2 || exit $?
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000022
23function query() {
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000024 bazel query "$@"
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000025}
26
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000027# Find the bazel-workspaceName link
28EXECUTION_ROOT_PATH=$(bazel info execution_root)
29WORKSPACE_PATH=$(bazel info workspace)
30for i in ${WORKSPACE_PATH}/bazel-*; do
31 if [[ "$(readlink $i)" == "${EXECUTION_ROOT_PATH}" ]]; then
32 EXECUTION_ROOT=$(basename $i)
33 fi
34done
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000035
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000036# Do a bazel query and replace the result by paths relative to the workspace.
37# @repo//package:target will be replaced by
38# bazel-%workspaceName%/external/repo/package/target
39# //package:target will be replaced by package/target
40function query_to_path() {
41 query "$1" | sed 's|:|/|' \
42 | sed 's|@\(.*\)///\{0,1\}|'"${EXECUTION_ROOT}"'/external/\1/|' \
43 | sed 's|^//||' | sort -u
44}
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000045
46# Now for java each targets, find all sources and all jars
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000047PATHS=$(query_to_path 'filter("\.java$",
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000048 deps(kind("(java_binary|java_library|java_test|java_plugin)",
49 deps('"$TARGET"')))
50 except deps(//tools/...))')
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000051# Java Files:
52JAVA_PATHS=$(echo "$PATHS" | sed 's_\(/java\(tests\)\{0,1\}\)/.*$_\1_' | sort -u)
53
54# Java plugins
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000055PLUGIN_PATHS=$(query_to_path 'filter("\.jar$",
56 deps(kind(java_import,
57 deps(kind(java_plugin,
58 deps('"$TARGET"')))))
59 except deps(//tools/...))')
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000060# Jar Files:
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000061JAR_FILES=$(query_to_path 'filter("\.jar$", deps(kind(java_import, deps('"$TARGET"')))
62 except deps(//tools/...))')
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000063
64# Generated files are direct dependencies of java rules that are not java rules,
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000065# filegroup, binaries or external dependencies.
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000066# We also handle genproto separately it is output in bazel-genfiles not in
67# bazel-bin.
68# We suppose that all files are generated in the same package than the library.
69GEN_LIBS=$(query 'let gendeps = kind(rule, deps(kind(java_*, deps('"$TARGET"')), 1))
Damien Martin-Guillerez9e51f222015-04-21 17:03:33 +000070 - kind("(java_.*|filegroup|.*_binary|genproto|bind)", deps('"$TARGET"'))
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000071 - deps(//tools/...)
72 in rdeps('"$TARGET"', set($gendeps), 1) - set($gendeps)' \
73 | sed 's|^//\(.*\):\(.*\)|bazel-bin/\1/lib\2.jar:bazel-genfiles/\1|')
74
75# Hack for genproto
Damien Martin-Guillerezaefa3d02015-04-22 13:09:53 +000076PROTOBUFS=$(query 'kind(genproto, deps('"$TARGET"'))' \
Damien Martin-Guillerezf9971e62015-04-20 13:51:07 +000077 | sed 's|^//\(.*\):\(.*\)$|bazel-bin/\1/lib\2.jar:bazel-bin/\1/lib\2.jar.proto_output/|')
78LIB_PATHS="${JAR_FILES} ${PROTOBUFS} ${GEN_LIBS}"