blob: 2796340b21adb2e9968a1f5f9249664389c973c4 [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
Steve Kondik20c21d22013-10-27 13:34:36 -070028- cafremote: Add git remote for matching CodeAurora repository.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060029- mka: Builds using SCHED_BATCH on all processors
30- reposync: Parallel repo sync using ionice and SCHED_BATCH
Chirayu Desai4a319b82013-06-05 20:14:33 +053031- repopick: Utility to fetch changes from Gerrit.
Steve Kondik83c03d52012-10-24 16:40:42 -070032- installboot: Installs a boot.img to the connected device.
33- installrecovery: Installs a recovery.img to the connected device.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070034
Dan Albert4ae5d4b2014-10-31 16:23:08 -070035Environemnt options:
36- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
37 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
38 build is leak-check clean.
39
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070040Look at the source to view more functions. The complete list is:
41EOF
42 T=$(gettop)
43 local A
44 A=""
Jeff Brown46cbd202014-07-26 15:15:41 -070045 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 -070046 A="$A $i"
47 done
48 echo $A
49}
50
51# Get the value of a build variable as an absolute path.
52function get_abs_build_var()
53{
54 T=$(gettop)
55 if [ ! "$T" ]; then
56 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
57 return
58 fi
Ying Wang9cd17642012-12-13 10:52:07 -080059 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070060 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070061}
62
63# Get the exact value of a build variable.
64function get_build_var()
65{
66 T=$(gettop)
67 if [ ! "$T" ]; then
68 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
69 return
70 fi
Andrew Boie6905e212013-12-19 13:21:46 -080071 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070072 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080073}
74
75# check to see if the supplied product is one we can build
76function check_product()
77{
78 T=$(gettop)
79 if [ ! "$T" ]; then
80 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
81 return
82 fi
Ricardo Cerqueira119d3bb2011-11-25 15:30:36 +000083
84 if (echo -n $1 | grep -q -e "^cm_") ; then
85 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
Ricardo Cerqueira34ae3232013-09-26 00:10:20 +010086 export BUILD_NUMBER=$((date +%s%N ; echo $CM_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10)
Ricardo Cerqueira119d3bb2011-11-25 15:30:36 +000087 else
88 CM_BUILD=
89 fi
90 export CM_BUILD
91
Jeff Browne33ba4c2011-07-11 22:11:46 -070092 TARGET_PRODUCT=$1 \
93 TARGET_BUILD_VARIANT= \
94 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070095 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080096 get_build_var TARGET_DEVICE > /dev/null
97 # hide successful answers, but allow the errors to show
98}
99
100VARIANT_CHOICES=(user userdebug eng)
101
102# check to see if the supplied variant is valid
103function check_variant()
104{
105 for v in ${VARIANT_CHOICES[@]}
106 do
107 if [ "$v" = "$1" ]
108 then
109 return 0
110 fi
111 done
112 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700113}
114
115function setpaths()
116{
117 T=$(gettop)
118 if [ ! "$T" ]; then
119 echo "Couldn't locate the top of the tree. Try setting TOP."
120 return
121 fi
122
123 ##################################################################
124 # #
125 # Read me before you modify this code #
126 # #
127 # This function sets ANDROID_BUILD_PATHS to what it is adding #
128 # to PATH, and the next time it is run, it removes that from #
129 # PATH. This is required so lunch can be run more than once #
130 # and still have working paths. #
131 # #
132 ##################################################################
133
Raphael Mollc639c782011-06-20 17:25:01 -0700134 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
135 # due to "C:\Program Files" being in the path.
136
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700137 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700138 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700139 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
140 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700141 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700142 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800143 # strip leading ':', if any
144 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500145 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700146
147 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700148 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700149 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700150
Ben Cheng8bc4c432012-11-16 13:29:13 -0800151 # defined in core/config.mk
152 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700153 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800154 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800155
Raphael Mollc639c782011-06-20 17:25:01 -0700156 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800157 export ANDROID_TOOLCHAIN=
158 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200159 local ARCH=$(get_build_var TARGET_ARCH)
160 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400161 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700162 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400163 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
164 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800165 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200166 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800167 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700168 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700169 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700170 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000171 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200172 *)
173 echo "Can't find toolchain for unknown architecture: $ARCH"
174 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700175 ;;
176 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700177 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800178 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700179 fi
Raphael732936d2011-06-22 14:35:32 -0700180
Ben Chengfba67bf2014-02-25 10:27:07 -0800181 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
182 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
183 fi
184
185 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700186 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700187 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800188 # Legacy toolchain configuration used for ARM kernel compilation
Ben Cheng8bc4c432012-11-16 13:29:13 -0800189 toolchaindir=arm/arm-eabi-$targetgccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700190 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100191 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
192 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700193 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700194 ;;
195 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700196 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700197 ;;
198 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700199
Jeff Vander Stoep5aa68322015-06-12 09:56:39 -0700200 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Ying Wang2a859d52014-06-30 10:25:33 -0700201 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 +0200202
203 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
204 # to ensure that the corresponding 'emulator' binaries are used.
205 case $(uname -s) in
206 Darwin)
207 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
208 ;;
209 Linux)
210 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
211 ;;
212 *)
213 ANDROID_EMULATOR_PREBUILTS=
214 ;;
215 esac
216 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700217 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200218 export ANDROID_EMULATOR_PREBUILTS
219 fi
220
Ying Wangaa1c9b52012-11-26 20:51:59 -0800221 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800222
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500223 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900224 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500225 if [ -n "$JAVA_HOME" ]; then
226 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900227 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
228 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500229 fi
230
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800231 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700232 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
233 export OUT=$ANDROID_PRODUCT_OUT
234
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700235 unset ANDROID_HOST_OUT
236 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
237
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800238 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700239 # TODO: fix the path
240 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
241}
242
243function printconfig()
244{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800245 T=$(gettop)
246 if [ ! "$T" ]; then
247 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
248 return
249 fi
250 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700251}
252
253function set_stuff_for_environment()
254{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800255 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500256 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800257 setpaths
258 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700259
Ben Chengaac3f812013-08-13 14:38:15 -0700260 # With this environment variable new GCC can apply colors to warnings/errors
261 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 -0700262 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700263}
264
265function set_sequence_number()
266{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700267 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700268}
269
270function settitle()
271{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800272 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700273 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700274 local product=$TARGET_PRODUCT
275 local variant=$TARGET_BUILD_VARIANT
276 local apps=$TARGET_BUILD_APPS
Steve Kondikd6fba552012-12-27 15:28:34 -0800277 if [ -z "$PROMPT_COMMAND" ]; then
278 # No prompts
279 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
280 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
281 # Prompts exist, but no hardstatus
282 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700283 fi
Steve Kondikd6fba552012-12-27 15:28:34 -0800284 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
Christopher Laisfa8d9352013-06-03 15:15:39 -0500285 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
Steve Kondikd6fba552012-12-27 15:28:34 -0800286 fi
287
288 if [ -z "$apps" ]; then
289 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
290 else
291 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
292 fi
293 export ANDROID_PROMPT_PREFIX
294
295 # Inject build data into hardstatus
296 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 -0800297 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700298}
299
Kyle Ladd031a0b62013-09-11 20:43:42 -0400300function check_bash_version()
Kenny Root52aa81c2011-07-15 11:07:06 -0700301{
Kenny Root52aa81c2011-07-15 11:07:06 -0700302 # Keep us from trying to run in something that isn't bash.
303 if [ -z "${BASH_VERSION}" ]; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400304 return 1
Kenny Root52aa81c2011-07-15 11:07:06 -0700305 fi
306
307 # Keep us from trying to run in bash that's too old.
James Roberts-Thomson24dd07d2013-04-16 15:53:39 +1200308 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400309 return 2
Kenny Root52aa81c2011-07-15 11:07:06 -0700310 fi
311
Kyle Ladd031a0b62013-09-11 20:43:42 -0400312 return 0
Kenny Root52aa81c2011-07-15 11:07:06 -0700313}
314
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700315function choosetype()
316{
317 echo "Build type choices are:"
318 echo " 1. release"
319 echo " 2. debug"
320 echo
321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700323 DEFAULT_NUM=1
324 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 export TARGET_BUILD_TYPE=
327 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328 while [ -z $TARGET_BUILD_TYPE ]
329 do
330 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800331 if [ -z "$1" ] ; then
332 read ANSWER
333 else
334 echo $1
335 ANSWER=$1
336 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700337 case $ANSWER in
338 "")
339 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
340 ;;
341 1)
342 export TARGET_BUILD_TYPE=release
343 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800344 release)
345 export TARGET_BUILD_TYPE=release
346 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700347 2)
348 export TARGET_BUILD_TYPE=debug
349 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800350 debug)
351 export TARGET_BUILD_TYPE=debug
352 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700353 *)
354 echo
355 echo "I didn't understand your response. Please try again."
356 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700357 ;;
358 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800359 if [ -n "$1" ] ; then
360 break
361 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700362 done
363
364 set_stuff_for_environment
365}
366
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800367#
368# This function isn't really right: It chooses a TARGET_PRODUCT
369# based on the list of boards. Usually, that gets you something
370# that kinda works with a generic product, but really, you should
371# pick a product by name.
372#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700373function chooseproduct()
374{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700375 if [ "x$TARGET_PRODUCT" != x ] ; then
376 default_value=$TARGET_PRODUCT
377 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700378 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700379 fi
380
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800381 export TARGET_PRODUCT=
382 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700383 while [ -z "$TARGET_PRODUCT" ]
384 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700385 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800386 if [ -z "$1" ] ; then
387 read ANSWER
388 else
389 echo $1
390 ANSWER=$1
391 fi
392
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700393 if [ -z "$ANSWER" ] ; then
394 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800395 else
396 if check_product $ANSWER
397 then
398 export TARGET_PRODUCT=$ANSWER
399 else
400 echo "** Not a valid product: $ANSWER"
401 fi
402 fi
403 if [ -n "$1" ] ; then
404 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700405 fi
406 done
407
408 set_stuff_for_environment
409}
410
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800411function choosevariant()
412{
413 echo "Variant choices are:"
414 local index=1
415 local v
416 for v in ${VARIANT_CHOICES[@]}
417 do
418 # The product name is the name of the directory containing
419 # the makefile we found, above.
420 echo " $index. $v"
421 index=$(($index+1))
422 done
423
424 local default_value=eng
425 local ANSWER
426
427 export TARGET_BUILD_VARIANT=
428 while [ -z "$TARGET_BUILD_VARIANT" ]
429 do
430 echo -n "Which would you like? [$default_value] "
431 if [ -z "$1" ] ; then
432 read ANSWER
433 else
434 echo $1
435 ANSWER=$1
436 fi
437
438 if [ -z "$ANSWER" ] ; then
439 export TARGET_BUILD_VARIANT=$default_value
440 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
441 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800442 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800443 fi
444 else
445 if check_variant $ANSWER
446 then
447 export TARGET_BUILD_VARIANT=$ANSWER
448 else
449 echo "** Not a valid variant: $ANSWER"
450 fi
451 fi
452 if [ -n "$1" ] ; then
453 break
454 fi
455 done
456}
457
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700458function choosecombo()
459{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700460 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700461
462 echo
463 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700464 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465
466 echo
467 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700468 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469
470 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700471 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800472 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700473}
474
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800475# Clear this variable. It will be built up again when the vendorsetup.sh
476# files are included at the end of this file.
477unset LUNCH_MENU_CHOICES
478function add_lunch_combo()
479{
480 local new_combo=$1
481 local c
482 for c in ${LUNCH_MENU_CHOICES[@]} ; do
483 if [ "$new_combo" = "$c" ] ; then
484 return
485 fi
486 done
487 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
488}
489
490# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700491add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800492add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000493add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800494add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000495add_lunch_combo aosp_x86-eng
496add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800497
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498function print_lunch_menu()
499{
500 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700501 echo
502 echo "You're building on" $uname
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000503 if [ "$(uname)" = "Darwin" ] ; then
504 echo " (ohai, koush!)"
505 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506 echo
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000507 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
508 echo "Breakfast menu... pick a combo:"
509 else
510 echo "Lunch menu... pick a combo:"
511 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512
513 local i=1
514 local choice
515 for choice in ${LUNCH_MENU_CHOICES[@]}
516 do
cybojenixa5dd6ce2013-06-28 20:30:00 +0100517 echo " $i. $choice "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800518 i=$(($i+1))
cybojenixa5dd6ce2013-06-28 20:30:00 +0100519 done | column
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800520
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000521 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
522 echo "... and don't forget the bacon!"
523 fi
524
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700525 echo
526}
527
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000528function brunch()
529{
530 breakfast $*
531 if [ $? -eq 0 ]; then
532 mka bacon
533 else
534 echo "No such item in brunch menu. Try 'breakfast'"
535 return 1
536 fi
537 return $?
538}
539
540function breakfast()
541{
542 target=$1
JustArchif70c7e62014-06-22 14:37:30 +0200543 local variant=$2
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000544 CM_DEVICES_ONLY="true"
545 unset LUNCH_MENU_CHOICES
546 add_lunch_combo full-eng
547 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
548 do
549 echo "including $f"
550 . $f
551 done
552 unset f
553
554 if [ $# -eq 0 ]; then
555 # No arguments, so let's have the full menu
556 lunch
557 else
558 echo "z$target" | grep -q "-"
559 if [ $? -eq 0 ]; then
560 # A buildtype was specified, assume a full device name
561 lunch $target
562 else
563 # This is probably just the CM model name
JustArchif70c7e62014-06-22 14:37:30 +0200564 if [ -z "$variant" ]; then
565 variant="userdebug"
566 fi
567 lunch cm_$target-$variant
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000568 fi
569 fi
570 return $?
571}
572
573alias bib=breakfast
574
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700575function lunch()
576{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800577 local answer
578
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800580 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581 else
582 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700583 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585 fi
586
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800587 local selection=
588
589 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700591 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
593 then
594 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
595 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800596 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 fi
598 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
599 then
600 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700601 fi
602
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 if [ -z "$selection" ]
604 then
605 echo
606 echo "Invalid lunch combo: $answer"
607 return 1
608 fi
609
Joe Onoratoda12daf2010-06-09 18:18:31 -0700610 export TARGET_BUILD_APPS=
611
Jeff Browne33ba4c2011-07-11 22:11:46 -0700612 local product=$(echo -n $selection | sed -e "s/-.*$//")
613 check_product $product
614 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800615 then
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800616 # if we can't find a product, try to grab it off the CM github
617 T=$(gettop)
618 pushd $T > /dev/null
619 build/tools/roomservice.py $product
620 popd > /dev/null
621 check_product $product
Diogo Ferreira0d109172012-03-18 21:18:29 +0000622 else
623 build/tools/roomservice.py $product true
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800624 fi
625 if [ $? -ne 0 ]
626 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700627 echo
628 echo "** Don't have a product spec for: '$product'"
629 echo "** Do you have the right repo manifest?"
630 product=
631 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800632
Jeff Browne33ba4c2011-07-11 22:11:46 -0700633 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
634 check_variant $variant
635 if [ $? -ne 0 ]
636 then
637 echo
638 echo "** Invalid variant: '$variant'"
639 echo "** Must be one of ${VARIANT_CHOICES[@]}"
640 variant=
641 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642
Jeff Browne33ba4c2011-07-11 22:11:46 -0700643 if [ -z "$product" -o -z "$variant" ]
644 then
645 echo
646 return 1
647 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648
Jeff Browne33ba4c2011-07-11 22:11:46 -0700649 export TARGET_PRODUCT=$product
650 export TARGET_BUILD_VARIANT=$variant
651 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700652
653 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800654
Chirayu Desaib89b3242013-06-30 10:04:25 +0530655 fixup_common_out_dir
656
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700657 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800658 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700659}
660
Jeff Davidson513d7a42010-08-02 10:00:44 -0700661# Tab completion for lunch.
662function _lunch()
663{
664 local cur prev opts
665 COMPREPLY=()
666 cur="${COMP_WORDS[COMP_CWORD]}"
667 prev="${COMP_WORDS[COMP_CWORD-1]}"
668
669 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
670 return 0
671}
Emilio López9ad6eea2013-11-03 13:02:13 -0300672complete -F _lunch lunch 2>/dev/null
Jeff Davidson513d7a42010-08-02 10:00:44 -0700673
Joe Onoratoda12daf2010-06-09 18:18:31 -0700674# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700675# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700676function tapas()
677{
Ying Wangb541ab62014-05-29 17:57:40 -0700678 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 -0700679 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700680 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
681 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 -0700682
Ying Wang67f02922012-08-22 10:25:20 -0700683 if [ $(echo $arch | wc -w) -gt 1 ]; then
684 echo "tapas: Error: Multiple build archs supplied: $arch"
685 return
686 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700687 if [ $(echo $variant | wc -w) -gt 1 ]; then
688 echo "tapas: Error: Multiple build variants supplied: $variant"
689 return
690 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700691 if [ $(echo $density | wc -w) -gt 1 ]; then
692 echo "tapas: Error: Multiple densities supplied: $density"
693 return
694 fi
Ying Wang67f02922012-08-22 10:25:20 -0700695
696 local product=full
697 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700698 x86) product=full_x86;;
699 mips) product=full_mips;;
700 armv5) product=generic_armv5;;
701 arm64) product=aosp_arm64;;
702 x86_64) product=aosp_x86_64;;
703 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700704 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700705 if [ -z "$variant" ]; then
706 variant=eng
707 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700708 if [ -z "$apps" ]; then
709 apps=all
710 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600711 if [ -z "$density" ]; then
712 density=alldpi
713 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700714
Ying Wang67f02922012-08-22 10:25:20 -0700715 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700716 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700717 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700718 export TARGET_BUILD_TYPE=release
719 export TARGET_BUILD_APPS=$apps
720
721 set_stuff_for_environment
722 printconfig
723}
724
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100725function eat()
726{
727 if [ "$OUT" ] ; then
Chirayu Desai80a277d2013-05-04 17:59:18 +0530728 MODVERSION=$(get_build_var CM_VERSION)
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100729 ZIPFILE=cm-$MODVERSION.zip
730 ZIPPATH=$OUT/$ZIPFILE
731 if [ ! -f $ZIPPATH ] ; then
732 echo "Nothing to eat"
733 return 1
734 fi
735 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
736 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
737 echo "No device is online. Waiting for one..."
738 echo "Please connect USB and/or enable USB debugging"
739 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
740 sleep 1
741 done
742 echo "Device Found.."
743 fi
Chirayu Desai6dadb572012-12-26 10:53:58 +0530744 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
745 then
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100746 # if adbd isn't root we can't write to /cache/recovery/
747 adb root
748 sleep 1
749 adb wait-for-device
Steve Kondik464574f2013-04-13 07:19:52 -0700750 cat << EOF > /tmp/command
751--sideload
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100752EOF
Steve Kondik464574f2013-04-13 07:19:52 -0700753 if adb push /tmp/command /cache/recovery/ ; then
754 echo "Rebooting into recovery for sideload installation"
755 adb reboot recovery
756 adb wait-for-sideload
757 adb sideload $ZIPPATH
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100758 fi
Steve Kondik464574f2013-04-13 07:19:52 -0700759 rm /tmp/command
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100760 else
761 echo "Nothing to eat"
762 return 1
763 fi
764 return $?
Chirayu Desai6dadb572012-12-26 10:53:58 +0530765 else
766 echo "The connected device does not appear to be $CM_BUILD, run away!"
767 fi
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100768}
769
Nebojsa Cvetkovicd5c0c872012-11-09 23:02:54 +0000770function omnom
771{
772 brunch $*
773 eat
774}
775
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776function gettop
777{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800778 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700779 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700780 # The following circumlocution ensures we remove symlinks from TOP.
781 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700782 else
783 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800784 # The following circumlocution (repeated below as well) ensures
785 # that we record the true directory name and not one that is
786 # faked up with symlink names.
787 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800789 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 T=
791 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800792 \cd ..
synergyb112a402013-07-05 19:47:47 -0700793 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700794 done
Ying Wang9cd17642012-12-13 10:52:07 -0800795 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700796 if [ -f "$T/$TOPFILE" ]; then
797 echo $T
798 fi
799 fi
800 fi
801}
802
Andrew Hsieh906cb782013-09-10 17:37:14 +0800803# Return driver for "make", if any (eg. static analyzer)
804function getdriver()
805{
806 local T="$1"
807 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
808 if [ -n "$WITH_STATIC_ANALYZER" ]; then
809 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800810$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
811--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800812--status-bugs \
813--top=$T"
814 fi
815}
816
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700817function m()
818{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800819 local T=$(gettop)
820 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700821 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800822 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700823 else
824 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700825 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826 fi
827}
828
829function findmakefile()
830{
831 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800832 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700833 T=
834 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700835 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700836 if [ -f "$T/Android.mk" ]; then
837 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800838 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700839 return
840 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800841 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700842 done
Ying Wang9cd17642012-12-13 10:52:07 -0800843 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700844}
845
846function mm()
847{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800848 local T=$(gettop)
849 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700850 # If we're sitting in the root of the build tree, just do a
851 # normal make.
852 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800853 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700854 else
855 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800856 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700857 local MODULES=
858 local GET_INSTALL_PATH=
859 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700860 # Remove the path to top as the makefilepath needs to be relative
861 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700862 if [ ! "$T" ]; then
863 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700864 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700865 elif [ ! "$M" ]; then
866 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700867 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700868 else
Ying Wanga7deb082013-08-16 13:24:47 -0700869 for ARG in $@; do
870 case $ARG in
871 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
872 esac
873 done
874 if [ -n "$GET_INSTALL_PATH" ]; then
875 MODULES=
876 ARGS=GET-INSTALL-PATH
877 else
878 MODULES=all_modules
879 ARGS=$@
880 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700881 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 -0700882 fi
883 fi
884}
885
886function mmm()
887{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800888 local T=$(gettop)
889 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700890 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800891 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800892 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800893 local ARGS=
894 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700895 local GET_INSTALL_PATH=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800896 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
897 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
898 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800899 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
900 if [ "$MODULES" = "" ]; then
901 MODULES=all_modules
902 fi
903 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700904 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700905 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
906 local TO_CHOP=`expr $TO_CHOP + 1`
907 local START=`PWD= /bin/pwd`
908 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700909 if [ "$MFILE" = "" ] ; then
910 MFILE=$DIR/Android.mk
911 else
912 MFILE=$MFILE/$DIR/Android.mk
913 fi
914 MAKEFILE="$MAKEFILE $MFILE"
915 else
Ying Wanga7deb082013-08-16 13:24:47 -0700916 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100917 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700918 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
919 *) echo "No Android.mk in $DIR."; return 1;;
920 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700921 fi
922 done
Ying Wanga7deb082013-08-16 13:24:47 -0700923 if [ -n "$GET_INSTALL_PATH" ]; then
924 ARGS=$GET_INSTALL_PATH
925 MODULES=
926 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800927 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 -0700928 else
929 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700930 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700931 fi
932}
933
Ying Wangb607f7b2013-02-08 18:01:04 -0800934function mma()
935{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800936 local T=$(gettop)
937 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800938 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800939 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800940 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800941 if [ ! "$T" ]; then
942 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700943 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800944 fi
945 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800946 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800947 fi
948}
949
950function mmma()
951{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800952 local T=$(gettop)
953 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800954 if [ "$T" ]; then
955 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
956 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
957 local MY_PWD=`PWD= /bin/pwd`
958 if [ "$MY_PWD" = "$T" ]; then
959 MY_PWD=
960 else
961 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
962 fi
963 local DIR=
964 local MODULE_PATHS=
965 local ARGS=
966 for DIR in $DIRS ; do
967 if [ -d $DIR ]; then
968 if [ "$MY_PWD" = "" ]; then
969 MODULE_PATHS="$MODULE_PATHS $DIR"
970 else
971 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
972 fi
973 else
974 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100975 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800976 *) echo "Couldn't find directory $DIR"; return 1;;
977 esac
978 fi
979 done
Andrew Hsieh906cb782013-09-10 17:37:14 +0800980 $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 -0800981 else
982 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700983 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800984 fi
985}
986
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700987function croot()
988{
989 T=$(gettop)
990 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800991 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700992 else
993 echo "Couldn't locate the top of the tree. Try setting TOP."
994 fi
995}
996
nebkatfb67a1e2012-12-28 10:40:45 +0000997function cout()
998{
999 if [ "$OUT" ]; then
1000 cd $OUT
1001 else
1002 echo "Couldn't locate out directory. Try setting OUT."
1003 fi
1004}
1005
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001006function cproj()
1007{
1008 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001009 local HERE=$PWD
1010 T=
1011 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1012 T=$PWD
1013 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001014 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001015 return
1016 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001017 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001018 done
Ying Wang9cd17642012-12-13 10:52:07 -08001019 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001020 echo "can't find Android.mk"
1021}
1022
Daniel Sandler47e0a882013-07-30 13:23:52 -04001023# simplified version of ps; output in the form
1024# <pid> <procname>
1025function qpid() {
1026 local prepend=''
1027 local append=''
1028 if [ "$1" = "--exact" ]; then
1029 prepend=' '
1030 append='$'
1031 shift
1032 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1033 echo "usage: qpid [[--exact] <process name|pid>"
1034 return 255
1035 fi
1036
1037 local EXE="$1"
1038 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001039 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001040 else
1041 adb shell ps \
1042 | tr -d '\r' \
1043 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1044 fi
1045}
1046
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001047function pid()
1048{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001049 local prepend=''
1050 local append=''
1051 if [ "$1" = "--exact" ]; then
1052 prepend=' '
1053 append='$'
1054 shift
1055 fi
1056 local EXE="$1"
1057 if [ "$EXE" ] ; then
1058 local PID=`adb shell ps \
1059 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001060 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001061 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1062 echo "$PID"
1063 else
1064 echo "usage: pid [--exact] <process name>"
1065 return 255
1066 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001067}
1068
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001069# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001070# that has the core-file-size limit set correctly
1071#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001072# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001073# if its core-file-size limit is not set already.
1074# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1075
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001076function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001077{
1078 echo "Getting root...";
1079 adb root;
1080 adb wait-for-device;
1081
1082 echo "Remounting root parition read-write...";
1083 adb shell mount -w -o remount -t rootfs rootfs;
1084 sleep 1;
1085 adb wait-for-device;
1086 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001087 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001088 adb shell chmod 0777 /cores;
1089
1090 echo "Granting SELinux permission to dump in /cores...";
1091 adb shell restorecon -R /cores;
1092
1093 echo "Set core pattern.";
1094 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1095
1096 echo "Done."
1097}
1098
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001099# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001100# $1 = PID of process (e.g., $(pid mediaserver))
1101#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001102# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001103# dump to actually be generated.
1104
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001105function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001106{
1107 local PID=$1;
1108 if [ -z "$PID" ]; then
1109 printf "Expecting a PID!\n";
1110 return;
1111 fi;
1112 echo "Setting core limit for $PID to infinite...";
1113 adb shell prlimit $PID 4 -1 -1
1114}
1115
1116# core - send SIGV and pull the core for process
1117# $1 = PID of process (e.g., $(pid mediaserver))
1118#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001119# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001120# enabled globally.
1121
1122function core()
1123{
1124 local PID=$1;
1125
1126 if [ -z "$PID" ]; then
1127 printf "Expecting a PID!\n";
1128 return;
1129 fi;
1130
1131 local CORENAME=core.$PID;
1132 local COREPATH=/cores/$CORENAME;
1133 local SIG=SEGV;
1134
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001135 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001136
1137 local done=0;
1138 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1139 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1140 adb shell kill -$SIG $PID;
1141 sleep 1;
1142 done;
1143
1144 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1145 echo "Done: core is under $COREPATH on device.";
1146}
1147
Christopher Tate744ee802009-11-12 15:33:08 -08001148# systemstack - dump the current stack trace of all threads in the system process
1149# to the usual ANR traces file
1150function systemstack()
1151{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001152 stacks system_server
1153}
1154
1155function stacks()
1156{
1157 if [[ $1 =~ ^[0-9]+$ ]] ; then
1158 local PID="$1"
1159 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001160 local PIDLIST="$(pid $1)"
1161 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1162 local PID="$PIDLIST"
1163 elif [ "$PIDLIST" ] ; then
1164 echo "more than one process: $1"
1165 else
1166 echo "no such process: $1"
1167 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001168 else
1169 echo "usage: stacks [pid|process name]"
1170 fi
1171
1172 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001173 # Determine whether the process is native
1174 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1175 # Dump stacks of Dalvik process
1176 local TRACES=/data/anr/traces.txt
1177 local ORIG=/data/anr/traces.orig
1178 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001179
Jeff Brownb12c2e52013-08-19 15:14:16 -07001180 # Keep original traces to avoid clobbering
1181 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001182
Jeff Brownb12c2e52013-08-19 15:14:16 -07001183 # Make sure we have a usable file
1184 adb shell touch $TRACES
1185 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001186
Jeff Brownb12c2e52013-08-19 15:14:16 -07001187 # Dump stacks and wait for dump to finish
1188 adb shell kill -3 $PID
1189 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001190
Jeff Brownb12c2e52013-08-19 15:14:16 -07001191 # Restore original stacks, and show current output
1192 adb shell mv $TRACES $TMP
1193 adb shell mv $ORIG $TRACES
1194 adb shell cat $TMP
1195 else
1196 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001197 local USE64BIT="$(is64bit $PID)"
1198 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001199 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001200 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001201}
1202
Michael Wrightaeed7212014-06-19 19:58:12 -07001203# Read the ELF header from /proc/$PID/exe to determine if the process is
1204# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001205function is64bit()
1206{
1207 local PID="$1"
1208 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001209 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001210 echo "64"
1211 else
1212 echo ""
1213 fi
1214 else
1215 echo ""
1216 fi
1217}
1218
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219case `uname -s` in
1220 Darwin)
1221 function sgrep()
1222 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001223 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 -07001224 }
1225
1226 ;;
1227 *)
1228 function sgrep()
1229 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001230 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 -07001231 }
1232 ;;
1233esac
1234
Raghu Gandham8da43102012-07-25 19:57:22 -07001235function gettargetarch
1236{
1237 get_build_var TARGET_ARCH
1238}
1239
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001240function ggrep()
1241{
1242 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1243}
1244
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245function jgrep()
1246{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001247 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 -07001248}
1249
1250function cgrep()
1251{
Dan Albert01961192014-11-22 10:16:01 -08001252 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 -07001253}
1254
1255function resgrep()
1256{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001257 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 -07001258}
1259
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001260function mangrep()
1261{
1262 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1263}
1264
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001265function sepgrep()
1266{
1267 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 "$@"
1268}
1269
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001270function rcgrep()
1271{
1272 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" -print0 | xargs -0 grep --color -n "$@"
1273}
1274
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001275case `uname -s` in
1276 Darwin)
1277 function mgrep()
1278 {
Ying Wang324c2b22012-08-17 15:03:20 -07001279 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 -07001280 }
1281
1282 function treegrep()
1283 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001284 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 -07001285 }
1286
1287 ;;
1288 *)
1289 function mgrep()
1290 {
Ying Wang324c2b22012-08-17 15:03:20 -07001291 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 -07001292 }
1293
1294 function treegrep()
1295 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001296 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 -07001297 }
1298
1299 ;;
1300esac
1301
1302function getprebuilt
1303{
1304 get_abs_build_var ANDROID_PREBUILTS
1305}
1306
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001307function tracedmdump()
1308{
1309 T=$(gettop)
1310 if [ ! "$T" ]; then
1311 echo "Couldn't locate the top of the tree. Try setting TOP."
1312 return
1313 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001314 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001315 local arch=$(gettargetarch)
1316 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001317
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001318 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001319 if [ ! "$TRACE" ] ; then
1320 echo "usage: tracedmdump tracename"
1321 return
1322 fi
1323
Jack Veenstra60116fc2009-04-09 18:12:34 -07001324 if [ ! -r "$KERNEL" ] ; then
1325 echo "Error: cannot find kernel: '$KERNEL'"
1326 return
1327 fi
1328
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001329 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001330 if [ "$BASETRACE" = "$TRACE" ] ; then
1331 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1332 fi
1333
1334 echo "post-processing traces..."
1335 rm -f $TRACE/qtrace.dexlist
1336 post_trace $TRACE
1337 if [ $? -ne 0 ]; then
1338 echo "***"
1339 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1340 echo "***"
1341 return
1342 fi
1343 echo "generating dexlist output..."
1344 /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
1345 echo "generating dmtrace data..."
1346 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1347 echo "generating html file..."
1348 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1349 echo "done, see $TRACE/dmtrace.html for details"
1350 echo "or run:"
1351 echo " traceview $TRACE/dmtrace"
1352}
1353
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001354# communicate with a running device or emulator, set up necessary state,
1355# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001356function runhat()
1357{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001358 # process standard adb options
1359 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001360 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001361 adbTarget=$1
1362 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001363 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001364 adbTarget="$1 $2"
1365 shift 2
1366 fi
1367 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001368 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001369
1370 # runhat options
1371 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001372
1373 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001374 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001375 return
1376 fi
1377
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001378 # confirm hat is available
1379 if [ -z $(which hat) ]; then
1380 echo "hat is not available in this configuration."
1381 return
1382 fi
1383
Andy McFaddenb6289852010-07-12 08:00:19 -07001384 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001385 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001386 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001387 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001388 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001389 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001390 echo -n "> "
1391 read
1392
The Android Open Source Project88b60792009-03-03 19:28:42 -08001393 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394
The Android Open Source Project88b60792009-03-03 19:28:42 -08001395 echo "Retrieving file $devFile..."
1396 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001397
The Android Open Source Project88b60792009-03-03 19:28:42 -08001398 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001399
The Android Open Source Project88b60792009-03-03 19:28:42 -08001400 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001401 echo "View the output by pointing your browser at http://localhost:7000/"
1402 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001403 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001404}
1405
1406function getbugreports()
1407{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001408 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001409
1410 if [ ! "$reports" ]; then
1411 echo "Could not locate any bugreports."
1412 return
1413 fi
1414
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001415 local report
1416 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001417 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001418 echo "/sdcard/bugreports/${report}"
1419 adb pull /sdcard/bugreports/${report} ${report}
1420 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001421 done
1422}
1423
Victoria Lease1b296b42012-08-21 15:44:06 -07001424function getsdcardpath()
1425{
1426 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1427}
1428
1429function getscreenshotpath()
1430{
1431 echo "$(getsdcardpath)/Pictures/Screenshots"
1432}
1433
1434function getlastscreenshot()
1435{
1436 local screenshot_path=$(getscreenshotpath)
1437 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1438 if [ "$screenshot" = "" ]; then
1439 echo "No screenshots found."
1440 return
1441 fi
1442 echo "${screenshot}"
1443 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1444}
1445
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001446function startviewserver()
1447{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001448 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001449 if [ $# -gt 0 ]; then
1450 port=$1
1451 fi
1452 adb shell service call window 1 i32 $port
1453}
1454
1455function stopviewserver()
1456{
1457 adb shell service call window 2
1458}
1459
1460function isviewserverstarted()
1461{
1462 adb shell service call window 3
1463}
1464
Romain Guyb84049a2010-10-04 16:56:11 -07001465function key_home()
1466{
1467 adb shell input keyevent 3
1468}
1469
1470function key_back()
1471{
1472 adb shell input keyevent 4
1473}
1474
1475function key_menu()
1476{
1477 adb shell input keyevent 82
1478}
1479
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001480function smoketest()
1481{
1482 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1483 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1484 return
1485 fi
1486 T=$(gettop)
1487 if [ ! "$T" ]; then
1488 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1489 return
1490 fi
1491
Ying Wang9cd17642012-12-13 10:52:07 -08001492 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001493 adb uninstall com.android.smoketest > /dev/null &&
1494 adb uninstall com.android.smoketest.tests > /dev/null &&
1495 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1496 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1497 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1498}
1499
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001500# simple shortcut to the runtest command
1501function runtest()
1502{
1503 T=$(gettop)
1504 if [ ! "$T" ]; then
1505 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1506 return
1507 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001508 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001509}
1510
The Android Open Source Project88b60792009-03-03 19:28:42 -08001511function godir () {
1512 if [[ -z "$1" ]]; then
1513 echo "Usage: godir <regex>"
1514 return
1515 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001516 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001517 if [[ ! -f $T/filelist ]]; then
1518 echo -n "Creating index..."
Ying Wang9cd17642012-12-13 10:52:07 -08001519 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001520 echo " Done"
1521 echo ""
1522 fi
1523 local lines
Jeff Hamilton293f9392011-11-18 17:15:25 -06001524 lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001525 if [[ ${#lines[@]} = 0 ]]; then
1526 echo "Not found"
1527 return
1528 fi
1529 local pathname
1530 local choice
1531 if [[ ${#lines[@]} > 1 ]]; then
1532 while [[ -z "$pathname" ]]; do
1533 local index=1
1534 local line
1535 for line in ${lines[@]}; do
1536 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001537 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001538 done
1539 echo
1540 echo -n "Select one: "
1541 unset choice
1542 read choice
1543 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1544 echo "Invalid choice"
1545 continue
1546 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001547 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001548 done
1549 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001550 pathname=${lines[0]}
1551 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001552 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001553}
1554
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001555function cmremote()
1556{
1557 git remote rm cmremote 2> /dev/null
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301558 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001559 if [ -z "$GERRIT_REMOTE" ]
1560 then
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301561 echo Unable to set up the git remote, are you under a git repo?
1562 return 0
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001563 fi
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301564 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001565 if [ -z "$CMUSER" ]
1566 then
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001567 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001568 else
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001569 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001570 fi
1571 echo You can now push to "cmremote".
1572}
Koushik Duttab55f13a2012-05-14 16:14:36 -07001573
Steve Kondik873fa562012-09-17 11:33:18 -07001574function aospremote()
1575{
1576 git remote rm aosp 2> /dev/null
1577 if [ ! -d .git ]
1578 then
1579 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1580 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001581 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik873fa562012-09-17 11:33:18 -07001582 if (echo $PROJECT | grep -qv "^device")
1583 then
1584 PFX="platform/"
1585 fi
1586 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1587 echo "Remote 'aosp' created"
1588}
Steve Kondik873fa562012-09-17 11:33:18 -07001589
Steve Kondik20c21d22013-10-27 13:34:36 -07001590function cafremote()
1591{
1592 git remote rm caf 2> /dev/null
1593 if [ ! -d .git ]
1594 then
1595 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1596 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001597 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik20c21d22013-10-27 13:34:36 -07001598 if (echo $PROJECT | grep -qv "^device")
1599 then
1600 PFX="platform/"
1601 fi
1602 git remote add caf git://codeaurora.org/$PFX$PROJECT
1603 echo "Remote 'caf' created"
1604}
Steve Kondik20c21d22013-10-27 13:34:36 -07001605
Steve Kondikf00e7d92012-09-23 23:46:55 -07001606function installboot()
1607{
1608 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1609 then
1610 echo "No recovery.fstab found. Build recovery first."
1611 return 1
1612 fi
1613 if [ ! -e "$OUT/boot.img" ];
1614 then
1615 echo "No boot.img found. Run make bootimage first."
1616 return 1
1617 fi
1618 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1619 if [ -z "$PARTITION" ];
1620 then
Ricardo Cerqueira9e4c4a72013-07-27 13:51:56 +01001621 # Try for RECOVERY_FSTAB_VERSION = 2
1622 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1623 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1624 if [ -z "$PARTITION" ];
1625 then
1626 echo "Unable to determine boot partition."
1627 return 1
1628 fi
Steve Kondikf00e7d92012-09-23 23:46:55 -07001629 fi
1630 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001631 adb wait-for-online
Steve Kondikf00e7d92012-09-23 23:46:55 -07001632 adb root
1633 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001634 adb wait-for-online shell mount /system 2>&1 > /dev/null
1635 adb wait-for-online remount
Steve Kondikf00e7d92012-09-23 23:46:55 -07001636 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
1637 then
1638 adb push $OUT/boot.img /cache/
1639 for i in $OUT/system/lib/modules/*;
1640 do
1641 adb push $i /system/lib/modules/
1642 done
1643 adb shell dd if=/cache/boot.img of=$PARTITION
1644 adb shell chmod 644 /system/lib/modules/*
1645 echo "Installation complete."
1646 else
1647 echo "The connected device does not appear to be $CM_BUILD, run away!"
1648 fi
1649}
1650
Steve Kondik83c03d52012-10-24 16:40:42 -07001651function installrecovery()
1652{
1653 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1654 then
1655 echo "No recovery.fstab found. Build recovery first."
1656 return 1
1657 fi
1658 if [ ! -e "$OUT/recovery.img" ];
1659 then
1660 echo "No recovery.img found. Run make recoveryimage first."
1661 return 1
1662 fi
1663 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1664 if [ -z "$PARTITION" ];
1665 then
Steve Kondik75f10762013-08-09 21:03:42 -07001666 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondikd8b510a2013-08-10 21:02:09 -07001667 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1668 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondik75f10762013-08-09 21:03:42 -07001669 if [ -z "$PARTITION" ];
1670 then
1671 echo "Unable to determine recovery partition."
1672 return 1
1673 fi
Steve Kondik83c03d52012-10-24 16:40:42 -07001674 fi
1675 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001676 adb wait-for-online
Steve Kondik83c03d52012-10-24 16:40:42 -07001677 adb root
1678 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001679 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1680 adb wait-for-online remount
Steve Kondik83c03d52012-10-24 16:40:42 -07001681 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
1682 then
1683 adb push $OUT/recovery.img /cache/
1684 adb shell dd if=/cache/recovery.img of=$PARTITION
1685 echo "Installation complete."
1686 else
1687 echo "The connected device does not appear to be $CM_BUILD, run away!"
1688 fi
1689}
Steve Kondikf00e7d92012-09-23 23:46:55 -07001690
Koushik Duttab55f13a2012-05-14 16:14:36 -07001691function makerecipe() {
1692 if [ -z "$1" ]
1693 then
1694 echo "No branch name provided."
1695 return 1
1696 fi
1697 cd android
1698 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1699 git commit -a -m "$1"
1700 cd ..
1701
1702 repo forall -c '
1703
1704 if [ "$REPO_REMOTE" == "github" ]
1705 then
1706 pwd
1707 cmremote
1708 git push cmremote HEAD:refs/heads/'$1'
1709 fi
1710 '
1711}
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001712
1713function cmgerrit() {
1714 if [ $# -eq 0 ]; then
1715 $FUNCNAME help
1716 return 1
1717 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001718 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001719 local review=`git config --get remote.github.review`
1720 local project=`git config --get remote.github.projectname`
1721 local command=$1
1722 shift
1723 case $command in
1724 help)
1725 if [ $# -eq 0 ]; then
1726 cat <<EOF
1727Usage:
1728 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1729
1730Commands:
1731 fetch Just fetch the change as FETCH_HEAD
1732 help Show this help, or for a specific command
1733 pull Pull a change into current branch
1734 push Push HEAD or a local branch to Gerrit for a specific branch
1735
1736Any other Git commands that support refname would work as:
1737 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1738
1739See '$FUNCNAME help COMMAND' for more information on a specific command.
1740
1741Example:
1742 $FUNCNAME checkout -b topic 1234/5
1743works as:
1744 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1745 && git checkout -b topic FETCH_HEAD
1746will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1747Patch-set 1 will be fetched if omitted.
1748EOF
1749 return
1750 fi
1751 case $1 in
1752 __cmg_*) echo "For internal use only." ;;
1753 changes|for)
1754 if [ "$FUNCNAME" = "cmgerrit" ]; then
1755 echo "'$FUNCNAME $1' is deprecated."
1756 fi
1757 ;;
1758 help) $FUNCNAME help ;;
1759 fetch|pull) cat <<EOF
1760usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1761
1762works as:
1763 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1764 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1765
1766Example:
1767 $FUNCNAME $1 1234
1768will $1 patch-set 1 of change 1234
1769EOF
1770 ;;
1771 push) cat <<EOF
1772usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1773
1774works as:
1775 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1776 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1777
1778Example:
1779 $FUNCNAME push fix6789:gingerbread
1780will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1781HEAD will be pushed from local if omitted.
1782EOF
1783 ;;
1784 *)
1785 $FUNCNAME __cmg_err_not_supported $1 && return
1786 cat <<EOF
1787usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1788
1789works as:
1790 git fetch http://DOMAIN/p/PROJECT \\
1791 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1792 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1793EOF
1794 ;;
1795 esac
1796 ;;
1797 __cmg_get_ref)
1798 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1799 local change_id patchset_id hash
1800 case $1 in
1801 */*)
1802 change_id=${1%%/*}
1803 patchset_id=${1#*/}
1804 ;;
1805 *)
1806 change_id=$1
1807 patchset_id=1
1808 ;;
1809 esac
1810 hash=$(($change_id % 100))
1811 case $hash in
1812 [0-9]) hash="0$hash" ;;
1813 esac
1814 echo "refs/changes/$hash/$change_id/$patchset_id"
1815 ;;
1816 fetch|pull)
1817 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1818 $FUNCNAME __cmg_err_not_repo && return 1
1819 local change=$1
1820 shift
1821 git $command $@ http://$review/p/$project \
1822 $($FUNCNAME __cmg_get_ref $change) || return 1
1823 ;;
1824 push)
1825 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1826 $FUNCNAME __cmg_err_not_repo && return 1
1827 if [ -z "$user" ]; then
1828 echo >&2 "Gerrit username not found."
1829 return 1
1830 fi
1831 local local_branch remote_branch
1832 case $1 in
1833 *:*)
1834 local_branch=${1%:*}
1835 remote_branch=${1##*:}
1836 ;;
1837 *)
1838 local_branch=HEAD
1839 remote_branch=$1
1840 ;;
1841 esac
1842 shift
1843 git push $@ ssh://$user@$review:29418/$project \
1844 $local_branch:refs/for/$remote_branch || return 1
1845 ;;
1846 changes|for)
1847 if [ "$FUNCNAME" = "cmgerrit" ]; then
1848 echo >&2 "'$FUNCNAME $command' is deprecated."
1849 fi
1850 ;;
1851 __cmg_err_no_arg)
1852 if [ $# -lt 2 ]; then
1853 echo >&2 "'$FUNCNAME $command' missing argument."
1854 elif [ $2 -eq 0 ]; then
1855 if [ -n "$3" ]; then
1856 $FUNCNAME help $1
1857 else
1858 echo >&2 "'$FUNCNAME $1' missing argument."
1859 fi
1860 else
1861 return 1
1862 fi
1863 ;;
1864 __cmg_err_not_repo)
1865 if [ -z "$review" -o -z "$project" ]; then
1866 echo >&2 "Not currently in any reviewable repository."
1867 else
1868 return 1
1869 fi
1870 ;;
1871 __cmg_err_not_supported)
1872 $FUNCNAME __cmg_err_no_arg $command $# && return
1873 case $1 in
1874 #TODO: filter more git commands that don't use refname
1875 init|add|rm|mv|status|clone|remote|bisect|config|stash)
1876 echo >&2 "'$FUNCNAME $1' is not supported."
1877 ;;
1878 *) return 1 ;;
1879 esac
1880 ;;
1881 #TODO: other special cases?
1882 *)
1883 $FUNCNAME __cmg_err_not_supported $command && return 1
1884 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1885 $FUNCNAME __cmg_err_not_repo && return 1
1886 local args="$@"
1887 local change pre_args refs_arg post_args
1888 case "$args" in
1889 *--\ *)
1890 pre_args=${args%%-- *}
1891 post_args="-- ${args#*-- }"
1892 ;;
1893 *) pre_args="$args" ;;
1894 esac
1895 args=($pre_args)
1896 pre_args=
1897 if [ ${#args[@]} -gt 0 ]; then
1898 change=${args[${#args[@]}-1]}
1899 fi
1900 if [ ${#args[@]} -gt 1 ]; then
1901 pre_args=${args[0]}
1902 for ((i=1; i<${#args[@]}-1; i++)); do
1903 pre_args="$pre_args ${args[$i]}"
1904 done
1905 fi
1906 while ((1)); do
1907 case $change in
1908 ""|--)
1909 $FUNCNAME help $command
1910 return 1
1911 ;;
1912 *@*)
1913 if [ -z "$refs_arg" ]; then
1914 refs_arg="@${change#*@}"
1915 change=${change%%@*}
1916 fi
1917 ;;
1918 *~*)
1919 if [ -z "$refs_arg" ]; then
1920 refs_arg="~${change#*~}"
1921 change=${change%%~*}
1922 fi
1923 ;;
1924 *^*)
1925 if [ -z "$refs_arg" ]; then
1926 refs_arg="^${change#*^}"
1927 change=${change%%^*}
1928 fi
1929 ;;
1930 *:*)
1931 if [ -z "$refs_arg" ]; then
1932 refs_arg=":${change#*:}"
1933 change=${change%%:*}
1934 fi
1935 ;;
1936 *) break ;;
1937 esac
1938 done
1939 $FUNCNAME fetch $change \
1940 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
1941 || return 1
1942 ;;
1943 esac
1944}
1945
1946function cmrebase() {
1947 local repo=$1
1948 local refs=$2
1949 local pwd="$(pwd)"
1950 local dir="$(gettop)/$repo"
1951
1952 if [ -z $repo ] || [ -z $refs ]; then
1953 echo "CyanogenMod Gerrit Rebase Usage: "
1954 echo " cmrebase <path to project> <patch IDs on Gerrit>"
1955 echo " The patch IDs appear on the Gerrit commands that are offered."
1956 echo " They consist on a series of numbers and slashes, after the text"
1957 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
1958 echo ""
1959 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
1960 echo ""
1961 return
1962 fi
1963
1964 if [ ! -d $dir ]; then
1965 echo "Directory $dir doesn't exist in tree."
1966 return
1967 fi
1968 cd $dir
1969 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
1970 echo "Starting branch..."
1971 repo start tmprebase .
1972 echo "Bringing it up to date..."
1973 repo sync .
1974 echo "Fetching change..."
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001975 git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001976 if [ "$?" != "0" ]; then
1977 echo "Error cherry-picking. Not uploading!"
1978 return
1979 fi
1980 echo "Uploading..."
1981 repo upload .
1982 echo "Cleaning up..."
1983 repo abandon tmprebase .
1984 cd $pwd
1985}
1986
1987function mka() {
1988 case `uname -s` in
1989 Darwin)
1990 make -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
1991 ;;
1992 *)
Arnav Gupta3b909942012-11-23 10:47:44 -07001993 schedtool -B -n 1 -e ionice -n 1 make -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001994 ;;
1995 esac
1996}
1997
1998function reposync() {
1999 case `uname -s` in
2000 Darwin)
2001 repo sync -j 4 "$@"
2002 ;;
2003 *)
Alan Orthef363cd2012-09-07 11:44:27 +03002004 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002005 ;;
2006 esac
2007}
Tanguy Pruvot2192fd22012-06-06 21:39:23 +02002008
2009function repodiff() {
2010 if [ -z "$*" ]; then
2011 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
2012 return
2013 fi
2014 diffopts=$* repo forall -c \
2015 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
2016}
2017
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302018# Credit for color strip sed: http://goo.gl/BoIcm
2019function dopush()
2020{
2021 local func=$1
2022 shift
2023
2024 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
2025 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
2026 echo "No device is online. Waiting for one..."
2027 echo "Please connect USB and/or enable USB debugging"
2028 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
2029 sleep 1
2030 done
2031 echo "Device Found."
2032 fi
2033
Chirayu Desai6dadb572012-12-26 10:53:58 +05302034 if (adb shell cat /system/build.prop | grep -q "ro.cm.device=$CM_BUILD");
2035 then
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002036 # retrieve IP and PORT info if we're using a TCP connection
2037 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
2038 | head -1 | awk '{print $1}')
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302039 adb root &> /dev/null
2040 sleep 0.3
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002041 if [ -n "$TCPIPPORT" ]
2042 then
2043 # adb root just killed our connection
2044 # so reconnect...
2045 adb connect "$TCPIPPORT"
2046 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302047 adb wait-for-device &> /dev/null
2048 sleep 0.3
2049 adb remount &> /dev/null
2050
Matt Mower668ea262014-05-20 02:52:23 -05002051 mkdir -p $OUT
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302052 $func $* | tee $OUT/.log
2053
2054 # Install: <file>
Matt Mower668ea262014-05-20 02:52:23 -05002055 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)"
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302056
2057 # Copy: <file>
Matt Mower668ea262014-05-20 02:52:23 -05002058 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)"
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302059
Matt Mower668ea262014-05-20 02:52:23 -05002060 stop_n_start=false
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302061 for FILE in $LOC; do
Matt Mower668ea262014-05-20 02:52:23 -05002062 # Make sure file is in $OUT/system
2063 case $FILE in
2064 $OUT/system/*)
2065 # Get target file name (i.e. /system/bin/adb)
2066 TARGET=$(echo $FILE | sed "s#$OUT##")
2067 ;;
2068 *) continue ;;
2069 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302070
Matt Mower668ea262014-05-20 02:52:23 -05002071 case $TARGET in
Pawit Pornkitprasan1822ad02014-12-22 20:11:47 +07002072 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
Matt Mower668ea262014-05-20 02:52:23 -05002073 # Only need to stop services once
2074 if ! $stop_n_start; then
2075 adb shell stop
2076 stop_n_start=true
2077 fi
2078 echo "Pushing: $TARGET"
2079 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302080 ;;
2081 *)
Matt Mower668ea262014-05-20 02:52:23 -05002082 echo "Pushing: $TARGET"
2083 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302084 ;;
Matt Mower668ea262014-05-20 02:52:23 -05002085 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302086 done
Matt Mower668ea262014-05-20 02:52:23 -05002087 if $stop_n_start; then
2088 adb shell start
2089 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302090 rm -f $OUT/.log
2091 return 0
Chirayu Desai6dadb572012-12-26 10:53:58 +05302092 else
2093 echo "The connected device does not appear to be $CM_BUILD, run away!"
2094 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302095}
2096
2097alias mmp='dopush mm'
2098alias mmmp='dopush mmm'
2099alias mkap='dopush mka'
2100alias cmkap='dopush cmka'
2101
Chirayu Desai4a319b82013-06-05 20:14:33 +05302102function repopick() {
2103 T=$(gettop)
2104 $T/build/tools/repopick.py $@
2105}
2106
Chirayu Desaib89b3242013-06-30 10:04:25 +05302107function fixup_common_out_dir() {
2108 common_out_dir=$(get_build_var OUT_DIR)/target/common
2109 target_device=$(get_build_var TARGET_DEVICE)
2110 if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
2111 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
2112 mv ${common_out_dir} ${common_out_dir}-${target_device}
2113 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2114 else
2115 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2116 mkdir -p ${common_out_dir}-${target_device}
2117 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2118 fi
2119 else
2120 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2121 mkdir -p ${common_out_dir}
2122 fi
2123}
2124
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002125# 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 +00002126#
2127# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
2128# For some reason, installing the JDK doesn't make it show up in the
2129# JavaVM.framework/Versions/1.7/ folder.
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002130function set_java_home() {
Narayan Kamath9260bba2014-01-17 10:05:25 +00002131 # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamathc84889b2014-04-01 14:16:26 +01002132 # we can reset it later, depending on the version of java the build
2133 # system needs.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002134 #
2135 # If we don't do this, the JAVA_HOME value set by the first call to
2136 # build/envsetup.sh will persist forever.
2137 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
2138 export JAVA_HOME=""
2139 fi
2140
Andy McFaddenbd960942010-06-24 12:52:51 -07002141 if [ ! "$JAVA_HOME" ]; then
Neil Fuller46e00ea2014-10-16 10:23:03 +01002142 case `uname -s` in
2143 Darwin)
2144 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2145 ;;
2146 *)
2147 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
2148 ;;
2149 esac
Narayan Kamath9260bba2014-01-17 10:05:25 +00002150
2151 # Keep track of the fact that we set JAVA_HOME ourselves, so that
2152 # we can change it on the next envsetup.sh, if required.
2153 export ANDROID_SET_JAVA_HOME=true
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002154 fi
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002155}
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002156
Alex Rayf0d08eb2013-03-08 15:15:06 -08002157# Print colored exit condition
2158function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08002159 "$@"
2160 local retval=$?
2161 if [ $retval -ne 0 ]
2162 then
2163 echo -e "\e[0;31mFAILURE\e[00m"
2164 else
2165 echo -e "\e[0;32mSUCCESS\e[00m"
2166 fi
2167 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08002168}
2169
Ying Wanged21d4c2014-08-24 22:14:19 -07002170function get_make_command()
2171{
2172 echo command make
2173}
2174
Ed Heylcc6be0a2014-06-18 14:55:58 -07002175function make()
2176{
2177 local start_time=$(date +"%s")
Ying Wanged21d4c2014-08-24 22:14:19 -07002178 $(get_make_command) "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002179 local ret=$?
2180 local end_time=$(date +"%s")
2181 local tdiff=$(($end_time-$start_time))
2182 local hours=$(($tdiff / 3600 ))
2183 local mins=$((($tdiff % 3600) / 60))
2184 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002185 local ncolors=$(tput colors 2>/dev/null)
2186 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
2187 color_failed="\e[0;31m"
2188 color_success="\e[0;32m"
2189 color_reset="\e[00m"
2190 else
2191 color_failed=""
2192 color_success=""
2193 color_reset=""
2194 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07002195 echo
2196 if [ $ret -eq 0 ] ; then
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002197 echo -n -e "${color_success}#### make completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002198 else
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002199 echo -n -e "${color_failed}#### make failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002200 fi
2201 if [ $hours -gt 0 ] ; then
2202 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
2203 elif [ $mins -gt 0 ] ; then
2204 printf "(%02g:%02g (mm:ss))" $mins $secs
2205 elif [ $secs -gt 0 ] ; then
2206 printf "(%s seconds)" $secs
2207 fi
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002208 echo -e " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002209 echo
2210 return $ret
2211}
2212
Raphael Moll70a86b02011-06-20 16:03:14 -07002213if [ "x$SHELL" != "x/bin/bash" ]; then
2214 case `ps -o command -p $$` in
2215 *bash*)
2216 ;;
Emilio López7ff9caa2013-11-03 13:05:43 -03002217 *zsh*)
2218 ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002219 *)
Emilio López7ff9caa2013-11-03 13:05:43 -03002220 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
Raphael Moll70a86b02011-06-20 16:03:14 -07002221 ;;
2222 esac
2223fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002224
2225# Execute the contents of any vendorsetup.sh files we can find.
Oleksiy Avramchenko15760a82014-10-06 18:51:58 +02002226for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
2227 `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 -08002228do
2229 echo "including $f"
2230 . $f
2231done
2232unset f
Kenny Root52aa81c2011-07-15 11:07:06 -07002233
Kyle Ladd031a0b62013-09-11 20:43:42 -04002234# Add completions
2235check_bash_version && {
2236 dirs="sdk/bash_completion vendor/cm/bash_completion"
2237 for dir in $dirs; do
2238 if [ -d ${dir} ]; then
2239 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
2240 echo "including $f"
2241 . $f
2242 done
2243 fi
2244 done
2245}
Chirayu Desaie1466d62013-03-19 17:50:37 +05302246
2247export ANDROID_BUILD_TOP=$(gettop)