1function hmm() {
2cat <<EOF
3
4Run "m help" for help with the build system itself.
5
6Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
7- lunch:      lunch <product_name>-<build_variant>
8              Selects <product_name> as the product to build, and <build_variant> as the variant to
9              build, and stores those selections in the environment to be read by subsequent
10              invocations of 'm' etc.
11- tapas:      tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
12              Sets up the build environment for building unbundled apps (APKs).
13- banchan:    banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
14              Sets up the build environment for building unbundled modules (APEXes).
15- croot:      Changes directory to the top of the tree, or a subdirectory thereof.
16- m:          Makes from the top of the tree.
17- mm:         Builds and installs all of the modules in the current directory, and their
18              dependencies.
19- mmm:        Builds and installs all of the modules in the supplied directories, and their
20              dependencies.
21              To limit the modules being built use the syntax: mmm dir/:target1,target2.
22- mma:        Same as 'mm'
23- mmma:       Same as 'mmm'
24- provision:  Flash device with all required partitions. Options will be passed on to fastboot.
25- cgrep:      Greps on all local C/C++ files.
26- ggrep:      Greps on all local Gradle files.
27- gogrep:     Greps on all local Go files.
28- jgrep:      Greps on all local Java files.
29- ktgrep:     Greps on all local Kotlin files.
30- resgrep:    Greps on all local res/*.xml files.
31- mangrep:    Greps on all local AndroidManifest.xml files.
32- mgrep:      Greps on all local Makefiles and *.bp files.
33- owngrep:    Greps on all local OWNERS files.
34- rsgrep:     Greps on all local Rust files.
35- sepgrep:    Greps on all local sepolicy files.
36- sgrep:      Greps on all local source files.
37- godir:      Go to the directory containing a file.
38- allmod:     List all modules.
39- gomod:      Go to the directory containing a module.
40- pathmod:    Get the directory containing a module.
41- outmod:     Gets the location of a module's installed outputs with a certain extension.
42- dirmods:    Gets the modules defined in a given directory.
43- installmod: Adb installs a module's built APK.
44- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
45- syswrite:   Remount partitions (e.g. system.img) as writable, rebooting if necessary.
46
47Environment options:
48- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
49- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
50
51Look at the source to view more functions. The complete list is:
52EOF
53    local T=$(gettop)
54    local A=""
55    local i
56    for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
57      A="$A $i"
58    done
59    echo $A
60}
61
62# Get all the build variables needed by this script in a single call to the build system.
63function build_build_var_cache()
64{
65    local T=$(gettop)
66    # Grep out the variable names from the script.
67    cached_vars=(`cat $T/build/envsetup.sh | tr '()' '  ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
68    cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' '  ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
69    # Call the build system to dump the "<val>=<value>" pairs as a shell script.
70    build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
71                        --vars="${cached_vars[*]}" \
72                        --abs-vars="${cached_abs_vars[*]}" \
73                        --var-prefix=var_cache_ \
74                        --abs-var-prefix=abs_var_cache_`
75    local ret=$?
76    if [ $ret -ne 0 ]
77    then
78        unset build_dicts_script
79        return $ret
80    fi
81    # Execute the script to store the "<val>=<value>" pairs as shell variables.
82    eval "$build_dicts_script"
83    ret=$?
84    unset build_dicts_script
85    if [ $ret -ne 0 ]
86    then
87        return $ret
88    fi
89    BUILD_VAR_CACHE_READY="true"
90}
91
92# Delete the build var cache, so that we can still call into the build system
93# to get build variables not listed in this script.
94function destroy_build_var_cache()
95{
96    unset BUILD_VAR_CACHE_READY
97    local v
98    for v in $cached_vars; do
99      unset var_cache_$v
100    done
101    unset cached_vars
102    for v in $cached_abs_vars; do
103      unset abs_var_cache_$v
104    done
105    unset cached_abs_vars
106}
107
108# Get the value of a build variable as an absolute path.
109function get_abs_build_var()
110{
111    if [ "$BUILD_VAR_CACHE_READY" = "true" ]
112    then
113        eval "echo \"\${abs_var_cache_$1}\""
114        return
115    fi
116
117    local T=$(gettop)
118    if [ ! "$T" ]; then
119        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
120        return
121    fi
122    (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
123}
124
125# Get the exact value of a build variable.
126function get_build_var()
127{
128    if [ "$BUILD_VAR_CACHE_READY" = "true" ]
129    then
130        eval "echo \"\${var_cache_$1}\""
131        return 0
132    fi
133
134    local T=$(gettop)
135    if [ ! "$T" ]; then
136        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
137        return 1
138    fi
139    (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
140}
141
142# check to see if the supplied product is one we can build
143function check_product()
144{
145    local T=$(gettop)
146    if [ ! "$T" ]; then
147        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
148        return
149    fi
150        TARGET_PRODUCT=$1 \
151        TARGET_BUILD_VARIANT= \
152        TARGET_BUILD_TYPE= \
153        TARGET_BUILD_APPS= \
154        get_build_var TARGET_DEVICE > /dev/null
155    # hide successful answers, but allow the errors to show
156}
157
158VARIANT_CHOICES=(user userdebug eng)
159
160# check to see if the supplied variant is valid
161function check_variant()
162{
163    local v
164    for v in ${VARIANT_CHOICES[@]}
165    do
166        if [ "$v" = "$1" ]
167        then
168            return 0
169        fi
170    done
171    return 1
172}
173
174function setpaths()
175{
176    local T=$(gettop)
177    if [ ! "$T" ]; then
178        echo "Couldn't locate the top of the tree.  Try setting TOP."
179        return
180    fi
181
182    ##################################################################
183    #                                                                #
184    #              Read me before you modify this code               #
185    #                                                                #
186    #   This function sets ANDROID_BUILD_PATHS to what it is adding  #
187    #   to PATH, and the next time it is run, it removes that from   #
188    #   PATH.  This is required so lunch can be run more than once   #
189    #   and still have working paths.                                #
190    #                                                                #
191    ##################################################################
192
193    # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
194    # due to "C:\Program Files" being in the path.
195
196    # out with the old
197    if [ -n "$ANDROID_BUILD_PATHS" ] ; then
198        export PATH=${PATH/$ANDROID_BUILD_PATHS/}
199    fi
200    if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
201        export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
202        # strip leading ':', if any
203        export PATH=${PATH/:%/}
204    fi
205
206    # and in with the new
207    local prebuiltdir=$(getprebuilt)
208    local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
209
210    # defined in core/config.mk
211    local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
212    local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
213    export TARGET_GCC_VERSION=$targetgccversion
214
215    # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
216    export ANDROID_TOOLCHAIN=
217    export ANDROID_TOOLCHAIN_2ND_ARCH=
218    local ARCH=$(get_build_var TARGET_ARCH)
219    local toolchaindir toolchaindir2=
220    case $ARCH in
221        x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
222            ;;
223        x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
224            ;;
225        arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
226            ;;
227        arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
228               toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
229            ;;
230        *)
231            echo "Can't find toolchain for unknown architecture: $ARCH"
232            toolchaindir=xxxxxxxxx
233            ;;
234    esac
235    if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
236        export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
237    fi
238
239    if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
240        export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
241    fi
242
243    export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
244
245    # add kernel specific binaries
246    case $(uname -s) in
247        Linux)
248            export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
249            ;;
250        *)
251            ;;
252    esac
253
254    ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
255    if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
256        ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
257    fi
258    ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
259
260    # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
261    local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
262    ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
263
264    # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
265    export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
266
267    # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
268    # to ensure that the corresponding 'emulator' binaries are used.
269    case $(uname -s) in
270        Darwin)
271            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
272            ;;
273        Linux)
274            ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
275            ;;
276        *)
277            ANDROID_EMULATOR_PREBUILTS=
278            ;;
279    esac
280    if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
281        ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
282        export ANDROID_EMULATOR_PREBUILTS
283    fi
284
285    # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
286    local os_arch=$(get_build_var HOST_PREBUILT_TAG)
287    local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
288    local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
289    local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
290    export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
291
292    export PATH=$ANDROID_BUILD_PATHS$PATH
293
294    # out with the duplicate old
295    if [ -n $ANDROID_PYTHONPATH ]; then
296        export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
297    fi
298    # and in with the new
299    export ANDROID_PYTHONPATH=$T/development/python-packages:
300    if [ -n $VENDOR_PYTHONPATH  ]; then
301        ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
302    fi
303    export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
304
305    export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
306    export JAVA_HOME=$ANDROID_JAVA_HOME
307    export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
308    export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
309    export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
310
311    unset ANDROID_PRODUCT_OUT
312    export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
313    export OUT=$ANDROID_PRODUCT_OUT
314
315    unset ANDROID_HOST_OUT
316    export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
317
318    unset ANDROID_SOONG_HOST_OUT
319    export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
320
321    unset ANDROID_HOST_OUT_TESTCASES
322    export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
323
324    unset ANDROID_TARGET_OUT_TESTCASES
325    export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
326
327    # needed for building linux on MacOS
328    # TODO: fix the path
329    #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
330}
331
332function bazel()
333{
334    local T="$(gettop)"
335    if [ ! "$T" ]; then
336        echo "Couldn't locate the top of the tree.  Try setting TOP."
337        return
338    fi
339
340    if which bazel &>/dev/null; then
341        >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
342        >&2 echo
343    fi
344
345    "$T/tools/bazel" "$@"
346}
347
348function printconfig()
349{
350    local T=$(gettop)
351    if [ ! "$T" ]; then
352        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
353        return
354    fi
355    get_build_var report_config
356}
357
358function set_stuff_for_environment()
359{
360    setpaths
361    set_sequence_number
362
363    export ANDROID_BUILD_TOP=$(gettop)
364    # With this environment variable new GCC can apply colors to warnings/errors
365    export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
366}
367
368function set_sequence_number()
369{
370    export BUILD_ENV_SEQUENCE_NUMBER=13
371}
372
373# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
374function should_add_completion() {
375    local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
376    case :"$ENVSETUP_NO_COMPLETION": in
377        *:"$cmd":*)
378            return 1
379            ;;
380    esac
381    return 0
382}
383
384function addcompletions()
385{
386    local f=
387
388    # Keep us from trying to run in something that's neither bash nor zsh.
389    if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
390        return
391    fi
392
393    # Keep us from trying to run in bash that's too old.
394    if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
395        return
396    fi
397
398    local completion_files=(
399      system/core/adb/adb.bash
400      system/core/fastboot/fastboot.bash
401      tools/asuite/asuite.sh
402    )
403    # Completion can be disabled selectively to allow users to use non-standard completion.
404    # e.g.
405    # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
406    # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
407    for f in ${completion_files[*]}; do
408        if [ -f "$f" ] && should_add_completion "$f"; then
409            . $f
410        fi
411    done
412
413    if should_add_completion bit ; then
414        complete -C "bit --tab" bit
415    fi
416    if [ -z "$ZSH_VERSION" ]; then
417        # Doesn't work in zsh.
418        complete -o nospace -F _croot croot
419    fi
420    complete -F _lunch lunch
421
422    complete -F _complete_android_module_names pathmod
423    complete -F _complete_android_module_names gomod
424    complete -F _complete_android_module_names outmod
425    complete -F _complete_android_module_names installmod
426    complete -F _complete_android_module_names m
427}
428
429function choosetype()
430{
431    echo "Build type choices are:"
432    echo "     1. release"
433    echo "     2. debug"
434    echo
435
436    local DEFAULT_NUM DEFAULT_VALUE
437    DEFAULT_NUM=1
438    DEFAULT_VALUE=release
439
440    export TARGET_BUILD_TYPE=
441    local ANSWER
442    while [ -z $TARGET_BUILD_TYPE ]
443    do
444        echo -n "Which would you like? ["$DEFAULT_NUM"] "
445        if [ -z "$1" ] ; then
446            read ANSWER
447        else
448            echo $1
449            ANSWER=$1
450        fi
451        case $ANSWER in
452        "")
453            export TARGET_BUILD_TYPE=$DEFAULT_VALUE
454            ;;
455        1)
456            export TARGET_BUILD_TYPE=release
457            ;;
458        release)
459            export TARGET_BUILD_TYPE=release
460            ;;
461        2)
462            export TARGET_BUILD_TYPE=debug
463            ;;
464        debug)
465            export TARGET_BUILD_TYPE=debug
466            ;;
467        *)
468            echo
469            echo "I didn't understand your response.  Please try again."
470            echo
471            ;;
472        esac
473        if [ -n "$1" ] ; then
474            break
475        fi
476    done
477
478    build_build_var_cache
479    set_stuff_for_environment
480    destroy_build_var_cache
481}
482
483#
484# This function isn't really right:  It chooses a TARGET_PRODUCT
485# based on the list of boards.  Usually, that gets you something
486# that kinda works with a generic product, but really, you should
487# pick a product by name.
488#
489function chooseproduct()
490{
491    local default_value
492    if [ "x$TARGET_PRODUCT" != x ] ; then
493        default_value=$TARGET_PRODUCT
494    else
495        default_value=aosp_arm
496    fi
497
498    export TARGET_BUILD_APPS=
499    export TARGET_PRODUCT=
500    local ANSWER
501    while [ -z "$TARGET_PRODUCT" ]
502    do
503        echo -n "Which product would you like? [$default_value] "
504        if [ -z "$1" ] ; then
505            read ANSWER
506        else
507            echo $1
508            ANSWER=$1
509        fi
510
511        if [ -z "$ANSWER" ] ; then
512            export TARGET_PRODUCT=$default_value
513        else
514            if check_product $ANSWER
515            then
516                export TARGET_PRODUCT=$ANSWER
517            else
518                echo "** Not a valid product: $ANSWER"
519            fi
520        fi
521        if [ -n "$1" ] ; then
522            break
523        fi
524    done
525
526    build_build_var_cache
527    set_stuff_for_environment
528    destroy_build_var_cache
529}
530
531function choosevariant()
532{
533    echo "Variant choices are:"
534    local index=1
535    local v
536    for v in ${VARIANT_CHOICES[@]}
537    do
538        # The product name is the name of the directory containing
539        # the makefile we found, above.
540        echo "     $index. $v"
541        index=$(($index+1))
542    done
543
544    local default_value=eng
545    local ANSWER
546
547    export TARGET_BUILD_VARIANT=
548    while [ -z "$TARGET_BUILD_VARIANT" ]
549    do
550        echo -n "Which would you like? [$default_value] "
551        if [ -z "$1" ] ; then
552            read ANSWER
553        else
554            echo $1
555            ANSWER=$1
556        fi
557
558        if [ -z "$ANSWER" ] ; then
559            export TARGET_BUILD_VARIANT=$default_value
560        elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
561            if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
562                export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
563            fi
564        else
565            if check_variant $ANSWER
566            then
567                export TARGET_BUILD_VARIANT=$ANSWER
568            else
569                echo "** Not a valid variant: $ANSWER"
570            fi
571        fi
572        if [ -n "$1" ] ; then
573            break
574        fi
575    done
576}
577
578function choosecombo()
579{
580    choosetype $1
581
582    echo
583    echo
584    chooseproduct $2
585
586    echo
587    echo
588    choosevariant $3
589
590    echo
591    build_build_var_cache
592    set_stuff_for_environment
593    printconfig
594    destroy_build_var_cache
595}
596
597function add_lunch_combo()
598{
599    if [ -n "$ZSH_VERSION" ]; then
600        echo -n "${funcfiletrace[1]}: "
601    else
602        echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
603    fi
604    echo "add_lunch_combo is obsolete. Use COMMON_LUNCH_CHOICES in your AndroidProducts.mk instead."
605}
606
607function print_lunch_menu()
608{
609    local uname=$(uname)
610    local choices
611    choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
612    local ret=$?
613
614    echo
615    echo "You're building on" $uname
616    echo
617
618    if [ $ret -ne 0 ]
619    then
620        echo "Warning: Cannot display lunch menu."
621        echo
622        echo "Note: You can invoke lunch with an explicit target:"
623        echo
624        echo "  usage: lunch [target]" >&2
625        echo
626        return
627    fi
628
629    echo "Lunch menu... pick a combo:"
630
631    local i=1
632    local choice
633    for choice in $(echo $choices)
634    do
635        echo "     $i. $choice"
636        i=$(($i+1))
637    done
638
639    echo
640}
641
642function lunch()
643{
644    local answer
645
646    if [[ $# -gt 1 ]]; then
647        echo "usage: lunch [target]" >&2
648        return 1
649    fi
650
651    if [ "$1" ]; then
652        answer=$1
653    else
654        print_lunch_menu
655        echo -n "Which would you like? [aosp_arm-eng] "
656        read answer
657    fi
658
659    local selection=
660
661    if [ -z "$answer" ]
662    then
663        selection=aosp_arm-eng
664    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
665    then
666        local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
667        if [ $answer -le ${#choices[@]} ]
668        then
669            # array in zsh starts from 1 instead of 0.
670            if [ -n "$ZSH_VERSION" ]
671            then
672                selection=${choices[$(($answer))]}
673            else
674                selection=${choices[$(($answer-1))]}
675            fi
676        fi
677    else
678        selection=$answer
679    fi
680
681    export TARGET_BUILD_APPS=
682
683    local product variant_and_version variant version
684    product=${selection%%-*} # Trim everything after first dash
685    variant_and_version=${selection#*-} # Trim everything up to first dash
686    if [ "$variant_and_version" != "$selection" ]; then
687        variant=${variant_and_version%%-*}
688        if [ "$variant" != "$variant_and_version" ]; then
689            version=${variant_and_version#*-}
690        fi
691    fi
692
693    if [ -z "$product" ]
694    then
695        echo
696        echo "Invalid lunch combo: $selection"
697        return 1
698    fi
699
700    TARGET_PRODUCT=$product \
701    TARGET_BUILD_VARIANT=$variant \
702    TARGET_PLATFORM_VERSION=$version \
703    build_build_var_cache
704    if [ $? -ne 0 ]
705    then
706        return 1
707    fi
708    export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
709    export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
710    if [ -n "$version" ]; then
711      export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
712    else
713      unset TARGET_PLATFORM_VERSION
714    fi
715    export TARGET_BUILD_TYPE=release
716
717    [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
718
719    set_stuff_for_environment
720    [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
721    destroy_build_var_cache
722}
723
724unset COMMON_LUNCH_CHOICES_CACHE
725# Tab completion for lunch.
726function _lunch()
727{
728    local cur prev opts
729    COMPREPLY=()
730    cur="${COMP_WORDS[COMP_CWORD]}"
731    prev="${COMP_WORDS[COMP_CWORD-1]}"
732
733    if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
734        COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
735    fi
736
737    COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
738    return 0
739}
740
741# Configures the build to build unbundled apps.
742# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
743function tapas()
744{
745    local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
746    local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
747    local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
748    local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
749    local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|arm64|x86_64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
750
751    if [ "$showHelp" != "" ]; then
752      $(gettop)/build/make/tapasHelp.sh
753      return
754    fi
755
756    if [ $(echo $arch | wc -w) -gt 1 ]; then
757        echo "tapas: Error: Multiple build archs supplied: $arch"
758        return
759    fi
760    if [ $(echo $variant | wc -w) -gt 1 ]; then
761        echo "tapas: Error: Multiple build variants supplied: $variant"
762        return
763    fi
764    if [ $(echo $density | wc -w) -gt 1 ]; then
765        echo "tapas: Error: Multiple densities supplied: $density"
766        return
767    fi
768
769    local product=aosp_arm
770    case $arch in
771      x86)    product=aosp_x86;;
772      arm64)  product=aosp_arm64;;
773      x86_64) product=aosp_x86_64;;
774    esac
775    if [ -z "$variant" ]; then
776        variant=eng
777    fi
778    if [ -z "$apps" ]; then
779        apps=all
780    fi
781    if [ -z "$density" ]; then
782        density=alldpi
783    fi
784
785    export TARGET_PRODUCT=$product
786    export TARGET_BUILD_VARIANT=$variant
787    export TARGET_BUILD_DENSITY=$density
788    export TARGET_BUILD_TYPE=release
789    export TARGET_BUILD_APPS=$apps
790
791    build_build_var_cache
792    set_stuff_for_environment
793    printconfig
794    destroy_build_var_cache
795}
796
797# Configures the build to build unbundled Android modules (APEXes).
798# Run banchan with one or more module names (from apex{} modules).
799function banchan()
800{
801    local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
802    local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64)$' | xargs)"
803    local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
804    local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|(.*_)?(arm|x86|arm64|x86_64))$' | xargs)"
805
806    if [ "$showHelp" != "" ]; then
807      $(gettop)/build/make/banchanHelp.sh
808      return
809    fi
810
811    if [ -z "$product" ]; then
812        product=arm
813    elif [ $(echo $product | wc -w) -gt 1 ]; then
814        echo "banchan: Error: Multiple build archs or products supplied: $products"
815        return
816    fi
817    if [ $(echo $variant | wc -w) -gt 1 ]; then
818        echo "banchan: Error: Multiple build variants supplied: $variant"
819        return
820    fi
821    if [ -z "$apps" ]; then
822        echo "banchan: Error: No modules supplied"
823        return
824    fi
825
826    case $product in
827      arm)    product=module_arm;;
828      x86)    product=module_x86;;
829      arm64)  product=module_arm64;;
830      x86_64) product=module_x86_64;;
831    esac
832    if [ -z "$variant" ]; then
833        variant=eng
834    fi
835
836    export TARGET_PRODUCT=$product
837    export TARGET_BUILD_VARIANT=$variant
838    export TARGET_BUILD_DENSITY=alldpi
839    export TARGET_BUILD_TYPE=release
840
841    # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
842    # case is different and it may diverge in the future.
843    export TARGET_BUILD_APPS=$apps
844
845    build_build_var_cache
846    set_stuff_for_environment
847    printconfig
848    destroy_build_var_cache
849}
850
851function gettop
852{
853    local TOPFILE=build/make/core/envsetup.mk
854    if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
855        # The following circumlocution ensures we remove symlinks from TOP.
856        (cd "$TOP"; PWD= /bin/pwd)
857    else
858        if [ -f $TOPFILE ] ; then
859            # The following circumlocution (repeated below as well) ensures
860            # that we record the true directory name and not one that is
861            # faked up with symlink names.
862            PWD= /bin/pwd
863        else
864            local HERE=$PWD
865            local T=
866            while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
867                \cd ..
868                T=`PWD= /bin/pwd -P`
869            done
870            \cd "$HERE"
871            if [ -f "$T/$TOPFILE" ]; then
872                echo "$T"
873            fi
874        fi
875    fi
876}
877
878function croot()
879{
880    local T=$(gettop)
881    if [ "$T" ]; then
882        if [ "$1" ]; then
883            \cd $(gettop)/$1
884        else
885            \cd $(gettop)
886        fi
887    else
888        echo "Couldn't locate the top of the tree.  Try setting TOP."
889    fi
890}
891
892function _croot()
893{
894    local T=$(gettop)
895    if [ "$T" ]; then
896        local cur="${COMP_WORDS[COMP_CWORD]}"
897        k=0
898        for c in $(compgen -d ${T}/${cur}); do
899            COMPREPLY[k++]=${c#${T}/}/
900        done
901    fi
902}
903
904function cproj()
905{
906    local TOPFILE=build/make/core/envsetup.mk
907    local HERE=$PWD
908    local T=
909    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
910        T=$PWD
911        if [ -f "$T/Android.mk" ]; then
912            \cd $T
913            return
914        fi
915        \cd ..
916    done
917    \cd $HERE
918    echo "can't find Android.mk"
919}
920
921# simplified version of ps; output in the form
922# <pid> <procname>
923function qpid() {
924    local prepend=''
925    local append=''
926    if [ "$1" = "--exact" ]; then
927        prepend=' '
928        append='$'
929        shift
930    elif [ "$1" = "--help" -o "$1" = "-h" ]; then
931        echo "usage: qpid [[--exact] <process name|pid>"
932        return 255
933    fi
934
935    local EXE="$1"
936    if [ "$EXE" ] ; then
937        qpid | \grep "$prepend$EXE$append"
938    else
939        adb shell ps \
940            | tr -d '\r' \
941            | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
942    fi
943}
944
945# syswrite - disable verity, reboot if needed, and remount image
946#
947# Easy way to make system.img/etc writable
948function syswrite() {
949  adb wait-for-device && adb root || return 1
950  if [[ $(adb disable-verity | grep "reboot") ]]; then
951      echo "rebooting"
952      adb reboot && adb wait-for-device && adb root || return 1
953  fi
954  adb wait-for-device && adb remount || return 1
955}
956
957# coredump_setup - enable core dumps globally for any process
958#                  that has the core-file-size limit set correctly
959#
960# NOTE: You must call also coredump_enable for a specific process
961#       if its core-file-size limit is not set already.
962# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
963
964function coredump_setup()
965{
966    echo "Getting root...";
967    adb root;
968    adb wait-for-device;
969
970    echo "Remounting root partition read-write...";
971    adb shell mount -w -o remount -t rootfs rootfs;
972    sleep 1;
973    adb wait-for-device;
974    adb shell mkdir -p /cores;
975    adb shell mount -t tmpfs tmpfs /cores;
976    adb shell chmod 0777 /cores;
977
978    echo "Granting SELinux permission to dump in /cores...";
979    adb shell restorecon -R /cores;
980
981    echo "Set core pattern.";
982    adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
983
984    echo "Done."
985}
986
987# coredump_enable - enable core dumps for the specified process
988# $1 = PID of process (e.g., $(pid mediaserver))
989#
990# NOTE: coredump_setup must have been called as well for a core
991#       dump to actually be generated.
992
993function coredump_enable()
994{
995    local PID=$1;
996    if [ -z "$PID" ]; then
997        printf "Expecting a PID!\n";
998        return;
999    fi;
1000    echo "Setting core limit for $PID to infinite...";
1001    adb shell /system/bin/ulimit -p $PID -c unlimited
1002}
1003
1004# core - send SIGV and pull the core for process
1005# $1 = PID of process (e.g., $(pid mediaserver))
1006#
1007# NOTE: coredump_setup must be called once per boot for core dumps to be
1008#       enabled globally.
1009
1010function core()
1011{
1012    local PID=$1;
1013
1014    if [ -z "$PID" ]; then
1015        printf "Expecting a PID!\n";
1016        return;
1017    fi;
1018
1019    local CORENAME=core.$PID;
1020    local COREPATH=/cores/$CORENAME;
1021    local SIG=SEGV;
1022
1023    coredump_enable $1;
1024
1025    local done=0;
1026    while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1027        printf "\tSending SIG%s to %d...\n" $SIG $PID;
1028        adb shell kill -$SIG $PID;
1029        sleep 1;
1030    done;
1031
1032    adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1033    echo "Done: core is under $COREPATH on device.";
1034}
1035
1036# systemstack - dump the current stack trace of all threads in the system process
1037# to the usual ANR traces file
1038function systemstack()
1039{
1040    stacks system_server
1041}
1042
1043# Read the ELF header from /proc/$PID/exe to determine if the process is
1044# 64-bit.
1045function is64bit()
1046{
1047    local PID="$1"
1048    if [ "$PID" ] ; then
1049        if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
1050            echo "64"
1051        else
1052            echo ""
1053        fi
1054    else
1055        echo ""
1056    fi
1057}
1058
1059case `uname -s` in
1060    Darwin)
1061        function sgrep()
1062        {
1063            find -E . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|kt|xml|sh|mk|aidl|vts|proto)' \
1064                -exec grep --color -n "$@" {} +
1065        }
1066
1067        ;;
1068    *)
1069        function sgrep()
1070        {
1071            find . -name .repo -prune -o -name .git -prune -o  -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|kt\|xml\|sh\|mk\|aidl\|vts\|proto\)' \
1072                -exec grep --color -n "$@" {} +
1073        }
1074        ;;
1075esac
1076
1077function gettargetarch
1078{
1079    get_build_var TARGET_ARCH
1080}
1081
1082function ggrep()
1083{
1084    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1085        -exec grep --color -n "$@" {} +
1086}
1087
1088function gogrep()
1089{
1090    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1091        -exec grep --color -n "$@" {} +
1092}
1093
1094function jgrep()
1095{
1096    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1097        -exec grep --color -n "$@" {} +
1098}
1099
1100function rsgrep()
1101{
1102    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1103        -exec grep --color -n "$@" {} +
1104}
1105
1106function ktgrep()
1107{
1108    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1109        -exec grep --color -n "$@" {} +
1110}
1111
1112function cgrep()
1113{
1114    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
1115        -exec grep --color -n "$@" {} +
1116}
1117
1118function resgrep()
1119{
1120    local dir
1121    for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1122        find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1123    done
1124}
1125
1126function mangrep()
1127{
1128    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1129        -exec grep --color -n "$@" {} +
1130}
1131
1132function owngrep()
1133{
1134    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1135        -exec grep --color -n "$@" {} +
1136}
1137
1138function sepgrep()
1139{
1140    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1141        -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
1142}
1143
1144function rcgrep()
1145{
1146    find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1147        -exec grep --color -n "$@" {} +
1148}
1149
1150case `uname -s` in
1151    Darwin)
1152        function mgrep()
1153        {
1154            find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
1155                -exec grep --color -n "$@" {} +
1156        }
1157
1158        function treegrep()
1159        {
1160            find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' \
1161                -exec grep --color -n -i "$@" {} +
1162        }
1163
1164        ;;
1165    *)
1166        function mgrep()
1167        {
1168            find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regextype posix-extended -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
1169                -exec grep --color -n "$@" {} +
1170        }
1171
1172        function treegrep()
1173        {
1174            find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' -type f \
1175                -exec grep --color -n -i "$@" {} +
1176        }
1177
1178        ;;
1179esac
1180
1181function getprebuilt
1182{
1183    get_abs_build_var ANDROID_PREBUILTS
1184}
1185
1186function tracedmdump()
1187{
1188    local T=$(gettop)
1189    if [ ! "$T" ]; then
1190        echo "Couldn't locate the top of the tree.  Try setting TOP."
1191        return
1192    fi
1193    local prebuiltdir=$(getprebuilt)
1194    local arch=$(gettargetarch)
1195    local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1196
1197    local TRACE=$1
1198    if [ ! "$TRACE" ] ; then
1199        echo "usage:  tracedmdump  tracename"
1200        return
1201    fi
1202
1203    if [ ! -r "$KERNEL" ] ; then
1204        echo "Error: cannot find kernel: '$KERNEL'"
1205        return
1206    fi
1207
1208    local BASETRACE=$(basename $TRACE)
1209    if [ "$BASETRACE" = "$TRACE" ] ; then
1210        TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1211    fi
1212
1213    echo "post-processing traces..."
1214    rm -f $TRACE/qtrace.dexlist
1215    post_trace $TRACE
1216    if [ $? -ne 0 ]; then
1217        echo "***"
1218        echo "*** Error: malformed trace.  Did you remember to exit the emulator?"
1219        echo "***"
1220        return
1221    fi
1222    echo "generating dexlist output..."
1223    /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
1224    echo "generating dmtrace data..."
1225    q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1226    echo "generating html file..."
1227    dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1228    echo "done, see $TRACE/dmtrace.html for details"
1229    echo "or run:"
1230    echo "    traceview $TRACE/dmtrace"
1231}
1232
1233# communicate with a running device or emulator, set up necessary state,
1234# and run the hat command.
1235function runhat()
1236{
1237    # process standard adb options
1238    local adbTarget=""
1239    if [ "$1" = "-d" -o "$1" = "-e" ]; then
1240        adbTarget=$1
1241        shift 1
1242    elif [ "$1" = "-s" ]; then
1243        adbTarget="$1 $2"
1244        shift 2
1245    fi
1246    local adbOptions=${adbTarget}
1247    #echo adbOptions = ${adbOptions}
1248
1249    # runhat options
1250    local targetPid=$1
1251
1252    if [ "$targetPid" = "" ]; then
1253        echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1254        return
1255    fi
1256
1257    # confirm hat is available
1258    if [ -z $(which hat) ]; then
1259        echo "hat is not available in this configuration."
1260        return
1261    fi
1262
1263    # issue "am" command to cause the hprof dump
1264    local devFile=/data/local/tmp/hprof-$targetPid
1265    echo "Poking $targetPid and waiting for data..."
1266    echo "Storing data at $devFile"
1267    adb ${adbOptions} shell am dumpheap $targetPid $devFile
1268    echo "Press enter when logcat shows \"hprof: heap dump completed\""
1269    echo -n "> "
1270    read
1271
1272    local localFile=/tmp/$$-hprof
1273
1274    echo "Retrieving file $devFile..."
1275    adb ${adbOptions} pull $devFile $localFile
1276
1277    adb ${adbOptions} shell rm $devFile
1278
1279    echo "Running hat on $localFile"
1280    echo "View the output by pointing your browser at http://localhost:7000/"
1281    echo ""
1282    hat -JXmx512m $localFile
1283}
1284
1285function getbugreports()
1286{
1287    local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1288
1289    if [ ! "$reports" ]; then
1290        echo "Could not locate any bugreports."
1291        return
1292    fi
1293
1294    local report
1295    for report in ${reports[@]}
1296    do
1297        echo "/sdcard/bugreports/${report}"
1298        adb pull /sdcard/bugreports/${report} ${report}
1299        gunzip ${report}
1300    done
1301}
1302
1303function getsdcardpath()
1304{
1305    adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1306}
1307
1308function getscreenshotpath()
1309{
1310    echo "$(getsdcardpath)/Pictures/Screenshots"
1311}
1312
1313function getlastscreenshot()
1314{
1315    local screenshot_path=$(getscreenshotpath)
1316    local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1317    if [ "$screenshot" = "" ]; then
1318        echo "No screenshots found."
1319        return
1320    fi
1321    echo "${screenshot}"
1322    adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1323}
1324
1325function startviewserver()
1326{
1327    local port=4939
1328    if [ $# -gt 0 ]; then
1329            port=$1
1330    fi
1331    adb shell service call window 1 i32 $port
1332}
1333
1334function stopviewserver()
1335{
1336    adb shell service call window 2
1337}
1338
1339function isviewserverstarted()
1340{
1341    adb shell service call window 3
1342}
1343
1344function key_home()
1345{
1346    adb shell input keyevent 3
1347}
1348
1349function key_back()
1350{
1351    adb shell input keyevent 4
1352}
1353
1354function key_menu()
1355{
1356    adb shell input keyevent 82
1357}
1358
1359function smoketest()
1360{
1361    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1362        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
1363        return
1364    fi
1365    local T=$(gettop)
1366    if [ ! "$T" ]; then
1367        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1368        return
1369    fi
1370
1371    (\cd "$T" && mmm tests/SmokeTest) &&
1372      adb uninstall com.android.smoketest > /dev/null &&
1373      adb uninstall com.android.smoketest.tests > /dev/null &&
1374      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1375      adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1376      adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1377}
1378
1379# simple shortcut to the runtest command
1380function runtest()
1381{
1382    local T=$(gettop)
1383    if [ ! "$T" ]; then
1384        echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
1385        return
1386    fi
1387    ("$T"/development/testrunner/runtest.py $@)
1388}
1389
1390function godir () {
1391    if [[ -z "$1" ]]; then
1392        echo "Usage: godir <regex>"
1393        return
1394    fi
1395    local T=$(gettop)
1396    local FILELIST
1397    if [ ! "$OUT_DIR" = "" ]; then
1398        mkdir -p $OUT_DIR
1399        FILELIST=$OUT_DIR/filelist
1400    else
1401        FILELIST=$T/filelist
1402    fi
1403    if [[ ! -f $FILELIST ]]; then
1404        echo -n "Creating index..."
1405        (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
1406        echo " Done"
1407        echo ""
1408    fi
1409    local lines
1410    lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
1411    if [[ ${#lines[@]} = 0 ]]; then
1412        echo "Not found"
1413        return
1414    fi
1415    local pathname
1416    local choice
1417    if [[ ${#lines[@]} > 1 ]]; then
1418        while [[ -z "$pathname" ]]; do
1419            local index=1
1420            local line
1421            for line in ${lines[@]}; do
1422                printf "%6s %s\n" "[$index]" $line
1423                index=$(($index + 1))
1424            done
1425            echo
1426            echo -n "Select one: "
1427            unset choice
1428            read choice
1429            if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1430                echo "Invalid choice"
1431                continue
1432            fi
1433            pathname=${lines[@]:$(($choice-1)):1}
1434        done
1435    else
1436        pathname=${lines[@]:0:1}
1437    fi
1438    \cd $T/$pathname
1439}
1440
1441# Update module-info.json in out.
1442function refreshmod() {
1443    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1444        echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1445        return 1
1446    fi
1447
1448    echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1449
1450    # for the output of the next command
1451    mkdir -p $ANDROID_PRODUCT_OUT || return 1
1452
1453    # Note, can't use absolute path because of the way make works.
1454    m $(get_build_var PRODUCT_OUT)/module-info.json \
1455        > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1456}
1457
1458# Verifies that module-info.txt exists, creating it if it doesn't.
1459function verifymodinfo() {
1460    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1461        if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1462            echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1463        fi
1464        return 1
1465    fi
1466
1467    if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1468        if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1469            echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1470        fi
1471        return 1
1472    fi
1473}
1474
1475# List all modules for the current device, as cached in module-info.json. If any build change is
1476# made and it should be reflected in the output, you should run 'refreshmod' first.
1477function allmod() {
1478    verifymodinfo || return 1
1479
1480    python3 -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
1481}
1482
1483# Get the path of a specific module in the android tree, as cached in module-info.json.
1484# If any build change is made, and it should be reflected in the output, you should run
1485# 'refreshmod' first.  Note: This is the inverse of dirmods.
1486function pathmod() {
1487    if [[ $# -ne 1 ]]; then
1488        echo "usage: pathmod <module>" >&2
1489        return 1
1490    fi
1491
1492    verifymodinfo || return 1
1493
1494    local relpath=$(python3 -c "import json, os
1495module = '$1'
1496module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1497if module not in module_info:
1498    exit(1)
1499print(module_info[module]['path'][0])" 2>/dev/null)
1500
1501    if [ -z "$relpath" ]; then
1502        echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1503        return 1
1504    else
1505        echo "$ANDROID_BUILD_TOP/$relpath"
1506    fi
1507}
1508
1509# Get the path of a specific module in the android tree, as cached in module-info.json.
1510# If any build change is made, and it should be reflected in the output, you should run
1511# 'refreshmod' first.  Note: This is the inverse of pathmod.
1512function dirmods() {
1513    if [[ $# -ne 1 ]]; then
1514        echo "usage: dirmods <path>" >&2
1515        return 1
1516    fi
1517
1518    verifymodinfo || return 1
1519
1520    python3 -c "import json, os
1521dir = '$1'
1522while dir.endswith('/'):
1523    dir = dir[:-1]
1524prefix = dir + '/'
1525module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1526results = set()
1527for m in module_info.values():
1528    for path in m.get(u'path', []):
1529        if path == dir or path.startswith(prefix):
1530            name = m.get(u'module_name')
1531            if name:
1532                results.add(name)
1533for name in sorted(results):
1534    print(name)
1535"
1536}
1537
1538
1539# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1540# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1541function gomod() {
1542    if [[ $# -ne 1 ]]; then
1543        echo "usage: gomod <module>" >&2
1544        return 1
1545    fi
1546
1547    local path="$(pathmod $@)"
1548    if [ -z "$path" ]; then
1549        return 1
1550    fi
1551    cd $path
1552}
1553
1554# Gets the list of a module's installed outputs, as cached in module-info.json.
1555# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1556function outmod() {
1557    if [[ $# -ne 1 ]]; then
1558        echo "usage: outmod <module>" >&2
1559        return 1
1560    fi
1561
1562    verifymodinfo || return 1
1563
1564    local relpath
1565    relpath=$(python3 -c "import json, os
1566module = '$1'
1567module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1568if module not in module_info:
1569    exit(1)
1570for output in module_info[module]['installed']:
1571    print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1572
1573    if [ $? -ne 0 ]; then
1574        echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1575        return 1
1576    elif [ ! -z "$relpath" ]; then
1577        echo "$relpath"
1578    fi
1579}
1580
1581# adb install a module's apk, as cached in module-info.json. If any build change
1582# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1583# Usage: installmod [adb install arguments] <module>
1584# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1585function installmod() {
1586    if [[ $# -eq 0 ]]; then
1587        echo "usage: installmod [adb install arguments] <module>" >&2
1588        return 1
1589    fi
1590
1591    local _path
1592    _path=$(outmod ${@:$#:1})
1593    if [ $? -ne 0 ]; then
1594        return 1
1595    fi
1596
1597    _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1598    if [ -z "$_path" ]; then
1599        echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1600        return 1
1601    fi
1602    local length=$(( $# - 1 ))
1603    echo adb install ${@:1:$length} $_path
1604    adb install ${@:1:$length} $_path
1605}
1606
1607function _complete_android_module_names() {
1608    local word=${COMP_WORDS[COMP_CWORD]}
1609    COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
1610}
1611
1612# Print colored exit condition
1613function pez {
1614    "$@"
1615    local retval=$?
1616    if [ $retval -ne 0 ]
1617    then
1618        echo $'\E'"[0;31mFAILURE\e[00m"
1619    else
1620        echo $'\E'"[0;32mSUCCESS\e[00m"
1621    fi
1622    return $retval
1623}
1624
1625function get_make_command()
1626{
1627    # If we're in the top of an Android tree, use soong_ui.bash instead of make
1628    if [ -f build/soong/soong_ui.bash ]; then
1629        # Always use the real make if -C is passed in
1630        for arg in "$@"; do
1631            if [[ $arg == -C* ]]; then
1632                echo command make
1633                return
1634            fi
1635        done
1636        echo build/soong/soong_ui.bash --make-mode
1637    else
1638        echo command make
1639    fi
1640}
1641
1642function _wrap_build()
1643{
1644    if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1645      "$@"
1646      return $?
1647    fi
1648    local start_time=$(date +"%s")
1649    "$@"
1650    local ret=$?
1651    local end_time=$(date +"%s")
1652    local tdiff=$(($end_time-$start_time))
1653    local hours=$(($tdiff / 3600 ))
1654    local mins=$((($tdiff % 3600) / 60))
1655    local secs=$(($tdiff % 60))
1656    local ncolors=$(tput colors 2>/dev/null)
1657    if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
1658        color_failed=$'\E'"[0;31m"
1659        color_success=$'\E'"[0;32m"
1660        color_reset=$'\E'"[00m"
1661    else
1662        color_failed=""
1663        color_success=""
1664        color_reset=""
1665    fi
1666    echo
1667    if [ $ret -eq 0 ] ; then
1668        echo -n "${color_success}#### build completed successfully "
1669    else
1670        echo -n "${color_failed}#### failed to build some targets "
1671    fi
1672    if [ $hours -gt 0 ] ; then
1673        printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1674    elif [ $mins -gt 0 ] ; then
1675        printf "(%02g:%02g (mm:ss))" $mins $secs
1676    elif [ $secs -gt 0 ] ; then
1677        printf "(%s seconds)" $secs
1678    fi
1679    echo " ####${color_reset}"
1680    echo
1681    return $ret
1682}
1683
1684function _trigger_build()
1685(
1686    local -r bc="$1"; shift
1687    if T="$(gettop)"; then
1688      _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1689    else
1690      echo "Couldn't locate the top of the tree. Try setting TOP."
1691    fi
1692)
1693
1694function m()
1695(
1696    _trigger_build "all-modules" "$@"
1697)
1698
1699function mm()
1700(
1701    _trigger_build "modules-in-a-dir-no-deps" "$@"
1702)
1703
1704function mmm()
1705(
1706    _trigger_build "modules-in-dirs-no-deps" "$@"
1707)
1708
1709function mma()
1710(
1711    _trigger_build "modules-in-a-dir" "$@"
1712)
1713
1714function mmma()
1715(
1716    _trigger_build "modules-in-dirs" "$@"
1717)
1718
1719function make()
1720{
1721    _wrap_build $(get_make_command "$@") "$@"
1722}
1723
1724function provision()
1725{
1726    if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1727        echo "Couldn't locate output files.  Try running 'lunch' first." >&2
1728        return 1
1729    fi
1730    if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1731        echo "There is no provisioning script for the device." >&2
1732        return 1
1733    fi
1734
1735    # Check if user really wants to do this.
1736    if [ "$1" = "--no-confirmation" ]; then
1737        shift 1
1738    else
1739        echo "This action will reflash your device."
1740        echo ""
1741        echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1742        echo ""
1743        echo -n "Are you sure you want to do this (yes/no)? "
1744        read
1745        if [[ "${REPLY}" != "yes" ]] ; then
1746            echo "Not taking any action. Exiting." >&2
1747            return 1
1748        fi
1749    fi
1750    "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1751}
1752
1753# Zsh needs bashcompinit called to support bash-style completion.
1754function enable_zsh_completion() {
1755    # Don't override user's options if bash-style completion is already enabled.
1756    if ! declare -f complete >/dev/null; then
1757        autoload -U compinit && compinit
1758        autoload -U bashcompinit && bashcompinit
1759    fi
1760}
1761
1762function validate_current_shell() {
1763    local current_sh="$(ps -o command -p $$)"
1764    case "$current_sh" in
1765        *bash*)
1766            function check_type() { type -t "$1"; }
1767            ;;
1768        *zsh*)
1769            function check_type() { type "$1"; }
1770            enable_zsh_completion ;;
1771        *)
1772            echo -e "WARNING: Only bash and zsh are supported.\nUse of other shell would lead to erroneous results."
1773            ;;
1774    esac
1775}
1776
1777# Execute the contents of any vendorsetup.sh files we can find.
1778# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1779# load those.
1780#
1781# This allows loading only approved vendorsetup.sh files
1782function source_vendorsetup() {
1783    unset VENDOR_PYTHONPATH
1784    local T="$(gettop)"
1785    allowed=
1786    for f in $(cd "$T" && find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1787        if [ -n "$allowed" ]; then
1788            echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1789            echo "  $allowed"
1790            echo "  $f"
1791            return
1792        fi
1793        allowed="$T/$f"
1794    done
1795
1796    allowed_files=
1797    [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
1798    for dir in device vendor product; do
1799        for f in $(cd "$T" && test -d $dir && \
1800            find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
1801
1802            if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1803                echo "including $f"; . "$T/$f"
1804            else
1805                echo "ignoring $f, not in $allowed"
1806            fi
1807        done
1808    done
1809}
1810
1811function showcommands() {
1812    local T=$(gettop)
1813    if [[ -z "$TARGET_PRODUCT" ]]; then
1814        >&2 echo "TARGET_PRODUCT not set. Run lunch."
1815        return
1816    fi
1817    case $(uname -s) in
1818        Darwin)
1819            PREBUILT_NAME=darwin-x86
1820            ;;
1821        Linux)
1822            PREBUILT_NAME=linux-x86
1823            ;;
1824        *)
1825            >&2 echo Unknown host $(uname -s)
1826            return
1827            ;;
1828    esac
1829    if [[ -z "$OUT_DIR" ]]; then
1830      if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1831        OUT_DIR=out
1832      else
1833        OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1834      fi
1835    fi
1836    if [[ "$1" == "--regenerate" ]]; then
1837      shift 1
1838      NINJA_ARGS="-t commands $@" m
1839    else
1840      (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1841          -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1842          -t commands "$@")
1843    fi
1844}
1845
1846validate_current_shell
1847source_vendorsetup
1848addcompletions
1849