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