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