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