blob: e20d68413d5100e9bb774162b74e9adc151b2aaf [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.
11
12Look at the source to view more functions. The complete list is:
13EOF
14 T=$(gettop)
15 local A
16 A=""
17 for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
18 A="$A $i"
19 done
20 echo $A
21}
22
23# Get the value of a build variable as an absolute path.
24function get_abs_build_var()
25{
26 T=$(gettop)
27 if [ ! "$T" ]; then
28 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
29 return
30 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080031 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
32 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-abs-$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070033}
34
35# Get the exact value of a build variable.
36function get_build_var()
37{
38 T=$(gettop)
39 if [ ! "$T" ]; then
40 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
41 return
42 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080043 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
44 make --no-print-directory -C "$T" -f build/core/config.mk dumpvar-$1
45}
46
47# check to see if the supplied product is one we can build
48function check_product()
49{
50 T=$(gettop)
51 if [ ! "$T" ]; then
52 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
53 return
54 fi
55 CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
56 TARGET_PRODUCT=$1 TARGET_BUILD_VARIANT= \
57 TARGET_SIMULATOR= TARGET_BUILD_TYPE= \
58 get_build_var TARGET_DEVICE > /dev/null
59 # hide successful answers, but allow the errors to show
60}
61
62VARIANT_CHOICES=(user userdebug eng)
63
64# check to see if the supplied variant is valid
65function check_variant()
66{
67 for v in ${VARIANT_CHOICES[@]}
68 do
69 if [ "$v" = "$1" ]
70 then
71 return 0
72 fi
73 done
74 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070075}
76
77function setpaths()
78{
79 T=$(gettop)
80 if [ ! "$T" ]; then
81 echo "Couldn't locate the top of the tree. Try setting TOP."
82 return
83 fi
84
85 ##################################################################
86 # #
87 # Read me before you modify this code #
88 # #
89 # This function sets ANDROID_BUILD_PATHS to what it is adding #
90 # to PATH, and the next time it is run, it removes that from #
91 # PATH. This is required so lunch can be run more than once #
92 # and still have working paths. #
93 # #
94 ##################################################################
95
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070096 # out with the old
97 if [ -n $ANDROID_BUILD_PATHS ] ; then
98 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
99 fi
100
101 # and in with the new
102 CODE_REVIEWS=
103 prebuiltdir=$(getprebuilt)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800104 export ANDROID_EABI_TOOLCHAIN=$prebuiltdir/toolchain/arm-eabi-4.2.1/bin
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700105 export ANDROID_TOOLCHAIN=$ANDROID_EABI_TOOLCHAIN
106 export ANDROID_QTOOLS=$T/development/emulator/qtools
107 export ANDROID_BUILD_PATHS=:$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_QTOOLS:$ANDROID_TOOLCHAIN:$ANDROID_EABI_TOOLCHAIN$CODE_REVIEWS
108 export PATH=$PATH$ANDROID_BUILD_PATHS
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800109
110 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700111 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
112 export OUT=$ANDROID_PRODUCT_OUT
113
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800114 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115 # TODO: fix the path
116 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800117
118 # needed for OProfile to post-process collected samples
119 export OPROFILE_EVENTS_DIR=$prebuiltdir/oprofile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700120}
121
122function printconfig()
123{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800124 T=$(gettop)
125 if [ ! "$T" ]; then
126 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
127 return
128 fi
129 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700130}
131
132function set_stuff_for_environment()
133{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800134 settitle
135 setpaths
136 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700137
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800138 # Don't try to do preoptimization until it works better on OSX.
139 export DISABLE_DEXPREOPT=true
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700140
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800141 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700142}
143
144function set_sequence_number()
145{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 export BUILD_ENV_SEQUENCE_NUMBER=9
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700147}
148
149function settitle()
150{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800151 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
152 local product=$(get_build_var TARGET_PRODUCT)
153 local variant=$(get_build_var TARGET_BUILD_VARIANT)
154 export PROMPT_COMMAND="echo -ne \"\033]0;[${product}-${variant}] ${USER}@${HOSTNAME}: ${PWD}\007\""
155 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700156}
157
158case `uname -s` in
159 Linux)
160 function choosesim()
161 {
162 echo "Build for the simulator or the device?"
163 echo " 1. Device"
164 echo " 2. Simulator"
165 echo
166
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800167 export TARGET_SIMULATOR=
168 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700169 while [ -z $TARGET_SIMULATOR ]
170 do
171 echo -n "Which would you like? [1] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800172 if [ -z "$1" ] ; then
173 read ANSWER
174 else
175 echo $1
176 ANSWER=$1
177 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700178 case $ANSWER in
179 "")
180 export TARGET_SIMULATOR=false
181 ;;
182 1)
183 export TARGET_SIMULATOR=false
184 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800185 Device)
186 export TARGET_SIMULATOR=false
187 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700188 2)
189 export TARGET_SIMULATOR=true
190 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800191 Simulator)
192 export TARGET_SIMULATOR=true
193 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700194 *)
195 echo
196 echo "I didn't understand your response. Please try again."
197 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 ;;
199 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800200 if [ -n "$1" ] ; then
201 break
202 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700203 done
204
205 set_stuff_for_environment
206 }
207 ;;
208 *)
209 function choosesim()
210 {
211 echo "Only device builds are supported for" `uname -s`
212 echo " Forcing TARGET_SIMULATOR=false"
213 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800214 if [ -z "$1" ]
215 then
216 echo -n "Press enter: "
217 read
218 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700219
220 export TARGET_SIMULATOR=false
221 set_stuff_for_environment
222 }
223 ;;
224esac
225
226function choosetype()
227{
228 echo "Build type choices are:"
229 echo " 1. release"
230 echo " 2. debug"
231 echo
232
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800233 local DEFAULT_NUM DEFAULT_VALUE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700234 if [ $TARGET_SIMULATOR = "false" ] ; then
235 DEFAULT_NUM=1
236 DEFAULT_VALUE=release
237 else
238 DEFAULT_NUM=2
239 DEFAULT_VALUE=debug
240 fi
241
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800242 export TARGET_BUILD_TYPE=
243 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700244 while [ -z $TARGET_BUILD_TYPE ]
245 do
246 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800247 if [ -z "$1" ] ; then
248 read ANSWER
249 else
250 echo $1
251 ANSWER=$1
252 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700253 case $ANSWER in
254 "")
255 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
256 ;;
257 1)
258 export TARGET_BUILD_TYPE=release
259 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800260 release)
261 export TARGET_BUILD_TYPE=release
262 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700263 2)
264 export TARGET_BUILD_TYPE=debug
265 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800266 debug)
267 export TARGET_BUILD_TYPE=debug
268 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700269 *)
270 echo
271 echo "I didn't understand your response. Please try again."
272 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700273 ;;
274 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800275 if [ -n "$1" ] ; then
276 break
277 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700278 done
279
280 set_stuff_for_environment
281}
282
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800283#
284# This function isn't really right: It chooses a TARGET_PRODUCT
285# based on the list of boards. Usually, that gets you something
286# that kinda works with a generic product, but really, you should
287# pick a product by name.
288#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700289function chooseproduct()
290{
291 # Find the makefiles that must exist for a product.
292 # Send stderr to /dev/null in case partner isn't present.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800293 local -a choices
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700294 choices=(`/bin/ls build/target/board/*/BoardConfig.mk vendor/*/*/BoardConfig.mk 2> /dev/null`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700295
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800296 local choice
297 local -a prodlist
298 for choice in ${choices[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700299 do
300 # The product name is the name of the directory containing
301 # the makefile we found, above.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800302 prodlist=(${prodlist[@]} `dirname ${choice} | xargs basename`)
303 done
304
305 local index=1
306 local p
307 echo "Product choices are:"
308 for p in ${prodlist[@]}
309 do
310 echo " $index. $p"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311 let "index = $index + 1"
312 done
313
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800314
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700315 if [ "x$TARGET_PRODUCT" != x ] ; then
316 default_value=$TARGET_PRODUCT
317 else
318 if [ "$TARGET_SIMULATOR" = true ] ; then
319 default_value=sim
320 else
321 default_value=generic
322 fi
323 fi
324
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 export TARGET_PRODUCT=
326 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327 while [ -z "$TARGET_PRODUCT" ]
328 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 echo "You can also type the name of a product if you know it."
330 echo -n "Which would you like? [$default_value] "
331 if [ -z "$1" ] ; then
332 read ANSWER
333 else
334 echo $1
335 ANSWER=$1
336 fi
337
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700338 if [ -z "$ANSWER" ] ; then
339 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800340 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
341 local poo=`echo -n $ANSWER`
342 if [ $poo -le ${#prodlist[@]} ] ; then
343 export TARGET_PRODUCT=${prodlist[$(($ANSWER-$_arrayoffset))]}
344 else
345 echo "** Bad product selection: $ANSWER"
346 fi
347 else
348 if check_product $ANSWER
349 then
350 export TARGET_PRODUCT=$ANSWER
351 else
352 echo "** Not a valid product: $ANSWER"
353 fi
354 fi
355 if [ -n "$1" ] ; then
356 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700357 fi
358 done
359
360 set_stuff_for_environment
361}
362
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800363function choosevariant()
364{
365 echo "Variant choices are:"
366 local index=1
367 local v
368 for v in ${VARIANT_CHOICES[@]}
369 do
370 # The product name is the name of the directory containing
371 # the makefile we found, above.
372 echo " $index. $v"
373 index=$(($index+1))
374 done
375
376 local default_value=eng
377 local ANSWER
378
379 export TARGET_BUILD_VARIANT=
380 while [ -z "$TARGET_BUILD_VARIANT" ]
381 do
382 echo -n "Which would you like? [$default_value] "
383 if [ -z "$1" ] ; then
384 read ANSWER
385 else
386 echo $1
387 ANSWER=$1
388 fi
389
390 if [ -z "$ANSWER" ] ; then
391 export TARGET_BUILD_VARIANT=$default_value
392 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
393 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
394 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-$_arrayoffset))]}
395 fi
396 else
397 if check_variant $ANSWER
398 then
399 export TARGET_BUILD_VARIANT=$ANSWER
400 else
401 echo "** Not a valid variant: $ANSWER"
402 fi
403 fi
404 if [ -n "$1" ] ; then
405 break
406 fi
407 done
408}
409
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700410function tapas()
411{
412 choosecombo
413}
414
415function choosecombo()
416{
417 choosesim $1
418
419 echo
420 echo
421 choosetype $2
422
423 echo
424 echo
425 chooseproduct $3
426
427 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800428 echo
429 choosevariant $4
430
431 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800433 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700434}
435
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800436# Clear this variable. It will be built up again when the vendorsetup.sh
437# files are included at the end of this file.
438unset LUNCH_MENU_CHOICES
439function add_lunch_combo()
440{
441 local new_combo=$1
442 local c
443 for c in ${LUNCH_MENU_CHOICES[@]} ; do
444 if [ "$new_combo" = "$c" ] ; then
445 return
446 fi
447 done
448 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
449}
450
451# add the default one here
452add_lunch_combo generic-eng
453
454# if we're on linux, add the simulator. There is a special case
455# in lunch to deal with the simulator
456if [ "$(uname)" = "Linux" ] ; then
457 add_lunch_combo simulator
458fi
459
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460function print_lunch_menu()
461{
462 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700463 echo
464 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800466 echo ${LUNCH_MENU_CHOICES[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468
469 local i=1
470 local choice
471 for choice in ${LUNCH_MENU_CHOICES[@]}
472 do
473 echo " $i. $choice"
474 i=$(($i+1))
475 done
476
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 echo
478}
479
480function lunch()
481{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800482 local answer
483
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800485 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700486 else
487 print_lunch_menu
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800488 echo -n "Which would you like? [generic-eng] "
489 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 fi
491
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800492 local selection=
493
494 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800496 selection=generic-eng
497 elif [ "$answer" = "simulator" ]
498 then
499 selection=simulator
500 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
501 then
502 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
503 then
504 selection=${LUNCH_MENU_CHOICES[$(($answer-$_arrayoffset))]}
505 fi
506 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
507 then
508 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 fi
510
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800511 if [ -z "$selection" ]
512 then
513 echo
514 echo "Invalid lunch combo: $answer"
515 return 1
516 fi
517
518 # special case the simulator
519 if [ "$selection" = "simulator" ]
520 then
521 export TARGET_PRODUCT=sim
522 export TARGET_BUILD_VARIANT=eng
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700523 export TARGET_SIMULATOR=true
524 export TARGET_BUILD_TYPE=debug
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800525 else
526 local product=$(echo -n $selection | sed -e "s/-.*$//")
527 check_product $product
528 if [ $? -ne 0 ]
529 then
530 echo
531 echo "** Don't have a product spec for: '$product'"
532 echo "** Do you have the right repo manifest?"
533 product=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800535
536 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
537 check_variant $variant
538 if [ $? -ne 0 ]
539 then
540 echo
541 echo "** Invalid variant: '$variant'"
542 echo "** Must be one of ${VARIANT_CHOICES[@]}"
543 variant=
544 fi
545
546 if [ -z "$product" -o -z "$variant" ]
547 then
548 echo
549 return 1
550 fi
551
552 export TARGET_PRODUCT=$product
553 export TARGET_BUILD_VARIANT=$variant
554 export TARGET_SIMULATOR=false
555 export TARGET_BUILD_TYPE=release
556 fi # !simulator
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700557
558 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700562}
563
564function gettop
565{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700567 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
568 echo $TOP
569 else
570 if [ -f $TOPFILE ] ; then
571 echo $PWD
572 else
573 # We redirect cd to /dev/null in case it's aliased to
574 # a command that prints something as a side-effect
575 # (like pushd)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800576 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 T=
578 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
579 cd .. > /dev/null
580 T=$PWD
581 done
582 cd $HERE > /dev/null
583 if [ -f "$T/$TOPFILE" ]; then
584 echo $T
585 fi
586 fi
587 fi
588}
589
590function m()
591{
592 T=$(gettop)
593 if [ "$T" ]; then
594 make -C $T $@
595 else
596 echo "Couldn't locate the top of the tree. Try setting TOP."
597 fi
598}
599
600function findmakefile()
601{
602 TOPFILE=build/core/envsetup.mk
603 # We redirect cd to /dev/null in case it's aliased to
604 # a command that prints something as a side-effect
605 # (like pushd)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800606 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700607 T=
608 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
609 T=$PWD
610 if [ -f "$T/Android.mk" ]; then
611 echo $T/Android.mk
612 cd $HERE > /dev/null
613 return
614 fi
615 cd .. > /dev/null
616 done
617 cd $HERE > /dev/null
618}
619
620function mm()
621{
622 # If we're sitting in the root of the build tree, just do a
623 # normal make.
624 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
625 make $@
626 else
627 # Find the closest Android.mk file.
628 T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800629 local M=$(findmakefile)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700630 if [ ! "$T" ]; then
631 echo "Couldn't locate the top of the tree. Try setting TOP."
632 elif [ ! "$M" ]; then
633 echo "Couldn't locate a makefile from the current directory."
634 else
635 ONE_SHOT_MAKEFILE=$M make -C $T files $@
636 fi
637 fi
638}
639
640function mmm()
641{
642 T=$(gettop)
643 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800644 local MAKEFILE=
645 local ARGS=
646 local DIR TO_CHOP
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700647 for DIR in $@ ; do
648 DIR=`echo $DIR | sed -e 's:/$::'`
649 if [ -f $DIR/Android.mk ]; then
650 TO_CHOP=`echo $T | wc -c | tr -d ' '`
651 TO_CHOP=`expr $TO_CHOP + 1`
652 MFILE=`echo $PWD | cut -c${TO_CHOP}-`
653 if [ "$MFILE" = "" ] ; then
654 MFILE=$DIR/Android.mk
655 else
656 MFILE=$MFILE/$DIR/Android.mk
657 fi
658 MAKEFILE="$MAKEFILE $MFILE"
659 else
660 if [ "$DIR" = snod ]; then
661 ARGS="$ARGS snod"
662 elif [ "$DIR" = showcommands ]; then
663 ARGS="$ARGS showcommands"
664 else
665 echo "No Android.mk in $DIR."
666 fi
667 fi
668 done
669 ONE_SHOT_MAKEFILE="$MAKEFILE" make -C $T files $ARGS
670 else
671 echo "Couldn't locate the top of the tree. Try setting TOP."
672 fi
673}
674
675function croot()
676{
677 T=$(gettop)
678 if [ "$T" ]; then
679 cd $(gettop)
680 else
681 echo "Couldn't locate the top of the tree. Try setting TOP."
682 fi
683}
684
685function pid()
686{
687 local EXE="$1"
688 if [ "$EXE" ] ; then
689 local PID=`adb shell ps | fgrep $1 | sed -e 's/[^ ]* *\([0-9]*\).*/\1/'`
690 echo "$PID"
691 else
692 echo "usage: pid name"
693 fi
694}
695
696function gdbclient()
697{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800698 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
699 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
700 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
701 local OUT_EXE_SYMBOLS=$(get_abs_build_var TARGET_OUT_EXECUTABLES_UNSTRIPPED)
702 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700703 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
704 local EXE="$1"
705 if [ "$EXE" ] ; then
706 EXE=$1
707 else
708 EXE="app_process"
709 fi
710
711 local PORT="$2"
712 if [ "$PORT" ] ; then
713 PORT=$2
714 else
715 PORT=":5039"
716 fi
717
718 local PID
719 local PROG="$3"
720 if [ "$PROG" ] ; then
721 PID=`pid $3`
722 adb forward "tcp$PORT" "tcp$PORT"
723 adb shell gdbserver $PORT --attach $PID &
724 sleep 2
725 else
726 echo ""
727 echo "If you haven't done so already, do this first on the device:"
728 echo " gdbserver $PORT /system/bin/$EXE"
729 echo " or"
730 echo " gdbserver $PORT --attach $PID"
731 echo ""
732 fi
733
734 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
735 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS"
736 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
737 echo >>"$OUT_ROOT/gdbclient.cmds" ""
738
739 arm-eabi-gdb -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
740 else
741 echo "Unable to determine build system output dir."
742 fi
743
744}
745
746case `uname -s` in
747 Darwin)
748 function sgrep()
749 {
750 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n "$@"
751 }
752
753 ;;
754 *)
755 function sgrep()
756 {
757 find . -type f -iregex '.*\.\(c\|h\|cpp\|S\|java\|xml\)' -print0 | xargs -0 grep --color -n "$@"
758 }
759 ;;
760esac
761
762function jgrep()
763{
764 find . -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
765}
766
767function cgrep()
768{
769 find . -type f -name "*\.c*" -print0 | xargs -0 grep --color -n "$@"
770}
771
772function resgrep()
773{
774 for dir in `find . -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
775}
776
777case `uname -s` in
778 Darwin)
779 function mgrep()
780 {
781 find -E . -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
782 }
783
784 function treegrep()
785 {
786 find -E . -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
787 }
788
789 ;;
790 *)
791 function mgrep()
792 {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800793 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 -0700794 }
795
796 function treegrep()
797 {
798 find . -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
799 }
800
801 ;;
802esac
803
804function getprebuilt
805{
806 get_abs_build_var ANDROID_PREBUILTS
807}
808
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700809function tracedmdump()
810{
811 T=$(gettop)
812 if [ ! "$T" ]; then
813 echo "Couldn't locate the top of the tree. Try setting TOP."
814 return
815 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800816 local prebuiltdir=$(getprebuilt)
817 local KERNEL=$T/prebuilt/android-arm/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700818
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800819 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700820 if [ ! "$TRACE" ] ; then
821 echo "usage: tracedmdump tracename"
822 return
823 fi
824
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800825 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826 if [ "$BASETRACE" = "$TRACE" ] ; then
827 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
828 fi
829
830 echo "post-processing traces..."
831 rm -f $TRACE/qtrace.dexlist
832 post_trace $TRACE
833 if [ $? -ne 0 ]; then
834 echo "***"
835 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
836 echo "***"
837 return
838 fi
839 echo "generating dexlist output..."
840 /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
841 echo "generating dmtrace data..."
842 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
843 echo "generating html file..."
844 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
845 echo "done, see $TRACE/dmtrace.html for details"
846 echo "or run:"
847 echo " traceview $TRACE/dmtrace"
848}
849
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800850# communicate with a running device or emulator, set up necessary state,
851# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700852function runhat()
853{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800854 # process standard adb options
855 local adbTarget=""
856 if [ $1 = "-d" -o $1 = "-e" ]; then
857 adbTarget=$1
858 shift 1
859 elif [ $1 = "-s" ]; then
860 adbTarget="$1 $2"
861 shift 2
862 fi
863 local adbOptions=${adbTarget}
864 echo adbOptions = ${adbOptions}
865
866 # runhat options
867 local targetPid=$1
868 local outputFile=$2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700869
870 if [ "$targetPid" = "" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800871 echo "Usage: runhat [ -d | -e | -s serial ] target-pid [output-file]"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700872 return
873 fi
874
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800875 # confirm hat is available
876 if [ -z $(which hat) ]; then
877 echo "hat is not available in this configuration."
878 return
879 fi
880
881 adb ${adbOptions} shell >/dev/null mkdir /data/misc
882 adb ${adbOptions} shell chmod 777 /data/misc
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700883
884 echo "Poking $targetPid and waiting for data..."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800885 adb ${adbOptions} shell kill -10 $targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700886 echo "Press enter when logcat shows \"GC freed ## objects / ## bytes\""
887 echo -n "> "
888 read
889
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800890 local availFiles=( $(adb ${adbOptions} shell ls /data/misc | grep '^heap-dump' | sed -e 's/.*heap-dump-/heap-dump-/' | sort -r | tr '[:space:][:cntrl:]' ' ') )
891 local devHeadFile=/data/misc/${availFiles[0]}
892 local devTailFile=/data/misc/${availFiles[1]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700893
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800894 local localHeadFile=/tmp/$$-hprof-head
895 local localTailFile=/tmp/$$-hprof-tail
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700896
897 echo "Retrieving file $devHeadFile..."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800898 adb ${adbOptions} pull $devHeadFile $localHeadFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700899 echo "Retrieving file $devTailFile..."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800900 adb ${adbOptions} pull $devTailFile $localTailFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700901
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800902 local combinedFile=$outputFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700903 if [ "$combinedFile" = "" ]; then
904 combinedFile=/tmp/$$.hprof
905 fi
906
907 cat $localHeadFile $localTailFile >$combinedFile
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800908 adb ${adbOptions} shell rm $devHeadFile
909 adb ${adbOptions} shell rm $devTailFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700910 rm $localHeadFile
911 rm $localTailFile
912
913 echo "Running hat on $combinedFile"
914 echo "View the output by pointing your browser at http://localhost:7000/"
915 echo ""
916 hat $combinedFile
917}
918
919function getbugreports()
920{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800921 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700922
923 if [ ! "$reports" ]; then
924 echo "Could not locate any bugreports."
925 return
926 fi
927
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800928 local report
929 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700930 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800931 echo "/sdcard/bugreports/${report}"
932 adb pull /sdcard/bugreports/${report} ${report}
933 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700934 done
935}
936
937function startviewserver()
938{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800939 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700940 if [ $# -gt 0 ]; then
941 port=$1
942 fi
943 adb shell service call window 1 i32 $port
944}
945
946function stopviewserver()
947{
948 adb shell service call window 2
949}
950
951function isviewserverstarted()
952{
953 adb shell service call window 3
954}
955
956function smoketest()
957{
958 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
959 echo "Couldn't locate output files. Try running 'lunch' first." >&2
960 return
961 fi
962 T=$(gettop)
963 if [ ! "$T" ]; then
964 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
965 return
966 fi
967
968 (cd "$T" && mmm tests/SmokeTest) &&
969 adb uninstall com.android.smoketest > /dev/null &&
970 adb uninstall com.android.smoketest.tests > /dev/null &&
971 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
972 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
973 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
974}
975
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800976# simple shortcut to the runtest command
977function runtest()
978{
979 T=$(gettop)
980 if [ ! "$T" ]; then
981 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
982 return
983 fi
984 (cd "$T" && development/tools/runtest $@)
985}
986
987# determine whether arrays are zero-based (bash) or one-based (zsh)
988_xarray=(a b c)
989if [ -z "${_xarray[${#_xarray[@]}]}" ]
990then
991 _arrayoffset=1
992else
993 _arrayoffset=0
994fi
995unset _xarray
996
997# Execute the contents of any vendorsetup.sh files we can find.
998for f in `/bin/ls vendor/*/vendorsetup.sh 2> /dev/null`
999do
1000 echo "including $f"
1001 . $f
1002done
1003unset f