1#!/usr/bin/env bash
2
3##############################################################################
4##
5##  Gradle start up script for UN*X
6##
7##############################################################################
8
9# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10DEFAULT_JVM_OPTS=""
11
12APP_NAME="Gradle"
13APP_BASE_NAME=`basename "$0"`
14
15# Use the maximum available, or set MAX_FD != -1 to use that value.
16MAX_FD="maximum"
17
18warn ( ) {
19    echo "$*"
20}
21
22die ( ) {
23    echo
24    echo "$*"
25    echo
26    exit 1
27}
28
29# OS specific support (must be 'true' or 'false').
30cygwin=false
31msys=false
32darwin=false
33case "`uname`" in
34  CYGWIN* )
35    cygwin=true
36    ;;
37  Darwin* )
38    darwin=true
39    ;;
40  MINGW* )
41    msys=true
42    ;;
43esac
44
45# For Cygwin, ensure paths are in UNIX format before anything is touched.
46if $cygwin ; then
47    [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48fi
49
50# Attempt to set APP_HOME
51# Resolve links: $0 may be a link
52PRG="$0"
53# Need this for relative symlinks.
54# TODO(jpd): b/15773596 - This is broken because we symlinked
55# gradlew and the build server can't clean it up. Don't resolve
56# links.
57#while [ -h "$PRG" ] ; do
58#    ls=`ls -ld "$PRG"`
59#    link=`expr "$ls" : '.*-> \(.*\)$'`
60#    if expr "$link" : '/.*' > /dev/null; then
61#        PRG="$link"
62#    else
63#        PRG=`dirname "$PRG"`"/$link"
64#    fi
65#done
66SAVED="`pwd`"
67cd "`dirname \"$PRG\"`/" >&-
68APP_HOME="`pwd -P`"
69cd "$SAVED" >&-
70
71if [ $(basename `pwd`) == "gradle" ]; then
72    echo "This cannot be run here. It should be copied to the root of the platform."
73    exit
74else
75    CLASSPATH=./gradle/wrapper/gradle-wrapper.jar
76fi
77
78# Determine the Java command to use to start the JVM.
79if [ -n "$JAVA_HOME" ] ; then
80    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
81        # IBM's JDK on AIX uses strange locations for the executables
82        JAVACMD="$JAVA_HOME/jre/sh/java"
83    else
84        JAVACMD="$JAVA_HOME/bin/java"
85    fi
86    if [ ! -x "$JAVACMD" ] ; then
87        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
88
89Please set the JAVA_HOME variable in your environment to match the
90location of your Java installation."
91    fi
92else
93    JAVACMD="java"
94    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
95
96Please set the JAVA_HOME variable in your environment to match the
97location of your Java installation."
98fi
99
100# Increase the maximum file descriptors if we can.
101if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
102    MAX_FD_LIMIT=`ulimit -H -n`
103    if [ $? -eq 0 ] ; then
104        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
105            MAX_FD="$MAX_FD_LIMIT"
106        fi
107        ulimit -n $MAX_FD
108        if [ $? -ne 0 ] ; then
109            warn "Could not set maximum file descriptor limit: $MAX_FD"
110        fi
111    else
112        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
113    fi
114fi
115
116# For Darwin, add options to specify how the application appears in the dock
117if $darwin; then
118    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
119fi
120
121# For Cygwin, switch paths to Windows format before running java
122if $cygwin ; then
123    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
124    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
125
126    # We build the pattern for arguments to be converted via cygpath
127    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
128    SEP=""
129    for dir in $ROOTDIRSRAW ; do
130        ROOTDIRS="$ROOTDIRS$SEP$dir"
131        SEP="|"
132    done
133    OURCYGPATTERN="(^($ROOTDIRS))"
134    # Add a user-defined pattern to the cygpath arguments
135    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
136        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
137    fi
138    # Now convert the arguments - kludge to limit ourselves to /bin/sh
139    i=0
140    for arg in "$@" ; do
141        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
142        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option
143
144        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
145            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
146        else
147            eval `echo args$i`="\"$arg\""
148        fi
149        i=$((i+1))
150    done
151    case $i in
152        (0) set -- ;;
153        (1) set -- "$args0" ;;
154        (2) set -- "$args0" "$args1" ;;
155        (3) set -- "$args0" "$args1" "$args2" ;;
156        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
157        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
158        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
159        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
160        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
161        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
162    esac
163fi
164
165# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
166function splitJvmOpts() {
167    JVM_OPTS=("$@")
168}
169eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
170JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
171
172# For reproducible builds, always use the SDKs stored in source control
173if $darwin; then
174    ANDROID_HOME="$APP_HOME/prebuilts/fullsdk/darwin"
175else
176    ANDROID_HOME="$APP_HOME/prebuilts/fullsdk/linux"
177fi
178export ANDROID_HOME
179
180# Change the project's .gradle to the android out dir.
181ANDROID_OUT_ROOT="$APP_HOME/out"
182if [[ -n "$OUT_DIR" ]]; then
183    ANDROID_OUT_ROOT="$OUT_DIR"
184fi
185
186ANDROID_CACHE_DIR="$ANDROID_OUT_ROOT/gradle/.gradle"
187
188# Prevent excess parallelization on the build servers, as it slows the
189# build to a crawl
190if [[ "$1" == --parallel-threads=* ]] && [[ "$2" == buildForBuildServer* ]]; then
191    set -- "--parallel-threads=4" "$2"
192fi
193
194# Change the local user directories to be under the android out dir
195export GRADLE_USER_HOME="$ANDROID_OUT_ROOT/gradle/.gradle"
196export M2_HOME="$ANDROID_OUT_ROOT/gradle/.m2"
197
198exec "$JAVACMD" "${JVM_OPTS[@]}" \
199    -classpath "$CLASSPATH" \
200    org.gradle.wrapper.GradleWrapperMain \
201    --project-cache-dir=$ANDROID_CACHE_DIR \
202    -Dorg.gradle.jvmargs="-Xmx4096m -XX:MaxPermSize=1024m" \
203    --configure-on-demand \
204    "$@"
205