aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt
blob: 18f75e2eeded852ae258eaeda07c8ed90078dfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash --posix
# Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script was generated from java_stub_template.txt.  Please
# don't edit it directly.
#
# If present, these flags should either be at the beginning of the command
# line, or they should be wrapped in a --wrapper_script_flag=FLAG argument.
#
# --debug               Launch the JVM in remote debugging mode listening
# --debug=<port>        to the specified port or the port set in the
#                       DEFAULT_JVM_DEBUG_PORT environment variable (e.g.
#                       'export DEFAULT_JVM_DEBUG_PORT=8000') or else the
#                       default port of 5005.  The JVM starts suspended
#                       unless the DEFAULT_JVM_DEBUG_SUSPEND environment
#                       variable is set to 'n'.
# --main_advice=<class> Run an alternate main class with the usual main
#                       program and arguments appended as arguments.
# --main_advice_classpath=<classpath>
#                       Prepend additional class path entries.
# --jvm_flag=<flag>     Pass <flag> to the "java" command itself.
#                       <flag> may contain spaces. Can be used multiple times.
# --jvm_flags=<flags>   Pass space-separated flags to the "java" command
#                       itself. Can be used multiple times.
# --singlejar           Start the program from the packed-up deployment
#                       jar rather than from the classpath.
# --print_javabin       Print the location of java executable binary and exit.
#
# The remainder of the command line is passed to the program.

# Make it easy to insert 'set -x' or similar commands when debugging problems with this script.
eval "$JAVA_STUB_DEBUG"

# Prevent problems where the caller has exported CLASSPATH, causing our
# computed value to be copied into the environment and double-counted
# against the argv limit.
unset CLASSPATH

JVM_FLAGS_CMDLINE=()

# Processes an argument for the wrapper. Returns 0 if the given argument
# was recognized as an argument for this wrapper, and 1 if it was not.
function process_wrapper_argument() {
  case "$1" in
    --debug) JVM_DEBUG_PORT="${DEFAULT_JVM_DEBUG_PORT:-5005}" ;;
    --debug=*) JVM_DEBUG_PORT="${1#--debug=}" ;;
    --main_advice=*) MAIN_ADVICE="${1#--main_advice=}" ;;
    --main_advice_classpath=*) MAIN_ADVICE_CLASSPATH="${1#--main_advice_classpath=}" ;;
    --jvm_flag=*) JVM_FLAGS_CMDLINE+=( "${1#--jvm_flag=}" ) ;;
    --jvm_flags=*) JVM_FLAGS_CMDLINE+=( ${1#--jvm_flags=} ) ;;
    --singlejar) SINGLEJAR=1 ;;
    --print_javabin) PRINT_JAVABIN=1 ;;
    *)
      return 1 ;;
  esac
  return 0
}

die() {
  printf "%s: $1\n" "$0" "${@:2}" >&2
  exit 1
}

# Parse arguments sequentially until the first unrecognized arg is encountered.
# Scan the remaining args for --wrapper_script_flag=X options and process them.
ARGS=()
for ARG in "$@"; do
  if [[ "$ARG" == --wrapper_script_flag=* ]]; then
    process_wrapper_argument "${ARG#--wrapper_script_flag=}" \
      || die "invalid wrapper argument '%s'" "$ARG"
  elif [[ "${#ARGS}" > 0 ]] || ! process_wrapper_argument "$ARG"; then
    ARGS+=( "$ARG" )
  fi
done

# Find our runfiles tree.  We need this to construct the classpath
# (unless --singlejar was passed).
#
# Call this program X.  X was generated by a java_binary or java_test rule.
# X may be invoked in many ways:
#   1a) directly by a user, with $0 in the output tree
#   1b) via 'bazel run' (similar to case 1a)
#   2) directly by a user, with $0 in X's runfiles tree
#   3) by another program Y which has a data dependency on X, with $0 in Y's runfiles tree
#   4) via 'bazel test'
#   5) by a genrule cmd, with $0 in the output tree
#   6) case 3 in the context of a genrule
#
# For case 1, $0 will be a regular file, and the runfiles tree will be
# at $0.runfiles.
# For case 2 or 3, $0 will be a symlink to the file seen in case 1.
# For case 4, $JAVA_RUNFILES and $TEST_SRCDIR should already be set.
# Case 5 is handled like case 1.
# Case 6 is handled like case 3.

