OSDN Git Service

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