Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Jeff Gaston | 6971329 | 2020-06-04 12:53:39 -0400 | [diff] [blame] | 2 | set -o pipefail |
| 3 | set -e |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 4 | |
| 5 | ############################################################################## |
| 6 | ## |
| 7 | ## Gradle start up script for UN*X |
| 8 | ## |
| 9 | ############################################################################## |
| 10 | |
Aurimas Liutikas | 9979d07 | 2018-03-13 15:38:56 -0700 | [diff] [blame] | 11 | # --------- androidx specific code needed for build server. ------------------ |
| 12 | |
Jeff Gaston | 1429225 | 2020-01-07 11:54:44 -0500 | [diff] [blame] | 13 | SCRIPT_PATH="$(cd $(dirname $0) && pwd)" |
Aurimas Liutikas | 9979d07 | 2018-03-13 15:38:56 -0700 | [diff] [blame] | 14 | if [ -n "$OUT_DIR" ] ; then |
Jeff Gaston | 8fd9fc8 | 2019-07-26 14:26:10 -0400 | [diff] [blame] | 15 | mkdir -p "$OUT_DIR" |
| 16 | OUT_DIR="$(cd $OUT_DIR && pwd)" |
Aurimas Liutikas | 9979d07 | 2018-03-13 15:38:56 -0700 | [diff] [blame] | 17 | export GRADLE_USER_HOME="$OUT_DIR/.gradle" |
Jeff Gaston | 38004a6 | 2019-12-11 15:43:10 -0500 | [diff] [blame] | 18 | export TMPDIR=$OUT_DIR |
Jeff Gaston | cc694ab | 2019-04-11 16:51:36 -0400 | [diff] [blame] | 19 | else |
Jeff Gaston | cc694ab | 2019-04-11 16:51:36 -0400 | [diff] [blame] | 20 | CHECKOUT_ROOT="$(cd $SCRIPT_PATH/../.. && pwd)" |
| 21 | export OUT_DIR="$CHECKOUT_ROOT/out" |
Aurimas Liutikas | 9979d07 | 2018-03-13 15:38:56 -0700 | [diff] [blame] | 22 | fi |
| 23 | |
Jeff Gaston | 1429225 | 2020-01-07 11:54:44 -0500 | [diff] [blame] | 24 | XMX_ARG="$(cd $SCRIPT_PATH && grep org.gradle.jvmargs gradle.properties | sed 's/^/-D/')" |
Jeff Gaston | 0e3d19a | 2019-10-02 12:17:39 -0400 | [diff] [blame] | 25 | if [ -n "$DIST_DIR" ]; then |
| 26 | mkdir -p "$DIST_DIR" |
| 27 | DIST_DIR="$(cd $DIST_DIR && pwd)" |
| 28 | export LINT_PRINT_STACKTRACE=true |
| 29 | |
Jeff Gaston | e72d230 | 2019-12-19 18:32:31 -0500 | [diff] [blame] | 30 | #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 Gaston | 0e3d19a | 2019-10-02 12:17:39 -0400 | [diff] [blame] | 34 | # 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 |
| 36 | fi |
| 37 | |
Aurimas Liutikas | 9979d07 | 2018-03-13 15:38:56 -0700 | [diff] [blame] | 38 | # ---------------------------------------------------------------------------- |
| 39 | |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 40 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 41 | |
| 42 | APP_NAME="Gradle" |
| 43 | APP_BASE_NAME=`basename "$0"` |
| 44 | |
| 45 | # Use the maximum available, or set MAX_FD != -1 to use that value. |
| 46 | MAX_FD="maximum" |
| 47 | |
| 48 | warn ( ) { |
| 49 | echo "$*" |
| 50 | } |
| 51 | |
| 52 | die ( ) { |
| 53 | echo |
| 54 | echo "$*" |
| 55 | echo |
| 56 | exit 1 |
| 57 | } |
| 58 | |
| 59 | # OS specific support (must be 'true' or 'false'). |
| 60 | cygwin=false |
| 61 | msys=false |
| 62 | darwin=false |
| 63 | case "`uname`" in |
| 64 | CYGWIN* ) |
| 65 | cygwin=true |
| 66 | ;; |
| 67 | Darwin* ) |
| 68 | darwin=true |
| 69 | ;; |
| 70 | MINGW* ) |
| 71 | msys=true |
| 72 | ;; |
| 73 | esac |
| 74 | |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 75 | # Attempt to set APP_HOME |
| 76 | # Resolve links: $0 may be a link |
| 77 | PRG="$0" |
| 78 | # Need this for relative symlinks. |
| 79 | while [ -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 |
| 87 | done |
| 88 | SAVED="`pwd`" |
Yigit Boyar | f77697d | 2016-08-16 10:55:36 -0700 | [diff] [blame] | 89 | cd "`dirname \"$PRG\"`/" >/dev/null |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 90 | APP_HOME="`pwd -P`" |
Yigit Boyar | f77697d | 2016-08-16 10:55:36 -0700 | [diff] [blame] | 91 | cd "$SAVED" >/dev/null |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 92 | |
| 93 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar |
| 94 | |
Jeff Gaston | 79a43f2 | 2019-04-09 16:19:12 -0400 | [diff] [blame] | 95 | # --------- androidx specific code needed for lint and java. ------------------ |
| 96 | |
Alan Viverette | d38b36c | 2017-02-01 16:45:31 -0500 | [diff] [blame] | 97 | # Pick the correct fullsdk for this OS. |
Alan Viverette | 7df63ff | 2017-03-06 13:12:24 -0500 | [diff] [blame] | 98 | if [ $darwin == "true" ]; then |
Alan Viverette | d38b36c | 2017-02-01 16:45:31 -0500 | [diff] [blame] | 99 | plat="darwin" |
| 100 | else |
| 101 | plat="linux" |
| 102 | fi |
| 103 | DEFAULT_JVM_OPTS="-DLINT_API_DATABASE=$APP_HOME/../../prebuilts/fullsdk-$plat/platform-tools/api/api-versions.xml" |
| 104 | |
Matthew Fraschilla | 6ab84fc3 | 2019-11-21 16:40:16 -0800 | [diff] [blame] | 105 | # Tests for lint checks default to using sdk defined by this variable. This removes a lot of |
| 106 | # setup from each lint module. |
| 107 | export ANDROID_HOME="$APP_HOME/../../prebuilts/fullsdk-$plat" |
Sergey Vasilinets | efab5eb | 2019-01-04 12:38:06 +0000 | [diff] [blame] | 108 | # override JAVA_HOME, because CI machines have it and it points to very old JDK |
Aurimas Liutikas | 4b897cb | 2019-10-14 13:25:08 -0700 | [diff] [blame] | 109 | export JAVA_HOME="$APP_HOME/../../prebuilts/jdk/jdk11/$plat-x86" |
| 110 | export JAVA_TOOLS_JAR="$APP_HOME/../../prebuilts/jdk/jdk8/$plat-x86/lib/tools.jar" |
| 111 | export STUDIO_GRADLE_JDK=$JAVA_HOME |
Oussama Ben Abdelbaki | f825eb5 | 2018-12-04 16:17:00 -0500 | [diff] [blame] | 112 | |
Jeff Gaston | 79a43f2 | 2019-04-09 16:19:12 -0400 | [diff] [blame] | 113 | # ---------------------------------------------------------------------------- |
| 114 | |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 115 | # Determine the Java command to use to start the JVM. |
| 116 | if [ -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 | |
| 126 | Please set the JAVA_HOME variable in your environment to match the |
| 127 | location of your Java installation." |
| 128 | fi |
| 129 | else |
| 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 | |
| 133 | Please set the JAVA_HOME variable in your environment to match the |
| 134 | location of your Java installation." |
| 135 | fi |
| 136 | |
| 137 | # Increase the maximum file descriptors if we can. |
| 138 | if [ "$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 |
| 151 | fi |
| 152 | |
| 153 | # For Darwin, add options to specify how the application appears in the dock |
| 154 | if $darwin; then |
| 155 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" |
| 156 | fi |
| 157 | |
| 158 | # For Cygwin, switch paths to Windows format before running java |
| 159 | if $cygwin ; then |
| 160 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` |
| 161 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` |
Yigit Boyar | f77697d | 2016-08-16 10:55:36 -0700 | [diff] [blame] | 162 | JAVACMD=`cygpath --unix "$JAVACMD"` |
Xavier Ducrohet | 0f3d903 | 2014-03-18 17:25:21 -0700 | [diff] [blame] | 163 | |
| 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 |
| 201 | fi |
| 202 | |
| 203 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules |
| 204 | function splitJvmOpts() { |
| 205 | JVM_OPTS=("$@") |
| 206 | } |
| 207 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS |
| 208 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" |
| 209 | |
Jeff Gaston | 826bdbe | 2019-11-20 14:56:24 -0500 | [diff] [blame] | 210 | #TODO: Remove HOME_SYSTEM_PROPERTY_ARGUMENT if https://github.com/gradle/gradle/issues/11433 gets fixed |
| 211 | HOME_SYSTEM_PROPERTY_ARGUMENT="" |
| 212 | if [ "$GRADLE_USER_HOME" != "" ]; then |
| 213 | HOME_SYSTEM_PROPERTY_ARGUMENT="-Duser.home=$GRADLE_USER_HOME" |
| 214 | fi |
Jeff Gaston | 38004a6 | 2019-12-11 15:43:10 -0500 | [diff] [blame] | 215 | if [ "$TMPDIR" != "" ]; then |
| 216 | TMPDIR_ARG="-Djava.io.tmpdir=$TMPDIR" |
| 217 | fi |
Jeff Gaston | 826bdbe | 2019-11-20 14:56:24 -0500 | [diff] [blame] | 218 | |
Jeff Gaston | 18c47d3 | 2020-01-27 18:00:28 -0500 | [diff] [blame] | 219 | function 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 Gaston | d54930d | 2020-02-13 17:25:02 -0500 | [diff] [blame] | 224 | cp ./hs_err* $DIST_DIR/ 2>/dev/null || true |
Jeff Gaston | 18c47d3 | 2020-01-27 18:00:28 -0500 | [diff] [blame] | 225 | fi |
| 226 | fi |
| 227 | } |
| 228 | |
Jeff Gaston | 224eb17 | 2020-01-09 12:31:47 -0500 | [diff] [blame] | 229 | function 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 Gaston | 18c47d3 | 2020-01-27 18:00:28 -0500 | [diff] [blame] | 233 | tryToDiagnosePossibleDaemonFailure |
Jeff Gaston | 224eb17 | 2020-01-09 12:31:47 -0500 | [diff] [blame] | 234 | # 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 Gaston | 6971329 | 2020-06-04 12:53:39 -0400 | [diff] [blame] | 239 | return 1 |
| 240 | fi |
| 241 | } |
| 242 | |
Jeff Gaston | 400ccb3 | 2020-06-08 16:44:58 -0400 | [diff] [blame] | 243 | if [[ " ${@} " =~ " -PdisallowExecution " ]]; then |
| 244 | echo "Passing '-PdisallowExecution' directly is forbidden. Did you mean -PverifyUpToDate ?" |
| 245 | echo "See TaskUpToDateValidator.java for more information" |
| 246 | exit 1 |
| 247 | fi |
| 248 | |
Jeff Gaston | e572f82 | 2020-06-12 14:04:48 -0400 | [diff] [blame] | 249 | runGradle "$@" |
Jeff Gaston | 224eb17 | 2020-01-09 12:31:47 -0500 | [diff] [blame] | 250 | # Check whether we were given the "-PverifyUpToDate" argument |
| 251 | if [[ " ${@} " =~ " -PverifyUpToDate " ]]; then |
Jeff Gaston | cefdeae | 2020-03-09 13:12:35 -0400 | [diff] [blame] | 252 | # Re-run Gradle, and find all tasks that are unexpectly out of date |
Jeff Gaston | d289794 | 2020-06-17 16:13:58 -0400 | [diff] [blame] | 253 | runGradle "$@" -PdisallowExecution --continue |
Jeff Gaston | b89c82b | 2019-08-21 16:24:09 -0400 | [diff] [blame] | 254 | fi |