blob: 9198ee5e5889b010588ab0ad479f34bbddcbe95c [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
11- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user]
Anton Hanssonece9c482019-02-04 18:15:39 +000012- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080013- m: Makes from the top of the tree.
14- mm: Builds all of the modules in the current directory, but not their dependencies.
15- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
16 To limit the modules being built use the syntax: mmm dir/:target1,target2.
17- mma: Builds all of the modules in the current directory, and their dependencies.
18- mmma: Builds all of the modules in the supplied directories, and their dependencies.
19- provision: Flash device with all required partitions. Options will be passed on to fastboot.
20- cgrep: Greps on all local C/C++ files.
21- ggrep: Greps on all local Gradle files.
22- jgrep: Greps on all local Java files.
23- resgrep: Greps on all local res/*.xml files.
24- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070025- mgrep: Greps on all local Makefiles and *.bp files.
Steven Moreland62054a42018-12-06 10:11:40 -080026- sepgrep: Greps on all local sepolicy files.
27- sgrep: Greps on all local source files.
28- godir: Go to the directory containing a file.
29- allmod: List all modules.
30- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080031- pathmod: Get the directory containing a module.
Steven Moreland62054a42018-12-06 10:11:40 -080032- refreshmod: Refresh list of modules for allmod/gomod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070033
Roland Levillain39341922015-10-20 12:48:19 +010034Environment options:
Dan Albert4ae5d4b2014-10-31 16:23:08 -070035- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
36 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
37 build is leak-check clean.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080038- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070039
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070040Look at the source to view more functions. The complete list is:
41EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070042 local T=$(gettop)
43 local A=""
44 local i
Jacky Cao89483b82015-05-15 22:12:53 +080045 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046 A="$A $i"
47 done
48 echo $A
49}
50
Ying Wang08800fd2016-03-03 20:57:21 -080051# Get all the build variables needed by this script in a single call to the build system.
52function build_build_var_cache()
53{
Christopher Ferris55257d22017-03-23 11:08:58 -070054 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080055 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080056 cached_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
57 cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
Ying Wang08800fd2016-03-03 20:57:21 -080058 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080059 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080060 --vars="${cached_vars[*]}" \
61 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070062 --var-prefix=var_cache_ \
63 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080064 local ret=$?
65 if [ $ret -ne 0 ]
66 then
67 unset build_dicts_script
68 return $ret
69 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070070 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080071 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080072 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080073 unset build_dicts_script
Ying Wang08800fd2016-03-03 20:57:21 -080074 if [ $ret -ne 0 ]
75 then
76 return $ret
77 fi
78 BUILD_VAR_CACHE_READY="true"
79}
80
Ying Wangf0cb3972016-03-04 13:56:23 -080081# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080082# to get build variables not listed in this script.
83function destroy_build_var_cache()
84{
85 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070086 local v
Ying Wang08800fd2016-03-03 20:57:21 -080087 for v in $cached_vars; do
88 unset var_cache_$v
89 done
90 unset cached_vars
91 for v in $cached_abs_vars; do
92 unset abs_var_cache_$v
93 done
94 unset cached_abs_vars
95}
96
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070097# Get the value of a build variable as an absolute path.
98function get_abs_build_var()
99{
Ying Wang08800fd2016-03-03 20:57:21 -0800100 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
101 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800102 eval "echo \"\${abs_var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800103 return
104 fi
105
Christopher Ferris55257d22017-03-23 11:08:58 -0700106 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700107 if [ ! "$T" ]; then
108 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
109 return
110 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700111 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700112}
113
114# Get the exact value of a build variable.
115function get_build_var()
116{
Ying Wang08800fd2016-03-03 20:57:21 -0800117 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
118 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800119 eval "echo \"\${var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800120 return
121 fi
122
Christopher Ferris55257d22017-03-23 11:08:58 -0700123 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700124 if [ ! "$T" ]; then
125 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
126 return
127 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700128 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800129}
130
131# check to see if the supplied product is one we can build
132function check_product()
133{
Christopher Ferris55257d22017-03-23 11:08:58 -0700134 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800135 if [ ! "$T" ]; then
136 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
137 return
138 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700139 TARGET_PRODUCT=$1 \
140 TARGET_BUILD_VARIANT= \
141 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700142 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800143 get_build_var TARGET_DEVICE > /dev/null
144 # hide successful answers, but allow the errors to show
145}
146
147VARIANT_CHOICES=(user userdebug eng)
148
149# check to see if the supplied variant is valid
150function check_variant()
151{
Christopher Ferris55257d22017-03-23 11:08:58 -0700152 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800153 for v in ${VARIANT_CHOICES[@]}
154 do
155 if [ "$v" = "$1" ]
156 then
157 return 0
158 fi
159 done
160 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700161}
162
163function setpaths()
164{
Christopher Ferris55257d22017-03-23 11:08:58 -0700165 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700166 if [ ! "$T" ]; then
167 echo "Couldn't locate the top of the tree. Try setting TOP."
168 return
169 fi
170
171 ##################################################################
172 # #
173 # Read me before you modify this code #
174 # #
175 # This function sets ANDROID_BUILD_PATHS to what it is adding #
176 # to PATH, and the next time it is run, it removes that from #
177 # PATH. This is required so lunch can be run more than once #
178 # and still have working paths. #
179 # #
180 ##################################################################
181
Raphael Mollc639c782011-06-20 17:25:01 -0700182 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
183 # due to "C:\Program Files" being in the path.
184
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700185 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700186 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700187 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
188 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700189 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700190 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800191 # strip leading ':', if any
192 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500193 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700194
195 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700196 local prebuiltdir=$(getprebuilt)
197 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700198
Ben Cheng8bc4c432012-11-16 13:29:13 -0800199 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700200 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
201 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800202 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800203
Raphael Mollc639c782011-06-20 17:25:01 -0700204 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800205 export ANDROID_TOOLCHAIN=
206 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200207 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700208 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200209 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400210 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700211 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400212 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
213 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800214 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200215 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800216 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700217 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700218 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700219 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000220 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200221 *)
222 echo "Can't find toolchain for unknown architecture: $ARCH"
223 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700224 ;;
225 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700226 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800227 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700228 fi
Raphael732936d2011-06-22 14:35:32 -0700229
Christopher Ferris55257d22017-03-23 11:08:58 -0700230 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800231 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
232 fi
233
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700234 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700235
236 # add kernel specific binaries
237 case $(uname -s) in
238 Linux)
239 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
240 ;;
241 *)
242 ;;
243 esac
244
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400245 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
246 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
247 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
248 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700249 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
250
251 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
252 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200254
255 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
256 # to ensure that the corresponding 'emulator' binaries are used.
257 case $(uname -s) in
258 Darwin)
259 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
260 ;;
261 Linux)
262 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
263 ;;
264 *)
265 ANDROID_EMULATOR_PREBUILTS=
266 ;;
267 esac
268 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700269 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200270 export ANDROID_EMULATOR_PREBUILTS
271 fi
272
Jim Tangb3fda302018-12-22 10:24:55 +0800273 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
274 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700275 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
276 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
277 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
278 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800279
Ryan Prichard8426a192019-07-15 18:05:29 -0700280 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800281
282 # out with the duplicate old
283 if [ -n $ANDROID_PYTHONPATH ]; then
284 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
285 fi
286 # and in with the new
287 export ANDROID_PYTHONPATH=$T/development/python-packages:
288 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800289
Colin Crosse97e6932017-06-30 16:01:45 -0700290 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
291 export JAVA_HOME=$ANDROID_JAVA_HOME
292 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
293 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
294 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500295
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800296 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700297 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
298 export OUT=$ANDROID_PRODUCT_OUT
299
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700300 unset ANDROID_HOST_OUT
301 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
302
Simran Basidd050ed2017-02-13 13:46:48 -0800303 unset ANDROID_HOST_OUT_TESTCASES
304 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
305
306 unset ANDROID_TARGET_OUT_TESTCASES
307 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
308
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800309 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700310 # TODO: fix the path
311 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
312}
313
314function printconfig()
315{
Christopher Ferris55257d22017-03-23 11:08:58 -0700316 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800317 if [ ! "$T" ]; then
318 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
319 return
320 fi
321 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322}
323
324function set_stuff_for_environment()
325{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 setpaths
327 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700330 # With this environment variable new GCC can apply colors to warnings/errors
331 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 -0700332 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333}
334
335function set_sequence_number()
336{
Colin Cross88737132017-03-21 17:41:03 -0700337 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700338}
339
Makoto Onukida971062018-06-18 10:15:19 -0700340# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
341function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800342 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700343 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800344 *:"$cmd":*)
345 return 1
346 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700347 esac
348 return 0
349}
350
Kenny Root52aa81c2011-07-15 11:07:06 -0700351function addcompletions()
352{
353 local T dir f
354
Jim Tanga881a252018-06-19 16:34:41 +0800355 # Keep us from trying to run in something that's neither bash nor zsh.
356 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700357 return
358 fi
359
360 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800361 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700362 return
363 fi
364
Jim Tanga881a252018-06-19 16:34:41 +0800365 local completion_files=(
366 system/core/adb/adb.bash
367 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800368 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800369 )
Makoto Onukida971062018-06-18 10:15:19 -0700370 # Completion can be disabled selectively to allow users to use non-standard completion.
371 # e.g.
372 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
373 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800374 for f in ${completion_files[*]}; do
375 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700376 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700377 fi
378 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700379
Makoto Onukida971062018-06-18 10:15:19 -0700380 if should_add_completion bit ; then
381 complete -C "bit --tab" bit
382 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000383 if [ -z "$ZSH_VERSION" ]; then
384 # Doesn't work in zsh.
385 complete -o nospace -F _croot croot
386 fi
Jim Tanga881a252018-06-19 16:34:41 +0800387 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800388
dimitry73b84812018-12-11 18:06:00 +0100389 complete -F _complete_android_module_names gomod
390 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700391}
392
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700393function choosetype()
394{
395 echo "Build type choices are:"
396 echo " 1. release"
397 echo " 2. debug"
398 echo
399
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800400 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700401 DEFAULT_NUM=1
402 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700403
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800404 export TARGET_BUILD_TYPE=
405 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700406 while [ -z $TARGET_BUILD_TYPE ]
407 do
408 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800409 if [ -z "$1" ] ; then
410 read ANSWER
411 else
412 echo $1
413 ANSWER=$1
414 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700415 case $ANSWER in
416 "")
417 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
418 ;;
419 1)
420 export TARGET_BUILD_TYPE=release
421 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800422 release)
423 export TARGET_BUILD_TYPE=release
424 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425 2)
426 export TARGET_BUILD_TYPE=debug
427 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800428 debug)
429 export TARGET_BUILD_TYPE=debug
430 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700431 *)
432 echo
433 echo "I didn't understand your response. Please try again."
434 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435 ;;
436 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437 if [ -n "$1" ] ; then
438 break
439 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700440 done
441
Ying Wang08800fd2016-03-03 20:57:21 -0800442 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700443 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800444 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445}
446
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800447#
448# This function isn't really right: It chooses a TARGET_PRODUCT
449# based on the list of boards. Usually, that gets you something
450# that kinda works with a generic product, but really, you should
451# pick a product by name.
452#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700453function chooseproduct()
454{
Christopher Ferris55257d22017-03-23 11:08:58 -0700455 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456 if [ "x$TARGET_PRODUCT" != x ] ; then
457 default_value=$TARGET_PRODUCT
458 else
Ying Wang0a76df52015-06-08 11:57:26 -0700459 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 fi
461
Ying Wang08800fd2016-03-03 20:57:21 -0800462 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463 export TARGET_PRODUCT=
464 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 while [ -z "$TARGET_PRODUCT" ]
466 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700467 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468 if [ -z "$1" ] ; then
469 read ANSWER
470 else
471 echo $1
472 ANSWER=$1
473 fi
474
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 if [ -z "$ANSWER" ] ; then
476 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800477 else
478 if check_product $ANSWER
479 then
480 export TARGET_PRODUCT=$ANSWER
481 else
482 echo "** Not a valid product: $ANSWER"
483 fi
484 fi
485 if [ -n "$1" ] ; then
486 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700487 fi
488 done
489
Ying Wang08800fd2016-03-03 20:57:21 -0800490 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700491 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800492 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493}
494
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800495function choosevariant()
496{
497 echo "Variant choices are:"
498 local index=1
499 local v
500 for v in ${VARIANT_CHOICES[@]}
501 do
502 # The product name is the name of the directory containing
503 # the makefile we found, above.
504 echo " $index. $v"
505 index=$(($index+1))
506 done
507
508 local default_value=eng
509 local ANSWER
510
511 export TARGET_BUILD_VARIANT=
512 while [ -z "$TARGET_BUILD_VARIANT" ]
513 do
514 echo -n "Which would you like? [$default_value] "
515 if [ -z "$1" ] ; then
516 read ANSWER
517 else
518 echo $1
519 ANSWER=$1
520 fi
521
522 if [ -z "$ANSWER" ] ; then
523 export TARGET_BUILD_VARIANT=$default_value
524 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
525 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800526 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800527 fi
528 else
529 if check_variant $ANSWER
530 then
531 export TARGET_BUILD_VARIANT=$ANSWER
532 else
533 echo "** Not a valid variant: $ANSWER"
534 fi
535 fi
536 if [ -n "$1" ] ; then
537 break
538 fi
539 done
540}
541
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700542function choosecombo()
543{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700544 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700545
546 echo
547 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700548 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549
550 echo
551 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700552 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800553
554 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800555 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800558 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700559}
560
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561function add_lunch_combo()
562{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800563 if [ -n "$ZSH_VERSION" ]; then
564 echo -n "${funcfiletrace[1]}: "
565 else
566 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
567 fi
568 echo "add_lunch_combo is obsolete. Use COMMON_LUNCH_CHOICES in your AndroidProducts.mk instead."
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800569}
570
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571function print_lunch_menu()
572{
573 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 echo
575 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576 echo
577 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578
579 local i=1
580 local choice
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800581 for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 do
583 echo " $i. $choice"
584 i=$(($i+1))
585 done
586
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 echo
588}
589
590function lunch()
591{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 local answer
593
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 else
597 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700598 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 fi
601
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 local selection=
603
604 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700606 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800607 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
608 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800609 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700610 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800611 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800612 # array in zsh starts from 1 instead of 0.
613 if [ -n "$ZSH_VERSION" ]
614 then
615 selection=${choices[$(($answer))]}
616 else
617 selection=${choices[$(($answer-1))]}
618 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 fi
Colin Cross88737132017-03-21 17:41:03 -0700620 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700622 fi
623
Joe Onoratoda12daf2010-06-09 18:18:31 -0700624 export TARGET_BUILD_APPS=
625
Colin Cross88737132017-03-21 17:41:03 -0700626 local product variant_and_version variant version
627
628 product=${selection%%-*} # Trim everything after first dash
629 variant_and_version=${selection#*-} # Trim everything up to first dash
630 if [ "$variant_and_version" != "$selection" ]; then
631 variant=${variant_and_version%%-*}
632 if [ "$variant" != "$variant_and_version" ]; then
633 version=${variant_and_version#*-}
634 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Colin Cross88737132017-03-21 17:41:03 -0700637 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800638 then
639 echo
Colin Cross88737132017-03-21 17:41:03 -0700640 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700641 return 1
642 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800643
Colin Cross88737132017-03-21 17:41:03 -0700644 TARGET_PRODUCT=$product \
645 TARGET_BUILD_VARIANT=$variant \
646 TARGET_PLATFORM_VERSION=$version \
647 build_build_var_cache
648 if [ $? -ne 0 ]
649 then
650 return 1
651 fi
652
653 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
654 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700655 if [ -n "$version" ]; then
656 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
657 else
658 unset TARGET_PLATFORM_VERSION
659 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700660 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
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800665 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800666 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667}
668
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700669unset COMMON_LUNCH_CHOICES_CACHE
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
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700678 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800679 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700680 fi
681
682 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700683 return 0
684}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700685
Joe Onoratoda12daf2010-06-09 18:18:31 -0700686# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700687# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700688function tapas()
689{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700690 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800691 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700692 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700693 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800694 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700695
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700696 if [ "$showHelp" != "" ]; then
697 $(gettop)/build/make/tapasHelp.sh
698 return
699 fi
700
Ying Wang67f02922012-08-22 10:25:20 -0700701 if [ $(echo $arch | wc -w) -gt 1 ]; then
702 echo "tapas: Error: Multiple build archs supplied: $arch"
703 return
704 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700705 if [ $(echo $variant | wc -w) -gt 1 ]; then
706 echo "tapas: Error: Multiple build variants supplied: $variant"
707 return
708 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700709 if [ $(echo $density | wc -w) -gt 1 ]; then
710 echo "tapas: Error: Multiple densities supplied: $density"
711 return
712 fi
Ying Wang67f02922012-08-22 10:25:20 -0700713
Ying Wang0a76df52015-06-08 11:57:26 -0700714 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700715 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700716 x86) product=aosp_x86;;
717 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700718 arm64) product=aosp_arm64;;
719 x86_64) product=aosp_x86_64;;
720 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700721 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700722 if [ -z "$variant" ]; then
723 variant=eng
724 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700725 if [ -z "$apps" ]; then
726 apps=all
727 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600728 if [ -z "$density" ]; then
729 density=alldpi
730 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700731
Ying Wang67f02922012-08-22 10:25:20 -0700732 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700733 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700734 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700735 export TARGET_BUILD_TYPE=release
736 export TARGET_BUILD_APPS=$apps
737
Ying Wang08800fd2016-03-03 20:57:21 -0800738 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700739 set_stuff_for_environment
740 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800741 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700742}
743
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700744function gettop
745{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700746 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700747 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700748 # The following circumlocution ensures we remove symlinks from TOP.
749 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700750 else
751 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800752 # The following circumlocution (repeated below as well) ensures
753 # that we record the true directory name and not one that is
754 # faked up with symlink names.
755 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700756 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800757 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700758 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800760 \cd ..
synergyb112a402013-07-05 19:47:47 -0700761 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700762 done
Ying Wang9cd17642012-12-13 10:52:07 -0800763 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 if [ -f "$T/$TOPFILE" ]; then
765 echo $T
766 fi
767 fi
768 fi
769}
770
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700771function croot()
772{
Christopher Ferris55257d22017-03-23 11:08:58 -0700773 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700775 if [ "$1" ]; then
776 \cd $(gettop)/$1
777 else
778 \cd $(gettop)
779 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700780 else
781 echo "Couldn't locate the top of the tree. Try setting TOP."
782 fi
783}
784
Anton Hanssonece9c482019-02-04 18:15:39 +0000785function _croot()
786{
787 local T=$(gettop)
788 if [ "$T" ]; then
789 local cur="${COMP_WORDS[COMP_CWORD]}"
790 k=0
791 for c in $(compgen -d ${T}/${cur}); do
792 COMPREPLY[k++]=${c#${T}/}/
793 done
794 fi
795}
796
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700797function cproj()
798{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700799 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700800 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700801 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700802 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
803 T=$PWD
804 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800805 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700806 return
807 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800808 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700809 done
Ying Wang9cd17642012-12-13 10:52:07 -0800810 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700811 echo "can't find Android.mk"
812}
813
Daniel Sandler47e0a882013-07-30 13:23:52 -0400814# simplified version of ps; output in the form
815# <pid> <procname>
816function qpid() {
817 local prepend=''
818 local append=''
819 if [ "$1" = "--exact" ]; then
820 prepend=' '
821 append='$'
822 shift
823 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800824 echo "usage: qpid [[--exact] <process name|pid>"
825 return 255
826 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400827
828 local EXE="$1"
829 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800830 qpid | \grep "$prepend$EXE$append"
831 else
832 adb shell ps \
833 | tr -d '\r' \
834 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
835 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400836}
837
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800838# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700839# that has the core-file-size limit set correctly
840#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800841# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700842# if its core-file-size limit is not set already.
843# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
844
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800845function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700846{
Ying Wang08800fd2016-03-03 20:57:21 -0800847 echo "Getting root...";
848 adb root;
849 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700850
Ying Wang08800fd2016-03-03 20:57:21 -0800851 echo "Remounting root partition read-write...";
852 adb shell mount -w -o remount -t rootfs rootfs;
853 sleep 1;
854 adb wait-for-device;
855 adb shell mkdir -p /cores;
856 adb shell mount -t tmpfs tmpfs /cores;
857 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700858
Ying Wang08800fd2016-03-03 20:57:21 -0800859 echo "Granting SELinux permission to dump in /cores...";
860 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700861
Ying Wang08800fd2016-03-03 20:57:21 -0800862 echo "Set core pattern.";
863 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700864
Ying Wang08800fd2016-03-03 20:57:21 -0800865 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866}
867
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800868# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869# $1 = PID of process (e.g., $(pid mediaserver))
870#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800871# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700872# dump to actually be generated.
873
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800874function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700875{
Ying Wang08800fd2016-03-03 20:57:21 -0800876 local PID=$1;
877 if [ -z "$PID" ]; then
878 printf "Expecting a PID!\n";
879 return;
880 fi;
881 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800882 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700883}
884
885# core - send SIGV and pull the core for process
886# $1 = PID of process (e.g., $(pid mediaserver))
887#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800888# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700889# enabled globally.
890
891function core()
892{
Ying Wang08800fd2016-03-03 20:57:21 -0800893 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700894
Ying Wang08800fd2016-03-03 20:57:21 -0800895 if [ -z "$PID" ]; then
896 printf "Expecting a PID!\n";
897 return;
898 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700899
Ying Wang08800fd2016-03-03 20:57:21 -0800900 local CORENAME=core.$PID;
901 local COREPATH=/cores/$CORENAME;
902 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700903
Ying Wang08800fd2016-03-03 20:57:21 -0800904 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700905
Ying Wang08800fd2016-03-03 20:57:21 -0800906 local done=0;
907 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
908 printf "\tSending SIG%s to %d...\n" $SIG $PID;
909 adb shell kill -$SIG $PID;
910 sleep 1;
911 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700912
Ying Wang08800fd2016-03-03 20:57:21 -0800913 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
914 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915}
916
Christopher Tate744ee802009-11-12 15:33:08 -0800917# systemstack - dump the current stack trace of all threads in the system process
918# to the usual ANR traces file
919function systemstack()
920{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800921 stacks system_server
922}
923
Michael Wrightaeed7212014-06-19 19:58:12 -0700924# Read the ELF header from /proc/$PID/exe to determine if the process is
925# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800926function is64bit()
927{
928 local PID="$1"
929 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800930 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800931 echo "64"
932 else
933 echo ""
934 fi
935 else
936 echo ""
937 fi
938}
939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700940case `uname -s` in
941 Darwin)
942 function sgrep()
943 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100944 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|xml|sh|mk|aidl|vts)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400945 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700946 }
947
948 ;;
949 *)
950 function sgrep()
951 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100952 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400953 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700954 }
955 ;;
956esac
957
Raghu Gandham8da43102012-07-25 19:57:22 -0700958function gettargetarch
959{
960 get_build_var TARGET_ARCH
961}
962
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700963function ggrep()
964{
Mike Frysinger5e479732015-09-22 18:13:48 -0400965 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
966 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700967}
968
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700969function jgrep()
970{
Mike Frysinger5e479732015-09-22 18:13:48 -0400971 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
972 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700973}
974
975function cgrep()
976{
Mike Frysinger5e479732015-09-22 18:13:48 -0400977 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' \) \
978 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700979}
980
981function resgrep()
982{
Christopher Ferris55257d22017-03-23 11:08:58 -0700983 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400984 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
985 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
986 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700987}
988
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800989function mangrep()
990{
Mike Frysinger5e479732015-09-22 18:13:48 -0400991 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
992 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800993}
994
Alex Klyubinba5fc8e2013-05-06 14:11:48 -0700995function sepgrep()
996{
Mike Frysinger5e479732015-09-22 18:13:48 -0400997 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
998 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -0700999}
1000
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001001function rcgrep()
1002{
Mike Frysinger5e479732015-09-22 18:13:48 -04001003 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1004 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001005}
1006
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001007case `uname -s` in
1008 Darwin)
1009 function mgrep()
1010 {
David Grossd1d6fc52017-05-10 10:49:44 -07001011 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?soong/[^/]*.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001012 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001013 }
1014
1015 function treegrep()
1016 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001017 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001018 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001019 }
1020
1021 ;;
1022 *)
1023 function mgrep()
1024 {
David Grossd1d6fc52017-05-10 10:49:44 -07001025 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regextype posix-extended -regex '(.*/)?soong/[^/]*.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001026 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001027 }
1028
1029 function treegrep()
1030 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001031 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001032 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001033 }
1034
1035 ;;
1036esac
1037
1038function getprebuilt
1039{
1040 get_abs_build_var ANDROID_PREBUILTS
1041}
1042
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001043function tracedmdump()
1044{
Christopher Ferris55257d22017-03-23 11:08:58 -07001045 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001046 if [ ! "$T" ]; then
1047 echo "Couldn't locate the top of the tree. Try setting TOP."
1048 return
1049 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001050 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001051 local arch=$(gettargetarch)
1052 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001053
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001054 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001055 if [ ! "$TRACE" ] ; then
1056 echo "usage: tracedmdump tracename"
1057 return
1058 fi
1059
Jack Veenstra60116fc2009-04-09 18:12:34 -07001060 if [ ! -r "$KERNEL" ] ; then
1061 echo "Error: cannot find kernel: '$KERNEL'"
1062 return
1063 fi
1064
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001065 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001066 if [ "$BASETRACE" = "$TRACE" ] ; then
1067 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1068 fi
1069
1070 echo "post-processing traces..."
1071 rm -f $TRACE/qtrace.dexlist
1072 post_trace $TRACE
1073 if [ $? -ne 0 ]; then
1074 echo "***"
1075 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1076 echo "***"
1077 return
1078 fi
1079 echo "generating dexlist output..."
1080 /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
1081 echo "generating dmtrace data..."
1082 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1083 echo "generating html file..."
1084 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1085 echo "done, see $TRACE/dmtrace.html for details"
1086 echo "or run:"
1087 echo " traceview $TRACE/dmtrace"
1088}
1089
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001090# communicate with a running device or emulator, set up necessary state,
1091# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001092function runhat()
1093{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001094 # process standard adb options
1095 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001096 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001097 adbTarget=$1
1098 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001099 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001100 adbTarget="$1 $2"
1101 shift 2
1102 fi
1103 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001104 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001105
1106 # runhat options
1107 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001108
1109 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001110 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001111 return
1112 fi
1113
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001114 # confirm hat is available
1115 if [ -z $(which hat) ]; then
1116 echo "hat is not available in this configuration."
1117 return
1118 fi
1119
Andy McFaddenb6289852010-07-12 08:00:19 -07001120 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001121 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001122 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001123 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001124 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001125 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001126 echo -n "> "
1127 read
1128
The Android Open Source Project88b60792009-03-03 19:28:42 -08001129 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001130
The Android Open Source Project88b60792009-03-03 19:28:42 -08001131 echo "Retrieving file $devFile..."
1132 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001133
The Android Open Source Project88b60792009-03-03 19:28:42 -08001134 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001135
The Android Open Source Project88b60792009-03-03 19:28:42 -08001136 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001137 echo "View the output by pointing your browser at http://localhost:7000/"
1138 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001139 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001140}
1141
1142function getbugreports()
1143{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001144 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001145
1146 if [ ! "$reports" ]; then
1147 echo "Could not locate any bugreports."
1148 return
1149 fi
1150
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001151 local report
1152 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001154 echo "/sdcard/bugreports/${report}"
1155 adb pull /sdcard/bugreports/${report} ${report}
1156 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001157 done
1158}
1159
Victoria Lease1b296b42012-08-21 15:44:06 -07001160function getsdcardpath()
1161{
1162 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1163}
1164
1165function getscreenshotpath()
1166{
1167 echo "$(getsdcardpath)/Pictures/Screenshots"
1168}
1169
1170function getlastscreenshot()
1171{
1172 local screenshot_path=$(getscreenshotpath)
1173 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1174 if [ "$screenshot" = "" ]; then
1175 echo "No screenshots found."
1176 return
1177 fi
1178 echo "${screenshot}"
1179 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1180}
1181
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001182function startviewserver()
1183{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001184 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001185 if [ $# -gt 0 ]; then
1186 port=$1
1187 fi
1188 adb shell service call window 1 i32 $port
1189}
1190
1191function stopviewserver()
1192{
1193 adb shell service call window 2
1194}
1195
1196function isviewserverstarted()
1197{
1198 adb shell service call window 3
1199}
1200
Romain Guyb84049a2010-10-04 16:56:11 -07001201function key_home()
1202{
1203 adb shell input keyevent 3
1204}
1205
1206function key_back()
1207{
1208 adb shell input keyevent 4
1209}
1210
1211function key_menu()
1212{
1213 adb shell input keyevent 82
1214}
1215
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216function smoketest()
1217{
1218 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1219 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1220 return
1221 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001222 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223 if [ ! "$T" ]; then
1224 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1225 return
1226 fi
1227
Ying Wang9cd17642012-12-13 10:52:07 -08001228 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001229 adb uninstall com.android.smoketest > /dev/null &&
1230 adb uninstall com.android.smoketest.tests > /dev/null &&
1231 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1232 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1233 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1234}
1235
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001236# simple shortcut to the runtest command
1237function runtest()
1238{
Christopher Ferris55257d22017-03-23 11:08:58 -07001239 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001240 if [ ! "$T" ]; then
1241 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1242 return
1243 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001244 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001245}
1246
The Android Open Source Project88b60792009-03-03 19:28:42 -08001247function godir () {
1248 if [[ -z "$1" ]]; then
1249 echo "Usage: godir <regex>"
1250 return
1251 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001252 local T=$(gettop)
1253 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001254 if [ ! "$OUT_DIR" = "" ]; then
1255 mkdir -p $OUT_DIR
1256 FILELIST=$OUT_DIR/filelist
1257 else
1258 FILELIST=$T/filelist
1259 fi
1260 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001261 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001262 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001263 echo " Done"
1264 echo ""
1265 fi
1266 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001267 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001268 if [[ ${#lines[@]} = 0 ]]; then
1269 echo "Not found"
1270 return
1271 fi
1272 local pathname
1273 local choice
1274 if [[ ${#lines[@]} > 1 ]]; then
1275 while [[ -z "$pathname" ]]; do
1276 local index=1
1277 local line
1278 for line in ${lines[@]}; do
1279 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001280 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001281 done
1282 echo
1283 echo -n "Select one: "
1284 unset choice
1285 read choice
1286 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1287 echo "Invalid choice"
1288 continue
1289 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001290 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001291 done
1292 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001293 pathname=${lines[0]}
1294 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001295 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001296}
1297
Steven Moreland62054a42018-12-06 10:11:40 -08001298# Update module-info.json in out.
1299function refreshmod() {
1300 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1301 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1302 return 1
1303 fi
1304
1305 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1306
1307 # for the output of the next command
1308 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1309
1310 # Note, can't use absolute path because of the way make works.
1311 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1312 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1313}
1314
1315# List all modules for the current device, as cached in module-info.json. If any build change is
1316# made and it should be reflected in the output, you should run 'refreshmod' first.
1317function allmod() {
1318 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1319 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1320 return 1
1321 fi
1322
1323 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1324 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1325 refreshmod || return 1
1326 fi
1327
1328 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1329}
1330
Rett Berg78d1c932019-01-24 14:34:23 -08001331# Get the path of a specific module in the android tree, as cached in module-info.json. If any build change
Steven Moreland62054a42018-12-06 10:11:40 -08001332# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001333function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001334 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1335 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1336 return 1
1337 fi
1338
1339 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001340 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001341 return 1
1342 fi
1343
1344 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1345 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1346 refreshmod || return 1
1347 fi
1348
1349 local relpath=$(python -c "import json, os
1350module = '$1'
1351module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1352if module not in module_info:
1353 exit(1)
1354print module_info[module]['path'][0]" 2>/dev/null)
1355
1356 if [ -z "$relpath" ]; then
1357 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1358 return 1
1359 else
Rett Berg78d1c932019-01-24 14:34:23 -08001360 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001361 fi
1362}
1363
Rett Berg78d1c932019-01-24 14:34:23 -08001364# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1365# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1366function gomod() {
1367 if [[ $# -ne 1 ]]; then
1368 echo "usage: gomod <module>" >&2
1369 return 1
1370 fi
1371
1372 local path="$(pathmod $@)"
1373 if [ -z "$path" ]; then
1374 return 1
1375 fi
1376 cd $path
1377}
1378
dimitry73b84812018-12-11 18:06:00 +01001379function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001380 local word=${COMP_WORDS[COMP_CWORD]}
1381 COMPREPLY=( $(allmod | grep -E "^$word") )
1382}
1383
Alex Rayf0d08eb2013-03-08 15:15:06 -08001384# Print colored exit condition
1385function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001386 "$@"
1387 local retval=$?
1388 if [ $retval -ne 0 ]
1389 then
Jacky Cao89483b82015-05-15 22:12:53 +08001390 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001391 else
Jacky Cao89483b82015-05-15 22:12:53 +08001392 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001393 fi
1394 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001395}
1396
Ying Wanged21d4c2014-08-24 22:14:19 -07001397function get_make_command()
1398{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001399 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1400 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001401 # Always use the real make if -C is passed in
1402 for arg in "$@"; do
1403 if [[ $arg == -C* ]]; then
1404 echo command make
1405 return
1406 fi
1407 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001408 echo build/soong/soong_ui.bash --make-mode
1409 else
1410 echo command make
1411 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001412}
1413
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001414function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001415{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001416 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1417 "$@"
1418 return $?
1419 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001420 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001421 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001422 local ret=$?
1423 local end_time=$(date +"%s")
1424 local tdiff=$(($end_time-$start_time))
1425 local hours=$(($tdiff / 3600 ))
1426 local mins=$((($tdiff % 3600) / 60))
1427 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001428 local ncolors=$(tput colors 2>/dev/null)
1429 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001430 color_failed=$'\E'"[0;31m"
1431 color_success=$'\E'"[0;32m"
1432 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001433 else
1434 color_failed=""
1435 color_success=""
1436 color_reset=""
1437 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001438 echo
1439 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001440 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001441 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001442 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001443 fi
1444 if [ $hours -gt 0 ] ; then
1445 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1446 elif [ $mins -gt 0 ] ; then
1447 printf "(%02g:%02g (mm:ss))" $mins $secs
1448 elif [ $secs -gt 0 ] ; then
1449 printf "(%s seconds)" $secs
1450 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001451 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001452 echo
1453 return $ret
1454}
1455
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001456function _trigger_build()
1457(
1458 local -r bc="$1"; shift
1459 if T="$(gettop)"; then
1460 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1461 else
1462 echo "Couldn't locate the top of the tree. Try setting TOP."
1463 fi
1464)
1465
1466function m()
1467(
1468 _trigger_build "all-modules" "$@"
1469)
1470
1471function mm()
1472(
1473 _trigger_build "modules-in-a-dir-no-deps" "$@"
1474)
1475
1476function mmm()
1477(
1478 _trigger_build "modules-in-dirs-no-deps" "$@"
1479)
1480
1481function mma()
1482(
1483 _trigger_build "modules-in-a-dir" "$@"
1484)
1485
1486function mmma()
1487(
1488 _trigger_build "modules-in-dirs" "$@"
1489)
1490
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001491function make()
1492{
Dan Willemsene9842242017-07-28 13:00:13 -07001493 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001494}
1495
David Zeuthen1b126ff2015-09-30 17:10:48 -04001496function provision()
1497{
1498 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1499 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1500 return 1
1501 fi
1502 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1503 echo "There is no provisioning script for the device." >&2
1504 return 1
1505 fi
1506
1507 # Check if user really wants to do this.
1508 if [ "$1" = "--no-confirmation" ]; then
1509 shift 1
1510 else
1511 echo "This action will reflash your device."
1512 echo ""
1513 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1514 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001515 echo -n "Are you sure you want to do this (yes/no)? "
1516 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001517 if [[ "${REPLY}" != "yes" ]] ; then
1518 echo "Not taking any action. Exiting." >&2
1519 return 1
1520 fi
1521 fi
1522 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1523}
1524
Jim Tanga881a252018-06-19 16:34:41 +08001525# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001526function enable_zsh_completion() {
1527 # Don't override user's options if bash-style completion is already enabled.
1528 if ! declare -f complete >/dev/null; then
1529 autoload -U compinit && compinit
1530 autoload -U bashcompinit && bashcompinit
1531 fi
Jim Tanga881a252018-06-19 16:34:41 +08001532}
1533
1534function validate_current_shell() {
1535 local current_sh="$(ps -o command -p $$)"
1536 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001537 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001538 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001539 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001540 *zsh*)
1541 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001542 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001543 *)
Jim Tanga881a252018-06-19 16:34:41 +08001544 echo -e "WARNING: Only bash and zsh are supported.\nUse of other shell would lead to erroneous results."
Raphael Moll70a86b02011-06-20 16:03:14 -07001545 ;;
1546 esac
Jim Tanga881a252018-06-19 16:34:41 +08001547}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001548
1549# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001550# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1551# load those.
1552#
1553# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001554function source_vendorsetup() {
Dan Willemsend855a722019-02-12 15:52:36 -08001555 allowed=
1556 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1557 if [ -n "$allowed" ]; then
1558 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1559 echo " $allowed"
1560 echo " $f"
1561 return
1562 fi
1563 allowed="$f"
1564 done
1565
1566 allowed_files=
1567 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001568 for dir in device vendor product; do
1569 for f in $(test -d $dir && \
1570 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001571
1572 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1573 echo "including $f"; . "$f"
1574 else
1575 echo "ignoring $f, not in $allowed"
1576 fi
Jim Tanga881a252018-06-19 16:34:41 +08001577 done
1578 done
1579}
Kenny Root52aa81c2011-07-15 11:07:06 -07001580
Jim Tanga881a252018-06-19 16:34:41 +08001581validate_current_shell
1582source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001583addcompletions