blob: 9c6e695360cb1952f9f9adb1f19092cf07352c16 [file] [log] [blame]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001function help() {
2cat <<EOF
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08003Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07004- 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.
The Android Open Source Project88b60792009-03-03 19:28:42 -080011- godir: Go to the directory containing a file.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070012
13Look at the source to view more functions. The complete list is:
14EOF
15 T=$(gettop)
16 local A
17 A=""
18 for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
19 A="$A $i"
20 done
21 echo $A
22}
23
24# Get the value of a build variable as an absolute path.
25function get_abs_build_var()
26{
27 T=$(gettop)
28 if [ ! "$T" ]; then
29 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
30 return
31 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080032 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
33 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070034}
35
36# Get the exact value of a build variable.
37function get_build_var()
38{
39 T=$(gettop)
40 if [ ! "$T" ]; then
41 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
42 return
43 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080044 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
45 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
46}
47
48# check to see if the supplied product is one we can build
49function check_product()
50{
51 T=$(gettop)
52 if [ ! "$T" ]; then
53 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
54 return
55 fi
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
61}
62
63VARIANT_CHOICES=(user userdebug eng)
64
65# check to see if the supplied variant is valid
66function check_variant()
67{
68 for v in ${VARIANT_CHOICES[@]}
69 do
70 if [ "$v" = "$1" ]
71 then
72 return 0
73 fi
74 done
75 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070076}
77
78function setpaths()
79{
80 T=$(gettop)
81 if [ ! "$T" ]; then
82 echo "Couldn't locate the top of the tree. Try setting TOP."
83 return
84 fi
85
86 ##################################################################
87 # #
88 # Read me before you modify this code #
89 # #
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. #
94 # #
95 ##################################################################
96
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070097 # out with the old
98 if [ -n $ANDROID_BUILD_PATHS ] ; then
99 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
100 fi
101
102 # and in with the new
103 CODE_REVIEWS=
104 prebuiltdir=$(getprebuilt)
Jing Yub845c2f2009-06-17 12:06:23 -0700105 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.4.0/bin
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700106 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
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800110
111 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700112 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
113 export OUT=$ANDROID_PRODUCT_OUT
114
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800115 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700116 # TODO: fix the path
117 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800118
119 # needed for OProfile to post-process collected samples
120 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700121}
122
123function printconfig()
124{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800125 T=$(gettop)
126 if [ ! "$T" ]; then
127 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
128 return
129 fi
130 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700131}
132
133function set_stuff_for_environment()
134{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800135 settitle
136 setpaths
137 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800139 # Don't try to do preoptimization until it works better on OSX.
140 export DISABLE_DEXPREOPT=true
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700141
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700143}
144
145function set_sequence_number()
146{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800147 export BUILD_ENV_SEQUENCE_NUMBER=9
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700148}
149
150function settitle()
151{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800152 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\""
156 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700157}
158
159case `uname -s` in
160 Linux)
161 function choosesim()
162 {
163 echo "Build for the simulator or the device?"
164 echo " 1. Device"
165 echo " 2. Simulator"
166 echo
167
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800168 export TARGET_SIMULATOR=
169 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700170 while [ -z $TARGET_SIMULATOR ]
171 do
172 echo -n "Which would you like? [1] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800173 if [ -z "$1" ] ; then
174 read ANSWER
175 else
176 echo $1
177 ANSWER=$1
178 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700179 case $ANSWER in
180 "")
181 export TARGET_SIMULATOR=false
182 ;;
183 1)
184 export TARGET_SIMULATOR=false
185 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800186 Device)
187 export TARGET_SIMULATOR=false
188 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700189 2)
190 export TARGET_SIMULATOR=true
191 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800192 Simulator)
193 export TARGET_SIMULATOR=true
194 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195 *)
196 echo
197 echo "I didn't understand your response. Please try again."
198 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700199 ;;
200 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800201 if [ -n "$1" ] ; then
202 break
203 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700204 done
205
206 set_stuff_for_environment
207 }
208 ;;
209 *)
210 function choosesim()
211 {
212 echo "Only device builds are supported for" `uname -s`
213 echo " Forcing TARGET_SIMULATOR=false"
214 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800215 if [ -z "$1" ]
216 then
217 echo -n "Press enter: "
218 read
219 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700220
221 export TARGET_SIMULATOR=false
222 set_stuff_for_environment
223 }
224 ;;
225esac
226
227function choosetype()
228{
229 echo "Build type choices are:"
230 echo " 1. release"
231 echo " 2. debug"
232 echo
233
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800234 local DEFAULT_NUM DEFAULT_VALUE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700235 if [ $TARGET_SIMULATOR = "false" ] ; then
236 DEFAULT_NUM=1
237 DEFAULT_VALUE=release
238 else
239 DEFAULT_NUM=2
240 DEFAULT_VALUE=debug
241 fi
242
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800243 export TARGET_BUILD_TYPE=
244 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700245 while [ -z $TARGET_BUILD_TYPE ]
246 do
247 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800248 if [ -z "$1" ] ; then
249 read ANSWER
250 else
251 echo $1
252 ANSWER=$1
253 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700254 case $ANSWER in
255 "")
256 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
257 ;;
258 1)
259 export TARGET_BUILD_TYPE=release
260 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800261 release)
262 export TARGET_BUILD_TYPE=release
263 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700264 2)
265 export TARGET_BUILD_TYPE=debug
266 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800267 debug)
268 export TARGET_BUILD_TYPE=debug
269 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700270 *)
271 echo
272 echo "I didn't understand your response. Please try again."
273 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700274 ;;
275 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800276 if [ -n "$1" ] ; then
277 break
278 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700279 done
280
281 set_stuff_for_environment
282}
283
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800284#
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.
289#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700290function chooseproduct()
291{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700292 if [ "x$TARGET_PRODUCT" != x ] ; then
293 default_value=$TARGET_PRODUCT
294 else
295 if [ "$TARGET_SIMULATOR" = true ] ; then
296 default_value=sim
297 else
298 default_value=generic
299 fi
300 fi
301
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800302 export TARGET_PRODUCT=
303 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700304 while [ -z "$TARGET_PRODUCT" ]
305 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700306 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800307 if [ -z "$1" ] ; then
308 read ANSWER
309 else
310 echo $1
311 ANSWER=$1
312 fi
313
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700314 if [ -z "$ANSWER" ] ; then
315 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 else
317 if check_product $ANSWER
318 then
319 export TARGET_PRODUCT=$ANSWER
320 else
321 echo "** Not a valid product: $ANSWER"
322 fi
323 fi
324 if [ -n "$1" ] ; then
325 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700326 fi
327 done
328
329 set_stuff_for_environment
330}
331
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800332function choosevariant()
333{
334 echo "Variant choices are:"
335 local index=1
336 local v
337 for v in ${VARIANT_CHOICES[@]}
338 do
339 # The product name is the name of the directory containing
340 # the makefile we found, above.
341 echo " $index. $v"
342 index=$(($index+1))
343 done
344
345 local default_value=eng
346 local ANSWER
347
348 export TARGET_BUILD_VARIANT=
349 while [ -z "$TARGET_BUILD_VARIANT" ]
350 do
351 echo -n "Which would you like? [$default_value] "
352 if [ -z "$1" ] ; then
353 read ANSWER
354 else
355 echo $1
356 ANSWER=$1
357 fi
358
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))]}
364 fi
365 else
366 if check_variant $ANSWER
367 then
368 export TARGET_BUILD_VARIANT=$ANSWER
369 else
370 echo "** Not a valid variant: $ANSWER"
371 fi
372 fi
373 if [ -n "$1" ] ; then
374 break
375 fi
376 done
377}
378
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700379function tapas()
380{
381 choosecombo
382}
383
384function choosecombo()
385{
386 choosesim $1
387
388 echo
389 echo
390 choosetype $2
391
392 echo
393 echo
394 chooseproduct $3
395
396 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800397 echo
398 choosevariant $4
399
400 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700401 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800402 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700403}
404
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405# Clear this variable. It will be built up again when the vendorsetup.sh
406# files are included at the end of this file.
407unset LUNCH_MENU_CHOICES
408function add_lunch_combo()
409{
410 local new_combo=$1
411 local c
412 for c in ${LUNCH_MENU_CHOICES[@]} ; do
413 if [ "$new_combo" = "$c" ] ; then
414 return
415 fi
416 done
417 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
418}
419
420# add the default one here
421add_lunch_combo generic-eng
422
423# if we're on linux, add the simulator. There is a special case
424# in lunch to deal with the simulator
425if [ "$(uname)" = "Linux" ] ; then
426 add_lunch_combo simulator
427fi
428
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700429function print_lunch_menu()
430{
431 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 echo
433 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700434 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800435 echo ${LUNCH_MENU_CHOICES[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437
438 local i=1
439 local choice
440 for choice in ${LUNCH_MENU_CHOICES[@]}
441 do
442 echo " $i. $choice"
443 i=$(($i+1))
444 done
445
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700446 echo
447}
448
449function lunch()
450{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800451 local answer
452
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700453 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800454 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455 else
456 print_lunch_menu
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800457 echo -n "Which would you like? [generic-eng] "
458 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700459 fi
460
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800461 local selection=
462
463 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700464 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800465 selection=generic-eng
466 elif [ "$answer" = "simulator" ]
467 then
468 selection=simulator
469 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
470 then
471 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
472 then
473 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
474 fi
475 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
476 then
477 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478 fi
479
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480 if [ -z "$selection" ]
481 then
482 echo
483 echo "Invalid lunch combo: $answer"
484 return 1
485 fi
486
487 # special case the simulator
488 if [ "$selection" = "simulator" ]
489 then
490 export TARGET_PRODUCT=sim
491 export TARGET_BUILD_VARIANT=eng
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492 export TARGET_SIMULATOR=true
493 export TARGET_BUILD_TYPE=debug
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800494 else
495 local product=$(echo -n $selection | sed -e "s/-.*$//")
496 check_product $product
497 if [ $? -ne 0 ]
498 then
499 echo
500 echo "** Don't have a product spec for: '$product'"
501 echo "** Do you have the right repo manifest?"
502 product=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700503 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800504
505 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
506 check_variant $variant
507 if [ $? -ne 0 ]
508 then
509 echo
510 echo "** Invalid variant: '$variant'"
511 echo "** Must be one of ${VARIANT_CHOICES[@]}"
512 variant=
513 fi
514
515 if [ -z "$product" -o -z "$variant" ]
516 then
517 echo
518 return 1
519 fi
520
521 export TARGET_PRODUCT=$product
522 export TARGET_BUILD_VARIANT=$variant
523 export TARGET_SIMULATOR=false
524 export TARGET_BUILD_TYPE=release
525 fi # !simulator
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700526
527 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700529 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800530 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700531}
532
533function gettop
534{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800535 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700536 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
537 echo $TOP
538 else
539 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800540 # 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.
543 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544 else
545 # We redirect cd to /dev/null in case it's aliased to
546 # a command that prints something as a side-effect
547 # (like pushd)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549 T=
550 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
551 cd .. > /dev/null
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800552 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700553 done
554 cd $HERE > /dev/null
555 if [ -f "$T/$TOPFILE" ]; then
556 echo $T
557 fi
558 fi
559 fi
560}
561
562function m()
563{
564 T=$(gettop)
565 if [ "$T" ]; then
566 make -C $T $@
567 else
568 echo "Couldn't locate the top of the tree. Try setting TOP."
569 fi
570}
571
572function findmakefile()
573{
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
577 # (like pushd)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 T=
580 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
581 T=$PWD
582 if [ -f "$T/Android.mk" ]; then
583 echo $T/Android.mk
584 cd $HERE > /dev/null
585 return
586 fi
587 cd .. > /dev/null
588 done
589 cd $HERE > /dev/null
590}
591
592function mm()
593{
594 # If we're sitting in the root of the build tree, just do a
595 # normal make.
596 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
597 make $@
598 else
599 # Find the closest Android.mk file.
600 T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800601 local M=$(findmakefile)
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700602 # Remove the path to top as the makefilepath needs to be relative
603 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604 if [ ! "$T" ]; then
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."
608 else
609 ONE_SHOT_MAKEFILE=$M make -C $T files $@
610 fi
611 fi
612}
613
614function mmm()
615{
616 T=$(gettop)
617 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800618 local MAKEFILE=
619 local ARGS=
620 local DIR TO_CHOP
The Android Open Source Project88b60792009-03-03 19:28:42 -0800621 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
622 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
623 for DIR in $DIRS ; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700624 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`
Gilles Debunne8a20f582010-02-17 15:56:45 -0800628 START=`PWD= /bin/pwd`
629 MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700630 if [ "$MFILE" = "" ] ; then
631 MFILE=$DIR/Android.mk
632 else
633 MFILE=$MFILE/$DIR/Android.mk
634 fi
635 MAKEFILE="$MAKEFILE $MFILE"
636 else
637 if [ "$DIR" = snod ]; then
638 ARGS="$ARGS snod"
639 elif [ "$DIR" = showcommands ]; then
640 ARGS="$ARGS showcommands"
641 else
642 echo "No Android.mk in $DIR."
Joe Onorato51e61822009-04-13 15:36:15 -0400643 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700644 fi
645 fi
646 done
The Android Open Source Project88b60792009-03-03 19:28:42 -0800647 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648 else
649 echo "Couldn't locate the top of the tree. Try setting TOP."
650 fi
651}
652
653function croot()
654{
655 T=$(gettop)
656 if [ "$T" ]; then
657 cd $(gettop)
658 else
659 echo "Couldn't locate the top of the tree. Try setting TOP."
660 fi
661}
662
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700663function cproj()
664{
665 TOPFILE=build/core/envsetup.mk
666 # We redirect cd to /dev/null in case it's aliased to
667 # a command that prints something as a side-effect
668 # (like pushd)
669 local HERE=$PWD
670 T=
671 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
672 T=$PWD
673 if [ -f "$T/Android.mk" ]; then
674 cd $T
675 return
676 fi
677 cd .. > /dev/null
678 done
679 cd $HERE > /dev/null
680 echo "can't find Android.mk"
681}
682
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700683function pid()
684{
685 local EXE="$1"
686 if [ "$EXE" ] ; then
687 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
688 echo "$PID"
689 else
690 echo "usage: pid name"
691 fi
692}
693
Christopher Tate744ee802009-11-12 15:33:08 -0800694# systemstack - dump the current stack trace of all threads in the system process
695# to the usual ANR traces file
696function systemstack()
697{
698 adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
699}
700
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700701function gdbclient()
702{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800703 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
704 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
705 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
706 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
707 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700708 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
709 local EXE="$1"
710 if [ "$EXE" ] ; then
711 EXE=$1
712 else
713 EXE="app_process"
714 fi
715
716 local PORT="$2"
717 if [ "$PORT" ] ; then
718 PORT=$2
719 else
720 PORT=":5039"
721 fi
722
723 local PID
724 local PROG="$3"
725 if [ "$PROG" ] ; then
726 PID=`pid $3`
727 adb forward "tcp$PORT" "tcp$PORT"
728 adb shell gdbserver $PORT --attach $PID &
729 sleep 2
730 else
731 echo ""
732 echo "If you haven't done so already, do this first on the device:"
733 echo " gdbserver $PORT /system/bin/$EXE"
734 echo " or"
735 echo " gdbserver $PORT --attach $PID"
736 echo ""
737 fi
738
739 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
740 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
741 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
742 echo >>"$OUT_ROOT/gdbclient.cmds" ""
743
744 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
745 else
746 echo "Unable to determine build system output dir."
747 fi
748
749}
750
751case `uname -s` in
752 Darwin)
753 function sgrep()
754 {
Benno Leslie5ef878a2009-02-27 21:04:08 +1100755 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml|sh|mk)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700756 }
757
758 ;;
759 *)
760 function sgrep()
761 {
Benno Leslie5ef878a2009-02-27 21:04:08 +1100762 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\|sh\|mk\)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700763 }
764 ;;
765esac
766
767function jgrep()
768{
769 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
770}
771
772function cgrep()
773{
Ficus Kirkpatrick8889bdf2010-03-01 16:51:47 -0800774 find . -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700775}
776
777function resgrep()
778{
779 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
780}
781
782case `uname -s` in
783 Darwin)
784 function mgrep()
785 {
786 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
787 }
788
789 function treegrep()
790 {
791 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
792 }
793
794 ;;
795 *)
796 function mgrep()
797 {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800798 find . -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700799 }
800
801 function treegrep()
802 {
803 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
804 }
805
806 ;;
807esac
808
809function getprebuilt
810{
811 get_abs_build_var ANDROID_PREBUILTS
812}
813
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700814function tracedmdump()
815{
816 T=$(gettop)
817 if [ ! "$T" ]; then
818 echo "Couldn't locate the top of the tree. Try setting TOP."
819 return
820 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800821 local prebuiltdir=$(getprebuilt)
Jack Veenstra60116fc2009-04-09 18:12:34 -0700822 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700823
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800824 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700825 if [ ! "$TRACE" ] ; then
826 echo "usage: tracedmdump tracename"
827 return
828 fi
829
Jack Veenstra60116fc2009-04-09 18:12:34 -0700830 if [ ! -r "$KERNEL" ] ; then
831 echo "Error: cannot find kernel: '$KERNEL'"
832 return
833 fi
834
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800835 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700836 if [ "$BASETRACE" = "$TRACE" ] ; then
837 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
838 fi
839
840 echo "post-processing traces..."
841 rm -f $TRACE/qtrace.dexlist
842 post_trace $TRACE
843 if [ $? -ne 0 ]; then
844 echo "***"
845 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
846 echo "***"
847 return
848 fi
849 echo "generating dexlist output..."
850 /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
851 echo "generating dmtrace data..."
852 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
853 echo "generating html file..."
854 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
855 echo "done, see $TRACE/dmtrace.html for details"
856 echo "or run:"
857 echo " traceview $TRACE/dmtrace"
858}
859
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800860# communicate with a running device or emulator, set up necessary state,
861# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700862function runhat()
863{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800864 # process standard adb options
865 local adbTarget=""
866 if [ $1 = "-d" -o $1 = "-e" ]; then
867 adbTarget=$1
868 shift 1
869 elif [ $1 = "-s" ]; then
870 adbTarget="$1 $2"
871 shift 2
872 fi
873 local adbOptions=${adbTarget}
874 echo adbOptions = ${adbOptions}
875
876 # runhat options
877 local targetPid=$1
878 local outputFile=$2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700879
880 if [ "$targetPid" = "" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800881 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700882 return
883 fi
884
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800885 # confirm hat is available
886 if [ -z $(which hat) ]; then
887 echo "hat is not available in this configuration."
888 return
889 fi
890
891 adb ${adbOptions} shell >/dev/null mkdir /data/misc
892 adb ${adbOptions} shell chmod 777 /data/misc
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700893
The Android Open Source Project88b60792009-03-03 19:28:42 -0800894 # send a SIGUSR1 to cause the hprof dump
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700895 echo "Poking $targetPid and waiting for data..."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800896 adb ${adbOptions} shell kill -10 $targetPid
The Android Open Source Project88b60792009-03-03 19:28:42 -0800897 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700898 echo -n "> "
899 read
900
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800901 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
The Android Open Source Project88b60792009-03-03 19:28:42 -0800902 local devFile=/data/misc/${availFiles[0]}
903 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700904
The Android Open Source Project88b60792009-03-03 19:28:42 -0800905 echo "Retrieving file $devFile..."
906 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700907
The Android Open Source Project88b60792009-03-03 19:28:42 -0800908 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700909
The Android Open Source Project88b60792009-03-03 19:28:42 -0800910 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700911 echo "View the output by pointing your browser at http://localhost:7000/"
912 echo ""
The Android Open Source Project88b60792009-03-03 19:28:42 -0800913 hat $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700914}
915
916function getbugreports()
917{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800918 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700919
920 if [ ! "$reports" ]; then
921 echo "Could not locate any bugreports."
922 return
923 fi
924
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800925 local report
926 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700927 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800928 echo "/sdcard/bugreports/${report}"
929 adb pull /sdcard/bugreports/${report} ${report}
930 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700931 done
932}
933
934function startviewserver()
935{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800936 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700937 if [ $# -gt 0 ]; then
938 port=$1
939 fi
940 adb shell service call window 1 i32 $port
941}
942
943function stopviewserver()
944{
945 adb shell service call window 2
946}
947
948function isviewserverstarted()
949{
950 adb shell service call window 3
951}
952
953function smoketest()
954{
955 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
956 echo "Couldn't locate output files. Try running 'lunch' first." >&2
957 return
958 fi
959 T=$(gettop)
960 if [ ! "$T" ]; then
961 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
962 return
963 fi
964
965 (cd "$T" && mmm tests/SmokeTest) &&
966 adb uninstall com.android.smoketest > /dev/null &&
967 adb uninstall com.android.smoketest.tests > /dev/null &&
968 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
969 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
970 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
971}
972
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800973# simple shortcut to the runtest command
974function runtest()
975{
976 T=$(gettop)
977 if [ ! "$T" ]; then
978 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
979 return
980 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -0700981 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -0700982}
983
The Android Open Source Project88b60792009-03-03 19:28:42 -0800984function godir () {
985 if [[ -z "$1" ]]; then
986 echo "Usage: godir <regex>"
987 return
988 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -0800989 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800990 if [[ ! -f $T/filelist ]]; then
991 echo -n "Creating index..."
Brian Carlstrom259e5b72010-01-30 11:45:03 -0800992 (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800993 echo " Done"
994 echo ""
995 fi
996 local lines
997 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
998 if [[ ${#lines[@]} = 0 ]]; then
999 echo "Not found"
1000 return
1001 fi
1002 local pathname
1003 local choice
1004 if [[ ${#lines[@]} > 1 ]]; then
1005 while [[ -z "$pathname" ]]; do
1006 local index=1
1007 local line
1008 for line in ${lines[@]}; do
1009 printf "%6s %s\n" "[$index]" $line
1010 index=$(($index + 1))
1011 done
1012 echo
1013 echo -n "Select one: "
1014 unset choice
1015 read choice
1016 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1017 echo "Invalid choice"
1018 continue
1019 fi
1020 pathname=${lines[$(($choice-$_arrayoffset))]}
1021 done
1022 else
1023 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1024 pathname=${lines[0]}
1025 fi
1026 cd $T/$pathname
1027}
1028
Jeff Hamilton04be0d82010-06-07 15:03:54 -05001029# Force JAVA_HOME to point to java 1.5 if it isn't already set
1030if [ "$STAY_OFF_MY_LAWN" = "" ]; then
1031 if [ ! "$JAVA_HOME" ]; then
1032 case `uname -s` in
1033 Darwin)
1034 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
1035 ;;
1036 *)
1037 export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
1038 ;;
1039 esac
1040 fi
1041fi
1042
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001043# determine whether arrays are zero-based (bash) or one-based (zsh)
1044_xarray=(a b c)
1045if [ -z "${_xarray[${#_xarray[@]}]}" ]
1046then
1047 _arrayoffset=1
1048else
1049 _arrayoffset=0
1050fi
1051unset _xarray
1052
1053# Execute the contents of any vendorsetup.sh files we can find.
Jean-Baptiste Queru748e6382010-03-22 10:59:08 -07001054for f in `/bin/ls vendor/*/vendorsetup.sh vendor/*/build/vendorsetup.sh device/*/*/vendorsetup.sh 2> /dev/null`
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001055do
1056 echo "including $f"
1057 . $f
1058done
1059unset f