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