OSDN Git Service

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