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