blob: 6bd6c6183f195d24798a41716559800c82453e0b [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.
Khalid Zubairfdc26cd2015-10-21 12:43:14 -070016- mms: Short circuit builder. Quickly re-build the kernel, rootfs, boot and system images
17 without deep dependencies. Requires the full build to have run before.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070018- cgrep: Greps on all local C/C++ files.
Jon Boekenoogencbca56f2014-04-07 10:57:38 -070019- ggrep: Greps on all local Gradle files.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070020- jgrep: Greps on all local Java files.
21- resgrep: Greps on all local res/*.xml files.
Trevor Drake9c889762015-01-30 04:42:21 +000022- mangrep: Greps on all local AndroidManifest.xml files.
23- sepgrep: Greps on all local sepolicy files.
Jeff Brown46cbd202014-07-26 15:15:41 -070024- sgrep: Greps on all local source files.
The Android Open Source Project88b60792009-03-03 19:28:42 -080025- godir: Go to the directory containing a file.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060026- cmremote: Add git remote for CM Gerrit Review
27- cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
28- cmrebase: Rebase a Gerrit change and push it again
Steve Kondik873fa562012-09-17 11:33:18 -070029- aospremote: Add git remote for matching AOSP repository
Steve Kondik20c21d22013-10-27 13:34:36 -070030- cafremote: Add git remote for matching CodeAurora repository.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060031- mka: Builds using SCHED_BATCH on all processors
Chirayu Desaicf2bdb62012-09-28 11:56:02 +053032- mkap: Builds the module(s) using mka and pushes them to the device.
33- cmka: Cleans and builds using mka.
Matt Mower8dacaed2013-12-29 12:57:20 -060034- repolastsync: Prints date and time of last repo sync.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060035- reposync: Parallel repo sync using ionice and SCHED_BATCH
Chirayu Desai4a319b82013-06-05 20:14:33 +053036- repopick: Utility to fetch changes from Gerrit.
Steve Kondik83c03d52012-10-24 16:40:42 -070037- installboot: Installs a boot.img to the connected device.
38- installrecovery: Installs a recovery.img to the connected device.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070039
Dan Albert4ae5d4b2014-10-31 16:23:08 -070040Environemnt options:
41- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
42 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
43 build is leak-check clean.
44
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070045Look at the source to view more functions. The complete list is:
46EOF
47 T=$(gettop)
Jeff Brown46cbd202014-07-26 15:15:41 -070048 for i in `cat $T/build/envsetup.sh | sed -n "/^[ \t]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
Anthony King0780f2f2015-04-30 23:16:10 +010049 echo "$i"
50 done | column
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070051}
52
53# Get the value of a build variable as an absolute path.
54function get_abs_build_var()
55{
56 T=$(gettop)
57 if [ ! "$T" ]; then
58 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
59 return
60 fi
Ying Wang9cd17642012-12-13 10:52:07 -080061 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070062 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070063}
64
65# Get the exact value of a build variable.
66function get_build_var()
67{
68 T=$(gettop)
69 if [ ! "$T" ]; then
70 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
71 return
72 fi
Andrew Boie6905e212013-12-19 13:21:46 -080073 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070074 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080075}
76
77# check to see if the supplied product is one we can build
78function check_product()
79{
80 T=$(gettop)
81 if [ ! "$T" ]; then
82 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
83 return
84 fi
Ricardo Cerqueira119d3bb2011-11-25 15:30:36 +000085
86 if (echo -n $1 | grep -q -e "^cm_") ; then
87 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
Ricardo Cerqueira34ae3232013-09-26 00:10:20 +010088 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 +000089 else
90 CM_BUILD=
91 fi
92 export CM_BUILD
93
Jeff Browne33ba4c2011-07-11 22:11:46 -070094 TARGET_PRODUCT=$1 \
95 TARGET_BUILD_VARIANT= \
96 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070097 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080098 get_build_var TARGET_DEVICE > /dev/null
99 # hide successful answers, but allow the errors to show
100}
101
102VARIANT_CHOICES=(user userdebug eng)
103
104# check to see if the supplied variant is valid
105function check_variant()
106{
107 for v in ${VARIANT_CHOICES[@]}
108 do
109 if [ "$v" = "$1" ]
110 then
111 return 0
112 fi
113 done
114 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115}
116
117function setpaths()
118{
119 T=$(gettop)
120 if [ ! "$T" ]; then
121 echo "Couldn't locate the top of the tree. Try setting TOP."
122 return
123 fi
124
125 ##################################################################
126 # #
127 # Read me before you modify this code #
128 # #
129 # This function sets ANDROID_BUILD_PATHS to what it is adding #
130 # to PATH, and the next time it is run, it removes that from #
131 # PATH. This is required so lunch can be run more than once #
132 # and still have working paths. #
133 # #
134 ##################################################################
135
Raphael Mollc639c782011-06-20 17:25:01 -0700136 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
137 # due to "C:\Program Files" being in the path.
138
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700139 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700140 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700141 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
142 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700143 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700144 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800145 # strip leading ':', if any
146 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500147 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700148
149 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700150 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700151 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700152
Ben Cheng8bc4c432012-11-16 13:29:13 -0800153 # defined in core/config.mk
154 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700155 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Brandon McAnshfaf35352015-10-10 10:20:38 -0400156 targetlegacygccversion=$(get_build_var TARGET_LEGACY_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800157 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800158
Raphael Mollc639c782011-06-20 17:25:01 -0700159 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800160 export ANDROID_TOOLCHAIN=
161 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200162 local ARCH=$(get_build_var TARGET_ARCH)
163 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400164 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700165 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400166 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
167 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800168 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200169 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800170 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700171 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700172 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700173 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000174 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200175 *)
176 echo "Can't find toolchain for unknown architecture: $ARCH"
177 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700178 ;;
179 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700180 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800181 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700182 fi
Raphael732936d2011-06-22 14:35:32 -0700183
Ben Chengfba67bf2014-02-25 10:27:07 -0800184 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
185 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
186 fi
187
188 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700189 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700190 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800191 # Legacy toolchain configuration used for ARM kernel compilation
Brandon McAnshfaf35352015-10-10 10:20:38 -0400192 toolchaindir=arm/arm-eabi-$targetlegacygccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700193 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100194 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
195 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700196 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700197 ;;
198 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700199 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700200 ;;
201 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700202
Jeff Vander Stoep5aa68322015-06-12 09:56:39 -0700203 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Ying Wang2a859d52014-06-30 10:25:33 -0700204 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 +0200205
206 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
207 # to ensure that the corresponding 'emulator' binaries are used.
208 case $(uname -s) in
209 Darwin)
210 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
211 ;;
212 Linux)
213 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
214 ;;
215 *)
216 ANDROID_EMULATOR_PREBUILTS=
217 ;;
218 esac
219 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700220 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200221 export ANDROID_EMULATOR_PREBUILTS
222 fi
223
Ying Wangaa1c9b52012-11-26 20:51:59 -0800224 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800225
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500226 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900227 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500228 if [ -n "$JAVA_HOME" ]; then
229 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900230 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
231 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500232 fi
233
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800234 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700235 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
236 export OUT=$ANDROID_PRODUCT_OUT
237
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700238 unset ANDROID_HOST_OUT
239 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
240
Matt Mowerf8ff73e2015-06-09 19:35:53 -0500241 if [ -n "$ANDROID_CCACHE_DIR" ]; then
242 export CCACHE_DIR=$ANDROID_CCACHE_DIR
243 fi
244
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800245 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700246 # TODO: fix the path
247 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
248}
249
250function printconfig()
251{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800252 T=$(gettop)
253 if [ ! "$T" ]; then
254 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
255 return
256 fi
257 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700258}
259
260function set_stuff_for_environment()
261{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800262 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500263 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800264 setpaths
265 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700266
Ben Chengaac3f812013-08-13 14:38:15 -0700267 # With this environment variable new GCC can apply colors to warnings/errors
268 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 -0700269 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700270}
271
272function set_sequence_number()
273{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700274 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700275}
276
277function settitle()
278{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800279 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700280 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700281 local product=$TARGET_PRODUCT
282 local variant=$TARGET_BUILD_VARIANT
283 local apps=$TARGET_BUILD_APPS
Steve Kondikd6fba552012-12-27 15:28:34 -0800284 if [ -z "$PROMPT_COMMAND" ]; then
285 # No prompts
286 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
287 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
288 # Prompts exist, but no hardstatus
289 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700290 fi
Steve Kondikd6fba552012-12-27 15:28:34 -0800291 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
Christopher Laisfa8d9352013-06-03 15:15:39 -0500292 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
Steve Kondikd6fba552012-12-27 15:28:34 -0800293 fi
294
295 if [ -z "$apps" ]; then
296 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
297 else
298 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
299 fi
300 export ANDROID_PROMPT_PREFIX
301
302 # Inject build data into hardstatus
303 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 -0800304 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700305}
306
Kyle Ladd031a0b62013-09-11 20:43:42 -0400307function check_bash_version()
Kenny Root52aa81c2011-07-15 11:07:06 -0700308{
Kenny Root52aa81c2011-07-15 11:07:06 -0700309 # Keep us from trying to run in something that isn't bash.
310 if [ -z "${BASH_VERSION}" ]; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400311 return 1
Kenny Root52aa81c2011-07-15 11:07:06 -0700312 fi
313
314 # Keep us from trying to run in bash that's too old.
James Roberts-Thomson24dd07d2013-04-16 15:53:39 +1200315 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400316 return 2
Kenny Root52aa81c2011-07-15 11:07:06 -0700317 fi
318
Kyle Ladd031a0b62013-09-11 20:43:42 -0400319 return 0
Kenny Root52aa81c2011-07-15 11:07:06 -0700320}
321
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322function choosetype()
323{
324 echo "Build type choices are:"
325 echo " 1. release"
326 echo " 2. debug"
327 echo
328
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700330 DEFAULT_NUM=1
331 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700332
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800333 export TARGET_BUILD_TYPE=
334 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700335 while [ -z $TARGET_BUILD_TYPE ]
336 do
337 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800338 if [ -z "$1" ] ; then
339 read ANSWER
340 else
341 echo $1
342 ANSWER=$1
343 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700344 case $ANSWER in
345 "")
346 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
347 ;;
348 1)
349 export TARGET_BUILD_TYPE=release
350 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800351 release)
352 export TARGET_BUILD_TYPE=release
353 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700354 2)
355 export TARGET_BUILD_TYPE=debug
356 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800357 debug)
358 export TARGET_BUILD_TYPE=debug
359 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700360 *)
361 echo
362 echo "I didn't understand your response. Please try again."
363 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700364 ;;
365 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800366 if [ -n "$1" ] ; then
367 break
368 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700369 done
370
371 set_stuff_for_environment
372}
373
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800374#
375# This function isn't really right: It chooses a TARGET_PRODUCT
376# based on the list of boards. Usually, that gets you something
377# that kinda works with a generic product, but really, you should
378# pick a product by name.
379#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700380function chooseproduct()
381{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700382 if [ "x$TARGET_PRODUCT" != x ] ; then
383 default_value=$TARGET_PRODUCT
384 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700385 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700386 fi
387
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800388 export TARGET_PRODUCT=
389 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700390 while [ -z "$TARGET_PRODUCT" ]
391 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700392 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800393 if [ -z "$1" ] ; then
394 read ANSWER
395 else
396 echo $1
397 ANSWER=$1
398 fi
399
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400 if [ -z "$ANSWER" ] ; then
401 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800402 else
403 if check_product $ANSWER
404 then
405 export TARGET_PRODUCT=$ANSWER
406 else
407 echo "** Not a valid product: $ANSWER"
408 fi
409 fi
410 if [ -n "$1" ] ; then
411 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700412 fi
413 done
414
415 set_stuff_for_environment
416}
417
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800418function choosevariant()
419{
420 echo "Variant choices are:"
421 local index=1
422 local v
423 for v in ${VARIANT_CHOICES[@]}
424 do
425 # The product name is the name of the directory containing
426 # the makefile we found, above.
427 echo " $index. $v"
428 index=$(($index+1))
429 done
430
431 local default_value=eng
432 local ANSWER
433
434 export TARGET_BUILD_VARIANT=
435 while [ -z "$TARGET_BUILD_VARIANT" ]
436 do
437 echo -n "Which would you like? [$default_value] "
438 if [ -z "$1" ] ; then
439 read ANSWER
440 else
441 echo $1
442 ANSWER=$1
443 fi
444
445 if [ -z "$ANSWER" ] ; then
446 export TARGET_BUILD_VARIANT=$default_value
447 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
448 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800449 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800450 fi
451 else
452 if check_variant $ANSWER
453 then
454 export TARGET_BUILD_VARIANT=$ANSWER
455 else
456 echo "** Not a valid variant: $ANSWER"
457 fi
458 fi
459 if [ -n "$1" ] ; then
460 break
461 fi
462 done
463}
464
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465function choosecombo()
466{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700467 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700468
469 echo
470 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700471 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472
473 echo
474 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700475 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800476
477 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800479 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700480}
481
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800482# Clear this variable. It will be built up again when the vendorsetup.sh
483# files are included at the end of this file.
484unset LUNCH_MENU_CHOICES
485function add_lunch_combo()
486{
487 local new_combo=$1
488 local c
489 for c in ${LUNCH_MENU_CHOICES[@]} ; do
490 if [ "$new_combo" = "$c" ] ; then
491 return
492 fi
493 done
494 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
495}
496
497# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700498add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800499add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000500add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800501add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000502add_lunch_combo aosp_x86-eng
503add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800504
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700505function print_lunch_menu()
506{
507 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700508 echo
509 echo "You're building on" $uname
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000510 if [ "$(uname)" = "Darwin" ] ; then
511 echo " (ohai, koush!)"
512 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700513 echo
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000514 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
515 echo "Breakfast menu... pick a combo:"
516 else
517 echo "Lunch menu... pick a combo:"
518 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800519
520 local i=1
521 local choice
522 for choice in ${LUNCH_MENU_CHOICES[@]}
523 do
cybojenixa5dd6ce2013-06-28 20:30:00 +0100524 echo " $i. $choice "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800525 i=$(($i+1))
cybojenixa5dd6ce2013-06-28 20:30:00 +0100526 done | column
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800527
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000528 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
529 echo "... and don't forget the bacon!"
530 fi
531
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700532 echo
533}
534
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000535function brunch()
536{
537 breakfast $*
538 if [ $? -eq 0 ]; then
539 mka bacon
540 else
541 echo "No such item in brunch menu. Try 'breakfast'"
542 return 1
543 fi
544 return $?
545}
546
547function breakfast()
548{
549 target=$1
JustArchif70c7e62014-06-22 14:37:30 +0200550 local variant=$2
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000551 CM_DEVICES_ONLY="true"
552 unset LUNCH_MENU_CHOICES
553 add_lunch_combo full-eng
554 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
555 do
556 echo "including $f"
557 . $f
558 done
559 unset f
560
561 if [ $# -eq 0 ]; then
562 # No arguments, so let's have the full menu
563 lunch
564 else
565 echo "z$target" | grep -q "-"
566 if [ $? -eq 0 ]; then
567 # A buildtype was specified, assume a full device name
568 lunch $target
569 else
570 # This is probably just the CM model name
JustArchif70c7e62014-06-22 14:37:30 +0200571 if [ -z "$variant" ]; then
572 variant="userdebug"
573 fi
574 lunch cm_$target-$variant
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000575 fi
576 fi
577 return $?
578}
579
580alias bib=breakfast
581
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582function lunch()
583{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 local answer
Anthony King850627c2015-04-30 22:57:08 +0100585 LUNCH_MENU_CHOICES=($(for l in ${LUNCH_MENU_CHOICES[@]}; do echo "$l"; done | sort))
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800588 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589 else
590 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700591 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700593 fi
594
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 local selection=
596
597 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700599 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800600 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
601 then
602 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
603 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800604 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800605 fi
606 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
607 then
608 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700609 fi
610
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800611 if [ -z "$selection" ]
612 then
613 echo
614 echo "Invalid lunch combo: $answer"
615 return 1
616 fi
617
Joe Onoratoda12daf2010-06-09 18:18:31 -0700618 export TARGET_BUILD_APPS=
619
Jeff Browne33ba4c2011-07-11 22:11:46 -0700620 local product=$(echo -n $selection | sed -e "s/-.*$//")
621 check_product $product
622 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800623 then
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800624 # if we can't find a product, try to grab it off the CM github
625 T=$(gettop)
626 pushd $T > /dev/null
627 build/tools/roomservice.py $product
628 popd > /dev/null
629 check_product $product
Diogo Ferreira0d109172012-03-18 21:18:29 +0000630 else
631 build/tools/roomservice.py $product true
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800632 fi
633 if [ $? -ne 0 ]
634 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700635 echo
636 echo "** Don't have a product spec for: '$product'"
637 echo "** Do you have the right repo manifest?"
638 product=
639 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640
Jeff Browne33ba4c2011-07-11 22:11:46 -0700641 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
642 check_variant $variant
643 if [ $? -ne 0 ]
644 then
645 echo
646 echo "** Invalid variant: '$variant'"
647 echo "** Must be one of ${VARIANT_CHOICES[@]}"
648 variant=
649 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800650
Jeff Browne33ba4c2011-07-11 22:11:46 -0700651 if [ -z "$product" -o -z "$variant" ]
652 then
653 echo
654 return 1
655 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800656
Jeff Browne33ba4c2011-07-11 22:11:46 -0700657 export TARGET_PRODUCT=$product
658 export TARGET_BUILD_VARIANT=$variant
659 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700660
661 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662
Chirayu Desaib89b3242013-06-30 10:04:25 +0530663 fixup_common_out_dir
664
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800666 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667}
668
Jeff Davidson513d7a42010-08-02 10:00:44 -0700669# Tab completion for lunch.
670function _lunch()
671{
672 local cur prev opts
673 COMPREPLY=()
674 cur="${COMP_WORDS[COMP_CWORD]}"
675 prev="${COMP_WORDS[COMP_CWORD-1]}"
676
677 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
678 return 0
679}
Emilio López9ad6eea2013-11-03 13:02:13 -0300680complete -F _lunch lunch 2>/dev/null
Jeff Davidson513d7a42010-08-02 10:00:44 -0700681
Joe Onoratoda12daf2010-06-09 18:18:31 -0700682# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700683# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700684function tapas()
685{
Ying Wangb541ab62014-05-29 17:57:40 -0700686 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 -0700687 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700688 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
689 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 -0700690
Ying Wang67f02922012-08-22 10:25:20 -0700691 if [ $(echo $arch | wc -w) -gt 1 ]; then
692 echo "tapas: Error: Multiple build archs supplied: $arch"
693 return
694 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700695 if [ $(echo $variant | wc -w) -gt 1 ]; then
696 echo "tapas: Error: Multiple build variants supplied: $variant"
697 return
698 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700699 if [ $(echo $density | wc -w) -gt 1 ]; then
700 echo "tapas: Error: Multiple densities supplied: $density"
701 return
702 fi
Ying Wang67f02922012-08-22 10:25:20 -0700703
704 local product=full
705 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700706 x86) product=full_x86;;
707 mips) product=full_mips;;
708 armv5) product=generic_armv5;;
709 arm64) product=aosp_arm64;;
710 x86_64) product=aosp_x86_64;;
711 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700712 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700713 if [ -z "$variant" ]; then
714 variant=eng
715 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700716 if [ -z "$apps" ]; then
717 apps=all
718 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600719 if [ -z "$density" ]; then
720 density=alldpi
721 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700722
Ying Wang67f02922012-08-22 10:25:20 -0700723 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700724 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700725 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700726 export TARGET_BUILD_TYPE=release
727 export TARGET_BUILD_APPS=$apps
728
729 set_stuff_for_environment
730 printconfig
731}
732
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100733function eat()
734{
735 if [ "$OUT" ] ; then
Chirayu Desai80a277d2013-05-04 17:59:18 +0530736 MODVERSION=$(get_build_var CM_VERSION)
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100737 ZIPFILE=cm-$MODVERSION.zip
738 ZIPPATH=$OUT/$ZIPFILE
739 if [ ! -f $ZIPPATH ] ; then
740 echo "Nothing to eat"
741 return 1
742 fi
743 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Steve Kondik58fa7162015-11-15 18:47:58 -0800744 if [ $(adb get-state) != device -a $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100745 echo "No device is online. Waiting for one..."
746 echo "Please connect USB and/or enable USB debugging"
Steve Kondik58fa7162015-11-15 18:47:58 -0800747 until [ $(adb get-state) = device -o $(adb shell test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100748 sleep 1
749 done
750 echo "Device Found.."
751 fi
Steve Kondikec4627c2015-07-10 02:31:24 -0700752 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Chirayu Desai6dadb572012-12-26 10:53:58 +0530753 then
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100754 # if adbd isn't root we can't write to /cache/recovery/
755 adb root
756 sleep 1
757 adb wait-for-device
Steve Kondik464574f2013-04-13 07:19:52 -0700758 cat << EOF > /tmp/command
Steve Kondike9f828c2015-11-10 23:51:21 +0100759--sideload_auto_reboot
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100760EOF
Steve Kondik464574f2013-04-13 07:19:52 -0700761 if adb push /tmp/command /cache/recovery/ ; then
762 echo "Rebooting into recovery for sideload installation"
763 adb reboot recovery
764 adb wait-for-sideload
765 adb sideload $ZIPPATH
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100766 fi
Steve Kondik464574f2013-04-13 07:19:52 -0700767 rm /tmp/command
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100768 else
769 echo "Nothing to eat"
770 return 1
771 fi
772 return $?
Chirayu Desai6dadb572012-12-26 10:53:58 +0530773 else
774 echo "The connected device does not appear to be $CM_BUILD, run away!"
775 fi
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100776}
777
Nebojsa Cvetkovicd5c0c872012-11-09 23:02:54 +0000778function omnom
779{
780 brunch $*
781 eat
782}
783
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700784function gettop
785{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800786 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700787 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700788 # The following circumlocution ensures we remove symlinks from TOP.
789 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 else
791 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800792 # The following circumlocution (repeated below as well) ensures
793 # that we record the true directory name and not one that is
794 # faked up with symlink names.
795 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700796 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800797 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 T=
799 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800800 \cd ..
synergyb112a402013-07-05 19:47:47 -0700801 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700802 done
Ying Wang9cd17642012-12-13 10:52:07 -0800803 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700804 if [ -f "$T/$TOPFILE" ]; then
805 echo $T
806 fi
807 fi
808 fi
809}
810
Andrew Hsieh906cb782013-09-10 17:37:14 +0800811# Return driver for "make", if any (eg. static analyzer)
812function getdriver()
813{
814 local T="$1"
815 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
816 if [ -n "$WITH_STATIC_ANALYZER" ]; then
817 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800818$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
819--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800820--status-bugs \
821--top=$T"
822 fi
823}
824
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700825function m()
826{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800827 local T=$(gettop)
828 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700829 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800830 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700831 else
832 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700833 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700834 fi
835}
836
837function findmakefile()
838{
839 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800840 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700841 T=
842 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700843 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700844 if [ -f "$T/Android.mk" ]; then
845 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800846 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700847 return
848 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800849 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700850 done
Ying Wang9cd17642012-12-13 10:52:07 -0800851 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700852}
853
854function mm()
855{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800856 local T=$(gettop)
857 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700858 # If we're sitting in the root of the build tree, just do a
859 # normal make.
860 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800861 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700862 else
863 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800864 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700865 local MODULES=
866 local GET_INSTALL_PATH=
867 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700868 # Remove the path to top as the makefilepath needs to be relative
869 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700870 if [ ! "$T" ]; then
871 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700872 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700873 elif [ ! "$M" ]; then
874 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700875 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700876 else
Ying Wanga7deb082013-08-16 13:24:47 -0700877 for ARG in $@; do
878 case $ARG in
879 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
880 esac
881 done
882 if [ -n "$GET_INSTALL_PATH" ]; then
883 MODULES=
884 ARGS=GET-INSTALL-PATH
885 else
886 MODULES=all_modules
887 ARGS=$@
888 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700889 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 -0700890 fi
891 fi
892}
893
894function mmm()
895{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800896 local T=$(gettop)
897 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700898 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800899 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800900 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800901 local ARGS=
902 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700903 local GET_INSTALL_PATH=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800904 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
905 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
906 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800907 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
908 if [ "$MODULES" = "" ]; then
909 MODULES=all_modules
910 fi
911 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700912 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700913 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
914 local TO_CHOP=`expr $TO_CHOP + 1`
915 local START=`PWD= /bin/pwd`
916 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700917 if [ "$MFILE" = "" ] ; then
918 MFILE=$DIR/Android.mk
919 else
920 MFILE=$MFILE/$DIR/Android.mk
921 fi
922 MAKEFILE="$MAKEFILE $MFILE"
923 else
Ying Wanga7deb082013-08-16 13:24:47 -0700924 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100925 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700926 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997c3eb1072015-10-18 19:49:13 +0200927 *) if [ -d $DIR ]; then
928 echo "No Android.mk in $DIR.";
929 else
930 echo "Couldn't locate the directory $DIR";
931 fi
932 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700933 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700934 fi
935 done
Ying Wanga7deb082013-08-16 13:24:47 -0700936 if [ -n "$GET_INSTALL_PATH" ]; then
937 ARGS=$GET_INSTALL_PATH
938 MODULES=
939 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800940 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 -0700941 else
942 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700943 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700944 fi
945}
946
Ying Wangb607f7b2013-02-08 18:01:04 -0800947function mma()
948{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800949 local T=$(gettop)
950 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800951 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800952 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800953 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800954 if [ ! "$T" ]; then
955 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700956 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800957 fi
958 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800959 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800960 fi
961}
962
963function mmma()
964{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800965 local T=$(gettop)
966 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800967 if [ "$T" ]; then
968 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
969 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
970 local MY_PWD=`PWD= /bin/pwd`
971 if [ "$MY_PWD" = "$T" ]; then
972 MY_PWD=
973 else
974 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
975 fi
976 local DIR=
977 local MODULE_PATHS=
978 local ARGS=
979 for DIR in $DIRS ; do
980 if [ -d $DIR ]; then
981 if [ "$MY_PWD" = "" ]; then
982 MODULE_PATHS="$MODULE_PATHS $DIR"
983 else
984 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
985 fi
986 else
987 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100988 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800989 *) echo "Couldn't find directory $DIR"; return 1;;
990 esac
991 fi
992 done
Andrew Hsieh906cb782013-09-10 17:37:14 +0800993 $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 -0800994 else
995 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700996 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800997 fi
998}
999
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001000function croot()
1001{
1002 T=$(gettop)
1003 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001004 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001005 else
1006 echo "Couldn't locate the top of the tree. Try setting TOP."
1007 fi
1008}
1009
nebkatfb67a1e2012-12-28 10:40:45 +00001010function cout()
1011{
1012 if [ "$OUT" ]; then
1013 cd $OUT
1014 else
1015 echo "Couldn't locate out directory. Try setting OUT."
1016 fi
1017}
1018
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001019function cproj()
1020{
1021 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001022 local HERE=$PWD
1023 T=
1024 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1025 T=$PWD
1026 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001027 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001028 return
1029 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001030 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001031 done
Ying Wang9cd17642012-12-13 10:52:07 -08001032 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001033 echo "can't find Android.mk"
1034}
1035
Daniel Sandler47e0a882013-07-30 13:23:52 -04001036# simplified version of ps; output in the form
1037# <pid> <procname>
1038function qpid() {
1039 local prepend=''
1040 local append=''
1041 if [ "$1" = "--exact" ]; then
1042 prepend=' '
1043 append='$'
1044 shift
1045 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1046 echo "usage: qpid [[--exact] <process name|pid>"
1047 return 255
1048 fi
1049
1050 local EXE="$1"
1051 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001052 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001053 else
1054 adb shell ps \
1055 | tr -d '\r' \
1056 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1057 fi
1058}
1059
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001060function pid()
1061{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001062 local prepend=''
1063 local append=''
1064 if [ "$1" = "--exact" ]; then
1065 prepend=' '
1066 append='$'
1067 shift
1068 fi
1069 local EXE="$1"
1070 if [ "$EXE" ] ; then
1071 local PID=`adb shell ps \
1072 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001073 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001074 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1075 echo "$PID"
1076 else
1077 echo "usage: pid [--exact] <process name>"
1078 return 255
1079 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001080}
1081
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001082# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001083# that has the core-file-size limit set correctly
1084#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001085# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001086# if its core-file-size limit is not set already.
1087# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1088
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001089function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001090{
1091 echo "Getting root...";
1092 adb root;
1093 adb wait-for-device;
1094
1095 echo "Remounting root parition read-write...";
1096 adb shell mount -w -o remount -t rootfs rootfs;
1097 sleep 1;
1098 adb wait-for-device;
1099 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001100 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001101 adb shell chmod 0777 /cores;
1102
1103 echo "Granting SELinux permission to dump in /cores...";
1104 adb shell restorecon -R /cores;
1105
1106 echo "Set core pattern.";
1107 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1108
1109 echo "Done."
1110}
1111
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001112# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001113# $1 = PID of process (e.g., $(pid mediaserver))
1114#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001115# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116# dump to actually be generated.
1117
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001118function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001119{
1120 local PID=$1;
1121 if [ -z "$PID" ]; then
1122 printf "Expecting a PID!\n";
1123 return;
1124 fi;
1125 echo "Setting core limit for $PID to infinite...";
1126 adb shell prlimit $PID 4 -1 -1
1127}
1128
1129# core - send SIGV and pull the core for process
1130# $1 = PID of process (e.g., $(pid mediaserver))
1131#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001132# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001133# enabled globally.
1134
1135function core()
1136{
1137 local PID=$1;
1138
1139 if [ -z "$PID" ]; then
1140 printf "Expecting a PID!\n";
1141 return;
1142 fi;
1143
1144 local CORENAME=core.$PID;
1145 local COREPATH=/cores/$CORENAME;
1146 local SIG=SEGV;
1147
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001148 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001149
1150 local done=0;
1151 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1152 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1153 adb shell kill -$SIG $PID;
1154 sleep 1;
1155 done;
1156
1157 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1158 echo "Done: core is under $COREPATH on device.";
1159}
1160
Christopher Tate744ee802009-11-12 15:33:08 -08001161# systemstack - dump the current stack trace of all threads in the system process
1162# to the usual ANR traces file
1163function systemstack()
1164{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001165 stacks system_server
1166}
1167
1168function stacks()
1169{
1170 if [[ $1 =~ ^[0-9]+$ ]] ; then
1171 local PID="$1"
1172 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001173 local PIDLIST="$(pid $1)"
1174 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1175 local PID="$PIDLIST"
1176 elif [ "$PIDLIST" ] ; then
1177 echo "more than one process: $1"
1178 else
1179 echo "no such process: $1"
1180 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001181 else
1182 echo "usage: stacks [pid|process name]"
1183 fi
1184
1185 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001186 # Determine whether the process is native
1187 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1188 # Dump stacks of Dalvik process
1189 local TRACES=/data/anr/traces.txt
1190 local ORIG=/data/anr/traces.orig
1191 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001192
Jeff Brownb12c2e52013-08-19 15:14:16 -07001193 # Keep original traces to avoid clobbering
1194 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001195
Jeff Brownb12c2e52013-08-19 15:14:16 -07001196 # Make sure we have a usable file
1197 adb shell touch $TRACES
1198 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001199
Jeff Brownb12c2e52013-08-19 15:14:16 -07001200 # Dump stacks and wait for dump to finish
1201 adb shell kill -3 $PID
1202 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001203
Jeff Brownb12c2e52013-08-19 15:14:16 -07001204 # Restore original stacks, and show current output
1205 adb shell mv $TRACES $TMP
1206 adb shell mv $ORIG $TRACES
1207 adb shell cat $TMP
1208 else
1209 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001210 local USE64BIT="$(is64bit $PID)"
1211 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001212 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001213 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001214}
1215
Michael Wrightaeed7212014-06-19 19:58:12 -07001216# Read the ELF header from /proc/$PID/exe to determine if the process is
1217# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001218function is64bit()
1219{
1220 local PID="$1"
1221 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001222 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001223 echo "64"
1224 else
1225 echo ""
1226 fi
1227 else
1228 echo ""
1229 fi
1230}
1231
Clark Scheff75b36a42014-12-23 13:30:51 -08001232function dddclient()
1233{
1234 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1235 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1236 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1237 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1238 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1239 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
1240 local ARCH=$(get_build_var TARGET_ARCH)
1241 local GDB
1242 case "$ARCH" in
1243 arm) GDB=arm-linux-androideabi-gdb;;
1244 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1245 mips|mips64) GDB=mips64el-linux-android-gdb;;
1246 x86) GDB=x86_64-linux-android-gdb;;
1247 x86_64) GDB=x86_64-linux-android-gdb;;
1248 *) echo "Unknown arch $ARCH"; return 1;;
1249 esac
1250
1251 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1252 local EXE="$1"
1253 if [ "$EXE" ] ; then
1254 EXE=$1
1255 if [[ $EXE =~ ^[^/].* ]] ; then
1256 EXE="system/bin/"$EXE
1257 fi
1258 else
1259 EXE="app_process"
1260 fi
1261
1262 local PORT="$2"
1263 if [ "$PORT" ] ; then
1264 PORT=$2
1265 else
1266 PORT=":5039"
1267 fi
1268
1269 local PID="$3"
1270 if [ "$PID" ] ; then
1271 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1272 PID=`pid $3`
1273 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1274 # that likely didn't work because of returning multiple processes
1275 # try again, filtering by root processes (don't contain colon)
1276 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1277 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1278 then
1279 echo "Couldn't resolve '$3' to single PID"
1280 return 1
1281 else
1282 echo ""
1283 echo "WARNING: multiple processes matching '$3' observed, using root process"
1284 echo ""
1285 fi
1286 fi
1287 fi
1288 adb forward "tcp$PORT" "tcp$PORT"
1289 local USE64BIT="$(is64bit $PID)"
1290 adb shell gdbserver$USE64BIT $PORT --attach $PID &
1291 sleep 2
1292 else
1293 echo ""
1294 echo "If you haven't done so already, do this first on the device:"
1295 echo " gdbserver $PORT /system/bin/$EXE"
1296 echo " or"
1297 echo " gdbserver $PORT --attach <PID>"
1298 echo ""
1299 fi
1300
1301 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1302 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1303
1304 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1305 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"
1306 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1307 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1308 # Enable special debugging for ART processes.
1309 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1310 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1311 fi
1312 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1313
1314 local WHICH_GDB=
1315 # 64-bit exe found
1316 if [ "$USE64BIT" != "" ] ; then
1317 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1318 # 32-bit exe / 32-bit platform
1319 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1320 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1321 # 32-bit exe / 64-bit platform
1322 else
1323 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1324 fi
1325
1326 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1327 else
1328 echo "Unable to determine build system output dir."
1329 fi
1330}
1331
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001332case `uname -s` in
1333 Darwin)
1334 function sgrep()
1335 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001336 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 -07001337 }
1338
1339 ;;
1340 *)
1341 function sgrep()
1342 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001343 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 -07001344 }
1345 ;;
1346esac
1347
Raghu Gandham8da43102012-07-25 19:57:22 -07001348function gettargetarch
1349{
1350 get_build_var TARGET_ARCH
1351}
1352
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001353function ggrep()
1354{
1355 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1356}
1357
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001358function jgrep()
1359{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001360 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 -07001361}
1362
1363function cgrep()
1364{
Dan Albert01961192014-11-22 10:16:01 -08001365 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 -07001366}
1367
1368function resgrep()
1369{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001370 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 -07001371}
1372
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001373function mangrep()
1374{
1375 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1376}
1377
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001378function sepgrep()
1379{
1380 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 "$@"
1381}
1382
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001383function rcgrep()
1384{
1385 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" -print0 | xargs -0 grep --color -n "$@"
1386}
1387
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001388case `uname -s` in
1389 Darwin)
1390 function mgrep()
1391 {
Ying Wang324c2b22012-08-17 15:03:20 -07001392 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 -07001393 }
1394
1395 function treegrep()
1396 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001397 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 -07001398 }
1399
1400 ;;
1401 *)
1402 function mgrep()
1403 {
Ying Wang324c2b22012-08-17 15:03:20 -07001404 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 -07001405 }
1406
1407 function treegrep()
1408 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001409 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 -07001410 }
1411
1412 ;;
1413esac
1414
1415function getprebuilt
1416{
1417 get_abs_build_var ANDROID_PREBUILTS
1418}
1419
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001420function tracedmdump()
1421{
1422 T=$(gettop)
1423 if [ ! "$T" ]; then
1424 echo "Couldn't locate the top of the tree. Try setting TOP."
1425 return
1426 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001427 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001428 local arch=$(gettargetarch)
1429 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001430
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001431 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001432 if [ ! "$TRACE" ] ; then
1433 echo "usage: tracedmdump tracename"
1434 return
1435 fi
1436
Jack Veenstra60116fc2009-04-09 18:12:34 -07001437 if [ ! -r "$KERNEL" ] ; then
1438 echo "Error: cannot find kernel: '$KERNEL'"
1439 return
1440 fi
1441
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001442 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001443 if [ "$BASETRACE" = "$TRACE" ] ; then
1444 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1445 fi
1446
1447 echo "post-processing traces..."
1448 rm -f $TRACE/qtrace.dexlist
1449 post_trace $TRACE
1450 if [ $? -ne 0 ]; then
1451 echo "***"
1452 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1453 echo "***"
1454 return
1455 fi
1456 echo "generating dexlist output..."
1457 /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
1458 echo "generating dmtrace data..."
1459 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1460 echo "generating html file..."
1461 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1462 echo "done, see $TRACE/dmtrace.html for details"
1463 echo "or run:"
1464 echo " traceview $TRACE/dmtrace"
1465}
1466
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001467# communicate with a running device or emulator, set up necessary state,
1468# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001469function runhat()
1470{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001471 # process standard adb options
1472 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001473 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001474 adbTarget=$1
1475 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001476 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001477 adbTarget="$1 $2"
1478 shift 2
1479 fi
1480 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001481 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001482
1483 # runhat options
1484 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001485
1486 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001487 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001488 return
1489 fi
1490
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001491 # confirm hat is available
1492 if [ -z $(which hat) ]; then
1493 echo "hat is not available in this configuration."
1494 return
1495 fi
1496
Andy McFaddenb6289852010-07-12 08:00:19 -07001497 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001498 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001499 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001500 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001501 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001502 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001503 echo -n "> "
1504 read
1505
The Android Open Source Project88b60792009-03-03 19:28:42 -08001506 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001507
The Android Open Source Project88b60792009-03-03 19:28:42 -08001508 echo "Retrieving file $devFile..."
1509 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001510
The Android Open Source Project88b60792009-03-03 19:28:42 -08001511 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001512
The Android Open Source Project88b60792009-03-03 19:28:42 -08001513 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001514 echo "View the output by pointing your browser at http://localhost:7000/"
1515 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001516 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001517}
1518
1519function getbugreports()
1520{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001521 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001522
1523 if [ ! "$reports" ]; then
1524 echo "Could not locate any bugreports."
1525 return
1526 fi
1527
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001528 local report
1529 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001530 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001531 echo "/sdcard/bugreports/${report}"
1532 adb pull /sdcard/bugreports/${report} ${report}
1533 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001534 done
1535}
1536
Victoria Lease1b296b42012-08-21 15:44:06 -07001537function getsdcardpath()
1538{
1539 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1540}
1541
1542function getscreenshotpath()
1543{
1544 echo "$(getsdcardpath)/Pictures/Screenshots"
1545}
1546
1547function getlastscreenshot()
1548{
1549 local screenshot_path=$(getscreenshotpath)
1550 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1551 if [ "$screenshot" = "" ]; then
1552 echo "No screenshots found."
1553 return
1554 fi
1555 echo "${screenshot}"
1556 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1557}
1558
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001559function startviewserver()
1560{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001561 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001562 if [ $# -gt 0 ]; then
1563 port=$1
1564 fi
1565 adb shell service call window 1 i32 $port
1566}
1567
1568function stopviewserver()
1569{
1570 adb shell service call window 2
1571}
1572
1573function isviewserverstarted()
1574{
1575 adb shell service call window 3
1576}
1577
Romain Guyb84049a2010-10-04 16:56:11 -07001578function key_home()
1579{
1580 adb shell input keyevent 3
1581}
1582
1583function key_back()
1584{
1585 adb shell input keyevent 4
1586}
1587
1588function key_menu()
1589{
1590 adb shell input keyevent 82
1591}
1592
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001593function smoketest()
1594{
1595 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1596 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1597 return
1598 fi
1599 T=$(gettop)
1600 if [ ! "$T" ]; then
1601 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1602 return
1603 fi
1604
Ying Wang9cd17642012-12-13 10:52:07 -08001605 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001606 adb uninstall com.android.smoketest > /dev/null &&
1607 adb uninstall com.android.smoketest.tests > /dev/null &&
1608 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1609 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1610 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1611}
1612
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001613# simple shortcut to the runtest command
1614function runtest()
1615{
1616 T=$(gettop)
1617 if [ ! "$T" ]; then
1618 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1619 return
1620 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001621 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001622}
1623
The Android Open Source Project88b60792009-03-03 19:28:42 -08001624function godir () {
1625 if [[ -z "$1" ]]; then
1626 echo "Usage: godir <regex>"
1627 return
1628 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001629 T=$(gettop)
Brian Carlstromf2257422015-09-30 20:28:54 -07001630 if [ ! "$OUT_DIR" = "" ]; then
1631 mkdir -p $OUT_DIR
1632 FILELIST=$OUT_DIR/filelist
1633 else
1634 FILELIST=$T/filelist
1635 fi
1636 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001637 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001638 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001639 echo " Done"
1640 echo ""
1641 fi
1642 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001643 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001644 if [[ ${#lines[@]} = 0 ]]; then
1645 echo "Not found"
1646 return
1647 fi
1648 local pathname
1649 local choice
1650 if [[ ${#lines[@]} > 1 ]]; then
1651 while [[ -z "$pathname" ]]; do
1652 local index=1
1653 local line
1654 for line in ${lines[@]}; do
1655 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001656 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001657 done
1658 echo
1659 echo -n "Select one: "
1660 unset choice
1661 read choice
1662 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1663 echo "Invalid choice"
1664 continue
1665 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001666 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001667 done
1668 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001669 pathname=${lines[0]}
1670 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001671 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001672}
1673
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001674function cmremote()
1675{
1676 git remote rm cmremote 2> /dev/null
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301677 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001678 if [ -z "$GERRIT_REMOTE" ]
1679 then
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301680 echo Unable to set up the git remote, are you under a git repo?
1681 return 0
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001682 fi
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301683 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001684 if [ -z "$CMUSER" ]
1685 then
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001686 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001687 else
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001688 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001689 fi
1690 echo You can now push to "cmremote".
1691}
Koushik Duttab55f13a2012-05-14 16:14:36 -07001692
Steve Kondik873fa562012-09-17 11:33:18 -07001693function aospremote()
1694{
1695 git remote rm aosp 2> /dev/null
1696 if [ ! -d .git ]
1697 then
1698 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1699 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001700 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik873fa562012-09-17 11:33:18 -07001701 if (echo $PROJECT | grep -qv "^device")
1702 then
1703 PFX="platform/"
1704 fi
1705 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1706 echo "Remote 'aosp' created"
1707}
Steve Kondik873fa562012-09-17 11:33:18 -07001708
Steve Kondik20c21d22013-10-27 13:34:36 -07001709function cafremote()
1710{
1711 git remote rm caf 2> /dev/null
1712 if [ ! -d .git ]
1713 then
1714 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1715 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001716 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik20c21d22013-10-27 13:34:36 -07001717 if (echo $PROJECT | grep -qv "^device")
1718 then
1719 PFX="platform/"
1720 fi
1721 git remote add caf git://codeaurora.org/$PFX$PROJECT
1722 echo "Remote 'caf' created"
1723}
Steve Kondik20c21d22013-10-27 13:34:36 -07001724
Steve Kondikf00e7d92012-09-23 23:46:55 -07001725function installboot()
1726{
1727 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1728 then
1729 echo "No recovery.fstab found. Build recovery first."
1730 return 1
1731 fi
1732 if [ ! -e "$OUT/boot.img" ];
1733 then
1734 echo "No boot.img found. Run make bootimage first."
1735 return 1
1736 fi
1737 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1738 if [ -z "$PARTITION" ];
1739 then
Ricardo Cerqueira9e4c4a72013-07-27 13:51:56 +01001740 # Try for RECOVERY_FSTAB_VERSION = 2
1741 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1742 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1743 if [ -z "$PARTITION" ];
1744 then
1745 echo "Unable to determine boot partition."
1746 return 1
1747 fi
Steve Kondikf00e7d92012-09-23 23:46:55 -07001748 fi
1749 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001750 adb wait-for-online
Steve Kondikf00e7d92012-09-23 23:46:55 -07001751 adb root
1752 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001753 adb wait-for-online shell mount /system 2>&1 > /dev/null
1754 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001755 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondikf00e7d92012-09-23 23:46:55 -07001756 then
1757 adb push $OUT/boot.img /cache/
1758 for i in $OUT/system/lib/modules/*;
1759 do
1760 adb push $i /system/lib/modules/
1761 done
1762 adb shell dd if=/cache/boot.img of=$PARTITION
1763 adb shell chmod 644 /system/lib/modules/*
1764 echo "Installation complete."
1765 else
1766 echo "The connected device does not appear to be $CM_BUILD, run away!"
1767 fi
1768}
1769
Steve Kondik83c03d52012-10-24 16:40:42 -07001770function installrecovery()
1771{
1772 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1773 then
1774 echo "No recovery.fstab found. Build recovery first."
1775 return 1
1776 fi
1777 if [ ! -e "$OUT/recovery.img" ];
1778 then
1779 echo "No recovery.img found. Run make recoveryimage first."
1780 return 1
1781 fi
1782 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1783 if [ -z "$PARTITION" ];
1784 then
Steve Kondik75f10762013-08-09 21:03:42 -07001785 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondikd8b510a2013-08-10 21:02:09 -07001786 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1787 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondik75f10762013-08-09 21:03:42 -07001788 if [ -z "$PARTITION" ];
1789 then
1790 echo "Unable to determine recovery partition."
1791 return 1
1792 fi
Steve Kondik83c03d52012-10-24 16:40:42 -07001793 fi
1794 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001795 adb wait-for-online
Steve Kondik83c03d52012-10-24 16:40:42 -07001796 adb root
1797 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001798 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1799 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001800 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondik83c03d52012-10-24 16:40:42 -07001801 then
1802 adb push $OUT/recovery.img /cache/
1803 adb shell dd if=/cache/recovery.img of=$PARTITION
1804 echo "Installation complete."
1805 else
1806 echo "The connected device does not appear to be $CM_BUILD, run away!"
1807 fi
1808}
Steve Kondikf00e7d92012-09-23 23:46:55 -07001809
Koushik Duttab55f13a2012-05-14 16:14:36 -07001810function makerecipe() {
1811 if [ -z "$1" ]
1812 then
1813 echo "No branch name provided."
1814 return 1
1815 fi
1816 cd android
1817 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1818 git commit -a -m "$1"
1819 cd ..
1820
1821 repo forall -c '
1822
1823 if [ "$REPO_REMOTE" == "github" ]
1824 then
1825 pwd
1826 cmremote
1827 git push cmremote HEAD:refs/heads/'$1'
1828 fi
1829 '
1830}
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001831
1832function cmgerrit() {
1833 if [ $# -eq 0 ]; then
1834 $FUNCNAME help
1835 return 1
1836 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001837 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001838 local review=`git config --get remote.github.review`
1839 local project=`git config --get remote.github.projectname`
1840 local command=$1
1841 shift
1842 case $command in
1843 help)
1844 if [ $# -eq 0 ]; then
1845 cat <<EOF
1846Usage:
1847 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1848
1849Commands:
1850 fetch Just fetch the change as FETCH_HEAD
1851 help Show this help, or for a specific command
1852 pull Pull a change into current branch
1853 push Push HEAD or a local branch to Gerrit for a specific branch
1854
1855Any other Git commands that support refname would work as:
1856 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1857
1858See '$FUNCNAME help COMMAND' for more information on a specific command.
1859
1860Example:
1861 $FUNCNAME checkout -b topic 1234/5
1862works as:
1863 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1864 && git checkout -b topic FETCH_HEAD
1865will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1866Patch-set 1 will be fetched if omitted.
1867EOF
1868 return
1869 fi
1870 case $1 in
1871 __cmg_*) echo "For internal use only." ;;
1872 changes|for)
1873 if [ "$FUNCNAME" = "cmgerrit" ]; then
1874 echo "'$FUNCNAME $1' is deprecated."
1875 fi
1876 ;;
1877 help) $FUNCNAME help ;;
1878 fetch|pull) cat <<EOF
1879usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1880
1881works as:
1882 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1883 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1884
1885Example:
1886 $FUNCNAME $1 1234
1887will $1 patch-set 1 of change 1234
1888EOF
1889 ;;
1890 push) cat <<EOF
1891usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1892
1893works as:
1894 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1895 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1896
1897Example:
1898 $FUNCNAME push fix6789:gingerbread
1899will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1900HEAD will be pushed from local if omitted.
1901EOF
1902 ;;
1903 *)
1904 $FUNCNAME __cmg_err_not_supported $1 && return
1905 cat <<EOF
1906usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1907
1908works as:
1909 git fetch http://DOMAIN/p/PROJECT \\
1910 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1911 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1912EOF
1913 ;;
1914 esac
1915 ;;
1916 __cmg_get_ref)
1917 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1918 local change_id patchset_id hash
1919 case $1 in
1920 */*)
1921 change_id=${1%%/*}
1922 patchset_id=${1#*/}
1923 ;;
1924 *)
1925 change_id=$1
1926 patchset_id=1
1927 ;;
1928 esac
1929 hash=$(($change_id % 100))
1930 case $hash in
1931 [0-9]) hash="0$hash" ;;
1932 esac
1933 echo "refs/changes/$hash/$change_id/$patchset_id"
1934 ;;
1935 fetch|pull)
1936 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1937 $FUNCNAME __cmg_err_not_repo && return 1
1938 local change=$1
1939 shift
1940 git $command $@ http://$review/p/$project \
1941 $($FUNCNAME __cmg_get_ref $change) || return 1
1942 ;;
1943 push)
1944 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1945 $FUNCNAME __cmg_err_not_repo && return 1
1946 if [ -z "$user" ]; then
1947 echo >&2 "Gerrit username not found."
1948 return 1
1949 fi
1950 local local_branch remote_branch
1951 case $1 in
1952 *:*)
1953 local_branch=${1%:*}
1954 remote_branch=${1##*:}
1955 ;;
1956 *)
1957 local_branch=HEAD
1958 remote_branch=$1
1959 ;;
1960 esac
1961 shift
1962 git push $@ ssh://$user@$review:29418/$project \
1963 $local_branch:refs/for/$remote_branch || return 1
1964 ;;
1965 changes|for)
1966 if [ "$FUNCNAME" = "cmgerrit" ]; then
1967 echo >&2 "'$FUNCNAME $command' is deprecated."
1968 fi
1969 ;;
1970 __cmg_err_no_arg)
1971 if [ $# -lt 2 ]; then
1972 echo >&2 "'$FUNCNAME $command' missing argument."
1973 elif [ $2 -eq 0 ]; then
1974 if [ -n "$3" ]; then
1975 $FUNCNAME help $1
1976 else
1977 echo >&2 "'$FUNCNAME $1' missing argument."
1978 fi
1979 else
1980 return 1
1981 fi
1982 ;;
1983 __cmg_err_not_repo)
1984 if [ -z "$review" -o -z "$project" ]; then
1985 echo >&2 "Not currently in any reviewable repository."
1986 else
1987 return 1
1988 fi
1989 ;;
1990 __cmg_err_not_supported)
1991 $FUNCNAME __cmg_err_no_arg $command $# && return
1992 case $1 in
1993 #TODO: filter more git commands that don't use refname
1994 init|add|rm|mv|status|clone|remote|bisect|config|stash)
1995 echo >&2 "'$FUNCNAME $1' is not supported."
1996 ;;
1997 *) return 1 ;;
1998 esac
1999 ;;
2000 #TODO: other special cases?
2001 *)
2002 $FUNCNAME __cmg_err_not_supported $command && return 1
2003 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
2004 $FUNCNAME __cmg_err_not_repo && return 1
2005 local args="$@"
2006 local change pre_args refs_arg post_args
2007 case "$args" in
2008 *--\ *)
2009 pre_args=${args%%-- *}
2010 post_args="-- ${args#*-- }"
2011 ;;
2012 *) pre_args="$args" ;;
2013 esac
2014 args=($pre_args)
2015 pre_args=
2016 if [ ${#args[@]} -gt 0 ]; then
2017 change=${args[${#args[@]}-1]}
2018 fi
2019 if [ ${#args[@]} -gt 1 ]; then
2020 pre_args=${args[0]}
2021 for ((i=1; i<${#args[@]}-1; i++)); do
2022 pre_args="$pre_args ${args[$i]}"
2023 done
2024 fi
2025 while ((1)); do
2026 case $change in
2027 ""|--)
2028 $FUNCNAME help $command
2029 return 1
2030 ;;
2031 *@*)
2032 if [ -z "$refs_arg" ]; then
2033 refs_arg="@${change#*@}"
2034 change=${change%%@*}
2035 fi
2036 ;;
2037 *~*)
2038 if [ -z "$refs_arg" ]; then
2039 refs_arg="~${change#*~}"
2040 change=${change%%~*}
2041 fi
2042 ;;
2043 *^*)
2044 if [ -z "$refs_arg" ]; then
2045 refs_arg="^${change#*^}"
2046 change=${change%%^*}
2047 fi
2048 ;;
2049 *:*)
2050 if [ -z "$refs_arg" ]; then
2051 refs_arg=":${change#*:}"
2052 change=${change%%:*}
2053 fi
2054 ;;
2055 *) break ;;
2056 esac
2057 done
2058 $FUNCNAME fetch $change \
2059 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
2060 || return 1
2061 ;;
2062 esac
2063}
2064
2065function cmrebase() {
2066 local repo=$1
2067 local refs=$2
2068 local pwd="$(pwd)"
2069 local dir="$(gettop)/$repo"
2070
2071 if [ -z $repo ] || [ -z $refs ]; then
2072 echo "CyanogenMod Gerrit Rebase Usage: "
2073 echo " cmrebase <path to project> <patch IDs on Gerrit>"
2074 echo " The patch IDs appear on the Gerrit commands that are offered."
2075 echo " They consist on a series of numbers and slashes, after the text"
2076 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
2077 echo ""
2078 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
2079 echo ""
2080 return
2081 fi
2082
2083 if [ ! -d $dir ]; then
2084 echo "Directory $dir doesn't exist in tree."
2085 return
2086 fi
2087 cd $dir
2088 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
2089 echo "Starting branch..."
2090 repo start tmprebase .
2091 echo "Bringing it up to date..."
2092 repo sync .
2093 echo "Fetching change..."
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07002094 git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002095 if [ "$?" != "0" ]; then
2096 echo "Error cherry-picking. Not uploading!"
2097 return
2098 fi
2099 echo "Uploading..."
2100 repo upload .
2101 echo "Cleaning up..."
2102 repo abandon tmprebase .
2103 cd $pwd
2104}
2105
2106function mka() {
Khalid Zubairabcd1942015-10-06 11:00:55 -07002107 local T=$(gettop)
2108 if [ "$T" ]; then
2109 case `uname -s` in
2110 Darwin)
2111 make -C $T -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
2112 ;;
2113 *)
2114 mk_timer schedtool -B -n 1 -e ionice -n 1 make -C $T -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
2115 ;;
2116 esac
2117
2118 else
2119 echo "Couldn't locate the top of the tree. Try setting TOP."
2120 fi
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002121}
2122
Chirayu Desaicf2bdb62012-09-28 11:56:02 +05302123function cmka() {
2124 if [ ! -z "$1" ]; then
2125 for i in "$@"; do
2126 case $i in
2127 bacon|otapackage|systemimage)
2128 mka installclean
2129 mka $i
2130 ;;
2131 *)
2132 mka clean-$i
2133 mka $i
2134 ;;
2135 esac
2136 done
2137 else
2138 mka clean
2139 mka
2140 fi
2141}
2142
Khalid Zubairfdc26cd2015-10-21 12:43:14 -07002143function mms() {
2144 local T=$(gettop)
2145 if [ -z "$T" ]
2146 then
2147 echo "Couldn't locate the top of the tree. Try setting TOP."
2148 return 1
2149 fi
2150
2151 case `uname -s` in
2152 Darwin)
2153 local NUM_CPUS=$(sysctl hw.ncpu|cut -d" " -f2)
2154 ONE_SHOT_MAKEFILE="__none__" \
2155 make -C $T -j $NUM_CPUS "$@"
2156 ;;
2157 *)
2158 local NUM_CPUS=$(cat /proc/cpuinfo | grep "^processor" | wc -l)
2159 ONE_SHOT_MAKEFILE="__none__" \
2160 mk_timer schedtool -B -n 1 -e ionice -n 1 \
2161 make -C $T -j $NUM_CPUS "$@"
2162 ;;
2163 esac
2164}
2165
2166
Matt Mower8dacaed2013-12-29 12:57:20 -06002167function repolastsync() {
2168 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
2169 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
2170 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
2171 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
2172}
2173
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002174function reposync() {
2175 case `uname -s` in
2176 Darwin)
2177 repo sync -j 4 "$@"
2178 ;;
2179 *)
Alan Orthef363cd2012-09-07 11:44:27 +03002180 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002181 ;;
2182 esac
2183}
Tanguy Pruvot2192fd22012-06-06 21:39:23 +02002184
2185function repodiff() {
2186 if [ -z "$*" ]; then
2187 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
2188 return
2189 fi
2190 diffopts=$* repo forall -c \
2191 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
2192}
2193
Khalid Zubair63eabff2016-02-02 12:00:14 -08002194# Return success if adb is up and not in recovery
2195function _adb_connected {
2196 {
2197 if [[ "$(adb get-state)" == device &&
2198 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
2199 then
2200 return 0
2201 fi
2202 } 2>/dev/null
2203
2204 return 1
2205};
2206
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302207# Credit for color strip sed: http://goo.gl/BoIcm
2208function dopush()
2209{
2210 local func=$1
2211 shift
2212
2213 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Khalid Zubair63eabff2016-02-02 12:00:14 -08002214 if ! _adb_connected; then
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302215 echo "No device is online. Waiting for one..."
2216 echo "Please connect USB and/or enable USB debugging"
Khalid Zubair63eabff2016-02-02 12:00:14 -08002217 until _adb_connected; do
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302218 sleep 1
2219 done
2220 echo "Device Found."
2221 fi
2222
Steve Kondikec4627c2015-07-10 02:31:24 -07002223 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD") || [ "$FORCE_PUSH" == "true" ];
Chirayu Desai6dadb572012-12-26 10:53:58 +05302224 then
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002225 # retrieve IP and PORT info if we're using a TCP connection
2226 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
2227 | head -1 | awk '{print $1}')
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302228 adb root &> /dev/null
2229 sleep 0.3
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002230 if [ -n "$TCPIPPORT" ]
2231 then
2232 # adb root just killed our connection
2233 # so reconnect...
2234 adb connect "$TCPIPPORT"
2235 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302236 adb wait-for-device &> /dev/null
2237 sleep 0.3
2238 adb remount &> /dev/null
2239
Matt Mower668ea262014-05-20 02:52:23 -05002240 mkdir -p $OUT
Marcos Marado37e92c22015-01-13 15:14:28 +00002241 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
2242 ret=$?;
2243 if [ $ret -ne 0 ]; then
2244 rm -f $OUT/.log;return $ret
2245 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302246
2247 # Install: <file>
LuK133756041e72015-12-01 14:53:23 +01002248 if [ `uname` = "Linux" ]; then
2249 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)"
2250 else
2251 LOC="$(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" | grep '^Install: ' | cut -d ':' -f 2)"
2252 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302253
2254 # Copy: <file>
LuK133756041e72015-12-01 14:53:23 +01002255 if [ `uname` = "Linux" ]; then
2256 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)"
2257 else
2258 LOC="$LOC $(cat $OUT/.log | sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" | grep '^Copy: ' | cut -d ':' -f 2)"
2259 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302260
Matt Mowerdfd4c082014-05-20 02:52:23 -05002261 # If any files are going to /data, push an octal file permissions reader to device
2262 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
2263 CHKPERM="/data/local/tmp/chkfileperm.sh"
2264(
2265cat <<'EOF'
2266#!/system/xbin/sh
2267FILE=$@
2268if [ -e $FILE ]; then
2269 ls -l $FILE | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}' | cut -d ' ' -f1
2270fi
2271EOF
2272) > $OUT/.chkfileperm.sh
2273 echo "Pushing file permissions checker to device"
2274 adb push $OUT/.chkfileperm.sh $CHKPERM
2275 adb shell chmod 755 $CHKPERM
2276 rm -f $OUT/.chkfileperm.sh
2277 fi
2278
Matt Mower668ea262014-05-20 02:52:23 -05002279 stop_n_start=false
Luca Stefani406c2af2015-11-02 05:33:10 -08002280 for FILE in $(echo $LOC | tr " " "\n"); do
Matt Mowerdfd4c082014-05-20 02:52:23 -05002281 # Make sure file is in $OUT/system or $OUT/data
Matt Mower668ea262014-05-20 02:52:23 -05002282 case $FILE in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002283 $OUT/system/*|$OUT/data/*)
Matt Mower668ea262014-05-20 02:52:23 -05002284 # Get target file name (i.e. /system/bin/adb)
2285 TARGET=$(echo $FILE | sed "s#$OUT##")
2286 ;;
2287 *) continue ;;
2288 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302289
Matt Mower668ea262014-05-20 02:52:23 -05002290 case $TARGET in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002291 /data/*)
2292 # fs_config only sets permissions and se labels for files pushed to /system
2293 if [ -n "$CHKPERM" ]; then
2294 OLDPERM=$(adb shell $CHKPERM $TARGET)
2295 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
2296 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
2297 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
2298 fi
2299 echo "Pushing: $TARGET"
2300 adb push $FILE $TARGET
2301 if [ -n "$OLDPERM" ]; then
2302 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
2303 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
2304 adb shell chmod "$OLDPERM" $TARGET
2305 else
2306 echo "$TARGET did not exist previously, you should set file permissions manually"
2307 fi
2308 adb shell restorecon "$TARGET"
2309 ;;
Pawit Pornkitprasan1822ad02014-12-22 20:11:47 +07002310 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
Matt Mower668ea262014-05-20 02:52:23 -05002311 # Only need to stop services once
2312 if ! $stop_n_start; then
2313 adb shell stop
2314 stop_n_start=true
2315 fi
2316 echo "Pushing: $TARGET"
2317 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302318 ;;
2319 *)
Matt Mower668ea262014-05-20 02:52:23 -05002320 echo "Pushing: $TARGET"
2321 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302322 ;;
Matt Mower668ea262014-05-20 02:52:23 -05002323 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302324 done
Matt Mowerdfd4c082014-05-20 02:52:23 -05002325 if [ -n "$CHKPERM" ]; then
2326 adb shell rm $CHKPERM
2327 fi
Matt Mower668ea262014-05-20 02:52:23 -05002328 if $stop_n_start; then
2329 adb shell start
2330 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302331 rm -f $OUT/.log
2332 return 0
Chirayu Desai6dadb572012-12-26 10:53:58 +05302333 else
2334 echo "The connected device does not appear to be $CM_BUILD, run away!"
2335 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302336}
2337
2338alias mmp='dopush mm'
2339alias mmmp='dopush mmm'
2340alias mkap='dopush mka'
2341alias cmkap='dopush cmka'
2342
Chirayu Desai4a319b82013-06-05 20:14:33 +05302343function repopick() {
2344 T=$(gettop)
2345 $T/build/tools/repopick.py $@
2346}
2347
Chirayu Desaib89b3242013-06-30 10:04:25 +05302348function fixup_common_out_dir() {
2349 common_out_dir=$(get_build_var OUT_DIR)/target/common
2350 target_device=$(get_build_var TARGET_DEVICE)
2351 if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
2352 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
2353 mv ${common_out_dir} ${common_out_dir}-${target_device}
2354 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2355 else
2356 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2357 mkdir -p ${common_out_dir}-${target_device}
2358 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2359 fi
2360 else
2361 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2362 mkdir -p ${common_out_dir}
2363 fi
2364}
2365
Michael Bestas163381a2015-10-23 20:36:47 +03002366# Force JAVA_HOME to point to java 1.7 if it isn't already set.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002367#
2368# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
2369# For some reason, installing the JDK doesn't make it show up in the
2370# JavaVM.framework/Versions/1.7/ folder.
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002371function set_java_home() {
Narayan Kamath9260bba2014-01-17 10:05:25 +00002372 # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamathc84889b2014-04-01 14:16:26 +01002373 # we can reset it later, depending on the version of java the build
2374 # system needs.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002375 #
2376 # If we don't do this, the JAVA_HOME value set by the first call to
2377 # build/envsetup.sh will persist forever.
2378 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
2379 export JAVA_HOME=""
2380 fi
2381
Andy McFaddenbd960942010-06-24 12:52:51 -07002382 if [ ! "$JAVA_HOME" ]; then
Neil Fuller46e00ea2014-10-16 10:23:03 +01002383 case `uname -s` in
2384 Darwin)
2385 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2386 ;;
2387 *)
2388 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
2389 ;;
2390 esac
Narayan Kamath9260bba2014-01-17 10:05:25 +00002391
2392 # Keep track of the fact that we set JAVA_HOME ourselves, so that
2393 # we can change it on the next envsetup.sh, if required.
2394 export ANDROID_SET_JAVA_HOME=true
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002395 fi
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002396}
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002397
Alex Rayf0d08eb2013-03-08 15:15:06 -08002398# Print colored exit condition
2399function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08002400 "$@"
2401 local retval=$?
2402 if [ $retval -ne 0 ]
2403 then
Roman Birgead9a5c2015-10-16 10:29:06 -07002404 printf "\e[0;31mFAILURE\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002405 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002406 printf "\e[0;32mSUCCESS\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002407 fi
2408 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08002409}
2410
Ying Wanged21d4c2014-08-24 22:14:19 -07002411function get_make_command()
2412{
2413 echo command make
2414}
2415
Anthony King40b093f2015-04-30 22:19:48 +01002416function mk_timer()
Ed Heylcc6be0a2014-06-18 14:55:58 -07002417{
2418 local start_time=$(date +"%s")
Anthony King40b093f2015-04-30 22:19:48 +01002419 $@
Ed Heylcc6be0a2014-06-18 14:55:58 -07002420 local ret=$?
2421 local end_time=$(date +"%s")
2422 local tdiff=$(($end_time-$start_time))
2423 local hours=$(($tdiff / 3600 ))
2424 local mins=$((($tdiff % 3600) / 60))
2425 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002426 local ncolors=$(tput colors 2>/dev/null)
2427 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
2428 color_failed="\e[0;31m"
2429 color_success="\e[0;32m"
Roman Birgead9a5c2015-10-16 10:29:06 -07002430 color_reset="\e[0m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002431 else
2432 color_failed=""
2433 color_success=""
2434 color_reset=""
2435 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07002436 echo
2437 if [ $ret -eq 0 ] ; then
Roman Birgead9a5c2015-10-16 10:29:06 -07002438 printf "${color_success}#### make completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002439 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002440 printf "${color_failed}#### make failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002441 fi
2442 if [ $hours -gt 0 ] ; then
2443 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
2444 elif [ $mins -gt 0 ] ; then
2445 printf "(%02g:%02g (mm:ss))" $mins $secs
2446 elif [ $secs -gt 0 ] ; then
2447 printf "(%s seconds)" $secs
2448 fi
Roman Birgead9a5c2015-10-16 10:29:06 -07002449 printf " ####${color_reset}\n\n"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002450 return $ret
2451}
2452
Anthony King40b093f2015-04-30 22:19:48 +01002453function make()
2454{
2455 mk_timer $(get_make_command) "$@"
2456}
2457
Raphael Moll70a86b02011-06-20 16:03:14 -07002458if [ "x$SHELL" != "x/bin/bash" ]; then
2459 case `ps -o command -p $$` in
2460 *bash*)
2461 ;;
Emilio López7ff9caa2013-11-03 13:05:43 -03002462 *zsh*)
2463 ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002464 *)
Emilio López7ff9caa2013-11-03 13:05:43 -03002465 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
Raphael Moll70a86b02011-06-20 16:03:14 -07002466 ;;
2467 esac
2468fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002469
2470# Execute the contents of any vendorsetup.sh files we can find.
Oleksiy Avramchenko15760a82014-10-06 18:51:58 +02002471for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
2472 `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 -08002473do
2474 echo "including $f"
2475 . $f
2476done
2477unset f
Kenny Root52aa81c2011-07-15 11:07:06 -07002478
Kyle Ladd031a0b62013-09-11 20:43:42 -04002479# Add completions
2480check_bash_version && {
2481 dirs="sdk/bash_completion vendor/cm/bash_completion"
2482 for dir in $dirs; do
2483 if [ -d ${dir} ]; then
2484 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
2485 echo "including $f"
2486 . $f
2487 done
2488 fi
2489 done
2490}
Chirayu Desaie1466d62013-03-19 17:50:37 +05302491
2492export ANDROID_BUILD_TOP=$(gettop)