Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 1 | #!/bin/bash |
Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 2 | # Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 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 in Bazel |
| 17 | |
| 18 | set -eu |
| 19 | |
| 20 | cd $(dirname "$0") |
| 21 | cd .. |
| 22 | |
| 23 | function query() { |
| 24 | ./output/bazel query "$@" |
| 25 | } |
| 26 | |
| 27 | # Compile bazel |
Lukacs Berki | 6462d87 | 2016-01-19 11:47:11 +0000 | [diff] [blame] | 28 | [ -f "output/bazel" ] || ./compile.sh compile >&2 || exit $? |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 29 | |
Lukacs Berki | f2b1fd6 | 2015-07-02 10:08:01 +0000 | [diff] [blame] | 30 | # Build almost everything. |
| 31 | # //third_party/ijar/test/... is disabled due to #273. |
| 32 | # xcode and android tools do not work out of the box. |
Lukacs Berki | ad67cc4 | 2015-08-25 08:54:45 +0000 | [diff] [blame] | 33 | ./output/bazel build -- //src/{main,java_tools,test/{java,cpp}}/... //third_party/... \ |
Lukacs Berki | 51c1640 | 2015-09-23 12:26:16 +0000 | [diff] [blame] | 34 | -//third_party/ijar/test/... -//third_party/java/j2objc/... >&2 \ |
Kristina Chodorow | 3052446 | 2015-07-27 13:52:18 +0000 | [diff] [blame] | 35 | || exit $? |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 36 | |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 37 | # Source roots. |
| 38 | JAVA_PATHS="$(find src -name "*.java" | sed "s|/com/google/.*$||" | sort -u)" |
Damien Martin-Guillerez | 63f2703 | 2015-05-26 13:14:57 +0000 | [diff] [blame] | 39 | if [ "$(uname -s | tr 'A-Z' 'a-z')" != "darwin" ]; then |
| 40 | JAVA_PATHS="$(echo "${JAVA_PATHS}" | fgrep -v "/objc_tools/")" |
| 41 | fi |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 42 | |
| 43 | THIRD_PARTY_JAR_PATHS="$(find third_party -name "*.jar" | sort -u)" |
| 44 | |
Janak | 025b189 | 2015-07-13 15:15:31 +0000 | [diff] [blame] | 45 | # Android-SDK-dependent files may need to be excluded from compilation. |
| 46 | ANDROID_IMPORTING_FILES="$(grep "^import android\." -R -l --include "*.java" src | sort)" |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 47 | |
| 48 | # All other generated libraries. |
| 49 | readonly package_list=$(find src -name "BUILD" | sed "s|/BUILD||" | sed "s|^|//|") |
| 50 | # Returns the package of file $1 |
| 51 | function get_package_of() { |
| 52 | # look for the longest matching package |
| 53 | for i in ${package_list}; do |
| 54 | if [[ "$1" =~ ^$i ]]; then # we got a match |
| 55 | echo $(echo -n $i | wc -c | xargs echo) $i |
| 56 | fi |
| 57 | done | sort -r -n | head -1 | cut -d " " -f 2 |
| 58 | } |
| 59 | |
| 60 | # returns the target corresponding to file $1 |
| 61 | function get_target_of() { |
| 62 | local package=$(get_package_of $1) |
| 63 | local file=$(echo $1 | sed "s|^${package}/||g") |
| 64 | echo "${package}:${file}" |
| 65 | } |
| 66 | |
| 67 | # Returns the target that consume file $1 |
| 68 | function get_consuming_target() { |
| 69 | # Here to the god of bazel, I should probably offer one or two memory chips for that |
| 70 | local target=$(get_target_of $1) |
| 71 | local generating_target=$(query "deps(${target}, 1) - ${target}") |
| 72 | local java_library=$(query "rdeps(//src/..., ${generating_target}, 1) - ${generating_target}") |
| 73 | echo "${java_library}" |
| 74 | } |
| 75 | |
| 76 | # Returns the library that contains the generated file $1 |
| 77 | function get_containing_library() { |
| 78 | get_consuming_target $1 | sed 's|:|/lib|' | sed 's|^//|bazel-bin/|' | sed 's|$|.jar|' |
| 79 | } |
| 80 | |
| 81 | function collect_generated_paths() { |
| 82 | # uniq to avoid doing blaze query on duplicates. |
| 83 | for path in $(find bazel-genfiles/ -name "*.java" | sed 's|/\{0,1\}bazel-genfiles/\{1,2\}|//|' | uniq); do |
| 84 | source_path=$(echo ${path} | sed 's|//|bazel-genfiles/|' | sed 's|/com/.*$||') |
| 85 | echo "$(get_containing_library ${path}):${source_path}" |
Janak | 025b189 | 2015-07-13 15:15:31 +0000 | [diff] [blame] | 86 | done && |
| 87 | # Add in "external" jars which don't have source paths. |
| 88 | for jardir in "jar/" ""; do |
| 89 | for path in $(find bazel-genfiles/${jardir}_ijar -name "*.jar" | sed 's|^/+||' | uniq); do |
| 90 | echo "${path}:" |
| 91 | done |
Janak Ramakrishnan | ee85e55 | 2015-04-18 20:14:04 +0000 | [diff] [blame] | 92 | done | sort -u |
| 93 | } |
| 94 | |
| 95 | # GENERATED_PATHS stores pairs of jar:source_path as a list of strings, with |
| 96 | # each pair internally delimited by a colon. Use ${string//:/ } to split one. |
| 97 | GENERATED_PATHS="$(collect_generated_paths)" |