OSDN Git Service

generic_x86: save logcat to /data/log.txt if debug.logcat=1
[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 add_lunch_combo generic_x86-eng
424
425 # if we're on linux, add the simulator.  There is a special case
426 # in lunch to deal with the simulator
427 if [ "$(uname)" = "Linux" ] ; then
428     add_lunch_combo simulator
429 fi
430
431 function print_lunch_menu()
432 {
433     local uname=$(uname)
434     echo
435     echo "You're building on" $uname
436     echo
437     echo "Lunch menu... pick a combo:"
438
439     local i=1
440     local choice
441     for choice in ${LUNCH_MENU_CHOICES[@]}
442     do
443         echo "     $i. $choice"
444         i=$(($i+1))
445     done
446
447     echo
448 }
449
450 function lunch()
451 {
452     local answer
453
454     if [ "$1" ] ; then
455         answer=$1
456     else
457         print_lunch_menu
458         echo -n "Which would you like? [generic-eng] "
459         read answer
460     fi
461
462     local selection=
463
464     if [ -z "$answer" ]
465     then
466         selection=generic-eng
467     elif [ "$answer" = "simulator" ]
468     then
469         selection=simulator
470     elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
471     then
472         if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
473         then
474             selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
475         fi
476     elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
477     then
478         selection=$answer
479     fi
480
481     if [ -z "$selection" ]
482     then
483         echo
484         echo "Invalid lunch combo: $answer"
485         return 1
486     fi
487
488     export TARGET_BUILD_APPS=
489
490     # special case the simulator
491     if [ "$selection" = "simulator" ]
492     then
493         export TARGET_PRODUCT=sim
494         export TARGET_BUILD_VARIANT=eng
495         export TARGET_SIMULATOR=true
496         export TARGET_BUILD_TYPE=debug
497     else
498         local product=$(echo -n $selection | sed -e "s/-.*$//")
499         check_product $product
500         if [ $? -ne 0 ]
501         then
502             echo
503             echo "** Don't have a product spec for: '$product'"
504             echo "** Do you have the right repo manifest?"
505             product=
506         fi
507
508         local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
509         check_variant $variant
510         if [ $? -ne 0 ]
511         then
512             echo
513             echo "** Invalid variant: '$variant'"
514             echo "** Must be one of ${VARIANT_CHOICES[@]}"
515             variant=
516         fi
517
518         if [ -z "$product" -o -z "$variant" ]
519         then
520             echo
521             return 1
522         fi
523
524         export TARGET_PRODUCT=$product
525         export TARGET_BUILD_VARIANT=$variant
526         export TARGET_SIMULATOR=false
527         export TARGET_BUILD_TYPE=release
528     fi # !simulator
529
530     echo
531
532     set_stuff_for_environment
533     printconfig
534 }
535
536 # Configures the build to build unbundled apps.
537 # Run tapas with one ore more app names (from LOCAL_PACKAGE_NAME)
538 function tapas()
539 {
540     local variant=$(echo -n $(echo $* | xargs -n 1 echo | grep -E '^(user|userdebug|eng)$'))
541     local apps=$(echo -n $(echo $* | xargs -n 1 echo | grep -E -v '^(user|userdebug|eng)$'))
542
543     if [ $(echo $variant | wc -w) -gt 1 ]; then
544         echo "tapas: Error: Multiple build variants supplied: $variant"
545         return
546     fi
547     if [ -z "$variant" ]; then
548         variant=eng
549     fi
550     if [ -z "$apps" ]; then
551         apps=all
552     fi
553
554     export TARGET_PRODUCT=generic
555     export TARGET_BUILD_VARIANT=$variant
556     export TARGET_SIMULATOR=false
557     export TARGET_BUILD_TYPE=release
558     export TARGET_BUILD_APPS=$apps
559
560     set_stuff_for_environment
561     printconfig
562 }
563
564 function gettop
565 {
566     local TOPFILE=build/core/envsetup.mk
567     if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
568         echo $TOP
569     else
570         if [ -f $TOPFILE ] ; then
571             # The following circumlocution (repeated below as well) ensures
572             # that we record the true directory name and not one that is
573             # faked up with symlink names.
574             PWD= /bin/pwd
575         else
576             # We redirect cd to /dev/null in case it's aliased to
577             # a command that prints something as a side-effect
578             # (like pushd)
579             local HERE=$PWD
580             T=
581             while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
582                 cd .. > /dev/null
583                 T=`PWD= /bin/pwd`
584             done
585             cd $HERE > /dev/null
586             if [ -f "$T/$TOPFILE" ]; then
587                 echo $T
588             fi
589         fi
590     fi
591 }
592
593 function m()
594 {
595     T=$(gettop)
596     if [ "$T" ]; then
597         make -C $T $@
598     else
599         echo "Couldn't locate the top of the tree.  Try setting TOP."
600     fi
601 }
602
603 function findmakefile()
604 {
605     TOPFILE=build/core/envsetup.mk
606     # We redirect cd to /dev/null in case it's aliased to
607     # a command that prints something as a side-effect
608     # (like pushd)
609     local HERE=$PWD
610     T=
611     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
612         T=$PWD
613         if [ -f "$T/Android.mk" ]; then
614             echo $T/Android.mk
615             cd $HERE > /dev/null
616             return
617         fi
618         cd .. > /dev/null
619     done
620     cd $HERE > /dev/null
621 }
622
623 function mm()
624 {
625     # If we're sitting in the root of the build tree, just do a
626     # normal make.
627     if [ -f build/core/envsetup.mk -a -f Makefile ]; then
628         make $@
629     else
630         # Find the closest Android.mk file.
631         T=$(gettop)
632         local M=$(findmakefile)
633         # Remove the path to top as the makefilepath needs to be relative
634         local M=`echo $M|sed 's:'$T'/::'`
635         if [ ! "$T" ]; then
636             echo "Couldn't locate the top of the tree.  Try setting TOP."
637         elif [ ! "$M" ]; then
638             echo "Couldn't locate a makefile from the current directory."
639         else
640             ONE_SHOT_MAKEFILE=$M make -C $T all_modules $@
641         fi
642     fi
643 }
644
645 function mmm()
646 {
647     T=$(gettop)
648     if [ "$T" ]; then
649         local MAKEFILE=
650         local ARGS=
651         local DIR TO_CHOP
652         local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
653         local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
654         for DIR in $DIRS ; do
655             DIR=`echo $DIR | sed -e 's:/$::'`
656             if [ -f $DIR/Android.mk ]; then
657                 TO_CHOP=`echo $T | wc -c | tr -d ' '`
658                 TO_CHOP=`expr $TO_CHOP + 1`
659                 START=`PWD= /bin/pwd`
660                 MFILE=`echo $START | cut -c${TO_CHOP}-`
661                 if [ "$MFILE" = "" ] ; then
662                     MFILE=$DIR/Android.mk
663                 else
664                     MFILE=$MFILE/$DIR/Android.mk
665                 fi
666                 MAKEFILE="$MAKEFILE $MFILE"
667             else
668                 if [ "$DIR" = snod ]; then
669                     ARGS="$ARGS snod"
670                 elif [ "$DIR" = showcommands ]; then
671                     ARGS="$ARGS showcommands"
672                 elif [ "$DIR" = dist ]; then
673                     ARGS="$ARGS dist"
674                 else
675                     echo "No Android.mk in $DIR."
676                     return 1
677                 fi
678             fi
679         done
680         ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS all_modules $ARGS
681     else
682         echo "Couldn't locate the top of the tree.  Try setting TOP."
683     fi
684 }
685
686 function croot()
687 {
688     T=$(gettop)
689     if [ "$T" ]; then
690         cd $(gettop)
691     else
692         echo "Couldn't locate the top of the tree.  Try setting TOP."
693     fi
694 }
695
696 function cproj()
697 {
698     TOPFILE=build/core/envsetup.mk
699     # We redirect cd to /dev/null in case it's aliased to
700     # a command that prints something as a side-effect
701     # (like pushd)
702     local HERE=$PWD
703     T=
704     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
705         T=$PWD
706         if [ -f "$T/Android.mk" ]; then
707             cd $T
708             return
709         fi
710         cd .. > /dev/null
711     done
712     cd $HERE > /dev/null
713     echo "can't find Android.mk"
714 }
715
716 function pid()
717 {
718    local EXE="$1"
719    if [ "$EXE" ] ; then
720        local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
721        echo "$PID"
722    else
723        echo "usage: pid name"
724    fi
725 }
726
727 # systemstack - dump the current stack trace of all threads in the system process
728 # to the usual ANR traces file
729 function systemstack()
730 {
731     adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
732 }
733
734 function gdbclient()
735 {
736    local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
737    local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
738    local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
739    local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
740    local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
741    if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
742        local EXE="$1"
743        if [ "$EXE" ] ; then
744            EXE=$1
745        else
746            EXE="app_process"
747        fi
748
749        local PORT="$2"
750        if [ "$PORT" ] ; then
751            PORT=$2
752        else
753            PORT=":5039"
754        fi
755
756        local PID
757        local PROG="$3"
758        if [ "$PROG" ] ; then
759            PID=`pid $3`
760            adb forward "tcp$PORT" "tcp$PORT"
761            adb shell gdbserver $PORT --attach $PID &
762            sleep 2
763        else
764                echo ""
765                echo "If you haven't done so already, do this first on the device:"
766                echo "    gdbserver $PORT /system/bin/$EXE"
767                    echo " or"
768                echo "    gdbserver $PORT --attach $PID"
769                echo ""
770        fi
771
772        echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
773        echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
774        echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
775        echo >>"$OUT_ROOT/gdbclient.cmds" ""
776
777        arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
778   else
779        echo "Unable to determine build system output dir."
780    fi
781
782 }
783
784 case `uname -s` in
785     Darwin)
786         function sgrep()
787         {
788             find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
789         }
790
791         ;;
792     *)
793         function sgrep()
794         {
795             find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
796         }
797         ;;
798 esac
799
800 function jgrep()
801 {
802     find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
803 }
804
805 function cgrep()
806 {
807     find . -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
808 }
809
810 function resgrep()
811 {
812     for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
813 }
814
815 case `uname -s` in
816     Darwin)
817         function mgrep()
818         {
819             find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
820         }
821
822         function treegrep()
823         {
824             find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
825         }
826
827         ;;
828     *)
829         function mgrep()
830         {
831             find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
832         }
833
834         function treegrep()
835         {
836             find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
837         }
838
839         ;;
840 esac
841
842 function getprebuilt
843 {
844     get_abs_build_var ANDROID_PREBUILTS
845 }
846
847 function tracedmdump()
848 {
849     T=$(gettop)
850     if [ ! "$T" ]; then
851         echo "Couldn't locate the top of the tree.  Try setting TOP."
852         return
853     fi
854     local prebuiltdir=$(getprebuilt)
855     local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
856
857     local TRACE=$1
858     if [ ! "$TRACE" ] ; then
859         echo "usage:  tracedmdump  tracename"
860         return
861     fi
862
863     if [ ! -r "$KERNEL" ] ; then
864         echo "Error: cannot find kernel: '$KERNEL'"
865         return
866     fi
867
868     local BASETRACE=$(basename $TRACE)
869     if [ "$BASETRACE" = "$TRACE" ] ; then
870         TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
871     fi
872
873     echo "post-processing traces..."
874     rm -f $TRACE/qtrace.dexlist
875     post_trace $TRACE
876     if [ $? -ne 0 ]; then
877         echo "***"
878         echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
879         echo "***"
880         return
881     fi
882     echo "generating dexlist output..."
883     /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
884     echo "generating dmtrace data..."
885     q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
886     echo "generating html file..."
887     dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
888     echo "done, see $TRACE/dmtrace.html for details"
889     echo "or run:"
890     echo "    traceview $TRACE/dmtrace"
891 }
892
893 # communicate with a running device or emulator, set up necessary state,
894 # and run the hat command.
895 function runhat()
896 {
897     # process standard adb options
898     local adbTarget=""
899     if [ $1 = "-d" -o $1 = "-e" ]; then
900         adbTarget=$1
901         shift 1
902     elif [ $1 = "-s" ]; then
903         adbTarget="$1 $2"
904         shift 2
905     fi
906     local adbOptions=${adbTarget}
907     echo adbOptions = ${adbOptions}
908
909     # runhat options
910     local targetPid=$1
911     local outputFile=$2
912
913     if [ "$targetPid" = "" ]; then
914         echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
915         return
916     fi
917
918     # confirm hat is available
919     if [ -z $(which hat) ]; then
920         echo "hat is not available in this configuration."
921         return
922     fi
923
924     adb ${adbOptions} shell >/dev/null mkdir /data/misc
925     adb ${adbOptions} shell chmod 777 /data/misc
926
927     # send a SIGUSR1 to cause the hprof dump
928     echo "Poking $targetPid and waiting for data..."
929     adb ${adbOptions} shell kill -10 $targetPid
930     echo "Press enter when logcat shows \"hprof: heap dump completed\""
931     echo -n "> "
932     read
933
934     local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
935     local devFile=/data/misc/${availFiles[0]}
936     local localFile=/tmp/$$-hprof
937
938     echo "Retrieving file $devFile..."
939     adb ${adbOptions} pull $devFile $localFile
940
941     adb ${adbOptions} shell rm $devFile
942
943     echo "Running hat on $localFile"
944     echo "View the output by pointing your browser at http://localhost:7000/"
945     echo ""
946     hat $localFile
947 }
948
949 function getbugreports()
950 {
951     local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
952
953     if [ ! "$reports" ]; then
954         echo "Could not locate any bugreports."
955         return
956     fi
957
958     local report
959     for report in ${reports[@]}
960     do
961         echo "/sdcard/bugreports/${report}"
962         adb pull /sdcard/bugreports/${report} ${report}
963         gunzip ${report}
964     done
965 }
966
967 function startviewserver()
968 {
969     local port=4939
970     if [ $# -gt 0 ]; then
971             port=$1
972     fi
973     adb shell service call window 1 i32 $port
974 }
975
976 function stopviewserver()
977 {
978     adb shell service call window 2
979 }
980
981 function isviewserverstarted()
982 {
983     adb shell service call window 3
984 }
985
986 function smoketest()
987 {
988     if [ ! "$ANDROID_PRODUCT_OUT" ]; then
989         echo "Couldn't locate output files.  Try running 'lunch' first." >&2
990         return
991     fi
992     T=$(gettop)
993     if [ ! "$T" ]; then
994         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
995         return
996     fi
997
998     (cd "$T" && mmm tests/SmokeTest) &&
999       adb uninstall com.android.smoketest > /dev/null &&
1000       adb uninstall com.android.smoketest.tests > /dev/null &&
1001       adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1002       adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1003       adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1004 }
1005
1006 # simple shortcut to the runtest command
1007 function runtest()
1008 {
1009     T=$(gettop)
1010     if [ ! "$T" ]; then
1011         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1012         return
1013     fi
1014     ("$T"/development/testrunner/runtest.py $@)
1015 }
1016
1017 function godir () {
1018     if [[ -z "$1" ]]; then
1019         echo "Usage: godir <regex>"
1020         return
1021     fi
1022     T=$(gettop)
1023     if [[ ! -f $T/filelist ]]; then
1024         echo -n "Creating index..."
1025         (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1026         echo " Done"
1027         echo ""
1028     fi
1029     local lines
1030     lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq)) 
1031     if [[ ${#lines[@]} = 0 ]]; then
1032         echo "Not found"
1033         return
1034     fi
1035     local pathname
1036     local choice
1037     if [[ ${#lines[@]} > 1 ]]; then
1038         while [[ -z "$pathname" ]]; do
1039             local index=1
1040             local line
1041             for line in ${lines[@]}; do
1042                 printf "%6s %s\n" "[$index]" $line
1043                 index=$(($index + 1)) 
1044             done
1045             echo
1046             echo -n "Select one: "
1047             unset choice
1048             read choice
1049             if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1050                 echo "Invalid choice"
1051                 continue
1052             fi
1053             pathname=${lines[$(($choice-$_arrayoffset))]}
1054         done
1055     else
1056         # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1057         pathname=${lines[0]}
1058     fi
1059     cd $T/$pathname
1060 }
1061
1062 # Force JAVA_HOME to point to java 1.5 if it isn't already set
1063 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
1064     if [ ! "$JAVA_HOME" ]; then
1065         case `uname -s` in
1066             Darwin)
1067                 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
1068                 ;;
1069             *)
1070                 export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
1071                 ;;
1072         esac
1073     fi
1074 fi
1075
1076 # determine whether arrays are zero-based (bash) or one-based (zsh)
1077 _xarray=(a b c)
1078 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1079 then
1080     _arrayoffset=1
1081 else
1082     _arrayoffset=0
1083 fi
1084 unset _xarray
1085
1086 # Execute the contents of any vendorsetup.sh files we can find.
1087 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`
1088 do
1089     echo "including $f"
1090     . $f
1091 done
1092 unset f