blob: 21a66c13e3301a6a1f7c026de626c201ad57b171 [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= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070059 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080060 get_build_var TARGET_DEVICE > /dev/null
61 # hide successful answers, but allow the errors to show
62}
63
64VARIANT_CHOICES=(user userdebug eng)
65
66# check to see if the supplied variant is valid
67function check_variant()
68{
69 for v in ${VARIANT_CHOICES[@]}
70 do
71 if [ "$v" = "$1" ]
72 then
73 return 0
74 fi
75 done
76 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070077}
78
79function setpaths()
80{
81 T=$(gettop)
82 if [ ! "$T" ]; then
83 echo "Couldn't locate the top of the tree. Try setting TOP."
84 return
85 fi
86
87 ##################################################################
88 # #
89 # Read me before you modify this code #
90 # #
91 # This function sets ANDROID_BUILD_PATHS to what it is adding #
92 # to PATH, and the next time it is run, it removes that from #
93 # PATH. This is required so lunch can be run more than once #
94 # and still have working paths. #
95 # #
96 ##################################################################
97
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070098 # out with the old
99 if [ -n $ANDROID_BUILD_PATHS ] ; then
100 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
101 fi
102
103 # and in with the new
104 CODE_REVIEWS=
105 prebuiltdir=$(getprebuilt)
Jing Yub845c2f2009-06-17 12:06:23 -0700106 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.4.0/bin
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700107 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
108 export ANDROID_QTOOLS=$T/development/emulator/qtools
109 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
110 export PATH=$PATH$ANDROID_BUILD_PATHS
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800111
112 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700113 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
114 export OUT=$ANDROID_PRODUCT_OUT
115
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800116 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700117 # TODO: fix the path
118 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800119
120 # needed for OProfile to post-process collected samples
121 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700122}
123
124function printconfig()
125{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800126 T=$(gettop)
127 if [ ! "$T" ]; then
128 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
129 return
130 fi
131 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700132}
133
134function set_stuff_for_environment()
135{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800136 settitle
137 setpaths
138 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700139
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140 # Don't try to do preoptimization until it works better on OSX.
141 export DISABLE_DEXPREOPT=true
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700142
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800143 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700144}
145
146function set_sequence_number()
147{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800148 export BUILD_ENV_SEQUENCE_NUMBER=9
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700149}
150
151function settitle()
152{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800153 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Joe Onoratoda12daf2010-06-09 18:18:31 -0700154 local product=$TARGET_PRODUCT
155 local variant=$TARGET_BUILD_VARIANT
156 local apps=$TARGET_BUILD_APPS
157 if [ -z "$apps" ]; then
158 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
159 else
160 export PROMPT_COMMAND="echo -ne \"\033]0;[$apps $variant] ${USER}@${HOSTNAME}: ${PWD}\007\""
161 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800162 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700163}
164
165case `uname -s` in
166 Linux)
167 function choosesim()
168 {
169 echo "Build for the simulator or the device?"
170 echo " 1. Device"
171 echo " 2. Simulator"
172 echo
173
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800174 export TARGET_SIMULATOR=
175 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700176 while [ -z $TARGET_SIMULATOR ]
177 do
178 echo -n "Which would you like? [1] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800179 if [ -z "$1" ] ; then
180 read ANSWER
181 else
182 echo $1
183 ANSWER=$1
184 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700185 case $ANSWER in
186 "")
187 export TARGET_SIMULATOR=false
188 ;;
189 1)
190 export TARGET_SIMULATOR=false
191 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800192 Device)
193 export TARGET_SIMULATOR=false
194 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195 2)
196 export TARGET_SIMULATOR=true
197 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800198 Simulator)
199 export TARGET_SIMULATOR=true
200 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700201 *)
202 echo
203 echo "I didn't understand your response. Please try again."
204 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700205 ;;
206 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800207 if [ -n "$1" ] ; then
208 break
209 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700210 done
211
212 set_stuff_for_environment
213 }
214 ;;
215 *)
216 function choosesim()
217 {
218 echo "Only device builds are supported for" `uname -s`
219 echo " Forcing TARGET_SIMULATOR=false"
220 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800221 if [ -z "$1" ]
222 then
223 echo -n "Press enter: "
224 read
225 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700226
227 export TARGET_SIMULATOR=false
228 set_stuff_for_environment
229 }
230 ;;
231esac
232
233function choosetype()
234{
235 echo "Build type choices are:"
236 echo " 1. release"
237 echo " 2. debug"
238 echo
239
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800240 local DEFAULT_NUM DEFAULT_VALUE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700241 if [ $TARGET_SIMULATOR = "false" ] ; then
242 DEFAULT_NUM=1
243 DEFAULT_VALUE=release
244 else
245 DEFAULT_NUM=2
246 DEFAULT_VALUE=debug
247 fi
248
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800249 export TARGET_BUILD_TYPE=
250 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700251 while [ -z $TARGET_BUILD_TYPE ]
252 do
253 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800254 if [ -z "$1" ] ; then
255 read ANSWER
256 else
257 echo $1
258 ANSWER=$1
259 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700260 case $ANSWER in
261 "")
262 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
263 ;;
264 1)
265 export TARGET_BUILD_TYPE=release
266 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800267 release)
268 export TARGET_BUILD_TYPE=release
269 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700270 2)
271 export TARGET_BUILD_TYPE=debug
272 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800273 debug)
274 export TARGET_BUILD_TYPE=debug
275 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700276 *)
277 echo
278 echo "I didn't understand your response. Please try again."
279 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700280 ;;
281 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800282 if [ -n "$1" ] ; then
283 break
284 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700285 done
286
287 set_stuff_for_environment
288}
289
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800290#
291# This function isn't really right: It chooses a TARGET_PRODUCT
292# based on the list of boards. Usually, that gets you something
293# that kinda works with a generic product, but really, you should
294# pick a product by name.
295#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700296function chooseproduct()
297{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700298 if [ "x$TARGET_PRODUCT" != x ] ; then
299 default_value=$TARGET_PRODUCT
300 else
301 if [ "$TARGET_SIMULATOR" = true ] ; then
302 default_value=sim
303 else
304 default_value=generic
305 fi
306 fi
307
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800308 export TARGET_PRODUCT=
309 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700310 while [ -z "$TARGET_PRODUCT" ]
311 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700312 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800313 if [ -z "$1" ] ; then
314 read ANSWER
315 else
316 echo $1
317 ANSWER=$1
318 fi
319
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700320 if [ -z "$ANSWER" ] ; then
321 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 else
323 if check_product $ANSWER
324 then
325 export TARGET_PRODUCT=$ANSWER
326 else
327 echo "** Not a valid product: $ANSWER"
328 fi
329 fi
330 if [ -n "$1" ] ; then
331 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700332 fi
333 done
334
335 set_stuff_for_environment
336}
337
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800338function choosevariant()
339{
340 echo "Variant choices are:"
341 local index=1
342 local v
343 for v in ${VARIANT_CHOICES[@]}
344 do
345 # The product name is the name of the directory containing
346 # the makefile we found, above.
347 echo " $index. $v"
348 index=$(($index+1))
349 done
350
351 local default_value=eng
352 local ANSWER
353
354 export TARGET_BUILD_VARIANT=
355 while [ -z "$TARGET_BUILD_VARIANT" ]
356 do
357 echo -n "Which would you like? [$default_value] "
358 if [ -z "$1" ] ; then
359 read ANSWER
360 else
361 echo $1
362 ANSWER=$1
363 fi
364
365 if [ -z "$ANSWER" ] ; then
366 export TARGET_BUILD_VARIANT=$default_value
367 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
368 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
369 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
370 fi
371 else
372 if check_variant $ANSWER
373 then
374 export TARGET_BUILD_VARIANT=$ANSWER
375 else
376 echo "** Not a valid variant: $ANSWER"
377 fi
378 fi
379 if [ -n "$1" ] ; then
380 break
381 fi
382 done
383}
384
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700385function choosecombo()
386{
387 choosesim $1
388
389 echo
390 echo
391 choosetype $2
392
393 echo
394 echo
395 chooseproduct $3
396
397 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800398 echo
399 choosevariant $4
400
401 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700402 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800403 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700404}
405
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800406# Clear this variable. It will be built up again when the vendorsetup.sh
407# files are included at the end of this file.
408unset LUNCH_MENU_CHOICES
409function add_lunch_combo()
410{
411 local new_combo=$1
412 local c
413 for c in ${LUNCH_MENU_CHOICES[@]} ; do
414 if [ "$new_combo" = "$c" ] ; then
415 return
416 fi
417 done
418 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
419}
420
421# add the default one here
422add_lunch_combo generic-eng
423
424# if we're on linux, add the simulator. There is a special case
425# in lunch to deal with the simulator
426if [ "$(uname)" = "Linux" ] ; then
427 add_lunch_combo simulator
428fi
429
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700430function print_lunch_menu()
431{
432 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700433 echo
434 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435 echo
436 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
Joe Onoratoda12daf2010-06-09 18:18:31 -0700487 export TARGET_BUILD_APPS=
488
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800489 # special case the simulator
490 if [ "$selection" = "simulator" ]
491 then
492 export TARGET_PRODUCT=sim
493 export TARGET_BUILD_VARIANT=eng
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494 export TARGET_SIMULATOR=true
495 export TARGET_BUILD_TYPE=debug
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800496 else
497 local product=$(echo -n $selection | sed -e "s/-.*$//")
498 check_product $product
499 if [ $? -ne 0 ]
500 then
501 echo
502 echo "** Don't have a product spec for: '$product'"
503 echo "** Do you have the right repo manifest?"
504 product=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700505 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800506
507 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
508 check_variant $variant
509 if [ $? -ne 0 ]
510 then
511 echo
512 echo "** Invalid variant: '$variant'"
513 echo "** Must be one of ${VARIANT_CHOICES[@]}"
514 variant=
515 fi
516
517 if [ -z "$product" -o -z "$variant" ]
518 then
519 echo
520 return 1
521 fi
522
523 export TARGET_PRODUCT=$product
524 export TARGET_BUILD_VARIANT=$variant
525 export TARGET_SIMULATOR=false
526 export TARGET_BUILD_TYPE=release
527 fi # !simulator
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700528
529 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800530
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700531 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800532 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700533}
534
Joe Onoratoda12daf2010-06-09 18:18:31 -0700535# Configures the build to build unbundled apps.
536# Run tapas with one ore more app names (from LOCAL_PACKAGE_NAME)
537function tapas()
538{
539 local variant=$(echo -n $(echo $* | xargs -n 1 echo | grep -E '^(user|userdebug|eng)$'))
540 local apps=$(echo -n $(echo $* | xargs -n 1 echo | grep -E -v '^(user|userdebug|eng)$'))
541
542 if [ $(echo $variant | wc -w) -gt 1 ]; then
543 echo "tapas: Error: Multiple build variants supplied: $variant"
544 return
545 fi
546 if [ -z "$variant" ]; then
547 variant=eng
548 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700549 if [ -z "$apps" ]; then
550 apps=all
551 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700552
553 export TARGET_PRODUCT=generic
554 export TARGET_BUILD_VARIANT=$variant
555 export TARGET_SIMULATOR=false
556 export TARGET_BUILD_TYPE=release
557 export TARGET_BUILD_APPS=$apps
558
559 set_stuff_for_environment
560 printconfig
561}
562
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563function gettop
564{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800565 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
567 echo $TOP
568 else
569 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800570 # The following circumlocution (repeated below as well) ensures
571 # that we record the true directory name and not one that is
572 # faked up with symlink names.
573 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 else
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 cd .. > /dev/null
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800582 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583 done
584 cd $HERE > /dev/null
585 if [ -f "$T/$TOPFILE" ]; then
586 echo $T
587 fi
588 fi
589 fi
590}
591
592function m()
593{
594 T=$(gettop)
595 if [ "$T" ]; then
596 make -C $T $@
597 else
598 echo "Couldn't locate the top of the tree. Try setting TOP."
599 fi
600}
601
602function findmakefile()
603{
604 TOPFILE=build/core/envsetup.mk
605 # We redirect cd to /dev/null in case it's aliased to
606 # a command that prints something as a side-effect
607 # (like pushd)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800608 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700609 T=
610 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
611 T=$PWD
612 if [ -f "$T/Android.mk" ]; then
613 echo $T/Android.mk
614 cd $HERE > /dev/null
615 return
616 fi
617 cd .. > /dev/null
618 done
619 cd $HERE > /dev/null
620}
621
622function mm()
623{
624 # If we're sitting in the root of the build tree, just do a
625 # normal make.
626 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
627 make $@
628 else
629 # Find the closest Android.mk file.
630 T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800631 local M=$(findmakefile)
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700632 # Remove the path to top as the makefilepath needs to be relative
633 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700634 if [ ! "$T" ]; then
635 echo "Couldn't locate the top of the tree. Try setting TOP."
636 elif [ ! "$M" ]; then
637 echo "Couldn't locate a makefile from the current directory."
638 else
639 ONE_SHOT_MAKEFILE=$M make -C $T files $@
640 fi
641 fi
642}
643
644function mmm()
645{
646 T=$(gettop)
647 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648 local MAKEFILE=
649 local ARGS=
650 local DIR TO_CHOP
The Android Open Source Project88b60792009-03-03 19:28:42 -0800651 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
652 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
653 for DIR in $DIRS ; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700654 DIR=`echo $DIR | sed -e 's:/$::'`
655 if [ -f $DIR/Android.mk ]; then
656 TO_CHOP=`echo $T | wc -c | tr -d ' '`
657 TO_CHOP=`expr $TO_CHOP + 1`
Gilles Debunne8a20f582010-02-17 15:56:45 -0800658 START=`PWD= /bin/pwd`
659 MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700660 if [ "$MFILE" = "" ] ; then
661 MFILE=$DIR/Android.mk
662 else
663 MFILE=$MFILE/$DIR/Android.mk
664 fi
665 MAKEFILE="$MAKEFILE $MFILE"
666 else
667 if [ "$DIR" = snod ]; then
668 ARGS="$ARGS snod"
669 elif [ "$DIR" = showcommands ]; then
670 ARGS="$ARGS showcommands"
671 else
672 echo "No Android.mk in $DIR."
Joe Onorato51e61822009-04-13 15:36:15 -0400673 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700674 fi
675 fi
676 done
The Android Open Source Project88b60792009-03-03 19:28:42 -0800677 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T $DASH_ARGS files $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700678 else
679 echo "Couldn't locate the top of the tree. Try setting TOP."
680 fi
681}
682
683function croot()
684{
685 T=$(gettop)
686 if [ "$T" ]; then
687 cd $(gettop)
688 else
689 echo "Couldn't locate the top of the tree. Try setting TOP."
690 fi
691}
692
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700693function cproj()
694{
695 TOPFILE=build/core/envsetup.mk
696 # We redirect cd to /dev/null in case it's aliased to
697 # a command that prints something as a side-effect
698 # (like pushd)
699 local HERE=$PWD
700 T=
701 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
702 T=$PWD
703 if [ -f "$T/Android.mk" ]; then
704 cd $T
705 return
706 fi
707 cd .. > /dev/null
708 done
709 cd $HERE > /dev/null
710 echo "can't find Android.mk"
711}
712
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700713function pid()
714{
715 local EXE="$1"
716 if [ "$EXE" ] ; then
717 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
718 echo "$PID"
719 else
720 echo "usage: pid name"
721 fi
722}
723
Christopher Tate744ee802009-11-12 15:33:08 -0800724# systemstack - dump the current stack trace of all threads in the system process
725# to the usual ANR traces file
726function systemstack()
727{
728 adb shell echo '""' '>>' /data/anr/traces.txt && adb shell chmod 776 /data/anr/traces.txt && adb shell kill -3 $(pid system_server)
729}
730
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700731function gdbclient()
732{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800733 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
734 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
735 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
736 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
737 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700738 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
739 local EXE="$1"
740 if [ "$EXE" ] ; then
741 EXE=$1
742 else
743 EXE="app_process"
744 fi
745
746 local PORT="$2"
747 if [ "$PORT" ] ; then
748 PORT=$2
749 else
750 PORT=":5039"
751 fi
752
753 local PID
754 local PROG="$3"
755 if [ "$PROG" ] ; then
756 PID=`pid $3`
757 adb forward "tcp$PORT" "tcp$PORT"
758 adb shell gdbserver $PORT --attach $PID &
759 sleep 2
760 else
761 echo ""
762 echo "If you haven't done so already, do this first on the device:"
763 echo " gdbserver $PORT /system/bin/$EXE"
764 echo " or"
765 echo " gdbserver $PORT --attach $PID"
766 echo ""
767 fi
768
769 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
770 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
771 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
772 echo >>"$OUT_ROOT/gdbclient.cmds" ""
773
774 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
775 else
776 echo "Unable to determine build system output dir."
777 fi
778
779}
780
781case `uname -s` in
782 Darwin)
783 function sgrep()
784 {
Benno Leslie5ef878a2009-02-27 21:04:08 +1100785 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 -0700786 }
787
788 ;;
789 *)
790 function sgrep()
791 {
Benno Leslie5ef878a2009-02-27 21:04:08 +1100792 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 -0700793 }
794 ;;
795esac
796
797function jgrep()
798{
799 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
800}
801
802function cgrep()
803{
Ficus Kirkpatrick8889bdf2010-03-01 16:51:47 -0800804 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 -0700805}
806
807function resgrep()
808{
809 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
810}
811
812case `uname -s` in
813 Darwin)
814 function mgrep()
815 {
816 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
817 }
818
819 function treegrep()
820 {
821 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
822 }
823
824 ;;
825 *)
826 function mgrep()
827 {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800828 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 -0700829 }
830
831 function treegrep()
832 {
833 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
834 }
835
836 ;;
837esac
838
839function getprebuilt
840{
841 get_abs_build_var ANDROID_PREBUILTS
842}
843
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700844function tracedmdump()
845{
846 T=$(gettop)
847 if [ ! "$T" ]; then
848 echo "Couldn't locate the top of the tree. Try setting TOP."
849 return
850 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800851 local prebuiltdir=$(getprebuilt)
Jack Veenstra60116fc2009-04-09 18:12:34 -0700852 local KERNEL=$T/prebuilt/android-arm/kernel/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700853
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800854 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700855 if [ ! "$TRACE" ] ; then
856 echo "usage: tracedmdump tracename"
857 return
858 fi
859
Jack Veenstra60116fc2009-04-09 18:12:34 -0700860 if [ ! -r "$KERNEL" ] ; then
861 echo "Error: cannot find kernel: '$KERNEL'"
862 return
863 fi
864
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800865 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700866 if [ "$BASETRACE" = "$TRACE" ] ; then
867 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
868 fi
869
870 echo "post-processing traces..."
871 rm -f $TRACE/qtrace.dexlist
872 post_trace $TRACE
873 if [ $? -ne 0 ]; then
874 echo "***"
875 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
876 echo "***"
877 return
878 fi
879 echo "generating dexlist output..."
880 /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
881 echo "generating dmtrace data..."
882 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
883 echo "generating html file..."
884 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
885 echo "done, see $TRACE/dmtrace.html for details"
886 echo "or run:"
887 echo " traceview $TRACE/dmtrace"
888}
889
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800890# communicate with a running device or emulator, set up necessary state,
891# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700892function runhat()
893{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800894 # process standard adb options
895 local adbTarget=""
896 if [ $1 = "-d" -o $1 = "-e" ]; then
897 adbTarget=$1
898 shift 1
899 elif [ $1 = "-s" ]; then
900 adbTarget="$1 $2"
901 shift 2
902 fi
903 local adbOptions=${adbTarget}
904 echo adbOptions = ${adbOptions}
905
906 # runhat options
907 local targetPid=$1
908 local outputFile=$2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700909
910 if [ "$targetPid" = "" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800911 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700912 return
913 fi
914
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800915 # confirm hat is available
916 if [ -z $(which hat) ]; then
917 echo "hat is not available in this configuration."
918 return
919 fi
920
921 adb ${adbOptions} shell >/dev/null mkdir /data/misc
922 adb ${adbOptions} shell chmod 777 /data/misc
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700923
The Android Open Source Project88b60792009-03-03 19:28:42 -0800924 # send a SIGUSR1 to cause the hprof dump
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700925 echo "Poking $targetPid and waiting for data..."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800926 adb ${adbOptions} shell kill -10 $targetPid
The Android Open Source Project88b60792009-03-03 19:28:42 -0800927 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700928 echo -n "> "
929 read
930
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800931 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 -0800932 local devFile=/data/misc/${availFiles[0]}
933 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700934
The Android Open Source Project88b60792009-03-03 19:28:42 -0800935 echo "Retrieving file $devFile..."
936 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700937
The Android Open Source Project88b60792009-03-03 19:28:42 -0800938 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700939
The Android Open Source Project88b60792009-03-03 19:28:42 -0800940 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700941 echo "View the output by pointing your browser at http://localhost:7000/"
942 echo ""
The Android Open Source Project88b60792009-03-03 19:28:42 -0800943 hat $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700944}
945
946function getbugreports()
947{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800948 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700949
950 if [ ! "$reports" ]; then
951 echo "Could not locate any bugreports."
952 return
953 fi
954
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800955 local report
956 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700957 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800958 echo "/sdcard/bugreports/${report}"
959 adb pull /sdcard/bugreports/${report} ${report}
960 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700961 done
962}
963
964function startviewserver()
965{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800966 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700967 if [ $# -gt 0 ]; then
968 port=$1
969 fi
970 adb shell service call window 1 i32 $port
971}
972
973function stopviewserver()
974{
975 adb shell service call window 2
976}
977
978function isviewserverstarted()
979{
980 adb shell service call window 3
981}
982
983function smoketest()
984{
985 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
986 echo "Couldn't locate output files. Try running 'lunch' first." >&2
987 return
988 fi
989 T=$(gettop)
990 if [ ! "$T" ]; then
991 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
992 return
993 fi
994
995 (cd "$T" && mmm tests/SmokeTest) &&
996 adb uninstall com.android.smoketest > /dev/null &&
997 adb uninstall com.android.smoketest.tests > /dev/null &&
998 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
999 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1000 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1001}
1002
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001003# simple shortcut to the runtest command
1004function runtest()
1005{
1006 T=$(gettop)
1007 if [ ! "$T" ]; then
1008 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1009 return
1010 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001011 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001012}
1013
The Android Open Source Project88b60792009-03-03 19:28:42 -08001014function godir () {
1015 if [[ -z "$1" ]]; then
1016 echo "Usage: godir <regex>"
1017 return
1018 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001019 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001020 if [[ ! -f $T/filelist ]]; then
1021 echo -n "Creating index..."
Brian Carlstrom259e5b72010-01-30 11:45:03 -08001022 (cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001023 echo " Done"
1024 echo ""
1025 fi
1026 local lines
1027 lines=($(grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
1028 if [[ ${#lines[@]} = 0 ]]; then
1029 echo "Not found"
1030 return
1031 fi
1032 local pathname
1033 local choice
1034 if [[ ${#lines[@]} > 1 ]]; then
1035 while [[ -z "$pathname" ]]; do
1036 local index=1
1037 local line
1038 for line in ${lines[@]}; do
1039 printf "%6s %s\n" "[$index]" $line
1040 index=$(($index + 1))
1041 done
1042 echo
1043 echo -n "Select one: "
1044 unset choice
1045 read choice
1046 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1047 echo "Invalid choice"
1048 continue
1049 fi
1050 pathname=${lines[$(($choice-$_arrayoffset))]}
1051 done
1052 else
1053 # even though zsh arrays are 1-based, $foo[0] is an alias for $foo[1]
1054 pathname=${lines[0]}
1055 fi
1056 cd $T/$pathname
1057}
1058
Jeff Hamilton04be0d82010-06-07 15:03:54 -05001059# Force JAVA_HOME to point to java 1.5 if it isn't already set
1060if [ "$STAY_OFF_MY_LAWN" = "" ]; then
1061 if [ ! "$JAVA_HOME" ]; then
1062 case `uname -s` in
1063 Darwin)
1064 export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
1065 ;;
1066 *)
1067 export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
1068 ;;
1069 esac
1070 fi
1071fi
1072
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001073# determine whether arrays are zero-based (bash) or one-based (zsh)
1074_xarray=(a b c)
1075if [ -z "${_xarray[${#_xarray[@]}]}" ]
1076then
1077 _arrayoffset=1
1078else
1079 _arrayoffset=0
1080fi
1081unset _xarray
1082
1083# Execute the contents of any vendorsetup.sh files we can find.
Jean-Baptiste Queru748e6382010-03-22 10:59:08 -07001084for 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 -08001085do
1086 echo "including $f"
1087 . $f
1088done
1089unset f