blob: 84f76a2c46f2c259bab399c478151d89f1af34a3 [file] [log] [blame]
Janak Ramakrishnanee85e552015-04-18 20:14:04 +00001#!/bin/bash
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00002# Copyright 2015 The Bazel Authors. All rights reserved.
Janak Ramakrishnanee85e552015-04-18 20:14:04 +00003#
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
18set -eu
19
20cd $(dirname "$0")
21cd ..
22
23function query() {
24 ./output/bazel query "$@"
25}
26
27# Compile bazel
Lukacs Berki6462d872016-01-19 11:47:11 +000028[ -f "output/bazel" ] || ./compile.sh compile >&2 || exit $?
Janak Ramakrishnanee85e552015-04-18 20:14:04 +000029
Lukacs Berkif2b1fd62015-07-02 10:08:01 +000030# 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 Berkiad67cc42015-08-25 08:54:45 +000033./output/bazel build -- //src/{main,java_tools,test/{java,cpp}}/... //third_party/... \
Lukacs Berki51c16402015-09-23 12:26:16 +000034 -//third_party/ijar/test/... -//third_party/java/j2objc/... >&2 \
Kristina Chodorow30524462015-07-27 13:52:18 +000035 || exit $?
Janak Ramakrishnanee85e552015-04-18 20:14:04 +000036
Janak Ramakrishnanee85e552015-04-18 20:14:04 +000037# Source roots.
38JAVA_PATHS="$(find src -name "*.java" | sed "s|/com/google/.*$||" | sort -u)"
Damien Martin-Guillerez63f27032015-05-26 13:14:57 +000039if [ "$(uname -s | tr 'A-Z' 'a-z')" != "darwin" ]; then
40 JAVA_PATHS="$(echo "${JAVA_PATHS}" | fgrep -v "/objc_tools/")"
41fi
Janak Ramakrishnanee85e552015-04-18 20:14:04 +000042
43THIRD_PARTY_JAR_PATHS="$(find third_party -name "*.jar" | sort -u)"
44
Janak025b1892015-07-13 15:15:31 +000045# Android-SDK-dependent files may need to be excluded from compilation.
46ANDROID_IMPORTING_FILES="$(grep "^import android\." -R -l --include "*.java" src | sort)"
Janak Ramakrishnanee85e552015-04-18 20:14:04 +000047
48# All other generated libraries.
49readonly package_list=$(find src -name "BUILD" | sed "s|/BUILD||" | sed "s|^|//|")
50# Returns the package of file $1
51function 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
61function 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
68function 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
77function get_containing_library() {
78 get_consuming_target $1 | sed 's|:|/lib|' | sed 's|^//|bazel-bin/|' | sed 's|$|.jar|'
79}
80
81function 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}"
Janak025b1892015-07-13 15:15:31 +000086 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 Ramakrishnanee85e552015-04-18 20:14:04 +000092 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.
97GENERATED_PATHS="$(collect_generated_paths)"