OSDN Git Service

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