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