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.4.0/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
540 # The following circumlocution (repeated below as well) ensures
541 # that we record the true directory name and not one that is
542 # faked up with symlink names.
545 # We redirect cd to /dev/null in case it's aliased to
546 # a command that prints something as a side-effect
550 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
555 if [ -f "$T/$TOPFILE" ]; then
568 echo "Couldn't locate the top of the tree. Try setting TOP."
572 function findmakefile()
574 TOPFILE=build/core/envsetup.mk
575 # We redirect cd to /dev/null in case it's aliased to
576 # a command that prints something as a side-effect
580 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
582 if [ -f "$T/Android.mk" ]; then
594 # If we're sitting in the root of the build tree, just do a
596 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
599 # Find the closest Android.mk file.
601 local M=$(findmakefile)
602 # Remove the path to top as the makefilepath needs to be relative
603 local M=`echo $M|sed 's:'$T'/::'`
605 echo "Couldn't locate the top of the tree. Try setting TOP."
606 elif [ ! "$M" ]; then
607 echo "Couldn't locate a makefile from the current directory."
609 ONE_SHOT_MAKEFILE=$M make -C $T files $@
621 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
622 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
623 for DIR in $DIRS ; do
624 DIR=`echo $DIR | sed -e 's:/$::'`
625 if [ -f $DIR/Android.mk ]; then
626 TO_CHOP=`echo $T | wc -c | tr -d ' '`
627 TO_CHOP=`expr $TO_CHOP + 1`
628 MFILE=`echo $PWD | cut -c${TO_CHOP}-`
629 if [ "$MFILE" = "" ] ; then
630 MFILE=$DIR/Android.mk
632 MFILE=$MFILE/$DIR/Android.mk
634 MAKEFILE="$MAKEFILE $MFILE"
636 if [ "$DIR" = snod ]; then
638 elif [ "$DIR" = showcommands ]; then
639 ARGS="$ARGS showcommands"
641 echo "No Android.mk in $DIR."
646 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
648 echo "Couldn't locate the top of the tree. Try setting TOP."
658 echo "Couldn't locate the top of the tree. Try setting TOP."
664 TOPFILE=build/core/envsetup.mk
665 # We redirect cd to /dev/null in case it's aliased to
666 # a command that prints something as a side-effect
670 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
672 if [ -f "$T/Android.mk" ]; then
679 echo "can't find Android.mk"
686 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
689 echo "usage: pid name"
693 # systemstack - dump the current stack trace of all threads in the system process
694 # to the usual ANR traces file
695 function systemstack()
697 adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
702 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
703 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
704 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
705 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
706 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
707 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
716 if [ "$PORT" ] ; then
724 if [ "$PROG" ] ; then
726 adb forward "tcp$PORT" "tcp$PORT"
727 adb shell gdbserver $PORT --attach $PID &
731 echo "If you haven't done so already, do this first on the device:"
732 echo " gdbserver $PORT /system/bin/$EXE"
734 echo " gdbserver $PORT --attach $PID"
738 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
739 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
740 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
741 echo >>"$OUT_ROOT/gdbclient.cmds" ""
743 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
745 echo "Unable to determine build system output dir."
754 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
761 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
768 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
773 find . -type f -name "*\.c*" -print0 | xargs -0 grep --color -n "$@"
778 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
785 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
790 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
797 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
802 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
810 get_abs_build_var ANDROID_PREBUILTS
813 function tracedmdump()
817 echo "Couldn't locate the top of the tree. Try setting TOP."
820 local prebuiltdir=$(getprebuilt)
821 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
824 if [ ! "$TRACE" ] ; then
825 echo "usage: tracedmdump tracename"
829 if [ ! -r "$KERNEL" ] ; then
830 echo "Error: cannot find kernel: '$KERNEL'"
834 local BASETRACE=$(basename $TRACE)
835 if [ "$BASETRACE" = "$TRACE" ] ; then
836 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
839 echo "post-processing traces..."
840 rm -f $TRACE/qtrace.dexlist
842 if [ $? -ne 0 ]; then
844 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
848 echo "generating dexlist output..."
849 /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
850 echo "generating dmtrace data..."
851 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
852 echo "generating html file..."
853 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
854 echo "done, see $TRACE/dmtrace.html for details"
856 echo " traceview $TRACE/dmtrace"
859 # communicate with a running device or emulator, set up necessary state,
860 # and run the hat command.
863 # process standard adb options
865 if [ $1 = "-d" -o $1 = "-e" ]; then
868 elif [ $1 = "-s" ]; then
872 local adbOptions=${adbTarget}
873 echo adbOptions = ${adbOptions}
879 if [ "$targetPid" = "" ]; then
880 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
884 # confirm hat is available
885 if [ -z $(which hat) ]; then
886 echo "hat is not available in this configuration."
890 adb ${adbOptions} shell >/dev/null mkdir /data/misc
891 adb ${adbOptions} shell chmod 777 /data/misc
893 # send a SIGUSR1 to cause the hprof dump
894 echo "Poking $targetPid and waiting for data..."
895 adb ${adbOptions} shell kill -10 $targetPid
896 echo "Press enter when logcat shows \"hprof: heap dump completed\""
900 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
901 local devFile=/data/misc/${availFiles[0]}
902 local localFile=/tmp/$$-hprof
904 echo "Retrieving file $devFile..."
905 adb ${adbOptions} pull $devFile $localFile
907 adb ${adbOptions} shell rm $devFile
909 echo "Running hat on $localFile"
910 echo "View the output by pointing your browser at http://localhost:7000/"
915 function getbugreports()
917 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
919 if [ ! "$reports" ]; then
920 echo "Could not locate any bugreports."
925 for report in ${reports[@]}
927 echo "/sdcard/bugreports/${report}"
928 adb pull /sdcard/bugreports/${report} ${report}
933 function startviewserver()
936 if [ $# -gt 0 ]; then
939 adb shell service call window 1 i32 $port
942 function stopviewserver()
944 adb shell service call window 2
947 function isviewserverstarted()
949 adb shell service call window 3
954 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
955 echo "Couldn't locate output files. Try running 'lunch' first." >&2
960 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
964 (cd "$T" && mmm tests/SmokeTest) &&
965 adb uninstall com.android.smoketest > /dev/null &&
966 adb uninstall com.android.smoketest.tests > /dev/null &&
967 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
968 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
969 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
972 # simple shortcut to the runtest command
977 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
980 ("$T"/development/testrunner/runtest.py $@)
984 if [[ -z "$1" ]]; then
985 echo "Usage: godir <regex>"
989 if [[ ! -f $T/filelist ]]; then
990 echo -n "Creating index..."
991 (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
996 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
997 if [[ ${#lines[@]} = 0 ]]; then
1003 if [[ ${#lines[@]} > 1 ]]; then
1004 while [[ -z "$pathname" ]]; do
1007 for line in ${lines[@]}; do
1008 printf "%6s %s\n" "[$index]" $line
1009 index=$(($index + 1))
1012 echo -n "Select one: "
1015 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1016 echo "Invalid choice"
1019 pathname=${lines[$(($choice-$_arrayoffset))]}
1022 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1023 pathname=${lines[0]}
1028 # determine whether arrays are zero-based (bash) or one-based (zsh)
1030 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1038 # Execute the contents of any vendorsetup.sh files we can find.
1039 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh 2> /dev/null`