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