Add Kythe to prebuilts/tools

Kythe in combination with Bazel will allow us to have
cross-references and jump-to-definition support on our
sources.

See README.google for details on how this was integrated.

Change-Id: I28afb766bdb806b0bedef8a8b7d11fc98e429c5b
diff --git a/linux-x86_64/kythe/studio/build_studio_kythe.sh b/linux-x86_64/kythe/studio/build_studio_kythe.sh
new file mode 100755
index 0000000..a328cf4
--- /dev/null
+++ b/linux-x86_64/kythe/studio/build_studio_kythe.sh
@@ -0,0 +1,49 @@
+#!/bin/bash -e
+
+# Bazel build target for running kythe extractor as an extra_action
+# to create kythe index files as a side effect of running the build.
+EAL=//prebuilts/tools/linux-x86_64/kythe/extractors:extract_kindex
+
+# Path to the kyth binaries.
+KYTHE_ROOT="$(readlink -f prebuilts/tools/linux-x86_64/kythe)"
+
+# Get the output path for the kythe artifacts.
+OUT="$1"
+if [ -z "${OUT}" ]; then
+  echo Usage: $0 \<out_dir\>
+  echo  e.g. $0 $HOME/studio_kythe
+  echo
+  echo $0 must be launched from the root of the studio branch.
+  exit 1
+fi
+OUT_ENTRIES="${OUT}/entries"
+mkdir -p "${OUT_ENTRIES}"
+
+#TODO: read from file
+TARGETS="//prebuilts/studio/... \
+  //prebuilts/tools/common/... \
+  //tools/adt/idea/... \
+  //tools/analytics-library/... \
+  //tools/base/... \
+  //tools/data-binding/... \
+  //tools/idea/... \
+  //tools/sherpa/..."
+
+# Build all targets and run the kythe extractor via extra_actions.
+bazel build \
+  --experimental_action_listener=${EAL} -- ${TARGETS}
+
+# Find all generated kythe index files.
+KINDEXES=$(find bazel-out/local-fastbuild/extra_actions/ \
+  -name *.kindex -exec realpath {} \;)
+
+# For each kythe index file run the java index to generate kythe
+# entries.
+cd "${OUT_ENTRIES}"
+for KINDEX in ${KINDEXES}; do
+  ENTRIES="$(basename "${KINDEX}").entries"
+  if [ ! -f "${ENTRIES}" ]; then
+    java -jar "${KYTHE_ROOT}/indexers/java_indexer.jar" \
+      "${KINDEX}" > "${ENTRIES}"
+  fi
+done;
diff --git a/linux-x86_64/kythe/studio/serve_studio_kythe.sh b/linux-x86_64/kythe/studio/serve_studio_kythe.sh
new file mode 100755
index 0000000..798802a
--- /dev/null
+++ b/linux-x86_64/kythe/studio/serve_studio_kythe.sh
@@ -0,0 +1,53 @@
+#!/bin/bash -e
+# Takes kythe indexer output and processes it into servable format
+# And brings up a webserver (port 8080) to inspect the data.
+#
+# This is not used for normal production runs as processing on a single
+# machine is very slow (our index output is currently around 20GB).
+#
+# Instead the processing is done with a MapReduce job.
+
+# Path to the kyth binaries.
+KYTHE_ROOT="$(readlink -f prebuilts/tools/linux-x86_64/kythe)"
+
+# Get the output path for the kythe artifacts.
+OUT="$1"
+OUT_ENTRIES="${OUT}/entries"
+OUT_GRAPHSTORE="${OUT}/graphstore"
+OUT_SERVING="${OUT}/serving"
+if [ -z "${OUT}" ]; then
+  echo Usage: $0 \<out_dir\>
+  echo  e.g. $0 $HOME/studio_kythe
+  echo
+  echo $0 must be launched from the root of the studio branch.
+  exit 1
+fi
+
+# if the graphstore has not been created, create it from the
+# entries created using "build_studio_kythe.sh"
+if [ ! -d "${OUT_GRAPHSTORE}" ]; then
+
+  # delete all empty files as the write_entries tool is not happy with
+  # empty files
+  find "${OUT_ENTRIES}" -empty -type f -delete
+  ENTRIES=$(find "${OUT_ENTRIES}" -name *.entries \
+    -exec realpath {} \;)
+
+  for ENTRY in ${ENTRIES}; do
+    "${KYTHE_ROOT}/tools/write_entries" \
+      --graphstore "leveldb:${OUT_GRAPHSTORE}" < "${ENTRY}"
+  done;
+fi
+
+# If no serving table exists yet, create it from the graphstore.
+if [ ! -d "${OUT_SERVING}" ]; then
+  "${KYTHE_ROOT}/tools/write_tables" \
+    --graphstore "${OUT_GRAPHSTORE}" \
+    --out "${OUT_SERVING}"
+fi
+
+# Start the kythe webserver for the serving table.
+"${KYTHE_ROOT}/tools/http_server" \
+  --public_resources "${KYTHE_ROOT}/web/ui" \
+  --listen localhost:8080 \
+  --serving_table "${OUT_SERVING}"
\ No newline at end of file