[go: nahoru, domu]

blob: 8f6607a371a5cd3fdf42a745b0c95a5614f82e63 [file] [log] [blame]
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -07001#!/usr/bin/env bash
Jeff Gaston69713292020-06-04 12:53:39 -04002set -o pipefail
3set -e
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -07004
5##############################################################################
6##
7## Gradle start up script for UN*X
8##
9##############################################################################
10
Aurimas Liutikas9979d072018-03-13 15:38:56 -070011# --------- androidx specific code needed for build server. ------------------
12
Jeff Gaston14292252020-01-07 11:54:44 -050013SCRIPT_PATH="$(cd $(dirname $0) && pwd)"
Aurimas Liutikas9979d072018-03-13 15:38:56 -070014if [ -n "$OUT_DIR" ] ; then
Jeff Gaston8fd9fc82019-07-26 14:26:10 -040015 mkdir -p "$OUT_DIR"
16 OUT_DIR="$(cd $OUT_DIR && pwd)"
Aurimas Liutikas9979d072018-03-13 15:38:56 -070017 export GRADLE_USER_HOME="$OUT_DIR/.gradle"
Jeff Gaston38004a62019-12-11 15:43:10 -050018 export TMPDIR=$OUT_DIR
Jeff Gastoncc694ab2019-04-11 16:51:36 -040019else
Jeff Gastoncc694ab2019-04-11 16:51:36 -040020 CHECKOUT_ROOT="$(cd $SCRIPT_PATH/../.. && pwd)"
21 export OUT_DIR="$CHECKOUT_ROOT/out"
Aurimas Liutikas9979d072018-03-13 15:38:56 -070022fi
23
Jeff Gaston14292252020-01-07 11:54:44 -050024XMX_ARG="$(cd $SCRIPT_PATH && grep org.gradle.jvmargs gradle.properties | sed 's/^/-D/')"
Jeff Gaston0e3d19a2019-10-02 12:17:39 -040025if [ -n "$DIST_DIR" ]; then
26 mkdir -p "$DIST_DIR"
27 DIST_DIR="$(cd $DIST_DIR && pwd)"
28 export LINT_PRINT_STACKTRACE=true
29
Jeff Gastone72d2302019-12-19 18:32:31 -050030 #Set the initial heap size to match the max heap size,
31 #by replacing a string like "-Xmx1g" with one like "-Xms1g -Xmx1g"
32 XMX_ARG="$(echo $XMX_ARG | sed 's/-Xmx\([^ ]*\)/-Xms\1 -Xmx\1/')"
33
Jeff Gaston0e3d19a2019-10-02 12:17:39 -040034 # We don't set a default DIST_DIR in an else clause here because Studio doesn't use gradlew
35 # and doesn't set DIST_DIR and we want gradlew and Studio to match
36fi
37
Aurimas Liutikas9979d072018-03-13 15:38:56 -070038# ----------------------------------------------------------------------------
39
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070040# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070041
42APP_NAME="Gradle"
43APP_BASE_NAME=`basename "$0"`
44
45# Use the maximum available, or set MAX_FD != -1 to use that value.
46MAX_FD="maximum"
47
48warn ( ) {
49 echo "$*"
50}
51
52die ( ) {
53 echo
54 echo "$*"
55 echo
56 exit 1
57}
58
59# OS specific support (must be 'true' or 'false').
60cygwin=false
61msys=false
62darwin=false
63case "`uname`" in
64 CYGWIN* )
65 cygwin=true
66 ;;
67 Darwin* )
68 darwin=true
69 ;;
70 MINGW* )
71 msys=true
72 ;;
73esac
74
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070075# Attempt to set APP_HOME
76# Resolve links: $0 may be a link
77PRG="$0"
78# Need this for relative symlinks.
79while [ -h "$PRG" ] ; do
80 ls=`ls -ld "$PRG"`
81 link=`expr "$ls" : '.*-> \(.*\)$'`
82 if expr "$link" : '/.*' > /dev/null; then
83 PRG="$link"
84 else
85 PRG=`dirname "$PRG"`"/$link"
86 fi
87done
88SAVED="`pwd`"
Yigit Boyarf77697d2016-08-16 10:55:36 -070089cd "`dirname \"$PRG\"`/" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070090APP_HOME="`pwd -P`"
Yigit Boyarf77697d2016-08-16 10:55:36 -070091cd "$SAVED" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070092
93CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
94
Jeff Gaston79a43f22019-04-09 16:19:12 -040095# --------- androidx specific code needed for lint and java. ------------------
96
Alan Viveretted38b36c2017-02-01 16:45:31 -050097# Pick the correct fullsdk for this OS.
Alan Viverette7df63ff2017-03-06 13:12:24 -050098if [ $darwin == "true" ]; then
Alan Viveretted38b36c2017-02-01 16:45:31 -050099 plat="darwin"
100else
101 plat="linux"
102fi
103DEFAULT_JVM_OPTS="-DLINT_API_DATABASE=$APP_HOME/../../prebuilts/fullsdk-$plat/platform-tools/api/api-versions.xml"
104
Matthew Fraschilla6ab84fc32019-11-21 16:40:16 -0800105# Tests for lint checks default to using sdk defined by this variable. This removes a lot of
106# setup from each lint module.
107export ANDROID_HOME="$APP_HOME/../../prebuilts/fullsdk-$plat"
Sergey Vasilinetsefab5eb2019-01-04 12:38:06 +0000108# override JAVA_HOME, because CI machines have it and it points to very old JDK
Aurimas Liutikas4b897cb2019-10-14 13:25:08 -0700109export JAVA_HOME="$APP_HOME/../../prebuilts/jdk/jdk11/$plat-x86"
110export JAVA_TOOLS_JAR="$APP_HOME/../../prebuilts/jdk/jdk8/$plat-x86/lib/tools.jar"
111export STUDIO_GRADLE_JDK=$JAVA_HOME
Oussama Ben Abdelbakif825eb52018-12-04 16:17:00 -0500112
Jeff Gaston79a43f22019-04-09 16:19:12 -0400113# ----------------------------------------------------------------------------
114
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700115# Determine the Java command to use to start the JVM.
116if [ -n "$JAVA_HOME" ] ; then
117 if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
118 # IBM's JDK on AIX uses strange locations for the executables
119 JAVACMD="$JAVA_HOME/jre/sh/java"
120 else
121 JAVACMD="$JAVA_HOME/bin/java"
122 fi
123 if [ ! -x "$JAVACMD" ] ; then
124 die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
125
126Please set the JAVA_HOME variable in your environment to match the
127location of your Java installation."
128 fi
129else
130 JAVACMD="java"
131 which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
132
133Please set the JAVA_HOME variable in your environment to match the
134location of your Java installation."
135fi
136
137# Increase the maximum file descriptors if we can.
138if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
139 MAX_FD_LIMIT=`ulimit -H -n`
140 if [ $? -eq 0 ] ; then
141 if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
142 MAX_FD="$MAX_FD_LIMIT"
143 fi
144 ulimit -n $MAX_FD
145 if [ $? -ne 0 ] ; then
146 warn "Could not set maximum file descriptor limit: $MAX_FD"
147 fi
148 else
149 warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
150 fi
151fi
152
153# For Darwin, add options to specify how the application appears in the dock
154if $darwin; then
155 GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
156fi
157
158# For Cygwin, switch paths to Windows format before running java
159if $cygwin ; then
160 APP_HOME=`cygpath --path --mixed "$APP_HOME"`
161 CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
Yigit Boyarf77697d2016-08-16 10:55:36 -0700162 JAVACMD=`cygpath --unix "$JAVACMD"`
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700163
164 # We build the pattern for arguments to be converted via cygpath
165 ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
166 SEP=""
167 for dir in $ROOTDIRSRAW ; do
168 ROOTDIRS="$ROOTDIRS$SEP$dir"
169 SEP="|"
170 done
171 OURCYGPATTERN="(^($ROOTDIRS))"
172 # Add a user-defined pattern to the cygpath arguments
173 if [ "$GRADLE_CYGPATTERN" != "" ] ; then
174 OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
175 fi
176 # Now convert the arguments - kludge to limit ourselves to /bin/sh
177 i=0
178 for arg in "$@" ; do
179 CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
180 CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
181
182 if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
183 eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
184 else
185 eval `echo args$i`="\"$arg\""
186 fi
187 i=$((i+1))
188 done
189 case $i in
190 (0) set -- ;;
191 (1) set -- "$args0" ;;
192 (2) set -- "$args0" "$args1" ;;
193 (3) set -- "$args0" "$args1" "$args2" ;;
194 (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
195 (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
196 (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
197 (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
198 (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
199 (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
200 esac
201fi
202
203# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
204function splitJvmOpts() {
205 JVM_OPTS=("$@")
206}
207eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
208JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
209
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500210#TODO: Remove HOME_SYSTEM_PROPERTY_ARGUMENT if https://github.com/gradle/gradle/issues/11433 gets fixed
211HOME_SYSTEM_PROPERTY_ARGUMENT=""
212if [ "$GRADLE_USER_HOME" != "" ]; then
213 HOME_SYSTEM_PROPERTY_ARGUMENT="-Duser.home=$GRADLE_USER_HOME"
214fi
Jeff Gaston38004a62019-12-11 15:43:10 -0500215if [ "$TMPDIR" != "" ]; then
216 TMPDIR_ARG="-Djava.io.tmpdir=$TMPDIR"
217fi
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500218
Jeff Gaston18c47d32020-01-27 18:00:28 -0500219function tryToDiagnosePossibleDaemonFailure() {
220 # copy daemon logs
221 if [ -n "$GRADLE_USER_HOME" ]; then
222 if [ -n "$DIST_DIR" ]; then
223 cp -r "$GRADLE_USER_HOME/daemon" "$DIST_DIR/gradle-daemon"
Jeff Gastond54930d2020-02-13 17:25:02 -0500224 cp ./hs_err* $DIST_DIR/ 2>/dev/null || true
Jeff Gaston18c47d32020-01-27 18:00:28 -0500225 fi
226 fi
227}
228
Jeff Gaston224eb172020-01-09 12:31:47 -0500229function runGradle() {
230 if "$JAVACMD" "${JVM_OPTS[@]}" $TMPDIR_ARG -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain $HOME_SYSTEM_PROPERTY_ARGUMENT $TMPDIR_ARG "$XMX_ARG" "$@"; then
231 return 0
232 else
Jeff Gaston18c47d32020-01-27 18:00:28 -0500233 tryToDiagnosePossibleDaemonFailure
Jeff Gaston224eb172020-01-09 12:31:47 -0500234 # Print AndroidX-specific help message if build fails
235 # Have to do this build-failure detection in gradlew rather than in build.gradle
236 # so that this message still prints even if buildSrc itself fails
237 echo
238 echo See also development/diagnose-build-failure for help with build failures in this project.
Jeff Gaston69713292020-06-04 12:53:39 -0400239 return 1
240 fi
241}
242
Jeff Gaston400ccb32020-06-08 16:44:58 -0400243if [[ " ${@} " =~ " -PdisallowExecution " ]]; then
244 echo "Passing '-PdisallowExecution' directly is forbidden. Did you mean -PverifyUpToDate ?"
245 echo "See TaskUpToDateValidator.java for more information"
246 exit 1
247fi
248
Jeff Gastone572f822020-06-12 14:04:48 -0400249runGradle "$@"
Jeff Gaston224eb172020-01-09 12:31:47 -0500250# Check whether we were given the "-PverifyUpToDate" argument
251if [[ " ${@} " =~ " -PverifyUpToDate " ]]; then
Jeff Gastoncefdeae2020-03-09 13:12:35 -0400252 # Re-run Gradle, and find all tasks that are unexpectly out of date
Jeff Gastond2897942020-06-17 16:13:58 -0400253 runGradle "$@" -PdisallowExecution --continue
Jeff Gastonb89c82b2019-08-21 16:24:09 -0400254fi