blob: 3d5d361c944c45946afdaabf4d1f816e47c45790 [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.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060026- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080027- sepgrep: Greps on all local sepolicy files.
28- sgrep: Greps on all local source files.
29- godir: Go to the directory containing a file.
30- allmod: List all modules.
31- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080032- pathmod: Get the directory containing a module.
Steven Moreland62054a42018-12-06 10:11:40 -080033- refreshmod: Refresh list of modules for allmod/gomod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070034
Roland Levillain39341922015-10-20 12:48:19 +010035Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070036- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080037- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070038
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070039Look at the source to view more functions. The complete list is:
40EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070041 local T=$(gettop)
42 local A=""
43 local i
Jacky Cao89483b82015-05-15 22:12:53 +080044 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 -070045 A="$A $i"
46 done
47 echo $A
48}
49
Ying Wang08800fd2016-03-03 20:57:21 -080050# Get all the build variables needed by this script in a single call to the build system.
51function build_build_var_cache()
52{
Christopher Ferris55257d22017-03-23 11:08:58 -070053 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080054 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080055 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' ' '`)
56 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 -080057 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080058 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080059 --vars="${cached_vars[*]}" \
60 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070061 --var-prefix=var_cache_ \
62 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080063 local ret=$?
64 if [ $ret -ne 0 ]
65 then
66 unset build_dicts_script
67 return $ret
68 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070069 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080070 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080071 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080072 unset build_dicts_script
Ying Wang08800fd2016-03-03 20:57:21 -080073 if [ $ret -ne 0 ]
74 then
75 return $ret
76 fi
77 BUILD_VAR_CACHE_READY="true"
78}
79
Ying Wangf0cb3972016-03-04 13:56:23 -080080# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080081# to get build variables not listed in this script.
82function destroy_build_var_cache()
83{
84 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070085 local v
Ying Wang08800fd2016-03-03 20:57:21 -080086 for v in $cached_vars; do
87 unset var_cache_$v
88 done
89 unset cached_vars
90 for v in $cached_abs_vars; do
91 unset abs_var_cache_$v
92 done
93 unset cached_abs_vars
94}
95
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070096# Get the value of a build variable as an absolute path.
97function get_abs_build_var()
98{
Ying Wang08800fd2016-03-03 20:57:21 -080099 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
100 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800101 eval "echo \"\${abs_var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800102 return
103 fi
104
Christopher Ferris55257d22017-03-23 11:08:58 -0700105 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700106 if [ ! "$T" ]; then
107 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
108 return
109 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700110 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700111}
112
113# Get the exact value of a build variable.
114function get_build_var()
115{
Ying Wang08800fd2016-03-03 20:57:21 -0800116 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
117 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800118 eval "echo \"\${var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800119 return
120 fi
121
Christopher Ferris55257d22017-03-23 11:08:58 -0700122 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700123 if [ ! "$T" ]; then
124 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
125 return
126 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700127 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800128}
129
130# check to see if the supplied product is one we can build
131function check_product()
132{
Christopher Ferris55257d22017-03-23 11:08:58 -0700133 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800134 if [ ! "$T" ]; then
135 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
136 return
137 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700138 TARGET_PRODUCT=$1 \
139 TARGET_BUILD_VARIANT= \
140 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700141 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142 get_build_var TARGET_DEVICE > /dev/null
143 # hide successful answers, but allow the errors to show
144}
145
146VARIANT_CHOICES=(user userdebug eng)
147
148# check to see if the supplied variant is valid
149function check_variant()
150{
Christopher Ferris55257d22017-03-23 11:08:58 -0700151 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800152 for v in ${VARIANT_CHOICES[@]}
153 do
154 if [ "$v" = "$1" ]
155 then
156 return 0
157 fi
158 done
159 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700160}
161
162function setpaths()
163{
Christopher Ferris55257d22017-03-23 11:08:58 -0700164 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700165 if [ ! "$T" ]; then
166 echo "Couldn't locate the top of the tree. Try setting TOP."
167 return
168 fi
169
170 ##################################################################
171 # #
172 # Read me before you modify this code #
173 # #
174 # This function sets ANDROID_BUILD_PATHS to what it is adding #
175 # to PATH, and the next time it is run, it removes that from #
176 # PATH. This is required so lunch can be run more than once #
177 # and still have working paths. #
178 # #
179 ##################################################################
180
Raphael Mollc639c782011-06-20 17:25:01 -0700181 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
182 # due to "C:\Program Files" being in the path.
183
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700184 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700185 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700186 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
187 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700188 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700189 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800190 # strip leading ':', if any
191 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500192 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700193
194 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700195 local prebuiltdir=$(getprebuilt)
196 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700197
Ben Cheng8bc4c432012-11-16 13:29:13 -0800198 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700199 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
200 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800201 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800202
Raphael Mollc639c782011-06-20 17:25:01 -0700203 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800204 export ANDROID_TOOLCHAIN=
205 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200206 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200208 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400209 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700210 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400211 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
212 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800213 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200214 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800215 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700216 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700217 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700218 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000219 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 *)
221 echo "Can't find toolchain for unknown architecture: $ARCH"
222 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700223 ;;
224 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700225 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800226 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700227 fi
Raphael732936d2011-06-22 14:35:32 -0700228
Christopher Ferris55257d22017-03-23 11:08:58 -0700229 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800230 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
231 fi
232
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700233 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700234
235 # add kernel specific binaries
236 case $(uname -s) in
237 Linux)
238 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
239 ;;
240 *)
241 ;;
242 esac
243
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400244 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
245 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
246 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
247 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700248 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
249
250 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
251 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
252 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200253
254 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
255 # to ensure that the corresponding 'emulator' binaries are used.
256 case $(uname -s) in
257 Darwin)
258 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
259 ;;
260 Linux)
261 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
262 ;;
263 *)
264 ANDROID_EMULATOR_PREBUILTS=
265 ;;
266 esac
267 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700268 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200269 export ANDROID_EMULATOR_PREBUILTS
270 fi
271
Jim Tangb3fda302018-12-22 10:24:55 +0800272 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
273 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700274 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
275 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
276 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
277 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800278
Ryan Prichard8426a192019-07-15 18:05:29 -0700279 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800280
281 # out with the duplicate old
282 if [ -n $ANDROID_PYTHONPATH ]; then
283 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
284 fi
285 # and in with the new
286 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800287 if [ -n $VENDOR_PYTHONPATH ]; then
288 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
289 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800290 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800291
Colin Crosse97e6932017-06-30 16:01:45 -0700292 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
293 export JAVA_HOME=$ANDROID_JAVA_HOME
294 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
295 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
296 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500297
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800298 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700299 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
300 export OUT=$ANDROID_PRODUCT_OUT
301
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700302 unset ANDROID_HOST_OUT
303 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
304
Simran Basidd050ed2017-02-13 13:46:48 -0800305 unset ANDROID_HOST_OUT_TESTCASES
306 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
307
308 unset ANDROID_TARGET_OUT_TESTCASES
309 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
310
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800311 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700312 # TODO: fix the path
313 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
314}
315
316function printconfig()
317{
Christopher Ferris55257d22017-03-23 11:08:58 -0700318 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800319 if [ ! "$T" ]; then
320 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
321 return
322 fi
323 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700324}
325
326function set_stuff_for_environment()
327{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800328 setpaths
329 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700330
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800331 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700332 # With this environment variable new GCC can apply colors to warnings/errors
333 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 -0700334}
335
336function set_sequence_number()
337{
Colin Cross88737132017-03-21 17:41:03 -0700338 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700339}
340
Makoto Onukida971062018-06-18 10:15:19 -0700341# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
342function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800343 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700344 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800345 *:"$cmd":*)
346 return 1
347 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700348 esac
349 return 0
350}
351
Kenny Root52aa81c2011-07-15 11:07:06 -0700352function addcompletions()
353{
354 local T dir f
355
Jim Tanga881a252018-06-19 16:34:41 +0800356 # Keep us from trying to run in something that's neither bash nor zsh.
357 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700358 return
359 fi
360
361 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800362 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700363 return
364 fi
365
Jim Tanga881a252018-06-19 16:34:41 +0800366 local completion_files=(
367 system/core/adb/adb.bash
368 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800369 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800370 )
Makoto Onukida971062018-06-18 10:15:19 -0700371 # Completion can be disabled selectively to allow users to use non-standard completion.
372 # e.g.
373 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
374 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800375 for f in ${completion_files[*]}; do
376 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700377 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700378 fi
379 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700380
Makoto Onukida971062018-06-18 10:15:19 -0700381 if should_add_completion bit ; then
382 complete -C "bit --tab" bit
383 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000384 if [ -z "$ZSH_VERSION" ]; then
385 # Doesn't work in zsh.
386 complete -o nospace -F _croot croot
387 fi
Jim Tanga881a252018-06-19 16:34:41 +0800388 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800389
dimitry73b84812018-12-11 18:06:00 +0100390 complete -F _complete_android_module_names gomod
391 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700392}
393
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700394function choosetype()
395{
396 echo "Build type choices are:"
397 echo " 1. release"
398 echo " 2. debug"
399 echo
400
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800401 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700402 DEFAULT_NUM=1
403 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700404
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405 export TARGET_BUILD_TYPE=
406 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700407 while [ -z $TARGET_BUILD_TYPE ]
408 do
409 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800410 if [ -z "$1" ] ; then
411 read ANSWER
412 else
413 echo $1
414 ANSWER=$1
415 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700416 case $ANSWER in
417 "")
418 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
419 ;;
420 1)
421 export TARGET_BUILD_TYPE=release
422 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800423 release)
424 export TARGET_BUILD_TYPE=release
425 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700426 2)
427 export TARGET_BUILD_TYPE=debug
428 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800429 debug)
430 export TARGET_BUILD_TYPE=debug
431 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 *)
433 echo
434 echo "I didn't understand your response. Please try again."
435 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436 ;;
437 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800438 if [ -n "$1" ] ; then
439 break
440 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700441 done
442
Ying Wang08800fd2016-03-03 20:57:21 -0800443 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700444 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800445 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700446}
447
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800448#
449# This function isn't really right: It chooses a TARGET_PRODUCT
450# based on the list of boards. Usually, that gets you something
451# that kinda works with a generic product, but really, you should
452# pick a product by name.
453#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700454function chooseproduct()
455{
Christopher Ferris55257d22017-03-23 11:08:58 -0700456 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700457 if [ "x$TARGET_PRODUCT" != x ] ; then
458 default_value=$TARGET_PRODUCT
459 else
Ying Wang0a76df52015-06-08 11:57:26 -0700460 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700461 fi
462
Ying Wang08800fd2016-03-03 20:57:21 -0800463 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800464 export TARGET_PRODUCT=
465 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466 while [ -z "$TARGET_PRODUCT" ]
467 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700468 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469 if [ -z "$1" ] ; then
470 read ANSWER
471 else
472 echo $1
473 ANSWER=$1
474 fi
475
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700476 if [ -z "$ANSWER" ] ; then
477 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800478 else
479 if check_product $ANSWER
480 then
481 export TARGET_PRODUCT=$ANSWER
482 else
483 echo "** Not a valid product: $ANSWER"
484 fi
485 fi
486 if [ -n "$1" ] ; then
487 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700488 fi
489 done
490
Ying Wang08800fd2016-03-03 20:57:21 -0800491 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800493 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494}
495
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800496function choosevariant()
497{
498 echo "Variant choices are:"
499 local index=1
500 local v
501 for v in ${VARIANT_CHOICES[@]}
502 do
503 # The product name is the name of the directory containing
504 # the makefile we found, above.
505 echo " $index. $v"
506 index=$(($index+1))
507 done
508
509 local default_value=eng
510 local ANSWER
511
512 export TARGET_BUILD_VARIANT=
513 while [ -z "$TARGET_BUILD_VARIANT" ]
514 do
515 echo -n "Which would you like? [$default_value] "
516 if [ -z "$1" ] ; then
517 read ANSWER
518 else
519 echo $1
520 ANSWER=$1
521 fi
522
523 if [ -z "$ANSWER" ] ; then
524 export TARGET_BUILD_VARIANT=$default_value
525 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
526 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200527 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528 fi
529 else
530 if check_variant $ANSWER
531 then
532 export TARGET_BUILD_VARIANT=$ANSWER
533 else
534 echo "** Not a valid variant: $ANSWER"
535 fi
536 fi
537 if [ -n "$1" ] ; then
538 break
539 fi
540 done
541}
542
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700543function choosecombo()
544{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700545 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546
547 echo
548 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700549 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700550
551 echo
552 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700553 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800554
555 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800556 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700557 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800559 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560}
561
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562function add_lunch_combo()
563{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800564 if [ -n "$ZSH_VERSION" ]; then
565 echo -n "${funcfiletrace[1]}: "
566 else
567 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
568 fi
569 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 -0800570}
571
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700572function print_lunch_menu()
573{
574 local uname=$(uname)
Colin Crosscb8337d2019-09-23 12:52:32 -0700575 local choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576 echo
577 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578 echo
579 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800580
581 local i=1
582 local choice
Colin Crosscb8337d2019-09-23 12:52:32 -0700583 for choice in $choices
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 do
585 echo " $i. $choice"
586 i=$(($i+1))
587 done
588
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589 echo
590}
591
592function lunch()
593{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800594 local answer
595
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 else
599 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700600 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800601 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602 fi
603
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800604 local selection=
605
606 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700607 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700608 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800609 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
610 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800611 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700612 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800613 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800614 # array in zsh starts from 1 instead of 0.
615 if [ -n "$ZSH_VERSION" ]
616 then
617 selection=${choices[$(($answer))]}
618 else
619 selection=${choices[$(($answer-1))]}
620 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 fi
Colin Cross88737132017-03-21 17:41:03 -0700622 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800623 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700624 fi
625
Joe Onoratoda12daf2010-06-09 18:18:31 -0700626 export TARGET_BUILD_APPS=
627
Colin Cross88737132017-03-21 17:41:03 -0700628 local product variant_and_version variant version
629
630 product=${selection%%-*} # Trim everything after first dash
631 variant_and_version=${selection#*-} # Trim everything up to first dash
632 if [ "$variant_and_version" != "$selection" ]; then
633 variant=${variant_and_version%%-*}
634 if [ "$variant" != "$variant_and_version" ]; then
635 version=${variant_and_version#*-}
636 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700637 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800638
Colin Cross88737132017-03-21 17:41:03 -0700639 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800640 then
641 echo
Colin Cross88737132017-03-21 17:41:03 -0700642 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700643 return 1
644 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800645
Colin Cross88737132017-03-21 17:41:03 -0700646 TARGET_PRODUCT=$product \
647 TARGET_BUILD_VARIANT=$variant \
648 TARGET_PLATFORM_VERSION=$version \
649 build_build_var_cache
650 if [ $? -ne 0 ]
651 then
652 return 1
653 fi
654
655 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
656 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700657 if [ -n "$version" ]; then
658 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
659 else
660 unset TARGET_PLATFORM_VERSION
661 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700662 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700663
664 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800665
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700666 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800667 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800668 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700669}
670
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700671unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700672# Tab completion for lunch.
673function _lunch()
674{
675 local cur prev opts
676 COMPREPLY=()
677 cur="${COMP_WORDS[COMP_CWORD]}"
678 prev="${COMP_WORDS[COMP_CWORD-1]}"
679
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700680 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800681 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700682 fi
683
684 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700685 return 0
686}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700687
Joe Onoratoda12daf2010-06-09 18:18:31 -0700688# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700689# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700690function tapas()
691{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700692 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800693 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700694 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700695 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 -0800696 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 -0700697
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700698 if [ "$showHelp" != "" ]; then
699 $(gettop)/build/make/tapasHelp.sh
700 return
701 fi
702
Ying Wang67f02922012-08-22 10:25:20 -0700703 if [ $(echo $arch | wc -w) -gt 1 ]; then
704 echo "tapas: Error: Multiple build archs supplied: $arch"
705 return
706 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700707 if [ $(echo $variant | wc -w) -gt 1 ]; then
708 echo "tapas: Error: Multiple build variants supplied: $variant"
709 return
710 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700711 if [ $(echo $density | wc -w) -gt 1 ]; then
712 echo "tapas: Error: Multiple densities supplied: $density"
713 return
714 fi
Ying Wang67f02922012-08-22 10:25:20 -0700715
Ying Wang0a76df52015-06-08 11:57:26 -0700716 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700717 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700718 x86) product=aosp_x86;;
719 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700720 arm64) product=aosp_arm64;;
721 x86_64) product=aosp_x86_64;;
722 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700723 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700724 if [ -z "$variant" ]; then
725 variant=eng
726 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700727 if [ -z "$apps" ]; then
728 apps=all
729 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600730 if [ -z "$density" ]; then
731 density=alldpi
732 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700733
Ying Wang67f02922012-08-22 10:25:20 -0700734 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700735 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700736 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700737 export TARGET_BUILD_TYPE=release
738 export TARGET_BUILD_APPS=$apps
739
Ying Wang08800fd2016-03-03 20:57:21 -0800740 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700741 set_stuff_for_environment
742 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800743 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700744}
745
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700746function gettop
747{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700748 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700749 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700750 # The following circumlocution ensures we remove symlinks from TOP.
751 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700752 else
753 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800754 # The following circumlocution (repeated below as well) ensures
755 # that we record the true directory name and not one that is
756 # faked up with symlink names.
757 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700758 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800759 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700760 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800762 \cd ..
synergyb112a402013-07-05 19:47:47 -0700763 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 done
Ying Wang9cd17642012-12-13 10:52:07 -0800765 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700766 if [ -f "$T/$TOPFILE" ]; then
767 echo $T
768 fi
769 fi
770 fi
771}
772
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700773function croot()
774{
Christopher Ferris55257d22017-03-23 11:08:58 -0700775 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700777 if [ "$1" ]; then
778 \cd $(gettop)/$1
779 else
780 \cd $(gettop)
781 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700782 else
783 echo "Couldn't locate the top of the tree. Try setting TOP."
784 fi
785}
786
Anton Hanssonece9c482019-02-04 18:15:39 +0000787function _croot()
788{
789 local T=$(gettop)
790 if [ "$T" ]; then
791 local cur="${COMP_WORDS[COMP_CWORD]}"
792 k=0
793 for c in $(compgen -d ${T}/${cur}); do
794 COMPREPLY[k++]=${c#${T}/}/
795 done
796 fi
797}
798
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700799function cproj()
800{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700801 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700802 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700803 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700804 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
805 T=$PWD
806 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800807 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700808 return
809 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800810 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700811 done
Ying Wang9cd17642012-12-13 10:52:07 -0800812 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700813 echo "can't find Android.mk"
814}
815
Daniel Sandler47e0a882013-07-30 13:23:52 -0400816# simplified version of ps; output in the form
817# <pid> <procname>
818function qpid() {
819 local prepend=''
820 local append=''
821 if [ "$1" = "--exact" ]; then
822 prepend=' '
823 append='$'
824 shift
825 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800826 echo "usage: qpid [[--exact] <process name|pid>"
827 return 255
828 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400829
830 local EXE="$1"
831 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800832 qpid | \grep "$prepend$EXE$append"
833 else
834 adb shell ps \
835 | tr -d '\r' \
836 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
837 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400838}
839
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800840# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700841# that has the core-file-size limit set correctly
842#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800843# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700844# if its core-file-size limit is not set already.
845# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
846
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800847function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700848{
Ying Wang08800fd2016-03-03 20:57:21 -0800849 echo "Getting root...";
850 adb root;
851 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700852
Ying Wang08800fd2016-03-03 20:57:21 -0800853 echo "Remounting root partition read-write...";
854 adb shell mount -w -o remount -t rootfs rootfs;
855 sleep 1;
856 adb wait-for-device;
857 adb shell mkdir -p /cores;
858 adb shell mount -t tmpfs tmpfs /cores;
859 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700860
Ying Wang08800fd2016-03-03 20:57:21 -0800861 echo "Granting SELinux permission to dump in /cores...";
862 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700863
Ying Wang08800fd2016-03-03 20:57:21 -0800864 echo "Set core pattern.";
865 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866
Ying Wang08800fd2016-03-03 20:57:21 -0800867 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700868}
869
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800870# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700871# $1 = PID of process (e.g., $(pid mediaserver))
872#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800873# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700874# dump to actually be generated.
875
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800876function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700877{
Ying Wang08800fd2016-03-03 20:57:21 -0800878 local PID=$1;
879 if [ -z "$PID" ]; then
880 printf "Expecting a PID!\n";
881 return;
882 fi;
883 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800884 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700885}
886
887# core - send SIGV and pull the core for process
888# $1 = PID of process (e.g., $(pid mediaserver))
889#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800890# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700891# enabled globally.
892
893function core()
894{
Ying Wang08800fd2016-03-03 20:57:21 -0800895 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700896
Ying Wang08800fd2016-03-03 20:57:21 -0800897 if [ -z "$PID" ]; then
898 printf "Expecting a PID!\n";
899 return;
900 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700901
Ying Wang08800fd2016-03-03 20:57:21 -0800902 local CORENAME=core.$PID;
903 local COREPATH=/cores/$CORENAME;
904 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700905
Ying Wang08800fd2016-03-03 20:57:21 -0800906 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700907
Ying Wang08800fd2016-03-03 20:57:21 -0800908 local done=0;
909 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
910 printf "\tSending SIG%s to %d...\n" $SIG $PID;
911 adb shell kill -$SIG $PID;
912 sleep 1;
913 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700914
Ying Wang08800fd2016-03-03 20:57:21 -0800915 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
916 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700917}
918
Christopher Tate744ee802009-11-12 15:33:08 -0800919# systemstack - dump the current stack trace of all threads in the system process
920# to the usual ANR traces file
921function systemstack()
922{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800923 stacks system_server
924}
925
Michael Wrightaeed7212014-06-19 19:58:12 -0700926# Read the ELF header from /proc/$PID/exe to determine if the process is
927# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800928function is64bit()
929{
930 local PID="$1"
931 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800932 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800933 echo "64"
934 else
935 echo ""
936 fi
937 else
938 echo ""
939 fi
940}
941
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700942case `uname -s` in
943 Darwin)
944 function sgrep()
945 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100946 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 -0400947 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700948 }
949
950 ;;
951 *)
952 function sgrep()
953 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100954 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 -0400955 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700956 }
957 ;;
958esac
959
Raghu Gandham8da43102012-07-25 19:57:22 -0700960function gettargetarch
961{
962 get_build_var TARGET_ARCH
963}
964
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700965function ggrep()
966{
Mike Frysinger5e479732015-09-22 18:13:48 -0400967 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
968 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700969}
970
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700971function jgrep()
972{
Mike Frysinger5e479732015-09-22 18:13:48 -0400973 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
974 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700975}
976
977function cgrep()
978{
Mike Frysinger5e479732015-09-22 18:13:48 -0400979 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' \) \
980 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700981}
982
983function resgrep()
984{
Christopher Ferris55257d22017-03-23 11:08:58 -0700985 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400986 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
987 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
988 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700989}
990
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800991function mangrep()
992{
Mike Frysinger5e479732015-09-22 18:13:48 -0400993 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
994 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800995}
996
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -0600997function owngrep()
998{
999 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1000 -exec grep --color -n "$@" {} +
1001}
1002
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001003function sepgrep()
1004{
Mike Frysinger5e479732015-09-22 18:13:48 -04001005 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1006 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001007}
1008
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001009function rcgrep()
1010{
Mike Frysinger5e479732015-09-22 18:13:48 -04001011 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1012 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001013}
1014
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001015case `uname -s` in
1016 Darwin)
1017 function mgrep()
1018 {
David Grossd1d6fc52017-05-10 10:49:44 -07001019 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 -04001020 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001021 }
1022
1023 function treegrep()
1024 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001025 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 -04001026 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001027 }
1028
1029 ;;
1030 *)
1031 function mgrep()
1032 {
David Grossd1d6fc52017-05-10 10:49:44 -07001033 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 -04001034 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001035 }
1036
1037 function treegrep()
1038 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001039 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 -04001040 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001041 }
1042
1043 ;;
1044esac
1045
1046function getprebuilt
1047{
1048 get_abs_build_var ANDROID_PREBUILTS
1049}
1050
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001051function tracedmdump()
1052{
Christopher Ferris55257d22017-03-23 11:08:58 -07001053 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001054 if [ ! "$T" ]; then
1055 echo "Couldn't locate the top of the tree. Try setting TOP."
1056 return
1057 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001058 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001059 local arch=$(gettargetarch)
1060 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001061
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001062 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001063 if [ ! "$TRACE" ] ; then
1064 echo "usage: tracedmdump tracename"
1065 return
1066 fi
1067
Jack Veenstra60116fc2009-04-09 18:12:34 -07001068 if [ ! -r "$KERNEL" ] ; then
1069 echo "Error: cannot find kernel: '$KERNEL'"
1070 return
1071 fi
1072
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001073 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001074 if [ "$BASETRACE" = "$TRACE" ] ; then
1075 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1076 fi
1077
1078 echo "post-processing traces..."
1079 rm -f $TRACE/qtrace.dexlist
1080 post_trace $TRACE
1081 if [ $? -ne 0 ]; then
1082 echo "***"
1083 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1084 echo "***"
1085 return
1086 fi
1087 echo "generating dexlist output..."
1088 /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
1089 echo "generating dmtrace data..."
1090 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1091 echo "generating html file..."
1092 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1093 echo "done, see $TRACE/dmtrace.html for details"
1094 echo "or run:"
1095 echo " traceview $TRACE/dmtrace"
1096}
1097
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001098# communicate with a running device or emulator, set up necessary state,
1099# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001100function runhat()
1101{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001102 # process standard adb options
1103 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001104 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001105 adbTarget=$1
1106 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001107 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001108 adbTarget="$1 $2"
1109 shift 2
1110 fi
1111 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001112 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001113
1114 # runhat options
1115 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001116
1117 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001118 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119 return
1120 fi
1121
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001122 # confirm hat is available
1123 if [ -z $(which hat) ]; then
1124 echo "hat is not available in this configuration."
1125 return
1126 fi
1127
Andy McFaddenb6289852010-07-12 08:00:19 -07001128 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001129 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001130 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001131 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001132 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001133 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134 echo -n "> "
1135 read
1136
The Android Open Source Project88b60792009-03-03 19:28:42 -08001137 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001138
The Android Open Source Project88b60792009-03-03 19:28:42 -08001139 echo "Retrieving file $devFile..."
1140 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001141
The Android Open Source Project88b60792009-03-03 19:28:42 -08001142 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001143
The Android Open Source Project88b60792009-03-03 19:28:42 -08001144 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001145 echo "View the output by pointing your browser at http://localhost:7000/"
1146 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001147 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001148}
1149
1150function getbugreports()
1151{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001152 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153
1154 if [ ! "$reports" ]; then
1155 echo "Could not locate any bugreports."
1156 return
1157 fi
1158
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001159 local report
1160 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001161 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001162 echo "/sdcard/bugreports/${report}"
1163 adb pull /sdcard/bugreports/${report} ${report}
1164 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001165 done
1166}
1167
Victoria Lease1b296b42012-08-21 15:44:06 -07001168function getsdcardpath()
1169{
1170 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1171}
1172
1173function getscreenshotpath()
1174{
1175 echo "$(getsdcardpath)/Pictures/Screenshots"
1176}
1177
1178function getlastscreenshot()
1179{
1180 local screenshot_path=$(getscreenshotpath)
1181 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1182 if [ "$screenshot" = "" ]; then
1183 echo "No screenshots found."
1184 return
1185 fi
1186 echo "${screenshot}"
1187 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1188}
1189
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190function startviewserver()
1191{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001192 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193 if [ $# -gt 0 ]; then
1194 port=$1
1195 fi
1196 adb shell service call window 1 i32 $port
1197}
1198
1199function stopviewserver()
1200{
1201 adb shell service call window 2
1202}
1203
1204function isviewserverstarted()
1205{
1206 adb shell service call window 3
1207}
1208
Romain Guyb84049a2010-10-04 16:56:11 -07001209function key_home()
1210{
1211 adb shell input keyevent 3
1212}
1213
1214function key_back()
1215{
1216 adb shell input keyevent 4
1217}
1218
1219function key_menu()
1220{
1221 adb shell input keyevent 82
1222}
1223
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001224function smoketest()
1225{
1226 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1227 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1228 return
1229 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001230 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231 if [ ! "$T" ]; then
1232 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1233 return
1234 fi
1235
Ying Wang9cd17642012-12-13 10:52:07 -08001236 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001237 adb uninstall com.android.smoketest > /dev/null &&
1238 adb uninstall com.android.smoketest.tests > /dev/null &&
1239 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1240 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1241 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1242}
1243
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001244# simple shortcut to the runtest command
1245function runtest()
1246{
Christopher Ferris55257d22017-03-23 11:08:58 -07001247 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001248 if [ ! "$T" ]; then
1249 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1250 return
1251 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001252 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001253}
1254
The Android Open Source Project88b60792009-03-03 19:28:42 -08001255function godir () {
1256 if [[ -z "$1" ]]; then
1257 echo "Usage: godir <regex>"
1258 return
1259 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001260 local T=$(gettop)
1261 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001262 if [ ! "$OUT_DIR" = "" ]; then
1263 mkdir -p $OUT_DIR
1264 FILELIST=$OUT_DIR/filelist
1265 else
1266 FILELIST=$T/filelist
1267 fi
1268 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001269 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001270 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001271 echo " Done"
1272 echo ""
1273 fi
1274 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001275 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001276 if [[ ${#lines[@]} = 0 ]]; then
1277 echo "Not found"
1278 return
1279 fi
1280 local pathname
1281 local choice
1282 if [[ ${#lines[@]} > 1 ]]; then
1283 while [[ -z "$pathname" ]]; do
1284 local index=1
1285 local line
1286 for line in ${lines[@]}; do
1287 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001288 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001289 done
1290 echo
1291 echo -n "Select one: "
1292 unset choice
1293 read choice
1294 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1295 echo "Invalid choice"
1296 continue
1297 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001298 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001299 done
1300 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001301 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001302 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001303 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001304}
1305
Steven Moreland62054a42018-12-06 10:11:40 -08001306# Update module-info.json in out.
1307function refreshmod() {
1308 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1309 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1310 return 1
1311 fi
1312
1313 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1314
1315 # for the output of the next command
1316 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1317
1318 # Note, can't use absolute path because of the way make works.
1319 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1320 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1321}
1322
1323# List all modules for the current device, as cached in module-info.json. If any build change is
1324# made and it should be reflected in the output, you should run 'refreshmod' first.
1325function allmod() {
1326 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1327 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1328 return 1
1329 fi
1330
1331 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1332 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1333 refreshmod || return 1
1334 fi
1335
1336 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1337}
1338
Rett Berg78d1c932019-01-24 14:34:23 -08001339# 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 -08001340# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001341function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001342 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1343 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1344 return 1
1345 fi
1346
1347 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001348 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001349 return 1
1350 fi
1351
1352 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1353 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1354 refreshmod || return 1
1355 fi
1356
1357 local relpath=$(python -c "import json, os
1358module = '$1'
1359module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1360if module not in module_info:
1361 exit(1)
1362print module_info[module]['path'][0]" 2>/dev/null)
1363
1364 if [ -z "$relpath" ]; then
1365 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1366 return 1
1367 else
Rett Berg78d1c932019-01-24 14:34:23 -08001368 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001369 fi
1370}
1371
Rett Berg78d1c932019-01-24 14:34:23 -08001372# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1373# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1374function gomod() {
1375 if [[ $# -ne 1 ]]; then
1376 echo "usage: gomod <module>" >&2
1377 return 1
1378 fi
1379
1380 local path="$(pathmod $@)"
1381 if [ -z "$path" ]; then
1382 return 1
1383 fi
1384 cd $path
1385}
1386
dimitry73b84812018-12-11 18:06:00 +01001387function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001388 local word=${COMP_WORDS[COMP_CWORD]}
1389 COMPREPLY=( $(allmod | grep -E "^$word") )
1390}
1391
Alex Rayf0d08eb2013-03-08 15:15:06 -08001392# Print colored exit condition
1393function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001394 "$@"
1395 local retval=$?
1396 if [ $retval -ne 0 ]
1397 then
Jacky Cao89483b82015-05-15 22:12:53 +08001398 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001399 else
Jacky Cao89483b82015-05-15 22:12:53 +08001400 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001401 fi
1402 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001403}
1404
Ying Wanged21d4c2014-08-24 22:14:19 -07001405function get_make_command()
1406{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001407 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1408 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001409 # Always use the real make if -C is passed in
1410 for arg in "$@"; do
1411 if [[ $arg == -C* ]]; then
1412 echo command make
1413 return
1414 fi
1415 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001416 echo build/soong/soong_ui.bash --make-mode
1417 else
1418 echo command make
1419 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001420}
1421
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001422function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001423{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001424 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1425 "$@"
1426 return $?
1427 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001428 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001429 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001430 local ret=$?
1431 local end_time=$(date +"%s")
1432 local tdiff=$(($end_time-$start_time))
1433 local hours=$(($tdiff / 3600 ))
1434 local mins=$((($tdiff % 3600) / 60))
1435 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001436 local ncolors=$(tput colors 2>/dev/null)
1437 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001438 color_failed=$'\E'"[0;31m"
1439 color_success=$'\E'"[0;32m"
1440 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001441 else
1442 color_failed=""
1443 color_success=""
1444 color_reset=""
1445 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001446 echo
1447 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001448 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001449 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001450 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001451 fi
1452 if [ $hours -gt 0 ] ; then
1453 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1454 elif [ $mins -gt 0 ] ; then
1455 printf "(%02g:%02g (mm:ss))" $mins $secs
1456 elif [ $secs -gt 0 ] ; then
1457 printf "(%s seconds)" $secs
1458 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001459 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001460 echo
1461 return $ret
1462}
1463
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001464function _trigger_build()
1465(
1466 local -r bc="$1"; shift
1467 if T="$(gettop)"; then
1468 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1469 else
1470 echo "Couldn't locate the top of the tree. Try setting TOP."
1471 fi
1472)
1473
1474function m()
1475(
1476 _trigger_build "all-modules" "$@"
1477)
1478
1479function mm()
1480(
1481 _trigger_build "modules-in-a-dir-no-deps" "$@"
1482)
1483
1484function mmm()
1485(
1486 _trigger_build "modules-in-dirs-no-deps" "$@"
1487)
1488
1489function mma()
1490(
1491 _trigger_build "modules-in-a-dir" "$@"
1492)
1493
1494function mmma()
1495(
1496 _trigger_build "modules-in-dirs" "$@"
1497)
1498
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001499function make()
1500{
Dan Willemsene9842242017-07-28 13:00:13 -07001501 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001502}
1503
David Zeuthen1b126ff2015-09-30 17:10:48 -04001504function provision()
1505{
1506 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1507 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1508 return 1
1509 fi
1510 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1511 echo "There is no provisioning script for the device." >&2
1512 return 1
1513 fi
1514
1515 # Check if user really wants to do this.
1516 if [ "$1" = "--no-confirmation" ]; then
1517 shift 1
1518 else
1519 echo "This action will reflash your device."
1520 echo ""
1521 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1522 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001523 echo -n "Are you sure you want to do this (yes/no)? "
1524 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001525 if [[ "${REPLY}" != "yes" ]] ; then
1526 echo "Not taking any action. Exiting." >&2
1527 return 1
1528 fi
1529 fi
1530 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1531}
1532
Jim Tanga881a252018-06-19 16:34:41 +08001533# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001534function enable_zsh_completion() {
1535 # Don't override user's options if bash-style completion is already enabled.
1536 if ! declare -f complete >/dev/null; then
1537 autoload -U compinit && compinit
1538 autoload -U bashcompinit && bashcompinit
1539 fi
Jim Tanga881a252018-06-19 16:34:41 +08001540}
1541
1542function validate_current_shell() {
1543 local current_sh="$(ps -o command -p $$)"
1544 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001545 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001546 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001547 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001548 *zsh*)
1549 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001550 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001551 *)
Jim Tanga881a252018-06-19 16:34:41 +08001552 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 -07001553 ;;
1554 esac
Jim Tanga881a252018-06-19 16:34:41 +08001555}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001556
1557# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001558# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1559# load those.
1560#
1561# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001562function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001563 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001564 allowed=
1565 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1566 if [ -n "$allowed" ]; then
1567 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1568 echo " $allowed"
1569 echo " $f"
1570 return
1571 fi
1572 allowed="$f"
1573 done
1574
1575 allowed_files=
1576 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001577 for dir in device vendor product; do
1578 for f in $(test -d $dir && \
1579 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001580
1581 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1582 echo "including $f"; . "$f"
1583 else
1584 echo "ignoring $f, not in $allowed"
1585 fi
Jim Tanga881a252018-06-19 16:34:41 +08001586 done
1587 done
1588}
Kenny Root52aa81c2011-07-15 11:07:06 -07001589
Jim Tanga881a252018-06-19 16:34:41 +08001590validate_current_shell
1591source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001592addcompletions