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