OSDN Git Service

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