3 Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
4 - croot: Changes directory to the top of the tree.
5 - m: Makes from the top of the tree.
6 - mm: Builds all of the modules in the current directory.
7 - mmm: Builds all of the modules in the supplied directories.
8 - cgrep: Greps on all local C/C++ files.
9 - jgrep: Greps on all local Java files.
10 - resgrep: Greps on all local res/*.xml files.
11 - godir: Go to the directory containing a file.
13 Look at the source to view more functions. The complete list is:
18 for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
24 # Get the value of a build variable as an absolute path.
25 function get_abs_build_var()
29 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
32 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
33 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1
36 # Get the exact value of a build variable.
37 function get_build_var()
41 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
44 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
45 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
48 # check to see if the supplied product is one we can build
49 function check_product()
53 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
56 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
57 TARGET_PRODUCT=$1 TARGET_BUILD_VARIANT= \
58 TARGET_SIMULATOR= TARGET_BUILD_TYPE= \
60 get_build_var TARGET_DEVICE > /dev/null
61 # hide successful answers, but allow the errors to show
64 VARIANT_CHOICES=(user userdebug eng)
66 # check to see if the supplied variant is valid
67 function check_variant()
69 for v in ${VARIANT_CHOICES[@]}
83 echo "Couldn't locate the top of the tree. Try setting TOP."
87 ##################################################################
89 # Read me before you modify this code #
91 # This function sets ANDROID_BUILD_PATHS to what it is adding #
92 # to PATH, and the next time it is run, it removes that from #
93 # PATH. This is required so lunch can be run more than once #
94 # and still have working paths. #
96 ##################################################################
99 if [ -n $ANDROID_BUILD_PATHS ] ; then
100 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
103 # and in with the new
105 prebuiltdir=$(getprebuilt)
106 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.4.0/bin
107 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
108 export ANDROID_QTOOLS=$T/development/emulator/qtools
109 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
110 export PATH=$PATH$ANDROID_BUILD_PATHS
112 unset ANDROID_PRODUCT_OUT
113 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
114 export OUT=$ANDROID_PRODUCT_OUT
116 # needed for building linux on MacOS
118 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
120 # needed for OProfile to post-process collected samples
121 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
124 function printconfig()
128 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
131 get_build_var report_config
134 function set_stuff_for_environment()
140 # Don't try to do preoptimization until it works better on OSX.
141 export DISABLE_DEXPREOPT=true
143 export ANDROID_BUILD_TOP=$(gettop)
146 function set_sequence_number()
148 export BUILD_ENV_SEQUENCE_NUMBER=9
153 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
154 local product=$TARGET_PRODUCT
155 local variant=$TARGET_BUILD_VARIANT
156 local apps=$TARGET_BUILD_APPS
157 if [ -z "$apps" ]; then
158 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
160 export PROMPT_COMMAND="echo -ne \"\033]0;[$apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\""
169 echo "Build for the simulator or the device?"
174 export TARGET_SIMULATOR=
176 while [ -z $TARGET_SIMULATOR ]
178 echo -n "Which would you like? [1] "
179 if [ -z "$1" ] ; then
187 export TARGET_SIMULATOR=false
190 export TARGET_SIMULATOR=false
193 export TARGET_SIMULATOR=false
196 export TARGET_SIMULATOR=true
199 export TARGET_SIMULATOR=true
203 echo "I didn't understand your response. Please try again."
207 if [ -n "$1" ] ; then
212 set_stuff_for_environment
218 echo "Only device builds are supported for" `uname -s`
219 echo " Forcing TARGET_SIMULATOR=false"
223 echo -n "Press enter: "
227 export TARGET_SIMULATOR=false
228 set_stuff_for_environment
233 function choosetype()
235 echo "Build type choices are:"
240 local DEFAULT_NUM DEFAULT_VALUE
241 if [ $TARGET_SIMULATOR = "false" ] ; then
243 DEFAULT_VALUE=release
249 export TARGET_BUILD_TYPE=
251 while [ -z $TARGET_BUILD_TYPE ]
253 echo -n "Which would you like? ["$DEFAULT_NUM"] "
254 if [ -z "$1" ] ; then
262 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
265 export TARGET_BUILD_TYPE=release
268 export TARGET_BUILD_TYPE=release
271 export TARGET_BUILD_TYPE=debug
274 export TARGET_BUILD_TYPE=debug
278 echo "I didn't understand your response. Please try again."
282 if [ -n "$1" ] ; then
287 set_stuff_for_environment
291 # This function isn't really right: It chooses a TARGET_PRODUCT
292 # based on the list of boards. Usually, that gets you something
293 # that kinda works with a generic product, but really, you should
294 # pick a product by name.
296 function chooseproduct()
298 if [ "x$TARGET_PRODUCT" != x ] ; then
299 default_value=$TARGET_PRODUCT
301 if [ "$TARGET_SIMULATOR" = true ] ; then
304 default_value=generic
308 export TARGET_PRODUCT=
310 while [ -z "$TARGET_PRODUCT" ]
312 echo -n "Which product would you like? [$default_value] "
313 if [ -z "$1" ] ; then
320 if [ -z "$ANSWER" ] ; then
321 export TARGET_PRODUCT=$default_value
323 if check_product $ANSWER
325 export TARGET_PRODUCT=$ANSWER
327 echo "** Not a valid product: $ANSWER"
330 if [ -n "$1" ] ; then
335 set_stuff_for_environment
338 function choosevariant()
340 echo "Variant choices are:"
343 for v in ${VARIANT_CHOICES[@]}
345 # The product name is the name of the directory containing
346 # the makefile we found, above.
351 local default_value=eng
354 export TARGET_BUILD_VARIANT=
355 while [ -z "$TARGET_BUILD_VARIANT" ]
357 echo -n "Which would you like? [$default_value] "
358 if [ -z "$1" ] ; then
365 if [ -z "$ANSWER" ] ; then
366 export TARGET_BUILD_VARIANT=$default_value
367 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
368 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
369 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
372 if check_variant $ANSWER
374 export TARGET_BUILD_VARIANT=$ANSWER
376 echo "** Not a valid variant: $ANSWER"
379 if [ -n "$1" ] ; then
385 function choosecombo()
402 set_stuff_for_environment
406 # Clear this variable. It will be built up again when the vendorsetup.sh
407 # files are included at the end of this file.
408 unset LUNCH_MENU_CHOICES
409 function add_lunch_combo()
413 for c in ${LUNCH_MENU_CHOICES[@]} ; do
414 if [ "$new_combo" = "$c" ] ; then
418 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
421 # add the default one here
422 add_lunch_combo generic-eng
423 add_lunch_combo generic_x86-eng
425 # if we're on linux, add the simulator. There is a special case
426 # in lunch to deal with the simulator
427 if [ "$(uname)" = "Linux" ] ; then
428 add_lunch_combo simulator
431 function print_lunch_menu()
435 echo "You're building on" $uname
437 echo "Lunch menu... pick a combo:"
441 for choice in ${LUNCH_MENU_CHOICES[@]}
458 echo -n "Which would you like? [generic-eng] "
466 selection=generic-eng
467 elif [ "$answer" = "simulator" ]
470 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
472 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
474 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
476 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
481 if [ -z "$selection" ]
484 echo "Invalid lunch combo: $answer"
488 export TARGET_BUILD_APPS=
490 # special case the simulator
491 if [ "$selection" = "simulator" ]
493 export TARGET_PRODUCT=sim
494 export TARGET_BUILD_VARIANT=eng
495 export TARGET_SIMULATOR=true
496 export TARGET_BUILD_TYPE=debug
498 local product=$(echo -n $selection | sed -e "s/-.*$//")
499 check_product $product
503 echo "** Don't have a product spec for: '$product'"
504 echo "** Do you have the right repo manifest?"
508 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
509 check_variant $variant
513 echo "** Invalid variant: '$variant'"
514 echo "** Must be one of ${VARIANT_CHOICES[@]}"
518 if [ -z "$product" -o -z "$variant" ]
524 export TARGET_PRODUCT=$product
525 export TARGET_BUILD_VARIANT=$variant
526 export TARGET_SIMULATOR=false
527 export TARGET_BUILD_TYPE=release
532 set_stuff_for_environment
536 # Configures the build to build unbundled apps.
537 # Run tapas with one ore more app names (from LOCAL_PACKAGE_NAME)
540 local variant=$(echo -n $(echo $* | xargs -n 1 echo | grep -E '^(user|userdebug|eng)$'))
541 local apps=$(echo -n $(echo $* | xargs -n 1 echo | grep -E -v '^(user|userdebug|eng)$'))
543 if [ $(echo $variant | wc -w) -gt 1 ]; then
544 echo "tapas: Error: Multiple build variants supplied: $variant"
547 if [ -z "$variant" ]; then
550 if [ -z "$apps" ]; then
554 export TARGET_PRODUCT=generic
555 export TARGET_BUILD_VARIANT=$variant
556 export TARGET_SIMULATOR=false
557 export TARGET_BUILD_TYPE=release
558 export TARGET_BUILD_APPS=$apps
560 set_stuff_for_environment
566 local TOPFILE=build/core/envsetup.mk
567 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
570 if [ -f $TOPFILE ] ; then
571 # The following circumlocution (repeated below as well) ensures
572 # that we record the true directory name and not one that is
573 # faked up with symlink names.
576 # We redirect cd to /dev/null in case it's aliased to
577 # a command that prints something as a side-effect
581 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
586 if [ -f "$T/$TOPFILE" ]; then
599 echo "Couldn't locate the top of the tree. Try setting TOP."
603 function findmakefile()
605 TOPFILE=build/core/envsetup.mk
606 # We redirect cd to /dev/null in case it's aliased to
607 # a command that prints something as a side-effect
611 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
613 if [ -f "$T/Android.mk" ]; then
625 # If we're sitting in the root of the build tree, just do a
627 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
630 # Find the closest Android.mk file.
632 local M=$(findmakefile)
633 # Remove the path to top as the makefilepath needs to be relative
634 local M=`echo $M|sed 's:'$T'/::'`
636 echo "Couldn't locate the top of the tree. Try setting TOP."
637 elif [ ! "$M" ]; then
638 echo "Couldn't locate a makefile from the current directory."
640 ONE_SHOT_MAKEFILE=$M make -C $T all_modules $@
652 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
653 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
654 for DIR in $DIRS ; do
655 DIR=`echo $DIR | sed -e 's:/$::'`
656 if [ -f $DIR/Android.mk ]; then
657 TO_CHOP=`echo $T | wc -c | tr -d ' '`
658 TO_CHOP=`expr $TO_CHOP + 1`
659 START=`PWD= /bin/pwd`
660 MFILE=`echo $START | cut -c${TO_CHOP}-`
661 if [ "$MFILE" = "" ] ; then
662 MFILE=$DIR/Android.mk
664 MFILE=$MFILE/$DIR/Android.mk
666 MAKEFILE="$MAKEFILE $MFILE"
668 if [ "$DIR" = snod ]; then
670 elif [ "$DIR" = showcommands ]; then
671 ARGS="$ARGS showcommands"
672 elif [ "$DIR" = dist ]; then
675 echo "No Android.mk in $DIR."
680 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS all_modules $ARGS
682 echo "Couldn't locate the top of the tree. Try setting TOP."
692 echo "Couldn't locate the top of the tree. Try setting TOP."
698 TOPFILE=build/core/envsetup.mk
699 # We redirect cd to /dev/null in case it's aliased to
700 # a command that prints something as a side-effect
704 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
706 if [ -f "$T/Android.mk" ]; then
713 echo "can't find Android.mk"
720 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
723 echo "usage: pid name"
727 # systemstack - dump the current stack trace of all threads in the system process
728 # to the usual ANR traces file
729 function systemstack()
731 adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
736 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
737 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
738 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
739 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
740 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
741 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
750 if [ "$PORT" ] ; then
758 if [ "$PROG" ] ; then
760 adb forward "tcp$PORT" "tcp$PORT"
761 adb shell gdbserver $PORT --attach $PID &
765 echo "If you haven't done so already, do this first on the device:"
766 echo " gdbserver $PORT /system/bin/$EXE"
768 echo " gdbserver $PORT --attach $PID"
772 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
773 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
774 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
775 echo >>"$OUT_ROOT/gdbclient.cmds" ""
777 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
779 echo "Unable to determine build system output dir."
788 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
795 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
802 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
807 find . -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
812 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
819 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
824 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
831 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
836 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
844 get_abs_build_var ANDROID_PREBUILTS
847 function tracedmdump()
851 echo "Couldn't locate the top of the tree. Try setting TOP."
854 local prebuiltdir=$(getprebuilt)
855 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
858 if [ ! "$TRACE" ] ; then
859 echo "usage: tracedmdump tracename"
863 if [ ! -r "$KERNEL" ] ; then
864 echo "Error: cannot find kernel: '$KERNEL'"
868 local BASETRACE=$(basename $TRACE)
869 if [ "$BASETRACE" = "$TRACE" ] ; then
870 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
873 echo "post-processing traces..."
874 rm -f $TRACE/qtrace.dexlist
876 if [ $? -ne 0 ]; then
878 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
882 echo "generating dexlist output..."
883 /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
884 echo "generating dmtrace data..."
885 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
886 echo "generating html file..."
887 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
888 echo "done, see $TRACE/dmtrace.html for details"
890 echo " traceview $TRACE/dmtrace"
893 # communicate with a running device or emulator, set up necessary state,
894 # and run the hat command.
897 # process standard adb options
899 if [ $1 = "-d" -o $1 = "-e" ]; then
902 elif [ $1 = "-s" ]; then
906 local adbOptions=${adbTarget}
907 echo adbOptions = ${adbOptions}
913 if [ "$targetPid" = "" ]; then
914 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
918 # confirm hat is available
919 if [ -z $(which hat) ]; then
920 echo "hat is not available in this configuration."
924 adb ${adbOptions} shell >/dev/null mkdir /data/misc
925 adb ${adbOptions} shell chmod 777 /data/misc
927 # send a SIGUSR1 to cause the hprof dump
928 echo "Poking $targetPid and waiting for data..."
929 adb ${adbOptions} shell kill -10 $targetPid
930 echo "Press enter when logcat shows \"hprof: heap dump completed\""
934 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
935 local devFile=/data/misc/${availFiles[0]}
936 local localFile=/tmp/$$-hprof
938 echo "Retrieving file $devFile..."
939 adb ${adbOptions} pull $devFile $localFile
941 adb ${adbOptions} shell rm $devFile
943 echo "Running hat on $localFile"
944 echo "View the output by pointing your browser at http://localhost:7000/"
949 function getbugreports()
951 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
953 if [ ! "$reports" ]; then
954 echo "Could not locate any bugreports."
959 for report in ${reports[@]}
961 echo "/sdcard/bugreports/${report}"
962 adb pull /sdcard/bugreports/${report} ${report}
967 function startviewserver()
970 if [ $# -gt 0 ]; then
973 adb shell service call window 1 i32 $port
976 function stopviewserver()
978 adb shell service call window 2
981 function isviewserverstarted()
983 adb shell service call window 3
988 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
989 echo "Couldn't locate output files. Try running 'lunch' first." >&2
994 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
998 (cd "$T" && mmm tests/SmokeTest) &&
999 adb uninstall com.android.smoketest > /dev/null &&
1000 adb uninstall com.android.smoketest.tests > /dev/null &&
1001 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1002 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1003 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1006 # simple shortcut to the runtest command
1011 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1014 ("$T"/development/testrunner/runtest.py $@)
1018 if [[ -z "$1" ]]; then
1019 echo "Usage: godir <regex>"
1023 if [[ ! -f $T/filelist ]]; then
1024 echo -n "Creating index..."
1025 (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1030 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1031 if [[ ${#lines[@]} = 0 ]]; then
1037 if [[ ${#lines[@]} > 1 ]]; then
1038 while [[ -z "$pathname" ]]; do
1041 for line in ${lines[@]}; do
1042 printf "%6s %s\n" "[$index]" $line
1043 index=$(($index + 1))
1046 echo -n "Select one: "
1049 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1050 echo "Invalid choice"
1053 pathname=${lines[$(($choice-$_arrayoffset))]}
1056 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1057 pathname=${lines[0]}
1062 # Force JAVA_HOME to point to java 1.5 if it isn't already set
1063 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
1064 if [ ! "$JAVA_HOME" ]; then
1067 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
1070 export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
1076 # determine whether arrays are zero-based (bash) or one-based (zsh)
1078 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1086 # Execute the contents of any vendorsetup.sh files we can find.
1087 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`