envsetup.sh revision db2ecc798b731becccf2f02c4826b6679d06d790
1function hmm() {
2cat <<EOF
3Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
4- lunch:   lunch <product_name>-<build_variant>
5- tapas:   tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]
6- croot:   Changes directory to the top of the tree.
7- m:       Makes from the top of the tree.
8- mm:      Builds all of the modules in the current directory, but not their dependencies.
9- mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
10- mma:     Builds all of the modules in the current directory, and their dependencies.
11- mmma:    Builds all of the modules in the supplied directories, and their dependencies.
12- cgrep:   Greps on all local C/C++ files.
13- jgrep:   Greps on all local Java files.
14- resgrep: Greps on all local res/*.xml files.
15- godir:   Go to the directory containing a file.
16
17Look at the source to view more functions. The complete list is:
18EOF
19    T=$(gettop)
20    local A
21    A=""
22    for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
23      A="$A $i"
24    done
25    echo $A
26}
27
28# Get the value of a build variable as an absolute path.
29function get_abs_build_var()
30{
31    T=$(gettop)
32    if [ ! "$T" ]; then
33        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
34        return
35    fi
36    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
37      make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
38}
39
40# Get the exact value of a build variable.
41function get_build_var()
42{
43    T=$(gettop)
44    if [ ! "$T" ]; then
45        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
46        return
47    fi
48    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
49      make --no-print-directory -f build/core/config.mk dumpvar-$1)
50}
51
52# check to see if the supplied product is one we can build
53function check_product()
54{
55    T=$(gettop)
56    if [ ! "$T" ]; then
57        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
58        return
59    fi
60    CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
61        TARGET_PRODUCT=$1 \
62        TARGET_BUILD_VARIANT= \
63        TARGET_BUILD_TYPE= \
64        TARGET_BUILD_APPS= \
65        get_build_var TARGET_DEVICE > /dev/null
66    # hide successful answers, but allow the errors to show
67}
68
69VARIANT_CHOICES=(user userdebug eng)
70
71# check to see if the supplied variant is valid
72function check_variant()
73{
74    for v in ${VARIANT_CHOICES[@]}
75    do
76        if [ "$v" = "$1" ]
77        then
78            return 0
79        fi
80    done
81    return 1
82}
83
84function setpaths()
85{
86    T=$(gettop)
87    if [ ! "$T" ]; then
88        echo "Couldn't locate the top of the tree.  Try setting TOP."
89        return
90    fi
91
92    ##################################################################
93    #                                                                #
94    #              Read me before you modify this code               #
95    #                                                                #
96    #   This function sets ANDROID_BUILD_PATHS to what it is adding  #
97    #   to PATH, and the next time it is run, it removes that from   #
98    #   PATH.  This is required so lunch can be run more than once   #
99    #   and still have working paths.                                #
100    #                                                                #
101    ##################################################################
102
103    # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
104    # due to "C:\Program Files" being in the path.
105
106    # out with the old
107    if [ -n "$ANDROID_BUILD_PATHS" ] ; then
108        export PATH=${PATH/$ANDROID_BUILD_PATHS/}
109    fi
110    if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
111        export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
112        # strip leading ':', if any
113        export PATH=${PATH/:%/}
114    fi
115
116    # and in with the new
117    CODE_REVIEWS=
118    prebuiltdir=$(getprebuilt)
119    gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
120
121    # defined in core/config.mk
122    targetgccversion=$(get_build_var TARGET_GCC_VERSION)
123    export TARGET_GCC_VERSION=$targetgccversion
124
125    # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
126    export ANDROID_TOOLCHAIN=
127    export ANDROID_TOOLCHAIN_2ND_ARCH=
128    local ARCH=$(get_build_var TARGET_ARCH)
129    case $ARCH in
130        x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
131            ;;
132        x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
133            ;;
134        arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
135            ;;
136        arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
137               toolchaindir2=arm/arm-linux-androideabi-$targetgccversion/bin
138            ;;
139        mips) toolchaindir=mips/mipsel-linux-android-$targetgccversion/bin
140            ;;
141        mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
142            ;;
143        *)
144            echo "Can't find toolchain for unknown architecture: $ARCH"
145            toolchaindir=xxxxxxxxx
146            ;;
147    esac
148    if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
149        export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
150    fi
151
152    if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
153        export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
154    fi
155
156    unset ANDROID_KERNEL_TOOLCHAIN_PATH
157    case $ARCH in
158        arm)
159            # Legacy toolchain configuration used for ARM kernel compilation
160            toolchaindir=arm/arm-eabi-$targetgccversion/bin
161            if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
162                 ANDROID_KERNEL_TOOLCHAIN_PATH="$gccprebuiltdir/$toolchaindir"
163                 export ARM_EABI_TOOLCHAIN=$ANDROID_KERNEL_TOOLCHAIN_PATH
164            fi
165            ;;
166        mips) toolchaindir=mips/mips-eabi-4.4.3/bin
167            ;;
168        *)
169            # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
170            ;;
171    esac
172
173    export ANDROID_QTOOLS=$T/development/emulator/qtools
174    export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
175    export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_KERNEL_TOOLCHAIN_PATH$CODE_REVIEWS:$ANDROID_DEV_SCRIPTS:
176    export PATH=$ANDROID_BUILD_PATHS$PATH
177
178    unset ANDROID_JAVA_TOOLCHAIN
179    unset ANDROID_PRE_BUILD_PATHS
180    if [ -n "$JAVA_HOME" ]; then
181        export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
182        export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
183        export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
184    fi
185
186    unset ANDROID_PRODUCT_OUT
187    export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
188    export OUT=$ANDROID_PRODUCT_OUT
189
190    unset ANDROID_HOST_OUT
191    export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
192
193    # needed for processing samples collected by perf counters
194    unset OPROFILE_EVENTS_DIR
195    export OPROFILE_EVENTS_DIR=$T/external/oprofile/events
196
197    # needed for building linux on MacOS
198    # TODO: fix the path
199    #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
200}
201
202function printconfig()
203{
204    T=$(gettop)
205    if [ ! "$T" ]; then
206        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
207        return
208    fi
209    get_build_var report_config
210}
211
212function set_stuff_for_environment()
213{
214    settitle
215    set_java_home
216    setpaths
217    set_sequence_number
218
219    export ANDROID_BUILD_TOP=$(gettop)
220    # With this environment variable new GCC can apply colors to warnings/errors
221    export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
222}
223
224function set_sequence_number()
225{
226    export BUILD_ENV_SEQUENCE_NUMBER=10
227}
228
229function settitle()
230{
231    if [ "$STAY_OFF_MY_LAWN" = "" ]; then
232        local arch=$(gettargetarch)
233        local product=$TARGET_PRODUCT
234        local variant=$TARGET_BUILD_VARIANT
235        local apps=$TARGET_BUILD_APPS
236        if [ -z "$apps" ]; then
237            export PROMPT_COMMAND="echo -ne \"\033]0;[${arch}-${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
238        else
239            export PROMPT_COMMAND="echo -ne \"\033]0;[$arch $apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\""
240        fi
241    fi
242}
243
244function addcompletions()
245{
246    local T dir f
247
248    # Keep us from trying to run in something that isn't bash.
249    if [ -z "${BASH_VERSION}" ]; then
250        return
251    fi
252
253    # Keep us from trying to run in bash that's too old.
254    if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
255        return
256    fi
257
258    dir="sdk/bash_completion"
259    if [ -d ${dir} ]; then
260        for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
261            echo "including $f"
262            . $f
263        done
264    fi
265}
266
267function choosetype()
268{
269    echo "Build type choices are:"
270    echo "     1. release"
271    echo "     2. debug"
272    echo
273
274    local DEFAULT_NUM DEFAULT_VALUE
275    DEFAULT_NUM=1
276    DEFAULT_VALUE=release
277
278    export TARGET_BUILD_TYPE=
279    local ANSWER
280    while [ -z $TARGET_BUILD_TYPE ]
281    do
282        echo -n "Which would you like? ["$DEFAULT_NUM"] "
283        if [ -z "$1" ] ; then
284            read ANSWER
285        else
286            echo $1
287            ANSWER=$1
288        fi
289        case $ANSWER in
290        "")
291            export TARGET_BUILD_TYPE=$DEFAULT_VALUE
292            ;;
293        1)
294            export TARGET_BUILD_TYPE=release
295            ;;
296        release)
297            export TARGET_BUILD_TYPE=release
298            ;;
299        2)
300            export TARGET_BUILD_TYPE=debug
301            ;;
302        debug)
303            export TARGET_BUILD_TYPE=debug
304            ;;
305        *)
306            echo
307            echo "I didn't understand your response.  Please try again."
308            echo
309            ;;
310        esac
311        if [ -n "$1" ] ; then
312            break
313        fi
314    done
315
316    set_stuff_for_environment
317}
318
319#
320# This function isn't really right:  It chooses a TARGET_PRODUCT
321# based on the list of boards.  Usually, that gets you something
322# that kinda works with a generic product, but really, you should
323# pick a product by name.
324#
325function chooseproduct()
326{
327    if [ "x$TARGET_PRODUCT" != x ] ; then
328        default_value=$TARGET_PRODUCT
329    else
330        default_value=full
331    fi
332
333    export TARGET_PRODUCT=
334    local ANSWER
335    while [ -z "$TARGET_PRODUCT" ]
336    do
337        echo -n "Which product would you like? [$default_value] "
338        if [ -z "$1" ] ; then
339            read ANSWER
340        else
341            echo $1
342            ANSWER=$1
343        fi
344
345        if [ -z "$ANSWER" ] ; then
346            export TARGET_PRODUCT=$default_value
347        else
348            if check_product $ANSWER
349            then
350                export TARGET_PRODUCT=$ANSWER
351            else
352                echo "** Not a valid product: $ANSWER"
353            fi
354        fi
355        if [ -n "$1" ] ; then
356            break
357        fi
358    done
359
360    set_stuff_for_environment
361}
362
363function choosevariant()
364{
365    echo "Variant choices are:"
366    local index=1
367    local v
368    for v in ${VARIANT_CHOICES[@]}
369    do
370        # The product name is the name of the directory containing
371        # the makefile we found, above.
372        echo "     $index. $v"
373        index=$(($index+1))
374    done
375
376    local default_value=eng
377    local ANSWER
378
379    export TARGET_BUILD_VARIANT=
380    while [ -z "$TARGET_BUILD_VARIANT" ]
381    do
382        echo -n "Which would you like? [$default_value] "
383        if [ -z "$1" ] ; then
384            read ANSWER
385        else
386            echo $1
387            ANSWER=$1
388        fi
389
390        if [ -z "$ANSWER" ] ; then
391            export TARGET_BUILD_VARIANT=$default_value
392        elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
393            if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
394                export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
395            fi
396        else
397            if check_variant $ANSWER
398            then
399                export TARGET_BUILD_VARIANT=$ANSWER
400            else
401                echo "** Not a valid variant: $ANSWER"
402            fi
403        fi
404        if [ -n "$1" ] ; then
405            break
406        fi
407    done
408}
409
410function choosecombo()
411{
412    choosetype $1
413
414    echo
415    echo
416    chooseproduct $2
417
418    echo
419    echo
420    choosevariant $3
421
422    echo
423    set_stuff_for_environment
424    printconfig
425}
426
427# Clear this variable.  It will be built up again when the vendorsetup.sh
428# files are included at the end of this file.
429unset LUNCH_MENU_CHOICES
430function add_lunch_combo()
431{
432    local new_combo=$1
433    local c
434    for c in ${LUNCH_MENU_CHOICES[@]} ; do
435        if [ "$new_combo" = "$c" ] ; then
436            return
437        fi
438    done
439    LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
440}
441
442# add the default one here
443add_lunch_combo aosp_arm-eng
444add_lunch_combo aosp_arm64-eng
445add_lunch_combo aosp_mips-eng
446add_lunch_combo aosp_mips64-eng
447add_lunch_combo aosp_x86-eng
448add_lunch_combo aosp_x86_64-eng
449add_lunch_combo vbox_x86-eng
450
451function print_lunch_menu()
452{
453    local uname=$(uname)
454    echo
455    echo "You're building on" $uname
456    echo
457    echo "Lunch menu... pick a combo:"
458
459    local i=1
460    local choice
461    for choice in ${LUNCH_MENU_CHOICES[@]}
462    do
463        echo "     $i. $choice"
464        i=$(($i+1))
465    done
466
467    echo
468}
469
470function lunch()
471{
472    local answer
473
474    if [ "$1" ] ; then
475        answer=$1
476    else
477        print_lunch_menu
478        echo -n "Which would you like? [aosp_arm-eng] "
479        read answer
480    fi
481
482    local selection=
483
484    if [ -z "$answer" ]
485    then
486        selection=aosp_arm-eng
487    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
488    then
489        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
490        then
491            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
492        fi
493    elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
494    then
495        selection=$answer
496    fi
497
498    if [ -z "$selection" ]
499    then
500        echo
501        echo "Invalid lunch combo: $answer"
502        return 1
503    fi
504
505    export TARGET_BUILD_APPS=
506
507    local product=$(echo -n $selection | sed -e "s/-.*$//")
508    check_product $product
509    if [ $? -ne 0 ]
510    then
511        echo
512        echo "** Don't have a product spec for: '$product'"
513        echo "** Do you have the right repo manifest?"
514        product=
515    fi
516
517    local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
518    check_variant $variant
519    if [ $? -ne 0 ]
520    then
521        echo
522        echo "** Invalid variant: '$variant'"
523        echo "** Must be one of ${VARIANT_CHOICES[@]}"
524        variant=
525    fi
526
527    if [ -z "$product" -o -z "$variant" ]
528    then
529        echo
530        return 1
531    fi
532
533    export TARGET_PRODUCT=$product
534    export TARGET_BUILD_VARIANT=$variant
535    export TARGET_BUILD_TYPE=release
536
537    echo
538
539    set_stuff_for_environment
540    printconfig
541}
542
543# Tab completion for lunch.
544function _lunch()
545{
546    local cur prev opts
547    COMPREPLY=()
548    cur="${COMP_WORDS[COMP_CWORD]}"
549    prev="${COMP_WORDS[COMP_CWORD-1]}"
550
551    COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
552    return 0
553}
554complete -F _lunch lunch
555
556# Configures the build to build unbundled apps.
557# Run tapas with one ore more app names (from LOCAL_PACKAGE_NAME)
558function tapas()
559{
560    local arch=$(echo -n $(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5)$'))
561    local variant=$(echo -n $(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$'))
562    local apps=$(echo -n $(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5)$'))
563
564    if [ $(echo $arch | wc -w) -gt 1 ]; then
565        echo "tapas: Error: Multiple build archs supplied: $arch"
566        return
567    fi
568    if [ $(echo $variant | wc -w) -gt 1 ]; then
569        echo "tapas: Error: Multiple build variants supplied: $variant"
570        return
571    fi
572
573    local product=full
574    case $arch in
575      x86)   product=full_x86;;
576      mips)  product=full_mips;;
577      armv5) product=generic_armv5;;
578    esac
579    if [ -z "$variant" ]; then
580        variant=eng
581    fi
582    if [ -z "$apps" ]; then
583        apps=all
584    fi
585
586    export TARGET_PRODUCT=$product
587    export TARGET_BUILD_VARIANT=$variant
588    export TARGET_BUILD_TYPE=release
589    export TARGET_BUILD_APPS=$apps
590
591    set_stuff_for_environment
592    printconfig
593}
594
595function gettop
596{
597    local TOPFILE=build/core/envsetup.mk
598    if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
599        echo $TOP
600    else
601        if [ -f $TOPFILE ] ; then
602            # The following circumlocution (repeated below as well) ensures
603            # that we record the true directory name and not one that is
604            # faked up with symlink names.
605            PWD= /bin/pwd
606        else
607            local HERE=$PWD
608            T=
609            while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
610                \cd ..
611                T=`PWD= /bin/pwd -P`
612            done
613            \cd $HERE
614            if [ -f "$T/$TOPFILE" ]; then
615                echo $T
616            fi
617        fi
618    fi
619}
620
621# Return driver for "make", if any (eg. static analyzer)
622function getdriver()
623{
624    local T="$1"
625    test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
626    if [ -n "$WITH_STATIC_ANALYZER" ]; then
627        echo "\
628$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
629--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
630--status-bugs \
631--top=$T"
632    fi
633}
634
635function m()
636{
637    local T=$(gettop)
638    local DRV=$(getdriver $T)
639    if [ "$T" ]; then
640        $DRV make -C $T -f build/core/main.mk $@
641    else
642        echo "Couldn't locate the top of the tree.  Try setting TOP."
643    fi
644}
645
646function findmakefile()
647{
648    TOPFILE=build/core/envsetup.mk
649    local HERE=$PWD
650    T=
651    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
652        T=`PWD= /bin/pwd`
653        if [ -f "$T/Android.mk" ]; then
654            echo $T/Android.mk
655            \cd $HERE
656            return
657        fi
658        \cd ..
659    done
660    \cd $HERE
661}
662
663function mm()
664{
665    local T=$(gettop)
666    local DRV=$(getdriver $T)
667    # If we're sitting in the root of the build tree, just do a
668    # normal make.
669    if [ -f build/core/envsetup.mk -a -f Makefile ]; then
670        $DRV make $@
671    else
672        # Find the closest Android.mk file.
673        local M=$(findmakefile)
674        local MODULES=
675        local GET_INSTALL_PATH=
676        local ARGS=
677        # Remove the path to top as the makefilepath needs to be relative
678        local M=`echo $M|sed 's:'$T'/::'`
679        if [ ! "$T" ]; then
680            echo "Couldn't locate the top of the tree.  Try setting TOP."
681        elif [ ! "$M" ]; then
682            echo "Couldn't locate a makefile from the current directory."
683        else
684            for ARG in $@; do
685                case $ARG in
686                  GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
687                esac
688            done
689            if [ -n "$GET_INSTALL_PATH" ]; then
690              MODULES=
691              ARGS=GET-INSTALL-PATH
692            else
693              MODULES=all_modules
694              ARGS=$@
695            fi
696            ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
697        fi
698    fi
699}
700
701function mmm()
702{
703    local T=$(gettop)
704    local DRV=$(getdriver $T)
705    if [ "$T" ]; then
706        local MAKEFILE=
707        local MODULES=
708        local ARGS=
709        local DIR TO_CHOP
710        local GET_INSTALL_PATH=
711        local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
712        local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
713        for DIR in $DIRS ; do
714            MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
715            if [ "$MODULES" = "" ]; then
716                MODULES=all_modules
717            fi
718            DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
719            if [ -f $DIR/Android.mk ]; then
720                local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
721                local TO_CHOP=`expr $TO_CHOP + 1`
722                local START=`PWD= /bin/pwd`
723                local MFILE=`echo $START | cut -c${TO_CHOP}-`
724                if [ "$MFILE" = "" ] ; then
725                    MFILE=$DIR/Android.mk
726                else
727                    MFILE=$MFILE/$DIR/Android.mk
728                fi
729                MAKEFILE="$MAKEFILE $MFILE"
730            else
731                case $DIR in
732                  showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
733                  GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
734                  *) echo "No Android.mk in $DIR."; return 1;;
735                esac
736            fi
737        done
738        if [ -n "$GET_INSTALL_PATH" ]; then
739          ARGS=$GET_INSTALL_PATH
740          MODULES=
741        fi
742        ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
743    else
744        echo "Couldn't locate the top of the tree.  Try setting TOP."
745    fi
746}
747
748function mma()
749{
750  local T=$(gettop)
751  local DRV=$(getdriver $T)
752  if [ -f build/core/envsetup.mk -a -f Makefile ]; then
753    $DRV make $@
754  else
755    if [ ! "$T" ]; then
756      echo "Couldn't locate the top of the tree.  Try setting TOP."
757    fi
758    local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
759    $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
760  fi
761}
762
763function mmma()
764{
765  local T=$(gettop)
766  local DRV=$(getdriver $T)
767  if [ "$T" ]; then
768    local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
769    local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
770    local MY_PWD=`PWD= /bin/pwd`
771    if [ "$MY_PWD" = "$T" ]; then
772      MY_PWD=
773    else
774      MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
775    fi
776    local DIR=
777    local MODULE_PATHS=
778    local ARGS=
779    for DIR in $DIRS ; do
780      if [ -d $DIR ]; then
781        if [ "$MY_PWD" = "" ]; then
782          MODULE_PATHS="$MODULE_PATHS $DIR"
783        else
784          MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
785        fi
786      else
787        case $DIR in
788          showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
789          *) echo "Couldn't find directory $DIR"; return 1;;
790        esac
791      fi
792    done
793    $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS all_modules BUILD_MODULES_IN_PATHS="$MODULE_PATHS"
794  else
795    echo "Couldn't locate the top of the tree.  Try setting TOP."
796  fi
797}
798
799function croot()
800{
801    T=$(gettop)
802    if [ "$T" ]; then
803        \cd $(gettop)
804    else
805        echo "Couldn't locate the top of the tree.  Try setting TOP."
806    fi
807}
808
809function cproj()
810{
811    TOPFILE=build/core/envsetup.mk
812    local HERE=$PWD
813    T=
814    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
815        T=$PWD
816        if [ -f "$T/Android.mk" ]; then
817            \cd $T
818            return
819        fi
820        \cd ..
821    done
822    \cd $HERE
823    echo "can't find Android.mk"
824}
825
826# simplified version of ps; output in the form
827# <pid> <procname>
828function qpid() {
829    local prepend=''
830    local append=''
831    if [ "$1" = "--exact" ]; then
832        prepend=' '
833        append='$'
834        shift
835    elif [ "$1" = "--help" -o "$1" = "-h" ]; then
836		echo "usage: qpid [[--exact] <process name|pid>"
837		return 255
838	fi
839
840    local EXE="$1"
841    if [ "$EXE" ] ; then
842		qpid | \grep "$prepend$EXE$append"
843	else
844		adb shell ps \
845			| tr -d '\r' \
846			| sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
847	fi
848}
849
850function pid()
851{
852    local prepend=''
853    local append=''
854    if [ "$1" = "--exact" ]; then
855        prepend=' '
856        append='$'
857        shift
858    fi
859    local EXE="$1"
860    if [ "$EXE" ] ; then
861        local PID=`adb shell ps \
862            | tr -d '\r' \
863            | \grep "$prepend$EXE$append" \
864            | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
865        echo "$PID"
866    else
867        echo "usage: pid [--exact] <process name>"
868		return 255
869    fi
870}
871
872# systemstack - dump the current stack trace of all threads in the system process
873# to the usual ANR traces file
874function systemstack()
875{
876    stacks system_server
877}
878
879function stacks()
880{
881    if [[ $1 =~ ^[0-9]+$ ]] ; then
882        local PID="$1"
883    elif [ "$1" ] ; then
884        local PIDLIST="$(pid $1)"
885        if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
886            local PID="$PIDLIST"
887        elif [ "$PIDLIST" ] ; then
888            echo "more than one process: $1"
889        else
890            echo "no such process: $1"
891        fi
892    else
893        echo "usage: stacks [pid|process name]"
894    fi
895
896    if [ "$PID" ] ; then
897        # Determine whether the process is native
898        if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
899            # Dump stacks of Dalvik process
900            local TRACES=/data/anr/traces.txt
901            local ORIG=/data/anr/traces.orig
902            local TMP=/data/anr/traces.tmp
903
904            # Keep original traces to avoid clobbering
905            adb shell mv $TRACES $ORIG
906
907            # Make sure we have a usable file
908            adb shell touch $TRACES
909            adb shell chmod 666 $TRACES
910
911            # Dump stacks and wait for dump to finish
912            adb shell kill -3 $PID
913            adb shell notify $TRACES >/dev/null
914
915            # Restore original stacks, and show current output
916            adb shell mv $TRACES $TMP
917            adb shell mv $ORIG $TRACES
918            adb shell cat $TMP
919        else
920            # Dump stacks of native process
921            adb shell debuggerd -b $PID
922        fi
923    fi
924}
925
926function gdbwrapper()
927{
928    local GDB_CMD="$1"
929    shift 1
930    $GDB_CMD -x "$@"
931}
932
933# process the symbolic link of /proc/$PID/exe and use the host file tool to
934# determine whether it is a 32-bit or 64-bit executable. It returns "" or "64"
935# which can be conveniently used as suffix.
936function is64bit()
937{
938    local PID="$1"
939    if [ "$PID" ] ; then
940        local EXE=`adb shell ls -l /proc/$PID/exe \
941                   | tr -d '\r' \
942                   | cut -d'>' -f2 \
943                   | tr -d ' ' \
944                   | cut -d'/' -f4`
945
946        local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
947        local IS64BIT=`file $OUT_EXE_SYMBOLS/$EXE | grep "64-bit"`
948        if [ "$IS64BIT" != "" ]; then
949            echo "64"
950        else
951            echo ""
952        fi
953    else
954        echo ""
955    fi
956}
957
958# gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
959# executable, set up the approriate gdbserver, then invokes the proper host
960# gdb.
961function gdbclient()
962{
963   local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
964   local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
965   local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
966   local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
967   local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
968   local ARCH=$(get_build_var TARGET_ARCH)
969   local GDB
970   case "$ARCH" in
971       arm) GDB=arm-linux-androideabi-gdb;;
972       arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
973       mips) GDB=mipsel-linux-android-gdb;;
974       mips64) GDB=mipsel-linux-android-gdb;;
975       x86) GDB=x86_64-linux-android-gdb;;
976       x86_64) GDB=x86_64-linux-android-gdb;;
977       *) echo "Unknown arch $ARCH"; return 1;;
978   esac
979
980   if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
981       local EXE="$1"
982       if [ "$EXE" ] ; then
983           EXE=$1
984       else
985           EXE="app_process"
986       fi
987
988       local PORT="$2"
989       if [ "$PORT" ] ; then
990           PORT=$2
991       else
992           PORT=":5039"
993       fi
994
995       local PID="$3"
996       if [ "$PID" ] ; then
997           if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
998               PID=`pid $3`
999               if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1000                   # that likely didn't work because of returning multiple processes
1001                   # try again, filtering by root processes (don't contain colon)
1002                   PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1003                   if [[ ! "$PID" =~ ^[0-9]+$ ]]
1004                   then
1005                       echo "Couldn't resolve '$3' to single PID"
1006                       return 1
1007                   else
1008                       echo ""
1009                       echo "WARNING: multiple processes matching '$3' observed, using root process"
1010                       echo ""
1011                   fi
1012               fi
1013           fi
1014           adb forward "tcp$PORT" "tcp$PORT"
1015           local USE64BIT="$(is64bit $PID)"
1016           adb shell gdbserver$USE64BIT $PORT --attach $PID &
1017           sleep 2
1018       else
1019               echo ""
1020               echo "If you haven't done so already, do this first on the device:"
1021               echo "    gdbserver $PORT /system/bin/$EXE"
1022                   echo " or"
1023               echo "    gdbserver $PORT --attach <PID>"
1024               echo ""
1025       fi
1026
1027       echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1028       echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS$USE64BIT:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx"
1029       echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1030       echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1031       echo >>"$OUT_ROOT/gdbclient.cmds" ""
1032
1033       local WHICH_GDB=
1034       # 64-bit exe found
1035       if [ "$USE64BIT" != "" ] ; then
1036           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1037       # 32-bit exe / 32-bit platform
1038       elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1039           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1040       # 32-bit exe / 64-bit platform
1041       else
1042           WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1043       fi
1044       gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1045  else
1046       echo "Unable to determine build system output dir."
1047   fi
1048
1049}
1050
1051case `uname -s` in
1052    Darwin)
1053        function sgrep()
1054        {
1055            find -E . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
1056        }
1057
1058        ;;
1059    *)
1060        function sgrep()
1061        {
1062            find . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
1063        }
1064        ;;
1065esac
1066
1067function gettargetarch
1068{
1069    get_build_var TARGET_ARCH
1070}
1071
1072function jgrep()
1073{
1074    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
1075}
1076
1077function cgrep()
1078{
1079    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
1080}
1081
1082function resgrep()
1083{
1084    for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
1085}
1086
1087function mangrep()
1088{
1089    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1090}
1091
1092function sepgrep()
1093{
1094    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d -print0 | xargs -0 grep --color -n -r --exclude-dir=\.git "$@"
1095}
1096
1097case `uname -s` in
1098    Darwin)
1099        function mgrep()
1100        {
1101            find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
1102        }
1103
1104        function treegrep()
1105        {
1106            find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
1107        }
1108
1109        ;;
1110    *)
1111        function mgrep()
1112        {
1113            find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
1114        }
1115
1116        function treegrep()
1117        {
1118            find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
1119        }
1120
1121        ;;
1122esac
1123
1124function getprebuilt
1125{
1126    get_abs_build_var ANDROID_PREBUILTS
1127}
1128
1129function tracedmdump()
1130{
1131    T=$(gettop)
1132    if [ ! "$T" ]; then
1133        echo "Couldn't locate the top of the tree.  Try setting TOP."
1134        return
1135    fi
1136    local prebuiltdir=$(getprebuilt)
1137    local arch=$(gettargetarch)
1138    local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1139
1140    local TRACE=$1
1141    if [ ! "$TRACE" ] ; then
1142        echo "usage:  tracedmdump  tracename"
1143        return
1144    fi
1145
1146    if [ ! -r "$KERNEL" ] ; then
1147        echo "Error: cannot find kernel: '$KERNEL'"
1148        return
1149    fi
1150
1151    local BASETRACE=$(basename $TRACE)
1152    if [ "$BASETRACE" = "$TRACE" ] ; then
1153        TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1154    fi
1155
1156    echo "post-processing traces..."
1157    rm -f $TRACE/qtrace.dexlist
1158    post_trace $TRACE
1159    if [ $? -ne 0 ]; then
1160        echo "***"
1161        echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
1162        echo "***"
1163        return
1164    fi
1165    echo "generating dexlist output..."
1166    /bin/ls $ANDROID_PRODUCT_OUT/system/framework/*.jar $ANDROID_PRODUCT_OUT/system/app/*.apk $ANDROID_PRODUCT_OUT/data/app/*.apk 2>/dev/null | xargs dexlist > $TRACE/qtrace.dexlist
1167    echo "generating dmtrace data..."
1168    q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1169    echo "generating html file..."
1170    dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1171    echo "done, see $TRACE/dmtrace.html for details"
1172    echo "or run:"
1173    echo "    traceview $TRACE/dmtrace"
1174}
1175
1176# communicate with a running device or emulator, set up necessary state,
1177# and run the hat command.
1178function runhat()
1179{
1180    # process standard adb options
1181    local adbTarget=""
1182    if [ "$1" = "-d" -o "$1" = "-e" ]; then
1183        adbTarget=$1
1184        shift 1
1185    elif [ "$1" = "-s" ]; then
1186        adbTarget="$1 $2"
1187        shift 2
1188    fi
1189    local adbOptions=${adbTarget}
1190    #echo adbOptions = ${adbOptions}
1191
1192    # runhat options
1193    local targetPid=$1
1194
1195    if [ "$targetPid" = "" ]; then
1196        echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1197        return
1198    fi
1199
1200    # confirm hat is available
1201    if [ -z $(which hat) ]; then
1202        echo "hat is not available in this configuration."
1203        return
1204    fi
1205
1206    # issue "am" command to cause the hprof dump
1207    local sdcard=$(adb ${adbOptions} shell echo -n '$EXTERNAL_STORAGE')
1208    local devFile=$sdcard/hprof-$targetPid
1209    #local devFile=/data/local/hprof-$targetPid
1210    echo "Poking $targetPid and waiting for data..."
1211    echo "Storing data at $devFile"
1212    adb ${adbOptions} shell am dumpheap $targetPid $devFile
1213    echo "Press enter when logcat shows \"hprof: heap dump completed\""
1214    echo -n "> "
1215    read
1216
1217    local localFile=/tmp/$$-hprof
1218
1219    echo "Retrieving file $devFile..."
1220    adb ${adbOptions} pull $devFile $localFile
1221
1222    adb ${adbOptions} shell rm $devFile
1223
1224    echo "Running hat on $localFile"
1225    echo "View the output by pointing your browser at http://localhost:7000/"
1226    echo ""
1227    hat -JXmx512m $localFile
1228}
1229
1230function getbugreports()
1231{
1232    local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1233
1234    if [ ! "$reports" ]; then
1235        echo "Could not locate any bugreports."
1236        return
1237    fi
1238
1239    local report
1240    for report in ${reports[@]}
1241    do
1242        echo "/sdcard/bugreports/${report}"
1243        adb pull /sdcard/bugreports/${report} ${report}
1244        gunzip ${report}
1245    done
1246}
1247
1248function getsdcardpath()
1249{
1250    adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1251}
1252
1253function getscreenshotpath()
1254{
1255    echo "$(getsdcardpath)/Pictures/Screenshots"
1256}
1257
1258function getlastscreenshot()
1259{
1260    local screenshot_path=$(getscreenshotpath)
1261    local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1262    if [ "$screenshot" = "" ]; then
1263        echo "No screenshots found."
1264        return
1265    fi
1266    echo "${screenshot}"
1267    adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1268}
1269
1270function startviewserver()
1271{
1272    local port=4939
1273    if [ $# -gt 0 ]; then
1274            port=$1
1275    fi
1276    adb shell service call window 1 i32 $port
1277}
1278
1279function stopviewserver()
1280{
1281    adb shell service call window 2
1282}
1283
1284function isviewserverstarted()
1285{
1286    adb shell service call window 3
1287}
1288
1289function key_home()
1290{
1291    adb shell input keyevent 3
1292}
1293
1294function key_back()
1295{
1296    adb shell input keyevent 4
1297}
1298
1299function key_menu()
1300{
1301    adb shell input keyevent 82
1302}
1303
1304function smoketest()
1305{
1306    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1307        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
1308        return
1309    fi
1310    T=$(gettop)
1311    if [ ! "$T" ]; then
1312        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1313        return
1314    fi
1315
1316    (\cd "$T" && mmm tests/SmokeTest) &&
1317      adb uninstall com.android.smoketest > /dev/null &&
1318      adb uninstall com.android.smoketest.tests > /dev/null &&
1319      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1320      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1321      adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1322}
1323
1324# simple shortcut to the runtest command
1325function runtest()
1326{
1327    T=$(gettop)
1328    if [ ! "$T" ]; then
1329        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1330        return
1331    fi
1332    ("$T"/development/testrunner/runtest.py $@)
1333}
1334
1335function godir () {
1336    if [[ -z "$1" ]]; then
1337        echo "Usage: godir <regex>"
1338        return
1339    fi
1340    T=$(gettop)
1341    if [[ ! -f $T/filelist ]]; then
1342        echo -n "Creating index..."
1343        (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1344        echo " Done"
1345        echo ""
1346    fi
1347    local lines
1348    lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1349    if [[ ${#lines[@]} = 0 ]]; then
1350        echo "Not found"
1351        return
1352    fi
1353    local pathname
1354    local choice
1355    if [[ ${#lines[@]} > 1 ]]; then
1356        while [[ -z "$pathname" ]]; do
1357            local index=1
1358            local line
1359            for line in ${lines[@]}; do
1360                printf "%6s %s\n" "[$index]" $line
1361                index=$(($index + 1))
1362            done
1363            echo
1364            echo -n "Select one: "
1365            unset choice
1366            read choice
1367            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1368                echo "Invalid choice"
1369                continue
1370            fi
1371            pathname=${lines[$(($choice-1))]}
1372        done
1373    else
1374        pathname=${lines[0]}
1375    fi
1376    \cd $T/$pathname
1377}
1378
1379# Force JAVA_HOME to point to java 1.7 or java 1.6  if it isn't already set.
1380#
1381# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
1382# For some reason, installing the JDK doesn't make it show up in the
1383# JavaVM.framework/Versions/1.7/ folder.
1384function set_java_home() {
1385    # Clear the existing JAVA_HOME value if we set it ourselves, so that
1386    # we can reset it later, depending on the value of EXPERIMENTAL_USE_JAVA7.
1387    #
1388    # If we don't do this, the JAVA_HOME value set by the first call to
1389    # build/envsetup.sh will persist forever.
1390    if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
1391      export JAVA_HOME=""
1392    fi
1393
1394    if [ ! "$JAVA_HOME" ]; then
1395      if [ ! "$EXPERIMENTAL_USE_JAVA7" ]; then
1396        case `uname -s` in
1397            Darwin)
1398                export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
1399                ;;
1400            *)
1401                export JAVA_HOME=/usr/lib/jvm/java-6-sun
1402                ;;
1403        esac
1404      else
1405        case `uname -s` in
1406            Darwin)
1407                export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
1408                ;;
1409            *)
1410                export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
1411                ;;
1412        esac
1413      fi
1414
1415      # Keep track of the fact that we set JAVA_HOME ourselves, so that
1416      # we can change it on the next envsetup.sh, if required.
1417      export ANDROID_SET_JAVA_HOME=true
1418    fi
1419}
1420
1421# Print colored exit condition
1422function pez {
1423    "$@"
1424    local retval=$?
1425    if [ $retval -ne 0 ]
1426    then
1427        echo -e "\e[0;31mFAILURE\e[00m"
1428    else
1429        echo -e "\e[0;32mSUCCESS\e[00m"
1430    fi
1431    return $retval
1432}
1433
1434if [ "x$SHELL" != "x/bin/bash" ]; then
1435    case `ps -o command -p $$` in
1436        *bash*)
1437            ;;
1438        *)
1439            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
1440            ;;
1441    esac
1442fi
1443
1444# Execute the contents of any vendorsetup.sh files we can find.
1445for f in `test -d device && find device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \
1446         `test -d vendor && find vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`
1447do
1448    echo "including $f"
1449    . $f
1450done
1451unset f
1452
1453addcompletions
1454