Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 1 | #!/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 | |
| 18 | set -eu |
| 19 | |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 20 | # Build everything |
| 21 | bazel build ${TARGET} >&2 || exit $? |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 22 | |
| 23 | function query() { |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 24 | bazel query "$@" |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 27 | # Find the bazel-workspaceName link |
| 28 | EXECUTION_ROOT_PATH=$(bazel info execution_root) |
| 29 | WORKSPACE_PATH=$(bazel info workspace) |
| 30 | for i in ${WORKSPACE_PATH}/bazel-*; do |
| 31 | if [[ "$(readlink $i)" == "${EXECUTION_ROOT_PATH}" ]]; then |
| 32 | EXECUTION_ROOT=$(basename $i) |
| 33 | fi |
| 34 | done |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 35 | |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 36 | # 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 |
| 40 | function 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-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 45 | |
| 46 | # Now for java each targets, find all sources and all jars |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 47 | PATHS=$(query_to_path 'filter("\.java$", |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 48 | deps(kind("(java_binary|java_library|java_test|java_plugin)", |
| 49 | deps('"$TARGET"'))) |
| 50 | except deps(//tools/...))') |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 51 | # Java Files: |
| 52 | JAVA_PATHS=$(echo "$PATHS" | sed 's_\(/java\(tests\)\{0,1\}\)/.*$_\1_' | sort -u) |
| 53 | |
| 54 | # Java plugins |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 55 | PLUGIN_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-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 60 | # Jar Files: |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 61 | JAR_FILES=$(query_to_path 'filter("\.jar$", deps(kind(java_import, deps('"$TARGET"'))) |
| 62 | except deps(//tools/...))') |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 63 | |
| 64 | # Generated files are direct dependencies of java rules that are not java rules, |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 65 | # filegroup, binaries or external dependencies. |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 66 | # 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. |
| 69 | GEN_LIBS=$(query 'let gendeps = kind(rule, deps(kind(java_*, deps('"$TARGET"')), 1)) |
Damien Martin-Guillerez | 9e51f22 | 2015-04-21 17:03:33 +0000 | [diff] [blame] | 70 | - kind("(java_.*|filegroup|.*_binary|genproto|bind)", deps('"$TARGET"')) |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 71 | - 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-Guillerez | aefa3d0 | 2015-04-22 13:09:53 +0000 | [diff] [blame] | 76 | PROTOBUFS=$(query 'kind(genproto, deps('"$TARGET"'))' \ |
Damien Martin-Guillerez | f9971e6 | 2015-04-20 13:51:07 +0000 | [diff] [blame] | 77 | | sed 's|^//\(.*\):\(.*\)$|bazel-bin/\1/lib\2.jar:bazel-bin/\1/lib\2.jar.proto_output/|') |
| 78 | LIB_PATHS="${JAR_FILES} ${PROTOBUFS} ${GEN_LIBS}" |