blob: 7fdfbbabe2b079a3ee745bf04c5d67c3af846458 [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<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:
Ying Wang67f02922012-08-22 10:25:20 -07004- lunch: lunch <product_name>-<build_variant>
Ying Wangb541ab62014-05-29 17:57:40 -07005- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07006- croot: Changes directory to the top of the tree.
nebkatfb67a1e2012-12-28 10:40:45 +00007- cout: Changes directory to out.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07008- m: Makes from the top of the tree.
Ying Wangb607f7b2013-02-08 18:01:04 -08009- mm: Builds all of the modules in the current directory, but not their dependencies.
10- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
Primiano Tucci6a8069d2014-02-26 11:09:19 +000011 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Ying Wangb607f7b2013-02-08 18:01:04 -080012- mma: Builds all of the modules in the current directory, and their dependencies.
Daniel Bateman22185ba2012-08-04 03:48:09 -050013- mmp: Builds all of the modules in the current directory and pushes them to the device.
14- mmmp: Builds all of the modules in the supplied directories and pushes them to the device.
Ying Wangb607f7b2013-02-08 18:01:04 -080015- mmma: Builds all of the modules in the supplied directories, and their dependencies.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070016- cgrep: Greps on all local C/C++ files.
Jon Boekenoogencbca56f2014-04-07 10:57:38 -070017- ggrep: Greps on all local Gradle files.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070018- jgrep: Greps on all local Java files.
19- resgrep: Greps on all local res/*.xml files.
Trevor Drake9c889762015-01-30 04:42:21 +000020- mangrep: Greps on all local AndroidManifest.xml files.
21- sepgrep: Greps on all local sepolicy files.
Jeff Brown46cbd202014-07-26 15:15:41 -070022- sgrep: Greps on all local source files.
The Android Open Source Project88b60792009-03-03 19:28:42 -080023- godir: Go to the directory containing a file.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060024- cmremote: Add git remote for CM Gerrit Review
25- cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
26- cmrebase: Rebase a Gerrit change and push it again
Steve Kondik873fa562012-09-17 11:33:18 -070027- aospremote: Add git remote for matching AOSP repository
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060028- mka: Builds using SCHED_BATCH on all processors
29- reposync: Parallel repo sync using ionice and SCHED_BATCH
Steve Kondik83c03d52012-10-24 16:40:42 -070030- installboot: Installs a boot.img to the connected device.
31- installrecovery: Installs a recovery.img to the connected device.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070032
Dan Albert4ae5d4b2014-10-31 16:23:08 -070033Environemnt options:
34- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
35 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
36 build is leak-check clean.
37
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070038Look at the source to view more functions. The complete list is:
39EOF
40 T=$(gettop)
41 local A
42 A=""
Jeff Brown46cbd202014-07-26 15:15:41 -070043 for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070044 A="$A $i"
45 done
46 echo $A
47}
48
49# Get the value of a build variable as an absolute path.
50function get_abs_build_var()
51{
52 T=$(gettop)
53 if [ ! "$T" ]; then
54 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
55 return
56 fi
Ying Wang9cd17642012-12-13 10:52:07 -080057 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070058 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070059}
60
61# Get the exact value of a build variable.
62function get_build_var()
63{
64 T=$(gettop)
65 if [ ! "$T" ]; then
66 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
67 return
68 fi
Andrew Boie6905e212013-12-19 13:21:46 -080069 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070070 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080071}
72
73# check to see if the supplied product is one we can build
74function check_product()
75{
76 T=$(gettop)
77 if [ ! "$T" ]; then
78 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
79 return
80 fi
Ricardo Cerqueira119d3bb2011-11-25 15:30:36 +000081
82 if (echo -n $1 | grep -q -e "^cm_") ; then
83 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
84 else
85 CM_BUILD=
86 fi
87 export CM_BUILD
88
Jeff Browne33ba4c2011-07-11 22:11:46 -070089 TARGET_PRODUCT=$1 \
90 TARGET_BUILD_VARIANT= \
91 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070092 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080093 get_build_var TARGET_DEVICE > /dev/null
94 # hide successful answers, but allow the errors to show
95}
96
97VARIANT_CHOICES=(user userdebug eng)
98
99# check to see if the supplied variant is valid
100function check_variant()
101{
102 for v in ${VARIANT_CHOICES[@]}
103 do
104 if [ "$v" = "$1" ]
105 then
106 return 0
107 fi
108 done
109 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700110}
111
112function setpaths()
113{
114 T=$(gettop)
115 if [ ! "$T" ]; then
116 echo "Couldn't locate the top of the tree. Try setting TOP."
117 return
118 fi
119
120 ##################################################################
121 # #
122 # Read me before you modify this code #
123 # #
124 # This function sets ANDROID_BUILD_PATHS to what it is adding #
125 # to PATH, and the next time it is run, it removes that from #
126 # PATH. This is required so lunch can be run more than once #
127 # and still have working paths. #
128 # #
129 ##################################################################
130
Raphael Mollc639c782011-06-20 17:25:01 -0700131 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
132 # due to "C:\Program Files" being in the path.
133
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700134 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700135 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700136 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
137 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700138 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700139 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800140 # strip leading ':', if any
141 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500142 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700143
144 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700145 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700146 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700147
Ben Cheng8bc4c432012-11-16 13:29:13 -0800148 # defined in core/config.mk
149 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700150 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800151 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800152
Raphael Mollc639c782011-06-20 17:25:01 -0700153 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800154 export ANDROID_TOOLCHAIN=
155 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200156 local ARCH=$(get_build_var TARGET_ARCH)
157 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400158 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700159 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400160 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
161 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800162 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200163 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800164 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700165 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700166 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700167 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000168 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200169 *)
170 echo "Can't find toolchain for unknown architecture: $ARCH"
171 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700172 ;;
173 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700174 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800175 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700176 fi
Raphael732936d2011-06-22 14:35:32 -0700177
Ben Chengfba67bf2014-02-25 10:27:07 -0800178 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
179 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
180 fi
181
182 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700183 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700184 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800185 # Legacy toolchain configuration used for ARM kernel compilation
Ben Cheng8bc4c432012-11-16 13:29:13 -0800186 toolchaindir=arm/arm-eabi-$targetgccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700187 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100188 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
189 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700190 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700191 ;;
192 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700193 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700194 ;;
195 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700196
Jeff Vander Stoep5aa68322015-06-12 09:56:39 -0700197 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Ying Wang2a859d52014-06-30 10:25:33 -0700198 export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_KERNEL_TOOLCHAIN_PATH$ANDROID_DEV_SCRIPTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200199
200 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
201 # to ensure that the corresponding 'emulator' binaries are used.
202 case $(uname -s) in
203 Darwin)
204 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
205 ;;
206 Linux)
207 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
208 ;;
209 *)
210 ANDROID_EMULATOR_PREBUILTS=
211 ;;
212 esac
213 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700214 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200215 export ANDROID_EMULATOR_PREBUILTS
216 fi
217
Ying Wangaa1c9b52012-11-26 20:51:59 -0800218 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800219
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500220 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900221 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500222 if [ -n "$JAVA_HOME" ]; then
223 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900224 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
225 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500226 fi
227
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800228 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700229 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
230 export OUT=$ANDROID_PRODUCT_OUT
231
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700232 unset ANDROID_HOST_OUT
233 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
234
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800235 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700236 # TODO: fix the path
237 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
238}
239
240function printconfig()
241{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800242 T=$(gettop)
243 if [ ! "$T" ]; then
244 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
245 return
246 fi
247 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700248}
249
250function set_stuff_for_environment()
251{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800252 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500253 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800254 setpaths
255 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700256
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800257 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700258 # With this environment variable new GCC can apply colors to warnings/errors
259 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700260 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700261}
262
263function set_sequence_number()
264{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700265 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700266}
267
268function settitle()
269{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800270 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700271 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700272 local product=$TARGET_PRODUCT
273 local variant=$TARGET_BUILD_VARIANT
274 local apps=$TARGET_BUILD_APPS
Steve Kondikd6fba552012-12-27 15:28:34 -0800275 if [ -z "$PROMPT_COMMAND" ]; then
276 # No prompts
277 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
278 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
279 # Prompts exist, but no hardstatus
280 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700281 fi
Steve Kondikd6fba552012-12-27 15:28:34 -0800282 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
283 PROMPT_COMMAND=$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')
284 fi
285
286 if [ -z "$apps" ]; then
287 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
288 else
289 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
290 fi
291 export ANDROID_PROMPT_PREFIX
292
293 # Inject build data into hardstatus
294 export PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/\\033]0;\(.*\)\\007/\\033]0;$ANDROID_PROMPT_PREFIX \1\\007/g')"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800295 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700296}
297
Kenny Root52aa81c2011-07-15 11:07:06 -0700298function addcompletions()
299{
300 local T dir f
301
302 # Keep us from trying to run in something that isn't bash.
303 if [ -z "${BASH_VERSION}" ]; then
304 return
305 fi
306
307 # Keep us from trying to run in bash that's too old.
308 if [ ${BASH_VERSINFO[0]} -lt 3 ]; then
309 return
310 fi
311
Chirayu Desai20a61372013-03-16 20:00:17 +0530312 dirs="sdk/bash_completion vendor/cm/bash_completion"
313 for dir in $dirs; do
Kenny Root52aa81c2011-07-15 11:07:06 -0700314 if [ -d ${dir} ]; then
Kenny Root7546d612011-07-18 13:11:34 -0700315 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
Kenny Root52aa81c2011-07-15 11:07:06 -0700316 echo "including $f"
317 . $f
318 done
319 fi
Chirayu Desai20a61372013-03-16 20:00:17 +0530320 done
Kenny Root52aa81c2011-07-15 11:07:06 -0700321}
322
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700323function choosetype()
324{
325 echo "Build type choices are:"
326 echo " 1. release"
327 echo " 2. debug"
328 echo
329
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800330 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700331 DEFAULT_NUM=1
332 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800334 export TARGET_BUILD_TYPE=
335 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700336 while [ -z $TARGET_BUILD_TYPE ]
337 do
338 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800339 if [ -z "$1" ] ; then
340 read ANSWER
341 else
342 echo $1
343 ANSWER=$1
344 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700345 case $ANSWER in
346 "")
347 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
348 ;;
349 1)
350 export TARGET_BUILD_TYPE=release
351 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800352 release)
353 export TARGET_BUILD_TYPE=release
354 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700355 2)
356 export TARGET_BUILD_TYPE=debug
357 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800358 debug)
359 export TARGET_BUILD_TYPE=debug
360 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700361 *)
362 echo
363 echo "I didn't understand your response. Please try again."
364 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700365 ;;
366 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800367 if [ -n "$1" ] ; then
368 break
369 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700370 done
371
372 set_stuff_for_environment
373}
374
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800375#
376# This function isn't really right: It chooses a TARGET_PRODUCT
377# based on the list of boards. Usually, that gets you something
378# that kinda works with a generic product, but really, you should
379# pick a product by name.
380#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700381function chooseproduct()
382{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700383 if [ "x$TARGET_PRODUCT" != x ] ; then
384 default_value=$TARGET_PRODUCT
385 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700386 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700387 fi
388
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 export TARGET_PRODUCT=
390 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700391 while [ -z "$TARGET_PRODUCT" ]
392 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700393 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800394 if [ -z "$1" ] ; then
395 read ANSWER
396 else
397 echo $1
398 ANSWER=$1
399 fi
400
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700401 if [ -z "$ANSWER" ] ; then
402 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800403 else
404 if check_product $ANSWER
405 then
406 export TARGET_PRODUCT=$ANSWER
407 else
408 echo "** Not a valid product: $ANSWER"
409 fi
410 fi
411 if [ -n "$1" ] ; then
412 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700413 fi
414 done
415
416 set_stuff_for_environment
417}
418
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800419function choosevariant()
420{
421 echo "Variant choices are:"
422 local index=1
423 local v
424 for v in ${VARIANT_CHOICES[@]}
425 do
426 # The product name is the name of the directory containing
427 # the makefile we found, above.
428 echo " $index. $v"
429 index=$(($index+1))
430 done
431
432 local default_value=eng
433 local ANSWER
434
435 export TARGET_BUILD_VARIANT=
436 while [ -z "$TARGET_BUILD_VARIANT" ]
437 do
438 echo -n "Which would you like? [$default_value] "
439 if [ -z "$1" ] ; then
440 read ANSWER
441 else
442 echo $1
443 ANSWER=$1
444 fi
445
446 if [ -z "$ANSWER" ] ; then
447 export TARGET_BUILD_VARIANT=$default_value
448 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
449 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800450 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800451 fi
452 else
453 if check_variant $ANSWER
454 then
455 export TARGET_BUILD_VARIANT=$ANSWER
456 else
457 echo "** Not a valid variant: $ANSWER"
458 fi
459 fi
460 if [ -n "$1" ] ; then
461 break
462 fi
463 done
464}
465
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466function choosecombo()
467{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700468 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700469
470 echo
471 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700472 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700473
474 echo
475 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700476 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800477
478 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700479 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481}
482
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800483# Clear this variable. It will be built up again when the vendorsetup.sh
484# files are included at the end of this file.
485unset LUNCH_MENU_CHOICES
486function add_lunch_combo()
487{
488 local new_combo=$1
489 local c
490 for c in ${LUNCH_MENU_CHOICES[@]} ; do
491 if [ "$new_combo" = "$c" ] ; then
492 return
493 fi
494 done
495 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
496}
497
498# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700499add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800500add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000501add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800502add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000503add_lunch_combo aosp_x86-eng
504add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800505
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506function print_lunch_menu()
507{
508 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 echo
510 echo "You're building on" $uname
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000511 if [ "$(uname)" = "Darwin" ] ; then
512 echo " (ohai, koush!)"
513 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700514 echo
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000515 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
516 echo "Breakfast menu... pick a combo:"
517 else
518 echo "Lunch menu... pick a combo:"
519 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800520
521 local i=1
522 local choice
523 for choice in ${LUNCH_MENU_CHOICES[@]}
524 do
525 echo " $i. $choice"
526 i=$(($i+1))
527 done
528
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000529 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
530 echo "... and don't forget the bacon!"
531 fi
532
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700533 echo
534}
535
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000536function brunch()
537{
538 breakfast $*
539 if [ $? -eq 0 ]; then
540 mka bacon
541 else
542 echo "No such item in brunch menu. Try 'breakfast'"
543 return 1
544 fi
545 return $?
546}
547
548function breakfast()
549{
550 target=$1
551 CM_DEVICES_ONLY="true"
552 unset LUNCH_MENU_CHOICES
553 add_lunch_combo full-eng
554 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
555 do
556 echo "including $f"
557 . $f
558 done
559 unset f
560
561 if [ $# -eq 0 ]; then
562 # No arguments, so let's have the full menu
563 lunch
564 else
565 echo "z$target" | grep -q "-"
566 if [ $? -eq 0 ]; then
567 # A buildtype was specified, assume a full device name
568 lunch $target
569 else
570 # This is probably just the CM model name
571 lunch cm_$target-userdebug
572 fi
573 fi
574 return $?
575}
576
577alias bib=breakfast
578
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579function lunch()
580{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581 local answer
582
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585 else
586 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700587 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800588 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589 fi
590
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800591 local selection=
592
593 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700595 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
597 then
598 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
599 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800600 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800601 fi
602 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
603 then
604 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 fi
606
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800607 if [ -z "$selection" ]
608 then
609 echo
610 echo "Invalid lunch combo: $answer"
611 return 1
612 fi
613
Joe Onoratoda12daf2010-06-09 18:18:31 -0700614 export TARGET_BUILD_APPS=
615
Jeff Browne33ba4c2011-07-11 22:11:46 -0700616 local product=$(echo -n $selection | sed -e "s/-.*$//")
617 check_product $product
618 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 then
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800620 # if we can't find a product, try to grab it off the CM github
621 T=$(gettop)
622 pushd $T > /dev/null
623 build/tools/roomservice.py $product
624 popd > /dev/null
625 check_product $product
Diogo Ferreira0d109172012-03-18 21:18:29 +0000626 else
627 build/tools/roomservice.py $product true
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800628 fi
629 if [ $? -ne 0 ]
630 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700631 echo
632 echo "** Don't have a product spec for: '$product'"
633 echo "** Do you have the right repo manifest?"
634 product=
635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Jeff Browne33ba4c2011-07-11 22:11:46 -0700637 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
638 check_variant $variant
639 if [ $? -ne 0 ]
640 then
641 echo
642 echo "** Invalid variant: '$variant'"
643 echo "** Must be one of ${VARIANT_CHOICES[@]}"
644 variant=
645 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646
Jeff Browne33ba4c2011-07-11 22:11:46 -0700647 if [ -z "$product" -o -z "$variant" ]
648 then
649 echo
650 return 1
651 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800652
Jeff Browne33ba4c2011-07-11 22:11:46 -0700653 export TARGET_PRODUCT=$product
654 export TARGET_BUILD_VARIANT=$variant
655 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700656
657 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800658
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700659 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800660 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700661}
662
Jeff Davidson513d7a42010-08-02 10:00:44 -0700663# Tab completion for lunch.
664function _lunch()
665{
666 local cur prev opts
667 COMPREPLY=()
668 cur="${COMP_WORDS[COMP_CWORD]}"
669 prev="${COMP_WORDS[COMP_CWORD-1]}"
670
671 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
672 return 0
673}
674complete -F _lunch lunch
675
Joe Onoratoda12daf2010-06-09 18:18:31 -0700676# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700677# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700678function tapas()
679{
Ying Wangb541ab62014-05-29 17:57:40 -0700680 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700681 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700682 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
683 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700684
Ying Wang67f02922012-08-22 10:25:20 -0700685 if [ $(echo $arch | wc -w) -gt 1 ]; then
686 echo "tapas: Error: Multiple build archs supplied: $arch"
687 return
688 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700689 if [ $(echo $variant | wc -w) -gt 1 ]; then
690 echo "tapas: Error: Multiple build variants supplied: $variant"
691 return
692 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700693 if [ $(echo $density | wc -w) -gt 1 ]; then
694 echo "tapas: Error: Multiple densities supplied: $density"
695 return
696 fi
Ying Wang67f02922012-08-22 10:25:20 -0700697
698 local product=full
699 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700700 x86) product=full_x86;;
701 mips) product=full_mips;;
702 armv5) product=generic_armv5;;
703 arm64) product=aosp_arm64;;
704 x86_64) product=aosp_x86_64;;
705 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700706 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700707 if [ -z "$variant" ]; then
708 variant=eng
709 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700710 if [ -z "$apps" ]; then
711 apps=all
712 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600713 if [ -z "$density" ]; then
714 density=alldpi
715 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700716
Ying Wang67f02922012-08-22 10:25:20 -0700717 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700718 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700719 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700720 export TARGET_BUILD_TYPE=release
721 export TARGET_BUILD_APPS=$apps
722
723 set_stuff_for_environment
724 printconfig
725}
726
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100727function eat()
728{
729 if [ "$OUT" ] ; then
Chirayu Desai80a277d2013-05-04 17:59:18 +0530730 MODVERSION=$(get_build_var CM_VERSION)
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100731 ZIPFILE=cm-$MODVERSION.zip
732 ZIPPATH=$OUT/$ZIPFILE
733 if [ ! -f $ZIPPATH ] ; then
734 echo "Nothing to eat"
735 return 1
736 fi
737 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
738 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
739 echo "No device is online. Waiting for one..."
740 echo "Please connect USB and/or enable USB debugging"
741 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
742 sleep 1
743 done
744 echo "Device Found.."
745 fi
Chirayu Desai6dadb572012-12-26 10:53:58 +0530746 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
747 then
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100748 # if adbd isn't root we can't write to /cache/recovery/
749 adb root
750 sleep 1
751 adb wait-for-device
Steve Kondik464574f2013-04-13 07:19:52 -0700752 cat << EOF > /tmp/command
753--sideload
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100754EOF
Steve Kondik464574f2013-04-13 07:19:52 -0700755 if adb push /tmp/command /cache/recovery/ ; then
756 echo "Rebooting into recovery for sideload installation"
757 adb reboot recovery
758 adb wait-for-sideload
759 adb sideload $ZIPPATH
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100760 fi
Steve Kondik464574f2013-04-13 07:19:52 -0700761 rm /tmp/command
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100762 else
763 echo "Nothing to eat"
764 return 1
765 fi
766 return $?
Chirayu Desai6dadb572012-12-26 10:53:58 +0530767 else
768 echo "The connected device does not appear to be $CM_BUILD, run away!"
769 fi
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100770}
771
Nebojsa Cvetkovicd5c0c872012-11-09 23:02:54 +0000772function omnom
773{
774 brunch $*
775 eat
776}
777
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700778function gettop
779{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800780 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700781 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700782 # The following circumlocution ensures we remove symlinks from TOP.
783 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700784 else
785 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800786 # The following circumlocution (repeated below as well) ensures
787 # that we record the true directory name and not one that is
788 # faked up with symlink names.
789 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800791 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700792 T=
793 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800794 \cd ..
synergyb112a402013-07-05 19:47:47 -0700795 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700796 done
Ying Wang9cd17642012-12-13 10:52:07 -0800797 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 if [ -f "$T/$TOPFILE" ]; then
799 echo $T
800 fi
801 fi
802 fi
803}
804
Andrew Hsieh906cb782013-09-10 17:37:14 +0800805# Return driver for "make", if any (eg. static analyzer)
806function getdriver()
807{
808 local T="$1"
809 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
810 if [ -n "$WITH_STATIC_ANALYZER" ]; then
811 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800812$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
813--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800814--status-bugs \
815--top=$T"
816 fi
817}
818
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700819function m()
820{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800821 local T=$(gettop)
822 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700823 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800824 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700825 else
826 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700827 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700828 fi
829}
830
831function findmakefile()
832{
833 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800834 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700835 T=
836 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700837 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700838 if [ -f "$T/Android.mk" ]; then
839 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800840 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700841 return
842 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800843 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700844 done
Ying Wang9cd17642012-12-13 10:52:07 -0800845 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700846}
847
848function mm()
849{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800850 local T=$(gettop)
851 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700852 # If we're sitting in the root of the build tree, just do a
853 # normal make.
854 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800855 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700856 else
857 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800858 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700859 local MODULES=
860 local GET_INSTALL_PATH=
861 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700862 # Remove the path to top as the makefilepath needs to be relative
863 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700864 if [ ! "$T" ]; then
865 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700866 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700867 elif [ ! "$M" ]; then
868 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700869 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700870 else
Ying Wanga7deb082013-08-16 13:24:47 -0700871 for ARG in $@; do
872 case $ARG in
873 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
874 esac
875 done
876 if [ -n "$GET_INSTALL_PATH" ]; then
877 MODULES=
878 ARGS=GET-INSTALL-PATH
879 else
880 MODULES=all_modules
881 ARGS=$@
882 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700883 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700884 fi
885 fi
886}
887
888function mmm()
889{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800890 local T=$(gettop)
891 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700892 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800893 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800894 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800895 local ARGS=
896 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700897 local GET_INSTALL_PATH=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800898 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
899 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
900 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800901 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
902 if [ "$MODULES" = "" ]; then
903 MODULES=all_modules
904 fi
905 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700906 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700907 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
908 local TO_CHOP=`expr $TO_CHOP + 1`
909 local START=`PWD= /bin/pwd`
910 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700911 if [ "$MFILE" = "" ] ; then
912 MFILE=$DIR/Android.mk
913 else
914 MFILE=$MFILE/$DIR/Android.mk
915 fi
916 MAKEFILE="$MAKEFILE $MFILE"
917 else
Ying Wanga7deb082013-08-16 13:24:47 -0700918 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100919 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700920 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
921 *) echo "No Android.mk in $DIR."; return 1;;
922 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700923 fi
924 done
Ying Wanga7deb082013-08-16 13:24:47 -0700925 if [ -n "$GET_INSTALL_PATH" ]; then
926 ARGS=$GET_INSTALL_PATH
927 MODULES=
928 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800929 ONE_SHOT_MAKEFILE="$MAKEFILE" $DRV make -C $T -f build/core/main.mk $DASH_ARGS $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700930 else
931 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700932 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700933 fi
934}
935
Ying Wangb607f7b2013-02-08 18:01:04 -0800936function mma()
937{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800938 local T=$(gettop)
939 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800940 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800941 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800942 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800943 if [ ! "$T" ]; then
944 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700945 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800946 fi
947 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800948 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800949 fi
950}
951
952function mmma()
953{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800954 local T=$(gettop)
955 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800956 if [ "$T" ]; then
957 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
958 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
959 local MY_PWD=`PWD= /bin/pwd`
960 if [ "$MY_PWD" = "$T" ]; then
961 MY_PWD=
962 else
963 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
964 fi
965 local DIR=
966 local MODULE_PATHS=
967 local ARGS=
968 for DIR in $DIRS ; do
969 if [ -d $DIR ]; then
970 if [ "$MY_PWD" = "" ]; then
971 MODULE_PATHS="$MODULE_PATHS $DIR"
972 else
973 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
974 fi
975 else
976 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100977 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800978 *) echo "Couldn't find directory $DIR"; return 1;;
979 esac
980 fi
981 done
Andrew Hsieh906cb782013-09-10 17:37:14 +0800982 $DRV make -C $T -f build/core/main.mk $DASH_ARGS $ARGS all_modules BUILD_MODULES_IN_PATHS="$MODULE_PATHS"
Ying Wangb607f7b2013-02-08 18:01:04 -0800983 else
984 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700985 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800986 fi
987}
988
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700989function croot()
990{
991 T=$(gettop)
992 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800993 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700994 else
995 echo "Couldn't locate the top of the tree. Try setting TOP."
996 fi
997}
998
nebkatfb67a1e2012-12-28 10:40:45 +0000999function cout()
1000{
1001 if [ "$OUT" ]; then
1002 cd $OUT
1003 else
1004 echo "Couldn't locate out directory. Try setting OUT."
1005 fi
1006}
1007
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001008function cproj()
1009{
1010 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001011 local HERE=$PWD
1012 T=
1013 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1014 T=$PWD
1015 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001016 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001017 return
1018 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001019 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001020 done
Ying Wang9cd17642012-12-13 10:52:07 -08001021 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001022 echo "can't find Android.mk"
1023}
1024
Daniel Sandler47e0a882013-07-30 13:23:52 -04001025# simplified version of ps; output in the form
1026# <pid> <procname>
1027function qpid() {
1028 local prepend=''
1029 local append=''
1030 if [ "$1" = "--exact" ]; then
1031 prepend=' '
1032 append='$'
1033 shift
1034 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1035 echo "usage: qpid [[--exact] <process name|pid>"
1036 return 255
1037 fi
1038
1039 local EXE="$1"
1040 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001041 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001042 else
1043 adb shell ps \
1044 | tr -d '\r' \
1045 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1046 fi
1047}
1048
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001049function pid()
1050{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001051 local prepend=''
1052 local append=''
1053 if [ "$1" = "--exact" ]; then
1054 prepend=' '
1055 append='$'
1056 shift
1057 fi
1058 local EXE="$1"
1059 if [ "$EXE" ] ; then
1060 local PID=`adb shell ps \
1061 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001062 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001063 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1064 echo "$PID"
1065 else
1066 echo "usage: pid [--exact] <process name>"
1067 return 255
1068 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001069}
1070
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001071# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001072# that has the core-file-size limit set correctly
1073#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001074# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001075# if its core-file-size limit is not set already.
1076# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1077
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001078function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001079{
1080 echo "Getting root...";
1081 adb root;
1082 adb wait-for-device;
1083
1084 echo "Remounting root parition read-write...";
1085 adb shell mount -w -o remount -t rootfs rootfs;
1086 sleep 1;
1087 adb wait-for-device;
1088 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001089 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001090 adb shell chmod 0777 /cores;
1091
1092 echo "Granting SELinux permission to dump in /cores...";
1093 adb shell restorecon -R /cores;
1094
1095 echo "Set core pattern.";
1096 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1097
1098 echo "Done."
1099}
1100
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001101# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001102# $1 = PID of process (e.g., $(pid mediaserver))
1103#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001104# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001105# dump to actually be generated.
1106
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001107function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001108{
1109 local PID=$1;
1110 if [ -z "$PID" ]; then
1111 printf "Expecting a PID!\n";
1112 return;
1113 fi;
1114 echo "Setting core limit for $PID to infinite...";
1115 adb shell prlimit $PID 4 -1 -1
1116}
1117
1118# core - send SIGV and pull the core for process
1119# $1 = PID of process (e.g., $(pid mediaserver))
1120#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001121# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001122# enabled globally.
1123
1124function core()
1125{
1126 local PID=$1;
1127
1128 if [ -z "$PID" ]; then
1129 printf "Expecting a PID!\n";
1130 return;
1131 fi;
1132
1133 local CORENAME=core.$PID;
1134 local COREPATH=/cores/$CORENAME;
1135 local SIG=SEGV;
1136
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001137 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001138
1139 local done=0;
1140 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1141 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1142 adb shell kill -$SIG $PID;
1143 sleep 1;
1144 done;
1145
1146 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1147 echo "Done: core is under $COREPATH on device.";
1148}
1149
Christopher Tate744ee802009-11-12 15:33:08 -08001150# systemstack - dump the current stack trace of all threads in the system process
1151# to the usual ANR traces file
1152function systemstack()
1153{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001154 stacks system_server
1155}
1156
1157function stacks()
1158{
1159 if [[ $1 =~ ^[0-9]+$ ]] ; then
1160 local PID="$1"
1161 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001162 local PIDLIST="$(pid $1)"
1163 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1164 local PID="$PIDLIST"
1165 elif [ "$PIDLIST" ] ; then
1166 echo "more than one process: $1"
1167 else
1168 echo "no such process: $1"
1169 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001170 else
1171 echo "usage: stacks [pid|process name]"
1172 fi
1173
1174 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001175 # Determine whether the process is native
1176 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1177 # Dump stacks of Dalvik process
1178 local TRACES=/data/anr/traces.txt
1179 local ORIG=/data/anr/traces.orig
1180 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001181
Jeff Brownb12c2e52013-08-19 15:14:16 -07001182 # Keep original traces to avoid clobbering
1183 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001184
Jeff Brownb12c2e52013-08-19 15:14:16 -07001185 # Make sure we have a usable file
1186 adb shell touch $TRACES
1187 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001188
Jeff Brownb12c2e52013-08-19 15:14:16 -07001189 # Dump stacks and wait for dump to finish
1190 adb shell kill -3 $PID
1191 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001192
Jeff Brownb12c2e52013-08-19 15:14:16 -07001193 # Restore original stacks, and show current output
1194 adb shell mv $TRACES $TMP
1195 adb shell mv $ORIG $TRACES
1196 adb shell cat $TMP
1197 else
1198 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001199 local USE64BIT="$(is64bit $PID)"
1200 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001201 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001202 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001203}
1204
Michael Wrightaeed7212014-06-19 19:58:12 -07001205# Read the ELF header from /proc/$PID/exe to determine if the process is
1206# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001207function is64bit()
1208{
1209 local PID="$1"
1210 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001211 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001212 echo "64"
1213 else
1214 echo ""
1215 fi
1216 else
1217 echo ""
1218 fi
1219}
1220
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001221case `uname -s` in
1222 Darwin)
1223 function sgrep()
1224 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001225 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|S|java|xml|sh|mk|aidl)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001226 }
1227
1228 ;;
1229 *)
1230 function sgrep()
1231 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001232 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|S\|java\|xml\|sh\|mk\|aidl\)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001233 }
1234 ;;
1235esac
1236
Raghu Gandham8da43102012-07-25 19:57:22 -07001237function gettargetarch
1238{
1239 get_build_var TARGET_ARCH
1240}
1241
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001242function ggrep()
1243{
1244 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1245}
1246
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001247function jgrep()
1248{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001249 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001250}
1251
1252function cgrep()
1253{
Dan Albert01961192014-11-22 10:16:01 -08001254 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001255}
1256
1257function resgrep()
1258{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001259 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"; done;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001260}
1261
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001262function mangrep()
1263{
1264 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1265}
1266
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001267function sepgrep()
1268{
1269 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d -print0 | xargs -0 grep --color -n -r --exclude-dir=\.git "$@"
1270}
1271
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001272function rcgrep()
1273{
1274 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" -print0 | xargs -0 grep --color -n "$@"
1275}
1276
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001277case `uname -s` in
1278 Darwin)
1279 function mgrep()
1280 {
Ying Wang324c2b22012-08-17 15:03:20 -07001281 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001282 }
1283
1284 function treegrep()
1285 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001286 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|S|java|xml)' -print0 | xargs -0 grep --color -n -i "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001287 }
1288
1289 ;;
1290 *)
1291 function mgrep()
1292 {
Ying Wang324c2b22012-08-17 15:03:20 -07001293 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -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 -07001294 }
1295
1296 function treegrep()
1297 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001298 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|S|java|xml)' -type f -print0 | xargs -0 grep --color -n -i "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001299 }
1300
1301 ;;
1302esac
1303
1304function getprebuilt
1305{
1306 get_abs_build_var ANDROID_PREBUILTS
1307}
1308
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001309function tracedmdump()
1310{
1311 T=$(gettop)
1312 if [ ! "$T" ]; then
1313 echo "Couldn't locate the top of the tree. Try setting TOP."
1314 return
1315 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001316 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001317 local arch=$(gettargetarch)
1318 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001319
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001320 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001321 if [ ! "$TRACE" ] ; then
1322 echo "usage: tracedmdump tracename"
1323 return
1324 fi
1325
Jack Veenstra60116fc2009-04-09 18:12:34 -07001326 if [ ! -r "$KERNEL" ] ; then
1327 echo "Error: cannot find kernel: '$KERNEL'"
1328 return
1329 fi
1330
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001331 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001332 if [ "$BASETRACE" = "$TRACE" ] ; then
1333 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1334 fi
1335
1336 echo "post-processing traces..."
1337 rm -f $TRACE/qtrace.dexlist
1338 post_trace $TRACE
1339 if [ $? -ne 0 ]; then
1340 echo "***"
1341 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1342 echo "***"
1343 return
1344 fi
1345 echo "generating dexlist output..."
1346 /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
1347 echo "generating dmtrace data..."
1348 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1349 echo "generating html file..."
1350 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1351 echo "done, see $TRACE/dmtrace.html for details"
1352 echo "or run:"
1353 echo " traceview $TRACE/dmtrace"
1354}
1355
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001356# communicate with a running device or emulator, set up necessary state,
1357# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001358function runhat()
1359{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001360 # process standard adb options
1361 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001362 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001363 adbTarget=$1
1364 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001365 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001366 adbTarget="$1 $2"
1367 shift 2
1368 fi
1369 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001370 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001371
1372 # runhat options
1373 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001374
1375 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001376 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001377 return
1378 fi
1379
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001380 # confirm hat is available
1381 if [ -z $(which hat) ]; then
1382 echo "hat is not available in this configuration."
1383 return
1384 fi
1385
Andy McFaddenb6289852010-07-12 08:00:19 -07001386 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001387 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001388 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001389 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001390 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001391 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001392 echo -n "> "
1393 read
1394
The Android Open Source Project88b60792009-03-03 19:28:42 -08001395 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001396
The Android Open Source Project88b60792009-03-03 19:28:42 -08001397 echo "Retrieving file $devFile..."
1398 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001399
The Android Open Source Project88b60792009-03-03 19:28:42 -08001400 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001401
The Android Open Source Project88b60792009-03-03 19:28:42 -08001402 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001403 echo "View the output by pointing your browser at http://localhost:7000/"
1404 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001405 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001406}
1407
1408function getbugreports()
1409{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001410 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001411
1412 if [ ! "$reports" ]; then
1413 echo "Could not locate any bugreports."
1414 return
1415 fi
1416
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001417 local report
1418 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001420 echo "/sdcard/bugreports/${report}"
1421 adb pull /sdcard/bugreports/${report} ${report}
1422 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001423 done
1424}
1425
Victoria Lease1b296b42012-08-21 15:44:06 -07001426function getsdcardpath()
1427{
1428 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1429}
1430
1431function getscreenshotpath()
1432{
1433 echo "$(getsdcardpath)/Pictures/Screenshots"
1434}
1435
1436function getlastscreenshot()
1437{
1438 local screenshot_path=$(getscreenshotpath)
1439 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1440 if [ "$screenshot" = "" ]; then
1441 echo "No screenshots found."
1442 return
1443 fi
1444 echo "${screenshot}"
1445 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1446}
1447
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001448function startviewserver()
1449{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001450 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001451 if [ $# -gt 0 ]; then
1452 port=$1
1453 fi
1454 adb shell service call window 1 i32 $port
1455}
1456
1457function stopviewserver()
1458{
1459 adb shell service call window 2
1460}
1461
1462function isviewserverstarted()
1463{
1464 adb shell service call window 3
1465}
1466
Romain Guyb84049a2010-10-04 16:56:11 -07001467function key_home()
1468{
1469 adb shell input keyevent 3
1470}
1471
1472function key_back()
1473{
1474 adb shell input keyevent 4
1475}
1476
1477function key_menu()
1478{
1479 adb shell input keyevent 82
1480}
1481
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001482function smoketest()
1483{
1484 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1485 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1486 return
1487 fi
1488 T=$(gettop)
1489 if [ ! "$T" ]; then
1490 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1491 return
1492 fi
1493
Ying Wang9cd17642012-12-13 10:52:07 -08001494 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001495 adb uninstall com.android.smoketest > /dev/null &&
1496 adb uninstall com.android.smoketest.tests > /dev/null &&
1497 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1498 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1499 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1500}
1501
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001502# simple shortcut to the runtest command
1503function runtest()
1504{
1505 T=$(gettop)
1506 if [ ! "$T" ]; then
1507 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1508 return
1509 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001510 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001511}
1512
The Android Open Source Project88b60792009-03-03 19:28:42 -08001513function godir () {
1514 if [[ -z "$1" ]]; then
1515 echo "Usage: godir <regex>"
1516 return
1517 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001518 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001519 if [[ ! -f $T/filelist ]]; then
1520 echo -n "Creating index..."
Ying Wang9cd17642012-12-13 10:52:07 -08001521 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001522 echo " Done"
1523 echo ""
1524 fi
1525 local lines
Jeff Hamilton293f9392011-11-18 17:15:25 -06001526 lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001527 if [[ ${#lines[@]} = 0 ]]; then
1528 echo "Not found"
1529 return
1530 fi
1531 local pathname
1532 local choice
1533 if [[ ${#lines[@]} > 1 ]]; then
1534 while [[ -z "$pathname" ]]; do
1535 local index=1
1536 local line
1537 for line in ${lines[@]}; do
1538 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001539 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001540 done
1541 echo
1542 echo -n "Select one: "
1543 unset choice
1544 read choice
1545 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1546 echo "Invalid choice"
1547 continue
1548 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001549 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001550 done
1551 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001552 pathname=${lines[0]}
1553 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001554 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001555}
1556
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001557function cmremote()
1558{
1559 git remote rm cmremote 2> /dev/null
1560 if [ ! -d .git ]
1561 then
1562 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1563 fi
1564 GERRIT_REMOTE=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
1565 if [ -z "$GERRIT_REMOTE" ]
1566 then
Koushik Duttab55f13a2012-05-14 16:14:36 -07001567 GERRIT_REMOTE=$(cat .git/config | grep http://github.com | awk '{ print $NF }' | sed s#http://github.com/##g)
1568 if [ -z "$GERRIT_REMOTE" ]
1569 then
1570 echo Unable to set up the git remote, are you in the root of the repo?
1571 return 0
1572 fi
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001573 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001574 CMUSER=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001575 if [ -z "$CMUSER" ]
1576 then
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001577 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001578 else
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001579 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001580 fi
1581 echo You can now push to "cmremote".
1582}
Koushik Duttab55f13a2012-05-14 16:14:36 -07001583export -f cmremote
1584
Steve Kondik873fa562012-09-17 11:33:18 -07001585function aospremote()
1586{
1587 git remote rm aosp 2> /dev/null
1588 if [ ! -d .git ]
1589 then
1590 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1591 fi
1592 PROJECT=`pwd | sed s#$ANDROID_BUILD_TOP/##g`
1593 if (echo $PROJECT | grep -qv "^device")
1594 then
1595 PFX="platform/"
1596 fi
1597 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1598 echo "Remote 'aosp' created"
1599}
1600export -f aospremote
1601
Steve Kondikf00e7d92012-09-23 23:46:55 -07001602function installboot()
1603{
1604 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1605 then
1606 echo "No recovery.fstab found. Build recovery first."
1607 return 1
1608 fi
1609 if [ ! -e "$OUT/boot.img" ];
1610 then
1611 echo "No boot.img found. Run make bootimage first."
1612 return 1
1613 fi
1614 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1615 if [ -z "$PARTITION" ];
1616 then
1617 echo "Unable to determine boot partition."
1618 return 1
1619 fi
1620 adb start-server
1621 adb root
1622 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001623 adb wait-for-online shell mount /system 2>&1 > /dev/null
1624 adb wait-for-online remount
Steve Kondikf00e7d92012-09-23 23:46:55 -07001625 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
1626 then
1627 adb push $OUT/boot.img /cache/
1628 for i in $OUT/system/lib/modules/*;
1629 do
1630 adb push $i /system/lib/modules/
1631 done
1632 adb shell dd if=/cache/boot.img of=$PARTITION
1633 adb shell chmod 644 /system/lib/modules/*
1634 echo "Installation complete."
1635 else
1636 echo "The connected device does not appear to be $CM_BUILD, run away!"
1637 fi
1638}
1639
Steve Kondik83c03d52012-10-24 16:40:42 -07001640function installrecovery()
1641{
1642 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1643 then
1644 echo "No recovery.fstab found. Build recovery first."
1645 return 1
1646 fi
1647 if [ ! -e "$OUT/recovery.img" ];
1648 then
1649 echo "No recovery.img found. Run make recoveryimage first."
1650 return 1
1651 fi
1652 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1653 if [ -z "$PARTITION" ];
1654 then
1655 echo "Unable to determine recovery partition."
1656 return 1
1657 fi
1658 adb start-server
1659 adb root
1660 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001661 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1662 adb wait-for-online remount
Steve Kondik83c03d52012-10-24 16:40:42 -07001663 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
1664 then
1665 adb push $OUT/recovery.img /cache/
1666 adb shell dd if=/cache/recovery.img of=$PARTITION
1667 echo "Installation complete."
1668 else
1669 echo "The connected device does not appear to be $CM_BUILD, run away!"
1670 fi
1671}
Steve Kondikf00e7d92012-09-23 23:46:55 -07001672
Koushik Duttab55f13a2012-05-14 16:14:36 -07001673function makerecipe() {
1674 if [ -z "$1" ]
1675 then
1676 echo "No branch name provided."
1677 return 1
1678 fi
1679 cd android
1680 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1681 git commit -a -m "$1"
1682 cd ..
1683
1684 repo forall -c '
1685
1686 if [ "$REPO_REMOTE" == "github" ]
1687 then
1688 pwd
1689 cmremote
1690 git push cmremote HEAD:refs/heads/'$1'
1691 fi
1692 '
1693}
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001694
1695function cmgerrit() {
1696 if [ $# -eq 0 ]; then
1697 $FUNCNAME help
1698 return 1
1699 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001700 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001701 local review=`git config --get remote.github.review`
1702 local project=`git config --get remote.github.projectname`
1703 local command=$1
1704 shift
1705 case $command in
1706 help)
1707 if [ $# -eq 0 ]; then
1708 cat <<EOF
1709Usage:
1710 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1711
1712Commands:
1713 fetch Just fetch the change as FETCH_HEAD
1714 help Show this help, or for a specific command
1715 pull Pull a change into current branch
1716 push Push HEAD or a local branch to Gerrit for a specific branch
1717
1718Any other Git commands that support refname would work as:
1719 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1720
1721See '$FUNCNAME help COMMAND' for more information on a specific command.
1722
1723Example:
1724 $FUNCNAME checkout -b topic 1234/5
1725works as:
1726 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1727 && git checkout -b topic FETCH_HEAD
1728will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1729Patch-set 1 will be fetched if omitted.
1730EOF
1731 return
1732 fi
1733 case $1 in
1734 __cmg_*) echo "For internal use only." ;;
1735 changes|for)
1736 if [ "$FUNCNAME" = "cmgerrit" ]; then
1737 echo "'$FUNCNAME $1' is deprecated."
1738 fi
1739 ;;
1740 help) $FUNCNAME help ;;
1741 fetch|pull) cat <<EOF
1742usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1743
1744works as:
1745 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1746 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1747
1748Example:
1749 $FUNCNAME $1 1234
1750will $1 patch-set 1 of change 1234
1751EOF
1752 ;;
1753 push) cat <<EOF
1754usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1755
1756works as:
1757 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1758 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1759
1760Example:
1761 $FUNCNAME push fix6789:gingerbread
1762will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1763HEAD will be pushed from local if omitted.
1764EOF
1765 ;;
1766 *)
1767 $FUNCNAME __cmg_err_not_supported $1 && return
1768 cat <<EOF
1769usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1770
1771works as:
1772 git fetch http://DOMAIN/p/PROJECT \\
1773 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1774 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1775EOF
1776 ;;
1777 esac
1778 ;;
1779 __cmg_get_ref)
1780 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1781 local change_id patchset_id hash
1782 case $1 in
1783 */*)
1784 change_id=${1%%/*}
1785 patchset_id=${1#*/}
1786 ;;
1787 *)
1788 change_id=$1
1789 patchset_id=1
1790 ;;
1791 esac
1792 hash=$(($change_id % 100))
1793 case $hash in
1794 [0-9]) hash="0$hash" ;;
1795 esac
1796 echo "refs/changes/$hash/$change_id/$patchset_id"
1797 ;;
1798 fetch|pull)
1799 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1800 $FUNCNAME __cmg_err_not_repo && return 1
1801 local change=$1
1802 shift
1803 git $command $@ http://$review/p/$project \
1804 $($FUNCNAME __cmg_get_ref $change) || return 1
1805 ;;
1806 push)
1807 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1808 $FUNCNAME __cmg_err_not_repo && return 1
1809 if [ -z "$user" ]; then
1810 echo >&2 "Gerrit username not found."
1811 return 1
1812 fi
1813 local local_branch remote_branch
1814 case $1 in
1815 *:*)
1816 local_branch=${1%:*}
1817 remote_branch=${1##*:}
1818 ;;
1819 *)
1820 local_branch=HEAD
1821 remote_branch=$1
1822 ;;
1823 esac
1824 shift
1825 git push $@ ssh://$user@$review:29418/$project \
1826 $local_branch:refs/for/$remote_branch || return 1
1827 ;;
1828 changes|for)
1829 if [ "$FUNCNAME" = "cmgerrit" ]; then
1830 echo >&2 "'$FUNCNAME $command' is deprecated."
1831 fi
1832 ;;
1833 __cmg_err_no_arg)
1834 if [ $# -lt 2 ]; then
1835 echo >&2 "'$FUNCNAME $command' missing argument."
1836 elif [ $2 -eq 0 ]; then
1837 if [ -n "$3" ]; then
1838 $FUNCNAME help $1
1839 else
1840 echo >&2 "'$FUNCNAME $1' missing argument."
1841 fi
1842 else
1843 return 1
1844 fi
1845 ;;
1846 __cmg_err_not_repo)
1847 if [ -z "$review" -o -z "$project" ]; then
1848 echo >&2 "Not currently in any reviewable repository."
1849 else
1850 return 1
1851 fi
1852 ;;
1853 __cmg_err_not_supported)
1854 $FUNCNAME __cmg_err_no_arg $command $# && return
1855 case $1 in
1856 #TODO: filter more git commands that don't use refname
1857 init|add|rm|mv|status|clone|remote|bisect|config|stash)
1858 echo >&2 "'$FUNCNAME $1' is not supported."
1859 ;;
1860 *) return 1 ;;
1861 esac
1862 ;;
1863 #TODO: other special cases?
1864 *)
1865 $FUNCNAME __cmg_err_not_supported $command && return 1
1866 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1867 $FUNCNAME __cmg_err_not_repo && return 1
1868 local args="$@"
1869 local change pre_args refs_arg post_args
1870 case "$args" in
1871 *--\ *)
1872 pre_args=${args%%-- *}
1873 post_args="-- ${args#*-- }"
1874 ;;
1875 *) pre_args="$args" ;;
1876 esac
1877 args=($pre_args)
1878 pre_args=
1879 if [ ${#args[@]} -gt 0 ]; then
1880 change=${args[${#args[@]}-1]}
1881 fi
1882 if [ ${#args[@]} -gt 1 ]; then
1883 pre_args=${args[0]}
1884 for ((i=1; i<${#args[@]}-1; i++)); do
1885 pre_args="$pre_args ${args[$i]}"
1886 done
1887 fi
1888 while ((1)); do
1889 case $change in
1890 ""|--)
1891 $FUNCNAME help $command
1892 return 1
1893 ;;
1894 *@*)
1895 if [ -z "$refs_arg" ]; then
1896 refs_arg="@${change#*@}"
1897 change=${change%%@*}
1898 fi
1899 ;;
1900 *~*)
1901 if [ -z "$refs_arg" ]; then
1902 refs_arg="~${change#*~}"
1903 change=${change%%~*}
1904 fi
1905 ;;
1906 *^*)
1907 if [ -z "$refs_arg" ]; then
1908 refs_arg="^${change#*^}"
1909 change=${change%%^*}
1910 fi
1911 ;;
1912 *:*)
1913 if [ -z "$refs_arg" ]; then
1914 refs_arg=":${change#*:}"
1915 change=${change%%:*}
1916 fi
1917 ;;
1918 *) break ;;
1919 esac
1920 done
1921 $FUNCNAME fetch $change \
1922 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
1923 || return 1
1924 ;;
1925 esac
1926}
1927
1928function cmrebase() {
1929 local repo=$1
1930 local refs=$2
1931 local pwd="$(pwd)"
1932 local dir="$(gettop)/$repo"
1933
1934 if [ -z $repo ] || [ -z $refs ]; then
1935 echo "CyanogenMod Gerrit Rebase Usage: "
1936 echo " cmrebase <path to project> <patch IDs on Gerrit>"
1937 echo " The patch IDs appear on the Gerrit commands that are offered."
1938 echo " They consist on a series of numbers and slashes, after the text"
1939 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
1940 echo ""
1941 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
1942 echo ""
1943 return
1944 fi
1945
1946 if [ ! -d $dir ]; then
1947 echo "Directory $dir doesn't exist in tree."
1948 return
1949 fi
1950 cd $dir
1951 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
1952 echo "Starting branch..."
1953 repo start tmprebase .
1954 echo "Bringing it up to date..."
1955 repo sync .
1956 echo "Fetching change..."
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001957 git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001958 if [ "$?" != "0" ]; then
1959 echo "Error cherry-picking. Not uploading!"
1960 return
1961 fi
1962 echo "Uploading..."
1963 repo upload .
1964 echo "Cleaning up..."
1965 repo abandon tmprebase .
1966 cd $pwd
1967}
1968
1969function mka() {
1970 case `uname -s` in
1971 Darwin)
1972 make -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
1973 ;;
1974 *)
Arnav Gupta3b909942012-11-23 10:47:44 -07001975 schedtool -B -n 1 -e ionice -n 1 make -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001976 ;;
1977 esac
1978}
1979
1980function reposync() {
1981 case `uname -s` in
1982 Darwin)
1983 repo sync -j 4 "$@"
1984 ;;
1985 *)
Alan Orthef363cd2012-09-07 11:44:27 +03001986 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001987 ;;
1988 esac
1989}
Tanguy Pruvot2192fd22012-06-06 21:39:23 +02001990
1991function repodiff() {
1992 if [ -z "$*" ]; then
1993 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
1994 return
1995 fi
1996 diffopts=$* repo forall -c \
1997 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
1998}
1999
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302000# Credit for color strip sed: http://goo.gl/BoIcm
2001function dopush()
2002{
2003 local func=$1
2004 shift
2005
2006 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
2007 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
2008 echo "No device is online. Waiting for one..."
2009 echo "Please connect USB and/or enable USB debugging"
2010 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
2011 sleep 1
2012 done
2013 echo "Device Found."
2014 fi
2015
Chirayu Desai6dadb572012-12-26 10:53:58 +05302016 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
2017 then
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302018 adb root &> /dev/null
2019 sleep 0.3
2020 adb wait-for-device &> /dev/null
2021 sleep 0.3
2022 adb remount &> /dev/null
2023
2024 $func $* | tee $OUT/.log
2025
2026 # Install: <file>
2027 LOC=$(cat $OUT/.log | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | grep 'Install' | cut -d ':' -f 2)
2028
2029 # Copy: <file>
2030 LOC=$LOC $(cat $OUT/.log | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' | grep 'Copy' | cut -d ':' -f 2)
2031
2032 for FILE in $LOC; do
2033 # Get target file name (i.e. system/bin/adb)
2034 TARGET=$(echo $FILE | sed "s#$OUT/##")
2035
2036 # Don't send files that are not in /system.
2037 if ! echo $TARGET | egrep '^system\/' > /dev/null ; then
2038 continue
2039 else
2040 case $TARGET in
2041 system/app/SystemUI.apk|system/framework/*)
2042 stop_n_start=true
2043 ;;
2044 *)
2045 stop_n_start=false
2046 ;;
2047 esac
2048 if $stop_n_start ; then adb shell stop ; fi
2049 echo "Pushing: $TARGET"
2050 adb push $FILE $TARGET
2051 if $stop_n_start ; then adb shell start ; fi
2052 fi
2053 done
2054 rm -f $OUT/.log
2055 return 0
Chirayu Desai6dadb572012-12-26 10:53:58 +05302056 else
2057 echo "The connected device does not appear to be $CM_BUILD, run away!"
2058 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302059}
2060
2061alias mmp='dopush mm'
2062alias mmmp='dopush mmm'
2063alias mkap='dopush mka'
2064alias cmkap='dopush cmka'
2065
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002066# Force JAVA_HOME to point to java 1.7 or java 1.6 if it isn't already set.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002067#
2068# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
2069# For some reason, installing the JDK doesn't make it show up in the
2070# JavaVM.framework/Versions/1.7/ folder.
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002071function set_java_home() {
Narayan Kamath9260bba2014-01-17 10:05:25 +00002072 # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamathc84889b2014-04-01 14:16:26 +01002073 # we can reset it later, depending on the version of java the build
2074 # system needs.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002075 #
2076 # If we don't do this, the JAVA_HOME value set by the first call to
2077 # build/envsetup.sh will persist forever.
2078 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
2079 export JAVA_HOME=""
2080 fi
2081
Andy McFaddenbd960942010-06-24 12:52:51 -07002082 if [ ! "$JAVA_HOME" ]; then
Neil Fuller46e00ea2014-10-16 10:23:03 +01002083 case `uname -s` in
2084 Darwin)
2085 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2086 ;;
2087 *)
2088 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
2089 ;;
2090 esac
Narayan Kamath9260bba2014-01-17 10:05:25 +00002091
2092 # Keep track of the fact that we set JAVA_HOME ourselves, so that
2093 # we can change it on the next envsetup.sh, if required.
2094 export ANDROID_SET_JAVA_HOME=true
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002095 fi
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002096}
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002097
Alex Rayf0d08eb2013-03-08 15:15:06 -08002098# Print colored exit condition
2099function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08002100 "$@"
2101 local retval=$?
2102 if [ $retval -ne 0 ]
2103 then
2104 echo -e "\e[0;31mFAILURE\e[00m"
2105 else
2106 echo -e "\e[0;32mSUCCESS\e[00m"
2107 fi
2108 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08002109}
2110
Ying Wanged21d4c2014-08-24 22:14:19 -07002111function get_make_command()
2112{
2113 echo command make
2114}
2115
Ed Heylcc6be0a2014-06-18 14:55:58 -07002116function make()
2117{
2118 local start_time=$(date +"%s")
Ying Wanged21d4c2014-08-24 22:14:19 -07002119 $(get_make_command) "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002120 local ret=$?
2121 local end_time=$(date +"%s")
2122 local tdiff=$(($end_time-$start_time))
2123 local hours=$(($tdiff / 3600 ))
2124 local mins=$((($tdiff % 3600) / 60))
2125 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002126 local ncolors=$(tput colors 2>/dev/null)
2127 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
2128 color_failed="\e[0;31m"
2129 color_success="\e[0;32m"
2130 color_reset="\e[00m"
2131 else
2132 color_failed=""
2133 color_success=""
2134 color_reset=""
2135 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07002136 echo
2137 if [ $ret -eq 0 ] ; then
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002138 echo -n -e "${color_success}#### make completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002139 else
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002140 echo -n -e "${color_failed}#### make failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002141 fi
2142 if [ $hours -gt 0 ] ; then
2143 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
2144 elif [ $mins -gt 0 ] ; then
2145 printf "(%02g:%02g (mm:ss))" $mins $secs
2146 elif [ $secs -gt 0 ] ; then
2147 printf "(%s seconds)" $secs
2148 fi
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002149 echo -e " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002150 echo
2151 return $ret
2152}
2153
Raphael Moll70a86b02011-06-20 16:03:14 -07002154if [ "x$SHELL" != "x/bin/bash" ]; then
2155 case `ps -o command -p $$` in
2156 *bash*)
2157 ;;
2158 *)
2159 echo "WARNING: Only bash is supported, use of other shell would lead to erroneous results"
2160 ;;
2161 esac
2162fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002163
2164# Execute the contents of any vendorsetup.sh files we can find.
Oleksiy Avramchenko15760a82014-10-06 18:51:58 +02002165for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
2166 `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002167do
2168 echo "including $f"
2169 . $f
2170done
2171unset f
Kenny Root52aa81c2011-07-15 11:07:06 -07002172
2173addcompletions