OSDN Git Service

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