blob: 25f55e8b0ae3676fb7571c48ad4d8921f6313ab8 [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.
Orion Hodson831472d2019-10-25 11:35:15 +010022- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080023- jgrep: Greps on all local Java files.
24- resgrep: Greps on all local res/*.xml files.
25- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070026- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060027- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080028- sepgrep: Greps on all local sepolicy files.
29- sgrep: Greps on all local source files.
30- godir: Go to the directory containing a file.
31- allmod: List all modules.
32- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080033- pathmod: Get the directory containing a module.
Steven Moreland62054a42018-12-06 10:11:40 -080034- refreshmod: Refresh list of modules for allmod/gomod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070035
Roland Levillain39341922015-10-20 12:48:19 +010036Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070037- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
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:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800288 if [ -n $VENDOR_PYTHONPATH ]; then
289 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
290 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800291 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800292
Colin Crosse97e6932017-06-30 16:01:45 -0700293 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
294 export JAVA_HOME=$ANDROID_JAVA_HOME
295 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
296 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
297 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500298
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800299 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700300 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
301 export OUT=$ANDROID_PRODUCT_OUT
302
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700303 unset ANDROID_HOST_OUT
304 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
305
Simran Basidd050ed2017-02-13 13:46:48 -0800306 unset ANDROID_HOST_OUT_TESTCASES
307 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
308
309 unset ANDROID_TARGET_OUT_TESTCASES
310 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
311
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800312 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700313 # TODO: fix the path
314 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
315}
316
317function printconfig()
318{
Christopher Ferris55257d22017-03-23 11:08:58 -0700319 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800320 if [ ! "$T" ]; then
321 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
322 return
323 fi
324 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700325}
326
327function set_stuff_for_environment()
328{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 setpaths
330 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700331
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800332 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700333 # With this environment variable new GCC can apply colors to warnings/errors
334 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700335}
336
337function set_sequence_number()
338{
Colin Cross88737132017-03-21 17:41:03 -0700339 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700340}
341
Makoto Onukida971062018-06-18 10:15:19 -0700342# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
343function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800344 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700345 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800346 *:"$cmd":*)
347 return 1
348 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700349 esac
350 return 0
351}
352
Kenny Root52aa81c2011-07-15 11:07:06 -0700353function addcompletions()
354{
355 local T dir f
356
Jim Tanga881a252018-06-19 16:34:41 +0800357 # Keep us from trying to run in something that's neither bash nor zsh.
358 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700359 return
360 fi
361
362 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800363 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700364 return
365 fi
366
Jim Tanga881a252018-06-19 16:34:41 +0800367 local completion_files=(
368 system/core/adb/adb.bash
369 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800370 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800371 )
Makoto Onukida971062018-06-18 10:15:19 -0700372 # Completion can be disabled selectively to allow users to use non-standard completion.
373 # e.g.
374 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
375 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800376 for f in ${completion_files[*]}; do
377 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700378 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700379 fi
380 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700381
Makoto Onukida971062018-06-18 10:15:19 -0700382 if should_add_completion bit ; then
383 complete -C "bit --tab" bit
384 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000385 if [ -z "$ZSH_VERSION" ]; then
386 # Doesn't work in zsh.
387 complete -o nospace -F _croot croot
388 fi
Jim Tanga881a252018-06-19 16:34:41 +0800389 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800390
dimitry73b84812018-12-11 18:06:00 +0100391 complete -F _complete_android_module_names gomod
392 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700393}
394
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700395function choosetype()
396{
397 echo "Build type choices are:"
398 echo " 1. release"
399 echo " 2. debug"
400 echo
401
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800402 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700403 DEFAULT_NUM=1
404 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700405
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800406 export TARGET_BUILD_TYPE=
407 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700408 while [ -z $TARGET_BUILD_TYPE ]
409 do
410 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800411 if [ -z "$1" ] ; then
412 read ANSWER
413 else
414 echo $1
415 ANSWER=$1
416 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700417 case $ANSWER in
418 "")
419 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
420 ;;
421 1)
422 export TARGET_BUILD_TYPE=release
423 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800424 release)
425 export TARGET_BUILD_TYPE=release
426 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700427 2)
428 export TARGET_BUILD_TYPE=debug
429 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800430 debug)
431 export TARGET_BUILD_TYPE=debug
432 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700433 *)
434 echo
435 echo "I didn't understand your response. Please try again."
436 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700437 ;;
438 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800439 if [ -n "$1" ] ; then
440 break
441 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700442 done
443
Ying Wang08800fd2016-03-03 20:57:21 -0800444 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800446 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700447}
448
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800449#
450# This function isn't really right: It chooses a TARGET_PRODUCT
451# based on the list of boards. Usually, that gets you something
452# that kinda works with a generic product, but really, you should
453# pick a product by name.
454#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455function chooseproduct()
456{
Christopher Ferris55257d22017-03-23 11:08:58 -0700457 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700458 if [ "x$TARGET_PRODUCT" != x ] ; then
459 default_value=$TARGET_PRODUCT
460 else
Ying Wang0a76df52015-06-08 11:57:26 -0700461 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462 fi
463
Ying Wang08800fd2016-03-03 20:57:21 -0800464 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800465 export TARGET_PRODUCT=
466 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 while [ -z "$TARGET_PRODUCT" ]
468 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700469 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800470 if [ -z "$1" ] ; then
471 read ANSWER
472 else
473 echo $1
474 ANSWER=$1
475 fi
476
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 if [ -z "$ANSWER" ] ; then
478 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800479 else
480 if check_product $ANSWER
481 then
482 export TARGET_PRODUCT=$ANSWER
483 else
484 echo "** Not a valid product: $ANSWER"
485 fi
486 fi
487 if [ -n "$1" ] ; then
488 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700489 fi
490 done
491
Ying Wang08800fd2016-03-03 20:57:21 -0800492 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800494 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495}
496
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800497function choosevariant()
498{
499 echo "Variant choices are:"
500 local index=1
501 local v
502 for v in ${VARIANT_CHOICES[@]}
503 do
504 # The product name is the name of the directory containing
505 # the makefile we found, above.
506 echo " $index. $v"
507 index=$(($index+1))
508 done
509
510 local default_value=eng
511 local ANSWER
512
513 export TARGET_BUILD_VARIANT=
514 while [ -z "$TARGET_BUILD_VARIANT" ]
515 do
516 echo -n "Which would you like? [$default_value] "
517 if [ -z "$1" ] ; then
518 read ANSWER
519 else
520 echo $1
521 ANSWER=$1
522 fi
523
524 if [ -z "$ANSWER" ] ; then
525 export TARGET_BUILD_VARIANT=$default_value
526 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
527 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200528 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800529 fi
530 else
531 if check_variant $ANSWER
532 then
533 export TARGET_BUILD_VARIANT=$ANSWER
534 else
535 echo "** Not a valid variant: $ANSWER"
536 fi
537 fi
538 if [ -n "$1" ] ; then
539 break
540 fi
541 done
542}
543
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544function choosecombo()
545{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700546 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700547
548 echo
549 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700550 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700551
552 echo
553 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700554 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800555
556 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800557 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700558 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800560 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700561}
562
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800563function add_lunch_combo()
564{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800565 if [ -n "$ZSH_VERSION" ]; then
566 echo -n "${funcfiletrace[1]}: "
567 else
568 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
569 fi
570 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 -0800571}
572
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700573function print_lunch_menu()
574{
575 local uname=$(uname)
Colin Crosscb8337d2019-09-23 12:52:32 -0700576 local choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 echo
578 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 echo
580 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581
582 local i=1
583 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700584 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 do
586 echo " $i. $choice"
587 i=$(($i+1))
588 done
589
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 echo
591}
592
593function lunch()
594{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 local answer
596
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700597 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800598 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 else
600 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700601 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700603 fi
604
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800605 local selection=
606
607 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700609 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800610 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
611 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800612 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700613 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800614 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800615 # array in zsh starts from 1 instead of 0.
616 if [ -n "$ZSH_VERSION" ]
617 then
618 selection=${choices[$(($answer))]}
619 else
620 selection=${choices[$(($answer-1))]}
621 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800622 fi
Colin Cross88737132017-03-21 17:41:03 -0700623 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700625 fi
626
Joe Onoratoda12daf2010-06-09 18:18:31 -0700627 export TARGET_BUILD_APPS=
628
Colin Cross88737132017-03-21 17:41:03 -0700629 local product variant_and_version variant version
630
631 product=${selection%%-*} # Trim everything after first dash
632 variant_and_version=${selection#*-} # Trim everything up to first dash
633 if [ "$variant_and_version" != "$selection" ]; then
634 variant=${variant_and_version%%-*}
635 if [ "$variant" != "$variant_and_version" ]; then
636 version=${variant_and_version#*-}
637 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700638 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800639
Colin Cross88737132017-03-21 17:41:03 -0700640 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800641 then
642 echo
Colin Cross88737132017-03-21 17:41:03 -0700643 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700644 return 1
645 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646
Colin Cross88737132017-03-21 17:41:03 -0700647 TARGET_PRODUCT=$product \
648 TARGET_BUILD_VARIANT=$variant \
649 TARGET_PLATFORM_VERSION=$version \
650 build_build_var_cache
651 if [ $? -ne 0 ]
652 then
653 return 1
654 fi
655
656 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
657 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700658 if [ -n "$version" ]; then
659 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
660 else
661 unset TARGET_PLATFORM_VERSION
662 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700663 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664
665 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800666
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800668 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800669 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700670}
671
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700672unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700673# Tab completion for lunch.
674function _lunch()
675{
676 local cur prev opts
677 COMPREPLY=()
678 cur="${COMP_WORDS[COMP_CWORD]}"
679 prev="${COMP_WORDS[COMP_CWORD-1]}"
680
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700681 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800682 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700683 fi
684
685 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700686 return 0
687}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700688
Joe Onoratoda12daf2010-06-09 18:18:31 -0700689# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700690# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700691function tapas()
692{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700693 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800694 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700695 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700696 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 -0800697 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 -0700698
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700699 if [ "$showHelp" != "" ]; then
700 $(gettop)/build/make/tapasHelp.sh
701 return
702 fi
703
Ying Wang67f02922012-08-22 10:25:20 -0700704 if [ $(echo $arch | wc -w) -gt 1 ]; then
705 echo "tapas: Error: Multiple build archs supplied: $arch"
706 return
707 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700708 if [ $(echo $variant | wc -w) -gt 1 ]; then
709 echo "tapas: Error: Multiple build variants supplied: $variant"
710 return
711 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700712 if [ $(echo $density | wc -w) -gt 1 ]; then
713 echo "tapas: Error: Multiple densities supplied: $density"
714 return
715 fi
Ying Wang67f02922012-08-22 10:25:20 -0700716
Ying Wang0a76df52015-06-08 11:57:26 -0700717 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700718 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700719 x86) product=aosp_x86;;
720 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700721 arm64) product=aosp_arm64;;
722 x86_64) product=aosp_x86_64;;
723 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700724 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700725 if [ -z "$variant" ]; then
726 variant=eng
727 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700728 if [ -z "$apps" ]; then
729 apps=all
730 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600731 if [ -z "$density" ]; then
732 density=alldpi
733 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700734
Ying Wang67f02922012-08-22 10:25:20 -0700735 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700737 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700738 export TARGET_BUILD_TYPE=release
739 export TARGET_BUILD_APPS=$apps
740
Ying Wang08800fd2016-03-03 20:57:21 -0800741 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700742 set_stuff_for_environment
743 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800744 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700745}
746
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700747function gettop
748{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700749 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700750 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700751 # The following circumlocution ensures we remove symlinks from TOP.
752 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700753 else
754 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800755 # The following circumlocution (repeated below as well) ensures
756 # that we record the true directory name and not one that is
757 # faked up with symlink names.
758 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800760 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700761 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700762 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800763 \cd ..
synergyb112a402013-07-05 19:47:47 -0700764 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700765 done
Ying Wang9cd17642012-12-13 10:52:07 -0800766 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 if [ -f "$T/$TOPFILE" ]; then
768 echo $T
769 fi
770 fi
771 fi
772}
773
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774function croot()
775{
Christopher Ferris55257d22017-03-23 11:08:58 -0700776 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700777 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700778 if [ "$1" ]; then
779 \cd $(gettop)/$1
780 else
781 \cd $(gettop)
782 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 else
784 echo "Couldn't locate the top of the tree. Try setting TOP."
785 fi
786}
787
Anton Hanssonece9c482019-02-04 18:15:39 +0000788function _croot()
789{
790 local T=$(gettop)
791 if [ "$T" ]; then
792 local cur="${COMP_WORDS[COMP_CWORD]}"
793 k=0
794 for c in $(compgen -d ${T}/${cur}); do
795 COMPREPLY[k++]=${c#${T}/}/
796 done
797 fi
798}
799
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700800function cproj()
801{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700802 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700803 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700804 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700805 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
806 T=$PWD
807 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800808 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700809 return
810 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800811 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700812 done
Ying Wang9cd17642012-12-13 10:52:07 -0800813 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700814 echo "can't find Android.mk"
815}
816
Daniel Sandler47e0a882013-07-30 13:23:52 -0400817# simplified version of ps; output in the form
818# <pid> <procname>
819function qpid() {
820 local prepend=''
821 local append=''
822 if [ "$1" = "--exact" ]; then
823 prepend=' '
824 append='$'
825 shift
826 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800827 echo "usage: qpid [[--exact] <process name|pid>"
828 return 255
829 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400830
831 local EXE="$1"
832 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800833 qpid | \grep "$prepend$EXE$append"
834 else
835 adb shell ps \
836 | tr -d '\r' \
837 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
838 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400839}
840
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800841# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700842# that has the core-file-size limit set correctly
843#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800844# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700845# if its core-file-size limit is not set already.
846# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
847
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800848function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700849{
Ying Wang08800fd2016-03-03 20:57:21 -0800850 echo "Getting root...";
851 adb root;
852 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700853
Ying Wang08800fd2016-03-03 20:57:21 -0800854 echo "Remounting root partition read-write...";
855 adb shell mount -w -o remount -t rootfs rootfs;
856 sleep 1;
857 adb wait-for-device;
858 adb shell mkdir -p /cores;
859 adb shell mount -t tmpfs tmpfs /cores;
860 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700861
Ying Wang08800fd2016-03-03 20:57:21 -0800862 echo "Granting SELinux permission to dump in /cores...";
863 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700864
Ying Wang08800fd2016-03-03 20:57:21 -0800865 echo "Set core pattern.";
866 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700867
Ying Wang08800fd2016-03-03 20:57:21 -0800868 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869}
870
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800871# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700872# $1 = PID of process (e.g., $(pid mediaserver))
873#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800874# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700875# dump to actually be generated.
876
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800877function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700878{
Ying Wang08800fd2016-03-03 20:57:21 -0800879 local PID=$1;
880 if [ -z "$PID" ]; then
881 printf "Expecting a PID!\n";
882 return;
883 fi;
884 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800885 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700886}
887
888# core - send SIGV and pull the core for process
889# $1 = PID of process (e.g., $(pid mediaserver))
890#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800891# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700892# enabled globally.
893
894function core()
895{
Ying Wang08800fd2016-03-03 20:57:21 -0800896 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700897
Ying Wang08800fd2016-03-03 20:57:21 -0800898 if [ -z "$PID" ]; then
899 printf "Expecting a PID!\n";
900 return;
901 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700902
Ying Wang08800fd2016-03-03 20:57:21 -0800903 local CORENAME=core.$PID;
904 local COREPATH=/cores/$CORENAME;
905 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700906
Ying Wang08800fd2016-03-03 20:57:21 -0800907 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700908
Ying Wang08800fd2016-03-03 20:57:21 -0800909 local done=0;
910 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
911 printf "\tSending SIG%s to %d...\n" $SIG $PID;
912 adb shell kill -$SIG $PID;
913 sleep 1;
914 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Ying Wang08800fd2016-03-03 20:57:21 -0800916 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
917 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700918}
919
Christopher Tate744ee802009-11-12 15:33:08 -0800920# systemstack - dump the current stack trace of all threads in the system process
921# to the usual ANR traces file
922function systemstack()
923{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800924 stacks system_server
925}
926
Michael Wrightaeed7212014-06-19 19:58:12 -0700927# Read the ELF header from /proc/$PID/exe to determine if the process is
928# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800929function is64bit()
930{
931 local PID="$1"
932 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800933 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800934 echo "64"
935 else
936 echo ""
937 fi
938 else
939 echo ""
940 fi
941}
942
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700943case `uname -s` in
944 Darwin)
945 function sgrep()
946 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100947 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 -0400948 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700949 }
950
951 ;;
952 *)
953 function sgrep()
954 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100955 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 -0400956 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700957 }
958 ;;
959esac
960
Raghu Gandham8da43102012-07-25 19:57:22 -0700961function gettargetarch
962{
963 get_build_var TARGET_ARCH
964}
965
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700966function ggrep()
967{
Mike Frysinger5e479732015-09-22 18:13:48 -0400968 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
969 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700970}
971
Orion Hodson831472d2019-10-25 11:35:15 +0100972function gogrep()
973{
974 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
975 -exec grep --color -n "$@" {} +
976}
977
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700978function jgrep()
979{
Mike Frysinger5e479732015-09-22 18:13:48 -0400980 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
981 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700982}
983
984function cgrep()
985{
Mike Frysinger5e479732015-09-22 18:13:48 -0400986 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' \) \
987 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700988}
989
990function resgrep()
991{
Christopher Ferris55257d22017-03-23 11:08:58 -0700992 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400993 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
994 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
995 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700996}
997
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800998function mangrep()
999{
Mike Frysinger5e479732015-09-22 18:13:48 -04001000 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1001 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001002}
1003
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001004function owngrep()
1005{
1006 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1007 -exec grep --color -n "$@" {} +
1008}
1009
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001010function sepgrep()
1011{
Mike Frysinger5e479732015-09-22 18:13:48 -04001012 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1013 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001014}
1015
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001016function rcgrep()
1017{
Mike Frysinger5e479732015-09-22 18:13:48 -04001018 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1019 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001020}
1021
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001022case `uname -s` in
1023 Darwin)
1024 function mgrep()
1025 {
Orion Hodson831472d2019-10-25 11:35:15 +01001026 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001027 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001028 }
1029
1030 function treegrep()
1031 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001032 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 -04001033 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001034 }
1035
1036 ;;
1037 *)
1038 function mgrep()
1039 {
Orion Hodson831472d2019-10-25 11:35:15 +01001040 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 '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001041 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001042 }
1043
1044 function treegrep()
1045 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001046 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 -04001047 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001048 }
1049
1050 ;;
1051esac
1052
1053function getprebuilt
1054{
1055 get_abs_build_var ANDROID_PREBUILTS
1056}
1057
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001058function tracedmdump()
1059{
Christopher Ferris55257d22017-03-23 11:08:58 -07001060 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001061 if [ ! "$T" ]; then
1062 echo "Couldn't locate the top of the tree. Try setting TOP."
1063 return
1064 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001065 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001066 local arch=$(gettargetarch)
1067 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001068
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001069 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001070 if [ ! "$TRACE" ] ; then
1071 echo "usage: tracedmdump tracename"
1072 return
1073 fi
1074
Jack Veenstra60116fc2009-04-09 18:12:34 -07001075 if [ ! -r "$KERNEL" ] ; then
1076 echo "Error: cannot find kernel: '$KERNEL'"
1077 return
1078 fi
1079
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001080 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001081 if [ "$BASETRACE" = "$TRACE" ] ; then
1082 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1083 fi
1084
1085 echo "post-processing traces..."
1086 rm -f $TRACE/qtrace.dexlist
1087 post_trace $TRACE
1088 if [ $? -ne 0 ]; then
1089 echo "***"
1090 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1091 echo "***"
1092 return
1093 fi
1094 echo "generating dexlist output..."
1095 /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
1096 echo "generating dmtrace data..."
1097 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1098 echo "generating html file..."
1099 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1100 echo "done, see $TRACE/dmtrace.html for details"
1101 echo "or run:"
1102 echo " traceview $TRACE/dmtrace"
1103}
1104
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001105# communicate with a running device or emulator, set up necessary state,
1106# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001107function runhat()
1108{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001109 # process standard adb options
1110 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001111 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001112 adbTarget=$1
1113 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001114 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001115 adbTarget="$1 $2"
1116 shift 2
1117 fi
1118 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001119 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001120
1121 # runhat options
1122 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001123
1124 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001125 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001126 return
1127 fi
1128
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001129 # confirm hat is available
1130 if [ -z $(which hat) ]; then
1131 echo "hat is not available in this configuration."
1132 return
1133 fi
1134
Andy McFaddenb6289852010-07-12 08:00:19 -07001135 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001136 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001137 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001138 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001139 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001140 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001141 echo -n "> "
1142 read
1143
The Android Open Source Project88b60792009-03-03 19:28:42 -08001144 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001145
The Android Open Source Project88b60792009-03-03 19:28:42 -08001146 echo "Retrieving file $devFile..."
1147 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001148
The Android Open Source Project88b60792009-03-03 19:28:42 -08001149 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001150
The Android Open Source Project88b60792009-03-03 19:28:42 -08001151 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001152 echo "View the output by pointing your browser at http://localhost:7000/"
1153 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001154 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001155}
1156
1157function getbugreports()
1158{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001159 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001160
1161 if [ ! "$reports" ]; then
1162 echo "Could not locate any bugreports."
1163 return
1164 fi
1165
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001166 local report
1167 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001168 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001169 echo "/sdcard/bugreports/${report}"
1170 adb pull /sdcard/bugreports/${report} ${report}
1171 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001172 done
1173}
1174
Victoria Lease1b296b42012-08-21 15:44:06 -07001175function getsdcardpath()
1176{
1177 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1178}
1179
1180function getscreenshotpath()
1181{
1182 echo "$(getsdcardpath)/Pictures/Screenshots"
1183}
1184
1185function getlastscreenshot()
1186{
1187 local screenshot_path=$(getscreenshotpath)
1188 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1189 if [ "$screenshot" = "" ]; then
1190 echo "No screenshots found."
1191 return
1192 fi
1193 echo "${screenshot}"
1194 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1195}
1196
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197function startviewserver()
1198{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001199 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 if [ $# -gt 0 ]; then
1201 port=$1
1202 fi
1203 adb shell service call window 1 i32 $port
1204}
1205
1206function stopviewserver()
1207{
1208 adb shell service call window 2
1209}
1210
1211function isviewserverstarted()
1212{
1213 adb shell service call window 3
1214}
1215
Romain Guyb84049a2010-10-04 16:56:11 -07001216function key_home()
1217{
1218 adb shell input keyevent 3
1219}
1220
1221function key_back()
1222{
1223 adb shell input keyevent 4
1224}
1225
1226function key_menu()
1227{
1228 adb shell input keyevent 82
1229}
1230
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231function smoketest()
1232{
1233 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1234 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1235 return
1236 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001237 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001238 if [ ! "$T" ]; then
1239 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1240 return
1241 fi
1242
Ying Wang9cd17642012-12-13 10:52:07 -08001243 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001244 adb uninstall com.android.smoketest > /dev/null &&
1245 adb uninstall com.android.smoketest.tests > /dev/null &&
1246 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1247 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1248 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1249}
1250
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001251# simple shortcut to the runtest command
1252function runtest()
1253{
Christopher Ferris55257d22017-03-23 11:08:58 -07001254 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001255 if [ ! "$T" ]; then
1256 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1257 return
1258 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001259 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001260}
1261
The Android Open Source Project88b60792009-03-03 19:28:42 -08001262function godir () {
1263 if [[ -z "$1" ]]; then
1264 echo "Usage: godir <regex>"
1265 return
1266 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001267 local T=$(gettop)
1268 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001269 if [ ! "$OUT_DIR" = "" ]; then
1270 mkdir -p $OUT_DIR
1271 FILELIST=$OUT_DIR/filelist
1272 else
1273 FILELIST=$T/filelist
1274 fi
1275 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001276 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001277 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001278 echo " Done"
1279 echo ""
1280 fi
1281 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001282 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001283 if [[ ${#lines[@]} = 0 ]]; then
1284 echo "Not found"
1285 return
1286 fi
1287 local pathname
1288 local choice
1289 if [[ ${#lines[@]} > 1 ]]; then
1290 while [[ -z "$pathname" ]]; do
1291 local index=1
1292 local line
1293 for line in ${lines[@]}; do
1294 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001295 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001296 done
1297 echo
1298 echo -n "Select one: "
1299 unset choice
1300 read choice
1301 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1302 echo "Invalid choice"
1303 continue
1304 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001305 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001306 done
1307 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001308 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001309 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001310 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001311}
1312
Steven Moreland62054a42018-12-06 10:11:40 -08001313# Update module-info.json in out.
1314function refreshmod() {
1315 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1316 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1317 return 1
1318 fi
1319
1320 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1321
1322 # for the output of the next command
1323 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1324
1325 # Note, can't use absolute path because of the way make works.
1326 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1327 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1328}
1329
1330# List all modules for the current device, as cached in module-info.json. If any build change is
1331# made and it should be reflected in the output, you should run 'refreshmod' first.
1332function allmod() {
1333 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1334 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1335 return 1
1336 fi
1337
1338 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1339 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1340 refreshmod || return 1
1341 fi
1342
1343 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1344}
1345
Rett Berg78d1c932019-01-24 14:34:23 -08001346# 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 -08001347# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001348function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001349 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1350 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1351 return 1
1352 fi
1353
1354 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001355 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001356 return 1
1357 fi
1358
1359 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1360 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1361 refreshmod || return 1
1362 fi
1363
1364 local relpath=$(python -c "import json, os
1365module = '$1'
1366module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1367if module not in module_info:
1368 exit(1)
1369print module_info[module]['path'][0]" 2>/dev/null)
1370
1371 if [ -z "$relpath" ]; then
1372 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1373 return 1
1374 else
Rett Berg78d1c932019-01-24 14:34:23 -08001375 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001376 fi
1377}
1378
Rett Berg78d1c932019-01-24 14:34:23 -08001379# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1380# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1381function gomod() {
1382 if [[ $# -ne 1 ]]; then
1383 echo "usage: gomod <module>" >&2
1384 return 1
1385 fi
1386
1387 local path="$(pathmod $@)"
1388 if [ -z "$path" ]; then
1389 return 1
1390 fi
1391 cd $path
1392}
1393
dimitry73b84812018-12-11 18:06:00 +01001394function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001395 local word=${COMP_WORDS[COMP_CWORD]}
1396 COMPREPLY=( $(allmod | grep -E "^$word") )
1397}
1398
Alex Rayf0d08eb2013-03-08 15:15:06 -08001399# Print colored exit condition
1400function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001401 "$@"
1402 local retval=$?
1403 if [ $retval -ne 0 ]
1404 then
Jacky Cao89483b82015-05-15 22:12:53 +08001405 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001406 else
Jacky Cao89483b82015-05-15 22:12:53 +08001407 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001408 fi
1409 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001410}
1411
Ying Wanged21d4c2014-08-24 22:14:19 -07001412function get_make_command()
1413{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001414 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1415 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001416 # Always use the real make if -C is passed in
1417 for arg in "$@"; do
1418 if [[ $arg == -C* ]]; then
1419 echo command make
1420 return
1421 fi
1422 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001423 echo build/soong/soong_ui.bash --make-mode
1424 else
1425 echo command make
1426 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001427}
1428
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001429function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001430{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001431 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1432 "$@"
1433 return $?
1434 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001435 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001436 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001437 local ret=$?
1438 local end_time=$(date +"%s")
1439 local tdiff=$(($end_time-$start_time))
1440 local hours=$(($tdiff / 3600 ))
1441 local mins=$((($tdiff % 3600) / 60))
1442 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001443 local ncolors=$(tput colors 2>/dev/null)
1444 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001445 color_failed=$'\E'"[0;31m"
1446 color_success=$'\E'"[0;32m"
1447 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001448 else
1449 color_failed=""
1450 color_success=""
1451 color_reset=""
1452 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001453 echo
1454 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001455 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001456 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001457 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001458 fi
1459 if [ $hours -gt 0 ] ; then
1460 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1461 elif [ $mins -gt 0 ] ; then
1462 printf "(%02g:%02g (mm:ss))" $mins $secs
1463 elif [ $secs -gt 0 ] ; then
1464 printf "(%s seconds)" $secs
1465 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001466 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001467 echo
1468 return $ret
1469}
1470
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001471function _trigger_build()
1472(
1473 local -r bc="$1"; shift
1474 if T="$(gettop)"; then
1475 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1476 else
1477 echo "Couldn't locate the top of the tree. Try setting TOP."
1478 fi
1479)
1480
1481function m()
1482(
1483 _trigger_build "all-modules" "$@"
1484)
1485
1486function mm()
1487(
1488 _trigger_build "modules-in-a-dir-no-deps" "$@"
1489)
1490
1491function mmm()
1492(
1493 _trigger_build "modules-in-dirs-no-deps" "$@"
1494)
1495
1496function mma()
1497(
1498 _trigger_build "modules-in-a-dir" "$@"
1499)
1500
1501function mmma()
1502(
1503 _trigger_build "modules-in-dirs" "$@"
1504)
1505
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001506function make()
1507{
Dan Willemsene9842242017-07-28 13:00:13 -07001508 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001509}
1510
David Zeuthen1b126ff2015-09-30 17:10:48 -04001511function provision()
1512{
1513 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1514 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1515 return 1
1516 fi
1517 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1518 echo "There is no provisioning script for the device." >&2
1519 return 1
1520 fi
1521
1522 # Check if user really wants to do this.
1523 if [ "$1" = "--no-confirmation" ]; then
1524 shift 1
1525 else
1526 echo "This action will reflash your device."
1527 echo ""
1528 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1529 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001530 echo -n "Are you sure you want to do this (yes/no)? "
1531 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001532 if [[ "${REPLY}" != "yes" ]] ; then
1533 echo "Not taking any action. Exiting." >&2
1534 return 1
1535 fi
1536 fi
1537 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1538}
1539
Jim Tanga881a252018-06-19 16:34:41 +08001540# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001541function enable_zsh_completion() {
1542 # Don't override user's options if bash-style completion is already enabled.
1543 if ! declare -f complete >/dev/null; then
1544 autoload -U compinit && compinit
1545 autoload -U bashcompinit && bashcompinit
1546 fi
Jim Tanga881a252018-06-19 16:34:41 +08001547}
1548
1549function validate_current_shell() {
1550 local current_sh="$(ps -o command -p $$)"
1551 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001552 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001553 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001554 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001555 *zsh*)
1556 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001557 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001558 *)
Jim Tanga881a252018-06-19 16:34:41 +08001559 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 -07001560 ;;
1561 esac
Jim Tanga881a252018-06-19 16:34:41 +08001562}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001563
1564# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001565# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1566# load those.
1567#
1568# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001569function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001570 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001571 allowed=
1572 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1573 if [ -n "$allowed" ]; then
1574 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1575 echo " $allowed"
1576 echo " $f"
1577 return
1578 fi
1579 allowed="$f"
1580 done
1581
1582 allowed_files=
1583 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001584 for dir in device vendor product; do
1585 for f in $(test -d $dir && \
1586 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001587
1588 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1589 echo "including $f"; . "$f"
1590 else
1591 echo "ignoring $f, not in $allowed"
1592 fi
Jim Tanga881a252018-06-19 16:34:41 +08001593 done
1594 done
1595}
Kenny Root52aa81c2011-07-15 11:07:06 -07001596
Jim Tanga881a252018-06-19 16:34:41 +08001597validate_current_shell
1598source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001599addcompletions