case "$0" in
  /*) self="$0" ;;
  *)  self="$PWD/$0" ;;
esac

if [[ "$SINGLEJAR" != 1 || "%needs_runfiles%" == 1 ]]; then
  if [[ -z "$JAVA_RUNFILES" ]]; then
    while true; do
      if [[ -e "$self.runfiles" ]]; then
        JAVA_RUNFILES="$self.runfiles"
        break
      fi
      if [[ $self == *.runfiles/* ]]; then
        JAVA_RUNFILES="${self%.runfiles/*}.runfiles"
        # don't break; this value is only a last resort for case 6b
      fi
      if [[ ! -L "$self" ]]; then
        break
      fi
      readlink="$(readlink "$self")"
      if [[ "$readlink" = /* ]]; then
        self="$readlink"
      else
        # resolve relative symlink
        self="${self%/*}/$readlink"
      fi
    done
    if [[ -n "$JAVA_RUNFILES" ]]; then
      export TEST_SRCDIR=${TEST_SRCDIR:-$JAVA_RUNFILES}
    elif [[ -f "${self}_deploy.jar" && "%needs_runfiles%" == 0 ]]; then
      SINGLEJAR=1;
    else
      die 'Cannot locate runfiles directory. (Set $JAVA_RUNFILES to inhibit searching.)'
    fi
  fi
fi

# Set JAVABIN to the path to the JVM launcher.
%javabin%

if [[ "$PRINT_JAVABIN" == 1 || "%java_start_class%" == "--print_javabin" ]]; then
  echo -n "$JAVABIN"
  exit 0
fi

if [[ "$SINGLEJAR" == 1 ]]; then
  CLASSPATH="${self}_deploy.jar"
  # Check for the deploy jar now.  If it doesn't exist, we can print a
  # more helpful error message than the JVM.
  [[ -r "$CLASSPATH" ]] \
    || die "Option --singlejar was passed, but %s does not exist.\n  (You may need to build it explicitly.)" "$CLASSPATH"
else
  # Create the shortest classpath we can, by making it relative if possible.
  RUNPATH="${JAVA_RUNFILES}/%workspace_prefix%"
  RUNPATH="${RUNPATH#$PWD/}"
  CLASSPATH=%classpath%
fi

if [[ -n "$JVM_DEBUG_PORT" ]]; then
  JVM_DEBUG_SUSPEND=${DEFAULT_JVM_DEBUG_SUSPEND:-"y"}
  JVM_DEBUG_FLAGS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=${JVM_DEBUG_SUSPEND},address=${JVM_DEBUG_PORT}"
fi

if [[ -n "$MAIN_ADVICE_CLASSPATH" ]]; then
  CLASSPATH="${MAIN_ADVICE_CLASSPATH}:${CLASSPATH}"
fi

# Check if TEST_TMPDIR is available to use for scratch.
if [[ -n "$TEST_TMPDIR" && -d "$TEST_TMPDIR" ]]; then
  JVM_FLAGS+=" -Djava.io.tmpdir=$TEST_TMPDIR"
fi

ARGS=(
  ${JVM_DEBUG_FLAGS}
  ${JVM_FLAGS}
  %jvm_flags%
  "${JVM_FLAGS_CMDLINE[@]}"
  ${MAIN_ADVICE}
  %java_start_class%
  "${ARGS[@]}")

# Linux per-arg limit MAX_ARG_STRLEN == 128k!
if (("${#CLASSPATH}" > 120000)); then
  set +o posix  # Enable process substitution.
  exec $JAVABIN -classpath @<(echo $CLASSPATH) "${ARGS[@]}"
else
  exec $JAVABIN -classpath $CLASSPATH "${ARGS[@]}"
fi