1#!/bin/bash -e
2
3# Bazel build target for running kythe extractor as an extra_action
4# to create kythe index files as a side effect of running the build.
5EAL=//prebuilts/tools/linux-x86_64/kythe/extractors:extract_kindex
6
7# Path to the kyth binaries.
8KYTHE_ROOT="$(readlink -f prebuilts/tools/linux-x86_64/kythe)"
9
10# Get the output path for the kythe artifacts.
11OUT="$1"
12if [ -z "${OUT}" ]; then
13  echo Usage: $0 \<out_dir\> [gcs_bucket]
14  echo  e.g. $0 $HOME/studio_kythe
15  echo
16  echo $0 must be launched from the root of the studio branch.
17  exit 1
18fi
19
20if [ -z "${JDK_18_x64}" ]; then
21  echo $0 requires the JDK_18_x64 env variable to be defined.
22  exit 1
23fi
24
25OUT_ENTRIES="${OUT}/entries"
26mkdir -p "${OUT_ENTRIES}"
27
28TARGETS="$(cat tools/base/bazel/targets)"
29
30# ensure no stale .kindex file exist
31tools/base/bazel/bazel clean
32
33# Build all targets and run the kythe extractor via extra_actions.
34# ignore failures in the build as Kythe will do statistical analysis
35# on build and produce indexes for builds with 95% coverage and up.
36tools/base/bazel/bazel build --keep_going \
37  --experimental_action_listener=${EAL} -- ${TARGETS} || true
38
39# Find all generated kythe index files.
40KINDEXES=$(find bazel-out/local-fastbuild/extra_actions/ \
41  -name *.kindex -exec realpath {} \;)
42
43# For each kythe index file run the java index to generate kythe
44# entries.
45# Ignore failures from analysis (|| true) as Kythe will do statistical analysis
46# on the entries and produce indexes for builds with 95% coverage and up.
47cd "${OUT_ENTRIES}"
48for KINDEX in ${KINDEXES}; do
49  ENTRIES="$(basename "${KINDEX}").entries"
50  if [ ! -f "${ENTRIES}" ]; then
51    "${JDK_18_x64}/bin/java" -jar \
52    "${KYTHE_ROOT}/indexers/java_indexer.jar" \
53      "${KINDEX}" > "${ENTRIES}" || true
54  fi
55done;
56
57GSBUCKET="$2"
58# allow buildbot to specify gsutil script to use.
59GSUTIL="${GSUTIL:-gsutil}"
60
61if [ -n "${GSBUCKET}" ]; then
62  TIMESTAMP=$(date +'%s')
63  "${GSUTIL}" -m cp "${OUT_ENTRIES}/*" "${GSBUCKET}/${TIMESTAMP}/"
64  LATEST_FILE="$(mktemp)"
65  echo "str_var <name:'kythe_index_version' value:'${TIMESTAMP}'>">"${LATEST_FILE}"
66  "${GSUTIL}" cp "${LATEST_FILE}" "${GSBUCKET}/latest.txt"
67  rm "${LATEST_FILE}"
68fi