xref: /aosp_15_r20/external/grpc-grpc-java/examples/gradlew (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker#!/bin/sh
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard Worker#
4*e07d83d3SAndroid Build Coastguard Worker# Copyright © 2015-2021 the original authors.
5*e07d83d3SAndroid Build Coastguard Worker#
6*e07d83d3SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
7*e07d83d3SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
8*e07d83d3SAndroid Build Coastguard Worker# You may obtain a copy of the License at
9*e07d83d3SAndroid Build Coastguard Worker#
10*e07d83d3SAndroid Build Coastguard Worker#      https://www.apache.org/licenses/LICENSE-2.0
11*e07d83d3SAndroid Build Coastguard Worker#
12*e07d83d3SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
13*e07d83d3SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
14*e07d83d3SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*e07d83d3SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
16*e07d83d3SAndroid Build Coastguard Worker# limitations under the License.
17*e07d83d3SAndroid Build Coastguard Worker#
18*e07d83d3SAndroid Build Coastguard Worker
19*e07d83d3SAndroid Build Coastguard Worker##############################################################################
20*e07d83d3SAndroid Build Coastguard Worker#
21*e07d83d3SAndroid Build Coastguard Worker#   Gradle start up script for POSIX generated by Gradle.
22*e07d83d3SAndroid Build Coastguard Worker#
23*e07d83d3SAndroid Build Coastguard Worker#   Important for running:
24*e07d83d3SAndroid Build Coastguard Worker#
25*e07d83d3SAndroid Build Coastguard Worker#   (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26*e07d83d3SAndroid Build Coastguard Worker#       noncompliant, but you have some other compliant shell such as ksh or
27*e07d83d3SAndroid Build Coastguard Worker#       bash, then to run this script, type that shell name before the whole
28*e07d83d3SAndroid Build Coastguard Worker#       command line, like:
29*e07d83d3SAndroid Build Coastguard Worker#
30*e07d83d3SAndroid Build Coastguard Worker#           ksh Gradle
31*e07d83d3SAndroid Build Coastguard Worker#
32*e07d83d3SAndroid Build Coastguard Worker#       Busybox and similar reduced shells will NOT work, because this script
33*e07d83d3SAndroid Build Coastguard Worker#       requires all of these POSIX shell features:
34*e07d83d3SAndroid Build Coastguard Worker#         * functions;
35*e07d83d3SAndroid Build Coastguard Worker#         * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36*e07d83d3SAndroid Build Coastguard Worker#           «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37*e07d83d3SAndroid Build Coastguard Worker#         * compound commands having a testable exit status, especially «case»;
38*e07d83d3SAndroid Build Coastguard Worker#         * various built-in commands including «command», «set», and «ulimit».
39*e07d83d3SAndroid Build Coastguard Worker#
40*e07d83d3SAndroid Build Coastguard Worker#   Important for patching:
41*e07d83d3SAndroid Build Coastguard Worker#
42*e07d83d3SAndroid Build Coastguard Worker#   (2) This script targets any POSIX shell, so it avoids extensions provided
43*e07d83d3SAndroid Build Coastguard Worker#       by Bash, Ksh, etc; in particular arrays are avoided.
44*e07d83d3SAndroid Build Coastguard Worker#
45*e07d83d3SAndroid Build Coastguard Worker#       The "traditional" practice of packing multiple parameters into a
46*e07d83d3SAndroid Build Coastguard Worker#       space-separated string is a well documented source of bugs and security
47*e07d83d3SAndroid Build Coastguard Worker#       problems, so this is (mostly) avoided, by progressively accumulating
48*e07d83d3SAndroid Build Coastguard Worker#       options in "$@", and eventually passing that to Java.
49*e07d83d3SAndroid Build Coastguard Worker#
50*e07d83d3SAndroid Build Coastguard Worker#       Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51*e07d83d3SAndroid Build Coastguard Worker#       and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52*e07d83d3SAndroid Build Coastguard Worker#       see the in-line comments for details.
53*e07d83d3SAndroid Build Coastguard Worker#
54*e07d83d3SAndroid Build Coastguard Worker#       There are tweaks for specific operating systems such as AIX, CygWin,
55*e07d83d3SAndroid Build Coastguard Worker#       Darwin, MinGW, and NonStop.
56*e07d83d3SAndroid Build Coastguard Worker#
57*e07d83d3SAndroid Build Coastguard Worker#   (3) This script is generated from the Groovy template
58*e07d83d3SAndroid Build Coastguard Worker#       https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59*e07d83d3SAndroid Build Coastguard Worker#       within the Gradle project.
60*e07d83d3SAndroid Build Coastguard Worker#
61*e07d83d3SAndroid Build Coastguard Worker#       You can find Gradle at https://github.com/gradle/gradle/.
62*e07d83d3SAndroid Build Coastguard Worker#
63*e07d83d3SAndroid Build Coastguard Worker##############################################################################
64*e07d83d3SAndroid Build Coastguard Worker
65*e07d83d3SAndroid Build Coastguard Worker# Attempt to set APP_HOME
66*e07d83d3SAndroid Build Coastguard Worker
67*e07d83d3SAndroid Build Coastguard Worker# Resolve links: $0 may be a link
68*e07d83d3SAndroid Build Coastguard Workerapp_path=$0
69*e07d83d3SAndroid Build Coastguard Worker
70*e07d83d3SAndroid Build Coastguard Worker# Need this for daisy-chained symlinks.
71*e07d83d3SAndroid Build Coastguard Workerwhile
72*e07d83d3SAndroid Build Coastguard Worker    APP_HOME=${app_path%"${app_path##*/}"}  # leaves a trailing /; empty if no leading path
73*e07d83d3SAndroid Build Coastguard Worker    [ -h "$app_path" ]
74*e07d83d3SAndroid Build Coastguard Workerdo
75*e07d83d3SAndroid Build Coastguard Worker    ls=$( ls -ld "$app_path" )
76*e07d83d3SAndroid Build Coastguard Worker    link=${ls#*' -> '}
77*e07d83d3SAndroid Build Coastguard Worker    case $link in             #(
78*e07d83d3SAndroid Build Coastguard Worker      /*)   app_path=$link ;; #(
79*e07d83d3SAndroid Build Coastguard Worker      *)    app_path=$APP_HOME$link ;;
80*e07d83d3SAndroid Build Coastguard Worker    esac
81*e07d83d3SAndroid Build Coastguard Workerdone
82*e07d83d3SAndroid Build Coastguard Worker
83*e07d83d3SAndroid Build Coastguard WorkerAPP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84*e07d83d3SAndroid Build Coastguard Worker
85*e07d83d3SAndroid Build Coastguard WorkerAPP_NAME="Gradle"
86*e07d83d3SAndroid Build Coastguard WorkerAPP_BASE_NAME=${0##*/}
87*e07d83d3SAndroid Build Coastguard Worker
88*e07d83d3SAndroid Build Coastguard Worker# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89*e07d83d3SAndroid Build Coastguard WorkerDEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
90*e07d83d3SAndroid Build Coastguard Worker
91*e07d83d3SAndroid Build Coastguard Worker# Use the maximum available, or set MAX_FD != -1 to use that value.
92*e07d83d3SAndroid Build Coastguard WorkerMAX_FD=maximum
93*e07d83d3SAndroid Build Coastguard Worker
94*e07d83d3SAndroid Build Coastguard Workerwarn () {
95*e07d83d3SAndroid Build Coastguard Worker    echo "$*"
96*e07d83d3SAndroid Build Coastguard Worker} >&2
97*e07d83d3SAndroid Build Coastguard Worker
98*e07d83d3SAndroid Build Coastguard Workerdie () {
99*e07d83d3SAndroid Build Coastguard Worker    echo
100*e07d83d3SAndroid Build Coastguard Worker    echo "$*"
101*e07d83d3SAndroid Build Coastguard Worker    echo
102*e07d83d3SAndroid Build Coastguard Worker    exit 1
103*e07d83d3SAndroid Build Coastguard Worker} >&2
104*e07d83d3SAndroid Build Coastguard Worker
105*e07d83d3SAndroid Build Coastguard Worker# OS specific support (must be 'true' or 'false').
106*e07d83d3SAndroid Build Coastguard Workercygwin=false
107*e07d83d3SAndroid Build Coastguard Workermsys=false
108*e07d83d3SAndroid Build Coastguard Workerdarwin=false
109*e07d83d3SAndroid Build Coastguard Workernonstop=false
110*e07d83d3SAndroid Build Coastguard Workercase "$( uname )" in                #(
111*e07d83d3SAndroid Build Coastguard Worker  CYGWIN* )         cygwin=true  ;; #(
112*e07d83d3SAndroid Build Coastguard Worker  Darwin* )         darwin=true  ;; #(
113*e07d83d3SAndroid Build Coastguard Worker  MSYS* | MINGW* )  msys=true    ;; #(
114*e07d83d3SAndroid Build Coastguard Worker  NONSTOP* )        nonstop=true ;;
115*e07d83d3SAndroid Build Coastguard Workeresac
116*e07d83d3SAndroid Build Coastguard Worker
117*e07d83d3SAndroid Build Coastguard WorkerCLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
118*e07d83d3SAndroid Build Coastguard Worker
119*e07d83d3SAndroid Build Coastguard Worker
120*e07d83d3SAndroid Build Coastguard Worker# Determine the Java command to use to start the JVM.
121*e07d83d3SAndroid Build Coastguard Workerif [ -n "$JAVA_HOME" ] ; then
122*e07d83d3SAndroid Build Coastguard Worker    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123*e07d83d3SAndroid Build Coastguard Worker        # IBM's JDK on AIX uses strange locations for the executables
124*e07d83d3SAndroid Build Coastguard Worker        JAVACMD=$JAVA_HOME/jre/sh/java
125*e07d83d3SAndroid Build Coastguard Worker    else
126*e07d83d3SAndroid Build Coastguard Worker        JAVACMD=$JAVA_HOME/bin/java
127*e07d83d3SAndroid Build Coastguard Worker    fi
128*e07d83d3SAndroid Build Coastguard Worker    if [ ! -x "$JAVACMD" ] ; then
129*e07d83d3SAndroid Build Coastguard Worker        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130*e07d83d3SAndroid Build Coastguard Worker
131*e07d83d3SAndroid Build Coastguard WorkerPlease set the JAVA_HOME variable in your environment to match the
132*e07d83d3SAndroid Build Coastguard Workerlocation of your Java installation."
133*e07d83d3SAndroid Build Coastguard Worker    fi
134*e07d83d3SAndroid Build Coastguard Workerelse
135*e07d83d3SAndroid Build Coastguard Worker    JAVACMD=java
136*e07d83d3SAndroid Build Coastguard Worker    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137*e07d83d3SAndroid Build Coastguard Worker
138*e07d83d3SAndroid Build Coastguard WorkerPlease set the JAVA_HOME variable in your environment to match the
139*e07d83d3SAndroid Build Coastguard Workerlocation of your Java installation."
140*e07d83d3SAndroid Build Coastguard Workerfi
141*e07d83d3SAndroid Build Coastguard Worker
142*e07d83d3SAndroid Build Coastguard Worker# Increase the maximum file descriptors if we can.
143*e07d83d3SAndroid Build Coastguard Workerif ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144*e07d83d3SAndroid Build Coastguard Worker    case $MAX_FD in #(
145*e07d83d3SAndroid Build Coastguard Worker      max*)
146*e07d83d3SAndroid Build Coastguard Worker        MAX_FD=$( ulimit -H -n ) ||
147*e07d83d3SAndroid Build Coastguard Worker            warn "Could not query maximum file descriptor limit"
148*e07d83d3SAndroid Build Coastguard Worker    esac
149*e07d83d3SAndroid Build Coastguard Worker    case $MAX_FD in  #(
150*e07d83d3SAndroid Build Coastguard Worker      '' | soft) :;; #(
151*e07d83d3SAndroid Build Coastguard Worker      *)
152*e07d83d3SAndroid Build Coastguard Worker        ulimit -n "$MAX_FD" ||
153*e07d83d3SAndroid Build Coastguard Worker            warn "Could not set maximum file descriptor limit to $MAX_FD"
154*e07d83d3SAndroid Build Coastguard Worker    esac
155*e07d83d3SAndroid Build Coastguard Workerfi
156*e07d83d3SAndroid Build Coastguard Worker
157*e07d83d3SAndroid Build Coastguard Worker# Collect all arguments for the java command, stacking in reverse order:
158*e07d83d3SAndroid Build Coastguard Worker#   * args from the command line
159*e07d83d3SAndroid Build Coastguard Worker#   * the main class name
160*e07d83d3SAndroid Build Coastguard Worker#   * -classpath
161*e07d83d3SAndroid Build Coastguard Worker#   * -D...appname settings
162*e07d83d3SAndroid Build Coastguard Worker#   * --module-path (only if needed)
163*e07d83d3SAndroid Build Coastguard Worker#   * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
164*e07d83d3SAndroid Build Coastguard Worker
165*e07d83d3SAndroid Build Coastguard Worker# For Cygwin or MSYS, switch paths to Windows format before running java
166*e07d83d3SAndroid Build Coastguard Workerif "$cygwin" || "$msys" ; then
167*e07d83d3SAndroid Build Coastguard Worker    APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
168*e07d83d3SAndroid Build Coastguard Worker    CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
169*e07d83d3SAndroid Build Coastguard Worker
170*e07d83d3SAndroid Build Coastguard Worker    JAVACMD=$( cygpath --unix "$JAVACMD" )
171*e07d83d3SAndroid Build Coastguard Worker
172*e07d83d3SAndroid Build Coastguard Worker    # Now convert the arguments - kludge to limit ourselves to /bin/sh
173*e07d83d3SAndroid Build Coastguard Worker    for arg do
174*e07d83d3SAndroid Build Coastguard Worker        if
175*e07d83d3SAndroid Build Coastguard Worker            case $arg in                                #(
176*e07d83d3SAndroid Build Coastguard Worker              -*)   false ;;                            # don't mess with options #(
177*e07d83d3SAndroid Build Coastguard Worker              /?*)  t=${arg#/} t=/${t%%/*}              # looks like a POSIX filepath
178*e07d83d3SAndroid Build Coastguard Worker                    [ -e "$t" ] ;;                      #(
179*e07d83d3SAndroid Build Coastguard Worker              *)    false ;;
180*e07d83d3SAndroid Build Coastguard Worker            esac
181*e07d83d3SAndroid Build Coastguard Worker        then
182*e07d83d3SAndroid Build Coastguard Worker            arg=$( cygpath --path --ignore --mixed "$arg" )
183*e07d83d3SAndroid Build Coastguard Worker        fi
184*e07d83d3SAndroid Build Coastguard Worker        # Roll the args list around exactly as many times as the number of
185*e07d83d3SAndroid Build Coastguard Worker        # args, so each arg winds up back in the position where it started, but
186*e07d83d3SAndroid Build Coastguard Worker        # possibly modified.
187*e07d83d3SAndroid Build Coastguard Worker        #
188*e07d83d3SAndroid Build Coastguard Worker        # NB: a `for` loop captures its iteration list before it begins, so
189*e07d83d3SAndroid Build Coastguard Worker        # changing the positional parameters here affects neither the number of
190*e07d83d3SAndroid Build Coastguard Worker        # iterations, nor the values presented in `arg`.
191*e07d83d3SAndroid Build Coastguard Worker        shift                   # remove old arg
192*e07d83d3SAndroid Build Coastguard Worker        set -- "$@" "$arg"      # push replacement arg
193*e07d83d3SAndroid Build Coastguard Worker    done
194*e07d83d3SAndroid Build Coastguard Workerfi
195*e07d83d3SAndroid Build Coastguard Worker
196*e07d83d3SAndroid Build Coastguard Worker# Collect all arguments for the java command;
197*e07d83d3SAndroid Build Coastguard Worker#   * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198*e07d83d3SAndroid Build Coastguard Worker#     shell script including quotes and variable substitutions, so put them in
199*e07d83d3SAndroid Build Coastguard Worker#     double quotes to make sure that they get re-expanded; and
200*e07d83d3SAndroid Build Coastguard Worker#   * put everything else in single quotes, so that it's not re-expanded.
201*e07d83d3SAndroid Build Coastguard Worker
202*e07d83d3SAndroid Build Coastguard Workerset -- \
203*e07d83d3SAndroid Build Coastguard Worker        "-Dorg.gradle.appname=$APP_BASE_NAME" \
204*e07d83d3SAndroid Build Coastguard Worker        -classpath "$CLASSPATH" \
205*e07d83d3SAndroid Build Coastguard Worker        org.gradle.wrapper.GradleWrapperMain \
206*e07d83d3SAndroid Build Coastguard Worker        "$@"
207*e07d83d3SAndroid Build Coastguard Worker
208*e07d83d3SAndroid Build Coastguard Worker# Use "xargs" to parse quoted args.
209*e07d83d3SAndroid Build Coastguard Worker#
210*e07d83d3SAndroid Build Coastguard Worker# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
211*e07d83d3SAndroid Build Coastguard Worker#
212*e07d83d3SAndroid Build Coastguard Worker# In Bash we could simply go:
213*e07d83d3SAndroid Build Coastguard Worker#
214*e07d83d3SAndroid Build Coastguard Worker#   readarray ARGS < <( xargs -n1 <<<"$var" ) &&
215*e07d83d3SAndroid Build Coastguard Worker#   set -- "${ARGS[@]}" "$@"
216*e07d83d3SAndroid Build Coastguard Worker#
217*e07d83d3SAndroid Build Coastguard Worker# but POSIX shell has neither arrays nor command substitution, so instead we
218*e07d83d3SAndroid Build Coastguard Worker# post-process each arg (as a line of input to sed) to backslash-escape any
219*e07d83d3SAndroid Build Coastguard Worker# character that might be a shell metacharacter, then use eval to reverse
220*e07d83d3SAndroid Build Coastguard Worker# that process (while maintaining the separation between arguments), and wrap
221*e07d83d3SAndroid Build Coastguard Worker# the whole thing up as a single "set" statement.
222*e07d83d3SAndroid Build Coastguard Worker#
223*e07d83d3SAndroid Build Coastguard Worker# This will of course break if any of these variables contains a newline or
224*e07d83d3SAndroid Build Coastguard Worker# an unmatched quote.
225*e07d83d3SAndroid Build Coastguard Worker#
226*e07d83d3SAndroid Build Coastguard Worker
227*e07d83d3SAndroid Build Coastguard Workereval "set -- $(
228*e07d83d3SAndroid Build Coastguard Worker        printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
229*e07d83d3SAndroid Build Coastguard Worker        xargs -n1 |
230*e07d83d3SAndroid Build Coastguard Worker        sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
231*e07d83d3SAndroid Build Coastguard Worker        tr '\n' ' '
232*e07d83d3SAndroid Build Coastguard Worker    )" '"$@"'
233*e07d83d3SAndroid Build Coastguard Worker
234*e07d83d3SAndroid Build Coastguard Workerexec "$JAVACMD" "$@"
235