[go: nahoru, domu]

blob: abff1b476cc94b9b76d0db7f67db18c1b8059726 [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
Jeff Gaston54723ef2021-08-16 11:35:08 -040048JAVA_OPTS="$JAVA_OPTS -Dkotlin.incremental.compilation=true" # b/188565660
49
Jeff Gastonc6df4152021-11-03 10:24:02 -040050if [[ " ${@} " =~ " -PupdateLintBaseline " ]]; then
51 # remove when b/188666845 is complete
52 # Inform lint to not fail even when creating a baseline file
53 JAVA_OPTS="$JAVA_OPTS -Dlint.baselines.continue=true"
54fi
55
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070056APP_NAME="Gradle"
57APP_BASE_NAME=`basename "$0"`
58
59# Use the maximum available, or set MAX_FD != -1 to use that value.
60MAX_FD="maximum"
61
62warn ( ) {
63 echo "$*"
64}
65
66die ( ) {
67 echo
68 echo "$*"
69 echo
70 exit 1
71}
72
73# OS specific support (must be 'true' or 'false').
74cygwin=false
75msys=false
76darwin=false
77case "`uname`" in
78 CYGWIN* )
79 cygwin=true
80 ;;
81 Darwin* )
82 darwin=true
83 ;;
84 MINGW* )
85 msys=true
86 ;;
87esac
88
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -070089# Attempt to set APP_HOME
90# Resolve links: $0 may be a link
91PRG="$0"
92# Need this for relative symlinks.
93while [ -h "$PRG" ] ; do
94 ls=`ls -ld "$PRG"`
95 link=`expr "$ls" : '.*-> \(.*\)$'`
96 if expr "$link" : '/.*' > /dev/null; then
97 PRG="$link"
98 else
99 PRG=`dirname "$PRG"`"/$link"
100 fi
101done
102SAVED="`pwd`"
Yigit Boyarf77697d2016-08-16 10:55:36 -0700103cd "`dirname \"$PRG\"`/" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700104APP_HOME="`pwd -P`"
Yigit Boyarf77697d2016-08-16 10:55:36 -0700105cd "$SAVED" >/dev/null
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700106
107CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
108
Jeff Gaston79a43f22019-04-09 16:19:12 -0400109# --------- androidx specific code needed for lint and java. ------------------
110
Alan Viveretted38b36c2017-02-01 16:45:31 -0500111# Pick the correct fullsdk for this OS.
Alan Viverette7df63ff2017-03-06 13:12:24 -0500112if [ $darwin == "true" ]; then
Alan Viveretted38b36c2017-02-01 16:45:31 -0500113 plat="darwin"
114else
115 plat="linux"
116fi
117DEFAULT_JVM_OPTS="-DLINT_API_DATABASE=$APP_HOME/../../prebuilts/fullsdk-$plat/platform-tools/api/api-versions.xml"
118
Matthew Fraschilla6ab84fc32019-11-21 16:40:16 -0800119# Tests for lint checks default to using sdk defined by this variable. This removes a lot of
120# setup from each lint module.
121export ANDROID_HOME="$APP_HOME/../../prebuilts/fullsdk-$plat"
Sergey Vasilinetsefab5eb2019-01-04 12:38:06 +0000122# override JAVA_HOME, because CI machines have it and it points to very old JDK
Aurimas Liutikas4b897cb2019-10-14 13:25:08 -0700123export JAVA_HOME="$APP_HOME/../../prebuilts/jdk/jdk11/$plat-x86"
124export JAVA_TOOLS_JAR="$APP_HOME/../../prebuilts/jdk/jdk8/$plat-x86/lib/tools.jar"
125export STUDIO_GRADLE_JDK=$JAVA_HOME
Oussama Ben Abdelbakif825eb52018-12-04 16:17:00 -0500126
Jeff Gaston79a43f22019-04-09 16:19:12 -0400127# ----------------------------------------------------------------------------
128
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700129# Determine the Java command to use to start the JVM.
130if [ -n "$JAVA_HOME" ] ; then
131 if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
132 # IBM's JDK on AIX uses strange locations for the executables
133 JAVACMD="$JAVA_HOME/jre/sh/java"
134 else
135 JAVACMD="$JAVA_HOME/bin/java"
136 fi
137 if [ ! -x "$JAVACMD" ] ; then
138 die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
139
140Please set the JAVA_HOME variable in your environment to match the
141location of your Java installation."
142 fi
143else
144 JAVACMD="java"
145 which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
146
147Please set the JAVA_HOME variable in your environment to match the
148location of your Java installation."
149fi
150
151# Increase the maximum file descriptors if we can.
152if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
153 MAX_FD_LIMIT=`ulimit -H -n`
154 if [ $? -eq 0 ] ; then
155 if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
156 MAX_FD="$MAX_FD_LIMIT"
157 fi
158 ulimit -n $MAX_FD
159 if [ $? -ne 0 ] ; then
160 warn "Could not set maximum file descriptor limit: $MAX_FD"
161 fi
162 else
163 warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
164 fi
165fi
166
167# For Darwin, add options to specify how the application appears in the dock
168if $darwin; then
169 GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
170fi
171
172# For Cygwin, switch paths to Windows format before running java
173if $cygwin ; then
174 APP_HOME=`cygpath --path --mixed "$APP_HOME"`
175 CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
Yigit Boyarf77697d2016-08-16 10:55:36 -0700176 JAVACMD=`cygpath --unix "$JAVACMD"`
Xavier Ducrohet0f3d9032014-03-18 17:25:21 -0700177
178 # We build the pattern for arguments to be converted via cygpath
179 ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
180 SEP=""
181 for dir in $ROOTDIRSRAW ; do
182 ROOTDIRS="$ROOTDIRS$SEP$dir"
183 SEP="|"
184 done
185 OURCYGPATTERN="(^($ROOTDIRS))"
186 # Add a user-defined pattern to the cygpath arguments
187 if [ "$GRADLE_CYGPATTERN" != "" ] ; then
188 OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
189 fi
190 # Now convert the arguments - kludge to limit ourselves to /bin/sh
191 i=0
192 for arg in "$@" ; do
193 CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
194 CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
195
196 if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
197 eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
198 else
199 eval `echo args$i`="\"$arg\""
200 fi
201 i=$((i+1))
202 done
203 case $i in
204 (0) set -- ;;
205 (1) set -- "$args0" ;;
206 (2) set -- "$args0" "$args1" ;;
207 (3) set -- "$args0" "$args1" "$args2" ;;
208 (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
209 (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
210 (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
211 (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
212 (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
213 (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
214 esac
215fi
216
217# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
218function splitJvmOpts() {
219 JVM_OPTS=("$@")
220}
221eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
222JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
223
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500224#TODO: Remove HOME_SYSTEM_PROPERTY_ARGUMENT if https://github.com/gradle/gradle/issues/11433 gets fixed
225HOME_SYSTEM_PROPERTY_ARGUMENT=""
226if [ "$GRADLE_USER_HOME" != "" ]; then
227 HOME_SYSTEM_PROPERTY_ARGUMENT="-Duser.home=$GRADLE_USER_HOME"
228fi
Jeff Gaston38004a62019-12-11 15:43:10 -0500229if [ "$TMPDIR" != "" ]; then
230 TMPDIR_ARG="-Djava.io.tmpdir=$TMPDIR"
231fi
Jeff Gaston826bdbe2019-11-20 14:56:24 -0500232
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400233if [[ " ${@} " =~ " --clean " ]]; then
234 cleanCaches=true
235else
236 cleanCaches=false
237fi
238
Jeff Gaston794b0b72021-09-28 12:47:47 -0400239if [[ " ${@} " =~ " --no-ci " ]]; then
240 disableCi=true
241else
242 disableCi=false
243fi
244
Jeff Gaston829fd822021-09-23 11:42:48 -0400245# workaround for https://github.com/gradle/gradle/issues/18386
246if [[ " ${@} " =~ " --profile " ]]; then
247 mkdir -p reports
248fi
249
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500250# Expand some arguments
Jeff Gaston794b0b72021-09-28 12:47:47 -0400251for compact in "--ci" "--strict" "--clean" "--no-ci"; do
252 expanded=""
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500253 if [ "$compact" == "--ci" ]; then
Jeff Gaston794b0b72021-09-28 12:47:47 -0400254 if [ "$disableCi" == "false" ]; then
255 expanded="--strict\
256 --stacktrace\
257 -Pandroidx.summarizeStderr\
258 -Pandroidx.enableAffectedModuleDetection\
259 --no-watch-fs"
260 fi
Jeff Gaston4537e142021-01-27 13:08:50 -0500261 fi
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500262 if [ "$compact" == "--strict" ]; then
263 expanded="-Pandroidx.allWarningsAsErrors\
264 -Pandroidx.validateNoUnrecognizedMessages\
Jeff Gaston55624742021-04-22 14:05:49 -0400265 -Pandroidx.verifyUpToDate\
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500266 --no-watch-fs\
267 --no-daemon\
268 --offline"
269 fi
Jeff Gaston794b0b72021-09-28 12:47:47 -0400270 # if compact is something else then we parsed the argument above but
271 # still have to remove it (expanded == "") to avoid confusing Gradle
Jeff Gaston7eafa5d2021-02-10 16:12:54 -0500272
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400273 # check whether this particular compat argument was passed (and therefore needs expansion)
274 if [[ " ${@} " =~ " $compact " ]]; then
275 # Expand an individual argument
276 # Start by making a copy of our list of arguments and iterating through the copy
277 for arg in "$@"; do
278 # Remove this argument from our list of arguments.
279 # By the time we've completed this loop, we will have removed the original copy of
280 # each argument, and potentially re-added a new copy or an expansion of each.
281 shift
282 # Determine whether to expand this argument
283 if [ "$arg" == "$compact" ]; then
284 # Add the expansion to our arguments
285 set -- "$@" $expanded
286 if [ "$expanded" != "" ]; then
287 echo "gradlew expanded '$compact' into '$expanded'"
288 echo
289 fi
290 # We avoid re-adding this argument itself back into the list for two reasons:
291 # 1. This argument might not be directly understood by Gradle
292 # 2. We want to enforce that all behaviors enabled by this flag can be toggled independently,
293 # so we don't want it to be easy to inadvertently check for the presence of this flag
294 # specifically
295 else
296 # Add this argument back into our arguments
297 set -- "$@" "$arg"
298 fi
299 done
300 fi
Jeff Gaston4537e142021-01-27 13:08:50 -0500301done
302
Jeff Gastond2806b32021-09-29 12:20:39 -0400303if [[ " ${@} " =~ " --scan " ]]; then
304 if [[ " ${@} " =~ " --offline " ]]; then
305 echo "--scan incompatible with --offline"
306 echo "you could try --no-ci"
307 exit 1
308 fi
309fi
310
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400311function removeCaches() {
312 rm -rf $SCRIPT_PATH/.gradle
313 rm -rf $SCRIPT_PATH/buildSrc/.gradle
314 rm -f $SCRIPT_PATH/local.properties
315 if [ "$GRADLE_USER_HOME" != "" ]; then
316 rm -rf "$GRADLE_USER_HOME"
317 else
318 rm -rf ~/.gradle
319 fi
Jeff Gaston829fd822021-09-23 11:42:48 -0400320 # https://github.com/gradle/gradle/issues/18386
321 rm -rf $SCRIPT_PATH/reports
Jeff Gaston96eb6012021-09-27 14:55:36 +0000322 rm -rf $SCRIPT_PATH/build
Jeff Gastonbaa2b202021-04-23 15:44:59 -0400323 rm -rf $OUT_DIR
324}
325
326if [ "$cleanCaches" == true ]; then
327 echo "IF ./gradlew --clean FIXES YOUR BUILD; OPEN A BUG."
328 echo "In nearly all cases, it should not be necessary to run a clean build."
329 echo
330 echo "You may be more interested in running:"
331 echo
332 echo " ./development/diagnose-build-failure/diagnose-build-failure.sh $*"
333 echo
334 echo "which attempts to diagnose more details about build failures."
335 echo
336 echo "Removing caches"
337 # 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
338 # another case where it is convenient to have a clean build is for performance testing
339 # 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)
340 echo
341
342 removeCaches
343fi
344
Jeff Gaston224eb172020-01-09 12:31:47 -0500345function runGradle() {
Jeff Gaston41b90222020-08-18 11:09:55 -0400346 processOutput=false
Jeff Gastone4b4b872020-08-25 09:02:13 -0400347 if [[ " ${@} " =~ " -Pandroidx.validateNoUnrecognizedMessages " ]]; then
Jeff Gaston41b90222020-08-18 11:09:55 -0400348 processOutput=true
349 fi
350 if [[ " ${@} " =~ " -Pandroidx.summarizeStderr " ]]; then
351 processOutput=true
352 fi
353 if [ "$processOutput" == "true" ]; then
354 wrapper="$SCRIPT_PATH/development/build_log_processor.sh"
355 else
356 wrapper=""
357 fi
Jeff Gaston3febf902021-03-16 11:23:15 -0400358
359 PROJECT_CACHE_DIR_ARGUMENT="--project-cache-dir $OUT_DIR/gradle-project-cache"
360 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 -0500361 return 0
362 else
363 # Print AndroidX-specific help message if build fails
364 # Have to do this build-failure detection in gradlew rather than in build.gradle
365 # so that this message still prints even if buildSrc itself fails
366 echo
Jeff Gaston61cef332020-12-22 11:23:09 -0500367 echo For help with unexpected failures, see development/diagnose-build-failure/README.md
368 echo
Jeff Gaston69713292020-06-04 12:53:39 -0400369 return 1
370 fi
371}
372
Jeff Gaston400ccb32020-06-08 16:44:58 -0400373if [[ " ${@} " =~ " -PdisallowExecution " ]]; then
Jeff Gaston55624742021-04-22 14:05:49 -0400374 echo "Passing '-PdisallowExecution' directly is forbidden. Did you mean -Pandroidx.verifyUpToDate ?"
Jeff Gaston400ccb32020-06-08 16:44:58 -0400375 echo "See TaskUpToDateValidator.java for more information"
376 exit 1
377fi
378
Jeff Gaston224eb172020-01-09 12:31:47 -0500379if [[ " ${@} " =~ " -PverifyUpToDate " ]]; then
Jeff Gaston55624742021-04-22 14:05:49 -0400380 echo "-PverifyUpToDate has been renamed to -Pandroidx.verifyUpToDate"
381 exit 1
382fi
383
384runGradle "$@"
385# Check whether we were given the "-Pandroidx.verifyUpToDate" argument
386if [[ " ${@} " =~ " -Pandroidx.verifyUpToDate " ]]; then
Jeff Gastoncefdeae2020-03-09 13:12:35 -0400387 # Re-run Gradle, and find all tasks that are unexpectly out of date
Jeff Gastone906e5c2020-11-05 12:33:10 -0500388 if ! runGradle "$@" -PdisallowExecution --continue; then
389 echo >&2
390 echo "TaskUpToDateValidator's second build failed, -PdisallowExecution specified" >&2
391 exit 1
392 fi
Jeff Gastonb89c82b2019-08-21 16:24:09 -0400393fi