OSDN Git Service

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