envsetup.sh revision 7a569a6282e44a5c7f90be76690ad839738b5162
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           if [[ $EXE =~ ^[^/].* ]] ; then
1000               EXE="system/bin/"$EXE
1001           fi
1002       else
1003           EXE="app_process"
1004       fi
1005
1006       local PORT="$2"
1007       if [ "$PORT" ] ; then
1008           PORT=$2
1009       else
1010           PORT=":5039"
1011       fi
1012
1013       local PID="$3"
1014       if [ "$PID" ] ; then
1015           if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1016               PID=`pid $3`
1017               if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1018                   # that likely didn't work because of returning multiple processes
1019                   # try again, filtering by root processes (don't contain colon)
1020                   PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1021                   if [[ ! "$PID" =~ ^[0-9]+$ ]]
1022                   then
1023                       echo "Couldn't resolve '$3' to single PID"
1024                       return 1
1025                   else
1026                       echo ""
1027                       echo "WARNING: multiple processes matching '$3' observed, using root process"
1028                       echo ""
1029                   fi
1030               fi
1031           fi
1032           adb forward "tcp$PORT" "tcp$PORT"
1033           local USE64BIT="$(is64bit $PID)"
1034           adb shell gdbserver$USE64BIT $PORT --attach $PID &
1035           sleep 2
1036       else
1037               echo ""
1038               echo "If you haven't done so already, do this first on the device:"
1039               echo "    gdbserver $PORT /system/bin/$EXE"
1040                   echo " or"
1041               echo "    gdbserver $PORT --attach <PID>"
1042               echo ""
1043       fi
1044
1045       OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1046
1047       echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1048       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"
1049       echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1050       echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1051       echo >>"$OUT_ROOT/gdbclient.cmds" ""
1052
1053       local WHICH_GDB=
1054       # 64-bit exe found
1055       if [ "$USE64BIT" != "" ] ; then
1056           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1057       # 32-bit exe / 32-bit platform
1058       elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1059           WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1060       # 32-bit exe / 64-bit platform
1061       else
1062           WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1063       fi
1064
1065       gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1066  else
1067       echo "Unable to determine build system output dir."
1068   fi
1069
1070}
1071
1072case `uname -s` in
1073    Darwin)
1074        function sgrep()
1075        {
1076            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 "$@"
1077        }
1078
1079        ;;
1080    *)
1081        function sgrep()
1082        {
1083            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 "$@"
1084        }
1085        ;;
1086esac
1087
1088function gettargetarch
1089{
1090    get_build_var TARGET_ARCH
1091}
1092
1093function jgrep()
1094{
1095    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
1096}
1097
1098function cgrep()
1099{
1100    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 "$@"
1101}
1102
1103function resgrep()
1104{
1105    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;
1106}
1107
1108function mangrep()
1109{
1110    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1111}
1112
1113function sepgrep()
1114{
1115    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 "$@"
1116}
1117
1118case `uname -s` in
1119    Darwin)
1120        function mgrep()
1121        {
1122            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 "$@"
1123        }
1124
1125        function treegrep()
1126        {
1127            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 "$@"
1128        }
1129
1130        ;;
1131    *)
1132        function mgrep()
1133        {
1134            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 "$@"
1135        }
1136
1137        function treegrep()
1138        {
1139            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 "$@"
1140        }
1141
1142        ;;
1143esac
1144
1145function getprebuilt
1146{
1147    get_abs_build_var ANDROID_PREBUILTS
1148}
1149
1150function tracedmdump()
1151{
1152    T=$(gettop)
1153    if [ ! "$T" ]; then
1154        echo "Couldn't locate the top of the tree.  Try setting TOP."
1155        return
1156    fi
1157    local prebuiltdir=$(getprebuilt)
1158    local arch=$(gettargetarch)
1159    local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1160
1161    local TRACE=$1
1162    if [ ! "$TRACE" ] ; then
1163        echo "usage:  tracedmdump  tracename"
1164        return
1165    fi
1166
1167    if [ ! -r "$KERNEL" ] ; then
1168        echo "Error: cannot find kernel: '$KERNEL'"
1169        return
1170    fi
1171
1172    local BASETRACE=$(basename $TRACE)
1173    if [ "$BASETRACE" = "$TRACE" ] ; then
1174        TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1175    fi
1176
1177    echo "post-processing traces..."
1178    rm -f $TRACE/qtrace.dexlist
1179    post_trace $TRACE
1180    if [ $? -ne 0 ]; then
1181        echo "***"
1182        echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
1183        echo "***"
1184        return
1185    fi
1186    echo "generating dexlist output..."
1187    /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
1188    echo "generating dmtrace data..."
1189    q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1190    echo "generating html file..."
1191    dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1192    echo "done, see $TRACE/dmtrace.html for details"
1193    echo "or run:"
1194    echo "    traceview $TRACE/dmtrace"
1195}
1196
1197# communicate with a running device or emulator, set up necessary state,
1198# and run the hat command.
1199function runhat()
1200{
1201    # process standard adb options
1202    local adbTarget=""
1203    if [ "$1" = "-d" -o "$1" = "-e" ]; then
1204        adbTarget=$1
1205        shift 1
1206    elif [ "$1" = "-s" ]; then
1207        adbTarget="$1 $2"
1208        shift 2
1209    fi
1210    local adbOptions=${adbTarget}
1211    #echo adbOptions = ${adbOptions}
1212
1213    # runhat options
1214    local targetPid=$1
1215
1216    if [ "$targetPid" = "" ]; then
1217        echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1218        return
1219    fi
1220
1221    # confirm hat is available
1222    if [ -z $(which hat) ]; then
1223        echo "hat is not available in this configuration."
1224        return
1225    fi
1226
1227    # issue "am" command to cause the hprof dump
1228    local sdcard=$(adb ${adbOptions} shell echo -n '$EXTERNAL_STORAGE')
1229    local devFile=$sdcard/hprof-$targetPid
1230    #local devFile=/data/local/hprof-$targetPid
1231    echo "Poking $targetPid and waiting for data..."
1232    echo "Storing data at $devFile"
1233    adb ${adbOptions} shell am dumpheap $targetPid $devFile
1234    echo "Press enter when logcat shows \"hprof: heap dump completed\""
1235    echo -n "> "
1236    read
1237
1238    local localFile=/tmp/$$-hprof
1239
1240    echo "Retrieving file $devFile..."
1241    adb ${adbOptions} pull $devFile $localFile
1242
1243    adb ${adbOptions} shell rm $devFile
1244
1245    echo "Running hat on $localFile"
1246    echo "View the output by pointing your browser at http://localhost:7000/"
1247    echo ""
1248    hat -JXmx512m $localFile
1249}
1250
1251function getbugreports()
1252{
1253    local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1254
1255    if [ ! "$reports" ]; then
1256        echo "Could not locate any bugreports."
1257        return
1258    fi
1259
1260    local report
1261    for report in ${reports[@]}
1262    do
1263        echo "/sdcard/bugreports/${report}"
1264        adb pull /sdcard/bugreports/${report} ${report}
1265        gunzip ${report}
1266    done
1267}
1268
1269function getsdcardpath()
1270{
1271    adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1272}
1273
1274function getscreenshotpath()
1275{
1276    echo "$(getsdcardpath)/Pictures/Screenshots"
1277}
1278
1279function getlastscreenshot()
1280{
1281    local screenshot_path=$(getscreenshotpath)
1282    local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1283    if [ "$screenshot" = "" ]; then
1284        echo "No screenshots found."
1285        return
1286    fi
1287    echo "${screenshot}"
1288    adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1289}
1290
1291function startviewserver()
1292{
1293    local port=4939
1294    if [ $# -gt 0 ]; then
1295            port=$1
1296    fi
1297    adb shell service call window 1 i32 $port
1298}
1299
1300function stopviewserver()
1301{
1302    adb shell service call window 2
1303}
1304
1305function isviewserverstarted()
1306{
1307    adb shell service call window 3
1308}
1309
1310function key_home()
1311{
1312    adb shell input keyevent 3
1313}
1314
1315function key_back()
1316{
1317    adb shell input keyevent 4
1318}
1319
1320function key_menu()
1321{
1322    adb shell input keyevent 82
1323}
1324
1325function smoketest()
1326{
1327    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1328        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
1329        return
1330    fi
1331    T=$(gettop)
1332    if [ ! "$T" ]; then
1333        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1334        return
1335    fi
1336
1337    (\cd "$T" && mmm tests/SmokeTest) &&
1338      adb uninstall com.android.smoketest > /dev/null &&
1339      adb uninstall com.android.smoketest.tests > /dev/null &&
1340      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1341      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1342      adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1343}
1344
1345# simple shortcut to the runtest command
1346function runtest()
1347{
1348    T=$(gettop)
1349    if [ ! "$T" ]; then
1350        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1351        return
1352    fi
1353    ("$T"/development/testrunner/runtest.py $@)
1354}
1355
1356function godir () {
1357    if [[ -z "$1" ]]; then
1358        echo "Usage: godir <regex>"
1359        return
1360    fi
1361    T=$(gettop)
1362    if [[ ! -f $T/filelist ]]; then
1363        echo -n "Creating index..."
1364        (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1365        echo " Done"
1366        echo ""
1367    fi
1368    local lines
1369    lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1370    if [[ ${#lines[@]} = 0 ]]; then
1371        echo "Not found"
1372        return
1373    fi
1374    local pathname
1375    local choice
1376    if [[ ${#lines[@]} > 1 ]]; then
1377        while [[ -z "$pathname" ]]; do
1378            local index=1
1379            local line
1380            for line in ${lines[@]}; do
1381                printf "%6s %s\n" "[$index]" $line
1382                index=$(($index + 1))
1383            done
1384            echo
1385            echo -n "Select one: "
1386            unset choice
1387            read choice
1388            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1389                echo "Invalid choice"
1390                continue
1391            fi
1392            pathname=${lines[$(($choice-1))]}
1393        done
1394    else
1395        pathname=${lines[0]}
1396    fi
1397    \cd $T/$pathname
1398}
1399
1400# Force JAVA_HOME to point to java 1.7 or java 1.6  if it isn't already set.
1401#
1402# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
1403# For some reason, installing the JDK doesn't make it show up in the
1404# JavaVM.framework/Versions/1.7/ folder.
1405function set_java_home() {
1406    # Clear the existing JAVA_HOME value if we set it ourselves, so that
1407    # we can reset it later, depending on the version of java the build
1408    # system needs.
1409    #
1410    # If we don't do this, the JAVA_HOME value set by the first call to
1411    # build/envsetup.sh will persist forever.
1412    if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
1413      export JAVA_HOME=""
1414    fi
1415
1416    if [ ! "$JAVA_HOME" ]; then
1417      if [ -n "$LEGACY_USE_JAVA6" ]; then
1418        case `uname -s` in
1419            Darwin)
1420                export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
1421                ;;
1422            *)
1423                export JAVA_HOME=/usr/lib/jvm/java-6-sun
1424                ;;
1425        esac
1426      else
1427        case `uname -s` in
1428            Darwin)
1429                export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
1430                ;;
1431            *)
1432                export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
1433                ;;
1434        esac
1435      fi
1436
1437      # Keep track of the fact that we set JAVA_HOME ourselves, so that
1438      # we can change it on the next envsetup.sh, if required.
1439      export ANDROID_SET_JAVA_HOME=true
1440    fi
1441}
1442
1443# Print colored exit condition
1444function pez {
1445    "$@"
1446    local retval=$?
1447    if [ $retval -ne 0 ]
1448    then
1449        echo -e "\e[0;31mFAILURE\e[00m"
1450    else
1451        echo -e "\e[0;32mSUCCESS\e[00m"
1452    fi
1453    return $retval
1454}
1455
1456if [ "x$SHELL" != "x/bin/bash" ]; then
1457    case `ps -o command -p $$` in
1458        *bash*)
1459            ;;
1460        *)
1461            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
1462            ;;
1463    esac
1464fi
1465
1466# Execute the contents of any vendorsetup.sh files we can find.
1467for f in `test -d device && find device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null` \
1468         `test -d vendor && find vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null`
1469do
1470    echo "including $f"
1471    . $f
1472done
1473unset f
1474
1475addcompletions
1476