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