xref: /aosp_15_r20/external/kotlinx.coroutines/integration-testing/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/master/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 WorkerAPP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84*7a7160feSAndroid Build Coastguard Worker
85*7a7160feSAndroid Build Coastguard WorkerAPP_NAME="Gradle"
86*7a7160feSAndroid Build Coastguard WorkerAPP_BASE_NAME=${0##*/}
87*7a7160feSAndroid Build Coastguard Worker
88*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.
89*7a7160feSAndroid Build Coastguard WorkerDEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
90*7a7160feSAndroid Build Coastguard Worker
91*7a7160feSAndroid Build Coastguard Worker# Use the maximum available, or set MAX_FD != -1 to use that value.
92*7a7160feSAndroid Build Coastguard WorkerMAX_FD=maximum
93*7a7160feSAndroid Build Coastguard Worker
94*7a7160feSAndroid Build Coastguard Workerwarn () {
95*7a7160feSAndroid Build Coastguard Worker    echo "$*"
96*7a7160feSAndroid Build Coastguard Worker} >&2
97*7a7160feSAndroid Build Coastguard Worker
98*7a7160feSAndroid Build Coastguard Workerdie () {
99*7a7160feSAndroid Build Coastguard Worker    echo
100*7a7160feSAndroid Build Coastguard Worker    echo "$*"
101*7a7160feSAndroid Build Coastguard Worker    echo
102*7a7160feSAndroid Build Coastguard Worker    exit 1
103*7a7160feSAndroid Build Coastguard Worker} >&2
104*7a7160feSAndroid Build Coastguard Worker
105*7a7160feSAndroid Build Coastguard Worker# OS specific support (must be 'true' or 'false').
106*7a7160feSAndroid Build Coastguard Workercygwin=false
107*7a7160feSAndroid Build Coastguard Workermsys=false
108*7a7160feSAndroid Build Coastguard Workerdarwin=false
109*7a7160feSAndroid Build Coastguard Workernonstop=false
110*7a7160feSAndroid Build Coastguard Workercase "$( uname )" in                #(
111*7a7160feSAndroid Build Coastguard Worker  CYGWIN* )         cygwin=true  ;; #(
112*7a7160feSAndroid Build Coastguard Worker  Darwin* )         darwin=true  ;; #(
113*7a7160feSAndroid Build Coastguard Worker  MSYS* | MINGW* )  msys=true    ;; #(
114*7a7160feSAndroid Build Coastguard Worker  NONSTOP* )        nonstop=true ;;
115*7a7160feSAndroid Build Coastguard Workeresac
116*7a7160feSAndroid Build Coastguard Worker
117*7a7160feSAndroid Build Coastguard WorkerCLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
118*7a7160feSAndroid Build Coastguard Worker
119*7a7160feSAndroid Build Coastguard Worker
120*7a7160feSAndroid Build Coastguard Worker# Determine the Java command to use to start the JVM.
121*7a7160feSAndroid Build Coastguard Workerif [ -n "$JAVA_HOME" ] ; then
122*7a7160feSAndroid Build Coastguard Worker    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123*7a7160feSAndroid Build Coastguard Worker        # IBM's JDK on AIX uses strange locations for the executables
124*7a7160feSAndroid Build Coastguard Worker        JAVACMD=$JAVA_HOME/jre/sh/java
125*7a7160feSAndroid Build Coastguard Worker    else
126*7a7160feSAndroid Build Coastguard Worker        JAVACMD=$JAVA_HOME/bin/java
127*7a7160feSAndroid Build Coastguard Worker    fi
128*7a7160feSAndroid Build Coastguard Worker    if [ ! -x "$JAVACMD" ] ; then
129*7a7160feSAndroid Build Coastguard Worker        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130*7a7160feSAndroid Build Coastguard Worker
131*7a7160feSAndroid Build Coastguard WorkerPlease set the JAVA_HOME variable in your environment to match the
132*7a7160feSAndroid Build Coastguard Workerlocation of your Java installation."
133*7a7160feSAndroid Build Coastguard Worker    fi
134*7a7160feSAndroid Build Coastguard Workerelse
135*7a7160feSAndroid Build Coastguard Worker    JAVACMD=java
136*7a7160feSAndroid 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*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 Workerfi
141*7a7160feSAndroid Build Coastguard Worker
142*7a7160feSAndroid Build Coastguard Worker# Increase the maximum file descriptors if we can.
143*7a7160feSAndroid Build Coastguard Workerif ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144*7a7160feSAndroid Build Coastguard Worker    case $MAX_FD in #(
145*7a7160feSAndroid Build Coastguard Worker      max*)
146*7a7160feSAndroid Build Coastguard Worker        MAX_FD=$( ulimit -H -n ) ||
147*7a7160feSAndroid Build Coastguard Worker            warn "Could not query maximum file descriptor limit"
148*7a7160feSAndroid Build Coastguard Worker    esac
149*7a7160feSAndroid Build Coastguard Worker    case $MAX_FD in  #(
150*7a7160feSAndroid Build Coastguard Worker      '' | soft) :;; #(
151*7a7160feSAndroid Build Coastguard Worker      *)
152*7a7160feSAndroid Build Coastguard Worker        ulimit -n "$MAX_FD" ||
153*7a7160feSAndroid Build Coastguard Worker            warn "Could not set maximum file descriptor limit to $MAX_FD"
154*7a7160feSAndroid Build Coastguard Worker    esac
155*7a7160feSAndroid Build Coastguard Workerfi
156*7a7160feSAndroid Build Coastguard Worker
157*7a7160feSAndroid Build Coastguard Worker# Collect all arguments for the java command, stacking in reverse order:
158*7a7160feSAndroid Build Coastguard Worker#   * args from the command line
159*7a7160feSAndroid Build Coastguard Worker#   * the main class name
160*7a7160feSAndroid Build Coastguard Worker#   * -classpath
161*7a7160feSAndroid Build Coastguard Worker#   * -D...appname settings
162*7a7160feSAndroid Build Coastguard Worker#   * --module-path (only if needed)
163*7a7160feSAndroid Build Coastguard Worker#   * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
164*7a7160feSAndroid Build Coastguard Worker
165*7a7160feSAndroid Build Coastguard Worker# For Cygwin or MSYS, switch paths to Windows format before running java
166*7a7160feSAndroid Build Coastguard Workerif "$cygwin" || "$msys" ; then
167*7a7160feSAndroid Build Coastguard Worker    APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
168*7a7160feSAndroid Build Coastguard Worker    CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
169*7a7160feSAndroid Build Coastguard Worker
170*7a7160feSAndroid Build Coastguard Worker    JAVACMD=$( cygpath --unix "$JAVACMD" )
171*7a7160feSAndroid Build Coastguard Worker
172*7a7160feSAndroid Build Coastguard Worker    # Now convert the arguments - kludge to limit ourselves to /bin/sh
173*7a7160feSAndroid Build Coastguard Worker    for arg do
174*7a7160feSAndroid Build Coastguard Worker        if
175*7a7160feSAndroid Build Coastguard Worker            case $arg in                                #(
176*7a7160feSAndroid Build Coastguard Worker              -*)   false ;;                            # don't mess with options #(
177*7a7160feSAndroid Build Coastguard Worker              /?*)  t=${arg#/} t=/${t%%/*}              # looks like a POSIX filepath
178*7a7160feSAndroid Build Coastguard Worker                    [ -e "$t" ] ;;                      #(
179*7a7160feSAndroid Build Coastguard Worker              *)    false ;;
180*7a7160feSAndroid Build Coastguard Worker            esac
181*7a7160feSAndroid Build Coastguard Worker        then
182*7a7160feSAndroid Build Coastguard Worker            arg=$( cygpath --path --ignore --mixed "$arg" )
183*7a7160feSAndroid Build Coastguard Worker        fi
184*7a7160feSAndroid Build Coastguard Worker        # Roll the args list around exactly as many times as the number of
185*7a7160feSAndroid Build Coastguard Worker        # args, so each arg winds up back in the position where it started, but
186*7a7160feSAndroid Build Coastguard Worker        # possibly modified.
187*7a7160feSAndroid Build Coastguard Worker        #
188*7a7160feSAndroid Build Coastguard Worker        # NB: a `for` loop captures its iteration list before it begins, so
189*7a7160feSAndroid Build Coastguard Worker        # changing the positional parameters here affects neither the number of
190*7a7160feSAndroid Build Coastguard Worker        # iterations, nor the values presented in `arg`.
191*7a7160feSAndroid Build Coastguard Worker        shift                   # remove old arg
192*7a7160feSAndroid Build Coastguard Worker        set -- "$@" "$arg"      # push replacement arg
193*7a7160feSAndroid Build Coastguard Worker    done
194*7a7160feSAndroid Build Coastguard Workerfi
195*7a7160feSAndroid Build Coastguard Worker
196*7a7160feSAndroid Build Coastguard Worker# Collect all arguments for the java command;
197*7a7160feSAndroid Build Coastguard Worker#   * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198*7a7160feSAndroid Build Coastguard Worker#     shell script including quotes and variable substitutions, so put them in
199*7a7160feSAndroid Build Coastguard Worker#     double quotes to make sure that they get re-expanded; and
200*7a7160feSAndroid Build Coastguard Worker#   * put everything else in single quotes, so that it's not re-expanded.
201*7a7160feSAndroid Build Coastguard Worker
202*7a7160feSAndroid Build Coastguard Workerset -- \
203*7a7160feSAndroid Build Coastguard Worker        "-Dorg.gradle.appname=$APP_BASE_NAME" \
204*7a7160feSAndroid Build Coastguard Worker        -classpath "$CLASSPATH" \
205*7a7160feSAndroid Build Coastguard Worker        org.gradle.wrapper.GradleWrapperMain \
206*7a7160feSAndroid Build Coastguard Worker        "$@"
207*7a7160feSAndroid Build Coastguard Worker
208*7a7160feSAndroid Build Coastguard Worker# Use "xargs" to parse quoted args.
209*7a7160feSAndroid Build Coastguard Worker#
210*7a7160feSAndroid Build Coastguard Worker# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
211*7a7160feSAndroid Build Coastguard Worker#
212*7a7160feSAndroid Build Coastguard Worker# In Bash we could simply go:
213*7a7160feSAndroid Build Coastguard Worker#
214*7a7160feSAndroid Build Coastguard Worker#   readarray ARGS < <( xargs -n1 <<<"$var" ) &&
215*7a7160feSAndroid Build Coastguard Worker#   set -- "${ARGS[@]}" "$@"
216*7a7160feSAndroid Build Coastguard Worker#
217*7a7160feSAndroid Build Coastguard Worker# but POSIX shell has neither arrays nor command substitution, so instead we
218*7a7160feSAndroid Build Coastguard Worker# post-process each arg (as a line of input to sed) to backslash-escape any
219*7a7160feSAndroid Build Coastguard Worker# character that might be a shell metacharacter, then use eval to reverse
220*7a7160feSAndroid Build Coastguard Worker# that process (while maintaining the separation between arguments), and wrap
221*7a7160feSAndroid Build Coastguard Worker# the whole thing up as a single "set" statement.
222*7a7160feSAndroid Build Coastguard Worker#
223*7a7160feSAndroid Build Coastguard Worker# This will of course break if any of these variables contains a newline or
224*7a7160feSAndroid Build Coastguard Worker# an unmatched quote.
225*7a7160feSAndroid Build Coastguard Worker#
226*7a7160feSAndroid Build Coastguard Worker
227*7a7160feSAndroid Build Coastguard Workereval "set -- $(
228*7a7160feSAndroid Build Coastguard Worker        printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
229*7a7160feSAndroid Build Coastguard Worker        xargs -n1 |
230*7a7160feSAndroid Build Coastguard Worker        sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
231*7a7160feSAndroid Build Coastguard Worker        tr '\n' ' '
232*7a7160feSAndroid Build Coastguard Worker    )" '"$@"'
233*7a7160feSAndroid Build Coastguard Worker
234*7a7160feSAndroid Build Coastguard Workerexec "$JAVACMD" "$@"
235