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