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