3 Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
4 - lunch: lunch <product_name>-<build_variant>
5 - tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5|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 - provision: Flash device with all required partitions. Options will be passed on to fastboot.
14 - cgrep: Greps on all local C/C++ files.
15 - ggrep: Greps on all local Gradle files.
16 - jgrep: Greps on all local Java files.
17 - resgrep: Greps on all local res/*.xml files.
18 - mangrep: Greps on all local AndroidManifest.xml files.
19 - mgrep: Greps on all local Makefiles files.
20 - sepgrep: Greps on all local sepolicy files.
21 - sgrep: Greps on all local source files.
22 - godir: Go to the directory containing a file.
26 __print_cm_functions_help
31 - SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
32 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
33 build is leak-check clean.
35 Look at the source to view more functions. The complete list is:
38 for i in `cat $T/build/envsetup.sh $T/vendor/cm/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
43 # Get all the build variables needed by this script in a single call to the build system.
44 function build_build_var_cache()
47 # Grep out the variable names from the script.
48 cached_vars=`cat $T/build/envsetup.sh $T/vendor/cm/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`
49 cached_abs_vars=`cat $T/build/envsetup.sh $T/vendor/cm/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`
50 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
51 build_dicts_script=`\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
52 command make --no-print-directory -f build/core/config.mk \
54 DUMP_MANY_VARS="$cached_vars" \
55 DUMP_MANY_ABS_VARS="$cached_abs_vars" \
56 DUMP_VAR_PREFIX="var_cache_" \
57 DUMP_ABS_VAR_PREFIX="abs_var_cache_"`
61 unset build_dicts_script
64 # Excute the script to store the "<val>=<value>" pairs as shell variables.
65 eval "$build_dicts_script"
67 unset build_dicts_script
72 BUILD_VAR_CACHE_READY="true"
75 # Delete the build var cache, so that we can still call into the build system
76 # to get build variables not listed in this script.
77 function destroy_build_var_cache()
79 unset BUILD_VAR_CACHE_READY
80 for v in $(echo $cached_vars | tr " " "\n"); do
84 for v in $(echo $cached_abs_vars | tr " " "\n"); do
85 unset abs_var_cache_$v
90 # Get the value of a build variable as an absolute path.
91 function get_abs_build_var()
93 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
95 eval echo \"\${abs_var_cache_$1}\"
101 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
104 (\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
105 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
108 # Get the exact value of a build variable.
109 function get_build_var()
111 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
113 eval echo \"\${var_cache_$1}\"
119 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
122 (\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
123 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
126 # check to see if the supplied product is one we can build
127 function check_product()
131 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
135 if (echo -n $1 | grep -q -e "^lineage_") ; then
136 CM_BUILD=$(echo -n $1 | sed -e 's/^lineage_//g')
137 export BUILD_NUMBER=$( (date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10 )
138 elif (echo -n $1 | grep -q -e "^cm_") ; then
139 # Fall back to cm_<product>
140 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
141 export BUILD_NUMBER=$( (date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10 )
148 TARGET_BUILD_VARIANT= \
151 get_build_var TARGET_DEVICE > /dev/null
152 # hide successful answers, but allow the errors to show
155 VARIANT_CHOICES=(user userdebug eng)
157 # check to see if the supplied variant is valid
158 function check_variant()
160 for v in ${VARIANT_CHOICES[@]}
174 echo "Couldn't locate the top of the tree. Try setting TOP."
178 ##################################################################
180 # Read me before you modify this code #
182 # This function sets ANDROID_BUILD_PATHS to what it is adding #
183 # to PATH, and the next time it is run, it removes that from #
184 # PATH. This is required so lunch can be run more than once #
185 # and still have working paths. #
187 ##################################################################
189 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
190 # due to "C:\Program Files" being in the path.
193 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
194 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
196 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
197 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
198 # strip leading ':', if any
199 export PATH=${PATH/:%/}
202 # and in with the new
203 prebuiltdir=$(getprebuilt)
204 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
206 # defined in core/config.mk
207 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
208 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
209 export TARGET_GCC_VERSION=$targetgccversion
211 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
212 export ANDROID_TOOLCHAIN=
213 export ANDROID_TOOLCHAIN_2ND_ARCH=
214 local ARCH=$(get_build_var TARGET_ARCH)
216 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
218 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
220 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
222 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
223 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
225 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
228 echo "Can't find toolchain for unknown architecture: $ARCH"
229 toolchaindir=xxxxxxxxx
232 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
233 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
236 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
237 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
240 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
241 export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_DEV_SCRIPTS:
243 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
244 # to ensure that the corresponding 'emulator' binaries are used.
247 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
250 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
253 ANDROID_EMULATOR_PREBUILTS=
256 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
257 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
258 export ANDROID_EMULATOR_PREBUILTS
261 export PATH=$ANDROID_BUILD_PATHS$PATH
262 export PYTHONPATH=$T/development/python-packages:$PYTHONPATH
264 unset ANDROID_JAVA_TOOLCHAIN
265 unset ANDROID_PRE_BUILD_PATHS
266 if [ -n "$JAVA_HOME" ]; then
267 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
268 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
269 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
272 unset ANDROID_PRODUCT_OUT
273 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
274 export OUT=$ANDROID_PRODUCT_OUT
276 unset ANDROID_HOST_OUT
277 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
279 if [ -n "$ANDROID_CCACHE_DIR" ]; then
280 export CCACHE_DIR=$ANDROID_CCACHE_DIR
283 # needed for building linux on MacOS
285 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
288 function printconfig()
292 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
295 get_build_var report_config
298 function set_stuff_for_environment()
305 # With this environment variable new GCC can apply colors to warnings/errors
306 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
307 export ASAN_OPTIONS=detect_leaks=0
310 function set_sequence_number()
312 export BUILD_ENV_SEQUENCE_NUMBER=10
317 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
318 local arch=$(gettargetarch)
319 local product=$TARGET_PRODUCT
320 local variant=$TARGET_BUILD_VARIANT
321 local apps=$TARGET_BUILD_APPS
322 if [ -z "$PROMPT_COMMAND" ]; then
324 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
325 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
326 # Prompts exist, but no hardstatus
327 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
329 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
330 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
333 if [ -z "$apps" ]; then
334 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
336 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
338 export ANDROID_PROMPT_PREFIX
340 # Inject build data into hardstatus
341 export PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/\\033]0;\(.*\)\\007/\\033]0;$ANDROID_PROMPT_PREFIX \1\\007/g')"
345 function check_bash_version()
347 # Keep us from trying to run in something that isn't bash.
348 if [ -z "${BASH_VERSION}" ]; then
352 # Keep us from trying to run in bash that's too old.
353 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
360 function choosetype()
362 echo "Build type choices are:"
367 local DEFAULT_NUM DEFAULT_VALUE
369 DEFAULT_VALUE=release
371 export TARGET_BUILD_TYPE=
373 while [ -z $TARGET_BUILD_TYPE ]
375 echo -n "Which would you like? ["$DEFAULT_NUM"] "
376 if [ -z "$1" ] ; then
384 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
387 export TARGET_BUILD_TYPE=release
390 export TARGET_BUILD_TYPE=release
393 export TARGET_BUILD_TYPE=debug
396 export TARGET_BUILD_TYPE=debug
400 echo "I didn't understand your response. Please try again."
404 if [ -n "$1" ] ; then
409 build_build_var_cache
410 set_stuff_for_environment
411 destroy_build_var_cache
415 # This function isn't really right: It chooses a TARGET_PRODUCT
416 # based on the list of boards. Usually, that gets you something
417 # that kinda works with a generic product, but really, you should
418 # pick a product by name.
420 function chooseproduct()
422 if [ "x$TARGET_PRODUCT" != x ] ; then
423 default_value=$TARGET_PRODUCT
425 default_value=aosp_arm
428 export TARGET_BUILD_APPS=
429 export TARGET_PRODUCT=
431 while [ -z "$TARGET_PRODUCT" ]
433 echo -n "Which product would you like? [$default_value] "
434 if [ -z "$1" ] ; then
441 if [ -z "$ANSWER" ] ; then
442 export TARGET_PRODUCT=$default_value
444 if check_product $ANSWER
446 export TARGET_PRODUCT=$ANSWER
448 echo "** Not a valid product: $ANSWER"
451 if [ -n "$1" ] ; then
456 build_build_var_cache
457 set_stuff_for_environment
458 destroy_build_var_cache
461 function choosevariant()
463 echo "Variant choices are:"
466 for v in ${VARIANT_CHOICES[@]}
468 # The product name is the name of the directory containing
469 # the makefile we found, above.
474 local default_value=eng
477 export TARGET_BUILD_VARIANT=
478 while [ -z "$TARGET_BUILD_VARIANT" ]
480 echo -n "Which would you like? [$default_value] "
481 if [ -z "$1" ] ; then
488 if [ -z "$ANSWER" ] ; then
489 export TARGET_BUILD_VARIANT=$default_value
490 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
491 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
492 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
495 if check_variant $ANSWER
497 export TARGET_BUILD_VARIANT=$ANSWER
499 echo "** Not a valid variant: $ANSWER"
502 if [ -n "$1" ] ; then
508 function choosecombo()
521 build_build_var_cache
522 set_stuff_for_environment
524 destroy_build_var_cache
527 # Clear this variable. It will be built up again when the vendorsetup.sh
528 # files are included at the end of this file.
529 unset LUNCH_MENU_CHOICES
530 function add_lunch_combo()
534 for c in ${LUNCH_MENU_CHOICES[@]} ; do
535 if [ "$new_combo" = "$c" ] ; then
539 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
542 # add the default one here
543 add_lunch_combo aosp_arm-eng
544 add_lunch_combo aosp_arm64-eng
545 add_lunch_combo aosp_mips-eng
546 add_lunch_combo aosp_mips64-eng
547 add_lunch_combo aosp_x86-eng
548 add_lunch_combo aosp_x86_64-eng
550 function print_lunch_menu()
554 echo "You're building on" $uname
556 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
557 echo "Breakfast menu... pick a combo:"
559 echo "Lunch menu... pick a combo:"
564 for choice in ${LUNCH_MENU_CHOICES[@]}
570 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
571 echo "... and don't forget the bacon!"
580 LUNCH_MENU_CHOICES=($(for l in ${LUNCH_MENU_CHOICES[@]}; do echo "$l"; done | sort))
586 echo -n "Which would you like? [aosp_arm-eng] "
594 selection=aosp_arm-eng
595 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
597 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
599 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
601 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
606 if [ -z "$selection" ]
609 echo "Invalid lunch combo: $answer"
613 export TARGET_BUILD_APPS=
615 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
616 check_variant $variant
620 echo "** Invalid variant: '$variant'"
621 echo "** Must be one of ${VARIANT_CHOICES[@]}"
625 local product=$(echo -n $selection | sed -e "s/-.*$//")
626 check_product $product
629 # if we can't find a product, try to grab it off the CM github
632 vendor/cm/build/tools/roomservice.py $product
634 check_product $product
638 vendor/cm/build/tools/roomservice.py $product true
641 TARGET_PRODUCT=$product \
642 TARGET_BUILD_VARIANT=$variant \
643 build_build_var_cache
648 echo "** Don't have a product spec for: '$product'"
649 echo "** Do you have the right repo manifest?"
653 if [ -z "$product" -o -z "$variant" ]
659 export TARGET_PRODUCT=$product
660 export TARGET_BUILD_VARIANT=$variant
661 export TARGET_BUILD_TYPE=release
667 set_stuff_for_environment
669 destroy_build_var_cache
672 # Tab completion for lunch.
677 cur="${COMP_WORDS[COMP_CWORD]}"
678 prev="${COMP_WORDS[COMP_CWORD-1]}"
680 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
683 complete -F _lunch lunch 2>/dev/null
685 # Configures the build to build unbundled apps.
686 # Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
689 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
690 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
691 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
692 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
694 if [ $(echo $arch | wc -w) -gt 1 ]; then
695 echo "tapas: Error: Multiple build archs supplied: $arch"
698 if [ $(echo $variant | wc -w) -gt 1 ]; then
699 echo "tapas: Error: Multiple build variants supplied: $variant"
702 if [ $(echo $density | wc -w) -gt 1 ]; then
703 echo "tapas: Error: Multiple densities supplied: $density"
707 local product=aosp_arm
709 x86) product=aosp_x86;;
710 mips) product=aosp_mips;;
711 armv5) product=generic_armv5;;
712 arm64) product=aosp_arm64;;
713 x86_64) product=aosp_x86_64;;
714 mips64) product=aosp_mips64;;
716 if [ -z "$variant" ]; then
719 if [ -z "$apps" ]; then
722 if [ -z "$density" ]; then
726 export TARGET_PRODUCT=$product
727 export TARGET_BUILD_VARIANT=$variant
728 export TARGET_BUILD_DENSITY=$density
729 export TARGET_BUILD_TYPE=release
730 export TARGET_BUILD_APPS=$apps
732 build_build_var_cache
733 set_stuff_for_environment
735 destroy_build_var_cache
740 local TOPFILE=build/core/envsetup.mk
741 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
742 # The following circumlocution ensures we remove symlinks from TOP.
743 (cd $TOP; PWD= /bin/pwd)
745 if [ -f $TOPFILE ] ; then
746 # The following circumlocution (repeated below as well) ensures
747 # that we record the true directory name and not one that is
748 # faked up with symlink names.
753 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
758 if [ -f "$T/$TOPFILE" ]; then
765 # Return driver for "make", if any (eg. static analyzer)
769 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
770 if [ -n "$WITH_STATIC_ANALYZER" ]; then
772 $T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
773 --use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
782 local DRV=$(getdriver $T)
784 $DRV make -C $T -f build/core/main.mk $@
786 echo "Couldn't locate the top of the tree. Try setting TOP."
791 function findmakefile()
793 TOPFILE=build/core/envsetup.mk
796 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
798 if [ -f "$T/Android.mk" ]; then
811 local DRV=$(getdriver $T)
812 # If we're sitting in the root of the build tree, just do a
814 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
817 # Find the closest Android.mk file.
818 local M=$(findmakefile)
820 local GET_INSTALL_PATH=
821 # Remove the path to top as the makefilepath needs to be relative
822 local M=`echo $M|sed 's:'$T'/::'`
824 echo "Couldn't locate the top of the tree. Try setting TOP."
826 elif [ ! "$M" ]; then
827 echo "Couldn't locate a makefile from the current directory."
832 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
835 if [ -n "$GET_INSTALL_PATH" ]; then
837 # set all args to 'GET-INSTALL-PATH'
838 set -- GET-INSTALL-PATH
842 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES "$@"
850 local DRV=$(getdriver $T)
856 local GET_INSTALL_PATH=
858 if [ "$(__detect_shell)" = "zsh" ]; then
859 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
860 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
862 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
863 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
866 for DIR in $DIRS ; do
867 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
868 if [ "$MODULES" = "" ]; then
871 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
872 if [ -f $DIR/Android.mk ]; then
873 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
874 local TO_CHOP=`expr $TO_CHOP + 1`
875 local START=`PWD= /bin/pwd`
876 local MFILE=`echo $START | cut -c${TO_CHOP}-`
877 if [ "$MFILE" = "" ] ; then
878 MFILE=$DIR/Android.mk
880 MFILE=$MFILE/$DIR/Android.mk
882 MAKEFILE="$MAKEFILE $MFILE"
885 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
886 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
887 *) if [ -d $DIR ]; then
888 echo "No Android.mk in $DIR.";
890 echo "Couldn't locate the directory $DIR";
896 if [ -n "$GET_INSTALL_PATH" ]; then
897 ARGS=$GET_INSTALL_PATH
900 ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
902 echo "Couldn't locate the top of the tree. Try setting TOP."
910 local DRV=$(getdriver $T)
911 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
915 echo "Couldn't locate the top of the tree. Try setting TOP."
918 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
919 local MODULES_IN_PATHS=MODULES-IN-$MY_PWD
920 # Convert "/" to "-".
921 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
922 $DRV make -C $T -f build/core/main.mk $@ $MODULES_IN_PATHS
929 local DRV=$(getdriver $T)
931 if [ "$(__detect_shell)" = "zsh" ]; then
932 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
933 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
935 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
936 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
938 local MY_PWD=`PWD= /bin/pwd`
939 if [ "$MY_PWD" = "$T" ]; then
942 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
945 local MODULES_IN_PATHS=
947 for DIR in $DIRS ; do
949 # Remove the leading ./ and trailing / if any exists.
952 if [ "$MY_PWD" != "" ]; then
955 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$DIR"
958 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
959 *) echo "Couldn't find directory $DIR"; return 1;;
963 # Convert "/" to "-".
964 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
965 $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS $MODULES_IN_PATHS
967 echo "Couldn't locate the top of the tree. Try setting TOP."
978 echo "Couldn't locate the top of the tree. Try setting TOP."
984 TOPFILE=build/core/envsetup.mk
987 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
989 if [ -f "$T/Android.mk" ]; then
996 echo "can't find Android.mk"
999 # simplified version of ps; output in the form
1004 if [ "$1" = "--exact" ]; then
1008 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1009 echo "usage: qpid [[--exact] <process name|pid>"
1014 if [ "$EXE" ] ; then
1015 qpid | \grep "$prepend$EXE$append"
1019 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1027 if [ "$1" = "--exact" ]; then
1033 if [ "$EXE" ] ; then
1034 local PID=`adb shell ps \
1036 | \grep "$prepend$EXE$append" \
1037 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1040 echo "usage: pid [--exact] <process name>"
1045 # coredump_setup - enable core dumps globally for any process
1046 # that has the core-file-size limit set correctly
1048 # NOTE: You must call also coredump_enable for a specific process
1049 # if its core-file-size limit is not set already.
1050 # NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1052 function coredump_setup()
1054 echo "Getting root...";
1056 adb wait-for-device;
1058 echo "Remounting root partition read-write...";
1059 adb shell mount -w -o remount -t rootfs rootfs;
1061 adb wait-for-device;
1062 adb shell mkdir -p /cores;
1063 adb shell mount -t tmpfs tmpfs /cores;
1064 adb shell chmod 0777 /cores;
1066 echo "Granting SELinux permission to dump in /cores...";
1067 adb shell restorecon -R /cores;
1069 echo "Set core pattern.";
1070 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1075 # coredump_enable - enable core dumps for the specified process
1076 # $1 = PID of process (e.g., $(pid mediaserver))
1078 # NOTE: coredump_setup must have been called as well for a core
1079 # dump to actually be generated.
1081 function coredump_enable()
1084 if [ -z "$PID" ]; then
1085 printf "Expecting a PID!\n";
1088 echo "Setting core limit for $PID to infinite...";
1089 adb shell prlimit $PID 4 -1 -1
1092 # core - send SIGV and pull the core for process
1093 # $1 = PID of process (e.g., $(pid mediaserver))
1095 # NOTE: coredump_setup must be called once per boot for core dumps to be
1102 if [ -z "$PID" ]; then
1103 printf "Expecting a PID!\n";
1107 local CORENAME=core.$PID;
1108 local COREPATH=/cores/$CORENAME;
1114 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1115 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1116 adb shell kill -$SIG $PID;
1120 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1121 echo "Done: core is under $COREPATH on device.";
1124 # systemstack - dump the current stack trace of all threads in the system process
1125 # to the usual ANR traces file
1126 function systemstack()
1128 stacks system_server
1133 if [[ $1 =~ ^[0-9]+$ ]] ; then
1135 elif [ "$1" ] ; then
1136 local PIDLIST="$(pid $1)"
1137 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1138 local PID="$PIDLIST"
1139 elif [ "$PIDLIST" ] ; then
1140 echo "more than one process: $1"
1142 echo "no such process: $1"
1145 echo "usage: stacks [pid|process name]"
1148 if [ "$PID" ] ; then
1149 # Determine whether the process is native
1150 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1151 # Dump stacks of Dalvik process
1152 local TRACES=/data/anr/traces.txt
1153 local ORIG=/data/anr/traces.orig
1154 local TMP=/data/anr/traces.tmp
1156 # Keep original traces to avoid clobbering
1157 adb shell mv $TRACES $ORIG
1159 # Make sure we have a usable file
1160 adb shell touch $TRACES
1161 adb shell chmod 666 $TRACES
1163 # Dump stacks and wait for dump to finish
1164 adb shell kill -3 $PID
1165 adb shell notify $TRACES >/dev/null
1167 # Restore original stacks, and show current output
1168 adb shell mv $TRACES $TMP
1169 adb shell mv $ORIG $TRACES
1172 # Dump stacks of native process
1173 local USE64BIT="$(is64bit $PID)"
1174 adb shell debuggerd$USE64BIT -b $PID
1179 # Read the ELF header from /proc/$PID/exe to determine if the process is
1184 if [ "$PID" ] ; then
1185 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
1199 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl|vts)' \
1200 -exec grep --color -n "$@" {} +
1207 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\)' \
1208 -exec grep --color -n "$@" {} +
1213 function gettargetarch
1215 get_build_var TARGET_ARCH
1220 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1221 -exec grep --color -n "$@" {} +
1226 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1227 -exec grep --color -n "$@" {} +
1232 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' \) \
1233 -exec grep --color -n "$@" {} +
1238 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1239 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1245 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1246 -exec grep --color -n "$@" {} +
1251 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1252 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
1257 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1258 -exec grep --color -n "$@" {} +
1265 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' \
1266 -exec grep --color -n "$@" {} +
1271 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' \
1272 -exec grep --color -n -i "$@" {} +
1279 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f \
1280 -exec grep --color -n "$@" {} +
1285 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f \
1286 -exec grep --color -n -i "$@" {} +
1292 function getprebuilt
1294 get_abs_build_var ANDROID_PREBUILTS
1297 function tracedmdump()
1301 echo "Couldn't locate the top of the tree. Try setting TOP."
1304 local prebuiltdir=$(getprebuilt)
1305 local arch=$(gettargetarch)
1306 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1309 if [ ! "$TRACE" ] ; then
1310 echo "usage: tracedmdump tracename"
1314 if [ ! -r "$KERNEL" ] ; then
1315 echo "Error: cannot find kernel: '$KERNEL'"
1319 local BASETRACE=$(basename $TRACE)
1320 if [ "$BASETRACE" = "$TRACE" ] ; then
1321 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1324 echo "post-processing traces..."
1325 rm -f $TRACE/qtrace.dexlist
1327 if [ $? -ne 0 ]; then
1329 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1333 echo "generating dexlist output..."
1334 /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
1335 echo "generating dmtrace data..."
1336 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1337 echo "generating html file..."
1338 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1339 echo "done, see $TRACE/dmtrace.html for details"
1341 echo " traceview $TRACE/dmtrace"
1344 # communicate with a running device or emulator, set up necessary state,
1345 # and run the hat command.
1348 # process standard adb options
1350 if [ "$1" = "-d" -o "$1" = "-e" ]; then
1353 elif [ "$1" = "-s" ]; then
1357 local adbOptions=${adbTarget}
1358 #echo adbOptions = ${adbOptions}
1363 if [ "$targetPid" = "" ]; then
1364 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1368 # confirm hat is available
1369 if [ -z $(which hat) ]; then
1370 echo "hat is not available in this configuration."
1374 # issue "am" command to cause the hprof dump
1375 local devFile=/data/local/tmp/hprof-$targetPid
1376 echo "Poking $targetPid and waiting for data..."
1377 echo "Storing data at $devFile"
1378 adb ${adbOptions} shell am dumpheap $targetPid $devFile
1379 echo "Press enter when logcat shows \"hprof: heap dump completed\""
1383 local localFile=/tmp/$$-hprof
1385 echo "Retrieving file $devFile..."
1386 adb ${adbOptions} pull $devFile $localFile
1388 adb ${adbOptions} shell rm $devFile
1390 echo "Running hat on $localFile"
1391 echo "View the output by pointing your browser at http://localhost:7000/"
1393 hat -JXmx512m $localFile
1396 function getbugreports()
1398 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1400 if [ ! "$reports" ]; then
1401 echo "Could not locate any bugreports."
1406 for report in ${reports[@]}
1408 echo "/sdcard/bugreports/${report}"
1409 adb pull /sdcard/bugreports/${report} ${report}
1414 function getsdcardpath()
1416 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1419 function getscreenshotpath()
1421 echo "$(getsdcardpath)/Pictures/Screenshots"
1424 function getlastscreenshot()
1426 local screenshot_path=$(getscreenshotpath)
1427 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1428 if [ "$screenshot" = "" ]; then
1429 echo "No screenshots found."
1432 echo "${screenshot}"
1433 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1436 function startviewserver()
1439 if [ $# -gt 0 ]; then
1442 adb shell service call window 1 i32 $port
1445 function stopviewserver()
1447 adb shell service call window 2
1450 function isviewserverstarted()
1452 adb shell service call window 3
1457 adb shell input keyevent 3
1462 adb shell input keyevent 4
1467 adb shell input keyevent 82
1470 function smoketest()
1472 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1473 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1478 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1482 (\cd "$T" && mmm tests/SmokeTest) &&
1483 adb uninstall com.android.smoketest > /dev/null &&
1484 adb uninstall com.android.smoketest.tests > /dev/null &&
1485 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1486 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1487 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1490 # simple shortcut to the runtest command
1495 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1498 ("$T"/development/testrunner/runtest.py $@)
1502 if [[ -z "$1" ]]; then
1503 echo "Usage: godir <regex>"
1507 if [ ! "$OUT_DIR" = "" ]; then
1509 FILELIST=$OUT_DIR/filelist
1511 FILELIST=$T/filelist
1513 if [[ ! -f $FILELIST ]]; then
1514 echo -n "Creating index..."
1515 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
1520 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
1521 if [[ ${#lines[@]} = 0 ]]; then
1527 if [[ ${#lines[@]} > 1 ]]; then
1528 while [[ -z "$pathname" ]]; do
1531 for line in ${lines[@]}; do
1532 printf "%6s %s\n" "[$index]" $line
1533 index=$(($index + 1))
1536 echo -n "Select one: "
1539 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1540 echo "Invalid choice"
1543 pathname=${lines[$(($choice-1))]}
1546 pathname=${lines[0]}
1551 # Force JAVA_HOME to point to java 1.7/1.8 if it isn't already set.
1552 function set_java_home() {
1553 # Clear the existing JAVA_HOME value if we set it ourselves, so that
1554 # we can reset it later, depending on the version of java the build
1557 # If we don't do this, the JAVA_HOME value set by the first call to
1558 # build/envsetup.sh will persist forever.
1559 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
1563 if [ ! "$JAVA_HOME" ]; then
1564 if [ -n "$LEGACY_USE_JAVA7" ]; then
1565 echo Warning: Support for JDK 7 will be dropped. Switch to JDK 8.
1568 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
1571 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
1577 export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
1580 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
1585 # Keep track of the fact that we set JAVA_HOME ourselves, so that
1586 # we can change it on the next envsetup.sh, if required.
1587 export ANDROID_SET_JAVA_HOME=true
1591 # Print colored exit condition
1595 if [ $retval -ne 0 ]
1597 echo $'\E'"[0;31mFAILURE\e[00m"
1599 echo $'\E'"[0;32mSUCCESS\e[00m"
1604 function get_make_command()
1611 local start_time=$(date +"%s")
1614 local end_time=$(date +"%s")
1615 local tdiff=$(($end_time-$start_time))
1616 local hours=$(($tdiff / 3600 ))
1617 local mins=$((($tdiff % 3600) / 60))
1618 local secs=$(($tdiff % 60))
1619 local ncolors=$(tput colors 2>/dev/null)
1620 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
1621 color_failed=$'\E'"[0;31m"
1622 color_success=$'\E'"[0;32m"
1623 color_reset=$'\E'"[00m"
1630 if [ $ret -eq 0 ] ; then
1631 echo -n "${color_success}#### make completed successfully "
1633 echo -n "${color_failed}#### make failed to build some targets "
1635 if [ $hours -gt 0 ] ; then
1636 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1637 elif [ $mins -gt 0 ] ; then
1638 printf "(%02g:%02g (mm:ss))" $mins $secs
1639 elif [ $secs -gt 0 ] ; then
1640 printf "(%s seconds)" $secs
1642 echo " ####${color_reset}"
1647 function provision()
1649 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1650 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1653 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1654 echo "There is no provisioning script for the device." >&2
1658 # Check if user really wants to do this.
1659 if [ "$1" = "--no-confirmation" ]; then
1662 echo "This action will reflash your device."
1664 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1666 echo -n "Are you sure you want to do this (yes/no)? "
1668 if [[ "${REPLY}" != "yes" ]] ; then
1669 echo "Not taking any action. Exiting." >&2
1673 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1678 mk_timer $(get_make_command) "$@"
1681 function __detect_shell() {
1682 case `ps -o command -p $$` in
1698 if ! __detect_shell > /dev/null; then
1699 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
1702 # Execute the contents of any vendorsetup.sh files we can find.
1703 for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
1704 `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
1705 `test -d product && find -L product -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
1713 check_bash_version && {
1714 dirs="sdk/bash_completion vendor/cm/bash_completion"
1715 for dir in $dirs; do
1716 if [ -d ${dir} ]; then
1717 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
1725 export ANDROID_BUILD_TOP=$(gettop)
1727 . $ANDROID_BUILD_TOP/vendor/cm/build/envsetup.sh