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