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