envsetup.sh revision a50ef1c12441d9eccfa75409c1392befaf2abed5
1function help() {
2cat <<EOF
3Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
4- croot:   Changes directory to the top of the tree.
5- m:       Makes from the top of the tree.
6- mm:      Builds all of the modules in the current directory.
7- mmm:     Builds all of the modules in the supplied directories.
8- cgrep:   Greps on all local C/C++ files.
9- jgrep:   Greps on all local Java files.
10- resgrep: Greps on all local res/*.xml files.
11- godir:   Go to the directory containing a file.
12
13Look at the source to view more functions. The complete list is:
14EOF
15    T=$(gettop)
16    local A
17    A=""
18    for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
19      A="$A $i"
20    done
21    echo $A
22}
23
24# Get the value of a build variable as an absolute path.
25function get_abs_build_var()
26{
27    T=$(gettop)
28    if [ ! "$T" ]; then
29        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
30        return
31    fi
32    (cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
33      make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1)
34}
35
36# Get the exact value of a build variable.
37function get_build_var()
38{
39    T=$(gettop)
40    if [ ! "$T" ]; then
41        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
42        return
43    fi
44    CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
45      make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
46}
47
48# check to see if the supplied product is one we can build
49function check_product()
50{
51    T=$(gettop)
52    if [ ! "$T" ]; then
53        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
54        return
55    fi
56    CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
57        TARGET_PRODUCT=$1 \
58        TARGET_BUILD_VARIANT= \
59        TARGET_BUILD_TYPE= \
60        TARGET_BUILD_APPS= \
61        get_build_var TARGET_DEVICE > /dev/null
62    # hide successful answers, but allow the errors to show
63}
64
65VARIANT_CHOICES=(user userdebug eng)
66
67# check to see if the supplied variant is valid
68function check_variant()
69{
70    for v in ${VARIANT_CHOICES[@]}
71    do
72        if [ "$v" = "$1" ]
73        then
74            return 0
75        fi
76    done
77    return 1
78}
79
80function setpaths()
81{
82    T=$(gettop)
83    if [ ! "$T" ]; then
84        echo "Couldn't locate the top of the tree.  Try setting TOP."
85        return
86    fi
87
88    ##################################################################
89    #                                                                #
90    #              Read me before you modify this code               #
91    #                                                                #
92    #   This function sets ANDROID_BUILD_PATHS to what it is adding  #
93    #   to PATH, and the next time it is run, it removes that from   #
94    #   PATH.  This is required so lunch can be run more than once   #
95    #   and still have working paths.                                #
96    #                                                                #
97    ##################################################################
98
99    # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
100    # due to "C:\Program Files" being in the path.
101
102    # out with the old
103    if [ -n "$ANDROID_BUILD_PATHS" ] ; then
104        export PATH=${PATH/$ANDROID_BUILD_PATHS/}
105    fi
106    if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
107        export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
108        # strip trailing ':', if any
109        export PATH=${PATH/%:/}
110    fi
111
112    # and in with the new
113    CODE_REVIEWS=
114    prebuiltdir=$(getprebuilt)
115    gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
116
117    # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
118    export ANDROID_EABI_TOOLCHAIN=
119    case $(get_build_var TARGET_ARCH) in
120        x86) toolchaindir=x86/i686-android-linux-4.4.3/bin
121            ;;
122        arm|*) toolchaindir=arm/arm-linux-androideabi-4.6/bin
123            ;;
124    esac
125    if [ -d "$prebuiltdir/$toolchaindir" ]; then
126        export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/$toolchaindir
127    fi
128
129    export ARM_EABI_TOOLCHAIN=
130    toolchaindir=toolchain/arm-eabi-4.4.3/bin
131    if [ -d "$prebuiltdir/$toolchaindir" ]; then
132        export ARM_EABI_TOOLCHAIN=$prebuiltdir/$toolchaindir
133    fi
134
135    export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
136    export ANDROID_QTOOLS=$T/development/emulator/qtools
137    export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ARM_EABI_TOOLCHAIN$CODE_REVIEWS
138    export PATH=$PATH$ANDROID_BUILD_PATHS
139
140    unset ANDROID_JAVA_TOOLCHAIN
141    unset ANDROID_PRE_BUILD_PATHS
142    if [ -n "$JAVA_HOME" ]; then
143        export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
144        export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
145        export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
146    fi
147
148    unset ANDROID_PRODUCT_OUT
149    export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
150    export OUT=$ANDROID_PRODUCT_OUT
151
152    unset ANDROID_HOST_OUT
153    export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
154
155    # needed for building linux on MacOS
156    # TODO: fix the path
157    #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
158}
159
160function printconfig()
161{
162    T=$(gettop)
163    if [ ! "$T" ]; then
164        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
165        return
166    fi
167    get_build_var report_config
168}
169
170function set_stuff_for_environment()
171{
172    settitle
173    set_java_home
174    setpaths
175    set_sequence_number
176
177    export ANDROID_BUILD_TOP=$(gettop)
178}
179
180function set_sequence_number()
181{
182    export BUILD_ENV_SEQUENCE_NUMBER=10
183}
184
185function settitle()
186{
187    if [ "$STAY_OFF_MY_LAWN" = "" ]; then
188        local product=$TARGET_PRODUCT
189        local variant=$TARGET_BUILD_VARIANT
190        local apps=$TARGET_BUILD_APPS
191        if [ -z "$apps" ]; then
192            export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
193        else
194            export PROMPT_COMMAND="echo -ne \"\033]0;[$apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\""
195        fi
196    fi
197}
198
199function addcompletions()
200{
201    local T dir f
202
203    # Keep us from trying to run in something that isn't bash.
204    if [ -z "${BASH_VERSION}" ]; then
205        return
206    fi
207
208    # Keep us from trying to run in bash that's too old.
209    if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
210        return
211    fi
212
213    dir="sdk/bash_completion"
214    if [ -d ${dir} ]; then
215        for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
216            echo "including $f"
217            . $f
218        done
219    fi
220}
221
222function choosetype()
223{
224    echo "Build type choices are:"
225    echo "     1. release"
226    echo "     2. debug"
227    echo
228
229    local DEFAULT_NUM DEFAULT_VALUE
230    DEFAULT_NUM=1
231    DEFAULT_VALUE=release
232
233    export TARGET_BUILD_TYPE=
234    local ANSWER
235    while [ -z $TARGET_BUILD_TYPE ]
236    do
237        echo -n "Which would you like? ["$DEFAULT_NUM"] "
238        if [ -z "$1" ] ; then
239            read ANSWER
240        else
241            echo $1
242            ANSWER=$1
243        fi
244        case $ANSWER in
245        "")
246            export TARGET_BUILD_TYPE=$DEFAULT_VALUE
247            ;;
248        1)
249            export TARGET_BUILD_TYPE=release
250            ;;
251        release)
252            export TARGET_BUILD_TYPE=release
253            ;;
254        2)
255            export TARGET_BUILD_TYPE=debug
256            ;;
257        debug)
258            export TARGET_BUILD_TYPE=debug
259            ;;
260        *)
261            echo
262            echo "I didn't understand your response.  Please try again."
263            echo
264            ;;
265        esac
266        if [ -n "$1" ] ; then
267            break
268        fi
269    done
270
271    set_stuff_for_environment
272}
273
274#
275# This function isn't really right:  It chooses a TARGET_PRODUCT
276# based on the list of boards.  Usually, that gets you something
277# that kinda works with a generic product, but really, you should
278# pick a product by name.
279#
280function chooseproduct()
281{
282    if [ "x$TARGET_PRODUCT" != x ] ; then
283        default_value=$TARGET_PRODUCT
284    else
285        default_value=full
286    fi
287
288    export TARGET_PRODUCT=
289    local ANSWER
290    while [ -z "$TARGET_PRODUCT" ]
291    do
292        echo -n "Which product would you like? [$default_value] "
293        if [ -z "$1" ] ; then
294            read ANSWER
295        else
296            echo $1
297            ANSWER=$1
298        fi
299
300        if [ -z "$ANSWER" ] ; then
301            export TARGET_PRODUCT=$default_value
302        else
303            if check_product $ANSWER
304            then
305                export TARGET_PRODUCT=$ANSWER
306            else
307                echo "** Not a valid product: $ANSWER"
308            fi
309        fi
310        if [ -n "$1" ] ; then
311            break
312        fi
313    done
314
315    set_stuff_for_environment
316}
317
318function choosevariant()
319{
320    echo "Variant choices are:"
321    local index=1
322    local v
323    for v in ${VARIANT_CHOICES[@]}
324    do
325        # The product name is the name of the directory containing
326        # the makefile we found, above.
327        echo "     $index. $v"
328        index=$(($index+1))
329    done
330
331    local default_value=eng
332    local ANSWER
333
334    export TARGET_BUILD_VARIANT=
335    while [ -z "$TARGET_BUILD_VARIANT" ]
336    do
337        echo -n "Which would you like? [$default_value] "
338        if [ -z "$1" ] ; then
339            read ANSWER
340        else
341            echo $1
342            ANSWER=$1
343        fi
344
345        if [ -z "$ANSWER" ] ; then
346            export TARGET_BUILD_VARIANT=$default_value
347        elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
348            if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
349                export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
350            fi
351        else
352            if check_variant $ANSWER
353            then
354                export TARGET_BUILD_VARIANT=$ANSWER
355            else
356                echo "** Not a valid variant: $ANSWER"
357            fi
358        fi
359        if [ -n "$1" ] ; then
360            break
361        fi
362    done
363}
364
365function choosecombo()
366{
367    choosetype $1
368
369    echo
370    echo
371    chooseproduct $2
372
373    echo
374    echo
375    choosevariant $3
376
377    echo
378    set_stuff_for_environment
379    printconfig
380}
381
382# Clear this variable.  It will be built up again when the vendorsetup.sh
383# files are included at the end of this file.
384unset LUNCH_MENU_CHOICES
385function add_lunch_combo()
386{
387    local new_combo=$1
388    local c
389    for c in ${LUNCH_MENU_CHOICES[@]} ; do
390        if [ "$new_combo" = "$c" ] ; then
391            return
392        fi
393    done
394    LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
395}
396
397# add the default one here
398add_lunch_combo full-eng
399add_lunch_combo full_x86-eng
400add_lunch_combo vbox_x86-eng
401
402function print_lunch_menu()
403{
404    local uname=$(uname)
405    echo
406    echo "You're building on" $uname
407    echo
408    echo "Lunch menu... pick a combo:"
409
410    local i=1
411    local choice
412    for choice in ${LUNCH_MENU_CHOICES[@]}
413    do
414        echo "     $i. $choice"
415        i=$(($i+1))
416    done
417
418    echo
419}
420
421function lunch()
422{
423    local answer
424
425    if [ "$1" ] ; then
426        answer=$1
427    else
428        print_lunch_menu
429        echo -n "Which would you like? [full-eng] "
430        read answer
431    fi
432
433    local selection=
434
435    if [ -z "$answer" ]
436    then
437        selection=full-eng
438    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
439    then
440        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
441        then
442            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
443        fi
444    elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
445    then
446        selection=$answer
447    fi
448
449    if [ -z "$selection" ]
450    then
451        echo
452        echo "Invalid lunch combo: $answer"
453        return 1
454    fi
455
456    export TARGET_BUILD_APPS=
457
458    local product=$(echo -n $selection | sed -e "s/-.*$//")
459    check_product $product
460    if [ $? -ne 0 ]
461    then
462        echo
463        echo "** Don't have a product spec for: '$product'"
464        echo "** Do you have the right repo manifest?"
465        product=
466    fi
467
468    local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
469    check_variant $variant
470    if [ $? -ne 0 ]
471    then
472        echo
473        echo "** Invalid variant: '$variant'"
474        echo "** Must be one of ${VARIANT_CHOICES[@]}"
475        variant=
476    fi
477
478    if [ -z "$product" -o -z "$variant" ]
479    then
480        echo
481        return 1
482    fi
483
484    export TARGET_PRODUCT=$product
485    export TARGET_BUILD_VARIANT=$variant
486    export TARGET_BUILD_TYPE=release
487
488    echo
489
490    set_stuff_for_environment
491    printconfig
492}
493
494# Tab completion for lunch.
495function _lunch()
496{
497    local cur prev opts
498    COMPREPLY=()
499    cur="${COMP_WORDS[COMP_CWORD]}"
500    prev="${COMP_WORDS[COMP_CWORD-1]}"
501
502    COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
503    return 0
504}
505complete -F _lunch lunch
506
507# Configures the build to build unbundled apps.
508# Run tapas with one ore more app names (from LOCAL_PACKAGE_NAME)
509function tapas()
510{
511    local variant=$(echo -n $(echo $* | xargs -n 1 echo | grep -E '^(user|userdebug|eng)$'))
512    local apps=$(echo -n $(echo $* | xargs -n 1 echo | grep -E -v '^(user|userdebug|eng)$'))
513
514    if [ $(echo $variant | wc -w) -gt 1 ]; then
515        echo "tapas: Error: Multiple build variants supplied: $variant"
516        return
517    fi
518    if [ -z "$variant" ]; then
519        variant=eng
520    fi
521    if [ -z "$apps" ]; then
522        apps=all
523    fi
524
525    export TARGET_PRODUCT=full
526    export TARGET_BUILD_VARIANT=$variant
527    export TARGET_BUILD_TYPE=release
528    export TARGET_BUILD_APPS=$apps
529
530    set_stuff_for_environment
531    printconfig
532}
533
534function gettop
535{
536    local TOPFILE=build/core/envsetup.mk
537    if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
538        echo $TOP
539    else
540        if [ -f $TOPFILE ] ; then
541            # The following circumlocution (repeated below as well) ensures
542            # that we record the true directory name and not one that is
543            # faked up with symlink names.
544            PWD= /bin/pwd
545        else
546            # We redirect cd to /dev/null in case it's aliased to
547            # a command that prints something as a side-effect
548            # (like pushd)
549            local HERE=$PWD
550            T=
551            while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
552                cd .. > /dev/null
553                T=`PWD= /bin/pwd`
554            done
555            cd $HERE > /dev/null
556            if [ -f "$T/$TOPFILE" ]; then
557                echo $T
558            fi
559        fi
560    fi
561}
562
563function m()
564{
565    T=$(gettop)
566    if [ "$T" ]; then
567        make -C $T $@
568    else
569        echo "Couldn't locate the top of the tree.  Try setting TOP."
570    fi
571}
572
573function findmakefile()
574{
575    TOPFILE=build/core/envsetup.mk
576    # We redirect cd to /dev/null in case it's aliased to
577    # a command that prints something as a side-effect
578    # (like pushd)
579    local HERE=$PWD
580    T=
581    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
582        T=$PWD
583        if [ -f "$T/Android.mk" ]; then
584            echo $T/Android.mk
585            cd $HERE > /dev/null
586            return
587        fi
588        cd .. > /dev/null
589    done
590    cd $HERE > /dev/null
591}
592
593function mm()
594{
595    # If we're sitting in the root of the build tree, just do a
596    # normal make.
597    if [ -f build/core/envsetup.mk -a -f Makefile ]; then
598        make $@
599    else
600        # Find the closest Android.mk file.
601        T=$(gettop)
602        local M=$(findmakefile)
603        # Remove the path to top as the makefilepath needs to be relative
604        local M=`echo $M|sed 's:'$T'/::'`
605        if [ ! "$T" ]; then
606            echo "Couldn't locate the top of the tree.  Try setting TOP."
607        elif [ ! "$M" ]; then
608            echo "Couldn't locate a makefile from the current directory."
609        else
610            ONE_SHOT_MAKEFILE=$M make -C $T all_modules $@
611        fi
612    fi
613}
614
615function mmm()
616{
617    T=$(gettop)
618    if [ "$T" ]; then
619        local MAKEFILE=
620        local ARGS=
621        local DIR TO_CHOP
622        local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
623        local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
624        for DIR in $DIRS ; do
625            DIR=`echo $DIR | sed -e 's:/$::'`
626            if [ -f $DIR/Android.mk ]; then
627                TO_CHOP=`(cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
628                TO_CHOP=`expr $TO_CHOP + 1`
629                START=`PWD= /bin/pwd`
630                MFILE=`echo $START | cut -c${TO_CHOP}-`
631                if [ "$MFILE" = "" ] ; then
632                    MFILE=$DIR/Android.mk
633                else
634                    MFILE=$MFILE/$DIR/Android.mk
635                fi
636                MAKEFILE="$MAKEFILE $MFILE"
637            else
638                if [ "$DIR" = snod ]; then
639                    ARGS="$ARGS snod"
640                elif [ "$DIR" = showcommands ]; then
641                    ARGS="$ARGS showcommands"
642                elif [ "$DIR" = dist ]; then
643                    ARGS="$ARGS dist"
644                elif [ "$DIR" = incrementaljavac ]; then
645                    ARGS="$ARGS incrementaljavac"
646                else
647                    echo "No Android.mk in $DIR."
648                    return 1
649                fi
650            fi
651        done
652        ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS all_modules $ARGS
653    else
654        echo "Couldn't locate the top of the tree.  Try setting TOP."
655    fi
656}
657
658function croot()
659{
660    T=$(gettop)
661    if [ "$T" ]; then
662        cd $(gettop)
663    else
664        echo "Couldn't locate the top of the tree.  Try setting TOP."
665    fi
666}
667
668function cproj()
669{
670    TOPFILE=build/core/envsetup.mk
671    # We redirect cd to /dev/null in case it's aliased to
672    # a command that prints something as a side-effect
673    # (like pushd)
674    local HERE=$PWD
675    T=
676    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
677        T=$PWD
678        if [ -f "$T/Android.mk" ]; then
679            cd $T
680            return
681        fi
682        cd .. > /dev/null
683    done
684    cd $HERE > /dev/null
685    echo "can't find Android.mk"
686}
687
688function pid()
689{
690   local EXE="$1"
691   if [ "$EXE" ] ; then
692       local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
693       echo "$PID"
694   else
695       echo "usage: pid name"
696   fi
697}
698
699# systemstack - dump the current stack trace of all threads in the system process
700# to the usual ANR traces file
701function systemstack()
702{
703    adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
704}
705
706function gdbclient()
707{
708   local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
709   local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
710   local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
711   local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
712   local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
713   if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
714       local EXE="$1"
715       if [ "$EXE" ] ; then
716           EXE=$1
717       else
718           EXE="app_process"
719       fi
720
721       local PORT="$2"
722       if [ "$PORT" ] ; then
723           PORT=$2
724       else
725           PORT=":5039"
726       fi
727
728       local PID
729       local PROG="$3"
730       if [ "$PROG" ] ; then
731           if [[ "$PROG" =~ ^[0-9]+$ ]] ; then
732               PID="$3"
733           else
734               PID=`pid $3`
735           fi
736           adb forward "tcp$PORT" "tcp$PORT"
737           adb shell gdbserver $PORT --attach $PID &
738           sleep 2
739       else
740               echo ""
741               echo "If you haven't done so already, do this first on the device:"
742               echo "    gdbserver $PORT /system/bin/$EXE"
743                   echo " or"
744               echo "    gdbserver $PORT --attach $PID"
745               echo ""
746       fi
747
748       echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
749       echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
750       echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
751       echo >>"$OUT_ROOT/gdbclient.cmds" ""
752
753       $ANDROID_EABI_TOOLCHAIN/*-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
754  else
755       echo "Unable to determine build system output dir."
756   fi
757
758}
759
760case `uname -s` in
761    Darwin)
762        function sgrep()
763        {
764            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 "$@"
765        }
766
767        ;;
768    *)
769        function sgrep()
770        {
771            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 "$@"
772        }
773        ;;
774esac
775
776function jgrep()
777{
778    find . -name .repo -prune -o -name .git -prune -o  -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
779}
780
781function cgrep()
782{
783    find . -name .repo -prune -o -name .git -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
784}
785
786function resgrep()
787{
788    for dir in `find . -name .repo -prune -o -name .git -prune -o -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
789}
790
791case `uname -s` in
792    Darwin)
793        function mgrep()
794        {
795            find -E . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
796        }
797
798        function treegrep()
799        {
800            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 "$@"
801        }
802
803        ;;
804    *)
805        function mgrep()
806        {
807            find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
808        }
809
810        function treegrep()
811        {
812            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 "$@"
813        }
814
815        ;;
816esac
817
818function getprebuilt
819{
820    get_abs_build_var ANDROID_PREBUILTS
821}
822
823function tracedmdump()
824{
825    T=$(gettop)
826    if [ ! "$T" ]; then
827        echo "Couldn't locate the top of the tree.  Try setting TOP."
828        return
829    fi
830    local prebuiltdir=$(getprebuilt)
831    local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
832
833    local TRACE=$1
834    if [ ! "$TRACE" ] ; then
835        echo "usage:  tracedmdump  tracename"
836        return
837    fi
838
839    if [ ! -r "$KERNEL" ] ; then
840        echo "Error: cannot find kernel: '$KERNEL'"
841        return
842    fi
843
844    local BASETRACE=$(basename $TRACE)
845    if [ "$BASETRACE" = "$TRACE" ] ; then
846        TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
847    fi
848
849    echo "post-processing traces..."
850    rm -f $TRACE/qtrace.dexlist
851    post_trace $TRACE
852    if [ $? -ne 0 ]; then
853        echo "***"
854        echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
855        echo "***"
856        return
857    fi
858    echo "generating dexlist output..."
859    /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
860    echo "generating dmtrace data..."
861    q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
862    echo "generating html file..."
863    dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
864    echo "done, see $TRACE/dmtrace.html for details"
865    echo "or run:"
866    echo "    traceview $TRACE/dmtrace"
867}
868
869# communicate with a running device or emulator, set up necessary state,
870# and run the hat command.
871function runhat()
872{
873    # process standard adb options
874    local adbTarget=""
875    if [ "$1" = "-d" -o "$1" = "-e" ]; then
876        adbTarget=$1
877        shift 1
878    elif [ "$1" = "-s" ]; then
879        adbTarget="$1 $2"
880        shift 2
881    fi
882    local adbOptions=${adbTarget}
883    echo adbOptions = ${adbOptions}
884
885    # runhat options
886    local targetPid=$1
887
888    if [ "$targetPid" = "" ]; then
889        echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
890        return
891    fi
892
893    # confirm hat is available
894    if [ -z $(which hat) ]; then
895        echo "hat is not available in this configuration."
896        return
897    fi
898
899    # issue "am" command to cause the hprof dump
900    local devFile=/sdcard/hprof-$targetPid
901    echo "Poking $targetPid and waiting for data..."
902    adb ${adbOptions} shell am dumpheap $targetPid $devFile
903    echo "Press enter when logcat shows \"hprof: heap dump completed\""
904    echo -n "> "
905    read
906
907    local localFile=/tmp/$$-hprof
908
909    echo "Retrieving file $devFile..."
910    adb ${adbOptions} pull $devFile $localFile
911
912    adb ${adbOptions} shell rm $devFile
913
914    echo "Running hat on $localFile"
915    echo "View the output by pointing your browser at http://localhost:7000/"
916    echo ""
917    hat $localFile
918}
919
920function getbugreports()
921{
922    local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
923
924    if [ ! "$reports" ]; then
925        echo "Could not locate any bugreports."
926        return
927    fi
928
929    local report
930    for report in ${reports[@]}
931    do
932        echo "/sdcard/bugreports/${report}"
933        adb pull /sdcard/bugreports/${report} ${report}
934        gunzip ${report}
935    done
936}
937
938function startviewserver()
939{
940    local port=4939
941    if [ $# -gt 0 ]; then
942            port=$1
943    fi
944    adb shell service call window 1 i32 $port
945}
946
947function stopviewserver()
948{
949    adb shell service call window 2
950}
951
952function isviewserverstarted()
953{
954    adb shell service call window 3
955}
956
957function key_home()
958{
959    adb shell input keyevent 3
960}
961
962function key_back()
963{
964    adb shell input keyevent 4
965}
966
967function key_menu()
968{
969    adb shell input keyevent 82
970}
971
972function smoketest()
973{
974    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
975        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
976        return
977    fi
978    T=$(gettop)
979    if [ ! "$T" ]; then
980        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
981        return
982    fi
983
984    (cd "$T" && mmm tests/SmokeTest) &&
985      adb uninstall com.android.smoketest > /dev/null &&
986      adb uninstall com.android.smoketest.tests > /dev/null &&
987      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
988      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
989      adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
990}
991
992# simple shortcut to the runtest command
993function runtest()
994{
995    T=$(gettop)
996    if [ ! "$T" ]; then
997        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
998        return
999    fi
1000    ("$T"/development/testrunner/runtest.py $@)
1001}
1002
1003function godir () {
1004    if [[ -z "$1" ]]; then
1005        echo "Usage: godir <regex>"
1006        return
1007    fi
1008    T=$(gettop)
1009    if [[ ! -f $T/filelist ]]; then
1010        echo -n "Creating index..."
1011        (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1012        echo " Done"
1013        echo ""
1014    fi
1015    local lines
1016    lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1017    if [[ ${#lines[@]} = 0 ]]; then
1018        echo "Not found"
1019        return
1020    fi
1021    local pathname
1022    local choice
1023    if [[ ${#lines[@]} > 1 ]]; then
1024        while [[ -z "$pathname" ]]; do
1025            local index=1
1026            local line
1027            for line in ${lines[@]}; do
1028                printf "%6s %s\n" "[$index]" $line
1029                index=$(($index + 1))
1030            done
1031            echo
1032            echo -n "Select one: "
1033            unset choice
1034            read choice
1035            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1036                echo "Invalid choice"
1037                continue
1038            fi
1039            pathname=${lines[$(($choice-1))]}
1040        done
1041    else
1042        pathname=${lines[0]}
1043    fi
1044    cd $T/$pathname
1045}
1046
1047# Force JAVA_HOME to point to java 1.6 if it isn't already set
1048function set_java_home() {
1049    if [ ! "$JAVA_HOME" ]; then
1050        case `uname -s` in
1051            Darwin)
1052                export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
1053                ;;
1054            *)
1055                export JAVA_HOME=/usr/lib/jvm/java-6-sun
1056                ;;
1057        esac
1058    fi
1059}
1060
1061if [ "x$SHELL" != "x/bin/bash" ]; then
1062    case `ps -o command -p $$` in
1063        *bash*)
1064            ;;
1065        *)
1066            echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
1067            ;;
1068    esac
1069fi
1070
1071# Execute the contents of any vendorsetup.sh files we can find.
1072for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/*/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`
1073do
1074    echo "including $f"
1075    . $f
1076done
1077unset f
1078
1079addcompletions
1080