OSDN Git Service

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