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.
12 Look at the source to view more functions. The complete list is:
17 for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
23 # Get the value of a build variable as an absolute path.
24 function get_abs_build_var()
28 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
31 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
32 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1
35 # Get the exact value of a build variable.
36 function get_build_var()
40 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
43 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
44 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
47 # check to see if the supplied product is one we can build
48 function check_product()
52 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
55 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
56 TARGET_PRODUCT=$1 TARGET_BUILD_VARIANT= \
57 TARGET_SIMULATOR= TARGET_BUILD_TYPE= \
58 get_build_var TARGET_DEVICE > /dev/null
59 # hide successful answers, but allow the errors to show
62 VARIANT_CHOICES=(user userdebug eng)
64 # check to see if the supplied variant is valid
65 function check_variant()
67 for v in ${VARIANT_CHOICES[@]}
81 echo "Couldn't locate the top of the tree. Try setting TOP."
85 ##################################################################
87 # Read me before you modify this code #
89 # This function sets ANDROID_BUILD_PATHS to what it is adding #
90 # to PATH, and the next time it is run, it removes that from #
91 # PATH. This is required so lunch can be run more than once #
92 # and still have working paths. #
94 ##################################################################
97 if [ -n $ANDROID_BUILD_PATHS ] ; then
98 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
101 # and in with the new
103 prebuiltdir=$(getprebuilt)
104 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.2.1/bin
105 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
106 export ANDROID_QTOOLS=$T/development/emulator/qtools
107 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
108 export PATH=$PATH$ANDROID_BUILD_PATHS
110 unset ANDROID_PRODUCT_OUT
111 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
112 export OUT=$ANDROID_PRODUCT_OUT
114 # needed for building linux on MacOS
116 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
118 # needed for OProfile to post-process collected samples
119 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
122 function printconfig()
126 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
129 get_build_var report_config
132 function set_stuff_for_environment()
138 # Don't try to do preoptimization until it works better on OSX.
139 export DISABLE_DEXPREOPT=true
141 export ANDROID_BUILD_TOP=$(gettop)
144 function set_sequence_number()
146 export BUILD_ENV_SEQUENCE_NUMBER=9
151 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
152 local product=$(get_build_var TARGET_PRODUCT)
153 local variant=$(get_build_var TARGET_BUILD_VARIANT)
154 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
162 echo "Build for the simulator or the device?"
167 export TARGET_SIMULATOR=
169 while [ -z $TARGET_SIMULATOR ]
171 echo -n "Which would you like? [1] "
172 if [ -z "$1" ] ; then
180 export TARGET_SIMULATOR=false
183 export TARGET_SIMULATOR=false
186 export TARGET_SIMULATOR=false
189 export TARGET_SIMULATOR=true
192 export TARGET_SIMULATOR=true
196 echo "I didn't understand your response. Please try again."
200 if [ -n "$1" ] ; then
205 set_stuff_for_environment
211 echo "Only device builds are supported for" `uname -s`
212 echo " Forcing TARGET_SIMULATOR=false"
216 echo -n "Press enter: "
220 export TARGET_SIMULATOR=false
221 set_stuff_for_environment
226 function choosetype()
228 echo "Build type choices are:"
233 local DEFAULT_NUM DEFAULT_VALUE
234 if [ $TARGET_SIMULATOR = "false" ] ; then
236 DEFAULT_VALUE=release
242 export TARGET_BUILD_TYPE=
244 while [ -z $TARGET_BUILD_TYPE ]
246 echo -n "Which would you like? ["$DEFAULT_NUM"] "
247 if [ -z "$1" ] ; then
255 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
258 export TARGET_BUILD_TYPE=release
261 export TARGET_BUILD_TYPE=release
264 export TARGET_BUILD_TYPE=debug
267 export TARGET_BUILD_TYPE=debug
271 echo "I didn't understand your response. Please try again."
275 if [ -n "$1" ] ; then
280 set_stuff_for_environment
284 # This function isn't really right: It chooses a TARGET_PRODUCT
285 # based on the list of boards. Usually, that gets you something
286 # that kinda works with a generic product, but really, you should
287 # pick a product by name.
289 function chooseproduct()
291 # Find the makefiles that must exist for a product.
292 # Send stderr to /dev/null in case partner isn't present.
294 choices=(`/bin/ls build/target/board/*/BoardConfig.mk vendor/*/*/BoardConfig.mk 2> /dev/null`)
298 for choice in ${choices[@]}
300 # The product name is the name of the directory containing
301 # the makefile we found, above.
302 prodlist=(${prodlist[@]} `dirname ${choice} | xargs basename`)
307 echo "Product choices are:"
308 for p in ${prodlist[@]}
311 let "index = $index + 1"
315 if [ "x$TARGET_PRODUCT" != x ] ; then
316 default_value=$TARGET_PRODUCT
318 if [ "$TARGET_SIMULATOR" = true ] ; then
321 default_value=generic
325 export TARGET_PRODUCT=
327 while [ -z "$TARGET_PRODUCT" ]
329 echo "You can also type the name of a product if you know it."
330 echo -n "Which would you like? [$default_value] "
331 if [ -z "$1" ] ; then
338 if [ -z "$ANSWER" ] ; then
339 export TARGET_PRODUCT=$default_value
340 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
341 local poo=`echo -n $ANSWER`
342 if [ $poo -le ${#prodlist[@]} ] ; then
343 export TARGET_PRODUCT=${prodlist[$(($ANSWER-$_arrayoffset))]}
345 echo "** Bad product selection: $ANSWER"
348 if check_product $ANSWER
350 export TARGET_PRODUCT=$ANSWER
352 echo "** Not a valid product: $ANSWER"
355 if [ -n "$1" ] ; then
360 set_stuff_for_environment
363 function choosevariant()
365 echo "Variant choices are:"
368 for v in ${VARIANT_CHOICES[@]}
370 # The product name is the name of the directory containing
371 # the makefile we found, above.
376 local default_value=eng
379 export TARGET_BUILD_VARIANT=
380 while [ -z "$TARGET_BUILD_VARIANT" ]
382 echo -n "Which would you like? [$default_value] "
383 if [ -z "$1" ] ; then
390 if [ -z "$ANSWER" ] ; then
391 export TARGET_BUILD_VARIANT=$default_value
392 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
393 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
394 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
397 if check_variant $ANSWER
399 export TARGET_BUILD_VARIANT=$ANSWER
401 echo "** Not a valid variant: $ANSWER"
404 if [ -n "$1" ] ; then
415 function choosecombo()
432 set_stuff_for_environment
436 # Clear this variable. It will be built up again when the vendorsetup.sh
437 # files are included at the end of this file.
438 unset LUNCH_MENU_CHOICES
439 function add_lunch_combo()
443 for c in ${LUNCH_MENU_CHOICES[@]} ; do
444 if [ "$new_combo" = "$c" ] ; then
448 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
451 # add the default one here
452 add_lunch_combo generic-eng
454 # if we're on linux, add the simulator. There is a special case
455 # in lunch to deal with the simulator
456 if [ "$(uname)" = "Linux" ] ; then
457 add_lunch_combo simulator
460 function print_lunch_menu()
464 echo "You're building on" $uname
466 echo ${LUNCH_MENU_CHOICES[@]}
467 echo "Lunch menu... pick a combo:"
471 for choice in ${LUNCH_MENU_CHOICES[@]}
488 echo -n "Which would you like? [generic-eng] "
496 selection=generic-eng
497 elif [ "$answer" = "simulator" ]
500 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
502 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
504 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
506 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
511 if [ -z "$selection" ]
514 echo "Invalid lunch combo: $answer"
518 # special case the simulator
519 if [ "$selection" = "simulator" ]
521 export TARGET_PRODUCT=sim
522 export TARGET_BUILD_VARIANT=eng
523 export TARGET_SIMULATOR=true
524 export TARGET_BUILD_TYPE=debug
526 local product=$(echo -n $selection | sed -e "s/-.*$//")
527 check_product $product
531 echo "** Don't have a product spec for: '$product'"
532 echo "** Do you have the right repo manifest?"
536 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
537 check_variant $variant
541 echo "** Invalid variant: '$variant'"
542 echo "** Must be one of ${VARIANT_CHOICES[@]}"
546 if [ -z "$product" -o -z "$variant" ]
552 export TARGET_PRODUCT=$product
553 export TARGET_BUILD_VARIANT=$variant
554 export TARGET_SIMULATOR=false
555 export TARGET_BUILD_TYPE=release
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
573 # We redirect cd to /dev/null in case it's aliased to
574 # a command that prints something as a side-effect
578 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
583 if [ -f "$T/$TOPFILE" ]; then
596 echo "Couldn't locate the top of the tree. Try setting TOP."
600 function findmakefile()
602 TOPFILE=build/core/envsetup.mk
603 # We redirect cd to /dev/null in case it's aliased to
604 # a command that prints something as a side-effect
608 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
610 if [ -f "$T/Android.mk" ]; then
622 # If we're sitting in the root of the build tree, just do a
624 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
627 # Find the closest Android.mk file.
629 local M=$(findmakefile)
631 echo "Couldn't locate the top of the tree. Try setting TOP."
632 elif [ ! "$M" ]; then
633 echo "Couldn't locate a makefile from the current directory."
635 ONE_SHOT_MAKEFILE=$M make -C $T files $@
648 DIR=`echo $DIR | sed -e 's:/$::'`
649 if [ -f $DIR/Android.mk ]; then
650 TO_CHOP=`echo $T | wc -c | tr -d ' '`
651 TO_CHOP=`expr $TO_CHOP + 1`
652 MFILE=`echo $PWD | cut -c${TO_CHOP}-`
653 if [ "$MFILE" = "" ] ; then
654 MFILE=$DIR/Android.mk
656 MFILE=$MFILE/$DIR/Android.mk
658 MAKEFILE="$MAKEFILE $MFILE"
660 if [ "$DIR" = snod ]; then
662 elif [ "$DIR" = showcommands ]; then
663 ARGS="$ARGS showcommands"
665 echo "No Android.mk in $DIR."
669 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T files $ARGS
671 echo "Couldn't locate the top of the tree. Try setting TOP."
681 echo "Couldn't locate the top of the tree. Try setting TOP."
689 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
692 echo "usage: pid name"
698 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
699 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
700 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
701 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
702 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
703 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
712 if [ "$PORT" ] ; then
720 if [ "$PROG" ] ; then
722 adb forward "tcp$PORT" "tcp$PORT"
723 adb shell gdbserver $PORT --attach $PID &
727 echo "If you haven't done so already, do this first on the device:"
728 echo " gdbserver $PORT /system/bin/$EXE"
730 echo " gdbserver $PORT --attach $PID"
734 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
735 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
736 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
737 echo >>"$OUT_ROOT/gdbclient.cmds" ""
739 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
741 echo "Unable to determine build system output dir."
750 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n "$@"
757 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\)' -print0 | xargs -0 grep --color -n "$@"
764 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
769 find . -type f -name "*\.c*" -print0 | xargs -0 grep --color -n "$@"
774 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
781 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
786 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
793 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
798 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
806 get_abs_build_var ANDROID_PREBUILTS
809 function tracedmdump()
813 echo "Couldn't locate the top of the tree. Try setting TOP."
816 local prebuiltdir=$(getprebuilt)
817 local KERNEL=$T/prebuilt/android-arm/vmlinux-qemu
820 if [ ! "$TRACE" ] ; then
821 echo "usage: tracedmdump tracename"
825 local BASETRACE=$(basename $TRACE)
826 if [ "$BASETRACE" = "$TRACE" ] ; then
827 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
830 echo "post-processing traces..."
831 rm -f $TRACE/qtrace.dexlist
833 if [ $? -ne 0 ]; then
835 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
839 echo "generating dexlist output..."
840 /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
841 echo "generating dmtrace data..."
842 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
843 echo "generating html file..."
844 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
845 echo "done, see $TRACE/dmtrace.html for details"
847 echo " traceview $TRACE/dmtrace"
850 # communicate with a running device or emulator, set up necessary state,
851 # and run the hat command.
854 # process standard adb options
856 if [ $1 = "-d" -o $1 = "-e" ]; then
859 elif [ $1 = "-s" ]; then
863 local adbOptions=${adbTarget}
864 echo adbOptions = ${adbOptions}
870 if [ "$targetPid" = "" ]; then
871 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
875 # confirm hat is available
876 if [ -z $(which hat) ]; then
877 echo "hat is not available in this configuration."
881 adb ${adbOptions} shell >/dev/null mkdir /data/misc
882 adb ${adbOptions} shell chmod 777 /data/misc
884 echo "Poking $targetPid and waiting for data..."
885 adb ${adbOptions} shell kill -10 $targetPid
886 echo "Press enter when logcat shows \"GC freed ## objects / ## bytes\""
890 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
891 local devHeadFile=/data/misc/${availFiles[0]}
892 local devTailFile=/data/misc/${availFiles[1]}
894 local localHeadFile=/tmp/$$-hprof-head
895 local localTailFile=/tmp/$$-hprof-tail
897 echo "Retrieving file $devHeadFile..."
898 adb ${adbOptions} pull $devHeadFile $localHeadFile
899 echo "Retrieving file $devTailFile..."
900 adb ${adbOptions} pull $devTailFile $localTailFile
902 local combinedFile=$outputFile
903 if [ "$combinedFile" = "" ]; then
904 combinedFile=/tmp/$$.hprof
907 cat $localHeadFile $localTailFile >$combinedFile
908 adb ${adbOptions} shell rm $devHeadFile
909 adb ${adbOptions} shell rm $devTailFile
913 echo "Running hat on $combinedFile"
914 echo "View the output by pointing your browser at http://localhost:7000/"
919 function getbugreports()
921 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
923 if [ ! "$reports" ]; then
924 echo "Could not locate any bugreports."
929 for report in ${reports[@]}
931 echo "/sdcard/bugreports/${report}"
932 adb pull /sdcard/bugreports/${report} ${report}
937 function startviewserver()
940 if [ $# -gt 0 ]; then
943 adb shell service call window 1 i32 $port
946 function stopviewserver()
948 adb shell service call window 2
951 function isviewserverstarted()
953 adb shell service call window 3
958 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
959 echo "Couldn't locate output files. Try running 'lunch' first." >&2
964 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
968 (cd "$T" && mmm tests/SmokeTest) &&
969 adb uninstall com.android.smoketest > /dev/null &&
970 adb uninstall com.android.smoketest.tests > /dev/null &&
971 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
972 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
973 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
976 # simple shortcut to the runtest command
981 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
984 (cd "$T" && development/tools/runtest $@)
987 # determine whether arrays are zero-based (bash) or one-based (zsh)
989 if [ -z "${_xarray[${#_xarray[@]}]}" ]
997 # Execute the contents of any vendorsetup.sh files we can find.
998 for f in `/bin/ls vendor/*/vendorsetup.sh 2> /dev/null`