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/}
101 if [ -n $ANDROID_PRE_BUILD_PATHS ] ; then
102 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
105 # and in with the new
107 prebuiltdir=$(getprebuilt)
108 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.4.0/bin
109 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
110 export ANDROID_QTOOLS=$T/development/emulator/qtools
111 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
112 export PATH=$PATH$ANDROID_BUILD_PATHS
114 unset ANDROID_JAVA_TOOLCHAIN
115 if [ -n "$JAVA_HOME" ]; then
116 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
118 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN
119 if [ -n "$ANDROID_PRE_BUILD_PATHS" ]; then
120 export PATH=$ANDROID_PRE_BUILD_PATHS:$PATH
123 unset ANDROID_PRODUCT_OUT
124 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
125 export OUT=$ANDROID_PRODUCT_OUT
127 # needed for building linux on MacOS
129 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
131 # needed for OProfile to post-process collected samples
132 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
135 function printconfig()
139 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
142 get_build_var report_config
145 function set_stuff_for_environment()
152 # Don't try to do preoptimization until it works better on OSX.
153 export DISABLE_DEXPREOPT=true
155 export ANDROID_BUILD_TOP=$(gettop)
158 function set_sequence_number()
160 export BUILD_ENV_SEQUENCE_NUMBER=9
165 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
166 local product=$(get_build_var TARGET_PRODUCT)
167 local variant=$(get_build_var TARGET_BUILD_VARIANT)
168 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
176 echo "Build for the simulator or the device?"
181 export TARGET_SIMULATOR=
183 while [ -z $TARGET_SIMULATOR ]
185 echo -n "Which would you like? [1] "
186 if [ -z "$1" ] ; then
194 export TARGET_SIMULATOR=false
197 export TARGET_SIMULATOR=false
200 export TARGET_SIMULATOR=false
203 export TARGET_SIMULATOR=true
206 export TARGET_SIMULATOR=true
210 echo "I didn't understand your response. Please try again."
214 if [ -n "$1" ] ; then
219 set_stuff_for_environment
225 echo "Only device builds are supported for" `uname -s`
226 echo " Forcing TARGET_SIMULATOR=false"
230 echo -n "Press enter: "
234 export TARGET_SIMULATOR=false
235 set_stuff_for_environment
240 function choosetype()
242 echo "Build type choices are:"
247 local DEFAULT_NUM DEFAULT_VALUE
248 if [ $TARGET_SIMULATOR = "false" ] ; then
250 DEFAULT_VALUE=release
256 export TARGET_BUILD_TYPE=
258 while [ -z $TARGET_BUILD_TYPE ]
260 echo -n "Which would you like? ["$DEFAULT_NUM"] "
261 if [ -z "$1" ] ; then
269 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
272 export TARGET_BUILD_TYPE=release
275 export TARGET_BUILD_TYPE=release
278 export TARGET_BUILD_TYPE=debug
281 export TARGET_BUILD_TYPE=debug
285 echo "I didn't understand your response. Please try again."
289 if [ -n "$1" ] ; then
294 set_stuff_for_environment
298 # This function isn't really right: It chooses a TARGET_PRODUCT
299 # based on the list of boards. Usually, that gets you something
300 # that kinda works with a generic product, but really, you should
301 # pick a product by name.
303 function chooseproduct()
305 if [ "x$TARGET_PRODUCT" != x ] ; then
306 default_value=$TARGET_PRODUCT
308 if [ "$TARGET_SIMULATOR" = true ] ; then
311 default_value=generic
315 export TARGET_PRODUCT=
317 while [ -z "$TARGET_PRODUCT" ]
319 echo -n "Which product would you like? [$default_value] "
320 if [ -z "$1" ] ; then
327 if [ -z "$ANSWER" ] ; then
328 export TARGET_PRODUCT=$default_value
330 if check_product $ANSWER
332 export TARGET_PRODUCT=$ANSWER
334 echo "** Not a valid product: $ANSWER"
337 if [ -n "$1" ] ; then
342 set_stuff_for_environment
345 function choosevariant()
347 echo "Variant choices are:"
350 for v in ${VARIANT_CHOICES[@]}
352 # The product name is the name of the directory containing
353 # the makefile we found, above.
358 local default_value=eng
361 export TARGET_BUILD_VARIANT=
362 while [ -z "$TARGET_BUILD_VARIANT" ]
364 echo -n "Which would you like? [$default_value] "
365 if [ -z "$1" ] ; then
372 if [ -z "$ANSWER" ] ; then
373 export TARGET_BUILD_VARIANT=$default_value
374 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
375 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
376 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
379 if check_variant $ANSWER
381 export TARGET_BUILD_VARIANT=$ANSWER
383 echo "** Not a valid variant: $ANSWER"
386 if [ -n "$1" ] ; then
397 function choosecombo()
414 set_stuff_for_environment
418 # Clear this variable. It will be built up again when the vendorsetup.sh
419 # files are included at the end of this file.
420 unset LUNCH_MENU_CHOICES
421 function add_lunch_combo()
425 for c in ${LUNCH_MENU_CHOICES[@]} ; do
426 if [ "$new_combo" = "$c" ] ; then
430 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
433 # add the default one here
434 add_lunch_combo generic-eng
436 # if we're on linux, add the simulator. There is a special case
437 # in lunch to deal with the simulator
438 if [ "$(uname)" = "Linux" ] ; then
439 add_lunch_combo simulator
442 function print_lunch_menu()
446 echo "You're building on" $uname
448 echo ${LUNCH_MENU_CHOICES[@]}
449 echo "Lunch menu... pick a combo:"
453 for choice in ${LUNCH_MENU_CHOICES[@]}
470 echo -n "Which would you like? [generic-eng] "
478 selection=generic-eng
479 elif [ "$answer" = "simulator" ]
482 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
484 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
486 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
488 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
493 if [ -z "$selection" ]
496 echo "Invalid lunch combo: $answer"
500 # special case the simulator
501 if [ "$selection" = "simulator" ]
503 export TARGET_PRODUCT=sim
504 export TARGET_BUILD_VARIANT=eng
505 export TARGET_SIMULATOR=true
506 export TARGET_BUILD_TYPE=debug
508 local product=$(echo -n $selection | sed -e "s/-.*$//")
509 check_product $product
513 echo "** Don't have a product spec for: '$product'"
514 echo "** Do you have the right repo manifest?"
518 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
519 check_variant $variant
523 echo "** Invalid variant: '$variant'"
524 echo "** Must be one of ${VARIANT_CHOICES[@]}"
528 if [ -z "$product" -o -z "$variant" ]
534 export TARGET_PRODUCT=$product
535 export TARGET_BUILD_VARIANT=$variant
536 export TARGET_SIMULATOR=false
537 export TARGET_BUILD_TYPE=release
542 set_stuff_for_environment
548 local TOPFILE=build/core/envsetup.mk
549 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
552 if [ -f $TOPFILE ] ; then
553 # The following circumlocution (repeated below as well) ensures
554 # that we record the true directory name and not one that is
555 # faked up with symlink names.
558 # We redirect cd to /dev/null in case it's aliased to
559 # a command that prints something as a side-effect
563 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
568 if [ -f "$T/$TOPFILE" ]; then
581 echo "Couldn't locate the top of the tree. Try setting TOP."
585 function findmakefile()
587 TOPFILE=build/core/envsetup.mk
588 # We redirect cd to /dev/null in case it's aliased to
589 # a command that prints something as a side-effect
593 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
595 if [ -f "$T/Android.mk" ]; then
607 # If we're sitting in the root of the build tree, just do a
609 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
612 # Find the closest Android.mk file.
614 local M=$(findmakefile)
615 # Remove the path to top as the makefilepath needs to be relative
616 local M=`echo $M|sed 's:'$T'/::'`
618 echo "Couldn't locate the top of the tree. Try setting TOP."
619 elif [ ! "$M" ]; then
620 echo "Couldn't locate a makefile from the current directory."
622 ONE_SHOT_MAKEFILE=$M make -C $T files $@
634 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
635 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
636 for DIR in $DIRS ; do
637 DIR=`echo $DIR | sed -e 's:/$::'`
638 if [ -f $DIR/Android.mk ]; then
639 TO_CHOP=`echo $T | wc -c | tr -d ' '`
640 TO_CHOP=`expr $TO_CHOP + 1`
641 START=`PWD= /bin/pwd`
642 MFILE=`echo $START | cut -c${TO_CHOP}-`
643 if [ "$MFILE" = "" ] ; then
644 MFILE=$DIR/Android.mk
646 MFILE=$MFILE/$DIR/Android.mk
648 MAKEFILE="$MAKEFILE $MFILE"
650 if [ "$DIR" = snod ]; then
652 elif [ "$DIR" = showcommands ]; then
653 ARGS="$ARGS showcommands"
655 echo "No Android.mk in $DIR."
660 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
662 echo "Couldn't locate the top of the tree. Try setting TOP."
672 echo "Couldn't locate the top of the tree. Try setting TOP."
678 TOPFILE=build/core/envsetup.mk
679 # We redirect cd to /dev/null in case it's aliased to
680 # a command that prints something as a side-effect
684 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
686 if [ -f "$T/Android.mk" ]; then
693 echo "can't find Android.mk"
700 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
703 echo "usage: pid name"
707 # systemstack - dump the current stack trace of all threads in the system process
708 # to the usual ANR traces file
709 function systemstack()
711 adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
716 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
717 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
718 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
719 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
720 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
721 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
730 if [ "$PORT" ] ; then
738 if [ "$PROG" ] ; then
740 adb forward "tcp$PORT" "tcp$PORT"
741 adb shell gdbserver $PORT --attach $PID &
745 echo "If you haven't done so already, do this first on the device:"
746 echo " gdbserver $PORT /system/bin/$EXE"
748 echo " gdbserver $PORT --attach $PID"
752 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
753 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
754 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
755 echo >>"$OUT_ROOT/gdbclient.cmds" ""
757 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
759 echo "Unable to determine build system output dir."
768 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
775 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
782 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
787 find . -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
792 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
799 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
804 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
811 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
816 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
824 get_abs_build_var ANDROID_PREBUILTS
827 function tracedmdump()
831 echo "Couldn't locate the top of the tree. Try setting TOP."
834 local prebuiltdir=$(getprebuilt)
835 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
838 if [ ! "$TRACE" ] ; then
839 echo "usage: tracedmdump tracename"
843 if [ ! -r "$KERNEL" ] ; then
844 echo "Error: cannot find kernel: '$KERNEL'"
848 local BASETRACE=$(basename $TRACE)
849 if [ "$BASETRACE" = "$TRACE" ] ; then
850 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
853 echo "post-processing traces..."
854 rm -f $TRACE/qtrace.dexlist
856 if [ $? -ne 0 ]; then
858 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
862 echo "generating dexlist output..."
863 /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
864 echo "generating dmtrace data..."
865 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
866 echo "generating html file..."
867 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
868 echo "done, see $TRACE/dmtrace.html for details"
870 echo " traceview $TRACE/dmtrace"
873 # communicate with a running device or emulator, set up necessary state,
874 # and run the hat command.
877 # process standard adb options
879 if [ $1 = "-d" -o $1 = "-e" ]; then
882 elif [ $1 = "-s" ]; then
886 local adbOptions=${adbTarget}
887 echo adbOptions = ${adbOptions}
893 if [ "$targetPid" = "" ]; then
894 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
898 # confirm hat is available
899 if [ -z $(which hat) ]; then
900 echo "hat is not available in this configuration."
904 adb ${adbOptions} shell >/dev/null mkdir /data/misc
905 adb ${adbOptions} shell chmod 777 /data/misc
907 # send a SIGUSR1 to cause the hprof dump
908 echo "Poking $targetPid and waiting for data..."
909 adb ${adbOptions} shell kill -10 $targetPid
910 echo "Press enter when logcat shows \"hprof: heap dump completed\""
914 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
915 local devFile=/data/misc/${availFiles[0]}
916 local localFile=/tmp/$$-hprof
918 echo "Retrieving file $devFile..."
919 adb ${adbOptions} pull $devFile $localFile
921 adb ${adbOptions} shell rm $devFile
923 echo "Running hat on $localFile"
924 echo "View the output by pointing your browser at http://localhost:7000/"
929 function getbugreports()
931 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
933 if [ ! "$reports" ]; then
934 echo "Could not locate any bugreports."
939 for report in ${reports[@]}
941 echo "/sdcard/bugreports/${report}"
942 adb pull /sdcard/bugreports/${report} ${report}
947 function startviewserver()
950 if [ $# -gt 0 ]; then
953 adb shell service call window 1 i32 $port
956 function stopviewserver()
958 adb shell service call window 2
961 function isviewserverstarted()
963 adb shell service call window 3
968 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
969 echo "Couldn't locate output files. Try running 'lunch' first." >&2
974 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
978 (cd "$T" && mmm tests/SmokeTest) &&
979 adb uninstall com.android.smoketest > /dev/null &&
980 adb uninstall com.android.smoketest.tests > /dev/null &&
981 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
982 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
983 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
986 # simple shortcut to the runtest command
991 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
994 ("$T"/development/testrunner/runtest.py $@)
998 if [[ -z "$1" ]]; then
999 echo "Usage: godir <regex>"
1003 if [[ ! -f $T/filelist ]]; then
1004 echo -n "Creating index..."
1005 (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
1010 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1011 if [[ ${#lines[@]} = 0 ]]; then
1017 if [[ ${#lines[@]} > 1 ]]; then
1018 while [[ -z "$pathname" ]]; do
1021 for line in ${lines[@]}; do
1022 printf "%6s %s\n" "[$index]" $line
1023 index=$(($index + 1))
1026 echo -n "Select one: "
1029 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1030 echo "Invalid choice"
1033 pathname=${lines[$(($choice-$_arrayoffset))]}
1036 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1037 pathname=${lines[0]}
1042 # Force JAVA_HOME to point to java 1.6 if it isn't already set
1043 function set_java_home() {
1044 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
1045 if [ ! "$JAVA_HOME" ]; then
1048 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
1051 export JAVA_HOME=/usr/lib/jvm/java-6-sun
1058 # determine whether arrays are zero-based (bash) or one-based (zsh)
1060 if [ -z "${_xarray[${#_xarray[@]}]}" ]
1068 # Execute the contents of any vendorsetup.sh files we can find.
1069 for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`