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