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