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 # Find the makefiles that must exist for a product.
293 # Send stderr to /dev/null in case partner isn't present.
295 choices=(`/bin/ls build/target/board/*/BoardConfig.mk vendor/*/*/BoardConfig.mk 2> /dev/null`)
299 for choice in ${choices[@]}
301 # The product name is the name of the directory containing
302 # the makefile we found, above.
303 prodlist=(${prodlist[@]} `dirname ${choice} | xargs basename`)
308 echo "Product choices are:"
309 for p in ${prodlist[@]}
312 let "index = $index + 1"
316 if [ "x$TARGET_PRODUCT" != x ] ; then
317 default_value=$TARGET_PRODUCT
319 if [ "$TARGET_SIMULATOR" = true ] ; then
322 default_value=generic
326 export TARGET_PRODUCT=
328 while [ -z "$TARGET_PRODUCT" ]
330 echo "You can also type the name of a product if you know it."
331 echo -n "Which would you like? [$default_value] "
332 if [ -z "$1" ] ; then
339 if [ -z "$ANSWER" ] ; then
340 export TARGET_PRODUCT=$default_value
341 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
342 local poo=`echo -n $ANSWER`
343 if [ $poo -le ${#prodlist[@]} ] ; then
344 export TARGET_PRODUCT=${prodlist[$(($ANSWER-$_arrayoffset))]}
346 echo "** Bad product selection: $ANSWER"
349 if check_product $ANSWER
351 export TARGET_PRODUCT=$ANSWER
353 echo "** Not a valid product: $ANSWER"
356 if [ -n "$1" ] ; then
361 set_stuff_for_environment
364 function choosevariant()
366 echo "Variant choices are:"
369 for v in ${VARIANT_CHOICES[@]}
371 # The product name is the name of the directory containing
372 # the makefile we found, above.
377 local default_value=eng
380 export TARGET_BUILD_VARIANT=
381 while [ -z "$TARGET_BUILD_VARIANT" ]
383 echo -n "Which would you like? [$default_value] "
384 if [ -z "$1" ] ; then
391 if [ -z "$ANSWER" ] ; then
392 export TARGET_BUILD_VARIANT=$default_value
393 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
394 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
395 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
398 if check_variant $ANSWER
400 export TARGET_BUILD_VARIANT=$ANSWER
402 echo "** Not a valid variant: $ANSWER"
405 if [ -n "$1" ] ; then
416 function choosecombo()
433 set_stuff_for_environment
437 # Clear this variable. It will be built up again when the vendorsetup.sh
438 # files are included at the end of this file.
439 unset LUNCH_MENU_CHOICES
440 function add_lunch_combo()
444 for c in ${LUNCH_MENU_CHOICES[@]} ; do
445 if [ "$new_combo" = "$c" ] ; then
449 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
452 # add the default one here
453 add_lunch_combo generic-eng
455 # if we're on linux, add the simulator. There is a special case
456 # in lunch to deal with the simulator
457 if [ "$(uname)" = "Linux" ] ; then
458 add_lunch_combo simulator
461 function print_lunch_menu()
465 echo "You're building on" $uname
467 echo ${LUNCH_MENU_CHOICES[@]}
468 echo "Lunch menu... pick a combo:"
472 for choice in ${LUNCH_MENU_CHOICES[@]}
489 echo -n "Which would you like? [generic-eng] "
497 selection=generic-eng
498 elif [ "$answer" = "simulator" ]
501 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
503 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
505 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
507 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
512 if [ -z "$selection" ]
515 echo "Invalid lunch combo: $answer"
519 # special case the simulator
520 if [ "$selection" = "simulator" ]
522 export TARGET_PRODUCT=sim
523 export TARGET_BUILD_VARIANT=eng
524 export TARGET_SIMULATOR=true
525 export TARGET_BUILD_TYPE=debug
527 local product=$(echo -n $selection | sed -e "s/-.*$//")
528 check_product $product
532 echo "** Don't have a product spec for: '$product'"
533 echo "** Do you have the right repo manifest?"
537 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
538 check_variant $variant
542 echo "** Invalid variant: '$variant'"
543 echo "** Must be one of ${VARIANT_CHOICES[@]}"
547 if [ -z "$product" -o -z "$variant" ]
553 export TARGET_PRODUCT=$product
554 export TARGET_BUILD_VARIANT=$variant
555 export TARGET_SIMULATOR=false
556 export TARGET_BUILD_TYPE=release
561 set_stuff_for_environment
567 local TOPFILE=build/core/envsetup.mk
568 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
571 if [ -f $TOPFILE ] ; then
574 # We redirect cd to /dev/null in case it's aliased to
575 # a command that prints something as a side-effect
579 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
584 if [ -f "$T/$TOPFILE" ]; then
597 echo "Couldn't locate the top of the tree. Try setting TOP."
601 function findmakefile()
603 TOPFILE=build/core/envsetup.mk
604 # We redirect cd to /dev/null in case it's aliased to
605 # a command that prints something as a side-effect
609 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
611 if [ -f "$T/Android.mk" ]; then
623 # If we're sitting in the root of the build tree, just do a
625 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
628 # Find the closest Android.mk file.
630 local M=$(findmakefile)
632 echo "Couldn't locate the top of the tree. Try setting TOP."
633 elif [ ! "$M" ]; then
634 echo "Couldn't locate a makefile from the current directory."
636 ONE_SHOT_MAKEFILE=$M make -C $T files $@
648 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
649 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
650 for DIR in $DIRS ; do
651 DIR=`echo $DIR | sed -e 's:/$::'`
652 if [ -f $DIR/Android.mk ]; then
653 TO_CHOP=`echo $T | wc -c | tr -d ' '`
654 TO_CHOP=`expr $TO_CHOP + 1`
655 MFILE=`echo $PWD | cut -c${TO_CHOP}-`
656 if [ "$MFILE" = "" ] ; then
657 MFILE=$DIR/Android.mk
659 MFILE=$MFILE/$DIR/Android.mk
661 MAKEFILE="$MAKEFILE $MFILE"
663 if [ "$DIR" = snod ]; then
665 elif [ "$DIR" = showcommands ]; then
666 ARGS="$ARGS showcommands"
668 echo "No Android.mk in $DIR."
672 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
674 echo "Couldn't locate the top of the tree. Try setting TOP."
684 echo "Couldn't locate the top of the tree. Try setting TOP."
692 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
695 echo "usage: pid name"
701 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
702 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
703 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
704 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
705 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
706 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
715 if [ "$PORT" ] ; then
723 if [ "$PROG" ] ; then
725 adb forward "tcp$PORT" "tcp$PORT"
726 adb shell gdbserver $PORT --attach $PID &
730 echo "If you haven't done so already, do this first on the device:"
731 echo " gdbserver $PORT /system/bin/$EXE"
733 echo " gdbserver $PORT --attach $PID"
737 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
738 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
739 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
740 echo >>"$OUT_ROOT/gdbclient.cmds" ""
742 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
744 echo "Unable to determine build system output dir."
753 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n "$@"
760 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\)' -print0 | xargs -0 grep --color -n "$@"
767 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
772 find . -type f -name "*\.c*" -print0 | xargs -0 grep --color -n "$@"
777 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
784 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
789 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
796 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
801 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
809 get_abs_build_var ANDROID_PREBUILTS
812 function tracedmdump()
816 echo "Couldn't locate the top of the tree. Try setting TOP."
819 local prebuiltdir=$(getprebuilt)
820 local KERNEL=$T/prebuilt/android-arm/vmlinux-qemu
823 if [ ! "$TRACE" ] ; then
824 echo "usage: tracedmdump tracename"
828 local BASETRACE=$(basename $TRACE)
829 if [ "$BASETRACE" = "$TRACE" ] ; then
830 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
833 echo "post-processing traces..."
834 rm -f $TRACE/qtrace.dexlist
836 if [ $? -ne 0 ]; then
838 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
842 echo "generating dexlist output..."
843 /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
844 echo "generating dmtrace data..."
845 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
846 echo "generating html file..."
847 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
848 echo "done, see $TRACE/dmtrace.html for details"
850 echo " traceview $TRACE/dmtrace"
853 # communicate with a running device or emulator, set up necessary state,
854 # and run the hat command.
857 # process standard adb options
859 if [ $1 = "-d" -o $1 = "-e" ]; then
862 elif [ $1 = "-s" ]; then
866 local adbOptions=${adbTarget}
867 echo adbOptions = ${adbOptions}
873 if [ "$targetPid" = "" ]; then
874 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
878 # confirm hat is available
879 if [ -z $(which hat) ]; then
880 echo "hat is not available in this configuration."
884 adb ${adbOptions} shell >/dev/null mkdir /data/misc
885 adb ${adbOptions} shell chmod 777 /data/misc
887 # send a SIGUSR1 to cause the hprof dump
888 echo "Poking $targetPid and waiting for data..."
889 adb ${adbOptions} shell kill -10 $targetPid
890 echo "Press enter when logcat shows \"hprof: heap dump completed\""
894 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
895 local devFile=/data/misc/${availFiles[0]}
896 local localFile=/tmp/$$-hprof
898 echo "Retrieving file $devFile..."
899 adb ${adbOptions} pull $devFile $localFile
901 adb ${adbOptions} shell rm $devFile
903 echo "Running hat on $localFile"
904 echo "View the output by pointing your browser at http://localhost:7000/"
909 function getbugreports()
911 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
913 if [ ! "$reports" ]; then
914 echo "Could not locate any bugreports."
919 for report in ${reports[@]}
921 echo "/sdcard/bugreports/${report}"
922 adb pull /sdcard/bugreports/${report} ${report}
927 function startviewserver()
930 if [ $# -gt 0 ]; then
933 adb shell service call window 1 i32 $port
936 function stopviewserver()
938 adb shell service call window 2
941 function isviewserverstarted()
943 adb shell service call window 3
948 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
949 echo "Couldn't locate output files. Try running 'lunch' first." >&2
954 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
958 (cd "$T" && mmm tests/SmokeTest) &&
959 adb uninstall com.android.smoketest > /dev/null &&
960 adb uninstall com.android.smoketest.tests > /dev/null &&
961 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
962 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
963 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
966 # simple shortcut to the runtest command
971 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
974 (cd "$T" && development/tools/runtest $@)
978 if [[ -z "$1" ]]; then
979 echo "Usage: godir <regex>"
982 if [[ ! -f $T/filelist ]]; then
983 echo -n "Creating index..."
984 (cd $T; find . -wholename ./out -prune -o -type f > filelist)
989 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
990 if [[ ${#lines[@]} = 0 ]]; then
996 if [[ ${#lines[@]} > 1 ]]; then
997 while [[ -z "$pathname" ]]; do
1000 for line in ${lines[@]}; do
1001 printf "%6s %s\n" "[$index]" $line
1002 index=$(($index + 1))
1005 echo -n "Select one: "
1008 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1009 echo "Invalid choice"
1012 pathname=${lines[$(($choice-$_arrayoffset))]}
1015 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1016 pathname=${lines[0]}
1021 # determine whether arrays are zero-based (bash) or one-based (zsh)
1023 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1031 # Execute the contents of any vendorsetup.sh files we can find.
1032 for f in `/bin/ls vendor/*/vendorsetup.sh 2> /dev/null`