1#!/bin/bash
2
3# Copyright 2015 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Wrapper for extracting compilation information from javac invocations.  It
18# requires the same environment variables as the javac extractor (see
19# AbstractJavacWrapper.java).  In particular, it needs KYTHE_ROOT_DIRECTORY and
20# KYTHE_OUTPUT_DIRECTORY set to understand where the root of the compiled source
21# repository is and where to put the resulting .kindex files, respectively.
22#
23# This script assumes a usable java binary is on $PATH.
24#
25# This script is meant as a replacement for $JAVA_HOME/bin/javac.  It assumes
26# the true javac binary is in the same directory as itself and named
27# "javac.real".  The location of the real javac binary can be configured with
28# the REAL_JAVAC environment variable.  The default path for the javac extractor
29# jar is /opt/kythe/javac_extractor.jar but can be set with the
30# JAVAC_EXTRACTOR_JAR environment variable.
31#
32# If used in a Docker environment where KYTHE_ROOT_DIRECTORY and
33# KYTHE_OUTPUT_DIRECTORY are volumes, it can be useful to set the DOCKER_CLEANUP
34# environment variable so that files modified/created in either volume have
35# their owner/group set to the volume's root directory's owner/group.
36#
37# Other environment variables that may be passed to this script include:
38#   KYTHE_EXTRACT_ONLY: if set, suppress the call to javac after extraction
39#   TMPDIR: override the location of extraction logs and other temporary output
40
41export TMPDIR="${TMPDIR:-/tmp}"
42
43if [[ -z "$REAL_JAVAC" ]]; then
44  readonly REAL_JAVAC="$(dirname "$(readlink -e "$0")")/javac.real"
45fi
46if [[ -z "$JAVAC_EXTRACTOR_JAR" ]]; then
47  readonly JAVAC_EXTRACTOR_JAR="/opt/kythe/extractors/javac_extractor.jar"
48fi
49
50fix_permissions() {
51  local dir="${1:?missing path}"
52  chown -R $(stat "$dir" -c %u:%g) "$dir"
53}
54cleanup() {
55  fix_permissions "$KYTHE_ROOT_DIRECTORY"
56  fix_permissions "$KYTHE_OUTPUT_DIRECTORY"
57}
58if [[ -n "$DOCKER_CLEANUP" ]]; then
59  trap cleanup EXIT ERR INT
60fi
61
62java -Xbootclasspath/p:"$JAVAC_EXTRACTOR_JAR" \
63     -jar "$JAVAC_EXTRACTOR_JAR" \
64     "$@" >>"$TMPDIR"/javac-extractor.out 2>> "$TMPDIR"/javac-extractor.err
65if [[ -z "$KYTHE_EXTRACT_ONLY" ]]; then
66  "$REAL_JAVAC" "$@"
67fi
68