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