blob: 4067d06f24f0f64199c5cf86f6a25ca8c088fdab [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 Gastondd8a6e92020-09-01 14:26:49 -040013SCRIPT_PATH="$(cd $(dirname $0) && pwd -P)"
Aurimas Liutikas9979d072018-03-13 15:38:56 -070014if [ -n "$OUT_DIR" ] ; then
Jeff Gaston8fd9fc82019-07-26 14:26:10 -040015 mkdir -p "$OUT_DIR"
Jeff Gastondd8a6e92020-09-01 14:26:49 -040016 OUT_DIR="$(cd $OUT_DIR && pwd -P)"
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 Gastondd8a6e92020-09-01 14:26:49 -040020 CHECKOUT_ROOT="$(cd $SCRIPT_PATH/../.. && pwd -P)"
Jeff Gastoncc694ab2019-04-11 16:51:36 -040021 export OUT_DIR="$CHECKOUT_ROOT/out"
Aurimas Liutikas9979d072018-03-13 15:38:56 -070022fi
23
Jeff Gaston440e1ac2020-09-09 08:32:22 -040024ORG_GRADLE_JVMARGS="$(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"
Jeff Gastondd8a6e92020-09-01 14:26:49 -040027 DIST_DIR="$(cd $DIST_DIR && pwd -P)"
Jeff Gaston0e3d19a2019-10-02 12:17:39 -040028 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"
Jeff Gaston77bb2b12021-04-15 12:51:08 -040032 MAX_MEM=24g
Jeff Gastoncca984c2020-10-05 12:54:17 -040033 ORG_GRADLE_JVMARGS="$(echo $ORG_GRADLE_JVMARGS | sed "s/-Xmx\([^ ]*\)/-Xms$MAX_MEM -Xmx$MAX_MEM/")"
Jeff Gaston440e1ac2020-09-09 08:32:22 -040034
35 # tell Gradle where to put a heap dump on failure
36 ORG_GRADLE_JVMARGS="$(echo $ORG_GRADLE_JVMARGS | sed "s|$| -XX:HeapDumpPath=$DIST_DIR|")"
Jeff Gastone72d2302019-12-19 18:32:31 -050037
Jeff Gaston0e3d19a2019-10-02 12:17:39 -040038 # We don't set a default DIST_DIR in an else clause here because Studio doesn't use gradlew
39 # and doesn't set DIST_DIR and we want gradlew and Studio to match
40fi
41
Jeff Gastonc21ecb32020-11-05 17:16:35 -050042# unset ANDROID_BUILD_TOP so that Lint doesn't think we're building the platform itself
43unset ANDROID_BUILD_TOP
Aurimas Liutikas9979d072018-03-13 15:38:56 -070044# ----------------------------------------------------------------------------
45
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070046# 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 -070047
48APP_NAME="Gradle"
49APP_BASE_NAME=`basename "$0"`
50
51# Use the maximum available, or set MAX_FD != -1 to use that value.
52MAX_FD="maximum"
53
54warn ( ) {
55 echo "$*"
56}
57
58die ( ) {
59 echo
60 echo "$*"
61 echo
62 exit 1
63}
64
65# OS specific support (must be 'true' or 'false').
66cygwin=false
67msys=false
68darwin=false
69case "`uname`" in
70 CYGWIN* )
71 cygwin=true
72 ;;
73 Darwin* )
74 darwin=true
75 ;;
76 MINGW* )
77 msys=true
78 ;;
79esac
80
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070081# Attempt to set APP_HOME
82# Resolve links: $0 may be a link
83PRG="$0"
84# Need this for relative symlinks.
85while [ -h "$PRG" ] ; do
86 ls=`ls -ld "$PRG"`
87 link=`expr "$ls" : '.*-> \(.*\)$'`
88 if expr "$link" : '/.*' > /dev/null; then
89 PRG="$link"
90 else
91 PRG=`dirname "$PRG"`"/$link"
92 fi
93done
94SAVED="`pwd`"
Yigit Boyarf77697d2016-08-16 10:55:36 -070095cd "`dirname \"$PRG\"`/" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070096APP_HOME="`pwd -P`"
Yigit Boyarf77697d2016-08-16 10:55:36 -070097cd "$SAVED" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070098
99CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
100
Jeff Gaston79a43f22019-04-09 16:19:12 -0400101# --------- androidx specific code needed for lint and java. ------------------
102
Alan Viveretted38b36c2017-02-01 16:45:31 -0500103# Pick the correct fullsdk for this OS.
Alan Viverette7df63ff2017-03-06 13:12:24 -0500104if [ $darwin == "true" ]; then
Alan Viveretted38b36c2017-02-01 16:45:31 -0500105 plat="darwin"
106else
107 plat="linux"
108fi
109DEFAULT_JVM_OPTS="-DLINT_API_DATABASE=$APP_HOME/../../prebuilts/fullsdk-$plat/platform-tools/api/api-versions.xml"
110
Matthew Fraschilla6ab84fc32019-11-21 16:40:16 -0800111# Tests for lint checks default to using sdk defined by this variable. This removes a lot of
112# setup from each lint module.
113export ANDROID_HOME="$APP_HOME/../../prebuilts/fullsdk-$plat"
Sergey Vasilinetsefab5eb2019-01-04 12:38:06 +0000114# override JAVA_HOME, because CI machines have it and it points to very old JDK
Aurimas Liutikas4b897cb2019-10-14 13:25:08 -0700115export JAVA_HOME="$APP_HOME/../../prebuilts/jdk/jdk11/$plat-x86"
116export JAVA_TOOLS_JAR="$APP_HOME/../../prebuilts/jdk/jdk8/$plat-x86/lib/tools.jar"
117export STUDIO_GRADLE_JDK=$JAVA_HOME
Oussama Ben Abdelbakif825eb52018-12-04 16:17:00 -0500118
Jeff Gaston79a43f22019-04-09 16:19:12 -0400119# ----------------------------------------------------------------------------
120
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700121# Determine the Java command to use to start the JVM.
122if [ -n "$JAVA_HOME" ] ; then
123 if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
124 # IBM's JDK on AIX uses strange locations for the executables
125 JAVACMD="$JAVA_HOME/jre/sh/java"
126 else
127 JAVACMD="$JAVA_HOME/bin/java"
128 fi
129 if [ ! -x "$JAVACMD" ] ; then
130 die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
131
132Please set the JAVA_HOME variable in your environment to match the
133location of your Java installation."
134 fi
135else
136 JAVACMD="java"
137 which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
138
139Please set the JAVA_HOME variable in your environment to match the
140location of your Java installation."
141fi
142
143# Increase the maximum file descriptors if we can.
144if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
145 MAX_FD_LIMIT=`ulimit -H -n`
146 if [ $? -eq 0 ] ; then
147 if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
148 MAX_FD="$MAX_FD_LIMIT"
149 fi
150 ulimit -n $MAX_FD
151 if [ $? -ne 0 ] ; then
152 warn "Could not set maximum file descriptor limit: $MAX_FD"
153 fi
154 else
155 warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
156 fi
157fi
158
159# For Darwin, add options to specify how the application appears in the dock
160if $darwin; then
161 GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
162fi
163
164# For Cygwin, switch paths to Windows format before running java
165if $cygwin ; then
166 APP_HOME=`cygpath --path --mixed "$APP_HOME"`
167 CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
Yigit Boyarf77697d2016-08-16 10:55:36 -0700168 JAVACMD=`cygpath --unix "$JAVACMD"`
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700169
170 # We build the pattern for arguments to be converted via cygpath
171 ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
172 SEP=""
173 for dir in $ROOTDIRSRAW ; do
174 ROOTDIRS="$ROOTDIRS$SEP$dir"
175 SEP="|"
176 done
177 OURCYGPATTERN="(^($ROOTDIRS))"
178 # Add a user-defined pattern to the cygpath arguments
179 if [ "$GRADLE_CYGPATTERN" != "" ] ; then
180 OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
181 fi
182 # Now convert the arguments - kludge to limit ourselves to /bin/sh
183 i=0
184 for arg in "$@" ; do
185 CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
186 CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
187
188 if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
189 eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
190 else
191 eval `echo args$i`="\"$arg\""
192 fi
193 i=$((i+1))
194 done
195 case $i in
196 (0) set -- ;;
197 (1) set -- "$args0" ;;
198 (2) set -- "$args0" "$args1" ;;
199 (3) set -- "$args0" "$args1" "$args2" ;;
200 (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
201 (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
202 (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
203 (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
204 (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
205 (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
206 esac
207fi
208
209# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
210function splitJvmOpts() {
211 JVM_OPTS=("$@")
212}
213eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
214JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
215
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500216#TODO: Remove HOME_SYSTEM_PROPERTY_ARGUMENT if https://github.com/gradle/gradle/issues/11433 gets fixed
217HOME_SYSTEM_PROPERTY_ARGUMENT=""
218if [ "$GRADLE_USER_HOME" != "" ]; then
219 HOME_SYSTEM_PROPERTY_ARGUMENT="-Duser.home=$GRADLE_USER_HOME"
220fi
Jeff Gaston38004a62019-12-11 15:43:10 -0500221if [ "$TMPDIR" != "" ]; then
222 TMPDIR_ARG="-Djava.io.tmpdir=$TMPDIR"
223fi
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500224
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400225if [[ " ${@} " =~ " --clean " ]]; then
226 cleanCaches=true
227else
228 cleanCaches=false
229fi
230
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500231# Expand some arguments
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400232for compact in "--ci" "--strict" "--clean"; do
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500233 if [ "$compact" == "--ci" ]; then
234 expanded="--strict\
235 --stacktrace\
236 -Pandroidx.summarizeStderr\
237 -Pandroidx.coverageEnabled=true\
238 -Pandroidx.enableAffectedModuleDetection\
239 --no-watch-fs"
Jeff Gaston4537e142021-01-27 13:08:50 -0500240 fi
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500241 if [ "$compact" == "--strict" ]; then
242 expanded="-Pandroidx.allWarningsAsErrors\
243 -Pandroidx.validateNoUnrecognizedMessages\
Jeff Gaston55624742021-04-22 14:05:49 -0400244 -Pandroidx.verifyUpToDate\
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500245 --no-watch-fs\
246 --no-daemon\
247 --offline"
248 fi
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400249 if [ "$compact" == "--clean" ]; then
250 expanded="" # we parsed the argument above but we still have to remove it to avoid confusing Gradle
251 fi
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500252
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400253 # check whether this particular compat argument was passed (and therefore needs expansion)
254 if [[ " ${@} " =~ " $compact " ]]; then
255 # Expand an individual argument
256 # Start by making a copy of our list of arguments and iterating through the copy
257 for arg in "$@"; do
258 # Remove this argument from our list of arguments.
259 # By the time we've completed this loop, we will have removed the original copy of
260 # each argument, and potentially re-added a new copy or an expansion of each.
261 shift
262 # Determine whether to expand this argument
263 if [ "$arg" == "$compact" ]; then
264 # Add the expansion to our arguments
265 set -- "$@" $expanded
266 if [ "$expanded" != "" ]; then
267 echo "gradlew expanded '$compact' into '$expanded'"
268 echo
269 fi
270 # We avoid re-adding this argument itself back into the list for two reasons:
271 # 1. This argument might not be directly understood by Gradle
272 # 2. We want to enforce that all behaviors enabled by this flag can be toggled independently,
273 # so we don't want it to be easy to inadvertently check for the presence of this flag
274 # specifically
275 else
276 # Add this argument back into our arguments
277 set -- "$@" "$arg"
278 fi
279 done
280 fi
Jeff Gaston4537e142021-01-27 13:08:50 -0500281done
282
Jeff Gaston18c47d32020-01-27 18:00:28 -0500283function tryToDiagnosePossibleDaemonFailure() {
284 # copy daemon logs
285 if [ -n "$GRADLE_USER_HOME" ]; then
286 if [ -n "$DIST_DIR" ]; then
287 cp -r "$GRADLE_USER_HOME/daemon" "$DIST_DIR/gradle-daemon"
Jeff Gastond54930d2020-02-13 17:25:02 -0500288 cp ./hs_err* $DIST_DIR/ 2>/dev/null || true
Jeff Gaston18c47d32020-01-27 18:00:28 -0500289 fi
290 fi
291}
292
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400293function removeCaches() {
294 rm -rf $SCRIPT_PATH/.gradle
295 rm -rf $SCRIPT_PATH/buildSrc/.gradle
296 rm -f $SCRIPT_PATH/local.properties
297 if [ "$GRADLE_USER_HOME" != "" ]; then
298 rm -rf "$GRADLE_USER_HOME"
299 else
300 rm -rf ~/.gradle
301 fi
302 # AGP should (also) do this automatically (b/170640263)
303 rm -rf $SCRIPT_PATH/appsearch/appsearch/.cxx
304 rm -rf $SCRIPT_PATH/appsearch/local-backend/.cxx
305 rm -rf $SCRIPT_PATH/appsearch/local-storage/.cxx
306 rm -rf $OUT_DIR
307}
308
309if [ "$cleanCaches" == true ]; then
310 echo "IF ./gradlew --clean FIXES YOUR BUILD; OPEN A BUG."
311 echo "In nearly all cases, it should not be necessary to run a clean build."
312 echo
313 echo "You may be more interested in running:"
314 echo
315 echo " ./development/diagnose-build-failure/diagnose-build-failure.sh $*"
316 echo
317 echo "which attempts to diagnose more details about build failures."
318 echo
319 echo "Removing caches"
320 # one case where it is convenient to have a clean build is for double-checking that a build failure isn't due to an incremental build failure
321 # another case where it is convenient to have a clean build is for performance testing
322 # another case where it is convenient to have a clean build is when you're modifying the build and may have introduced some errors but haven't shared your changes yet (at which point you should have fixed the errors)
323 echo
324
325 removeCaches
326fi
327
Jeff Gaston224eb172020-01-09 12:31:47 -0500328function runGradle() {
Jeff Gaston41b90222020-08-18 11:09:55 -0400329 processOutput=false
Jeff Gastone4b4b872020-08-25 09:02:13 -0400330 if [[ " ${@} " =~ " -Pandroidx.validateNoUnrecognizedMessages " ]]; then
Jeff Gaston41b90222020-08-18 11:09:55 -0400331 processOutput=true
332 fi
333 if [[ " ${@} " =~ " -Pandroidx.summarizeStderr " ]]; then
334 processOutput=true
335 fi
336 if [ "$processOutput" == "true" ]; then
337 wrapper="$SCRIPT_PATH/development/build_log_processor.sh"
338 else
339 wrapper=""
340 fi
Jeff Gaston3febf902021-03-16 11:23:15 -0400341
342 PROJECT_CACHE_DIR_ARGUMENT="--project-cache-dir $OUT_DIR/gradle-project-cache"
343 if $wrapper "$JAVACMD" "${JVM_OPTS[@]}" $TMPDIR_ARG -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain $HOME_SYSTEM_PROPERTY_ARGUMENT $TMPDIR_ARG $PROJECT_CACHE_DIR_ARGUMENT "$ORG_GRADLE_JVMARGS" "$@"; then
Jeff Gaston224eb172020-01-09 12:31:47 -0500344 return 0
345 else
Jeff Gaston18c47d32020-01-27 18:00:28 -0500346 tryToDiagnosePossibleDaemonFailure
Jeff Gaston224eb172020-01-09 12:31:47 -0500347 # Print AndroidX-specific help message if build fails
348 # Have to do this build-failure detection in gradlew rather than in build.gradle
349 # so that this message still prints even if buildSrc itself fails
350 echo
Jeff Gaston61cef332020-12-22 11:23:09 -0500351 echo For help with unexpected failures, see development/diagnose-build-failure/README.md
352 echo
Jeff Gaston69713292020-06-04 12:53:39 -0400353 return 1
354 fi
355}
356
Jeff Gaston400ccb32020-06-08 16:44:58 -0400357if [[ " ${@} " =~ " -PdisallowExecution " ]]; then
Jeff Gaston55624742021-04-22 14:05:49 -0400358 echo "Passing '-PdisallowExecution' directly is forbidden. Did you mean -Pandroidx.verifyUpToDate ?"
Jeff Gaston400ccb32020-06-08 16:44:58 -0400359 echo "See TaskUpToDateValidator.java for more information"
360 exit 1
361fi
362
Jeff Gaston224eb172020-01-09 12:31:47 -0500363if [[ " ${@} " =~ " -PverifyUpToDate " ]]; then
Jeff Gaston55624742021-04-22 14:05:49 -0400364 echo "-PverifyUpToDate has been renamed to -Pandroidx.verifyUpToDate"
365 exit 1
366fi
367
368runGradle "$@"
369# Check whether we were given the "-Pandroidx.verifyUpToDate" argument
370if [[ " ${@} " =~ " -Pandroidx.verifyUpToDate " ]]; then
Jeff Gastoncefdeae2020-03-09 13:12:35 -0400371 # Re-run Gradle, and find all tasks that are unexpectly out of date
Jeff Gastone906e5c2020-11-05 12:33:10 -0500372 if ! runGradle "$@" -PdisallowExecution --continue; then
373 echo >&2
374 echo "TaskUpToDateValidator's second build failed, -PdisallowExecution specified" >&2
375 exit 1
376 fi
Jeff Gastonb89c82b2019-08-21 16:24:09 -0400377fi