OSDN Git Service

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