OSDN Git Service

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