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