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.
25 - SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
26 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
27 build is leak-check clean.
29 Look at the source to view more functions. The complete list is:
32 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
37 # Get all the build variables needed by this script in a single call to the build system.
38 function build_build_var_cache()
41 # Grep out the variable names from the script.
42 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' ' '`
43 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' ' '`
44 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
45 build_dicts_script=`\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
46 command make --no-print-directory -f build/core/config.mk \
48 DUMP_MANY_VARS="$cached_vars" \
49 DUMP_MANY_ABS_VARS="$cached_abs_vars" \
50 DUMP_VAR_PREFIX="var_cache_" \
51 DUMP_ABS_VAR_PREFIX="abs_var_cache_"`
55 unset build_dicts_script
58 # Excute the script to store the "<val>=<value>" pairs as shell variables.
59 eval "$build_dicts_script"
61 unset build_dicts_script
66 BUILD_VAR_CACHE_READY="true"
69 # Delete the build var cache, so that we can still call into the build system
70 # to get build variables not listed in this script.
71 function destroy_build_var_cache()
73 unset BUILD_VAR_CACHE_READY
74 for v in $cached_vars; do
78 for v in $cached_abs_vars; do
79 unset abs_var_cache_$v
84 # Get the value of a build variable as an absolute path.
85 function get_abs_build_var()
87 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
89 eval echo \"\${abs_var_cache_$1}\"
95 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
98 (\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
99 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
102 # Get the exact value of a build variable.
103 function get_build_var()
105 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
107 eval echo \"\${var_cache_$1}\"
113 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
116 (\cd $T; export CALLED_FROM_SETUP=true; export BUILD_SYSTEM=build/core; \
117 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
120 # check to see if the supplied product is one we can build
121 function check_product()
125 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
129 if (echo -n $1 | grep -q -e "^cm_") ; then
130 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
131 export BUILD_NUMBER=$((date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10)
138 TARGET_BUILD_VARIANT= \
141 get_build_var TARGET_DEVICE > /dev/null
142 # hide successful answers, but allow the errors to show
145 VARIANT_CHOICES=(user userdebug eng)
147 # check to see if the supplied variant is valid
148 function check_variant()
150 for v in ${VARIANT_CHOICES[@]}
164 echo "Couldn't locate the top of the tree. Try setting TOP."
168 ##################################################################
170 # Read me before you modify this code #
172 # This function sets ANDROID_BUILD_PATHS to what it is adding #
173 # to PATH, and the next time it is run, it removes that from #
174 # PATH. This is required so lunch can be run more than once #
175 # and still have working paths. #
177 ##################################################################
179 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
180 # due to "C:\Program Files" being in the path.
183 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
184 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
186 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
187 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
188 # strip leading ':', if any
189 export PATH=${PATH/:%/}
192 # and in with the new
193 prebuiltdir=$(getprebuilt)
194 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
196 # defined in core/config.mk
197 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
198 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
199 export TARGET_GCC_VERSION=$targetgccversion
201 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
202 export ANDROID_TOOLCHAIN=
203 export ANDROID_TOOLCHAIN_2ND_ARCH=
204 local ARCH=$(get_build_var TARGET_ARCH)
206 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
208 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
210 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
212 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
213 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
215 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
218 echo "Can't find toolchain for unknown architecture: $ARCH"
219 toolchaindir=xxxxxxxxx
222 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
223 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
226 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
227 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
230 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
231 export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_DEV_SCRIPTS:
233 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
234 # to ensure that the corresponding 'emulator' binaries are used.
237 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
240 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
243 ANDROID_EMULATOR_PREBUILTS=
246 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
247 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
248 export ANDROID_EMULATOR_PREBUILTS
251 export PATH=$ANDROID_BUILD_PATHS$PATH
252 export PYTHONPATH=$T/development/python-packages:$PYTHONPATH
254 unset ANDROID_JAVA_TOOLCHAIN
255 unset ANDROID_PRE_BUILD_PATHS
256 if [ -n "$JAVA_HOME" ]; then
257 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
258 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
259 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
262 unset ANDROID_PRODUCT_OUT
263 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
264 export OUT=$ANDROID_PRODUCT_OUT
266 unset ANDROID_HOST_OUT
267 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
269 if [ -n "$ANDROID_CCACHE_DIR" ]; then
270 export CCACHE_DIR=$ANDROID_CCACHE_DIR
273 # needed for building linux on MacOS
275 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
278 function printconfig()
282 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
285 get_build_var report_config
288 function set_stuff_for_environment()
295 # With this environment variable new GCC can apply colors to warnings/errors
296 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
297 export ASAN_OPTIONS=detect_leaks=0
300 function set_sequence_number()
302 export BUILD_ENV_SEQUENCE_NUMBER=10
307 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
308 local arch=$(gettargetarch)
309 local product=$TARGET_PRODUCT
310 local variant=$TARGET_BUILD_VARIANT
311 local apps=$TARGET_BUILD_APPS
312 if [ -z "$PROMPT_COMMAND" ]; then
314 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
315 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
316 # Prompts exist, but no hardstatus
317 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
319 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
320 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
323 if [ -z "$apps" ]; then
324 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
326 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
328 export ANDROID_PROMPT_PREFIX
330 # Inject build data into hardstatus
331 export PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/\\033]0;\(.*\)\\007/\\033]0;$ANDROID_PROMPT_PREFIX \1\\007/g')"
335 function check_bash_version()
337 # Keep us from trying to run in something that isn't bash.
338 if [ -z "${BASH_VERSION}" ]; then
342 # Keep us from trying to run in bash that's too old.
343 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
350 function choosetype()
352 echo "Build type choices are:"
357 local DEFAULT_NUM DEFAULT_VALUE
359 DEFAULT_VALUE=release
361 export TARGET_BUILD_TYPE=
363 while [ -z $TARGET_BUILD_TYPE ]
365 echo -n "Which would you like? ["$DEFAULT_NUM"] "
366 if [ -z "$1" ] ; then
374 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
377 export TARGET_BUILD_TYPE=release
380 export TARGET_BUILD_TYPE=release
383 export TARGET_BUILD_TYPE=debug
386 export TARGET_BUILD_TYPE=debug
390 echo "I didn't understand your response. Please try again."
394 if [ -n "$1" ] ; then
399 build_build_var_cache
400 set_stuff_for_environment
401 destroy_build_var_cache
405 # This function isn't really right: It chooses a TARGET_PRODUCT
406 # based on the list of boards. Usually, that gets you something
407 # that kinda works with a generic product, but really, you should
408 # pick a product by name.
410 function chooseproduct()
412 if [ "x$TARGET_PRODUCT" != x ] ; then
413 default_value=$TARGET_PRODUCT
415 default_value=aosp_arm
418 export TARGET_BUILD_APPS=
419 export TARGET_PRODUCT=
421 while [ -z "$TARGET_PRODUCT" ]
423 echo -n "Which product would you like? [$default_value] "
424 if [ -z "$1" ] ; then
431 if [ -z "$ANSWER" ] ; then
432 export TARGET_PRODUCT=$default_value
434 if check_product $ANSWER
436 export TARGET_PRODUCT=$ANSWER
438 echo "** Not a valid product: $ANSWER"
441 if [ -n "$1" ] ; then
446 build_build_var_cache
447 set_stuff_for_environment
448 destroy_build_var_cache
451 function choosevariant()
453 echo "Variant choices are:"
456 for v in ${VARIANT_CHOICES[@]}
458 # The product name is the name of the directory containing
459 # the makefile we found, above.
464 local default_value=eng
467 export TARGET_BUILD_VARIANT=
468 while [ -z "$TARGET_BUILD_VARIANT" ]
470 echo -n "Which would you like? [$default_value] "
471 if [ -z "$1" ] ; then
478 if [ -z "$ANSWER" ] ; then
479 export TARGET_BUILD_VARIANT=$default_value
480 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
481 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
482 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
485 if check_variant $ANSWER
487 export TARGET_BUILD_VARIANT=$ANSWER
489 echo "** Not a valid variant: $ANSWER"
492 if [ -n "$1" ] ; then
498 function choosecombo()
511 build_build_var_cache
512 set_stuff_for_environment
514 destroy_build_var_cache
517 # Clear this variable. It will be built up again when the vendorsetup.sh
518 # files are included at the end of this file.
519 unset LUNCH_MENU_CHOICES
520 function add_lunch_combo()
524 for c in ${LUNCH_MENU_CHOICES[@]} ; do
525 if [ "$new_combo" = "$c" ] ; then
529 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
532 # add the default one here
533 add_lunch_combo aosp_arm-eng
534 add_lunch_combo aosp_arm64-eng
535 add_lunch_combo aosp_mips-eng
536 add_lunch_combo aosp_mips64-eng
537 add_lunch_combo aosp_x86-eng
538 add_lunch_combo aosp_x86_64-eng
540 function print_lunch_menu()
544 echo "You're building on" $uname
546 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
547 echo "Breakfast menu... pick a combo:"
549 echo "Lunch menu... pick a combo:"
554 for choice in ${LUNCH_MENU_CHOICES[@]}
560 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
561 echo "... and don't forget the bacon!"
570 LUNCH_MENU_CHOICES=($(for l in ${LUNCH_MENU_CHOICES[@]}; do echo "$l"; done | sort))
576 echo -n "Which would you like? [aosp_arm-eng] "
584 selection=aosp_arm-eng
585 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
587 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
589 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
591 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
596 if [ -z "$selection" ]
599 echo "Invalid lunch combo: $answer"
603 export TARGET_BUILD_APPS=
605 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
606 check_variant $variant
610 echo "** Invalid variant: '$variant'"
611 echo "** Must be one of ${VARIANT_CHOICES[@]}"
615 local product=$(echo -n $selection | sed -e "s/-.*$//")
616 check_product $product
619 # if we can't find a product, try to grab it off the CM github
622 vendor/cm/build/tools/roomservice.py $product
624 check_product $product
628 vendor/cm/build/tools/roomservice.py $product true
631 TARGET_PRODUCT=$product \
632 TARGET_BUILD_VARIANT=$variant \
633 build_build_var_cache
638 echo "** Don't have a product spec for: '$product'"
639 echo "** Do you have the right repo manifest?"
643 if [ -z "$product" -o -z "$variant" ]
649 export TARGET_PRODUCT=$product
650 export TARGET_BUILD_VARIANT=$variant
651 export TARGET_BUILD_TYPE=release
657 set_stuff_for_environment
659 destroy_build_var_cache
662 # Tab completion for lunch.
667 cur="${COMP_WORDS[COMP_CWORD]}"
668 prev="${COMP_WORDS[COMP_CWORD-1]}"
670 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
673 complete -F _lunch lunch 2>/dev/null
675 # Configures the build to build unbundled apps.
676 # Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
679 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
680 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
681 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
682 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)"
684 if [ $(echo $arch | wc -w) -gt 1 ]; then
685 echo "tapas: Error: Multiple build archs supplied: $arch"
688 if [ $(echo $variant | wc -w) -gt 1 ]; then
689 echo "tapas: Error: Multiple build variants supplied: $variant"
692 if [ $(echo $density | wc -w) -gt 1 ]; then
693 echo "tapas: Error: Multiple densities supplied: $density"
697 local product=aosp_arm
699 x86) product=aosp_x86;;
700 mips) product=aosp_mips;;
701 armv5) product=generic_armv5;;
702 arm64) product=aosp_arm64;;
703 x86_64) product=aosp_x86_64;;
704 mips64) product=aosp_mips64;;
706 if [ -z "$variant" ]; then
709 if [ -z "$apps" ]; then
712 if [ -z "$density" ]; then
716 export TARGET_PRODUCT=$product
717 export TARGET_BUILD_VARIANT=$variant
718 export TARGET_BUILD_DENSITY=$density
719 export TARGET_BUILD_TYPE=release
720 export TARGET_BUILD_APPS=$apps
722 build_build_var_cache
723 set_stuff_for_environment
725 destroy_build_var_cache
730 local TOPFILE=build/core/envsetup.mk
731 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
732 # The following circumlocution ensures we remove symlinks from TOP.
733 (cd $TOP; PWD= /bin/pwd)
735 if [ -f $TOPFILE ] ; then
736 # The following circumlocution (repeated below as well) ensures
737 # that we record the true directory name and not one that is
738 # faked up with symlink names.
743 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
748 if [ -f "$T/$TOPFILE" ]; then
755 # Return driver for "make", if any (eg. static analyzer)
759 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
760 if [ -n "$WITH_STATIC_ANALYZER" ]; then
762 $T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
763 --use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
772 local DRV=$(getdriver $T)
774 $DRV make -C $T -f build/core/main.mk $@
776 echo "Couldn't locate the top of the tree. Try setting TOP."
781 function findmakefile()
783 TOPFILE=build/core/envsetup.mk
786 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
788 if [ -f "$T/Android.mk" ]; then
801 local DRV=$(getdriver $T)
802 # If we're sitting in the root of the build tree, just do a
804 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
807 # Find the closest Android.mk file.
808 local M=$(findmakefile)
810 local GET_INSTALL_PATH=
811 # Remove the path to top as the makefilepath needs to be relative
812 local M=`echo $M|sed 's:'$T'/::'`
814 echo "Couldn't locate the top of the tree. Try setting TOP."
816 elif [ ! "$M" ]; then
817 echo "Couldn't locate a makefile from the current directory."
822 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
825 if [ -n "$GET_INSTALL_PATH" ]; then
827 # set all args to 'GET-INSTALL-PATH'
828 set -- GET-INSTALL-PATH
832 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES "$@"
840 local DRV=$(getdriver $T)
846 local GET_INSTALL_PATH=
848 if [ "$(__detect_shell)" = "zsh" ]; then
849 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
850 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
852 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
853 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
856 for DIR in $DIRS ; do
857 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
858 if [ "$MODULES" = "" ]; then
861 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
862 if [ -f $DIR/Android.mk ]; then
863 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
864 local TO_CHOP=`expr $TO_CHOP + 1`
865 local START=`PWD= /bin/pwd`
866 local MFILE=`echo $START | cut -c${TO_CHOP}-`
867 if [ "$MFILE" = "" ] ; then
868 MFILE=$DIR/Android.mk
870 MFILE=$MFILE/$DIR/Android.mk
872 MAKEFILE="$MAKEFILE $MFILE"
875 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
876 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
877 *) if [ -d $DIR ]; then
878 echo "No Android.mk in $DIR.";
880 echo "Couldn't locate the directory $DIR";
886 if [ -n "$GET_INSTALL_PATH" ]; then
887 ARGS=$GET_INSTALL_PATH
890 ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
892 echo "Couldn't locate the top of the tree. Try setting TOP."
900 local DRV=$(getdriver $T)
901 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
905 echo "Couldn't locate the top of the tree. Try setting TOP."
908 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
909 local MODULES_IN_PATHS=MODULES-IN-$MY_PWD
910 # Convert "/" to "-".
911 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
912 $DRV make -C $T -f build/core/main.mk $@ $MODULES_IN_PATHS
919 local DRV=$(getdriver $T)
921 if [ "$(__detect_shell)" = "zsh" ]; then
922 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
923 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
925 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
926 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
928 local MY_PWD=`PWD= /bin/pwd`
929 if [ "$MY_PWD" = "$T" ]; then
932 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
935 local MODULES_IN_PATHS=
937 for DIR in $DIRS ; do
939 # Remove the leading ./ and trailing / if any exists.
942 if [ "$MY_PWD" != "" ]; then
945 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$DIR"
948 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
949 *) echo "Couldn't find directory $DIR"; return 1;;
953 # Convert "/" to "-".
954 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
955 $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS $MODULES_IN_PATHS
957 echo "Couldn't locate the top of the tree. Try setting TOP."
968 echo "Couldn't locate the top of the tree. Try setting TOP."
974 TOPFILE=build/core/envsetup.mk
977 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
979 if [ -f "$T/Android.mk" ]; then
986 echo "can't find Android.mk"
989 # simplified version of ps; output in the form
994 if [ "$1" = "--exact" ]; then
998 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
999 echo "usage: qpid [[--exact] <process name|pid>"
1004 if [ "$EXE" ] ; then
1005 qpid | \grep "$prepend$EXE$append"
1009 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1017 if [ "$1" = "--exact" ]; then
1023 if [ "$EXE" ] ; then
1024 local PID=`adb shell ps \
1026 | \grep "$prepend$EXE$append" \
1027 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1030 echo "usage: pid [--exact] <process name>"
1035 # coredump_setup - enable core dumps globally for any process
1036 # that has the core-file-size limit set correctly
1038 # NOTE: You must call also coredump_enable for a specific process
1039 # if its core-file-size limit is not set already.
1040 # NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1042 function coredump_setup()
1044 echo "Getting root...";
1046 adb wait-for-device;
1048 echo "Remounting root partition read-write...";
1049 adb shell mount -w -o remount -t rootfs rootfs;
1051 adb wait-for-device;
1052 adb shell mkdir -p /cores;
1053 adb shell mount -t tmpfs tmpfs /cores;
1054 adb shell chmod 0777 /cores;
1056 echo "Granting SELinux permission to dump in /cores...";
1057 adb shell restorecon -R /cores;
1059 echo "Set core pattern.";
1060 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1065 # coredump_enable - enable core dumps for the specified process
1066 # $1 = PID of process (e.g., $(pid mediaserver))
1068 # NOTE: coredump_setup must have been called as well for a core
1069 # dump to actually be generated.
1071 function coredump_enable()
1074 if [ -z "$PID" ]; then
1075 printf "Expecting a PID!\n";
1078 echo "Setting core limit for $PID to infinite...";
1079 adb shell prlimit $PID 4 -1 -1
1082 # core - send SIGV and pull the core for process
1083 # $1 = PID of process (e.g., $(pid mediaserver))
1085 # NOTE: coredump_setup must be called once per boot for core dumps to be
1092 if [ -z "$PID" ]; then
1093 printf "Expecting a PID!\n";
1097 local CORENAME=core.$PID;
1098 local COREPATH=/cores/$CORENAME;
1104 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1105 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1106 adb shell kill -$SIG $PID;
1110 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1111 echo "Done: core is under $COREPATH on device.";
1114 # systemstack - dump the current stack trace of all threads in the system process
1115 # to the usual ANR traces file
1116 function systemstack()
1118 stacks system_server
1123 if [[ $1 =~ ^[0-9]+$ ]] ; then
1125 elif [ "$1" ] ; then
1126 local PIDLIST="$(pid $1)"
1127 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1128 local PID="$PIDLIST"
1129 elif [ "$PIDLIST" ] ; then
1130 echo "more than one process: $1"
1132 echo "no such process: $1"
1135 echo "usage: stacks [pid|process name]"
1138 if [ "$PID" ] ; then
1139 # Determine whether the process is native
1140 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1141 # Dump stacks of Dalvik process
1142 local TRACES=/data/anr/traces.txt
1143 local ORIG=/data/anr/traces.orig
1144 local TMP=/data/anr/traces.tmp
1146 # Keep original traces to avoid clobbering
1147 adb shell mv $TRACES $ORIG
1149 # Make sure we have a usable file
1150 adb shell touch $TRACES
1151 adb shell chmod 666 $TRACES
1153 # Dump stacks and wait for dump to finish
1154 adb shell kill -3 $PID
1155 adb shell notify $TRACES >/dev/null
1157 # Restore original stacks, and show current output
1158 adb shell mv $TRACES $TMP
1159 adb shell mv $ORIG $TRACES
1162 # Dump stacks of native process
1163 local USE64BIT="$(is64bit $PID)"
1164 adb shell debuggerd$USE64BIT -b $PID
1169 # Read the ELF header from /proc/$PID/exe to determine if the process is
1174 if [ "$PID" ] ; then
1175 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
1189 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl|vts)' \
1190 -exec grep --color -n "$@" {} +
1197 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\)' \
1198 -exec grep --color -n "$@" {} +
1203 function gettargetarch
1205 get_build_var TARGET_ARCH
1210 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1211 -exec grep --color -n "$@" {} +
1216 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1217 -exec grep --color -n "$@" {} +
1222 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' \) \
1223 -exec grep --color -n "$@" {} +
1228 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1229 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1235 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1236 -exec grep --color -n "$@" {} +
1241 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1242 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
1247 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1248 -exec grep --color -n "$@" {} +
1255 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' \
1256 -exec grep --color -n "$@" {} +
1261 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' \
1262 -exec grep --color -n -i "$@" {} +
1269 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f \
1270 -exec grep --color -n "$@" {} +
1275 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f \
1276 -exec grep --color -n -i "$@" {} +
1282 function getprebuilt
1284 get_abs_build_var ANDROID_PREBUILTS
1287 function tracedmdump()
1291 echo "Couldn't locate the top of the tree. Try setting TOP."
1294 local prebuiltdir=$(getprebuilt)
1295 local arch=$(gettargetarch)
1296 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
1299 if [ ! "$TRACE" ] ; then
1300 echo "usage: tracedmdump tracename"
1304 if [ ! -r "$KERNEL" ] ; then
1305 echo "Error: cannot find kernel: '$KERNEL'"
1309 local BASETRACE=$(basename $TRACE)
1310 if [ "$BASETRACE" = "$TRACE" ] ; then
1311 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1314 echo "post-processing traces..."
1315 rm -f $TRACE/qtrace.dexlist
1317 if [ $? -ne 0 ]; then
1319 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1323 echo "generating dexlist output..."
1324 /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
1325 echo "generating dmtrace data..."
1326 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1327 echo "generating html file..."
1328 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1329 echo "done, see $TRACE/dmtrace.html for details"
1331 echo " traceview $TRACE/dmtrace"
1334 # communicate with a running device or emulator, set up necessary state,
1335 # and run the hat command.
1338 # process standard adb options
1340 if [ "$1" = "-d" -o "$1" = "-e" ]; then
1343 elif [ "$1" = "-s" ]; then
1347 local adbOptions=${adbTarget}
1348 #echo adbOptions = ${adbOptions}
1353 if [ "$targetPid" = "" ]; then
1354 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
1358 # confirm hat is available
1359 if [ -z $(which hat) ]; then
1360 echo "hat is not available in this configuration."
1364 # issue "am" command to cause the hprof dump
1365 local devFile=/data/local/tmp/hprof-$targetPid
1366 echo "Poking $targetPid and waiting for data..."
1367 echo "Storing data at $devFile"
1368 adb ${adbOptions} shell am dumpheap $targetPid $devFile
1369 echo "Press enter when logcat shows \"hprof: heap dump completed\""
1373 local localFile=/tmp/$$-hprof
1375 echo "Retrieving file $devFile..."
1376 adb ${adbOptions} pull $devFile $localFile
1378 adb ${adbOptions} shell rm $devFile
1380 echo "Running hat on $localFile"
1381 echo "View the output by pointing your browser at http://localhost:7000/"
1383 hat -JXmx512m $localFile
1386 function getbugreports()
1388 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
1390 if [ ! "$reports" ]; then
1391 echo "Could not locate any bugreports."
1396 for report in ${reports[@]}
1398 echo "/sdcard/bugreports/${report}"
1399 adb pull /sdcard/bugreports/${report} ${report}
1404 function getsdcardpath()
1406 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1409 function getscreenshotpath()
1411 echo "$(getsdcardpath)/Pictures/Screenshots"
1414 function getlastscreenshot()
1416 local screenshot_path=$(getscreenshotpath)
1417 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1418 if [ "$screenshot" = "" ]; then
1419 echo "No screenshots found."
1422 echo "${screenshot}"
1423 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1426 function startviewserver()
1429 if [ $# -gt 0 ]; then
1432 adb shell service call window 1 i32 $port
1435 function stopviewserver()
1437 adb shell service call window 2
1440 function isviewserverstarted()
1442 adb shell service call window 3
1447 adb shell input keyevent 3
1452 adb shell input keyevent 4
1457 adb shell input keyevent 82
1460 function smoketest()
1462 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1463 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1468 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1472 (\cd "$T" && mmm tests/SmokeTest) &&
1473 adb uninstall com.android.smoketest > /dev/null &&
1474 adb uninstall com.android.smoketest.tests > /dev/null &&
1475 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1476 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1477 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1480 # simple shortcut to the runtest command
1485 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1488 ("$T"/development/testrunner/runtest.py $@)
1492 if [[ -z "$1" ]]; then
1493 echo "Usage: godir <regex>"
1497 if [ ! "$OUT_DIR" = "" ]; then
1499 FILELIST=$OUT_DIR/filelist
1501 FILELIST=$T/filelist
1503 if [[ ! -f $FILELIST ]]; then
1504 echo -n "Creating index..."
1505 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
1510 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
1511 if [[ ${#lines[@]} = 0 ]]; then
1517 if [[ ${#lines[@]} > 1 ]]; then
1518 while [[ -z "$pathname" ]]; do
1521 for line in ${lines[@]}; do
1522 printf "%6s %s\n" "[$index]" $line
1523 index=$(($index + 1))
1526 echo -n "Select one: "
1529 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1530 echo "Invalid choice"
1533 pathname=${lines[$(($choice-1))]}
1536 pathname=${lines[0]}
1541 # Force JAVA_HOME to point to java 1.7/1.8 if it isn't already set.
1542 function set_java_home() {
1543 # Clear the existing JAVA_HOME value if we set it ourselves, so that
1544 # we can reset it later, depending on the version of java the build
1547 # If we don't do this, the JAVA_HOME value set by the first call to
1548 # build/envsetup.sh will persist forever.
1549 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
1553 if [ ! "$JAVA_HOME" ]; then
1554 if [ -n "$LEGACY_USE_JAVA7" ]; then
1555 echo Warning: Support for JDK 7 will be dropped. Switch to JDK 8.
1558 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
1561 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
1567 export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
1570 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
1575 # Keep track of the fact that we set JAVA_HOME ourselves, so that
1576 # we can change it on the next envsetup.sh, if required.
1577 export ANDROID_SET_JAVA_HOME=true
1581 # Print colored exit condition
1585 if [ $retval -ne 0 ]
1587 echo $'\E'"[0;31mFAILURE\e[00m"
1589 echo $'\E'"[0;32mSUCCESS\e[00m"
1594 function get_make_command()
1601 local start_time=$(date +"%s")
1604 local end_time=$(date +"%s")
1605 local tdiff=$(($end_time-$start_time))
1606 local hours=$(($tdiff / 3600 ))
1607 local mins=$((($tdiff % 3600) / 60))
1608 local secs=$(($tdiff % 60))
1609 local ncolors=$(tput colors 2>/dev/null)
1610 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
1611 color_failed=$'\E'"[0;31m"
1612 color_success=$'\E'"[0;32m"
1613 color_reset=$'\E'"[00m"
1620 if [ $ret -eq 0 ] ; then
1621 echo -n "${color_success}#### make completed successfully "
1623 echo -n "${color_failed}#### make failed to build some targets "
1625 if [ $hours -gt 0 ] ; then
1626 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1627 elif [ $mins -gt 0 ] ; then
1628 printf "(%02g:%02g (mm:ss))" $mins $secs
1629 elif [ $secs -gt 0 ] ; then
1630 printf "(%s seconds)" $secs
1632 echo " ####${color_reset}"
1637 function provision()
1639 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1640 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1643 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1644 echo "There is no provisioning script for the device." >&2
1648 # Check if user really wants to do this.
1649 if [ "$1" = "--no-confirmation" ]; then
1652 echo "This action will reflash your device."
1654 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1656 echo -n "Are you sure you want to do this (yes/no)? "
1658 if [[ "${REPLY}" != "yes" ]] ; then
1659 echo "Not taking any action. Exiting." >&2
1663 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1668 mk_timer $(get_make_command) "$@"
1671 function __detect_shell() {
1672 case `ps -o command -p $$` in
1688 if ! __detect_shell > /dev/null; then
1689 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
1692 # Execute the contents of any vendorsetup.sh files we can find.
1693 for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
1694 `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
1695 `test -d product && find -L product -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
1703 check_bash_version && {
1704 dirs="sdk/bash_completion vendor/cm/bash_completion"
1705 for dir in $dirs; do
1706 if [ -d ${dir} ]; then
1707 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
1715 export ANDROID_BUILD_TOP=$(gettop)