blob: cad039717d5a493baf95dc2c34edbfd30804ed98 [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.
nebkat1f103db2012-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 Bateman20693f82012-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.
Jeff Brown46cbd202014-07-26 15:15:41 -070020- sgrep: Greps on all local source files.
The Android Open Source Project88b60792009-03-03 19:28:42 -080021- godir: Go to the directory containing a file.
Andrew Sutherlanda2549212011-11-18 00:45:55 -060022- cmremote: Add git remote for CM Gerrit Review
23- cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
24- cmrebase: Rebase a Gerrit change and push it again
Steve Kondik02854742012-09-17 11:33:18 -070025- aospremote: Add git remote for matching AOSP repository
Steve Kondik996e29a2013-10-27 13:34:36 -070026- cafremote: Add git remote for matching CodeAurora repository.
Andrew Sutherlanda2549212011-11-18 00:45:55 -060027- mka: Builds using SCHED_BATCH on all processors
Chirayu Desai99deacf2012-09-28 11:56:02 +053028- mkap: Builds the module(s) using mka and pushes them to the device.
29- cmka: Cleans and builds using mka.
Andrew Sutherlanda2549212011-11-18 00:45:55 -060030- reposync: Parallel repo sync using ionice and SCHED_BATCH
Chirayu Desai9cfcdb92013-06-05 20:14:33 +053031- repopick: Utility to fetch changes from Gerrit.
Steve Kondikc731b702012-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
35Look at the source to view more functions. The complete list is:
36EOF
37 T=$(gettop)
38 local A
39 A=""
Jeff Brown46cbd202014-07-26 15:15:41 -070040 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 -070041 A="$A $i"
42 done
43 echo $A
44}
45
46# Get the value of a build variable as an absolute path.
47function get_abs_build_var()
48{
49 T=$(gettop)
50 if [ ! "$T" ]; then
51 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
52 return
53 fi
Ying Wang9cd17642012-12-13 10:52:07 -080054 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070055 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070056}
57
58# Get the exact value of a build variable.
59function get_build_var()
60{
61 T=$(gettop)
62 if [ ! "$T" ]; then
63 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
64 return
65 fi
Andrew Boie6905e212013-12-19 13:21:46 -080066 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070067 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080068}
69
70# check to see if the supplied product is one we can build
71function check_product()
72{
73 T=$(gettop)
74 if [ ! "$T" ]; then
75 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
76 return
77 fi
Ricardo Cerqueirad0734b12011-11-25 15:30:36 +000078
noname55804a6269c52015-01-11 18:16:16 +010079 if (echo -n $1 | grep -q -e "^ch_") ; then
80 CH_BUILD=$(echo -n $1 | sed -e 's/^ch_//g')
81 export BUILD_NUMBER=$((date +%s%N ; echo $CH_BUILD; hostname) | openssl sha1 | sed -e 's/.*=//g; s/ //g' | cut -c1-10)
Ricardo Cerqueirad0734b12011-11-25 15:30:36 +000082 else
noname55804a6269c52015-01-11 18:16:16 +010083 CH_BUILD=
Ricardo Cerqueirad0734b12011-11-25 15:30:36 +000084 fi
noname55804a6269c52015-01-11 18:16:16 +010085 export CH_BUILD
Ricardo Cerqueirad0734b12011-11-25 15:30:36 +000086
Jeff Browne33ba4c2011-07-11 22:11:46 -070087 TARGET_PRODUCT=$1 \
88 TARGET_BUILD_VARIANT= \
89 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070090 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080091 get_build_var TARGET_DEVICE > /dev/null
92 # hide successful answers, but allow the errors to show
93}
94
95VARIANT_CHOICES=(user userdebug eng)
96
97# check to see if the supplied variant is valid
98function check_variant()
99{
100 for v in ${VARIANT_CHOICES[@]}
101 do
102 if [ "$v" = "$1" ]
103 then
104 return 0
105 fi
106 done
107 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108}
109
110function setpaths()
111{
112 T=$(gettop)
113 if [ ! "$T" ]; then
114 echo "Couldn't locate the top of the tree. Try setting TOP."
115 return
116 fi
117
118 ##################################################################
119 # #
120 # Read me before you modify this code #
121 # #
122 # This function sets ANDROID_BUILD_PATHS to what it is adding #
123 # to PATH, and the next time it is run, it removes that from #
124 # PATH. This is required so lunch can be run more than once #
125 # and still have working paths. #
126 # #
127 ##################################################################
128
Raphael Mollc639c782011-06-20 17:25:01 -0700129 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
130 # due to "C:\Program Files" being in the path.
131
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700132 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700133 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700134 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
135 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700136 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700137 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800138 # strip leading ':', if any
139 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500140 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700141
142 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700143 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700144 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700145
Ben Cheng8bc4c432012-11-16 13:29:13 -0800146 # defined in core/config.mk
147 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700148 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800149 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800150
Raphael Mollc639c782011-06-20 17:25:01 -0700151 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800152 export ANDROID_TOOLCHAIN=
153 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200154 local ARCH=$(get_build_var TARGET_ARCH)
155 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400156 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700157 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400158 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
159 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800160 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200161 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800162 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700163 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700164 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700165 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000166 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200167 *)
168 echo "Can't find toolchain for unknown architecture: $ARCH"
169 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700170 ;;
171 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700172 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800173 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700174 fi
Raphael732936d2011-06-22 14:35:32 -0700175
Ben Chengfba67bf2014-02-25 10:27:07 -0800176 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
177 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
178 fi
179
180 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700181 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700182 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800183 # Legacy toolchain configuration used for ARM kernel compilation
Ben Cheng8bc4c432012-11-16 13:29:13 -0800184 toolchaindir=arm/arm-eabi-$targetgccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700185 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100186 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
187 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700188 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700189 ;;
190 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700191 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700192 ;;
193 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700194
Ying Wang564f9122013-03-29 17:40:14 -0700195 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Ying Wang2a859d52014-06-30 10:25:33 -0700196 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 +0200197
198 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
199 # to ensure that the corresponding 'emulator' binaries are used.
200 case $(uname -s) in
201 Darwin)
202 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
203 ;;
204 Linux)
205 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
206 ;;
207 *)
208 ANDROID_EMULATOR_PREBUILTS=
209 ;;
210 esac
211 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700212 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200213 export ANDROID_EMULATOR_PREBUILTS
214 fi
215
Ying Wangaa1c9b52012-11-26 20:51:59 -0800216 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800217
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500218 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900219 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500220 if [ -n "$JAVA_HOME" ]; then
221 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900222 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
223 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500224 fi
225
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800226 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700227 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
228 export OUT=$ANDROID_PRODUCT_OUT
229
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700230 unset ANDROID_HOST_OUT
231 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
232
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800233 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700234 # TODO: fix the path
235 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
236}
237
238function printconfig()
239{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800240 T=$(gettop)
241 if [ ! "$T" ]; then
242 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
243 return
244 fi
245 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700246}
247
248function set_stuff_for_environment()
249{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800250 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500251 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800252 setpaths
253 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700254
Ben Chengaac3f812013-08-13 14:38:15 -0700255 # With this environment variable new GCC can apply colors to warnings/errors
256 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700257}
258
259function set_sequence_number()
260{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700261 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700262}
263
264function settitle()
265{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800266 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700267 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700268 local product=$TARGET_PRODUCT
269 local variant=$TARGET_BUILD_VARIANT
270 local apps=$TARGET_BUILD_APPS
Steve Kondikdf3a7d22012-12-27 15:28:34 -0800271 if [ -z "$PROMPT_COMMAND" ]; then
272 # No prompts
273 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
274 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
275 # Prompts exist, but no hardstatus
276 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700277 fi
Steve Kondikdf3a7d22012-12-27 15:28:34 -0800278 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
Christopher Laisf706ea82013-06-03 15:15:39 -0500279 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
Steve Kondikdf3a7d22012-12-27 15:28:34 -0800280 fi
281
282 if [ -z "$apps" ]; then
283 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
284 else
285 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
286 fi
287 export ANDROID_PROMPT_PREFIX
288
289 # Inject build data into hardstatus
290 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 -0800291 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700292}
293
Kyle Ladd62f2c432013-09-11 20:43:42 -0400294function check_bash_version()
Kenny Root52aa81c2011-07-15 11:07:06 -0700295{
Kenny Root52aa81c2011-07-15 11:07:06 -0700296 # Keep us from trying to run in something that isn't bash.
297 if [ -z "${BASH_VERSION}" ]; then
Kyle Ladd62f2c432013-09-11 20:43:42 -0400298 return 1
Kenny Root52aa81c2011-07-15 11:07:06 -0700299 fi
300
301 # Keep us from trying to run in bash that's too old.
James Roberts-Thomson3fcbb622013-04-16 15:53:39 +1200302 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
Kyle Ladd62f2c432013-09-11 20:43:42 -0400303 return 2
Kenny Root52aa81c2011-07-15 11:07:06 -0700304 fi
305
Kyle Ladd62f2c432013-09-11 20:43:42 -0400306 return 0
Kenny Root52aa81c2011-07-15 11:07:06 -0700307}
308
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700309function choosetype()
310{
311 echo "Build type choices are:"
312 echo " 1. release"
313 echo " 2. debug"
314 echo
315
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700317 DEFAULT_NUM=1
318 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700319
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800320 export TARGET_BUILD_TYPE=
321 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322 while [ -z $TARGET_BUILD_TYPE ]
323 do
324 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 if [ -z "$1" ] ; then
326 read ANSWER
327 else
328 echo $1
329 ANSWER=$1
330 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700331 case $ANSWER in
332 "")
333 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
334 ;;
335 1)
336 export TARGET_BUILD_TYPE=release
337 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800338 release)
339 export TARGET_BUILD_TYPE=release
340 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700341 2)
342 export TARGET_BUILD_TYPE=debug
343 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800344 debug)
345 export TARGET_BUILD_TYPE=debug
346 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700347 *)
348 echo
349 echo "I didn't understand your response. Please try again."
350 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700351 ;;
352 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800353 if [ -n "$1" ] ; then
354 break
355 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700356 done
357
358 set_stuff_for_environment
359}
360
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800361#
362# This function isn't really right: It chooses a TARGET_PRODUCT
363# based on the list of boards. Usually, that gets you something
364# that kinda works with a generic product, but really, you should
365# pick a product by name.
366#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700367function chooseproduct()
368{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700369 if [ "x$TARGET_PRODUCT" != x ] ; then
370 default_value=$TARGET_PRODUCT
371 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700372 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700373 fi
374
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800375 export TARGET_PRODUCT=
376 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700377 while [ -z "$TARGET_PRODUCT" ]
378 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700379 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800380 if [ -z "$1" ] ; then
381 read ANSWER
382 else
383 echo $1
384 ANSWER=$1
385 fi
386
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700387 if [ -z "$ANSWER" ] ; then
388 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 else
390 if check_product $ANSWER
391 then
392 export TARGET_PRODUCT=$ANSWER
393 else
394 echo "** Not a valid product: $ANSWER"
395 fi
396 fi
397 if [ -n "$1" ] ; then
398 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700399 fi
400 done
401
402 set_stuff_for_environment
403}
404
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405function choosevariant()
406{
407 echo "Variant choices are:"
408 local index=1
409 local v
410 for v in ${VARIANT_CHOICES[@]}
411 do
412 # The product name is the name of the directory containing
413 # the makefile we found, above.
414 echo " $index. $v"
415 index=$(($index+1))
416 done
417
418 local default_value=eng
419 local ANSWER
420
421 export TARGET_BUILD_VARIANT=
422 while [ -z "$TARGET_BUILD_VARIANT" ]
423 do
424 echo -n "Which would you like? [$default_value] "
425 if [ -z "$1" ] ; then
426 read ANSWER
427 else
428 echo $1
429 ANSWER=$1
430 fi
431
432 if [ -z "$ANSWER" ] ; then
433 export TARGET_BUILD_VARIANT=$default_value
434 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
435 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800436 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437 fi
438 else
439 if check_variant $ANSWER
440 then
441 export TARGET_BUILD_VARIANT=$ANSWER
442 else
443 echo "** Not a valid variant: $ANSWER"
444 fi
445 fi
446 if [ -n "$1" ] ; then
447 break
448 fi
449 done
450}
451
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700452function choosecombo()
453{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700454 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455
456 echo
457 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700458 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700459
460 echo
461 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700462 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463
464 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800466 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467}
468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469# Clear this variable. It will be built up again when the vendorsetup.sh
470# files are included at the end of this file.
471unset LUNCH_MENU_CHOICES
472function add_lunch_combo()
473{
474 local new_combo=$1
475 local c
476 for c in ${LUNCH_MENU_CHOICES[@]} ; do
477 if [ "$new_combo" = "$c" ] ; then
478 return
479 fi
480 done
481 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
482}
483
484# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700485add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800486add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000487add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800488add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000489add_lunch_combo aosp_x86-eng
490add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492function print_lunch_menu()
493{
494 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 echo
496 echo "You're building on" $uname
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000497 if [ "$(uname)" = "Darwin" ] ; then
498 echo " (ohai, koush!)"
499 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700500 echo
noname55804a6269c52015-01-11 18:16:16 +0100501 if [ "z${CH_DEVICES_ONLY}" != "z" ]; then
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000502 echo "Breakfast menu... pick a combo:"
503 else
504 echo "Lunch menu... pick a combo:"
505 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800506
507 local i=1
508 local choice
509 for choice in ${LUNCH_MENU_CHOICES[@]}
510 do
cybojenix3f7599d2013-06-28 20:30:00 +0100511 echo " $i. $choice "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 i=$(($i+1))
cybojenix3f7599d2013-06-28 20:30:00 +0100513 done | column
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800514
noname55804a6269c52015-01-11 18:16:16 +0100515 if [ "z${CH_DEVICES_ONLY}" != "z" ]; then
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000516 echo "... and don't forget the bacon!"
517 fi
518
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700519 echo
520}
521
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000522function brunch()
523{
524 breakfast $*
525 if [ $? -eq 0 ]; then
526 mka bacon
527 else
528 echo "No such item in brunch menu. Try 'breakfast'"
529 return 1
530 fi
531 return $?
532}
533
534function breakfast()
535{
536 target=$1
JustArchi73db70d2014-06-22 14:37:30 +0200537 local variant=$2
noname55804a6269c52015-01-11 18:16:16 +0100538 CH_DEVICES_ONLY="true"
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000539 unset LUNCH_MENU_CHOICES
540 add_lunch_combo full-eng
noname55804c59d9cf2015-01-09 17:04:40 +0100541 for f in `/bin/ls vendor/ch/vendorsetup.sh 2> /dev/null`
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000542 do
543 echo "including $f"
544 . $f
545 done
546 unset f
547
548 if [ $# -eq 0 ]; then
549 # No arguments, so let's have the full menu
550 lunch
551 else
552 echo "z$target" | grep -q "-"
553 if [ $? -eq 0 ]; then
554 # A buildtype was specified, assume a full device name
555 lunch $target
556 else
noname55804a6269c52015-01-11 18:16:16 +0100557 # This is probably just the CH model name
JustArchi73db70d2014-06-22 14:37:30 +0200558 if [ -z "$variant" ]; then
559 variant="userdebug"
560 fi
noname55804a6269c52015-01-11 18:16:16 +0100561 lunch ch_$target-$variant
Ricardo Cerqueiraa2c8fa92011-01-31 00:49:49 +0000562 fi
563 fi
564 return $?
565}
566
567alias bib=breakfast
568
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700569function lunch()
570{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800571 local answer
572
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700573 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800574 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700575 else
576 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700577 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 fi
580
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581 local selection=
582
583 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700585 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
587 then
588 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
589 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800590 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800591 fi
592 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
593 then
594 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595 fi
596
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 if [ -z "$selection" ]
598 then
599 echo
600 echo "Invalid lunch combo: $answer"
601 return 1
602 fi
603
Joe Onoratoda12daf2010-06-09 18:18:31 -0700604 export TARGET_BUILD_APPS=
605
Jeff Browne33ba4c2011-07-11 22:11:46 -0700606 local product=$(echo -n $selection | sed -e "s/-.*$//")
607 check_product $product
608 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800609 then
Koushik Dutta05cc1b12011-11-26 18:51:42 -0800610 # if we can't find a product, try to grab it off the CM github
611 T=$(gettop)
612 pushd $T > /dev/null
613 build/tools/roomservice.py $product
614 popd > /dev/null
615 check_product $product
Diogo Ferreira552b0432012-03-18 21:18:29 +0000616 else
617 build/tools/roomservice.py $product true
Koushik Dutta05cc1b12011-11-26 18:51:42 -0800618 fi
619 if [ $? -ne 0 ]
620 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700621 echo
622 echo "** Don't have a product spec for: '$product'"
623 echo "** Do you have the right repo manifest?"
624 product=
625 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626
Jeff Browne33ba4c2011-07-11 22:11:46 -0700627 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
628 check_variant $variant
629 if [ $? -ne 0 ]
630 then
631 echo
632 echo "** Invalid variant: '$variant'"
633 echo "** Must be one of ${VARIANT_CHOICES[@]}"
634 variant=
635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Jeff Browne33ba4c2011-07-11 22:11:46 -0700637 if [ -z "$product" -o -z "$variant" ]
638 then
639 echo
640 return 1
641 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642
Jeff Browne33ba4c2011-07-11 22:11:46 -0700643 export TARGET_PRODUCT=$product
644 export TARGET_BUILD_VARIANT=$variant
645 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700646
647 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648
Chirayu Desai917fb7e2013-06-30 10:04:25 +0530649 fixup_common_out_dir
650
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800652 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700653}
654
Jeff Davidson513d7a42010-08-02 10:00:44 -0700655# Tab completion for lunch.
656function _lunch()
657{
658 local cur prev opts
659 COMPREPLY=()
660 cur="${COMP_WORDS[COMP_CWORD]}"
661 prev="${COMP_WORDS[COMP_CWORD-1]}"
662
663 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
664 return 0
665}
Emilio López04511402013-11-03 13:02:13 -0300666complete -F _lunch lunch 2>/dev/null
Jeff Davidson513d7a42010-08-02 10:00:44 -0700667
Joe Onoratoda12daf2010-06-09 18:18:31 -0700668# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700669# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700670function tapas()
671{
Ying Wangb541ab62014-05-29 17:57:40 -0700672 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 -0700673 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ying Wangb541ab62014-05-29 17:57:40 -0700674 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700675
Ying Wang67f02922012-08-22 10:25:20 -0700676 if [ $(echo $arch | wc -w) -gt 1 ]; then
677 echo "tapas: Error: Multiple build archs supplied: $arch"
678 return
679 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700680 if [ $(echo $variant | wc -w) -gt 1 ]; then
681 echo "tapas: Error: Multiple build variants supplied: $variant"
682 return
683 fi
Ying Wang67f02922012-08-22 10:25:20 -0700684
685 local product=full
686 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700687 x86) product=full_x86;;
688 mips) product=full_mips;;
689 armv5) product=generic_armv5;;
690 arm64) product=aosp_arm64;;
691 x86_64) product=aosp_x86_64;;
692 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700693 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700694 if [ -z "$variant" ]; then
695 variant=eng
696 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700697 if [ -z "$apps" ]; then
698 apps=all
699 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700700
Ying Wang67f02922012-08-22 10:25:20 -0700701 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700702 export TARGET_BUILD_VARIANT=$variant
Joe Onoratoda12daf2010-06-09 18:18:31 -0700703 export TARGET_BUILD_TYPE=release
704 export TARGET_BUILD_APPS=$apps
705
706 set_stuff_for_environment
707 printconfig
708}
709
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100710function eat()
711{
712 if [ "$OUT" ] ; then
noname55804a6269c52015-01-11 18:16:16 +0100713 MODVERSION=$(get_build_var CH_VERSION)
714 ZIPFILE=ch-$MODVERSION.zip
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100715 ZIPPATH=$OUT/$ZIPFILE
716 if [ ! -f $ZIPPATH ] ; then
717 echo "Nothing to eat"
718 return 1
719 fi
720 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
721 if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
722 echo "No device is online. Waiting for one..."
723 echo "Please connect USB and/or enable USB debugging"
724 until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
725 sleep 1
726 done
727 echo "Device Found.."
728 fi
noname55804a6269c52015-01-11 18:16:16 +0100729 if (adb shell cat /system/build.prop | grep -q "ro.ch.device=$CH_BUILD");
Chirayu Desai028d1022012-12-26 10:53:58 +0530730 then
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100731 # if adbd isn't root we can't write to /cache/recovery/
732 adb root
733 sleep 1
734 adb wait-for-device
Steve Kondikbc4c8ac2013-04-13 07:19:52 -0700735 cat << EOF > /tmp/command
736--sideload
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100737EOF
Steve Kondikbc4c8ac2013-04-13 07:19:52 -0700738 if adb push /tmp/command /cache/recovery/ ; then
739 echo "Rebooting into recovery for sideload installation"
740 adb reboot recovery
741 adb wait-for-sideload
742 adb sideload $ZIPPATH
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100743 fi
Steve Kondikbc4c8ac2013-04-13 07:19:52 -0700744 rm /tmp/command
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100745 else
746 echo "Nothing to eat"
747 return 1
748 fi
749 return $?
Chirayu Desai028d1022012-12-26 10:53:58 +0530750 else
noname55804a6269c52015-01-11 18:16:16 +0100751 echo "The connected device does not appear to be $CH_BUILD, run away!"
Chirayu Desai028d1022012-12-26 10:53:58 +0530752 fi
Ricardo Cerqueira16134f02011-08-19 20:37:12 +0100753}
754
Nebojsa Cvetkovic2b19eb42012-11-09 23:02:54 +0000755function omnom
756{
757 brunch $*
758 eat
759}
760
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761function gettop
762{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800763 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700765 # The following circumlocution ensures we remove symlinks from TOP.
766 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 else
768 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800769 # The following circumlocution (repeated below as well) ensures
770 # that we record the true directory name and not one that is
771 # faked up with symlink names.
772 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700773 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800774 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700775 T=
776 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800777 \cd ..
synergyb112a402013-07-05 19:47:47 -0700778 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700779 done
Ying Wang9cd17642012-12-13 10:52:07 -0800780 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700781 if [ -f "$T/$TOPFILE" ]; then
782 echo $T
783 fi
784 fi
785 fi
786}
787
Andrew Hsieh906cb782013-09-10 17:37:14 +0800788# Return driver for "make", if any (eg. static analyzer)
789function getdriver()
790{
791 local T="$1"
792 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
793 if [ -n "$WITH_STATIC_ANALYZER" ]; then
794 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800795$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
796--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800797--status-bugs \
798--top=$T"
799 fi
800}
801
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700802function m()
803{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800804 local T=$(gettop)
805 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700806 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800807 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700808 else
809 echo "Couldn't locate the top of the tree. Try setting TOP."
810 fi
811}
812
813function findmakefile()
814{
815 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800816 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700817 T=
818 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700819 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700820 if [ -f "$T/Android.mk" ]; then
821 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800822 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700823 return
824 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800825 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826 done
Ying Wang9cd17642012-12-13 10:52:07 -0800827 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700828}
829
830function mm()
831{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800832 local T=$(gettop)
833 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700834 # If we're sitting in the root of the build tree, just do a
835 # normal make.
836 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800837 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700838 else
839 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800840 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700841 local MODULES=
842 local GET_INSTALL_PATH=
843 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700844 # Remove the path to top as the makefilepath needs to be relative
845 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700846 if [ ! "$T" ]; then
847 echo "Couldn't locate the top of the tree. Try setting TOP."
848 elif [ ! "$M" ]; then
849 echo "Couldn't locate a makefile from the current directory."
850 else
Ying Wanga7deb082013-08-16 13:24:47 -0700851 for ARG in $@; do
852 case $ARG in
853 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
854 esac
855 done
856 if [ -n "$GET_INSTALL_PATH" ]; then
857 MODULES=
858 ARGS=GET-INSTALL-PATH
859 else
860 MODULES=all_modules
861 ARGS=$@
862 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700863 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 -0700864 fi
865 fi
866}
867
868function mmm()
869{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800870 local T=$(gettop)
871 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700872 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800873 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800874 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800875 local ARGS=
876 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700877 local GET_INSTALL_PATH=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800878 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
879 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
880 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800881 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
882 if [ "$MODULES" = "" ]; then
883 MODULES=all_modules
884 fi
885 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700886 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700887 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
888 local TO_CHOP=`expr $TO_CHOP + 1`
889 local START=`PWD= /bin/pwd`
890 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700891 if [ "$MFILE" = "" ] ; then
892 MFILE=$DIR/Android.mk
893 else
894 MFILE=$MFILE/$DIR/Android.mk
895 fi
896 MAKEFILE="$MAKEFILE $MFILE"
897 else
Ying Wanga7deb082013-08-16 13:24:47 -0700898 case $DIR in
899 showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
900 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
901 *) echo "No Android.mk in $DIR."; return 1;;
902 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700903 fi
904 done
Ying Wanga7deb082013-08-16 13:24:47 -0700905 if [ -n "$GET_INSTALL_PATH" ]; then
906 ARGS=$GET_INSTALL_PATH
907 MODULES=
908 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800909 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 -0700910 else
911 echo "Couldn't locate the top of the tree. Try setting TOP."
912 fi
913}
914
Ying Wangb607f7b2013-02-08 18:01:04 -0800915function mma()
916{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800917 local T=$(gettop)
918 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800919 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800920 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800921 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800922 if [ ! "$T" ]; then
923 echo "Couldn't locate the top of the tree. Try setting TOP."
924 fi
925 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800926 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800927 fi
928}
929
930function mmma()
931{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800932 local T=$(gettop)
933 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800934 if [ "$T" ]; then
935 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
936 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
937 local MY_PWD=`PWD= /bin/pwd`
938 if [ "$MY_PWD" = "$T" ]; then
939 MY_PWD=
940 else
941 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
942 fi
943 local DIR=
944 local MODULE_PATHS=
945 local ARGS=
946 for DIR in $DIRS ; do
947 if [ -d $DIR ]; then
948 if [ "$MY_PWD" = "" ]; then
949 MODULE_PATHS="$MODULE_PATHS $DIR"
950 else
951 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
952 fi
953 else
954 case $DIR in
955 showcommands | snod | dist | incrementaljavac) ARGS="$ARGS $DIR";;
956 *) echo "Couldn't find directory $DIR"; return 1;;
957 esac
958 fi
959 done
Andrew Hsieh906cb782013-09-10 17:37:14 +0800960 $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 -0800961 else
962 echo "Couldn't locate the top of the tree. Try setting TOP."
963 fi
964}
965
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700966function croot()
967{
968 T=$(gettop)
969 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800970 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700971 else
972 echo "Couldn't locate the top of the tree. Try setting TOP."
973 fi
974}
975
nebkat1f103db2012-12-28 10:40:45 +0000976function cout()
977{
978 if [ "$OUT" ]; then
979 cd $OUT
980 else
981 echo "Couldn't locate out directory. Try setting OUT."
982 fi
983}
984
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700985function cproj()
986{
987 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700988 local HERE=$PWD
989 T=
990 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
991 T=$PWD
992 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800993 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700994 return
995 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800996 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700997 done
Ying Wang9cd17642012-12-13 10:52:07 -0800998 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700999 echo "can't find Android.mk"
1000}
1001
Daniel Sandler47e0a882013-07-30 13:23:52 -04001002# simplified version of ps; output in the form
1003# <pid> <procname>
1004function qpid() {
1005 local prepend=''
1006 local append=''
1007 if [ "$1" = "--exact" ]; then
1008 prepend=' '
1009 append='$'
1010 shift
1011 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1012 echo "usage: qpid [[--exact] <process name|pid>"
1013 return 255
1014 fi
1015
1016 local EXE="$1"
1017 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001018 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001019 else
1020 adb shell ps \
1021 | tr -d '\r' \
1022 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1023 fi
1024}
1025
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001026function pid()
1027{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001028 local prepend=''
1029 local append=''
1030 if [ "$1" = "--exact" ]; then
1031 prepend=' '
1032 append='$'
1033 shift
1034 fi
1035 local EXE="$1"
1036 if [ "$EXE" ] ; then
1037 local PID=`adb shell ps \
1038 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001039 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001040 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1041 echo "$PID"
1042 else
1043 echo "usage: pid [--exact] <process name>"
1044 return 255
1045 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001046}
1047
Christopher Tate744ee802009-11-12 15:33:08 -08001048# systemstack - dump the current stack trace of all threads in the system process
1049# to the usual ANR traces file
1050function systemstack()
1051{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001052 stacks system_server
1053}
1054
1055function stacks()
1056{
1057 if [[ $1 =~ ^[0-9]+$ ]] ; then
1058 local PID="$1"
1059 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001060 local PIDLIST="$(pid $1)"
1061 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1062 local PID="$PIDLIST"
1063 elif [ "$PIDLIST" ] ; then
1064 echo "more than one process: $1"
1065 else
1066 echo "no such process: $1"
1067 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001068 else
1069 echo "usage: stacks [pid|process name]"
1070 fi
1071
1072 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001073 # Determine whether the process is native
1074 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1075 # Dump stacks of Dalvik process
1076 local TRACES=/data/anr/traces.txt
1077 local ORIG=/data/anr/traces.orig
1078 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001079
Jeff Brownb12c2e52013-08-19 15:14:16 -07001080 # Keep original traces to avoid clobbering
1081 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001082
Jeff Brownb12c2e52013-08-19 15:14:16 -07001083 # Make sure we have a usable file
1084 adb shell touch $TRACES
1085 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001086
Jeff Brownb12c2e52013-08-19 15:14:16 -07001087 # Dump stacks and wait for dump to finish
1088 adb shell kill -3 $PID
1089 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001090
Jeff Brownb12c2e52013-08-19 15:14:16 -07001091 # Restore original stacks, and show current output
1092 adb shell mv $TRACES $TMP
1093 adb shell mv $ORIG $TRACES
1094 adb shell cat $TMP
1095 else
1096 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001097 local USE64BIT="$(is64bit $PID)"
1098 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001099 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001100 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001101}
1102
John Michelau50200252012-11-09 11:48:04 -06001103function gdbwrapper()
1104{
Ben Chengfba67bf2014-02-25 10:27:07 -08001105 local GDB_CMD="$1"
1106 shift 1
1107 $GDB_CMD -x "$@"
John Michelau50200252012-11-09 11:48:04 -06001108}
1109
Brigid Smith0a2712d2014-05-28 14:36:43 -07001110function get_symbols_directory()
1111{
1112 echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1113}
1114
Michael Wrightaeed7212014-06-19 19:58:12 -07001115# Read the ELF header from /proc/$PID/exe to determine if the process is
1116# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001117function is64bit()
1118{
1119 local PID="$1"
1120 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001121 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001122 echo "64"
1123 else
1124 echo ""
1125 fi
1126 else
1127 echo ""
1128 fi
1129}
1130
1131# gdbclient now determines whether the user wants to debug a 32-bit or 64-bit
1132# executable, set up the approriate gdbserver, then invokes the proper host
1133# gdb.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134function gdbclient()
1135{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001136 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1137 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1138 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
Colin Cross3655a682014-05-22 11:59:10 -07001139 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
Brigid Smith0a2712d2014-05-28 14:36:43 -07001140 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001141 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001142 local ARCH=$(get_build_var TARGET_ARCH)
1143 local GDB
1144 case "$ARCH" in
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001145 arm) GDB=arm-linux-androideabi-gdb;;
Ben Chengfba67bf2014-02-25 10:27:07 -08001146 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
Duane Sand3c4fcd82014-07-22 14:34:00 -07001147 mips|mips64) GDB=mips64el-linux-android-gdb;;
Serban Constantinescu9b68fb22014-01-14 10:33:53 +00001148 x86) GDB=x86_64-linux-android-gdb;;
1149 x86_64) GDB=x86_64-linux-android-gdb;;
Nick Kralevich0ab21d32011-11-11 09:02:01 -08001150 *) echo "Unknown arch $ARCH"; return 1;;
1151 esac
1152
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1154 local EXE="$1"
1155 if [ "$EXE" ] ; then
1156 EXE=$1
Brigid Smith7a569a62014-06-20 14:12:12 -07001157 if [[ $EXE =~ ^[^/].* ]] ; then
1158 EXE="system/bin/"$EXE
1159 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001160 else
1161 EXE="app_process"
1162 fi
1163
1164 local PORT="$2"
1165 if [ "$PORT" ] ; then
1166 PORT=$2
1167 else
1168 PORT=":5039"
1169 fi
1170
Chris Craik20c136a2012-11-09 18:02:19 -08001171 local PID="$3"
1172 if [ "$PID" ] ; then
1173 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
Grace Klobae9d04bf2011-04-30 11:04:05 -07001174 PID=`pid $3`
Chris Craik20c136a2012-11-09 18:02:19 -08001175 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1176 # that likely didn't work because of returning multiple processes
1177 # try again, filtering by root processes (don't contain colon)
Mathias Agopian44583272013-08-27 14:15:50 -07001178 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
Chris Craik20c136a2012-11-09 18:02:19 -08001179 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1180 then
1181 echo "Couldn't resolve '$3' to single PID"
1182 return 1
1183 else
1184 echo ""
1185 echo "WARNING: multiple processes matching '$3' observed, using root process"
1186 echo ""
1187 fi
1188 fi
Grace Klobae9d04bf2011-04-30 11:04:05 -07001189 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190 adb forward "tcp$PORT" "tcp$PORT"
Ben Chengfba67bf2014-02-25 10:27:07 -08001191 local USE64BIT="$(is64bit $PID)"
1192 adb shell gdbserver$USE64BIT $PORT --attach $PID &
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193 sleep 2
1194 else
1195 echo ""
1196 echo "If you haven't done so already, do this first on the device:"
1197 echo " gdbserver $PORT /system/bin/$EXE"
1198 echo " or"
Chris Craik20c136a2012-11-09 18:02:19 -08001199 echo " gdbserver $PORT --attach <PID>"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 echo ""
1201 fi
1202
Colin Cross84480ad2014-04-10 12:51:39 -07001203 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
Colin Crossa58f8e02014-06-20 13:33:36 -07001204 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
Colin Cross84480ad2014-04-10 12:51:39 -07001205
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
Colin Cross3655a682014-05-22 11:59:10 -07001207 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
Ben Cheng72398162013-06-24 14:36:21 -07001208 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001209 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
Christopher Ferris855d27f2014-10-03 17:29:27 -07001210 # Enable special debugging for ART processes.
1211 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1212 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1213 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001214 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1215
Ben Chengfba67bf2014-02-25 10:27:07 -08001216 local WHICH_GDB=
1217 # 64-bit exe found
1218 if [ "$USE64BIT" != "" ] ; then
1219 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1220 # 32-bit exe / 32-bit platform
1221 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1222 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1223 # 32-bit exe / 64-bit platform
1224 else
1225 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1226 fi
Brigid Smith0a2712d2014-05-28 14:36:43 -07001227
Ben Chengfba67bf2014-02-25 10:27:07 -08001228 gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001229 else
1230 echo "Unable to determine build system output dir."
1231 fi
1232
1233}
1234
Clark Scheffb9dc4372014-12-23 13:30:51 -08001235function dddclient()
1236{
1237 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1238 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1239 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1240 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1241 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1242 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
1243 local ARCH=$(get_build_var TARGET_ARCH)
1244 local GDB
1245 case "$ARCH" in
1246 arm) GDB=arm-linux-androideabi-gdb;;
1247 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1248 mips|mips64) GDB=mips64el-linux-android-gdb;;
1249 x86) GDB=x86_64-linux-android-gdb;;
1250 x86_64) GDB=x86_64-linux-android-gdb;;
1251 *) echo "Unknown arch $ARCH"; return 1;;
1252 esac
1253
1254 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1255 local EXE="$1"
1256 if [ "$EXE" ] ; then
1257 EXE=$1
1258 if [[ $EXE =~ ^[^/].* ]] ; then
1259 EXE="system/bin/"$EXE
1260 fi
1261 else
1262 EXE="app_process"
1263 fi
1264
1265 local PORT="$2"
1266 if [ "$PORT" ] ; then
1267 PORT=$2
1268 else
1269 PORT=":5039"
1270 fi
1271
1272 local PID="$3"
1273 if [ "$PID" ] ; then
1274 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1275 PID=`pid $3`
1276 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1277 # that likely didn't work because of returning multiple processes
1278 # try again, filtering by root processes (don't contain colon)
1279 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1280 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1281 then
1282 echo "Couldn't resolve '$3' to single PID"
1283 return 1
1284 else
1285 echo ""
1286 echo "WARNING: multiple processes matching '$3' observed, using root process"
1287 echo ""
1288 fi
1289 fi
1290 fi
1291 adb forward "tcp$PORT" "tcp$PORT"
1292 local USE64BIT="$(is64bit $PID)"
1293 adb shell gdbserver$USE64BIT $PORT --attach $PID &
1294 sleep 2
1295 else
1296 echo ""
1297 echo "If you haven't done so already, do this first on the device:"
1298 echo " gdbserver $PORT /system/bin/$EXE"
1299 echo " or"
1300 echo " gdbserver $PORT --attach <PID>"
1301 echo ""
1302 fi
1303
1304 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1305 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1306
1307 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1308 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
1309 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1310 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1311 # Enable special debugging for ART processes.
1312 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1313 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1314 fi
1315 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1316
1317 local WHICH_GDB=
1318 # 64-bit exe found
1319 if [ "$USE64BIT" != "" ] ; then
1320 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1321 # 32-bit exe / 32-bit platform
1322 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1323 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1324 # 32-bit exe / 64-bit platform
1325 else
1326 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1327 fi
1328
1329 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1330 else
1331 echo "Unable to determine build system output dir."
1332 fi
1333}
1334
1335
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001336case `uname -s` in
1337 Darwin)
1338 function sgrep()
1339 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001340 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 -07001341 }
1342
1343 ;;
1344 *)
1345 function sgrep()
1346 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001347 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 -07001348 }
1349 ;;
1350esac
1351
Raghu Gandham8da43102012-07-25 19:57:22 -07001352function gettargetarch
1353{
1354 get_build_var TARGET_ARCH
1355}
1356
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001357function ggrep()
1358{
1359 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1360}
1361
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001362function jgrep()
1363{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001364 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 -07001365}
1366
1367function cgrep()
1368{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001369 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' \) -print0 | xargs -0 grep --color -n "$@"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001370}
1371
1372function resgrep()
1373{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001374 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 -07001375}
1376
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001377function mangrep()
1378{
1379 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1380}
1381
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001382function sepgrep()
1383{
1384 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 "$@"
1385}
1386
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001387case `uname -s` in
1388 Darwin)
1389 function mgrep()
1390 {
Ying Wang324c2b22012-08-17 15:03:20 -07001391 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 -07001392 }
1393
1394 function treegrep()
1395 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001396 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 -07001397 }
1398
1399 ;;
1400 *)
1401 function mgrep()
1402 {
Ying Wang324c2b22012-08-17 15:03:20 -07001403 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 -07001404 }
1405
1406 function treegrep()
1407 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001408 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 -07001409 }
1410
1411 ;;
1412esac
1413
1414function getprebuilt
1415{
1416 get_abs_build_var ANDROID_PREBUILTS
1417}
1418
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419function tracedmdump()
1420{
1421 T=$(gettop)
1422 if [ ! "$T" ]; then
1423 echo "Couldn't locate the top of the tree. Try setting TOP."
1424 return
1425 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001426 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001427 local arch=$(gettargetarch)
1428 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001430 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001431 if [ ! "$TRACE" ] ; then
1432 echo "usage: tracedmdump tracename"
1433 return
1434 fi
1435
Jack Veenstra60116fc2009-04-09 18:12:34 -07001436 if [ ! -r "$KERNEL" ] ; then
1437 echo "Error: cannot find kernel: '$KERNEL'"
1438 return
1439 fi
1440
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001441 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001442 if [ "$BASETRACE" = "$TRACE" ] ; then
1443 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1444 fi
1445
1446 echo "post-processing traces..."
1447 rm -f $TRACE/qtrace.dexlist
1448 post_trace $TRACE
1449 if [ $? -ne 0 ]; then
1450 echo "***"
1451 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1452 echo "***"
1453 return
1454 fi
1455 echo "generating dexlist output..."
1456 /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
1457 echo "generating dmtrace data..."
1458 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1459 echo "generating html file..."
1460 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1461 echo "done, see $TRACE/dmtrace.html for details"
1462 echo "or run:"
1463 echo " traceview $TRACE/dmtrace"
1464}
1465
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001466# communicate with a running device or emulator, set up necessary state,
1467# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001468function runhat()
1469{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001470 # process standard adb options
1471 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001472 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001473 adbTarget=$1
1474 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001475 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001476 adbTarget="$1 $2"
1477 shift 2
1478 fi
1479 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001480 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001481
1482 # runhat options
1483 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001484
1485 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001486 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001487 return
1488 fi
1489
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001490 # confirm hat is available
1491 if [ -z $(which hat) ]; then
1492 echo "hat is not available in this configuration."
1493 return
1494 fi
1495
Andy McFaddenb6289852010-07-12 08:00:19 -07001496 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001497 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001498 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001499 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001500 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001501 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001502 echo -n "> "
1503 read
1504
The Android Open Source Project88b60792009-03-03 19:28:42 -08001505 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001506
The Android Open Source Project88b60792009-03-03 19:28:42 -08001507 echo "Retrieving file $devFile..."
1508 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001509
The Android Open Source Project88b60792009-03-03 19:28:42 -08001510 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001511
The Android Open Source Project88b60792009-03-03 19:28:42 -08001512 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001513 echo "View the output by pointing your browser at http://localhost:7000/"
1514 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001515 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001516}
1517
1518function getbugreports()
1519{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001520 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001521
1522 if [ ! "$reports" ]; then
1523 echo "Could not locate any bugreports."
1524 return
1525 fi
1526
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001527 local report
1528 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001529 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001530 echo "/sdcard/bugreports/${report}"
1531 adb pull /sdcard/bugreports/${report} ${report}
1532 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001533 done
1534}
1535
Victoria Lease1b296b42012-08-21 15:44:06 -07001536function getsdcardpath()
1537{
1538 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1539}
1540
1541function getscreenshotpath()
1542{
1543 echo "$(getsdcardpath)/Pictures/Screenshots"
1544}
1545
1546function getlastscreenshot()
1547{
1548 local screenshot_path=$(getscreenshotpath)
1549 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1550 if [ "$screenshot" = "" ]; then
1551 echo "No screenshots found."
1552 return
1553 fi
1554 echo "${screenshot}"
1555 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1556}
1557
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001558function startviewserver()
1559{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001560 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001561 if [ $# -gt 0 ]; then
1562 port=$1
1563 fi
1564 adb shell service call window 1 i32 $port
1565}
1566
1567function stopviewserver()
1568{
1569 adb shell service call window 2
1570}
1571
1572function isviewserverstarted()
1573{
1574 adb shell service call window 3
1575}
1576
Romain Guyb84049a2010-10-04 16:56:11 -07001577function key_home()
1578{
1579 adb shell input keyevent 3
1580}
1581
1582function key_back()
1583{
1584 adb shell input keyevent 4
1585}
1586
1587function key_menu()
1588{
1589 adb shell input keyevent 82
1590}
1591
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001592function smoketest()
1593{
1594 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1595 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1596 return
1597 fi
1598 T=$(gettop)
1599 if [ ! "$T" ]; then
1600 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1601 return
1602 fi
1603
Ying Wang9cd17642012-12-13 10:52:07 -08001604 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001605 adb uninstall com.android.smoketest > /dev/null &&
1606 adb uninstall com.android.smoketest.tests > /dev/null &&
1607 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1608 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1609 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1610}
1611
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001612# simple shortcut to the runtest command
1613function runtest()
1614{
1615 T=$(gettop)
1616 if [ ! "$T" ]; then
1617 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1618 return
1619 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001620 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001621}
1622
The Android Open Source Project88b60792009-03-03 19:28:42 -08001623function godir () {
1624 if [[ -z "$1" ]]; then
1625 echo "Usage: godir <regex>"
1626 return
1627 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001628 T=$(gettop)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001629 if [[ ! -f $T/filelist ]]; then
1630 echo -n "Creating index..."
Ying Wang9cd17642012-12-13 10:52:07 -08001631 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > filelist)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001632 echo " Done"
1633 echo ""
1634 fi
1635 local lines
Jeff Hamilton293f9392011-11-18 17:15:25 -06001636 lines=($(\grep "$1" $T/filelist | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001637 if [[ ${#lines[@]} = 0 ]]; then
1638 echo "Not found"
1639 return
1640 fi
1641 local pathname
1642 local choice
1643 if [[ ${#lines[@]} > 1 ]]; then
1644 while [[ -z "$pathname" ]]; do
1645 local index=1
1646 local line
1647 for line in ${lines[@]}; do
1648 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001649 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001650 done
1651 echo
1652 echo -n "Select one: "
1653 unset choice
1654 read choice
1655 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1656 echo "Invalid choice"
1657 continue
1658 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001659 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001660 done
1661 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001662 pathname=${lines[0]}
1663 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001664 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001665}
1666
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001667function cmremote()
1668{
1669 git remote rm cmremote 2> /dev/null
Chirayu Desaice2cf242014-12-16 18:22:55 +05301670 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001671 if [ -z "$GERRIT_REMOTE" ]
1672 then
Chirayu Desaice2cf242014-12-16 18:22:55 +05301673 echo Unable to set up the git remote, are you under a git repo?
1674 return 0
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001675 fi
Chirayu Desaice2cf242014-12-16 18:22:55 +05301676 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001677 if [ -z "$CMUSER" ]
1678 then
Pawit Pornkitprasanbdfb3c52012-12-11 16:41:07 +07001679 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001680 else
Pawit Pornkitprasanbdfb3c52012-12-11 16:41:07 +07001681 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001682 fi
1683 echo You can now push to "cmremote".
1684}
Koushik Dutta747e2782012-05-14 16:14:36 -07001685
Steve Kondik02854742012-09-17 11:33:18 -07001686function aospremote()
1687{
1688 git remote rm aosp 2> /dev/null
1689 if [ ! -d .git ]
1690 then
1691 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1692 fi
Steve Kondik23009bc2013-11-16 03:48:30 -08001693 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik02854742012-09-17 11:33:18 -07001694 if (echo $PROJECT | grep -qv "^device")
1695 then
1696 PFX="platform/"
1697 fi
1698 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1699 echo "Remote 'aosp' created"
1700}
Steve Kondik02854742012-09-17 11:33:18 -07001701
Steve Kondik996e29a2013-10-27 13:34:36 -07001702function cafremote()
1703{
1704 git remote rm caf 2> /dev/null
1705 if [ ! -d .git ]
1706 then
1707 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1708 fi
Steve Kondik23009bc2013-11-16 03:48:30 -08001709 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik996e29a2013-10-27 13:34:36 -07001710 if (echo $PROJECT | grep -qv "^device")
1711 then
1712 PFX="platform/"
1713 fi
1714 git remote add caf git://codeaurora.org/$PFX$PROJECT
1715 echo "Remote 'caf' created"
1716}
Steve Kondik996e29a2013-10-27 13:34:36 -07001717
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001718function installboot()
1719{
1720 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1721 then
1722 echo "No recovery.fstab found. Build recovery first."
1723 return 1
1724 fi
1725 if [ ! -e "$OUT/boot.img" ];
1726 then
1727 echo "No boot.img found. Run make bootimage first."
1728 return 1
1729 fi
1730 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1731 if [ -z "$PARTITION" ];
1732 then
Ricardo Cerqueirab9278232013-07-27 13:51:56 +01001733 # Try for RECOVERY_FSTAB_VERSION = 2
1734 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1735 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1736 if [ -z "$PARTITION" ];
1737 then
1738 echo "Unable to determine boot partition."
1739 return 1
1740 fi
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001741 fi
1742 adb start-server
Steve Kondikc8b50742013-08-30 17:34:37 -07001743 adb wait-for-online
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001744 adb root
1745 sleep 1
Steve Kondikae38d162013-04-13 13:23:35 -07001746 adb wait-for-online shell mount /system 2>&1 > /dev/null
1747 adb wait-for-online remount
noname558043b9f6752015-01-14 20:11:50 +01001748 if (adb shell cat /system/build.prop | grep -q "ro.ch.device=$CH_BUILD");
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001749 then
1750 adb push $OUT/boot.img /cache/
1751 for i in $OUT/system/lib/modules/*;
1752 do
1753 adb push $i /system/lib/modules/
1754 done
1755 adb shell dd if=/cache/boot.img of=$PARTITION
1756 adb shell chmod 644 /system/lib/modules/*
1757 echo "Installation complete."
1758 else
noname55804a6269c52015-01-11 18:16:16 +01001759 echo "The connected device does not appear to be $CH_BUILD, run away!"
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001760 fi
1761}
1762
Steve Kondikc731b702012-10-24 16:40:42 -07001763function installrecovery()
1764{
1765 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1766 then
1767 echo "No recovery.fstab found. Build recovery first."
1768 return 1
1769 fi
1770 if [ ! -e "$OUT/recovery.img" ];
1771 then
1772 echo "No recovery.img found. Run make recoveryimage first."
1773 return 1
1774 fi
1775 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1776 if [ -z "$PARTITION" ];
1777 then
Steve Kondikf03d3632013-08-09 21:03:42 -07001778 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondik6ee0ecf2013-08-10 21:02:09 -07001779 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1780 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondikf03d3632013-08-09 21:03:42 -07001781 if [ -z "$PARTITION" ];
1782 then
1783 echo "Unable to determine recovery partition."
1784 return 1
1785 fi
Steve Kondikc731b702012-10-24 16:40:42 -07001786 fi
1787 adb start-server
Steve Kondikc8b50742013-08-30 17:34:37 -07001788 adb wait-for-online
Steve Kondikc731b702012-10-24 16:40:42 -07001789 adb root
1790 sleep 1
Steve Kondikae38d162013-04-13 13:23:35 -07001791 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1792 adb wait-for-online remount
noname558043b9f6752015-01-14 20:11:50 +01001793 if (adb shell cat /system/build.prop | grep -q "ro.ch.device=$CH_BUILD");
Steve Kondikc731b702012-10-24 16:40:42 -07001794 then
1795 adb push $OUT/recovery.img /cache/
1796 adb shell dd if=/cache/recovery.img of=$PARTITION
1797 echo "Installation complete."
1798 else
noname55804a6269c52015-01-11 18:16:16 +01001799 echo "The connected device does not appear to be $CH_BUILD, run away!"
Steve Kondikc731b702012-10-24 16:40:42 -07001800 fi
1801}
Steve Kondik4b5ddcb2012-09-23 23:46:55 -07001802
Koushik Dutta747e2782012-05-14 16:14:36 -07001803function makerecipe() {
1804 if [ -z "$1" ]
1805 then
1806 echo "No branch name provided."
1807 return 1
1808 fi
1809 cd android
1810 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1811 git commit -a -m "$1"
1812 cd ..
1813
1814 repo forall -c '
1815
1816 if [ "$REPO_REMOTE" == "github" ]
1817 then
1818 pwd
1819 cmremote
1820 git push cmremote HEAD:refs/heads/'$1'
1821 fi
1822 '
1823}
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001824
1825function cmgerrit() {
1826 if [ $# -eq 0 ]; then
1827 $FUNCNAME help
1828 return 1
1829 fi
Pawit Pornkitprasanbdfb3c52012-12-11 16:41:07 +07001830 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlanda2549212011-11-18 00:45:55 -06001831 local review=`git config --get remote.github.review`
1832 local project=`git config --get remote.github.projectname`
1833 local command=$1
1834 shift
1835 case $command in
1836 help)
1837 if [ $# -eq 0 ]; then
1838 cat <<EOF
1839Usage:
1840 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1841
1842Commands:
1843 fetch Just fetch the change as FETCH_HEAD
1844 help Show this help, or for a specific command
1845 pull Pull a change into current branch
1846 push Push HEAD or a local branch to Gerrit for a specific branch
1847
1848Any other Git commands that support refname would work as:
1849 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1850
1851See '$FUNCNAME help COMMAND' for more information on a specific command.
1852
1853Example:
1854 $FUNCNAME checkout -b topic 1234/5
1855works as:
1856 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1857 && git checkout -b topic FETCH_HEAD
1858will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1859Patch-set 1 will be fetched if omitted.
1860EOF
1861 return
1862 fi
1863 case $1 in
1864 __cmg_*) echo "For internal use only." ;;
1865 changes|for)
1866 if [ "$FUNCNAME" = "cmgerrit" ]; then
1867 echo "'$FUNCNAME $1' is deprecated."
1868 fi
1869 ;;
1870 help) $FUNCNAME help ;;
1871 fetch|pull) cat <<EOF
1872usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1873
1874works as:
1875 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1876 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1877
1878Example:
1879 $FUNCNAME $1 1234
1880will $1 patch-set 1 of change 1234
1881EOF
1882 ;;
1883 push) cat <<EOF
1884usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1885
1886works as:
1887 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1888 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1889
1890Example:
1891 $FUNCNAME push fix6789:gingerbread
1892will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1893HEAD will be pushed from local if omitted.
1894EOF
1895 ;;
1896 *)
1897 $FUNCNAME __cmg_err_not_supported $1 && return
1898 cat <<EOF
1899usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1900
1901works as:
1902 git fetch http://DOMAIN/p/PROJECT \\
1903 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1904 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1905EOF
1906 ;;
1907 esac
1908 ;;
1909 __cmg_get_ref)
1910 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1911 local change_id patchset_id hash
1912 case $1 in
1913 */*)
1914 change_id=${1%%/*}
1915 patchset_id=${1#*/}
1916 ;;
1917 *)
1918 change_id=$1
1919 patchset_id=1
1920 ;;
1921 esac
1922 hash=$(($change_id % 100))
1923 case $hash in
1924 [0-9]) hash="0$hash" ;;
1925 esac
1926 echo "refs/changes/$hash/$change_id/$patchset_id"
1927 ;;
1928 fetch|pull)
1929 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1930 $FUNCNAME __cmg_err_not_repo && return 1
1931 local change=$1
1932 shift
1933 git $command $@ http://$review/p/$project \
1934 $($FUNCNAME __cmg_get_ref $change) || return 1
1935 ;;
1936 push)
1937 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1938 $FUNCNAME __cmg_err_not_repo && return 1
1939 if [ -z "$user" ]; then
1940 echo >&2 "Gerrit username not found."
1941 return 1
1942 fi
1943 local local_branch remote_branch
1944 case $1 in
1945 *:*)
1946 local_branch=${1%:*}
1947 remote_branch=${1##*:}
1948 ;;
1949 *)
1950 local_branch=HEAD
1951 remote_branch=$1
1952 ;;
1953 esac
1954 shift
1955 git push $@ ssh://$user@$review:29418/$project \
1956 $local_branch:refs/for/$remote_branch || return 1
1957 ;;
1958 changes|for)
1959 if [ "$FUNCNAME" = "cmgerrit" ]; then
1960 echo >&2 "'$FUNCNAME $command' is deprecated."
1961 fi
1962 ;;
1963 __cmg_err_no_arg)
1964 if [ $# -lt 2 ]; then
1965 echo >&2 "'$FUNCNAME $command' missing argument."
1966 elif [ $2 -eq 0 ]; then
1967 if [ -n "$3" ]; then
1968 $FUNCNAME help $1
1969 else
1970 echo >&2 "'$FUNCNAME $1' missing argument."
1971 fi
1972 else
1973 return 1
1974 fi
1975 ;;
1976 __cmg_err_not_repo)
1977 if [ -z "$review" -o -z "$project" ]; then
1978 echo >&2 "Not currently in any reviewable repository."
1979 else
1980 return 1
1981 fi
1982 ;;
1983 __cmg_err_not_supported)
1984 $FUNCNAME __cmg_err_no_arg $command $# && return
1985 case $1 in
1986 #TODO: filter more git commands that don't use refname
1987 init|add|rm|mv|status|clone|remote|bisect|config|stash)
1988 echo >&2 "'$FUNCNAME $1' is not supported."
1989 ;;
1990 *) return 1 ;;
1991 esac
1992 ;;
1993 #TODO: other special cases?
1994 *)
1995 $FUNCNAME __cmg_err_not_supported $command && return 1
1996 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1997 $FUNCNAME __cmg_err_not_repo && return 1
1998 local args="$@"
1999 local change pre_args refs_arg post_args
2000 case "$args" in
2001 *--\ *)
2002 pre_args=${args%%-- *}
2003 post_args="-- ${args#*-- }"
2004 ;;
2005 *) pre_args="$args" ;;
2006 esac
2007 args=($pre_args)
2008 pre_args=
2009 if [ ${#args[@]} -gt 0 ]; then
2010 change=${args[${#args[@]}-1]}
2011 fi
2012 if [ ${#args[@]} -gt 1 ]; then
2013 pre_args=${args[0]}
2014 for ((i=1; i<${#args[@]}-1; i++)); do
2015 pre_args="$pre_args ${args[$i]}"
2016 done
2017 fi
2018 while ((1)); do
2019 case $change in
2020 ""|--)
2021 $FUNCNAME help $command
2022 return 1
2023 ;;
2024 *@*)
2025 if [ -z "$refs_arg" ]; then
2026 refs_arg="@${change#*@}"
2027 change=${change%%@*}
2028 fi
2029 ;;
2030 *~*)
2031 if [ -z "$refs_arg" ]; then
2032 refs_arg="~${change#*~}"
2033 change=${change%%~*}
2034 fi
2035 ;;
2036 *^*)
2037 if [ -z