blob: 3384211de0aca170abb16e1c4f0b0bc93a3e8a4c [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=
868 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700869 # Remove the path to top as the makefilepath needs to be relative
870 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700871 if [ ! "$T" ]; then
872 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700873 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700874 elif [ ! "$M" ]; then
875 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700876 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700877 else
Ying Wanga7deb082013-08-16 13:24:47 -0700878 for ARG in $@; do
879 case $ARG in
880 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
881 esac
882 done
883 if [ -n "$GET_INSTALL_PATH" ]; then
884 MODULES=
885 ARGS=GET-INSTALL-PATH
886 else
887 MODULES=all_modules
888 ARGS=$@
889 fi
Andrew Hsieh246daf72013-09-10 18:07:23 -0700890 ONE_SHOT_MAKEFILE=$M $DRV make -C $T -f build/core/main.mk $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700891 fi
892 fi
893}
894
895function mmm()
896{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800897 local T=$(gettop)
898 local DRV=$(getdriver $T)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700899 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800900 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800901 local MODULES=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800902 local ARGS=
903 local DIR TO_CHOP
Ying Wanga7deb082013-08-16 13:24:47 -0700904 local GET_INSTALL_PATH=
Khalid Zubair521aa1e2016-02-11 14:34:12 -0800905
906 if [ "$(__detect_shell)" == "zsh" ]; then
907 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
908 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
909 else
910 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
911 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
912 fi
913
The Android Open Source Project88b60792009-03-03 19:28:42 -0800914 for DIR in $DIRS ; do
Alon Albert68895a92011-11-30 12:40:19 -0800915 MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
916 if [ "$MODULES" = "" ]; then
917 MODULES=all_modules
918 fi
919 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700920 if [ -f $DIR/Android.mk ]; then
Ying Wanga7deb082013-08-16 13:24:47 -0700921 local TO_CHOP=`(\cd -P -- $T && pwd -P) | wc -c | tr -d ' '`
922 local TO_CHOP=`expr $TO_CHOP + 1`
923 local START=`PWD= /bin/pwd`
924 local MFILE=`echo $START | cut -c${TO_CHOP}-`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700925 if [ "$MFILE" = "" ] ; then
926 MFILE=$DIR/Android.mk
927 else
928 MFILE=$MFILE/$DIR/Android.mk
929 fi
930 MAKEFILE="$MAKEFILE $MFILE"
931 else
Ying Wanga7deb082013-08-16 13:24:47 -0700932 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +0100933 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700934 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997c3eb1072015-10-18 19:49:13 +0200935 *) if [ -d $DIR ]; then
936 echo "No Android.mk in $DIR.";
937 else
938 echo "Couldn't locate the directory $DIR";
939 fi
940 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700941 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700942 fi
943 done
Ying Wanga7deb082013-08-16 13:24:47 -0700944 if [ -n "$GET_INSTALL_PATH" ]; then
945 ARGS=$GET_INSTALL_PATH
946 MODULES=
947 fi
Andrew Hsieh906cb782013-09-10 17:37:14 +0800948 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 -0700949 else
950 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700951 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700952 fi
953}
954
Ying Wangb607f7b2013-02-08 18:01:04 -0800955function mma()
956{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800957 local T=$(gettop)
958 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800959 if [ -f build/core/envsetup.mk -a -f Makefile ]; then
Andrew Hsieh906cb782013-09-10 17:37:14 +0800960 $DRV make $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800961 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800962 if [ ! "$T" ]; then
963 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700964 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800965 fi
966 local MY_PWD=`PWD= /bin/pwd|sed 's:'$T'/::'`
Andrew Hsieh906cb782013-09-10 17:37:14 +0800967 $DRV make -C $T -f build/core/main.mk $@ all_modules BUILD_MODULES_IN_PATHS="$MY_PWD"
Ying Wangb607f7b2013-02-08 18:01:04 -0800968 fi
969}
970
971function mmma()
972{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800973 local T=$(gettop)
974 local DRV=$(getdriver $T)
Ying Wangb607f7b2013-02-08 18:01:04 -0800975 if [ "$T" ]; then
Khalid Zubair521aa1e2016-02-11 14:34:12 -0800976 if [ "$(__detect_shell)" == "zsh" ]; then
977 set -lA DASH_ARGS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
978 set -lA DIRS $(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
979 else
980 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
981 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
982 fi
Ying Wangb607f7b2013-02-08 18:01:04 -0800983 local MY_PWD=`PWD= /bin/pwd`
984 if [ "$MY_PWD" = "$T" ]; then
985 MY_PWD=
986 else
987 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
988 fi
989 local DIR=
990 local MODULE_PATHS=
991 local ARGS=
992 for DIR in $DIRS ; do
993 if [ -d $DIR ]; then
994 if [ "$MY_PWD" = "" ]; then
995 MODULE_PATHS="$MODULE_PATHS $DIR"
996 else
997 MODULE_PATHS="$MODULE_PATHS $MY_PWD/$DIR"
998 fi
999 else
1000 case $DIR in
Adrian Roosafa958f2015-03-05 19:52:55 +01001001 showcommands | snod | dist | incrementaljavac | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -08001002 *) echo "Couldn't find directory $DIR"; return 1;;
1003 esac
1004 fi
1005 done
Andrew Hsieh906cb782013-09-10 17:37:14 +08001006 $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 -08001007 else
1008 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -07001009 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -08001010 fi
1011}
1012
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001013function croot()
1014{
1015 T=$(gettop)
1016 if [ "$T" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001017 \cd $(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001018 else
1019 echo "Couldn't locate the top of the tree. Try setting TOP."
1020 fi
1021}
1022
nebkatfb67a1e2012-12-28 10:40:45 +00001023function cout()
1024{
1025 if [ "$OUT" ]; then
1026 cd $OUT
1027 else
1028 echo "Couldn't locate out directory. Try setting OUT."
1029 fi
1030}
1031
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001032function cproj()
1033{
1034 TOPFILE=build/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001035 local HERE=$PWD
1036 T=
1037 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1038 T=$PWD
1039 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001040 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001041 return
1042 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001043 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001044 done
Ying Wang9cd17642012-12-13 10:52:07 -08001045 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001046 echo "can't find Android.mk"
1047}
1048
Daniel Sandler47e0a882013-07-30 13:23:52 -04001049# simplified version of ps; output in the form
1050# <pid> <procname>
1051function qpid() {
1052 local prepend=''
1053 local append=''
1054 if [ "$1" = "--exact" ]; then
1055 prepend=' '
1056 append='$'
1057 shift
1058 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
1059 echo "usage: qpid [[--exact] <process name|pid>"
1060 return 255
1061 fi
1062
1063 local EXE="$1"
1064 if [ "$EXE" ] ; then
Mathias Agopian44583272013-08-27 14:15:50 -07001065 qpid | \grep "$prepend$EXE$append"
Daniel Sandler47e0a882013-07-30 13:23:52 -04001066 else
1067 adb shell ps \
1068 | tr -d '\r' \
1069 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1070 fi
1071}
1072
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001073function pid()
1074{
Daniel Sandler47e0a882013-07-30 13:23:52 -04001075 local prepend=''
1076 local append=''
1077 if [ "$1" = "--exact" ]; then
1078 prepend=' '
1079 append='$'
1080 shift
1081 fi
1082 local EXE="$1"
1083 if [ "$EXE" ] ; then
1084 local PID=`adb shell ps \
1085 | tr -d '\r' \
Mathias Agopian44583272013-08-27 14:15:50 -07001086 | \grep "$prepend$EXE$append" \
Daniel Sandler47e0a882013-07-30 13:23:52 -04001087 | sed -e 's/^[^ ]* *\([0-9]*\).*$/\1/'`
1088 echo "$PID"
1089 else
1090 echo "usage: pid [--exact] <process name>"
1091 return 255
1092 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001093}
1094
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001095# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001096# that has the core-file-size limit set correctly
1097#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001098# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001099# if its core-file-size limit is not set already.
1100# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1101
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001102function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001103{
1104 echo "Getting root...";
1105 adb root;
1106 adb wait-for-device;
1107
1108 echo "Remounting root parition read-write...";
1109 adb shell mount -w -o remount -t rootfs rootfs;
1110 sleep 1;
1111 adb wait-for-device;
1112 adb shell mkdir -p /cores;
Nick Kralevicha94282c2014-11-04 11:17:20 -08001113 adb shell mount -t tmpfs tmpfs /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114 adb shell chmod 0777 /cores;
1115
1116 echo "Granting SELinux permission to dump in /cores...";
1117 adb shell restorecon -R /cores;
1118
1119 echo "Set core pattern.";
1120 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
1121
1122 echo "Done."
1123}
1124
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001125# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126# $1 = PID of process (e.g., $(pid mediaserver))
1127#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001128# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001129# dump to actually be generated.
1130
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001131function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001132{
1133 local PID=$1;
1134 if [ -z "$PID" ]; then
1135 printf "Expecting a PID!\n";
1136 return;
1137 fi;
1138 echo "Setting core limit for $PID to infinite...";
1139 adb shell prlimit $PID 4 -1 -1
1140}
1141
1142# core - send SIGV and pull the core for process
1143# $1 = PID of process (e.g., $(pid mediaserver))
1144#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001145# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001146# enabled globally.
1147
1148function core()
1149{
1150 local PID=$1;
1151
1152 if [ -z "$PID" ]; then
1153 printf "Expecting a PID!\n";
1154 return;
1155 fi;
1156
1157 local CORENAME=core.$PID;
1158 local COREPATH=/cores/$CORENAME;
1159 local SIG=SEGV;
1160
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001161 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001162
1163 local done=0;
1164 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1165 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1166 adb shell kill -$SIG $PID;
1167 sleep 1;
1168 done;
1169
1170 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1171 echo "Done: core is under $COREPATH on device.";
1172}
1173
Christopher Tate744ee802009-11-12 15:33:08 -08001174# systemstack - dump the current stack trace of all threads in the system process
1175# to the usual ANR traces file
1176function systemstack()
1177{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001178 stacks system_server
1179}
1180
1181function stacks()
1182{
1183 if [[ $1 =~ ^[0-9]+$ ]] ; then
1184 local PID="$1"
1185 elif [ "$1" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001186 local PIDLIST="$(pid $1)"
1187 if [[ $PIDLIST =~ ^[0-9]+$ ]] ; then
1188 local PID="$PIDLIST"
1189 elif [ "$PIDLIST" ] ; then
1190 echo "more than one process: $1"
1191 else
1192 echo "no such process: $1"
1193 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001194 else
1195 echo "usage: stacks [pid|process name]"
1196 fi
1197
1198 if [ "$PID" ] ; then
Jeff Brownb12c2e52013-08-19 15:14:16 -07001199 # Determine whether the process is native
1200 if adb shell ls -l /proc/$PID/exe | grep -q /system/bin/app_process ; then
1201 # Dump stacks of Dalvik process
1202 local TRACES=/data/anr/traces.txt
1203 local ORIG=/data/anr/traces.orig
1204 local TMP=/data/anr/traces.tmp
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001205
Jeff Brownb12c2e52013-08-19 15:14:16 -07001206 # Keep original traces to avoid clobbering
1207 adb shell mv $TRACES $ORIG
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001208
Jeff Brownb12c2e52013-08-19 15:14:16 -07001209 # Make sure we have a usable file
1210 adb shell touch $TRACES
1211 adb shell chmod 666 $TRACES
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001212
Jeff Brownb12c2e52013-08-19 15:14:16 -07001213 # Dump stacks and wait for dump to finish
1214 adb shell kill -3 $PID
1215 adb shell notify $TRACES >/dev/null
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001216
Jeff Brownb12c2e52013-08-19 15:14:16 -07001217 # Restore original stacks, and show current output
1218 adb shell mv $TRACES $TMP
1219 adb shell mv $ORIG $TRACES
1220 adb shell cat $TMP
1221 else
1222 # Dump stacks of native process
Michael Wrightaeed7212014-06-19 19:58:12 -07001223 local USE64BIT="$(is64bit $PID)"
1224 adb shell debuggerd$USE64BIT -b $PID
Jeff Brownb12c2e52013-08-19 15:14:16 -07001225 fi
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001226 fi
Christopher Tate744ee802009-11-12 15:33:08 -08001227}
1228
Michael Wrightaeed7212014-06-19 19:58:12 -07001229# Read the ELF header from /proc/$PID/exe to determine if the process is
1230# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001231function is64bit()
1232{
1233 local PID="$1"
1234 if [ "$PID" ] ; then
Michael Wrightaeed7212014-06-19 19:58:12 -07001235 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -ps)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001236 echo "64"
1237 else
1238 echo ""
1239 fi
1240 else
1241 echo ""
1242 fi
1243}
1244
Clark Scheff75b36a42014-12-23 13:30:51 -08001245function dddclient()
1246{
1247 local OUT_ROOT=$(get_abs_build_var PRODUCT_OUT)
1248 local OUT_SYMBOLS=$(get_abs_build_var TARGET_OUT_UNSTRIPPED)
1249 local OUT_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_SHARED_LIBRARIES_UNSTRIPPED)
1250 local OUT_VENDOR_SO_SYMBOLS=$(get_abs_build_var TARGET_OUT_VENDOR_SHARED_LIBRARIES_UNSTRIPPED)
1251 local OUT_EXE_SYMBOLS=$(get_symbols_directory)
1252 local PREBUILTS=$(get_abs_build_var ANDROID_PREBUILTS)
1253 local ARCH=$(get_build_var TARGET_ARCH)
1254 local GDB
1255 case "$ARCH" in
1256 arm) GDB=arm-linux-androideabi-gdb;;
1257 arm64) GDB=arm-linux-androideabi-gdb; GDB64=aarch64-linux-android-gdb;;
1258 mips|mips64) GDB=mips64el-linux-android-gdb;;
1259 x86) GDB=x86_64-linux-android-gdb;;
1260 x86_64) GDB=x86_64-linux-android-gdb;;
1261 *) echo "Unknown arch $ARCH"; return 1;;
1262 esac
1263
1264 if [ "$OUT_ROOT" -a "$PREBUILTS" ]; then
1265 local EXE="$1"
1266 if [ "$EXE" ] ; then
1267 EXE=$1
1268 if [[ $EXE =~ ^[^/].* ]] ; then
1269 EXE="system/bin/"$EXE
1270 fi
1271 else
1272 EXE="app_process"
1273 fi
1274
1275 local PORT="$2"
1276 if [ "$PORT" ] ; then
1277 PORT=$2
1278 else
1279 PORT=":5039"
1280 fi
1281
1282 local PID="$3"
1283 if [ "$PID" ] ; then
1284 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1285 PID=`pid $3`
1286 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
1287 # that likely didn't work because of returning multiple processes
1288 # try again, filtering by root processes (don't contain colon)
1289 PID=`adb shell ps | \grep $3 | \grep -v ":" | awk '{print $2}'`
1290 if [[ ! "$PID" =~ ^[0-9]+$ ]]
1291 then
1292 echo "Couldn't resolve '$3' to single PID"
1293 return 1
1294 else
1295 echo ""
1296 echo "WARNING: multiple processes matching '$3' observed, using root process"
1297 echo ""
1298 fi
1299 fi
1300 fi
1301 adb forward "tcp$PORT" "tcp$PORT"
1302 local USE64BIT="$(is64bit $PID)"
1303 adb shell gdbserver$USE64BIT $PORT --attach $PID &
1304 sleep 2
1305 else
1306 echo ""
1307 echo "If you haven't done so already, do this first on the device:"
1308 echo " gdbserver $PORT /system/bin/$EXE"
1309 echo " or"
1310 echo " gdbserver $PORT --attach <PID>"
1311 echo ""
1312 fi
1313
1314 OUT_SO_SYMBOLS=$OUT_SO_SYMBOLS$USE64BIT
1315 OUT_VENDOR_SO_SYMBOLS=$OUT_VENDOR_SO_SYMBOLS$USE64BIT
1316
1317 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $OUT_SYMBOLS"
1318 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"
1319 echo >>"$OUT_ROOT/gdbclient.cmds" "source $ANDROID_BUILD_TOP/development/scripts/gdb/dalvik.gdb"
1320 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote $PORT"
1321 # Enable special debugging for ART processes.
1322 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
1323 echo >> "$OUT_ROOT/gdbclient.cmds" "art-on"
1324 fi
1325 echo >>"$OUT_ROOT/gdbclient.cmds" ""
1326
1327 local WHICH_GDB=
1328 # 64-bit exe found
1329 if [ "$USE64BIT" != "" ] ; then
1330 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB64
1331 # 32-bit exe / 32-bit platform
1332 elif [ "$(get_build_var TARGET_2ND_ARCH)" = "" ]; then
1333 WHICH_GDB=$ANDROID_TOOLCHAIN/$GDB
1334 # 32-bit exe / 64-bit platform
1335 else
1336 WHICH_GDB=$ANDROID_TOOLCHAIN_2ND_ARCH/$GDB
1337 fi
1338
1339 ddd --debugger $WHICH_GDB -x "$OUT_ROOT/gdbclient.cmds" "$OUT_EXE_SYMBOLS/$EXE"
1340 else
1341 echo "Unable to determine build system output dir."
1342 fi
1343}
1344
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001345case `uname -s` in
1346 Darwin)
1347 function sgrep()
1348 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001349 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 -07001350 }
1351
1352 ;;
1353 *)
1354 function sgrep()
1355 {
Jeff Brown46cbd202014-07-26 15:15:41 -07001356 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 -07001357 }
1358 ;;
1359esac
1360
Raghu Gandham8da43102012-07-25 19:57:22 -07001361function gettargetarch
1362{
1363 get_build_var TARGET_ARCH
1364}
1365
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001366function ggrep()
1367{
1368 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" -print0 | xargs -0 grep --color -n "$@"
1369}
1370
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001371function jgrep()
1372{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001373 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 -07001374}
1375
1376function cgrep()
1377{
Dan Albert01961192014-11-22 10:16:01 -08001378 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 -07001379}
1380
1381function resgrep()
1382{
Anatolii Shuba91c72d22013-07-05 16:09:25 +03001383 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 -07001384}
1385
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001386function mangrep()
1387{
1388 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' -print0 | xargs -0 grep --color -n "$@"
1389}
1390
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001391function sepgrep()
1392{
1393 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 "$@"
1394}
1395
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001396function rcgrep()
1397{
1398 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" -print0 | xargs -0 grep --color -n "$@"
1399}
1400
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001401case `uname -s` in
1402 Darwin)
1403 function mgrep()
1404 {
Ying Wang324c2b22012-08-17 15:03:20 -07001405 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 -07001406 }
1407
1408 function treegrep()
1409 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001410 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 -07001411 }
1412
1413 ;;
1414 *)
1415 function mgrep()
1416 {
Ying Wang324c2b22012-08-17 15:03:20 -07001417 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 -07001418 }
1419
1420 function treegrep()
1421 {
Joe Onoratof50e84b2011-03-15 14:15:46 -07001422 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 -07001423 }
1424
1425 ;;
1426esac
1427
1428function getprebuilt
1429{
1430 get_abs_build_var ANDROID_PREBUILTS
1431}
1432
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001433function tracedmdump()
1434{
1435 T=$(gettop)
1436 if [ ! "$T" ]; then
1437 echo "Couldn't locate the top of the tree. Try setting TOP."
1438 return
1439 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001440 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001441 local arch=$(gettargetarch)
1442 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001443
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001444 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001445 if [ ! "$TRACE" ] ; then
1446 echo "usage: tracedmdump tracename"
1447 return
1448 fi
1449
Jack Veenstra60116fc2009-04-09 18:12:34 -07001450 if [ ! -r "$KERNEL" ] ; then
1451 echo "Error: cannot find kernel: '$KERNEL'"
1452 return
1453 fi
1454
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001455 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001456 if [ "$BASETRACE" = "$TRACE" ] ; then
1457 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1458 fi
1459
1460 echo "post-processing traces..."
1461 rm -f $TRACE/qtrace.dexlist
1462 post_trace $TRACE
1463 if [ $? -ne 0 ]; then
1464 echo "***"
1465 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1466 echo "***"
1467 return
1468 fi
1469 echo "generating dexlist output..."
1470 /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
1471 echo "generating dmtrace data..."
1472 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1473 echo "generating html file..."
1474 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1475 echo "done, see $TRACE/dmtrace.html for details"
1476 echo "or run:"
1477 echo " traceview $TRACE/dmtrace"
1478}
1479
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001480# communicate with a running device or emulator, set up necessary state,
1481# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001482function runhat()
1483{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001484 # process standard adb options
1485 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001486 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001487 adbTarget=$1
1488 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001489 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001490 adbTarget="$1 $2"
1491 shift 2
1492 fi
1493 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001494 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001495
1496 # runhat options
1497 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001498
1499 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001500 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001501 return
1502 fi
1503
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001504 # confirm hat is available
1505 if [ -z $(which hat) ]; then
1506 echo "hat is not available in this configuration."
1507 return
1508 fi
1509
Andy McFaddenb6289852010-07-12 08:00:19 -07001510 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001511 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001512 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001513 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001514 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001515 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001516 echo -n "> "
1517 read
1518
The Android Open Source Project88b60792009-03-03 19:28:42 -08001519 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001520
The Android Open Source Project88b60792009-03-03 19:28:42 -08001521 echo "Retrieving file $devFile..."
1522 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001523
The Android Open Source Project88b60792009-03-03 19:28:42 -08001524 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001525
The Android Open Source Project88b60792009-03-03 19:28:42 -08001526 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001527 echo "View the output by pointing your browser at http://localhost:7000/"
1528 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001529 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001530}
1531
1532function getbugreports()
1533{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001534 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001535
1536 if [ ! "$reports" ]; then
1537 echo "Could not locate any bugreports."
1538 return
1539 fi
1540
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001541 local report
1542 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001543 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001544 echo "/sdcard/bugreports/${report}"
1545 adb pull /sdcard/bugreports/${report} ${report}
1546 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001547 done
1548}
1549
Victoria Lease1b296b42012-08-21 15:44:06 -07001550function getsdcardpath()
1551{
1552 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1553}
1554
1555function getscreenshotpath()
1556{
1557 echo "$(getsdcardpath)/Pictures/Screenshots"
1558}
1559
1560function getlastscreenshot()
1561{
1562 local screenshot_path=$(getscreenshotpath)
1563 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1564 if [ "$screenshot" = "" ]; then
1565 echo "No screenshots found."
1566 return
1567 fi
1568 echo "${screenshot}"
1569 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1570}
1571
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001572function startviewserver()
1573{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001574 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001575 if [ $# -gt 0 ]; then
1576 port=$1
1577 fi
1578 adb shell service call window 1 i32 $port
1579}
1580
1581function stopviewserver()
1582{
1583 adb shell service call window 2
1584}
1585
1586function isviewserverstarted()
1587{
1588 adb shell service call window 3
1589}
1590
Romain Guyb84049a2010-10-04 16:56:11 -07001591function key_home()
1592{
1593 adb shell input keyevent 3
1594}
1595
1596function key_back()
1597{
1598 adb shell input keyevent 4
1599}
1600
1601function key_menu()
1602{
1603 adb shell input keyevent 82
1604}
1605
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001606function smoketest()
1607{
1608 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1609 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1610 return
1611 fi
1612 T=$(gettop)
1613 if [ ! "$T" ]; then
1614 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1615 return
1616 fi
1617
Ying Wang9cd17642012-12-13 10:52:07 -08001618 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001619 adb uninstall com.android.smoketest > /dev/null &&
1620 adb uninstall com.android.smoketest.tests > /dev/null &&
1621 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1622 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1623 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1624}
1625
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001626# simple shortcut to the runtest command
1627function runtest()
1628{
1629 T=$(gettop)
1630 if [ ! "$T" ]; then
1631 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1632 return
1633 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001634 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001635}
1636
The Android Open Source Project88b60792009-03-03 19:28:42 -08001637function godir () {
1638 if [[ -z "$1" ]]; then
1639 echo "Usage: godir <regex>"
1640 return
1641 fi
Brian Carlstromb9915a62010-01-29 16:39:32 -08001642 T=$(gettop)
Brian Carlstromf2257422015-09-30 20:28:54 -07001643 if [ ! "$OUT_DIR" = "" ]; then
1644 mkdir -p $OUT_DIR
1645 FILELIST=$OUT_DIR/filelist
1646 else
1647 FILELIST=$T/filelist
1648 fi
1649 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001650 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001651 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001652 echo " Done"
1653 echo ""
1654 fi
1655 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001656 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001657 if [[ ${#lines[@]} = 0 ]]; then
1658 echo "Not found"
1659 return
1660 fi
1661 local pathname
1662 local choice
1663 if [[ ${#lines[@]} > 1 ]]; then
1664 while [[ -z "$pathname" ]]; do
1665 local index=1
1666 local line
1667 for line in ${lines[@]}; do
1668 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001669 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001670 done
1671 echo
1672 echo -n "Select one: "
1673 unset choice
1674 read choice
1675 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1676 echo "Invalid choice"
1677 continue
1678 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001679 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001680 done
1681 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001682 pathname=${lines[0]}
1683 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001684 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001685}
1686
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001687function cmremote()
1688{
1689 git remote rm cmremote 2> /dev/null
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301690 GERRIT_REMOTE=$(git config --get remote.github.projectname)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001691 if [ -z "$GERRIT_REMOTE" ]
1692 then
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301693 echo Unable to set up the git remote, are you under a git repo?
1694 return 0
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001695 fi
Chirayu Desaif2f15f12014-12-16 18:22:55 +05301696 CMUSER=$(git config --get review.review.cyanogenmod.org.username)
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001697 if [ -z "$CMUSER" ]
1698 then
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001699 git remote add cmremote ssh://review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001700 else
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001701 git remote add cmremote ssh://$CMUSER@review.cyanogenmod.org:29418/$GERRIT_REMOTE
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001702 fi
1703 echo You can now push to "cmremote".
1704}
Koushik Duttab55f13a2012-05-14 16:14:36 -07001705
Steve Kondik873fa562012-09-17 11:33:18 -07001706function aospremote()
1707{
1708 git remote rm aosp 2> /dev/null
1709 if [ ! -d .git ]
1710 then
1711 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1712 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001713 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik873fa562012-09-17 11:33:18 -07001714 if (echo $PROJECT | grep -qv "^device")
1715 then
1716 PFX="platform/"
1717 fi
1718 git remote add aosp https://android.googlesource.com/$PFX$PROJECT
1719 echo "Remote 'aosp' created"
1720}
Steve Kondik873fa562012-09-17 11:33:18 -07001721
Steve Kondik20c21d22013-10-27 13:34:36 -07001722function cafremote()
1723{
1724 git remote rm caf 2> /dev/null
1725 if [ ! -d .git ]
1726 then
1727 echo .git directory not found. Please run this from the root directory of the Android repository you wish to set up.
1728 fi
Steve Kondike917827b2013-11-16 03:48:30 -08001729 PROJECT=`pwd -P | sed s#$ANDROID_BUILD_TOP/##g`
Steve Kondik20c21d22013-10-27 13:34:36 -07001730 if (echo $PROJECT | grep -qv "^device")
1731 then
1732 PFX="platform/"
1733 fi
1734 git remote add caf git://codeaurora.org/$PFX$PROJECT
1735 echo "Remote 'caf' created"
1736}
Steve Kondik20c21d22013-10-27 13:34:36 -07001737
Steve Kondikf00e7d92012-09-23 23:46:55 -07001738function installboot()
1739{
1740 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1741 then
1742 echo "No recovery.fstab found. Build recovery first."
1743 return 1
1744 fi
1745 if [ ! -e "$OUT/boot.img" ];
1746 then
1747 echo "No boot.img found. Run make bootimage first."
1748 return 1
1749 fi
1750 PARTITION=`grep "^\/boot" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1751 if [ -z "$PARTITION" ];
1752 then
Ricardo Cerqueira9e4c4a72013-07-27 13:51:56 +01001753 # Try for RECOVERY_FSTAB_VERSION = 2
1754 PARTITION=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1755 PARTITION_TYPE=`grep "[[:space:]]\/boot[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1756 if [ -z "$PARTITION" ];
1757 then
1758 echo "Unable to determine boot partition."
1759 return 1
1760 fi
Steve Kondikf00e7d92012-09-23 23:46:55 -07001761 fi
1762 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001763 adb wait-for-online
Steve Kondikf00e7d92012-09-23 23:46:55 -07001764 adb root
1765 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001766 adb wait-for-online shell mount /system 2>&1 > /dev/null
1767 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001768 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondikf00e7d92012-09-23 23:46:55 -07001769 then
1770 adb push $OUT/boot.img /cache/
1771 for i in $OUT/system/lib/modules/*;
1772 do
1773 adb push $i /system/lib/modules/
1774 done
1775 adb shell dd if=/cache/boot.img of=$PARTITION
1776 adb shell chmod 644 /system/lib/modules/*
1777 echo "Installation complete."
1778 else
1779 echo "The connected device does not appear to be $CM_BUILD, run away!"
1780 fi
1781}
1782
Steve Kondik83c03d52012-10-24 16:40:42 -07001783function installrecovery()
1784{
1785 if [ ! -e "$OUT/recovery/root/etc/recovery.fstab" ];
1786 then
1787 echo "No recovery.fstab found. Build recovery first."
1788 return 1
1789 fi
1790 if [ ! -e "$OUT/recovery.img" ];
1791 then
1792 echo "No recovery.img found. Run make recoveryimage first."
1793 return 1
1794 fi
1795 PARTITION=`grep "^\/recovery" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
1796 if [ -z "$PARTITION" ];
1797 then
Steve Kondik75f10762013-08-09 21:03:42 -07001798 # Try for RECOVERY_FSTAB_VERSION = 2
Steve Kondikd8b510a2013-08-10 21:02:09 -07001799 PARTITION=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $1'}`
1800 PARTITION_TYPE=`grep "[[:space:]]\/recovery[[:space:]]" $OUT/recovery/root/etc/recovery.fstab | awk {'print $3'}`
Steve Kondik75f10762013-08-09 21:03:42 -07001801 if [ -z "$PARTITION" ];
1802 then
1803 echo "Unable to determine recovery partition."
1804 return 1
1805 fi
Steve Kondik83c03d52012-10-24 16:40:42 -07001806 fi
1807 adb start-server
Steve Kondik3bc5cfd2013-08-30 17:34:37 -07001808 adb wait-for-online
Steve Kondik83c03d52012-10-24 16:40:42 -07001809 adb root
1810 sleep 1
Steve Kondik21e4f7f2013-04-13 13:23:35 -07001811 adb wait-for-online shell mount /system 2>&1 >> /dev/null
1812 adb wait-for-online remount
Steve Kondikec4627c2015-07-10 02:31:24 -07001813 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD");
Steve Kondik83c03d52012-10-24 16:40:42 -07001814 then
1815 adb push $OUT/recovery.img /cache/
1816 adb shell dd if=/cache/recovery.img of=$PARTITION
1817 echo "Installation complete."
1818 else
1819 echo "The connected device does not appear to be $CM_BUILD, run away!"
1820 fi
1821}
Steve Kondikf00e7d92012-09-23 23:46:55 -07001822
Koushik Duttab55f13a2012-05-14 16:14:36 -07001823function makerecipe() {
1824 if [ -z "$1" ]
1825 then
1826 echo "No branch name provided."
1827 return 1
1828 fi
1829 cd android
1830 sed -i s/'default revision=.*'/'default revision="refs\/heads\/'$1'"'/ default.xml
1831 git commit -a -m "$1"
1832 cd ..
1833
1834 repo forall -c '
1835
1836 if [ "$REPO_REMOTE" == "github" ]
1837 then
1838 pwd
1839 cmremote
1840 git push cmremote HEAD:refs/heads/'$1'
1841 fi
1842 '
1843}
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001844
1845function cmgerrit() {
Khalid Zubair01da9972016-02-08 16:54:01 -08001846
1847 if [ "$(__detect_shell)" == "zsh" ]; then
1848 # zsh does not define FUNCNAME, derive from funcstack
1849 local FUNCNAME=$funcstack[1]
1850 fi
1851
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001852 if [ $# -eq 0 ]; then
1853 $FUNCNAME help
1854 return 1
1855 fi
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07001856 local user=`git config --get review.review.cyanogenmod.org.username`
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06001857 local review=`git config --get remote.github.review`
1858 local project=`git config --get remote.github.projectname`
1859 local command=$1
1860 shift
1861 case $command in
1862 help)
1863 if [ $# -eq 0 ]; then
1864 cat <<EOF
1865Usage:
1866 $FUNCNAME COMMAND [OPTIONS] [CHANGE-ID[/PATCH-SET]][{@|^|~|:}ARG] [-- ARGS]
1867
1868Commands:
1869 fetch Just fetch the change as FETCH_HEAD
1870 help Show this help, or for a specific command
1871 pull Pull a change into current branch
1872 push Push HEAD or a local branch to Gerrit for a specific branch
1873
1874Any other Git commands that support refname would work as:
1875 git fetch URL CHANGE && git COMMAND OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1876
1877See '$FUNCNAME help COMMAND' for more information on a specific command.
1878
1879Example:
1880 $FUNCNAME checkout -b topic 1234/5
1881works as:
1882 git fetch http://DOMAIN/p/PROJECT refs/changes/34/1234/5 \\
1883 && git checkout -b topic FETCH_HEAD
1884will checkout a new branch 'topic' base on patch-set 5 of change 1234.
1885Patch-set 1 will be fetched if omitted.
1886EOF
1887 return
1888 fi
1889 case $1 in
1890 __cmg_*) echo "For internal use only." ;;
1891 changes|for)
1892 if [ "$FUNCNAME" = "cmgerrit" ]; then
1893 echo "'$FUNCNAME $1' is deprecated."
1894 fi
1895 ;;
1896 help) $FUNCNAME help ;;
1897 fetch|pull) cat <<EOF
1898usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET]
1899
1900works as:
1901 git $1 OPTIONS http://DOMAIN/p/PROJECT \\
1902 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1}
1903
1904Example:
1905 $FUNCNAME $1 1234
1906will $1 patch-set 1 of change 1234
1907EOF
1908 ;;
1909 push) cat <<EOF
1910usage: $FUNCNAME push [OPTIONS] [LOCAL_BRANCH:]REMOTE_BRANCH
1911
1912works as:
1913 git push OPTIONS ssh://USER@DOMAIN:29418/PROJECT \\
1914 {LOCAL_BRANCH|HEAD}:refs/for/REMOTE_BRANCH
1915
1916Example:
1917 $FUNCNAME push fix6789:gingerbread
1918will push local branch 'fix6789' to Gerrit for branch 'gingerbread'.
1919HEAD will be pushed from local if omitted.
1920EOF
1921 ;;
1922 *)
1923 $FUNCNAME __cmg_err_not_supported $1 && return
1924 cat <<EOF
1925usage: $FUNCNAME $1 [OPTIONS] CHANGE-ID[/PATCH-SET][{@|^|~|:}ARG] [-- ARGS]
1926
1927works as:
1928 git fetch http://DOMAIN/p/PROJECT \\
1929 refs/changes/HASH/CHANGE-ID/{PATCH-SET|1} \\
1930 && git $1 OPTIONS FETCH_HEAD{@|^|~|:}ARG -- ARGS
1931EOF
1932 ;;
1933 esac
1934 ;;
1935 __cmg_get_ref)
1936 $FUNCNAME __cmg_err_no_arg $command $# && return 1
1937 local change_id patchset_id hash
1938 case $1 in
1939 */*)
1940 change_id=${1%%/*}
1941 patchset_id=${1#*/}
1942 ;;
1943 *)
1944 change_id=$1
1945 patchset_id=1
1946 ;;
1947 esac
1948 hash=$(($change_id % 100))
1949 case $hash in
1950 [0-9]) hash="0$hash" ;;
1951 esac
1952 echo "refs/changes/$hash/$change_id/$patchset_id"
1953 ;;
1954 fetch|pull)
1955 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1956 $FUNCNAME __cmg_err_not_repo && return 1
1957 local change=$1
1958 shift
1959 git $command $@ http://$review/p/$project \
1960 $($FUNCNAME __cmg_get_ref $change) || return 1
1961 ;;
1962 push)
1963 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
1964 $FUNCNAME __cmg_err_not_repo && return 1
1965 if [ -z "$user" ]; then
1966 echo >&2 "Gerrit username not found."
1967 return 1
1968 fi
1969 local local_branch remote_branch
1970 case $1 in
1971 *:*)
1972 local_branch=${1%:*}
1973 remote_branch=${1##*:}
1974 ;;
1975 *)
1976 local_branch=HEAD
1977 remote_branch=$1
1978 ;;
1979 esac
1980 shift
1981 git push $@ ssh://$user@$review:29418/$project \
1982 $local_branch:refs/for/$remote_branch || return 1
1983 ;;
1984 changes|for)
1985 if [ "$FUNCNAME" = "cmgerrit" ]; then
1986 echo >&2 "'$FUNCNAME $command' is deprecated."
1987 fi
1988 ;;
1989 __cmg_err_no_arg)
1990 if [ $# -lt 2 ]; then
1991 echo >&2 "'$FUNCNAME $command' missing argument."
1992 elif [ $2 -eq 0 ]; then
1993 if [ -n "$3" ]; then
1994 $FUNCNAME help $1
1995 else
1996 echo >&2 "'$FUNCNAME $1' missing argument."
1997 fi
1998 else
1999 return 1
2000 fi
2001 ;;
2002 __cmg_err_not_repo)
2003 if [ -z "$review" -o -z "$project" ]; then
2004 echo >&2 "Not currently in any reviewable repository."
2005 else
2006 return 1
2007 fi
2008 ;;
2009 __cmg_err_not_supported)
2010 $FUNCNAME __cmg_err_no_arg $command $# && return
2011 case $1 in
2012 #TODO: filter more git commands that don't use refname
2013 init|add|rm|mv|status|clone|remote|bisect|config|stash)
2014 echo >&2 "'$FUNCNAME $1' is not supported."
2015 ;;
2016 *) return 1 ;;
2017 esac
2018 ;;
2019 #TODO: other special cases?
2020 *)
2021 $FUNCNAME __cmg_err_not_supported $command && return 1
2022 $FUNCNAME __cmg_err_no_arg $command $# help && return 1
2023 $FUNCNAME __cmg_err_not_repo && return 1
2024 local args="$@"
2025 local change pre_args refs_arg post_args
2026 case "$args" in
2027 *--\ *)
2028 pre_args=${args%%-- *}
2029 post_args="-- ${args#*-- }"
2030 ;;
2031 *) pre_args="$args" ;;
2032 esac
2033 args=($pre_args)
2034 pre_args=
2035 if [ ${#args[@]} -gt 0 ]; then
2036 change=${args[${#args[@]}-1]}
2037 fi
2038 if [ ${#args[@]} -gt 1 ]; then
2039 pre_args=${args[0]}
2040 for ((i=1; i<${#args[@]}-1; i++)); do
2041 pre_args="$pre_args ${args[$i]}"
2042 done
2043 fi
2044 while ((1)); do
2045 case $change in
2046 ""|--)
2047 $FUNCNAME help $command
2048 return 1
2049 ;;
2050 *@*)
2051 if [ -z "$refs_arg" ]; then
2052 refs_arg="@${change#*@}"
2053 change=${change%%@*}
2054 fi
2055 ;;
2056 *~*)
2057 if [ -z "$refs_arg" ]; then
2058 refs_arg="~${change#*~}"
2059 change=${change%%~*}
2060 fi
2061 ;;
2062 *^*)
2063 if [ -z "$refs_arg" ]; then
2064 refs_arg="^${change#*^}"
2065 change=${change%%^*}
2066 fi
2067 ;;
2068 *:*)
2069 if [ -z "$refs_arg" ]; then
2070 refs_arg=":${change#*:}"
2071 change=${change%%:*}
2072 fi
2073 ;;
2074 *) break ;;
2075 esac
2076 done
2077 $FUNCNAME fetch $change \
2078 && git $command $pre_args FETCH_HEAD$refs_arg $post_args \
2079 || return 1
2080 ;;
2081 esac
2082}
2083
2084function cmrebase() {
2085 local repo=$1
2086 local refs=$2
2087 local pwd="$(pwd)"
2088 local dir="$(gettop)/$repo"
2089
2090 if [ -z $repo ] || [ -z $refs ]; then
2091 echo "CyanogenMod Gerrit Rebase Usage: "
2092 echo " cmrebase <path to project> <patch IDs on Gerrit>"
2093 echo " The patch IDs appear on the Gerrit commands that are offered."
2094 echo " They consist on a series of numbers and slashes, after the text"
2095 echo " refs/changes. For example, the ID in the following command is 26/8126/2"
2096 echo ""
2097 echo " git[...]ges_apps_Camera refs/changes/26/8126/2 && git cherry-pick FETCH_HEAD"
2098 echo ""
2099 return
2100 fi
2101
2102 if [ ! -d $dir ]; then
2103 echo "Directory $dir doesn't exist in tree."
2104 return
2105 fi
2106 cd $dir
2107 repo=$(cat .git/config | grep git://github.com | awk '{ print $NF }' | sed s#git://github.com/##g)
2108 echo "Starting branch..."
2109 repo start tmprebase .
2110 echo "Bringing it up to date..."
2111 repo sync .
2112 echo "Fetching change..."
Pawit Pornkitprasan7bef61f2012-12-11 16:41:07 +07002113 git fetch "http://review.cyanogenmod.org/p/$repo" "refs/changes/$refs" && git cherry-pick FETCH_HEAD
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002114 if [ "$?" != "0" ]; then
2115 echo "Error cherry-picking. Not uploading!"
2116 return
2117 fi
2118 echo "Uploading..."
2119 repo upload .
2120 echo "Cleaning up..."
2121 repo abandon tmprebase .
2122 cd $pwd
2123}
2124
2125function mka() {
Khalid Zubairabcd1942015-10-06 11:00:55 -07002126 local T=$(gettop)
2127 if [ "$T" ]; then
2128 case `uname -s` in
2129 Darwin)
2130 make -C $T -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
2131 ;;
2132 *)
2133 mk_timer schedtool -B -n 1 -e ionice -n 1 make -C $T -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
2134 ;;
2135 esac
2136
2137 else
2138 echo "Couldn't locate the top of the tree. Try setting TOP."
2139 fi
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002140}
2141
Chirayu Desaicf2bdb62012-09-28 11:56:02 +05302142function cmka() {
2143 if [ ! -z "$1" ]; then
2144 for i in "$@"; do
2145 case $i in
2146 bacon|otapackage|systemimage)
2147 mka installclean
2148 mka $i
2149 ;;
2150 *)
2151 mka clean-$i
2152 mka $i
2153 ;;
2154 esac
2155 done
2156 else
2157 mka clean
2158 mka
2159 fi
2160}
2161
Khalid Zubairfdc26cd2015-10-21 12:43:14 -07002162function mms() {
2163 local T=$(gettop)
2164 if [ -z "$T" ]
2165 then
2166 echo "Couldn't locate the top of the tree. Try setting TOP."
2167 return 1
2168 fi
2169
2170 case `uname -s` in
2171 Darwin)
2172 local NUM_CPUS=$(sysctl hw.ncpu|cut -d" " -f2)
2173 ONE_SHOT_MAKEFILE="__none__" \
2174 make -C $T -j $NUM_CPUS "$@"
2175 ;;
2176 *)
2177 local NUM_CPUS=$(cat /proc/cpuinfo | grep "^processor" | wc -l)
2178 ONE_SHOT_MAKEFILE="__none__" \
2179 mk_timer schedtool -B -n 1 -e ionice -n 1 \
2180 make -C $T -j $NUM_CPUS "$@"
2181 ;;
2182 esac
2183}
2184
2185
Matt Mower8dacaed2013-12-29 12:57:20 -06002186function repolastsync() {
2187 RLSPATH="$ANDROID_BUILD_TOP/.repo/.repo_fetchtimes.json"
2188 RLSLOCAL=$(date -d "$(stat -c %z $RLSPATH)" +"%e %b %Y, %T %Z")
2189 RLSUTC=$(date -d "$(stat -c %z $RLSPATH)" -u +"%e %b %Y, %T %Z")
2190 echo "Last repo sync: $RLSLOCAL / $RLSUTC"
2191}
2192
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002193function reposync() {
2194 case `uname -s` in
2195 Darwin)
2196 repo sync -j 4 "$@"
2197 ;;
2198 *)
Alan Orthef363cd2012-09-07 11:44:27 +03002199 schedtool -B -n 1 -e ionice -n 1 `which repo` sync -j 4 "$@"
Andrew Sutherlandd6bd0652011-11-18 00:45:55 -06002200 ;;
2201 esac
2202}
Tanguy Pruvot2192fd22012-06-06 21:39:23 +02002203
2204function repodiff() {
2205 if [ -z "$*" ]; then
2206 echo "Usage: repodiff <ref-from> [[ref-to] [--numstat]]"
2207 return
2208 fi
2209 diffopts=$* repo forall -c \
2210 'echo "$REPO_PATH ($REPO_REMOTE)"; git diff ${diffopts} 2>/dev/null ;'
2211}
2212
Khalid Zubair63eabff2016-02-02 12:00:14 -08002213# Return success if adb is up and not in recovery
2214function _adb_connected {
2215 {
2216 if [[ "$(adb get-state)" == device &&
2217 "$(adb shell test -e /sbin/recovery; echo $?)" == 0 ]]
2218 then
2219 return 0
2220 fi
2221 } 2>/dev/null
2222
2223 return 1
2224};
2225
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302226# Credit for color strip sed: http://goo.gl/BoIcm
2227function dopush()
2228{
2229 local func=$1
2230 shift
2231
2232 adb start-server # Prevent unexpected starting server message from adb get-state in the next line
Khalid Zubair63eabff2016-02-02 12:00:14 -08002233 if ! _adb_connected; then
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302234 echo "No device is online. Waiting for one..."
2235 echo "Please connect USB and/or enable USB debugging"
Khalid Zubair63eabff2016-02-02 12:00:14 -08002236 until _adb_connected; do
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302237 sleep 1
2238 done
2239 echo "Device Found."
2240 fi
2241
Steve Kondikec4627c2015-07-10 02:31:24 -07002242 if (adb shell getprop ro.cm.device | grep -q "$CM_BUILD") || [ "$FORCE_PUSH" == "true" ];
Chirayu Desai6dadb572012-12-26 10:53:58 +05302243 then
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002244 # retrieve IP and PORT info if we're using a TCP connection
2245 TCPIPPORT=$(adb devices | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+[^0-9]+' \
2246 | head -1 | awk '{print $1}')
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302247 adb root &> /dev/null
2248 sleep 0.3
Sam Mortimer9e0a3f72013-08-10 22:57:08 -07002249 if [ -n "$TCPIPPORT" ]
2250 then
2251 # adb root just killed our connection
2252 # so reconnect...
2253 adb connect "$TCPIPPORT"
2254 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302255 adb wait-for-device &> /dev/null
2256 sleep 0.3
2257 adb remount &> /dev/null
2258
Matt Mower668ea262014-05-20 02:52:23 -05002259 mkdir -p $OUT
Marcos Marado37e92c22015-01-13 15:14:28 +00002260 ($func $*|tee $OUT/.log;return ${PIPESTATUS[0]})
2261 ret=$?;
2262 if [ $ret -ne 0 ]; then
2263 rm -f $OUT/.log;return $ret
2264 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302265
2266 # Install: <file>
LuK133756041e72015-12-01 14:53:23 +01002267 if [ `uname` = "Linux" ]; then
2268 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)"
2269 else
2270 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)"
2271 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302272
2273 # Copy: <file>
LuK133756041e72015-12-01 14:53:23 +01002274 if [ `uname` = "Linux" ]; then
2275 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)"
2276 else
2277 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)"
2278 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302279
Matt Mowerdfd4c082014-05-20 02:52:23 -05002280 # If any files are going to /data, push an octal file permissions reader to device
2281 if [ -n "$(echo $LOC | egrep '(^|\s)/data')" ]; then
2282 CHKPERM="/data/local/tmp/chkfileperm.sh"
2283(
2284cat <<'EOF'
2285#!/system/xbin/sh
2286FILE=$@
2287if [ -e $FILE ]; then
2288 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
2289fi
2290EOF
2291) > $OUT/.chkfileperm.sh
2292 echo "Pushing file permissions checker to device"
2293 adb push $OUT/.chkfileperm.sh $CHKPERM
2294 adb shell chmod 755 $CHKPERM
2295 rm -f $OUT/.chkfileperm.sh
2296 fi
2297
Matt Mower668ea262014-05-20 02:52:23 -05002298 stop_n_start=false
Luca Stefani406c2af2015-11-02 05:33:10 -08002299 for FILE in $(echo $LOC | tr " " "\n"); do
Matt Mowerdfd4c082014-05-20 02:52:23 -05002300 # Make sure file is in $OUT/system or $OUT/data
Matt Mower668ea262014-05-20 02:52:23 -05002301 case $FILE in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002302 $OUT/system/*|$OUT/data/*)
Matt Mower668ea262014-05-20 02:52:23 -05002303 # Get target file name (i.e. /system/bin/adb)
2304 TARGET=$(echo $FILE | sed "s#$OUT##")
2305 ;;
2306 *) continue ;;
2307 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302308
Matt Mower668ea262014-05-20 02:52:23 -05002309 case $TARGET in
Matt Mowerdfd4c082014-05-20 02:52:23 -05002310 /data/*)
2311 # fs_config only sets permissions and se labels for files pushed to /system
2312 if [ -n "$CHKPERM" ]; then
2313 OLDPERM=$(adb shell $CHKPERM $TARGET)
2314 OLDPERM=$(echo $OLDPERM | tr -d '\r' | tr -d '\n')
2315 OLDOWN=$(adb shell ls -al $TARGET | awk '{print $2}')
2316 OLDGRP=$(adb shell ls -al $TARGET | awk '{print $3}')
2317 fi
2318 echo "Pushing: $TARGET"
2319 adb push $FILE $TARGET
2320 if [ -n "$OLDPERM" ]; then
2321 echo "Setting file permissions: $OLDPERM, $OLDOWN":"$OLDGRP"
2322 adb shell chown "$OLDOWN":"$OLDGRP" $TARGET
2323 adb shell chmod "$OLDPERM" $TARGET
2324 else
2325 echo "$TARGET did not exist previously, you should set file permissions manually"
2326 fi
2327 adb shell restorecon "$TARGET"
2328 ;;
Pawit Pornkitprasan1822ad02014-12-22 20:11:47 +07002329 /system/priv-app/SystemUI/SystemUI.apk|/system/framework/*)
Matt Mower668ea262014-05-20 02:52:23 -05002330 # Only need to stop services once
2331 if ! $stop_n_start; then
2332 adb shell stop
2333 stop_n_start=true
2334 fi
2335 echo "Pushing: $TARGET"
2336 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302337 ;;
2338 *)
Matt Mower668ea262014-05-20 02:52:23 -05002339 echo "Pushing: $TARGET"
2340 adb push $FILE $TARGET
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302341 ;;
Matt Mower668ea262014-05-20 02:52:23 -05002342 esac
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302343 done
Matt Mowerdfd4c082014-05-20 02:52:23 -05002344 if [ -n "$CHKPERM" ]; then
2345 adb shell rm $CHKPERM
2346 fi
Matt Mower668ea262014-05-20 02:52:23 -05002347 if $stop_n_start; then
2348 adb shell start
2349 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302350 rm -f $OUT/.log
2351 return 0
Chirayu Desai6dadb572012-12-26 10:53:58 +05302352 else
2353 echo "The connected device does not appear to be $CM_BUILD, run away!"
2354 fi
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302355}
2356
2357alias mmp='dopush mm'
2358alias mmmp='dopush mmm'
Martin Brabhamb77db582016-02-05 15:59:21 -05002359alias mmap='dopush mma'
Chirayu Desaiae7a4d02012-10-16 19:40:18 +05302360alias mkap='dopush mka'
2361alias cmkap='dopush cmka'
2362
Chirayu Desai4a319b82013-06-05 20:14:33 +05302363function repopick() {
2364 T=$(gettop)
2365 $T/build/tools/repopick.py $@
2366}
2367
Chirayu Desaib89b3242013-06-30 10:04:25 +05302368function fixup_common_out_dir() {
2369 common_out_dir=$(get_build_var OUT_DIR)/target/common
2370 target_device=$(get_build_var TARGET_DEVICE)
2371 if [ ! -z $CM_FIXUP_COMMON_OUT ]; then
2372 if [ -d ${common_out_dir} ] && [ ! -L ${common_out_dir} ]; then
2373 mv ${common_out_dir} ${common_out_dir}-${target_device}
2374 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2375 else
2376 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2377 mkdir -p ${common_out_dir}-${target_device}
2378 ln -s ${common_out_dir}-${target_device} ${common_out_dir}
2379 fi
2380 else
2381 [ -L ${common_out_dir} ] && rm ${common_out_dir}
2382 mkdir -p ${common_out_dir}
2383 fi
2384}
2385
Michael Bestas163381a2015-10-23 20:36:47 +03002386# Force JAVA_HOME to point to java 1.7 if it isn't already set.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002387#
2388# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
2389# For some reason, installing the JDK doesn't make it show up in the
2390# JavaVM.framework/Versions/1.7/ folder.
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002391function set_java_home() {
Narayan Kamath9260bba2014-01-17 10:05:25 +00002392 # Clear the existing JAVA_HOME value if we set it ourselves, so that
Narayan Kamathc84889b2014-04-01 14:16:26 +01002393 # we can reset it later, depending on the version of java the build
2394 # system needs.
Narayan Kamath9260bba2014-01-17 10:05:25 +00002395 #
2396 # If we don't do this, the JAVA_HOME value set by the first call to
2397 # build/envsetup.sh will persist forever.
2398 if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
2399 export JAVA_HOME=""
2400 fi
2401
Andy McFaddenbd960942010-06-24 12:52:51 -07002402 if [ ! "$JAVA_HOME" ]; then
Neil Fuller46e00ea2014-10-16 10:23:03 +01002403 case `uname -s` in
2404 Darwin)
2405 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2406 ;;
2407 *)
2408 export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
2409 ;;
2410 esac
Narayan Kamath9260bba2014-01-17 10:05:25 +00002411
2412 # Keep track of the fact that we set JAVA_HOME ourselves, so that
2413 # we can change it on the next envsetup.sh, if required.
2414 export ANDROID_SET_JAVA_HOME=true
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002415 fi
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -05002416}
Jeff Hamilton04be0d82010-06-07 15:03:54 -05002417
Alex Rayf0d08eb2013-03-08 15:15:06 -08002418# Print colored exit condition
2419function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08002420 "$@"
2421 local retval=$?
2422 if [ $retval -ne 0 ]
2423 then
Roman Birgead9a5c2015-10-16 10:29:06 -07002424 printf "\e[0;31mFAILURE\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002425 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002426 printf "\e[0;32mSUCCESS\e[00m\n"
Michael Wrighteb733842013-03-08 17:34:02 -08002427 fi
2428 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08002429}
2430
Ying Wanged21d4c2014-08-24 22:14:19 -07002431function get_make_command()
2432{
2433 echo command make
2434}
2435
Anthony King40b093f2015-04-30 22:19:48 +01002436function mk_timer()
Ed Heylcc6be0a2014-06-18 14:55:58 -07002437{
2438 local start_time=$(date +"%s")
Anthony King40b093f2015-04-30 22:19:48 +01002439 $@
Ed Heylcc6be0a2014-06-18 14:55:58 -07002440 local ret=$?
2441 local end_time=$(date +"%s")
2442 local tdiff=$(($end_time-$start_time))
2443 local hours=$(($tdiff / 3600 ))
2444 local mins=$((($tdiff % 3600) / 60))
2445 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002446 local ncolors=$(tput colors 2>/dev/null)
2447 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
2448 color_failed="\e[0;31m"
2449 color_success="\e[0;32m"
Roman Birgead9a5c2015-10-16 10:29:06 -07002450 color_reset="\e[0m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07002451 else
2452 color_failed=""
2453 color_success=""
2454 color_reset=""
2455 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07002456 echo
2457 if [ $ret -eq 0 ] ; then
Roman Birgead9a5c2015-10-16 10:29:06 -07002458 printf "${color_success}#### make completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002459 else
Roman Birgead9a5c2015-10-16 10:29:06 -07002460 printf "${color_failed}#### make failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07002461 fi
2462 if [ $hours -gt 0 ] ; then
2463 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
2464 elif [ $mins -gt 0 ] ; then
2465 printf "(%02g:%02g (mm:ss))" $mins $secs
2466 elif [ $secs -gt 0 ] ; then
2467 printf "(%s seconds)" $secs
2468 fi
Roman Birgead9a5c2015-10-16 10:29:06 -07002469 printf " ####${color_reset}\n\n"
Ed Heylcc6be0a2014-06-18 14:55:58 -07002470 return $ret
2471}
2472
Anthony King40b093f2015-04-30 22:19:48 +01002473function make()
2474{
2475 mk_timer $(get_make_command) "$@"
2476}
2477
Khalid Zubair695992c2016-02-08 16:27:55 -08002478function __detect_shell() {
Raphael Moll70a86b02011-06-20 16:03:14 -07002479 case `ps -o command -p $$` in
2480 *bash*)
Khalid Zubair695992c2016-02-08 16:27:55 -08002481 echo bash
Raphael Moll70a86b02011-06-20 16:03:14 -07002482 ;;
Emilio López7ff9caa2013-11-03 13:05:43 -03002483 *zsh*)
Khalid Zubair695992c2016-02-08 16:27:55 -08002484 echo zsh
Emilio López7ff9caa2013-11-03 13:05:43 -03002485 ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002486 *)
Khalid Zubair695992c2016-02-08 16:27:55 -08002487 echo unknown
2488 return 1
Raphael Moll70a86b02011-06-20 16:03:14 -07002489 ;;
2490 esac
Khalid Zubair695992c2016-02-08 16:27:55 -08002491 return
2492}
2493
2494
2495if ! __detect_shell > /dev/null; then
2496 echo "WARNING: Only bash and zsh are supported, use of other shell may lead to erroneous results"
Raphael Moll70a86b02011-06-20 16:03:14 -07002497fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002498
2499# Execute the contents of any vendorsetup.sh files we can find.
Oleksiy Avramchenko15760a82014-10-06 18:51:58 +02002500for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
2501 `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 -08002502do
2503 echo "including $f"
2504 . $f
2505done
2506unset f
Kenny Root52aa81c2011-07-15 11:07:06 -07002507
Kyle Ladd031a0b62013-09-11 20:43:42 -04002508# Add completions
2509check_bash_version && {
2510 dirs="sdk/bash_completion vendor/cm/bash_completion"
2511 for dir in $dirs; do
2512 if [ -d ${dir} ]; then
2513 for f in `/bin/ls ${dir}/[a-z]*.bash 2> /dev/null`; do
2514 echo "including $f"
2515 . $f
2516 done
2517 fi
2518 done
2519}
Chirayu Desaie1466d62013-03-19 17:50:37 +05302520
2521export ANDROID_BUILD_TOP=$(gettop)