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= \
59 get_build_var TARGET_DEVICE > /dev/null
60 # hide successful answers, but allow the errors to show
63 VARIANT_CHOICES=(user userdebug eng)
65 # check to see if the supplied variant is valid
66 function check_variant()
68 for v in ${VARIANT_CHOICES[@]}
82 echo "Couldn't locate the top of the tree. Try setting TOP."
86 ##################################################################
88 # Read me before you modify this code #
90 # This function sets ANDROID_BUILD_PATHS to what it is adding #
91 # to PATH, and the next time it is run, it removes that from #
92 # PATH. This is required so lunch can be run more than once #
93 # and still have working paths. #
95 ##################################################################
98 if [ -n $ANDROID_BUILD_PATHS ] ; then
99 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
102 # and in with the new
104 prebuiltdir=$(getprebuilt)
105 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.2.1/bin
106 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
107 export ANDROID_QTOOLS=$T/development/emulator/qtools
108 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
109 export PATH=$PATH$ANDROID_BUILD_PATHS
111 unset ANDROID_PRODUCT_OUT
112 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
113 export OUT=$ANDROID_PRODUCT_OUT
115 # needed for building linux on MacOS
117 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
119 # needed for OProfile to post-process collected samples
120 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
123 function printconfig()
127 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
130 get_build_var report_config
133 function set_stuff_for_environment()
139 # Don't try to do preoptimization until it works better on OSX.
140 export DISABLE_DEXPREOPT=true
142 export ANDROID_BUILD_TOP=$(gettop)
145 function set_sequence_number()
147 export BUILD_ENV_SEQUENCE_NUMBER=9
152 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
153 local product=$(get_build_var TARGET_PRODUCT)
154 local variant=$(get_build_var TARGET_BUILD_VARIANT)
155 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
163 echo "Build for the simulator or the device?"
168 export TARGET_SIMULATOR=
170 while [ -z $TARGET_SIMULATOR ]
172 echo -n "Which would you like? [1] "
173 if [ -z "$1" ] ; then
181 export TARGET_SIMULATOR=false
184 export TARGET_SIMULATOR=false
187 export TARGET_SIMULATOR=false
190 export TARGET_SIMULATOR=true
193 export TARGET_SIMULATOR=true
197 echo "I didn't understand your response. Please try again."
201 if [ -n "$1" ] ; then
206 set_stuff_for_environment
212 echo "Only device builds are supported for" `uname -s`
213 echo " Forcing TARGET_SIMULATOR=false"
217 echo -n "Press enter: "
221 export TARGET_SIMULATOR=false
222 set_stuff_for_environment
227 function choosetype()
229 echo "Build type choices are:"
234 local DEFAULT_NUM DEFAULT_VALUE
235 if [ $TARGET_SIMULATOR = "false" ] ; then
237 DEFAULT_VALUE=release
243 export TARGET_BUILD_TYPE=
245 while [ -z $TARGET_BUILD_TYPE ]
247 echo -n "Which would you like? ["$DEFAULT_NUM"] "
248 if [ -z "$1" ] ; then
256 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
259 export TARGET_BUILD_TYPE=release
262 export TARGET_BUILD_TYPE=release
265 export TARGET_BUILD_TYPE=debug
268 export TARGET_BUILD_TYPE=debug
272 echo "I didn't understand your response. Please try again."
276 if [ -n "$1" ] ; then
281 set_stuff_for_environment
285 # This function isn't really right: It chooses a TARGET_PRODUCT
286 # based on the list of boards. Usually, that gets you something
287 # that kinda works with a generic product, but really, you should
288 # pick a product by name.
290 function chooseproduct()
292 if [ "x$TARGET_PRODUCT" != x ] ; then
293 default_value=$TARGET_PRODUCT
295 if [ "$TARGET_SIMULATOR" = true ] ; then
298 default_value=generic
302 export TARGET_PRODUCT=
304 while [ -z "$TARGET_PRODUCT" ]
306 echo -n "Which product would you like? [$default_value] "
307 if [ -z "$1" ] ; then
314 if [ -z "$ANSWER" ] ; then
315 export TARGET_PRODUCT=$default_value
317 if check_product $ANSWER
319 export TARGET_PRODUCT=$ANSWER
321 echo "** Not a valid product: $ANSWER"
324 if [ -n "$1" ] ; then
329 set_stuff_for_environment
332 function choosevariant()
334 echo "Variant choices are:"
337 for v in ${VARIANT_CHOICES[@]}
339 # The product name is the name of the directory containing
340 # the makefile we found, above.
345 local default_value=eng
348 export TARGET_BUILD_VARIANT=
349 while [ -z "$TARGET_BUILD_VARIANT" ]
351 echo -n "Which would you like? [$default_value] "
352 if [ -z "$1" ] ; then
359 if [ -z "$ANSWER" ] ; then
360 export TARGET_BUILD_VARIANT=$default_value
361 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
362 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
363 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
366 if check_variant $ANSWER
368 export TARGET_BUILD_VARIANT=$ANSWER
370 echo "** Not a valid variant: $ANSWER"
373 if [ -n "$1" ] ; then
384 function choosecombo()
401 set_stuff_for_environment
405 # Clear this variable. It will be built up again when the vendorsetup.sh
406 # files are included at the end of this file.
407 unset LUNCH_MENU_CHOICES
408 function add_lunch_combo()
412 for c in ${LUNCH_MENU_CHOICES[@]} ; do
413 if [ "$new_combo" = "$c" ] ; then
417 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
420 # add the default one here
421 add_lunch_combo generic-eng
423 # if we're on linux, add the simulator. There is a special case
424 # in lunch to deal with the simulator
425 if [ "$(uname)" = "Linux" ] ; then
426 add_lunch_combo simulator
429 function print_lunch_menu()
433 echo "You're building on" $uname
435 echo ${LUNCH_MENU_CHOICES[@]}
436 echo "Lunch menu... pick a combo:"
440 for choice in ${LUNCH_MENU_CHOICES[@]}
457 echo -n "Which would you like? [generic-eng] "
465 selection=generic-eng
466 elif [ "$answer" = "simulator" ]
469 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
471 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
473 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
475 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
480 if [ -z "$selection" ]
483 echo "Invalid lunch combo: $answer"
487 # special case the simulator
488 if [ "$selection" = "simulator" ]
490 export TARGET_PRODUCT=sim
491 export TARGET_BUILD_VARIANT=eng
492 export TARGET_SIMULATOR=true
493 export TARGET_BUILD_TYPE=debug
495 local product=$(echo -n $selection | sed -e "s/-.*$//")
496 check_product $product
500 echo "** Don't have a product spec for: '$product'"
501 echo "** Do you have the right repo manifest?"
505 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
506 check_variant $variant
510 echo "** Invalid variant: '$variant'"
511 echo "** Must be one of ${VARIANT_CHOICES[@]}"
515 if [ -z "$product" -o -z "$variant" ]
521 export TARGET_PRODUCT=$product
522 export TARGET_BUILD_VARIANT=$variant
523 export TARGET_SIMULATOR=false
524 export TARGET_BUILD_TYPE=release
529 set_stuff_for_environment
535 local TOPFILE=build/core/envsetup.mk
536 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
539 if [ -f $TOPFILE ] ; then
542 # We redirect cd to /dev/null in case it's aliased to
543 # a command that prints something as a side-effect
547 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
552 if [ -f "$T/$TOPFILE" ]; then
565 echo "Couldn't locate the top of the tree. Try setting TOP."
569 function findmakefile()
571 TOPFILE=build/core/envsetup.mk
572 # We redirect cd to /dev/null in case it's aliased to
573 # a command that prints something as a side-effect
577 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
579 if [ -f "$T/Android.mk" ]; then
591 # If we're sitting in the root of the build tree, just do a
593 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
596 # Find the closest Android.mk file.
598 local M=$(findmakefile)
600 echo "Couldn't locate the top of the tree. Try setting TOP."
601 elif [ ! "$M" ]; then
602 echo "Couldn't locate a makefile from the current directory."
604 ONE_SHOT_MAKEFILE=$M make -C $T files $@
616 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
617 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
618 for DIR in $DIRS ; do
619 DIR=`echo $DIR | sed -e 's:/$::'`
620 if [ -f $DIR/Android.mk ]; then
621 TO_CHOP=`echo $T | wc -c | tr -d ' '`
622 TO_CHOP=`expr $TO_CHOP + 1`
623 MFILE=`echo $PWD | cut -c${TO_CHOP}-`
624 if [ "$MFILE" = "" ] ; then
625 MFILE=$DIR/Android.mk
627 MFILE=$MFILE/$DIR/Android.mk
629 MAKEFILE="$MAKEFILE $MFILE"
631 if [ "$DIR" = snod ]; then
633 elif [ "$DIR" = showcommands ]; then
634 ARGS="$ARGS showcommands"
636 echo "No Android.mk in $DIR."
640 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
642 echo "Couldn't locate the top of the tree. Try setting TOP."
652 echo "Couldn't locate the top of the tree. Try setting TOP."
660 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
663 echo "usage: pid name"
669 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
670 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
671 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
672 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
673 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
674 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
683 if [ "$PORT" ] ; then
691 if [ "$PROG" ] ; then
693 adb forward "tcp$PORT" "tcp$PORT"
694 adb shell gdbserver $PORT --attach $PID &
698 echo "If you haven't done so already, do this first on the device:"
699 echo " gdbserver $PORT /system/bin/$EXE"
701 echo " gdbserver $PORT --attach $PID"
705 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
706 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
707 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
708 echo >>"$OUT_ROOT/gdbclient.cmds" ""
710 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
712 echo "Unable to determine build system output dir."
721 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n "$@"
728 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\)' -print0 | xargs -0 grep --color -n "$@"
735 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
740 find . -type f -name "*\.c*" -print0 | xargs -0 grep --color -n "$@"
745 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
752 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
757 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
764 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
769 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
777 get_abs_build_var ANDROID_PREBUILTS
780 function tracedmdump()
784 echo "Couldn't locate the top of the tree. Try setting TOP."
787 local prebuiltdir=$(getprebuilt)
788 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
791 if [ ! "$TRACE" ] ; then
792 echo "usage: tracedmdump tracename"
796 if [ ! -r "$KERNEL" ] ; then
797 echo "Error: cannot find kernel: '$KERNEL'"
801 local BASETRACE=$(basename $TRACE)
802 if [ "$BASETRACE" = "$TRACE" ] ; then
803 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
806 echo "post-processing traces..."
807 rm -f $TRACE/qtrace.dexlist
809 if [ $? -ne 0 ]; then
811 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
815 echo "generating dexlist output..."
816 /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
817 echo "generating dmtrace data..."
818 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
819 echo "generating html file..."
820 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
821 echo "done, see $TRACE/dmtrace.html for details"
823 echo " traceview $TRACE/dmtrace"
826 # communicate with a running device or emulator, set up necessary state,
827 # and run the hat command.
830 # process standard adb options
832 if [ $1 = "-d" -o $1 = "-e" ]; then
835 elif [ $1 = "-s" ]; then
839 local adbOptions=${adbTarget}
840 echo adbOptions = ${adbOptions}
846 if [ "$targetPid" = "" ]; then
847 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
851 # confirm hat is available
852 if [ -z $(which hat) ]; then
853 echo "hat is not available in this configuration."
857 adb ${adbOptions} shell >/dev/null mkdir /data/misc
858 adb ${adbOptions} shell chmod 777 /data/misc
860 # send a SIGUSR1 to cause the hprof dump
861 echo "Poking $targetPid and waiting for data..."
862 adb ${adbOptions} shell kill -10 $targetPid
863 echo "Press enter when logcat shows \"hprof: heap dump completed\""
867 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
868 local devFile=/data/misc/${availFiles[0]}
869 local localFile=/tmp/$$-hprof
871 echo "Retrieving file $devFile..."
872 adb ${adbOptions} pull $devFile $localFile
874 adb ${adbOptions} shell rm $devFile
876 echo "Running hat on $localFile"
877 echo "View the output by pointing your browser at http://localhost:7000/"
882 function getbugreports()
884 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
886 if [ ! "$reports" ]; then
887 echo "Could not locate any bugreports."
892 for report in ${reports[@]}
894 echo "/sdcard/bugreports/${report}"
895 adb pull /sdcard/bugreports/${report} ${report}
900 function startviewserver()
903 if [ $# -gt 0 ]; then
906 adb shell service call window 1 i32 $port
909 function stopviewserver()
911 adb shell service call window 2
914 function isviewserverstarted()
916 adb shell service call window 3
921 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
922 echo "Couldn't locate output files. Try running 'lunch' first." >&2
927 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
931 (cd "$T" && mmm tests/SmokeTest) &&
932 adb uninstall com.android.smoketest > /dev/null &&
933 adb uninstall com.android.smoketest.tests > /dev/null &&
934 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
935 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
936 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
939 # simple shortcut to the runtest command
944 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
947 (cd "$T" && development/testrunner/runtest.py $@)
950 # TODO: Remove this some time after 1 June 2009
951 function runtest_py()
953 echo "runtest_py is obsolete; use runtest instead" >&2
958 if [[ -z "$1" ]]; then
959 echo "Usage: godir <regex>"
962 if [[ ! -f $T/filelist ]]; then
963 echo -n "Creating index..."
964 (cd $T; find . -wholename ./out -prune -o -type f > filelist)
969 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
970 if [[ ${#lines[@]} = 0 ]]; then
976 if [[ ${#lines[@]} > 1 ]]; then
977 while [[ -z "$pathname" ]]; do
980 for line in ${lines[@]}; do
981 printf "%6s %s\n" "[$index]" $line
982 index=$(($index + 1))
985 echo -n "Select one: "
988 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
989 echo "Invalid choice"
992 pathname=${lines[$(($choice-$_arrayoffset))]}
995 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1001 # determine whether arrays are zero-based (bash) or one-based (zsh)
1003 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1011 # Execute the contents of any vendorsetup.sh files we can find.
1012 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh 2> /dev/null`