OSDN Git Service

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