blob: 446947f73ab1e61e7752eb899bb9d2c9afa549ce [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.
Martin Brabhamb77db582016-02-05 15:59:21 -050014- mmap: Builds all of the modules in the current directory, and its dependencies, then pushes the package to the device.
Daniel Bateman22185ba2012-08-04 03:48:09 -050015- mmmp: Builds all of the modules in the supplied directories and pushes them to the device.
Ying Wangb607f7b2013-02-08 18:01:04 -080016- mmma: Builds all of the modules in the supplied directories, and their dependencies.
Khalid Zubairfdc26cd2015-10-21 12:43:14 -070017- mms: Short circuit builder. Quickly re-build the kernel, rootfs, boot and system images
18 without deep dependencies. Requires the full build to have run before.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070019- cgrep: Greps on all local C/C++ files.
Jon Boekenoogencbca56f2014-04-07 10:57:38 -070020- ggrep: Greps on all local Gradle files.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070021- jgrep: Greps on all local Java files.
22- resgrep: Greps on all local res/*.xml files.
Trevor Drake9c889762015-01-30 04:42:21 +000023- mangrep: Greps on all local AndroidManifest.xml files.
24- sepgrep: Greps on all local sepolicy files.
Jeff Brown46cbd202014-07-26 15:15:41 -070025- sgrep: Greps on all local source files.
The Android Open Source Project88b60792009-03-03 19:28:42 -080026- godir: Go to the directory containing a file.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060027- cmremote: Add git remote for CM Gerrit Review
28- cmgerrit: A Git wrapper that fetches/pushes patch from/to CM Gerrit Review
29- cmrebase: Rebase a Gerrit change and push it again
Steve Kondik873fa562012-09-17 11:33:18 -070030- aospremote: Add git remote for matching AOSP repository
Steve Kondik20c21d22013-10-27 13:34:36 -070031- cafremote: Add git remote for matching CodeAurora repository.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060032- mka: Builds using SCHED_BATCH on all processors
Chirayu Desaicf2bdb62012-09-28 11:56:02 +053033- mkap: Builds the module(s) using mka and pushes them to the device.
34- cmka: Cleans and builds using mka.
Matt Mower8dacaed2013-12-29 12:57:20 -060035- repolastsync: Prints date and time of last repo sync.
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -060036- reposync: Parallel repo sync using ionice and SCHED_BATCH
Chirayu Desai4a319b82013-06-05 20:14:33 +053037- repopick: Utility to fetch changes from Gerrit.
Steve Kondik83c03d52012-10-24 16:40:42 -070038- installboot: Installs a boot.img to the connected device.
39- installrecovery: Installs a recovery.img to the connected device.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070040
Dan Albert4ae5d4b2014-10-31 16:23:08 -070041Environemnt options:
42- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
43 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
44 build is leak-check clean.
45
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046Look at the source to view more functions. The complete list is:
47EOF
48 T=$(gettop)
Jeff Brown46cbd202014-07-26 15:15:41 -070049 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 +010050 echo "$i"
51 done | column
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070052}
53
54# Get the value of a build variable as an absolute path.
55function get_abs_build_var()
56{
57 T=$(gettop)
58 if [ ! "$T" ]; then
59 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
60 return
61 fi
Ying Wang9cd17642012-12-13 10:52:07 -080062 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070063 command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070064}
65
66# Get the exact value of a build variable.
67function get_build_var()
68{
69 T=$(gettop)
70 if [ ! "$T" ]; then
71 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
72 return
73 fi
Andrew Boie6905e212013-12-19 13:21:46 -080074 (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
Ed Heylc6d11f82014-06-27 13:32:39 -070075 command make --no-print-directory -f build/core/config.mk dumpvar-$1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080076}
77
78# check to see if the supplied product is one we can build
79function check_product()
80{
81 T=$(gettop)
82 if [ ! "$T" ]; then
83 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
84 return
85 fi
Ricardo Cerqueira119d3bb2011-11-25 15:30:36 +000086
87 if (echo -n $1 | grep -q -e "^cm_") ; then
88 CM_BUILD=$(echo -n $1 | sed -e 's/^cm_//g')
Ricardo Cerqueira34ae3232013-09-26 00:10:20 +010089 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 +000090 else
91 CM_BUILD=
92 fi
93 export CM_BUILD
94
Jeff Browne33ba4c2011-07-11 22:11:46 -070095 TARGET_PRODUCT=$1 \
96 TARGET_BUILD_VARIANT= \
97 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -070098 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080099 get_build_var TARGET_DEVICE > /dev/null
100 # hide successful answers, but allow the errors to show
101}
102
103VARIANT_CHOICES=(user userdebug eng)
104
105# check to see if the supplied variant is valid
106function check_variant()
107{
108 for v in ${VARIANT_CHOICES[@]}
109 do
110 if [ "$v" = "$1" ]
111 then
112 return 0
113 fi
114 done
115 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700116}
117
118function setpaths()
119{
120 T=$(gettop)
121 if [ ! "$T" ]; then
122 echo "Couldn't locate the top of the tree. Try setting TOP."
123 return
124 fi
125
126 ##################################################################
127 # #
128 # Read me before you modify this code #
129 # #
130 # This function sets ANDROID_BUILD_PATHS to what it is adding #
131 # to PATH, and the next time it is run, it removes that from #
132 # PATH. This is required so lunch can be run more than once #
133 # and still have working paths. #
134 # #
135 ##################################################################
136
Raphael Mollc639c782011-06-20 17:25:01 -0700137 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
138 # due to "C:\Program Files" being in the path.
139
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700140 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700141 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700142 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
143 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700144 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700145 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800146 # strip leading ':', if any
147 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500148 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700149
150 # and in with the new
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700151 prebuiltdir=$(getprebuilt)
Jing Yuf5172c72012-03-29 20:45:50 -0700152 gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700153
Ben Cheng8bc4c432012-11-16 13:29:13 -0800154 # defined in core/config.mk
155 targetgccversion=$(get_build_var TARGET_GCC_VERSION)
Colin Cross03b424a2014-05-22 11:57:43 -0700156 targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Brandon McAnshfaf35352015-10-10 10:20:38 -0400157 targetlegacygccversion=$(get_build_var TARGET_LEGACY_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800158 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800159
Raphael Mollc639c782011-06-20 17:25:01 -0700160 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800161 export ANDROID_TOOLCHAIN=
162 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200163 local ARCH=$(get_build_var TARGET_ARCH)
164 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400165 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700166 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400167 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
168 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800169 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200170 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800171 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700172 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700173 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700174 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000175 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200176 *)
177 echo "Can't find toolchain for unknown architecture: $ARCH"
178 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700179 ;;
180 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700181 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800182 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700183 fi
Raphael732936d2011-06-22 14:35:32 -0700184
Ben Chengfba67bf2014-02-25 10:27:07 -0800185 if [ -d "$gccprebuiltdir/$toolchaindir2" ]; then
186 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
187 fi
188
189 unset ANDROID_KERNEL_TOOLCHAIN_PATH
Ying Wang08f5e9a2012-04-17 18:10:11 -0700190 case $ARCH in
Bruce Beare42ced6d2012-07-17 21:40:01 -0700191 arm)
Ben Chengfba67bf2014-02-25 10:27:07 -0800192 # Legacy toolchain configuration used for ARM kernel compilation
Brandon McAnshfaf35352015-10-10 10:20:38 -0400193 toolchaindir=arm/arm-eabi-$targetlegacygccversion/bin
Bruce Beare42ced6d2012-07-17 21:40:01 -0700194 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Torne (Richard Coles)f24c3562014-04-25 16:24:22 +0100195 export ARM_EABI_TOOLCHAIN="$gccprebuiltdir/$toolchaindir"
196 ANDROID_KERNEL_TOOLCHAIN_PATH="$ARM_EABI_TOOLCHAIN":
Bruce Beare42ced6d2012-07-17 21:40:01 -0700197 fi
Ying Wang08f5e9a2012-04-17 18:10:11 -0700198 ;;
199 *)
Bruce Beare42ced6d2012-07-17 21:40:01 -0700200 # No need to set ARM_EABI_TOOLCHAIN for other ARCHs
Ying Wang08f5e9a2012-04-17 18:10:11 -0700201 ;;
202 esac
Ying Wang08f5e9a2012-04-17 18:10:11 -0700203
Jeff Vander Stoep5aa68322015-06-12 09:56:39 -0700204 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Ying Wang2a859d52014-06-30 10:25:33 -0700205 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 +0200206
207 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
208 # to ensure that the corresponding 'emulator' binaries are used.
209 case $(uname -s) in
210 Darwin)
211 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
212 ;;
213 Linux)
214 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
215 ;;
216 *)
217 ANDROID_EMULATOR_PREBUILTS=
218 ;;
219 esac
220 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700221 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200222 export ANDROID_EMULATOR_PREBUILTS
223 fi
224
Ying Wangaa1c9b52012-11-26 20:51:59 -0800225 export PATH=$ANDROID_BUILD_PATHS$PATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800226
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500227 unset ANDROID_JAVA_TOOLCHAIN
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900228 unset ANDROID_PRE_BUILD_PATHS
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500229 if [ -n "$JAVA_HOME" ]; then
230 export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
Ji-Hwan Lee752ad062011-07-04 14:09:47 +0900231 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
232 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500233 fi
234
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800235 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700236 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
237 export OUT=$ANDROID_PRODUCT_OUT
238
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700239 unset ANDROID_HOST_OUT
240 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
241
Matt Mowerf8ff73e2015-06-09 19:35:53 -0500242 if [ -n "$ANDROID_CCACHE_DIR" ]; then
243 export CCACHE_DIR=$ANDROID_CCACHE_DIR
244 fi
245
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800246 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700247 # TODO: fix the path
248 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
249}
250
251function printconfig()
252{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800253 T=$(gettop)
254 if [ ! "$T" ]; then
255 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
256 return
257 fi
258 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700259}
260
261function set_stuff_for_environment()
262{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800263 settitle
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500264 set_java_home
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800265 setpaths
266 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700267
Ben Chengaac3f812013-08-13 14:38:15 -0700268 # With this environment variable new GCC can apply colors to warnings/errors
269 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 -0700270 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700271}
272
273function set_sequence_number()
274{
Joe Onoratoaee4daa2010-06-23 14:03:13 -0700275 export BUILD_ENV_SEQUENCE_NUMBER=10
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700276}
277
278function settitle()
279{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800280 if [ "$STAY_OFF_MY_LAWN" = "" ]; then
Raghu Gandham8da43102012-07-25 19:57:22 -0700281 local arch=$(gettargetarch)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700282 local product=$TARGET_PRODUCT
283 local variant=$TARGET_BUILD_VARIANT
284 local apps=$TARGET_BUILD_APPS
Steve Kondikd6fba552012-12-27 15:28:34 -0800285 if [ -z "$PROMPT_COMMAND" ]; then
286 # No prompts
287 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\""
288 elif [ -z "$(echo $PROMPT_COMMAND | grep '033]0;')" ]; then
289 # Prompts exist, but no hardstatus
290 PROMPT_COMMAND="echo -ne \"\033]0;${USER}@${HOSTNAME}: ${PWD}\007\";${PROMPT_COMMAND}"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700291 fi
Steve Kondikd6fba552012-12-27 15:28:34 -0800292 if [ ! -z "$ANDROID_PROMPT_PREFIX" ]; then
Christopher Laisfa8d9352013-06-03 15:15:39 -0500293 PROMPT_COMMAND="$(echo $PROMPT_COMMAND | sed -e 's/$ANDROID_PROMPT_PREFIX //g')"
Steve Kondikd6fba552012-12-27 15:28:34 -0800294 fi
295
296 if [ -z "$apps" ]; then
297 ANDROID_PROMPT_PREFIX="[${arch}-${product}-${variant}]"
298 else
299 ANDROID_PROMPT_PREFIX="[$arch $apps $variant]"
300 fi
301 export ANDROID_PROMPT_PREFIX
302
303 # Inject build data into hardstatus
304 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 -0800305 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700306}
307
Kyle Ladd031a0b62013-09-11 20:43:42 -0400308function check_bash_version()
Kenny Root52aa81c2011-07-15 11:07:06 -0700309{
Kenny Root52aa81c2011-07-15 11:07:06 -0700310 # Keep us from trying to run in something that isn't bash.
311 if [ -z "${BASH_VERSION}" ]; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400312 return 1
Kenny Root52aa81c2011-07-15 11:07:06 -0700313 fi
314
315 # Keep us from trying to run in bash that's too old.
James Roberts-Thomson24dd07d2013-04-16 15:53:39 +1200316 if [ "${BASH_VERSINFO[0]}" -lt 4 ] ; then
Kyle Ladd031a0b62013-09-11 20:43:42 -0400317 return 2
Kenny Root52aa81c2011-07-15 11:07:06 -0700318 fi
319
Kyle Ladd031a0b62013-09-11 20:43:42 -0400320 return 0
Kenny Root52aa81c2011-07-15 11:07:06 -0700321}
322
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700323function choosetype()
324{
325 echo "Build type choices are:"
326 echo " 1. release"
327 echo " 2. debug"
328 echo
329
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800330 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700331 DEFAULT_NUM=1
332 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800334 export TARGET_BUILD_TYPE=
335 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700336 while [ -z $TARGET_BUILD_TYPE ]
337 do
338 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800339 if [ -z "$1" ] ; then
340 read ANSWER
341 else
342 echo $1
343 ANSWER=$1
344 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700345 case $ANSWER in
346 "")
347 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
348 ;;
349 1)
350 export TARGET_BUILD_TYPE=release
351 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800352 release)
353 export TARGET_BUILD_TYPE=release
354 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700355 2)
356 export TARGET_BUILD_TYPE=debug
357 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800358 debug)
359 export TARGET_BUILD_TYPE=debug
360 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700361 *)
362 echo
363 echo "I didn't understand your response. Please try again."
364 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700365 ;;
366 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800367 if [ -n "$1" ] ; then
368 break
369 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700370 done
371
372 set_stuff_for_environment
373}
374
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800375#
376# This function isn't really right: It chooses a TARGET_PRODUCT
377# based on the list of boards. Usually, that gets you something
378# that kinda works with a generic product, but really, you should
379# pick a product by name.
380#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700381function chooseproduct()
382{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700383 if [ "x$TARGET_PRODUCT" != x ] ; then
384 default_value=$TARGET_PRODUCT
385 else
Jeff Browne33ba4c2011-07-11 22:11:46 -0700386 default_value=full
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700387 fi
388
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 export TARGET_PRODUCT=
390 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700391 while [ -z "$TARGET_PRODUCT" ]
392 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700393 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800394 if [ -z "$1" ] ; then
395 read ANSWER
396 else
397 echo $1
398 ANSWER=$1
399 fi
400
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700401 if [ -z "$ANSWER" ] ; then
402 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800403 else
404 if check_product $ANSWER
405 then
406 export TARGET_PRODUCT=$ANSWER
407 else
408 echo "** Not a valid product: $ANSWER"
409 fi
410 fi
411 if [ -n "$1" ] ; then
412 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700413 fi
414 done
415
416 set_stuff_for_environment
417}
418
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800419function choosevariant()
420{
421 echo "Variant choices are:"
422 local index=1
423 local v
424 for v in ${VARIANT_CHOICES[@]}
425 do
426 # The product name is the name of the directory containing
427 # the makefile we found, above.
428 echo " $index. $v"
429 index=$(($index+1))
430 done
431
432 local default_value=eng
433 local ANSWER
434
435 export TARGET_BUILD_VARIANT=
436 while [ -z "$TARGET_BUILD_VARIANT" ]
437 do
438 echo -n "Which would you like? [$default_value] "
439 if [ -z "$1" ] ; then
440 read ANSWER
441 else
442 echo $1
443 ANSWER=$1
444 fi
445
446 if [ -z "$ANSWER" ] ; then
447 export TARGET_BUILD_VARIANT=$default_value
448 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
449 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800450 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800451 fi
452 else
453 if check_variant $ANSWER
454 then
455 export TARGET_BUILD_VARIANT=$ANSWER
456 else
457 echo "** Not a valid variant: $ANSWER"
458 fi
459 fi
460 if [ -n "$1" ] ; then
461 break
462 fi
463 done
464}
465
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466function choosecombo()
467{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700468 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700469
470 echo
471 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700472 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700473
474 echo
475 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700476 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800477
478 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700479 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481}
482
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800483# Clear this variable. It will be built up again when the vendorsetup.sh
484# files are included at the end of this file.
485unset LUNCH_MENU_CHOICES
486function add_lunch_combo()
487{
488 local new_combo=$1
489 local c
490 for c in ${LUNCH_MENU_CHOICES[@]} ; do
491 if [ "$new_combo" = "$c" ] ; then
492 return
493 fi
494 done
495 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
496}
497
498# add the default one here
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700499add_lunch_combo aosp_arm-eng
Colin Cross4f0eb7d2014-01-21 19:35:38 -0800500add_lunch_combo aosp_arm64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000501add_lunch_combo aosp_mips-eng
Chris Dearman1efd9e42014-02-03 15:01:24 -0800502add_lunch_combo aosp_mips64-eng
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000503add_lunch_combo aosp_x86-eng
504add_lunch_combo aosp_x86_64-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800505
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506function print_lunch_menu()
507{
508 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 echo
510 echo "You're building on" $uname
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000511 if [ "$(uname)" = "Darwin" ] ; then
512 echo " (ohai, koush!)"
513 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700514 echo
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000515 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
516 echo "Breakfast menu... pick a combo:"
517 else
518 echo "Lunch menu... pick a combo:"
519 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800520
521 local i=1
522 local choice
523 for choice in ${LUNCH_MENU_CHOICES[@]}
524 do
cybojenixa5dd6ce2013-06-28 20:30:00 +0100525 echo " $i. $choice "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800526 i=$(($i+1))
cybojenixa5dd6ce2013-06-28 20:30:00 +0100527 done | column
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000529 if [ "z${CM_DEVICES_ONLY}" != "z" ]; then
530 echo "... and don't forget the bacon!"
531 fi
532
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700533 echo
534}
535
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000536function brunch()
537{
538 breakfast $*
539 if [ $? -eq 0 ]; then
540 mka bacon
541 else
542 echo "No such item in brunch menu. Try 'breakfast'"
543 return 1
544 fi
545 return $?
546}
547
548function breakfast()
549{
550 target=$1
JustArchif70c7e62014-06-22 14:37:30 +0200551 local variant=$2
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000552 CM_DEVICES_ONLY="true"
553 unset LUNCH_MENU_CHOICES
554 add_lunch_combo full-eng
555 for f in `/bin/ls vendor/cm/vendorsetup.sh 2> /dev/null`
556 do
557 echo "including $f"
558 . $f
559 done
560 unset f
561
562 if [ $# -eq 0 ]; then
563 # No arguments, so let's have the full menu
564 lunch
565 else
566 echo "z$target" | grep -q "-"
567 if [ $? -eq 0 ]; then
568 # A buildtype was specified, assume a full device name
569 lunch $target
570 else
571 # This is probably just the CM model name
JustArchif70c7e62014-06-22 14:37:30 +0200572 if [ -z "$variant" ]; then
573 variant="userdebug"
574 fi
575 lunch cm_$target-$variant
Ricardo Cerqueira8b2014d2011-01-31 00:49:49 +0000576 fi
577 fi
578 return $?
579}
580
581alias bib=breakfast
582
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583function lunch()
584{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 local answer
Anthony King850627c2015-04-30 22:57:08 +0100586 LUNCH_MENU_CHOICES=($(for l in ${LUNCH_MENU_CHOICES[@]}; do echo "$l"; done | sort))
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800587
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700588 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 else
591 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700592 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800593 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594 fi
595
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 local selection=
597
598 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700600 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800601 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
602 then
603 if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
604 then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800605 selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800606 fi
607 elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
608 then
609 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700610 fi
611
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800612 if [ -z "$selection" ]
613 then
614 echo
615 echo "Invalid lunch combo: $answer"
616 return 1
617 fi
618
Joe Onoratoda12daf2010-06-09 18:18:31 -0700619 export TARGET_BUILD_APPS=
620
Jeff Browne33ba4c2011-07-11 22:11:46 -0700621 local product=$(echo -n $selection | sed -e "s/-.*$//")
622 check_product $product
623 if [ $? -ne 0 ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624 then
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800625 # if we can't find a product, try to grab it off the CM github
626 T=$(gettop)
627 pushd $T > /dev/null
628 build/tools/roomservice.py $product
629 popd > /dev/null
630 check_product $product
Diogo Ferreira0d109172012-03-18 21:18:29 +0000631 else
632 build/tools/roomservice.py $product true
Koushik Dutta780fb5f2011-11-26 18:51:42 -0800633 fi
634 if [ $? -ne 0 ]
635 then
Jeff Browne33ba4c2011-07-11 22:11:46 -0700636 echo
637 echo "** Don't have a product spec for: '$product'"
638 echo "** Do you have the right repo manifest?"
639 product=
640 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800641
Jeff Browne33ba4c2011-07-11 22:11:46 -0700642 local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//")
643 check_variant $variant
644 if [ $? -ne 0 ]
645 then
646 echo
647 echo "** Invalid variant: '$variant'"
648 echo "** Must be one of ${VARIANT_CHOICES[@]}"
649 variant=
650 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800651
Jeff Browne33ba4c2011-07-11 22:11:46 -0700652 if [ -z "$product" -o -z "$variant" ]
653 then
654 echo
655 return 1
656 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800657
Jeff Browne33ba4c2011-07-11 22:11:46 -0700658 export TARGET_PRODUCT=$product
659 export TARGET_BUILD_VARIANT=$variant
660 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700661
662 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800663
Chirayu Desaib89b3242013-06-30 10:04:25 +0530664 fixup_common_out_dir
665
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700666 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800667 printconfig
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700668}
669
Jeff Davidson513d7a42010-08-02 10:00:44 -0700670# Tab completion for lunch.
671function _lunch()
672{
673 local cur prev opts
674 COMPREPLY=()
675 cur="${COMP_WORDS[COMP_CWORD]}"
676 prev="${COMP_WORDS[COMP_CWORD-1]}"
677
678 COMPREPLY=( $(compgen -W "${LUNCH_MENU_CHOICES[*]}" -- ${cur}) )
679 return 0
680}
Emilio López9ad6eea2013-11-03 13:02:13 -0300681complete -F _lunch lunch 2>/dev/null
Jeff Davidson513d7a42010-08-02 10:00:44 -0700682
Joe Onoratoda12daf2010-06-09 18:18:31 -0700683# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700684# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700685function tapas()
686{
Ying Wangb541ab62014-05-29 17:57:40 -0700687 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 -0700688 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700689 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
690 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 -0700691
Ying Wang67f02922012-08-22 10:25:20 -0700692 if [ $(echo $arch | wc -w) -gt 1 ]; then
693 echo "tapas: Error: Multiple build archs supplied: $arch"
694 return
695 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700696 if [ $(echo $variant | wc -w) -gt 1 ]; then
697 echo "tapas: Error: Multiple build variants supplied: $variant"
698 return
699 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700700 if [ $(echo $density | wc -w) -gt 1 ]; then
701 echo "tapas: Error: Multiple densities supplied: $density"
702 return
703 fi
Ying Wang67f02922012-08-22 10:25:20 -0700704
705 local product=full
706 case $arch in
Ying Wangb541ab62014-05-29 17:57:40 -0700707 x86) product=full_x86;;
708 mips) product=full_mips;;
709 armv5) product=generic_armv5;;
710 arm64) product=aosp_arm64;;
711 x86_64) product=aosp_x86_64;;
712 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700713 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700714 if [ -z "$variant" ]; then
715 variant=eng
716 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700717 if [ -z "$apps" ]; then
718 apps=all
719 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600720 if [ -z "$density" ]; then
721 density=alldpi
722 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700723
Ying Wang67f02922012-08-22 10:25:20 -0700724 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700725 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700726 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700727 export TARGET_BUILD_TYPE=release
728 export TARGET_BUILD_APPS=$apps
729
730 set_stuff_for_environment
731 printconfig
732}
733
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100734function eat()
735{
736 if [ "$OUT" ] ; then
Chirayu Desai80a277d2013-05-04 17:59:18 +0530737 MODVERSION=$(get_build_var CM_VERSION)
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100738 ZIPFILE=cm-$MODVERSION.zip
739 ZIPPATH=$OUT/$ZIPFILE
740 if [ ! -f $ZIPPATH ] ; then
741 echo "Nothing to eat"
742 return 1
743 fi
744 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Steve Kondik58fa7162015-11-15 18:47:58 -0800745 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 +0100746 echo "No device is online. Waiting for one..."
747 echo "Please connect USB and/or enable USB debugging"
Steve Kondik58fa7162015-11-15 18:47:58 -0800748 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 +0100749 sleep 1
750 done
751 echo "Device Found.."
752 fi
Steve Kondikec4627c2015-07-10 02:31:24 -0700753 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Chirayu Desai6dadb572012-12-26 10:53:58 +0530754 then
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100755 # if adbd isn't root we can't write to /cache/recovery/
756 adb root
757 sleep 1
758 adb wait-for-device
Steve Kondik464574f2013-04-13 07:19:52 -0700759 cat << EOF > /tmp/command
Steve Kondike9f828c2015-11-10 23:51:21 +0100760--sideload_auto_reboot
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100761EOF
Steve Kondik464574f2013-04-13 07:19:52 -0700762 if adb push /tmp/command /cache/recovery/ ; then
763 echo "Rebooting into recovery for sideload installation"
764 adb reboot recovery
765 adb wait-for-sideload
766 adb sideload $ZIPPATH
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100767 fi
Steve Kondik464574f2013-04-13 07:19:52 -0700768 rm /tmp/command
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100769 else
770 echo "Nothing to eat"
771 return 1
772 fi
773 return $?
Chirayu Desai6dadb572012-12-26 10:53:58 +0530774 else
775 echo "The connected device does not appear to be $CM_BUILD, run away!"
776 fi
Ricardo Cerqueiradbc3c4e2011-08-19 20:37:12 +0100777}
778
Nebojsa Cvetkovicd5c0c872012-11-09 23:02:54 +0000779function omnom
780{
781 brunch $*
782 eat
783}
784
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700785function gettop
786{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800787 local TOPFILE=build/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700789 # The following circumlocution ensures we remove symlinks from TOP.
790 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700791 else
792 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800793 # The following circumlocution (repeated below as well) ensures
794 # that we record the true directory name and not one that is
795 # faked up with symlink names.
796 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700797 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800798 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700799 T=
800 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800801 \cd ..
synergyb112a402013-07-05 19:47:47 -0700802 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700803 done
Ying Wang9cd17642012-12-13 10:52:07 -0800804 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700805 if [ -f "$T/$TOPFILE" ]; then
806 echo $T
807 fi
808 fi
809 fi
810}
811
Andrew Hsieh906cb782013-09-10 17:37:14 +0800812# Return driver for "make", if any (eg. static analyzer)
813function getdriver()
814{
815 local T="$1"
816 test "$WITH_STATIC_ANALYZER" = "0" && unset WITH_STATIC_ANALYZER
817 if [ -n "$WITH_STATIC_ANALYZER" ]; then
818 echo "\
Andrew Hsiehc4f7fba2014-03-03 16:53:17 +0800819$T/prebuilts/misc/linux-x86/analyzer/tools/scan-build/scan-build \
820--use-analyzer $T/prebuilts/misc/linux-x86/analyzer/bin/analyzer \
Andrew Hsieh906cb782013-09-10 17:37:14 +0800821--status-bugs \
822--top=$T"
823 fi
824}
825
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826function m()
827{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800828 local T=$(gettop)
829 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700830 if [ "$T" ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800831 $DRV make -C $T -f build/core/main.mk $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700832 else
833 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700834 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700835 fi
836}
837
838function findmakefile()
839{
840 TOPFILE=build/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800841 local HERE=$PWD
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700842 T=
843 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700844 T=`PWD= /bin/pwd`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700845 if [ -f "$T/Android.mk" ]; then
846 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800847 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700848 return
849 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800850 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700851 done
Ying Wang9cd17642012-12-13 10:52:07 -0800852 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700853}
854
855function mm()
856{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800857 local T=$(gettop)
858 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700859 # If we're sitting in the root of the build tree, just do a
860 # normal make.
861 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800862 $DRV make $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700863 else
864 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800865 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700866 local MODULES=
867 local GET_INSTALL_PATH=
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=
Khalid Zubaircbf96922016-03-02 14:01:45 -0800884 # set all args to 'GET-INSTALL-PATH'
885 set -- GET-INSTALL-PATH
Ying Wanga7deb082013-08-16 13:24:47 -0700886 else
887 MODULES=all_modules
Ying Wanga7deb082013-08-16 13:24:47 -0700888 fi
Khalid Zubaircbf96922016-03-02 14:01:45 -0800889 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES "$@"
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=
Khalid Zubair521aa1e2016-02-11 14:34:12 -0800904
Roman Birg5771f5d2016-03-01 10:27:51 -0800905 if [ "$(__detect_shell)" = "zsh" ]; then
Khalid Zubair521aa1e2016-02-11 14:34:12 -0800906 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
907 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
908 else
909 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
910 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
911 fi
912
The Android Open Source Project88b60792009-03-03 19:28:42 -0800913 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800914 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
915 if [ "$MODULES" = "" ]; then
916 MODULES=all_modules
917 fi
918 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700919 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700920 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
921 local TO_CHOP=`expr $TO_CHOP + 1`
922 local START=`PWD= /bin/pwd`
923 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700924 if [ "$MFILE" = "" ] ; then
925 MFILE=$DIR/Android.mk
926 else
927 MFILE=$MFILE/$DIR/Android.mk
928 fi
929 MAKEFILE="$MAKEFILE $MFILE"
930 else
Ying Wanga7deb082013-08-16 13:24:47 -0700931 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100932 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700933 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997c3eb1072015-10-18 19:49:13 +0200934 *) if [ -d $DIR ]; then
935 echo "No Android.mk in $DIR.";
936 else
937 echo "Couldn't locate the directory $DIR";
938 fi
939 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700940 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700941 fi
942 done
Ying Wanga7deb082013-08-16 13:24:47 -0700943 if [ -n "$GET_INSTALL_PATH" ]; then
944 ARGS=$GET_INSTALL_PATH
945 MODULES=
946 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800947 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 -0700948 else
949 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700950 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700951 fi
952}
953
Ying Wangb607f7b2013-02-08 18:01:04 -0800954function mma()
955{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800956 local T=$(gettop)
957 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800958 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800959 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800960 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800961 if [ ! "$T" ]; then
962 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700963 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800964 fi
965 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800966 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800967 fi
968}
969
970function mmma()
971{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800972 local T=$(gettop)
973 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800974 if [ "$T" ]; then
Roman Birg5771f5d2016-03-01 10:27:51 -0800975 if [ "$(__detect_shell)" = "zsh" ]; then
Khalid Zubair521aa1e2016-02-11 14:34:12 -0800976 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
977 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
978 else
979 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
980 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
981 fi
Ying Wangb607f7b2013-02-08 18:01:04 -0800982 local MY_PWD=`PWD= /bin/pwd`
983 if [ "$MY_PWD" = "$T" ]; then
984 MY_PWD=
985 else
986 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
987 fi
988 local DIR=
989 local MODULE_PATHS=
990 local ARGS=
991 for DIR in $DIRS ; do
992 if [ -d $DIR ]; then
993 if [ "$MY_PWD" = "" ]; then
994 MODULE_PATHS="$MODULE_PATHS $DIR"
995 else
996 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
997 fi
998 else
999 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +01001000 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -08001001 *) echo "Couldn't find directory $DIR"; return 1;;
1002 esac
1003 fi
1004 done
Andrew Hsieh906cb782013-09-10 17:37:14 +08001005 $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 -08001006 else
1007 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -07001008 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -08001009 fi
1010}
1011
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001012function croot()
1013{
1014 T=$(gettop)
1015 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001016 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001017 else
1018 echo "Couldn't locate the top of the tree. Try setting TOP."
1019 fi
1020}
1021
nebkatfb67a1e2012-12-28 10:40:45 +00001022function cout()
1023{
1024 if [ "$OUT" ]; then
1025 cd $OUT
1026 else
1027 echo "Couldn't locate out directory. Try setting OUT."
1028 fi
1029}
1030
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001031function cproj()
1032{
1033 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001034 local HERE=$PWD
1035 T=
1036 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1037 T=$PWD
1038 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001039 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001040 return
1041 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001042 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001043 done
Ying Wang9cd17642012-12-13 10:52:07 -08001044 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001045 echo "can't find Android.mk"
1046}
1047
Daniel Sandler47e0a882013-07-30 13:23:52 -04001048# simplified version of ps; output in the form
1049# <pid> <procname>
1050function qpid() {
1051 local prepend=''
1052 local append=''
1053 if [ "$1" = "--exact" ]; then
1054 prepend=' '
1055 append='$'
1056 shift
1057 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1058 echo "usage: qpid [[--exact] <process name|pid>"
1059 return 255
1060 fi
1061
1062 local EXE="$1"
1063 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001064 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001065 else
1066 adb shell ps \
1067 | tr -d '\r' \
1068 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1069 fi
1070}
1071
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001072function pid()
1073{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001074 local prepend=''
1075 local append=''
1076 if [ "$1" = "--exact" ]; then
1077 prepend=' '
1078 append='$'
1079 shift
1080 fi
1081 local EXE="$1"
1082 if [ "$EXE" ] ; then
1083 local PID=`adb shell ps \
1084 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001085 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001086 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1087 echo "$PID"
1088 else
1089 echo "usage: pid [--exact] <process name>"
1090 return 255
1091 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001092}
1093
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001094# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001095# that has the core-file-size limit set correctly
1096#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001097# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001098# if its core-file-size limit is not set already.
1099# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1100
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001101function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001102{
1103 echo "Getting root...";
1104 adb root;
1105 adb wait-for-device;
1106
1107 echo "Remounting root parition read-write...";
1108 adb shell mount -w -o remount -t rootfs rootfs;
1109 sleep 1;
1110 adb wait-for-device;
1111 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001112 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001113 adb shell chmod 0777 /cores;
1114
1115 echo "Granting SELinux permission to dump in /cores...";
1116 adb shell restorecon -R /cores;
1117
1118 echo "Set core pattern.";
1119 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1120
1121 echo "Done."
1122}
1123
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001124# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001125# $1 = PID of process (e.g., $(pid mediaserver))
1126#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001127# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001128# dump to actually be generated.
1129
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001130function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001131{
1132 local PID=$1;
1133 if [ -z "$PID" ]; then
1134 printf "Expecting a PID!\n";
1135 return;
1136 fi;
1137 echo "Setting core limit for $PID to infinite...";
1138 adb shell prlimit $PID 4 -1 -1
1139}
1140
1141# core - send SIGV and pull the core for process
1142# $1 = PID of process (e.g., $(pid mediaserver))
1143#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001144# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001145# enabled globally.
1146
1147function core()
1148{
1149 local PID=$1;
1150
1151 if [ -z "$PID" ]; then
1152 printf "Expecting a PID!\n";
1153 return;
1154 fi;
1155
1156 local CORENAME=core.$PID;
1157 local COREPATH=/cores/$CORENAME;
1158 local SIG=SEGV;
1159
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001160 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001161
1162 local done=0;
1163 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1164 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1165 adb shell kill -$SIG $PID;
1166 sleep 1;
1167 done;
1168
1169 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1170 echo "Done: core is under $COREPATH on device.";
1171}
1172
Christopher Tate744ee802009-11-12 15:33:08 -08001173# systemstack - dump the current stack trace of all threads in the system process
1174# to the usual ANR traces file
1175function systemstack()
1176{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001177 stacks system_server
1178}
1179
1180function stacks()
1181{
1182 if [[ $1 =~ ^[0-9]+$ ]] ; then
1183 local PID="$1"
1184 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001185 local PIDLIST="$(pid $1)"
1186 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1187 local PID="$PIDLIST"
1188 elif [ "$PIDLIST" ] ; then
1189 echo "more than one process: $1"
1190 else
1191 echo "no such process: $1"
1192 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001193 else
1194 echo "usage: stacks [pid|process name]"
1195 fi
1196
1197 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001198 # Determine whether the process is native
1199 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1200 # Dump stacks of Dalvik process
1201 local TRACES=/data/anr/traces.txt
1202 local ORIG=/data/anr/traces.orig
1203 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001204
Jeff Brownb12c2e52013-08-19 15:14:16 -07001205 # Keep original traces to avoid clobbering
1206 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001207
Jeff Brownb12c2e52013-08-19 15:14:16 -07001208 # Make sure we have a usable file
1209 adb shell touch $TRACES
1210 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001211
Jeff Brownb12c2e52013-08-19 15:14:16 -07001212 # Dump stacks and wait for dump to finish
1213 adb shell kill -3 $PID
1214 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001215
Jeff Brownb12c2e52013-08-19 15:14:16 -07001216 # Restore original stacks, and show current output
1217 adb shell mv $TRACES $TMP
1218 adb shell mv $ORIG $TRACES
1219 adb shell cat $TMP
1220 else
1221 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001222 local USE64BIT="$(is64bit $PID)"
1223 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001224 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001225 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001226}
1227
Michael Wrightaeed7212014-06-19 19:58:12 -07001228# Read the ELF header from /proc/$PID/exe to determine if the process is
1229# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001230function is64bit()
1231{
1232 local PID="$1"
1233 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001234 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001235 echo "64"
1236 else
1237 echo ""
1238 fi
1239 else
1240 echo ""
1241 fi
1242}
1243
Clark Scheff75b36a42014-12-23 13:30:51 -08001244function dddclient()
1245{
1246 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1247 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1248 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1249 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1250 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1251 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
1252 local ARCH=$(get_build_var TARGET_ARCH)
1253 local GDB
1254 case "$ARCH" in
1255 arm) GDB=arm-linux-androideabi-gdb;;
1256 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1257 mips|mips64) GDB=mips64el-linux-android-gdb;;
1258 x86) GDB=x86_64-linux-android-gdb;;
1259 x86_64) GDB=x86_64-linux-android-gdb;;
1260 *) echo "Unknown arch $ARCH"; return 1;;
1261 esac
1262
1263 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1264 local EXE="$1"
1265 if [ "$EXE" ] ; then
1266 EXE=$1
1267 if [[ $EXE =~ ^[^/].* ]] ; then
1268 EXE="system/bin/"$EXE
1269 fi
1270 else
1271 EXE="app_process"
1272 fi
1273
1274 local PORT="$2"
1275 if [ "$PORT" ] ; then
1276 PORT=$2
1277 else
1278 PORT=":5039"
1279 fi
1280
1281 local PID="$3"
1282 if [ "$PID" ] ; then
1283 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1284 PID=`pid $3`
1285 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1286 # that likely didn't work because of returning multiple processes
1287 # try again, filtering by root processes (don't contain colon)
1288 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1289 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1290 then
1291 echo "Couldn't resolve '$3' to single PID"
1292 return 1
1293 else
1294 echo ""
1295 echo "WARNING: multiple processes matching '$3' observed, using root process"
1296 echo ""
1297 fi
1298 fi
1299 fi
1300 adb forward "tcp$PORT" "tcp$PORT"
1301 local USE64BIT="$(is64bit $PID)"
1302 adb shell gdbserver$USE64BIT $PORT --attach $PID &
1303 sleep 2
1304 else
1305 echo ""
1306 echo "If you haven't done so already, do this first on the device:"
1307 echo " gdbserver $PORT /system/bin/$EXE"
1308 echo " or"
1309 echo " gdbserver $PORT --attach <PID>"
1310 echo ""
1311 fi
1312
1313 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1314 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1315
1316 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1317 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"
1318 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1319 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1320 # Enable special debugging for ART processes.
1321 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1322 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1323 fi
1324 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1325
1326 local WHICH_GDB=
1327 # 64-bit exe found
1328 if [ "$USE64BIT" != "" ] ; then
1329 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1330 # 32-bit exe / 32-bit platform
1331 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1332 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1333 # 32-bit exe / 64-bit platform
1334 else
1335 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1336 fi
1337
1338 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1339 else
1340 echo "Unable to determine build system output dir."
1341 fi
1342}
1343
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001344case `uname -s` in
1345 Darwin)
1346 function sgrep()
1347 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001348 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 -07001349 }
1350
1351 ;;
1352 *)
1353 function sgrep()
1354 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001355 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 -07001356 }
1357 ;;
1358esac
1359
Raghu Gandham8da43102012-07-25 19:57:22 -07001360function gettargetarch
1361{
1362 get_build_var TARGET_ARCH
1363}
1364
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001365function ggrep()
1366{
1367 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1368}
1369
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001370function jgrep()
1371{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001372 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 -07001373}
1374
1375function cgrep()
1376{
Dan Albert01961192014-11-22 10:16:01 -08001377 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 -07001378}
1379
1380function resgrep()
1381{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001382 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 -07001383}
1384
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001385function mangrep()
1386{
1387 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1388}
1389
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001390function sepgrep()
1391{
1392 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 "$@"
1393}
1394
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001395function rcgrep()
1396{
1397 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" -print0 | xargs -0 grep --color -n "$@"
1398}
1399
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001400case `uname -s` in
1401 Darwin)
1402 function mgrep()
1403 {
Ying Wang324c2b22012-08-17 15:03:20 -07001404 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 -07001405 }
1406
1407 function treegrep()
1408 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001409 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 -07001410 }
1411
1412 ;;
1413 *)
1414 function mgrep()
1415 {
Ying Wang324c2b22012-08-17 15:03:20 -07001416 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 -07001417 }
1418
1419 function treegrep()
1420 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001421 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 -07001422 }
1423
1424 ;;
1425esac
1426
1427function getprebuilt
1428{
1429 get_abs_build_var ANDROID_PREBUILTS
1430}
1431
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001432function tracedmdump()
1433{
1434 T=$(gettop)
1435 if [ ! "$T" ]; then
1436 echo "Couldn't locate the top of the tree. Try setting TOP."
1437 return
1438 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001439 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001440 local arch=$(gettargetarch)
1441 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001442
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001443 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001444 if [ ! "$TRACE" ] ; then
1445 echo "usage: tracedmdump tracename"
1446 return
1447 fi
1448
Jack Veenstra60116fc2009-04-09 18:12:34 -07001449 if [ ! -r "$KERNEL" ] ; then
1450 echo "Error: cannot find kernel: '$KERNEL'"
1451 return
1452 fi
1453
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001454 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001455 if [ "$BASETRACE" = "$TRACE" ] ; then
1456 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1457 fi
1458
1459 echo "post-processing traces..."
1460 rm -f $TRACE/qtrace.dexlist
1461 post_trace $TRACE
1462 if [ $? -ne 0 ]; then
1463 echo "***"
1464 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1465 echo "***"
1466 return
1467 fi
1468 echo "generating dexlist output..."
1469 /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
1470 echo "generating dmtrace data..."
1471 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1472 echo "generating html file..."
1473 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1474 echo "done, see $TRACE/dmtrace.html for details"
1475 echo "or run:"
1476 echo " traceview $TRACE/dmtrace"
1477}
1478
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001479# communicate with a running device or emulator, set up necessary state,
1480# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001481function runhat()
1482{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001483 # process standard adb options
1484 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001485 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001486 adbTarget=$1
1487 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001488 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001489 adbTarget="$1 $2"
1490 shift 2
1491 fi
1492 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001493 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001494
1495 # runhat options
1496 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001497
1498 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001499 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001500 return
1501 fi
1502
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001503 # confirm hat is available
1504 if [ -z $(which hat) ]; then
1505 echo "hat is not available in this configuration."
1506 return
1507 fi
1508
Andy McFaddenb6289852010-07-12 08:00:19 -07001509 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001510 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001511 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001512 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001513 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001514 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001515 echo -n "> "
1516 read
1517
The Android Open Source Project88b60792009-03-03 19:28:42 -08001518 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001519
The Android Open Source Project88b60792009-03-03 19:28:42 -08001520 echo "Retrieving file $devFile..."
1521 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001522
The Android Open Source Project88b60792009-03-03 19:28:42 -08001523 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001524
The Android Open Source Project88b60792009-03-03 19:28:42 -08001525 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001526 echo "View the output by pointing your browser at http://localhost:7000/"
1527 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001528 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001529}
1530
1531function getbugreports()
1532{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001533 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001534
1535 if [ ! "$reports" ]; then
1536 echo "Could not locate any bugreports."
1537 return
1538 fi
1539
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001540 local report
1541 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001542 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001543 echo "/sdcard/bugreports/${report}"
1544 adb pull /sdcard/bugreports/${report} ${report}
1545 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001546 done
1547}
1548
Victoria Lease1b296b42012-08-21 15:44:06 -07001549function getsdcardpath()
1550{
1551 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1552}
1553
1554function getscreenshotpath()
1555{
1556 echo "$(getsdcardpath)/Pictures/Screenshots"
1557}
1558
1559function getlastscreenshot()
1560{
1561 local screenshot_path=$(getscreenshotpath)
1562 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1563 if [ "$screenshot" = "" ]; then
1564 echo "No screenshots found."
1565 return
1566 fi
1567 echo "${screenshot}"
1568 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1569}
1570
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001571function startviewserver()
1572{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001573 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001574 if [ $# -gt 0 ]; then
1575 port=$1
1576 fi
1577 adb shell service call window 1 i32 $port
1578}
1579
1580function stopviewserver()
1581{
1582 adb shell service call window 2
1583}
1584
1585function isviewserverstarted()
1586{
1587 adb shell service call window 3
1588}
1589
Romain Guyb84049a2010-10-04 16:56:11 -07001590function key_home()
1591{
1592 adb shell input keyevent 3
1593}
1594
1595function key_back()
1596{
1597 adb shell input keyevent 4
1598}
1599
1600function key_menu()
1601{
1602 adb shell input keyevent 82
1603}
1604
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001605function smoketest()
1606{
1607 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1608 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1609 return
1610 fi
1611 T=$(gettop)
1612 if [ ! "$T" ]; then
1613 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1614 return
1615 fi
1616
Ying Wang9cd17642012-12-13 10:52:07 -08001617 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001618 adb uninstall com.android.smoketest > /dev/null &&
1619 adb uninstall com.android.smoketest.tests > /dev/null &&
1620 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1621 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1622 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1623}
1624
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001625# simple shortcut to the runtest command
1626function runtest()
1627{
1628 T=$(gettop)
1629 if [ ! "$T" ]; then
1630 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1631 return
1632 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001633 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001634}
1635
The Android Open Source Project88b60792009-03-03 19:28:42 -08001636function godir () {
1637 if [[ -z "$1" ]]; then
1638 echo "Usage: godir <regex>"
1639 return
1640 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001641 T=$(gettop)
Brian Carlstromf2257422015-09-30 20:28:54 -07001642 if [ ! "$OUT_DIR" = "" ]; then
1643 mkdir -p $OUT_DIR
1644 FILELIST=$OUT_DIR/filelist
1645 else
1646 FILELIST=$T/filelist
1647 fi
1648 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001649 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001650 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001651 echo " Done"
1652 echo ""
1653 fi
1654 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001655 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001656 if [[ ${#lines[@]} = 0 ]]; then
1657 echo "Not found"
1658 return
1659 fi
1660 local pathname
1661 local choice
1662 if [[ ${#lines[@]} > 1 ]]; then
1663 while [[ -z "$pathname" ]]; do
1664 local index=1
1665 local line
1666 for line in ${lines[@]}; do
1667 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001668 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001669 done
1670 echo
1671 echo -n "Select one: "
1672 unset choice
1673 read choice
1674 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1675 echo "Invalid choice"
1676 continue
1677 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001678 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001679 done
1680 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001681 pathname=${lines[0]}
1682 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001683 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001684}
1685
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001686function cmremote()
1687{
1688 git remote rm cmremote 2> /dev/null
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301689 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001690 if [ -z "$GERRIT_REMOTE" ]
1691 then
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301692 echo Unable to set up the git remote, are you under a git repo?
1693 return 0
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001694 fi
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301695 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001696 if [ -z "$CMUSER" ]
1697 then
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001698 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001699 else
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001700 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001701 fi
1702 echo You can now push to "cmremote".
1703}
Koushik Duttab55f13a2012-05-14 16:14:36 -07001704
Steve Kondik873fa562012-09-17 11:33:18 -07001705function aospremote()
1706{
1707 git remote rm aosp 2> /dev/null
1708 if [ ! -d .git ]
1709 then
1710 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1711 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001712 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik873fa562012-09-17 11:33:18 -07001713 if (echo $PROJECT | grep -qv "^device")
1714 then
1715 PFX="platform/"
1716 fi
1717 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1718 echo "Remote 'aosp' created"
1719}
Steve Kondik873fa562012-09-17 11:33:18 -07001720
Steve Kondik20c21d22013-10-27 13:34:36 -07001721function cafremote()
1722{
1723 git remote rm caf 2> /dev/null
1724 if [ ! -d .git ]
1725 then
1726 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1727 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001728 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik20c21d22013-10-27 13:34:36 -07001729 if (echo $PROJECT | grep -qv "^device")
1730 then
1731 PFX="platform/"
1732 fi
1733 git remote add caf git://codeaurora.org/$PFX$PROJECT
1734 echo "Remote 'caf' created"
1735}
Steve Kondik20c21d22013-10-27 13:34:36 -07001736
Steve Kondikf00e7d92012-09-23 23:46:55 -07001737function installboot()
1738{
1739 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1740 then
1741 echo "No recovery.fstab found. Build recovery first."
1742 return 1
1743 fi
1744 if [ ! -e "$OUT/boot.img" ];
1745 then
1746 echo "No boot.img found. Run make bootimage first."
1747 return 1
1748 fi
1749 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1750 if [ -z "$PARTITION" ];
1751 then
Ricardo Cerqueira9e4c4a72013-07-27 13:51:56 +01001752 # Try for RECOVERY_FSTAB_VERSION = 2
1753 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1754 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1755 if [ -z "$PARTITION" ];
1756 then
1757 echo "Unable to determine boot partition."
1758 return 1
1759 fi
Steve Kondikf00e7d92012-09-23 23:46:55 -07001760 fi
1761 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001762 adb wait-for-online
Steve Kondikf00e7d92012-09-23 23:46:55 -07001763 adb root
1764 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001765 adb wait-for-online shell mount /system 2>&1 > /dev/null
1766 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001767 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondikf00e7d92012-09-23 23:46:55 -07001768 then
1769 adb push $OUT/boot.img /cache/
1770 for i in $OUT/system/lib/modules/*;
1771 do
1772 adb push $i /system/lib/modules/
1773 done
1774 adb shell dd if=/cache/boot.img of=$PARTITION
1775 adb shell chmod 644 /system/lib/modules/*
1776 echo "Installation complete."
1777 else
1778 echo "The connected device does not appear to be $CM_BUILD, run away!"
1779 fi
1780}
1781
Steve Kondik83c03d52012-10-24 16:40:42 -07001782function installrecovery()
1783{
1784 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1785 then
1786 echo "No recovery.fstab found. Build recovery first."
1787 return 1
1788 fi
1789 if [ ! -e "$OUT/recovery.img" ];
1790 then
1791 echo "No recovery.img found. Run make recoveryimage first."
1792 return 1
1793 fi
1794 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1795 if [ -z "$PARTITION" ];
1796 then
Steve Kondik75f10762013-08-09 21:03:42 -07001797 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondikd8b510a2013-08-10 21:02:09 -07001798 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1799 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondik75f10762013-08-09 21:03:42 -07001800 if [ -z "$PARTITION" ];
1801 then
1802 echo "Unable to determine recovery partition."
1803 return 1
1804 fi
Steve Kondik83c03d52012-10-24 16:40:42 -07001805 fi
1806 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001807 adb wait-for-online
Steve Kondik83c03d52012-10-24 16:40:42 -07001808 adb root
1809 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001810 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1811 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001812 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondik83c03d52012-10-24 16:40:42 -07001813 then
1814 adb push $OUT/recovery.img /cache/
1815 adb shell dd if=/cache/recovery.img of=$PARTITION
1816 echo "Installation complete."
1817 else
1818 echo "The connected device does not appear to be $CM_BUILD, run away!"
1819 fi
1820}
Steve Kondikf00e7d92012-09-23 23:46:55 -07001821
Koushik Duttab55f13a2012-05-14 16:14:36 -07001822function makerecipe() {
1823 if [ -z "$1" ]
1824 then
1825 echo "No branch name provided."
1826 return 1
1827 fi
1828 cd android
1829 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1830 git commit -a -m "$1"
1831 cd ..
1832
1833 repo forall -c '
1834
Roman Birg5771f5d2016-03-01 10:27:51 -08001835 if [ "$REPO_REMOTE" = "github" ]
Koushik Duttab55f13a2012-05-14 16:14:36 -07001836 then
1837 pwd
1838 cmremote
1839 git push cmremote HEAD:refs/heads/'$1'
1840 fi
1841 '
1842}
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001843
1844function cmgerrit() {
Khalid Zubair01da9972016-02-08 16:54:01 -08001845
Roman Birg5771f5d2016-03-01 10:27:51 -08001846 if [ "$(__detect_shell)" = "zsh" ]; then
Khalid Zubair01da9972016-02-08 16:54:01 -08001847 # zsh does not define FUNCNAME, derive from funcstack
1848 local FUNCNAME=$funcstack[1]
1849 fi
1850
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001851 if [ $# -eq 0 ]; then
1852 $FUNCNAME help
1853 return 1
1854 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001855 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001856 local review=`git config --get remote.github.review`
1857 local project=`git config --get remote.github.projectname`
1858 local command=$1
1859 shift
1860 case $command in
1861 help)
1862 if [ $# -eq 0 ]; then
1863 cat <<EOF
1864Usage:
1865 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1866
1867Commands:
1868 fetch Just fetch the change as FETCH_HEAD
1869 help Show this help, or for a specific command
1870 pull Pull a change into current branch
1871 push Push HEAD or a local branch to Gerrit for a specific branch
1872
1873Any other Git commands that support refname would work as:
1874 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1875
1876See '$FUNCNAME help COMMAND' for more information on a specific command.
1877
1878Example:
1879 $FUNCNAME checkout -b topic 1234/5
1880works as:
1881 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1882 && git checkout -b topic FETCH_HEAD
1883will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1884Patch-set 1 will be fetched if omitted.
1885EOF
1886 return
1887 fi
1888 case $1 in
1889 __cmg_*) echo "For internal use only." ;;
1890 changes|for)
1891 if [ "$FUNCNAME" = "cmgerrit" ]; then
1892 echo "'$FUNCNAME $1' is deprecated."
1893 fi
1894 ;;
1895 help) $FUNCNAME help ;;
1896 fetch|pull) cat <<EOF
1897usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1898
1899works as:
1900 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1901 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1902
1903Example:
1904 $FUNCNAME $1 1234
1905will $1 patch-set 1 of change 1234
1906EOF
1907 ;;
1908 push) cat <<EOF
1909usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1910
1911works as:
1912 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1913 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1914
1915Example:
1916 $FUNCNAME push fix6789:gingerbread
1917will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1918HEAD will be pushed from local if omitted.
1919EOF
1920 ;;
1921 *)
1922 $FUNCNAME __cmg_err_not_supported $1 && return
1923 cat <<EOF
1924usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1925
1926works as:
1927 git fetch http://DOMAIN/p/PROJECT \\
1928 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1929 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1930EOF
1931 ;;
1932 esac
1933 ;;
1934 __cmg_get_ref)
1935 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1936 local change_id patchset_id hash
1937 case $1 in
1938 */*)
1939 change_id=${1%%/*}
1940 patchset_id=${1#*/}
1941 ;;
1942 *)
1943 change_id=$1
1944 patchset_id=1
1945 ;;
1946 esac
1947 hash=$(($change_id % 100))
1948 case $hash in
1949 [0-9]) hash="0$hash" ;;
1950 esac
1951 echo "refs/changes/$hash/$change_id/$patchset_id"
1952 ;;
1953 fetch|pull)
1954 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1955 $FUNCNAME __cmg_err_not_repo && return 1
1956 local change=$1
1957 shift
1958 git $command $@ http://$review/p/$project \
1959 $($FUNCNAME __cmg_get_ref $change) || return 1
1960 ;;
1961 push)
1962 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1963 $FUNCNAME __cmg_err_not_repo && return 1
1964 if [ -z "$user" ]; then
1965 echo >&2 "Gerrit username not found."
1966 return 1
1967 fi
1968 local local_branch remote_branch
1969 case $1 in
1970 *:*)
1971 local_branch=${1%:*}
1972 remote_branch=${1##*:}
1973 ;;
1974 *)
1975 local_branch=HEAD
1976 remote_branch=$1
1977 ;;
1978 esac
1979 shift
1980 git push $@ ssh://$user@$review:29418/$project \
1981 $local_branch:refs/for/$remote_branch || return 1
1982 ;;
1983 changes|for)
1984 if [ "$FUNCNAME" = "cmgerrit" ]; then
1985 echo >&2 "'$FUNCNAME $command' is deprecated."
1986 fi
1987 ;;
1988 __cmg_err_no_arg)
1989 if [ $# -lt 2 ]; then
1990 echo >&2 "'$FUNCNAME $command' missing argument."
1991 elif [ $2 -eq 0 ]; then
1992 if [ -n "$3" ]; then
1993 $FUNCNAME help $1
1994 else
1995 echo >&2 "'$FUNCNAME $1' missing argument."
1996 fi
1997 else
1998 return 1
1999 fi
2000 ;;
2001 __cmg_err_not_repo)
2002 if [ -z "$review" -o -z "$project" ]; then
2003 echo >&2 "Not currently in any reviewable repository."
2004 else
2005 return 1
2006 fi
2007 ;;
2008 __cmg_err_not_supported)
2009 $FUNCNAME __cmg_err_no_arg $command $# && return
2010 case $1 in
2011 #TODO: filter more git commands that don't use refname
2012 init|add|rm|mv|status|clone|remote|bisect|config|stash)
2013 echo >&2 "'$FUNCNAME $1' is not supported."
2014 ;;
2015 *) return 1 ;;
2016 esac
2017 ;;
2018 #TODO: other special cases?
2019 *)
2020 $FUNCNAME __cmg_err_not_supported $command && return 1
2021 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
2022 $FUNCNAME __cmg_err_not_repo && return 1
2023 local args="$@"
2024 local change pre_args refs_arg post_args
2025 case "$args" in
2026 *--\ *)
2027 pre_args=${args%%-- *}
2028 post_args="-- ${args#*-- }"
2029 ;;
2030 *) pre_args="$args" ;;
2031 esac
2032 args=($pre_args)
2033 pre_args=
2034 if [ ${#args[@]} -gt 0 ]; then
2035 change=${args[${#args[@]}-1]}
2036 fi
2037 if [ ${#args[@]} -gt 1 ]; then
2038 pre_args=${args[0]}
2039 for ((i=1; i<${#args[@]}-1; i++)); do
2040 pre_args="$pre_args ${args[$i]}"
2041 done
2042 fi
2043 while ((1)); do
2044 case $change in
2045 ""|--)
2046 $FUNCNAME help $command
2047 return 1
2048 ;;
2049 *@*)
2050 if [ -z "$refs_arg" ]; then
2051 refs_arg="@${change#*@}"
2052 change=${change%%@*}
2053 fi
2054 ;;
2055 *~*)
2056 if [ -z "$refs_arg" ]; then
2057 refs_arg="~${change#*~}"
2058 change=${change%%~*}
2059 fi
2060 ;;
2061 *^*)
2062 if [ -z "$refs_arg" ]; then
2063 refs_arg="^${change#*^}"
2064 change=${change%%^*}
2065 fi
2066 ;;
2067 *:*)
2068 if [ -z "$refs_arg" ]; then
2069 refs_arg=":${change#*:}"
2070 change=${change%%:*}
2071 fi
2072 ;;
2073 *) break ;;
2074 esac
2075 done
2076 $FUNCNAME fetch $change \
2077 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
2078 || return 1
2079 ;;
2080 esac
2081}
2082
2083function cmrebase() {
2084 local repo=$1
2085 local refs=$2
2086 local pwd="$(pwd)"
2087 local dir="$(gettop)/$repo"
2088
2089 if [ -z $repo ] || [ -z $refs ]; then
2090 echo "CyanogenMod Gerrit Rebase Usage: "
2091 echo " cmrebase <path to project> <patch IDs on Gerrit>"
2092 echo " The patch IDs appear on the Gerrit commands that are offered."
2093 echo " They consist on a series of numbers and slashes, after the text"
2094 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
2095 echo ""
2096 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
2097 echo ""
2098 return
2099 fi
2100
2101 if [ ! -d $dir ]; then
2102 echo "Directory $dir doesn't exist in tree."
2103 return
2104 fi
2105 cd $dir
2106 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
2107 echo "Starting branch..."
2108 repo start tmprebase .
2109 echo "Bringing it up to date..."
2110 repo sync .
2111 echo "Fetching change..."
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07002112 git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002113 if [ "$?" != "0" ]; then
2114 echo "Error cherry-picking. Not uploading!"
2115 return
2116 fi
2117 echo "Uploading..."
2118 repo upload .
2119 echo "Cleaning up..."
2120 repo abandon tmprebase .
2121 cd $pwd
2122}
2123
2124function mka() {
Khalid Zubairabcd1942015-10-06 11:00:55 -07002125 local T=$(gettop)
2126 if [ "$T" ]; then
2127 case `uname -s` in
2128 Darwin)
2129 make -C $T -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
2130 ;;
2131 *)
2132 mk_timer schedtool -B -n 1 -e ionice -n 1 make -C $T -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
2133 ;;
2134 esac
2135
2136 else
2137 echo "Couldn't locate the top of the tree. Try setting TOP."
2138 fi
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002139}
2140
Chirayu Desaicf2bdb62012-09-28 11:56:02 +05302141function cmka() {
2142 if [ ! -z "$1" ]; then
2143 for i in "$@"; do
2144 case $i in
2145 bacon|otapackage|systemimage)
2146 mka installclean
2147 mka $i
2148 ;;
2149 *)
2150 mka clean-$i
2151 mka $i
2152 ;;
2153 esac
2154 done
2155 else
2156 mka clean
2157 mka
2158 fi
2159}
2160
Khalid Zubairfdc26cd2015-10-21 12:43:14 -07002161function mms() {
2162 local T=$(gettop)
2163 if [ -z "$T" ]
2164 then
2165 echo "Couldn't locate the top of the tree. Try setting TOP."
2166 return 1
2167 fi
2168
2169 case `uname -s` in
2170 Darwin)
2171 local NUM_CPUS=$(sysctl hw.ncpu|cut -d" " -f2)
2172 ONE_SHOT_MAKEFILE="__none__" \
2173 make -C $T -j $NUM_CPUS "$@"
2174 ;;
2175 *)
2176 local NUM_CPUS=$(cat /proc/cpuinfo | grep "^processor" | wc -l)
2177 ONE_SHOT_MAKEFILE="__none__" \
2178 mk_timer schedtool -B -n 1 -e ionice -n 1 \
2179 make -C $T -j $NUM_CPUS "$@"
2180 ;;
2181 esac
2182}
2183
2184
Matt Mower8dacaed2013-12-29 12:57:20 -06002185function repolastsync() {
2186 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
2187 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
2188 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
2189 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
2190}
2191
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002192function reposync() {
2193 case `uname -s` in
2194 Darwin)
2195 repo sync -j 4 "$@"
2196 ;;
2197 *)
Alan Orthef363cd2012-09-07 11:44:27 +03002198 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002199 ;;
2200 esac
2201}
Tanguy Pruvot2192fd22012-06-06 21:39:23 +02002202
2203function repodiff() {
2204 if [ -z "$*" ]; then
2205 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
2206 return
2207 fi
2208 diffopts=$* repo forall -c \
2209 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
2210}
2211
Khalid Zubair63eabff2016-02-02 12:00:14 -08002212# Return success if adb is up and not in recovery
2213function _adb_connected {
2214 {
2215 if [[ "$(adb get-state)" == device &&
2216 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
2217 then
2218 return 0
2219 fi
2220 } 2>/dev/null
2221
2222 return 1
2223};
2224
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302225# Credit for color strip sed: http://goo.gl/BoIcm
2226function dopush()
2227{
2228 local func=$1
2229 shift
2230
2231 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Khalid Zubair63eabff2016-02-02 12:00:14 -08002232 if ! _adb_connected; then
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302233 echo "No device is online. Waiting for one..."
2234 echo "Please connect USB and/or enable USB debugging"
Khalid Zubair63eabff2016-02-02 12:00:14 -08002235 until _adb_connected; do
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302236 sleep 1
2237 done
2238 echo "Device Found."
2239 fi
2240
Roman Birg5771f5d2016-03-01 10:27:51 -08002241 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD") || [ "$FORCE_PUSH" = "true" ];
Chirayu Desai6dadb572012-12-26 10:53:58 +05302242 then
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002243 # retrieve IP and PORT info if we're using a TCP connection
2244 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
2245 | head -1 | awk '{print $1}')
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302246 adb root &> /dev/null
2247 sleep 0.3
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002248 if [ -n "$TCPIPPORT" ]
2249 then
2250 # adb root just killed our connection
2251 # so reconnect...
2252 adb connect "$TCPIPPORT"
2253 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302254 adb wait-for-device &> /dev/null
2255 sleep 0.3
2256 adb remount &> /dev/null
2257
Matt Mower668ea262014-05-20 02:52:23 -05002258 mkdir -p $OUT
Marcos Marado37e92c22015-01-13 15:14:28 +00002259 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
2260 ret=$?;
2261 if [ $ret -ne 0 ]; then
2262 rm -f $OUT/.log;return $ret
2263 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302264
2265 # Install: <file>
LuK133756041e72015-12-01 14:53:23 +01002266 if [ `uname` = "Linux" ]; then
2267 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)"
2268 else
2269 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)"
2270 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302271
2272 # Copy: <file>
LuK133756041e72015-12-01 14:53:23 +01002273 if [ `uname` = "Linux" ]; then
2274 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)"
2275 else
2276 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)"
2277 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302278
Matt Mowerdfd4c082014-05-20 02:52:23 -05002279 # If any files are going to /data, push an octal file permissions reader to device
2280 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
2281 CHKPERM="/data/local/tmp/chkfileperm.sh"
2282(
2283cat <<'EOF'
2284#!/system/xbin/sh
2285FILE=$@
2286if [ -e $FILE ]; then
2287 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
2288fi
2289EOF
2290) > $OUT/.chkfileperm.sh
2291 echo "Pushing file permissions checker to device"
2292 adb push $OUT/.chkfileperm.sh $CHKPERM
2293 adb shell chmod 755 $CHKPERM
2294 rm -f $OUT/.chkfileperm.sh
2295 fi
2296
Matt Mower668ea262014-05-20 02:52:23 -05002297 stop_n_start=false
Luca Stefani406c2af2015-11-02 05:33:10 -08002298 for FILE in $(echo $LOC | tr " " "\n"); do
Matt Mowerdfd4c082014-05-20 02:52:23 -05002299 # Make sure file is in $OUT/system or $OUT/data
Matt Mower668ea262014-05-20 02:52:23 -05002300 case $FILE in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002301 $OUT/system/*|$OUT/data/*)
Matt Mower668ea262014-05-20 02:52:23 -05002302 # Get target file name (i.e. /system/bin/adb)
2303 TARGET=$(echo $FILE | sed "s#$OUT##")
2304 ;;
2305 *) continue ;;
2306 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302307
Matt Mower668ea262014-05-20 02:52:23 -05002308 case $TARGET in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002309 /data/*)
2310 # fs_config only sets permissions and se labels for files pushed to /system
2311 if [ -n "$CHKPERM" ]; then
2312 OLDPERM=$(adb shell $CHKPERM $TARGET)
2313 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
2314 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
2315 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
2316 fi
2317 echo "Pushing: $TARGET"
2318 adb push $FILE $TARGET
2319 if [ -n "$OLDPERM" ]; then
2320 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
2321 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
2322 adb shell chmod "$OLDPERM" $TARGET
2323 else
2324 echo "$TARGET did not exist previously, you should set file permissions manually"
2325 fi
2326 adb shell restorecon "$TARGET"
2327 ;;
Pawit Pornkitprasan1822ad02014-12-22 20:11:47 +07002328 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
Matt Mower668ea262014-05-20 02:52:23 -05002329 # Only need to stop services once
2330 if ! $stop_n_start; then
2331 adb shell stop
2332 stop_n_start=true
2333 fi
2334 echo "Pushing: $TARGET"
2335 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302336 ;;
2337 *)
Matt Mower668ea262014-05-20 02:52:23 -05002338 echo "Pushing: $TARGET"
2339 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302340 ;;
Matt Mower668ea262014-05-20 02:52:23 -05002341 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302342 done
Matt Mowerdfd4c082014-05-20 02:52:23 -05002343 if [ -n "$CHKPERM" ]; then
2344 adb shell rm $CHKPERM
2345 fi
Matt Mower668ea262014-05-20 02:52:23 -05002346 if $stop_n_start; then
2347 adb shell start
2348 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302349 rm -f $OUT/.log
2350 return 0
Chirayu Desai6dadb572012-12-26 10:53:58 +05302351 else
2352 echo "The connected device does not appear to be $CM_BUILD, run away!"
2353 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302354}
2355
2356alias mmp='dopush mm'
2357alias mmmp='dopush mmm'
Martin Brabhamb77db582016-02-05 15:59:21 -05002358alias mmap='dopush mma'
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302359alias mkap='dopush mka'
2360alias cmkap='dopush cmka'
2361
Chirayu Desai4a319b82013-06-05 20:14:33 +05302362function repopick() {
2363 T=$(gettop)
2364 $T/build/tools/repopick.py $@
2365}
2366
Chirayu Desaib89b3242013-06-30 10:04:25 +05302367function fixup_common_out_dir() {
2368 common_out_dir=$(get_build_var OUT_DIR)/target/common
2369 target_device=$(get_build_var TARGET_DEVICE)
2370 if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
2371 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
2372 mv ${common_out_dir} ${common_out_dir}-${target_device}
2373 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2374 else
2375 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2376 mkdir -p ${common_out_dir}-${target_device}
2377 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2378 fi
2379 else
2380 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2381 mkdir -p ${common_out_dir}
2382 fi
2383}
2384
Michael Bestas163381a2015-10-23 20:36:47 +03002385# Force JAVA_HOME to point to java 1.7 if it isn't already set.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002386#
2387# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
2388# For some reason, installing the JDK doesn't make it show up in the
2389# JavaVM.framework/Versions/1.7/ folder.
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002390function set_java_home() {
Narayan Kamath9260bba2014-01-17 10:05:25 +00002391 # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamathc84889b2014-04-01 14:16:26 +01002392 # we can reset it later, depending on the version of java the build
2393 # system needs.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002394 #
2395 # If we don't do this, the JAVA_HOME value set by the first call to
2396 # build/envsetup.sh will persist forever.
2397 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
2398 export JAVA_HOME=""
2399 fi
2400
Andy McFaddenbd960942010-06-24 12:52:51 -07002401 if [ ! "$JAVA_HOME" ]; then
Neil Fuller46e00ea2014-10-16 10:23:03 +01002402 case `uname -s` in
2403 Darwin)
2404 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2405 ;;
2406 *)
2407 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
2408 ;;
2409 esac
Narayan Kamath9260bba2014-01-17 10:05:25 +00002410
2411 # Keep track of the fact that we set JAVA_HOME ourselves, so that
2412 # we can change it on the next envsetup.sh, if required.
2413 export ANDROID_SET_JAVA_HOME=true
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002414 fi
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002415}
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002416
Alex Rayf0d08eb2013-03-08 15:15:06 -08002417# Print colored exit condition
2418function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08002419 "$@"
2420 local retval=$?
2421 if [ $retval -ne 0 ]
2422 then
Roman Birgead9a5c2015-10-16 10:29:06 -07002423 printf "\e[0;31mFAILURE\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002424 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002425 printf "\e[0;32mSUCCESS\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002426 fi
2427 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08002428}
2429
Ying Wanged21d4c2014-08-24 22:14:19 -07002430function get_make_command()
2431{
2432 echo command make
2433}
2434
Anthony King40b093f2015-04-30 22:19:48 +01002435function mk_timer()
Ed Heylcc6be0a2014-06-18 14:55:58 -07002436{
2437 local start_time=$(date +"%s")
Anthony King40b093f2015-04-30 22:19:48 +01002438 $@
Ed Heylcc6be0a2014-06-18 14:55:58 -07002439 local ret=$?
2440 local end_time=$(date +"%s")
2441 local tdiff=$(($end_time-$start_time))
2442 local hours=$(($tdiff / 3600 ))
2443 local mins=$((($tdiff % 3600) / 60))
2444 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002445 local ncolors=$(tput colors 2>/dev/null)
2446 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
2447 color_failed="\e[0;31m"
2448 color_success="\e[0;32m"
Roman Birgead9a5c2015-10-16 10:29:06 -07002449 color_reset="\e[0m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002450 else
2451 color_failed=""
2452 color_success=""
2453 color_reset=""
2454 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07002455 echo
2456 if [ $ret -eq 0 ] ; then
Roman Birgead9a5c2015-10-16 10:29:06 -07002457 printf "${color_success}#### make completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002458 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002459 printf "${color_failed}#### make failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002460 fi
2461 if [ $hours -gt 0 ] ; then
2462 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
2463 elif [ $mins -gt 0 ] ; then
2464 printf "(%02g:%02g (mm:ss))" $mins $secs
2465 elif [ $secs -gt 0 ] ; then
2466 printf "(%s seconds)" $secs
2467 fi
Roman Birgead9a5c2015-10-16 10:29:06 -07002468 printf " ####${color_reset}\n\n"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002469 return $ret
2470}
2471
Anthony King40b093f2015-04-30 22:19:48 +01002472function make()
2473{
2474 mk_timer $(get_make_command) "$@"
2475}
2476
Khalid Zubair695992c2016-02-08 16:27:55 -08002477function __detect_shell() {
Raphael Moll70a86b02011-06-20 16:03:14 -07002478 case `ps -o command -p $$` in
2479 *bash*)
Khalid Zubair695992c2016-02-08 16:27:55 -08002480 echo bash
Raphael Moll70a86b02011-06-20 16:03:14 -07002481 ;;
Emilio López7ff9caa2013-11-03 13:05:43 -03002482 *zsh*)
Khalid Zubair695992c2016-02-08 16:27:55 -08002483 echo zsh
Emilio López7ff9caa2013-11-03 13:05:43 -03002484 ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002485 *)
Khalid Zubair695992c2016-02-08 16:27:55 -08002486 echo unknown
2487 return 1
Raphael Moll70a86b02011-06-20 16:03:14 -07002488 ;;
2489 esac
Khalid Zubair695992c2016-02-08 16:27:55 -08002490 return
2491}
2492
2493
2494if ! __detect_shell > /dev/null; then
2495 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
Raphael Moll70a86b02011-06-20 16:03:14 -07002496fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002497
2498# Execute the contents of any vendorsetup.sh files we can find.
Oleksiy Avramchenko15760a82014-10-06 18:51:58 +02002499for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
2500 `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 -08002501do
2502 echo "including $f"
2503 . $f
2504done
2505unset f
Kenny Root52aa81c2011-07-15 11:07:06 -07002506
Kyle Ladd031a0b62013-09-11 20:43:42 -04002507# Add completions
2508check_bash_version && {
2509 dirs="sdk/bash_completion vendor/cm/bash_completion"
2510 for dir in $dirs; do
2511 if [ -d ${dir} ]; then
2512 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
2513 echo "including $f"
2514 . $f
2515 done
2516 fi
2517 done
2518}
Chirayu Desaie1466d62013-03-19 17:50:37 +05302519
2520export ANDROID_BUILD_TOP=$(gettop)