blob: 793f4b6cfadd63d203c07201945de0e5f4b72648 [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.
Dan Willemsen67074fe2019-10-30 12:35:34 -070014- mm: Builds and installs all of the modules in the current directory, and their
15 dependencies.
16- mmm: Builds and installs all of the modules in the supplied directories, and their
17 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080018 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070019- mma: Same as 'mm'
20- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080021- provision: Flash device with all required partitions. Options will be passed on to fastboot.
22- cgrep: Greps on all local C/C++ files.
23- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010024- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080025- jgrep: Greps on all local Java files.
26- resgrep: Greps on all local res/*.xml files.
27- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070028- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060029- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- sepgrep: Greps on all local sepolicy files.
31- sgrep: Greps on all local source files.
32- godir: Go to the directory containing a file.
33- allmod: List all modules.
34- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080035- pathmod: Get the directory containing a module.
Steven Moreland62054a42018-12-06 10:11:40 -080036- refreshmod: Refresh list of modules for allmod/gomod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070037
Roland Levillain39341922015-10-20 12:48:19 +010038Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070039- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080040- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070041
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070042Look at the source to view more functions. The complete list is:
43EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070044 local T=$(gettop)
45 local A=""
46 local i
Jacky Cao89483b82015-05-15 22:12:53 +080047 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 -070048 A="$A $i"
49 done
50 echo $A
51}
52
Ying Wang08800fd2016-03-03 20:57:21 -080053# Get all the build variables needed by this script in a single call to the build system.
54function build_build_var_cache()
55{
Christopher Ferris55257d22017-03-23 11:08:58 -070056 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080057 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080058 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' ' '`)
59 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 -080060 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080061 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080062 --vars="${cached_vars[*]}" \
63 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070064 --var-prefix=var_cache_ \
65 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080066 local ret=$?
67 if [ $ret -ne 0 ]
68 then
69 unset build_dicts_script
70 return $ret
71 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070072 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080073 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080074 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080075 unset build_dicts_script
Ying Wang08800fd2016-03-03 20:57:21 -080076 if [ $ret -ne 0 ]
77 then
78 return $ret
79 fi
80 BUILD_VAR_CACHE_READY="true"
81}
82
Ying Wangf0cb3972016-03-04 13:56:23 -080083# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080084# to get build variables not listed in this script.
85function destroy_build_var_cache()
86{
87 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070088 local v
Ying Wang08800fd2016-03-03 20:57:21 -080089 for v in $cached_vars; do
90 unset var_cache_$v
91 done
92 unset cached_vars
93 for v in $cached_abs_vars; do
94 unset abs_var_cache_$v
95 done
96 unset cached_abs_vars
97}
98
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070099# Get the value of a build variable as an absolute path.
100function get_abs_build_var()
101{
Ying Wang08800fd2016-03-03 20:57:21 -0800102 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
103 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800104 eval "echo \"\${abs_var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800105 return
106 fi
107
Christopher Ferris55257d22017-03-23 11:08:58 -0700108 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700109 if [ ! "$T" ]; then
110 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
111 return
112 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700113 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700114}
115
116# Get the exact value of a build variable.
117function get_build_var()
118{
Ying Wang08800fd2016-03-03 20:57:21 -0800119 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
120 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800121 eval "echo \"\${var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800122 return
123 fi
124
Christopher Ferris55257d22017-03-23 11:08:58 -0700125 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700126 if [ ! "$T" ]; then
127 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
128 return
129 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700130 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800131}
132
133# check to see if the supplied product is one we can build
134function check_product()
135{
Christopher Ferris55257d22017-03-23 11:08:58 -0700136 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800137 if [ ! "$T" ]; then
138 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
139 return
140 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700141 TARGET_PRODUCT=$1 \
142 TARGET_BUILD_VARIANT= \
143 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700144 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800145 get_build_var TARGET_DEVICE > /dev/null
146 # hide successful answers, but allow the errors to show
147}
148
149VARIANT_CHOICES=(user userdebug eng)
150
151# check to see if the supplied variant is valid
152function check_variant()
153{
Christopher Ferris55257d22017-03-23 11:08:58 -0700154 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800155 for v in ${VARIANT_CHOICES[@]}
156 do
157 if [ "$v" = "$1" ]
158 then
159 return 0
160 fi
161 done
162 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700163}
164
165function setpaths()
166{
Christopher Ferris55257d22017-03-23 11:08:58 -0700167 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700168 if [ ! "$T" ]; then
169 echo "Couldn't locate the top of the tree. Try setting TOP."
170 return
171 fi
172
173 ##################################################################
174 # #
175 # Read me before you modify this code #
176 # #
177 # This function sets ANDROID_BUILD_PATHS to what it is adding #
178 # to PATH, and the next time it is run, it removes that from #
179 # PATH. This is required so lunch can be run more than once #
180 # and still have working paths. #
181 # #
182 ##################################################################
183
Raphael Mollc639c782011-06-20 17:25:01 -0700184 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
185 # due to "C:\Program Files" being in the path.
186
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700187 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700188 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700189 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
190 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700191 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700192 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800193 # strip leading ':', if any
194 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500195 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196
197 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700198 local prebuiltdir=$(getprebuilt)
199 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700200
Ben Cheng8bc4c432012-11-16 13:29:13 -0800201 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700202 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
203 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800204 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800205
Raphael Mollc639c782011-06-20 17:25:01 -0700206 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800207 export ANDROID_TOOLCHAIN=
208 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200209 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700210 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200211 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400212 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700213 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400214 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
215 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800216 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200217 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800218 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700219 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700220 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700221 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000222 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200223 *)
224 echo "Can't find toolchain for unknown architecture: $ARCH"
225 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700226 ;;
227 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700228 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800229 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700230 fi
Raphael732936d2011-06-22 14:35:32 -0700231
Christopher Ferris55257d22017-03-23 11:08:58 -0700232 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800233 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
234 fi
235
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700236 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700237
238 # add kernel specific binaries
239 case $(uname -s) in
240 Linux)
241 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
242 ;;
243 *)
244 ;;
245 esac
246
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400247 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
248 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
249 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
250 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700251 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
252
253 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
254 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
255 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200256
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800257 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
258 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
259
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200260 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
261 # to ensure that the corresponding 'emulator' binaries are used.
262 case $(uname -s) in
263 Darwin)
264 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
265 ;;
266 Linux)
267 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
268 ;;
269 *)
270 ANDROID_EMULATOR_PREBUILTS=
271 ;;
272 esac
273 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700274 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200275 export ANDROID_EMULATOR_PREBUILTS
276 fi
277
Jim Tangb3fda302018-12-22 10:24:55 +0800278 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
279 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700280 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
281 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
282 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
283 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800284
Ryan Prichard8426a192019-07-15 18:05:29 -0700285 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800286
287 # out with the duplicate old
288 if [ -n $ANDROID_PYTHONPATH ]; then
289 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
290 fi
291 # and in with the new
292 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800293 if [ -n $VENDOR_PYTHONPATH ]; then
294 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
295 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800296 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800297
Colin Crosse97e6932017-06-30 16:01:45 -0700298 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
299 export JAVA_HOME=$ANDROID_JAVA_HOME
300 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
301 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
302 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500303
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800304 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700305 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
306 export OUT=$ANDROID_PRODUCT_OUT
307
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700308 unset ANDROID_HOST_OUT
309 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
310
Simran Basidd050ed2017-02-13 13:46:48 -0800311 unset ANDROID_HOST_OUT_TESTCASES
312 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
313
314 unset ANDROID_TARGET_OUT_TESTCASES
315 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
316
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800317 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700318 # TODO: fix the path
319 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
320}
321
322function printconfig()
323{
Christopher Ferris55257d22017-03-23 11:08:58 -0700324 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 if [ ! "$T" ]; then
326 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
327 return
328 fi
329 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700330}
331
332function set_stuff_for_environment()
333{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800334 setpaths
335 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700336
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800337 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700338 # With this environment variable new GCC can apply colors to warnings/errors
339 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 -0700340}
341
342function set_sequence_number()
343{
Colin Cross88737132017-03-21 17:41:03 -0700344 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700345}
346
Makoto Onukida971062018-06-18 10:15:19 -0700347# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
348function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800349 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700350 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800351 *:"$cmd":*)
352 return 1
353 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700354 esac
355 return 0
356}
357
Kenny Root52aa81c2011-07-15 11:07:06 -0700358function addcompletions()
359{
360 local T dir f
361
Jim Tanga881a252018-06-19 16:34:41 +0800362 # Keep us from trying to run in something that's neither bash nor zsh.
363 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700364 return
365 fi
366
367 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800368 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700369 return
370 fi
371
Jim Tanga881a252018-06-19 16:34:41 +0800372 local completion_files=(
373 system/core/adb/adb.bash
374 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800375 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800376 )
Makoto Onukida971062018-06-18 10:15:19 -0700377 # Completion can be disabled selectively to allow users to use non-standard completion.
378 # e.g.
379 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
380 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800381 for f in ${completion_files[*]}; do
382 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700383 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700384 fi
385 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700386
Makoto Onukida971062018-06-18 10:15:19 -0700387 if should_add_completion bit ; then
388 complete -C "bit --tab" bit
389 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000390 if [ -z "$ZSH_VERSION" ]; then
391 # Doesn't work in zsh.
392 complete -o nospace -F _croot croot
393 fi
Jim Tanga881a252018-06-19 16:34:41 +0800394 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800395
dimitry73b84812018-12-11 18:06:00 +0100396 complete -F _complete_android_module_names gomod
397 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700398}
399
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400function choosetype()
401{
402 echo "Build type choices are:"
403 echo " 1. release"
404 echo " 2. debug"
405 echo
406
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800407 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700408 DEFAULT_NUM=1
409 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700410
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800411 export TARGET_BUILD_TYPE=
412 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700413 while [ -z $TARGET_BUILD_TYPE ]
414 do
415 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800416 if [ -z "$1" ] ; then
417 read ANSWER
418 else
419 echo $1
420 ANSWER=$1
421 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700422 case $ANSWER in
423 "")
424 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
425 ;;
426 1)
427 export TARGET_BUILD_TYPE=release
428 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800429 release)
430 export TARGET_BUILD_TYPE=release
431 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 2)
433 export TARGET_BUILD_TYPE=debug
434 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800435 debug)
436 export TARGET_BUILD_TYPE=debug
437 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438 *)
439 echo
440 echo "I didn't understand your response. Please try again."
441 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700442 ;;
443 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800444 if [ -n "$1" ] ; then
445 break
446 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700447 done
448
Ying Wang08800fd2016-03-03 20:57:21 -0800449 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800451 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700452}
453
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800454#
455# This function isn't really right: It chooses a TARGET_PRODUCT
456# based on the list of boards. Usually, that gets you something
457# that kinda works with a generic product, but really, you should
458# pick a product by name.
459#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460function chooseproduct()
461{
Christopher Ferris55257d22017-03-23 11:08:58 -0700462 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700463 if [ "x$TARGET_PRODUCT" != x ] ; then
464 default_value=$TARGET_PRODUCT
465 else
Ying Wang0a76df52015-06-08 11:57:26 -0700466 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 fi
468
Ying Wang08800fd2016-03-03 20:57:21 -0800469 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800470 export TARGET_PRODUCT=
471 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472 while [ -z "$TARGET_PRODUCT" ]
473 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700474 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800475 if [ -z "$1" ] ; then
476 read ANSWER
477 else
478 echo $1
479 ANSWER=$1
480 fi
481
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 if [ -z "$ANSWER" ] ; then
483 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800484 else
485 if check_product $ANSWER
486 then
487 export TARGET_PRODUCT=$ANSWER
488 else
489 echo "** Not a valid product: $ANSWER"
490 fi
491 fi
492 if [ -n "$1" ] ; then
493 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494 fi
495 done
496
Ying Wang08800fd2016-03-03 20:57:21 -0800497 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800499 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700500}
501
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502function choosevariant()
503{
504 echo "Variant choices are:"
505 local index=1
506 local v
507 for v in ${VARIANT_CHOICES[@]}
508 do
509 # The product name is the name of the directory containing
510 # the makefile we found, above.
511 echo " $index. $v"
512 index=$(($index+1))
513 done
514
515 local default_value=eng
516 local ANSWER
517
518 export TARGET_BUILD_VARIANT=
519 while [ -z "$TARGET_BUILD_VARIANT" ]
520 do
521 echo -n "Which would you like? [$default_value] "
522 if [ -z "$1" ] ; then
523 read ANSWER
524 else
525 echo $1
526 ANSWER=$1
527 fi
528
529 if [ -z "$ANSWER" ] ; then
530 export TARGET_BUILD_VARIANT=$default_value
531 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
532 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200533 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800534 fi
535 else
536 if check_variant $ANSWER
537 then
538 export TARGET_BUILD_VARIANT=$ANSWER
539 else
540 echo "** Not a valid variant: $ANSWER"
541 fi
542 fi
543 if [ -n "$1" ] ; then
544 break
545 fi
546 done
547}
548
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549function choosecombo()
550{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700551 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552
553 echo
554 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700555 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556
557 echo
558 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700559 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800560
561 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800562 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800564 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800565 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566}
567
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800568function add_lunch_combo()
569{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800570 if [ -n "$ZSH_VERSION" ]; then
571 echo -n "${funcfiletrace[1]}: "
572 else
573 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
574 fi
575 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 -0800576}
577
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578function print_lunch_menu()
579{
580 local uname=$(uname)
Colin Crosscb8337d2019-09-23 12:52:32 -0700581 local choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582 echo
583 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 echo
585 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586
587 local i=1
588 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700589 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800590 do
591 echo " $i. $choice"
592 i=$(($i+1))
593 done
594
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595 echo
596}
597
598function lunch()
599{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800600 local answer
601
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604 else
605 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700606 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800607 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 fi
609
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800610 local selection=
611
612 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700613 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700614 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800615 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
616 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800617 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700618 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800620 # array in zsh starts from 1 instead of 0.
621 if [ -n "$ZSH_VERSION" ]
622 then
623 selection=${choices[$(($answer))]}
624 else
625 selection=${choices[$(($answer-1))]}
626 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800627 fi
Colin Cross88737132017-03-21 17:41:03 -0700628 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800629 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700630 fi
631
Joe Onoratoda12daf2010-06-09 18:18:31 -0700632 export TARGET_BUILD_APPS=
633
Colin Cross88737132017-03-21 17:41:03 -0700634 local product variant_and_version variant version
635
636 product=${selection%%-*} # Trim everything after first dash
637 variant_and_version=${selection#*-} # Trim everything up to first dash
638 if [ "$variant_and_version" != "$selection" ]; then
639 variant=${variant_and_version%%-*}
640 if [ "$variant" != "$variant_and_version" ]; then
641 version=${variant_and_version#*-}
642 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700643 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800644
Colin Cross88737132017-03-21 17:41:03 -0700645 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800646 then
647 echo
Colin Cross88737132017-03-21 17:41:03 -0700648 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700649 return 1
650 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800651
Colin Cross88737132017-03-21 17:41:03 -0700652 TARGET_PRODUCT=$product \
653 TARGET_BUILD_VARIANT=$variant \
654 TARGET_PLATFORM_VERSION=$version \
655 build_build_var_cache
656 if [ $? -ne 0 ]
657 then
658 return 1
659 fi
660
661 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
662 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700663 if [ -n "$version" ]; then
664 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
665 else
666 unset TARGET_PLATFORM_VERSION
667 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700668 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700669
670 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800671
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700672 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800673 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800674 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700675}
676
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700677unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700678# Tab completion for lunch.
679function _lunch()
680{
681 local cur prev opts
682 COMPREPLY=()
683 cur="${COMP_WORDS[COMP_CWORD]}"
684 prev="${COMP_WORDS[COMP_CWORD-1]}"
685
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700686 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800687 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700688 fi
689
690 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700691 return 0
692}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700693
Joe Onoratoda12daf2010-06-09 18:18:31 -0700694# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700695# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700696function tapas()
697{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700698 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800699 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700700 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700701 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 -0800702 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 -0700703
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700704 if [ "$showHelp" != "" ]; then
705 $(gettop)/build/make/tapasHelp.sh
706 return
707 fi
708
Ying Wang67f02922012-08-22 10:25:20 -0700709 if [ $(echo $arch | wc -w) -gt 1 ]; then
710 echo "tapas: Error: Multiple build archs supplied: $arch"
711 return
712 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700713 if [ $(echo $variant | wc -w) -gt 1 ]; then
714 echo "tapas: Error: Multiple build variants supplied: $variant"
715 return
716 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700717 if [ $(echo $density | wc -w) -gt 1 ]; then
718 echo "tapas: Error: Multiple densities supplied: $density"
719 return
720 fi
Ying Wang67f02922012-08-22 10:25:20 -0700721
Ying Wang0a76df52015-06-08 11:57:26 -0700722 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700723 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700724 x86) product=aosp_x86;;
725 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700726 arm64) product=aosp_arm64;;
727 x86_64) product=aosp_x86_64;;
728 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700729 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700730 if [ -z "$variant" ]; then
731 variant=eng
732 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700733 if [ -z "$apps" ]; then
734 apps=all
735 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600736 if [ -z "$density" ]; then
737 density=alldpi
738 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700739
Ying Wang67f02922012-08-22 10:25:20 -0700740 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700741 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700742 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700743 export TARGET_BUILD_TYPE=release
744 export TARGET_BUILD_APPS=$apps
745
Ying Wang08800fd2016-03-03 20:57:21 -0800746 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700747 set_stuff_for_environment
748 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800749 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700750}
751
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700752function gettop
753{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700754 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700755 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700756 # The following circumlocution ensures we remove symlinks from TOP.
757 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700758 else
759 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800760 # The following circumlocution (repeated below as well) ensures
761 # that we record the true directory name and not one that is
762 # faked up with symlink names.
763 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800765 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700766 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800768 \cd ..
synergyb112a402013-07-05 19:47:47 -0700769 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700770 done
Ying Wang9cd17642012-12-13 10:52:07 -0800771 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700772 if [ -f "$T/$TOPFILE" ]; then
773 echo $T
774 fi
775 fi
776 fi
777}
778
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700779function croot()
780{
Christopher Ferris55257d22017-03-23 11:08:58 -0700781 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700782 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700783 if [ "$1" ]; then
784 \cd $(gettop)/$1
785 else
786 \cd $(gettop)
787 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 else
789 echo "Couldn't locate the top of the tree. Try setting TOP."
790 fi
791}
792
Anton Hanssonece9c482019-02-04 18:15:39 +0000793function _croot()
794{
795 local T=$(gettop)
796 if [ "$T" ]; then
797 local cur="${COMP_WORDS[COMP_CWORD]}"
798 k=0
799 for c in $(compgen -d ${T}/${cur}); do
800 COMPREPLY[k++]=${c#${T}/}/
801 done
802 fi
803}
804
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700805function cproj()
806{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700807 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700808 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700809 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700810 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
811 T=$PWD
812 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800813 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700814 return
815 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800816 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700817 done
Ying Wang9cd17642012-12-13 10:52:07 -0800818 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700819 echo "can't find Android.mk"
820}
821
Daniel Sandler47e0a882013-07-30 13:23:52 -0400822# simplified version of ps; output in the form
823# <pid> <procname>
824function qpid() {
825 local prepend=''
826 local append=''
827 if [ "$1" = "--exact" ]; then
828 prepend=' '
829 append='$'
830 shift
831 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800832 echo "usage: qpid [[--exact] <process name|pid>"
833 return 255
834 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400835
836 local EXE="$1"
837 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800838 qpid | \grep "$prepend$EXE$append"
839 else
840 adb shell ps \
841 | tr -d '\r' \
842 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
843 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400844}
845
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800846# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700847# that has the core-file-size limit set correctly
848#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800849# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700850# if its core-file-size limit is not set already.
851# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
852
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800853function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700854{
Ying Wang08800fd2016-03-03 20:57:21 -0800855 echo "Getting root...";
856 adb root;
857 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700858
Ying Wang08800fd2016-03-03 20:57:21 -0800859 echo "Remounting root partition read-write...";
860 adb shell mount -w -o remount -t rootfs rootfs;
861 sleep 1;
862 adb wait-for-device;
863 adb shell mkdir -p /cores;
864 adb shell mount -t tmpfs tmpfs /cores;
865 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866
Ying Wang08800fd2016-03-03 20:57:21 -0800867 echo "Granting SELinux permission to dump in /cores...";
868 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869
Ying Wang08800fd2016-03-03 20:57:21 -0800870 echo "Set core pattern.";
871 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700872
Ying Wang08800fd2016-03-03 20:57:21 -0800873 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700874}
875
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800876# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700877# $1 = PID of process (e.g., $(pid mediaserver))
878#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800879# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700880# dump to actually be generated.
881
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800882function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700883{
Ying Wang08800fd2016-03-03 20:57:21 -0800884 local PID=$1;
885 if [ -z "$PID" ]; then
886 printf "Expecting a PID!\n";
887 return;
888 fi;
889 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800890 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700891}
892
893# core - send SIGV and pull the core for process
894# $1 = PID of process (e.g., $(pid mediaserver))
895#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800896# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700897# enabled globally.
898
899function core()
900{
Ying Wang08800fd2016-03-03 20:57:21 -0800901 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700902
Ying Wang08800fd2016-03-03 20:57:21 -0800903 if [ -z "$PID" ]; then
904 printf "Expecting a PID!\n";
905 return;
906 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700907
Ying Wang08800fd2016-03-03 20:57:21 -0800908 local CORENAME=core.$PID;
909 local COREPATH=/cores/$CORENAME;
910 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700911
Ying Wang08800fd2016-03-03 20:57:21 -0800912 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700913
Ying Wang08800fd2016-03-03 20:57:21 -0800914 local done=0;
915 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
916 printf "\tSending SIG%s to %d...\n" $SIG $PID;
917 adb shell kill -$SIG $PID;
918 sleep 1;
919 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700920
Ying Wang08800fd2016-03-03 20:57:21 -0800921 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
922 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923}
924
Christopher Tate744ee802009-11-12 15:33:08 -0800925# systemstack - dump the current stack trace of all threads in the system process
926# to the usual ANR traces file
927function systemstack()
928{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800929 stacks system_server
930}
931
Michael Wrightaeed7212014-06-19 19:58:12 -0700932# Read the ELF header from /proc/$PID/exe to determine if the process is
933# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800934function is64bit()
935{
936 local PID="$1"
937 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800938 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800939 echo "64"
940 else
941 echo ""
942 fi
943 else
944 echo ""
945 fi
946}
947
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700948case `uname -s` in
949 Darwin)
950 function sgrep()
951 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100952 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 -0400953 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700954 }
955
956 ;;
957 *)
958 function sgrep()
959 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100960 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 -0400961 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700962 }
963 ;;
964esac
965
Raghu Gandham8da43102012-07-25 19:57:22 -0700966function gettargetarch
967{
968 get_build_var TARGET_ARCH
969}
970
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700971function ggrep()
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 "*\.gradle" \
974 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700975}
976
Orion Hodson831472d2019-10-25 11:35:15 +0100977function gogrep()
978{
979 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
980 -exec grep --color -n "$@" {} +
981}
982
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700983function jgrep()
984{
Mike Frysinger5e479732015-09-22 18:13:48 -0400985 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
986 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700987}
988
989function cgrep()
990{
Mike Frysinger5e479732015-09-22 18:13:48 -0400991 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' \) \
992 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700993}
994
995function resgrep()
996{
Christopher Ferris55257d22017-03-23 11:08:58 -0700997 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400998 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
999 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1000 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001001}
1002
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001003function mangrep()
1004{
Mike Frysinger5e479732015-09-22 18:13:48 -04001005 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1006 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001007}
1008
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001009function owngrep()
1010{
1011 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1012 -exec grep --color -n "$@" {} +
1013}
1014
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001015function sepgrep()
1016{
Mike Frysinger5e479732015-09-22 18:13:48 -04001017 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1018 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001019}
1020
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001021function rcgrep()
1022{
Mike Frysinger5e479732015-09-22 18:13:48 -04001023 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1024 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001025}
1026
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001027case `uname -s` in
1028 Darwin)
1029 function mgrep()
1030 {
Orion Hodson831472d2019-10-25 11:35:15 +01001031 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 -04001032 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001033 }
1034
1035 function treegrep()
1036 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001037 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 -04001038 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001039 }
1040
1041 ;;
1042 *)
1043 function mgrep()
1044 {
Orion Hodson831472d2019-10-25 11:35:15 +01001045 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 -04001046 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001047 }
1048
1049 function treegrep()
1050 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001051 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 -04001052 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001053 }
1054
1055 ;;
1056esac
1057
1058function getprebuilt
1059{
1060 get_abs_build_var ANDROID_PREBUILTS
1061}
1062
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001063function tracedmdump()
1064{
Christopher Ferris55257d22017-03-23 11:08:58 -07001065 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001066 if [ ! "$T" ]; then
1067 echo "Couldn't locate the top of the tree. Try setting TOP."
1068 return
1069 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001070 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001071 local arch=$(gettargetarch)
1072 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001073
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001074 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001075 if [ ! "$TRACE" ] ; then
1076 echo "usage: tracedmdump tracename"
1077 return
1078 fi
1079
Jack Veenstra60116fc2009-04-09 18:12:34 -07001080 if [ ! -r "$KERNEL" ] ; then
1081 echo "Error: cannot find kernel: '$KERNEL'"
1082 return
1083 fi
1084
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001085 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001086 if [ "$BASETRACE" = "$TRACE" ] ; then
1087 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1088 fi
1089
1090 echo "post-processing traces..."
1091 rm -f $TRACE/qtrace.dexlist
1092 post_trace $TRACE
1093 if [ $? -ne 0 ]; then
1094 echo "***"
1095 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1096 echo "***"
1097 return
1098 fi
1099 echo "generating dexlist output..."
1100 /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
1101 echo "generating dmtrace data..."
1102 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1103 echo "generating html file..."
1104 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1105 echo "done, see $TRACE/dmtrace.html for details"
1106 echo "or run:"
1107 echo " traceview $TRACE/dmtrace"
1108}
1109
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001110# communicate with a running device or emulator, set up necessary state,
1111# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001112function runhat()
1113{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001114 # process standard adb options
1115 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001116 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001117 adbTarget=$1
1118 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001119 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001120 adbTarget="$1 $2"
1121 shift 2
1122 fi
1123 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001124 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001125
1126 # runhat options
1127 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001128
1129 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001130 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001131 return
1132 fi
1133
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001134 # confirm hat is available
1135 if [ -z $(which hat) ]; then
1136 echo "hat is not available in this configuration."
1137 return
1138 fi
1139
Andy McFaddenb6289852010-07-12 08:00:19 -07001140 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001141 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001142 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001143 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001144 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001145 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001146 echo -n "> "
1147 read
1148
The Android Open Source Project88b60792009-03-03 19:28:42 -08001149 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001150
The Android Open Source Project88b60792009-03-03 19:28:42 -08001151 echo "Retrieving file $devFile..."
1152 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153
The Android Open Source Project88b60792009-03-03 19:28:42 -08001154 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001155
The Android Open Source Project88b60792009-03-03 19:28:42 -08001156 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001157 echo "View the output by pointing your browser at http://localhost:7000/"
1158 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001159 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001160}
1161
1162function getbugreports()
1163{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001164 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001165
1166 if [ ! "$reports" ]; then
1167 echo "Could not locate any bugreports."
1168 return
1169 fi
1170
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001171 local report
1172 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001173 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001174 echo "/sdcard/bugreports/${report}"
1175 adb pull /sdcard/bugreports/${report} ${report}
1176 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001177 done
1178}
1179
Victoria Lease1b296b42012-08-21 15:44:06 -07001180function getsdcardpath()
1181{
1182 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1183}
1184
1185function getscreenshotpath()
1186{
1187 echo "$(getsdcardpath)/Pictures/Screenshots"
1188}
1189
1190function getlastscreenshot()
1191{
1192 local screenshot_path=$(getscreenshotpath)
1193 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1194 if [ "$screenshot" = "" ]; then
1195 echo "No screenshots found."
1196 return
1197 fi
1198 echo "${screenshot}"
1199 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1200}
1201
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001202function startviewserver()
1203{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001204 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001205 if [ $# -gt 0 ]; then
1206 port=$1
1207 fi
1208 adb shell service call window 1 i32 $port
1209}
1210
1211function stopviewserver()
1212{
1213 adb shell service call window 2
1214}
1215
1216function isviewserverstarted()
1217{
1218 adb shell service call window 3
1219}
1220
Romain Guyb84049a2010-10-04 16:56:11 -07001221function key_home()
1222{
1223 adb shell input keyevent 3
1224}
1225
1226function key_back()
1227{
1228 adb shell input keyevent 4
1229}
1230
1231function key_menu()
1232{
1233 adb shell input keyevent 82
1234}
1235
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001236function smoketest()
1237{
1238 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1239 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1240 return
1241 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001242 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001243 if [ ! "$T" ]; then
1244 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1245 return
1246 fi
1247
Ying Wang9cd17642012-12-13 10:52:07 -08001248 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001249 adb uninstall com.android.smoketest > /dev/null &&
1250 adb uninstall com.android.smoketest.tests > /dev/null &&
1251 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1252 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1253 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1254}
1255
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001256# simple shortcut to the runtest command
1257function runtest()
1258{
Christopher Ferris55257d22017-03-23 11:08:58 -07001259 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001260 if [ ! "$T" ]; then
1261 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1262 return
1263 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001264 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001265}
1266
The Android Open Source Project88b60792009-03-03 19:28:42 -08001267function godir () {
1268 if [[ -z "$1" ]]; then
1269 echo "Usage: godir <regex>"
1270 return
1271 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001272 local T=$(gettop)
1273 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001274 if [ ! "$OUT_DIR" = "" ]; then
1275 mkdir -p $OUT_DIR
1276 FILELIST=$OUT_DIR/filelist
1277 else
1278 FILELIST=$T/filelist
1279 fi
1280 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001281 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001282 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001283 echo " Done"
1284 echo ""
1285 fi
1286 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001287 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001288 if [[ ${#lines[@]} = 0 ]]; then
1289 echo "Not found"
1290 return
1291 fi
1292 local pathname
1293 local choice
1294 if [[ ${#lines[@]} > 1 ]]; then
1295 while [[ -z "$pathname" ]]; do
1296 local index=1
1297 local line
1298 for line in ${lines[@]}; do
1299 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001300 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001301 done
1302 echo
1303 echo -n "Select one: "
1304 unset choice
1305 read choice
1306 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1307 echo "Invalid choice"
1308 continue
1309 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001310 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001311 done
1312 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001313 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001314 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001315 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001316}
1317
Steven Moreland62054a42018-12-06 10:11:40 -08001318# Update module-info.json in out.
1319function refreshmod() {
1320 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1321 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1322 return 1
1323 fi
1324
1325 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1326
1327 # for the output of the next command
1328 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1329
1330 # Note, can't use absolute path because of the way make works.
1331 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1332 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1333}
1334
1335# List all modules for the current device, as cached in module-info.json. If any build change is
1336# made and it should be reflected in the output, you should run 'refreshmod' first.
1337function allmod() {
1338 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1339 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1340 return 1
1341 fi
1342
1343 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1344 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1345 refreshmod || return 1
1346 fi
1347
LuK1337b6a78192020-01-12 03:12:17 +01001348 python -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
Steven Moreland62054a42018-12-06 10:11:40 -08001349}
1350
Rett Berg78d1c932019-01-24 14:34:23 -08001351# 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 -08001352# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001353function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001354 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1355 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1356 return 1
1357 fi
1358
1359 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001360 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001361 return 1
1362 fi
1363
1364 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1365 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1366 refreshmod || return 1
1367 fi
1368
1369 local relpath=$(python -c "import json, os
1370module = '$1'
1371module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1372if module not in module_info:
1373 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001374print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001375
1376 if [ -z "$relpath" ]; then
1377 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1378 return 1
1379 else
Rett Berg78d1c932019-01-24 14:34:23 -08001380 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001381 fi
1382}
1383
Rett Berg78d1c932019-01-24 14:34:23 -08001384# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1385# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1386function gomod() {
1387 if [[ $# -ne 1 ]]; then
1388 echo "usage: gomod <module>" >&2
1389 return 1
1390 fi
1391
1392 local path="$(pathmod $@)"
1393 if [ -z "$path" ]; then
1394 return 1
1395 fi
1396 cd $path
1397}
1398
dimitry73b84812018-12-11 18:06:00 +01001399function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001400 local word=${COMP_WORDS[COMP_CWORD]}
1401 COMPREPLY=( $(allmod | grep -E "^$word") )
1402}
1403
Alex Rayf0d08eb2013-03-08 15:15:06 -08001404# Print colored exit condition
1405function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001406 "$@"
1407 local retval=$?
1408 if [ $retval -ne 0 ]
1409 then
Jacky Cao89483b82015-05-15 22:12:53 +08001410 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001411 else
Jacky Cao89483b82015-05-15 22:12:53 +08001412 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001413 fi
1414 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001415}
1416
Ying Wanged21d4c2014-08-24 22:14:19 -07001417function get_make_command()
1418{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001419 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1420 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001421 # Always use the real make if -C is passed in
1422 for arg in "$@"; do
1423 if [[ $arg == -C* ]]; then
1424 echo command make
1425 return
1426 fi
1427 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001428 echo build/soong/soong_ui.bash --make-mode
1429 else
1430 echo command make
1431 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001432}
1433
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001434function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001435{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001436 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1437 "$@"
1438 return $?
1439 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001440 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001441 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001442 local ret=$?
1443 local end_time=$(date +"%s")
1444 local tdiff=$(($end_time-$start_time))
1445 local hours=$(($tdiff / 3600 ))
1446 local mins=$((($tdiff % 3600) / 60))
1447 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001448 local ncolors=$(tput colors 2>/dev/null)
1449 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001450 color_failed=$'\E'"[0;31m"
1451 color_success=$'\E'"[0;32m"
1452 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001453 else
1454 color_failed=""
1455 color_success=""
1456 color_reset=""
1457 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001458 echo
1459 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001460 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001461 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001462 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001463 fi
1464 if [ $hours -gt 0 ] ; then
1465 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1466 elif [ $mins -gt 0 ] ; then
1467 printf "(%02g:%02g (mm:ss))" $mins $secs
1468 elif [ $secs -gt 0 ] ; then
1469 printf "(%s seconds)" $secs
1470 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001471 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001472 echo
1473 return $ret
1474}
1475
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001476function _trigger_build()
1477(
1478 local -r bc="$1"; shift
1479 if T="$(gettop)"; then
1480 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1481 else
1482 echo "Couldn't locate the top of the tree. Try setting TOP."
1483 fi
1484)
1485
1486function m()
1487(
1488 _trigger_build "all-modules" "$@"
1489)
1490
1491function mm()
1492(
1493 _trigger_build "modules-in-a-dir-no-deps" "$@"
1494)
1495
1496function mmm()
1497(
1498 _trigger_build "modules-in-dirs-no-deps" "$@"
1499)
1500
1501function mma()
1502(
1503 _trigger_build "modules-in-a-dir" "$@"
1504)
1505
1506function mmma()
1507(
1508 _trigger_build "modules-in-dirs" "$@"
1509)
1510
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001511function make()
1512{
Dan Willemsene9842242017-07-28 13:00:13 -07001513 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001514}
1515
David Zeuthen1b126ff2015-09-30 17:10:48 -04001516function provision()
1517{
1518 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1519 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1520 return 1
1521 fi
1522 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1523 echo "There is no provisioning script for the device." >&2
1524 return 1
1525 fi
1526
1527 # Check if user really wants to do this.
1528 if [ "$1" = "--no-confirmation" ]; then
1529 shift 1
1530 else
1531 echo "This action will reflash your device."
1532 echo ""
1533 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1534 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001535 echo -n "Are you sure you want to do this (yes/no)? "
1536 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001537 if [[ "${REPLY}" != "yes" ]] ; then
1538 echo "Not taking any action. Exiting." >&2
1539 return 1
1540 fi
1541 fi
1542 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1543}
1544
Jim Tanga881a252018-06-19 16:34:41 +08001545# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001546function enable_zsh_completion() {
1547 # Don't override user's options if bash-style completion is already enabled.
1548 if ! declare -f complete >/dev/null; then
1549 autoload -U compinit && compinit
1550 autoload -U bashcompinit && bashcompinit
1551 fi
Jim Tanga881a252018-06-19 16:34:41 +08001552}
1553
1554function validate_current_shell() {
1555 local current_sh="$(ps -o command -p $$)"
1556 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001557 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001558 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001559 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001560 *zsh*)
1561 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001562 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001563 *)
Jim Tanga881a252018-06-19 16:34:41 +08001564 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 -07001565 ;;
1566 esac
Jim Tanga881a252018-06-19 16:34:41 +08001567}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001568
1569# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001570# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1571# load those.
1572#
1573# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001574function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001575 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001576 allowed=
1577 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1578 if [ -n "$allowed" ]; then
1579 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1580 echo " $allowed"
1581 echo " $f"
1582 return
1583 fi
1584 allowed="$f"
1585 done
1586
1587 allowed_files=
1588 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001589 for dir in device vendor product; do
1590 for f in $(test -d $dir && \
1591 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001592
1593 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1594 echo "including $f"; . "$f"
1595 else
1596 echo "ignoring $f, not in $allowed"
1597 fi
Jim Tanga881a252018-06-19 16:34:41 +08001598 done
1599 done
1600}
Kenny Root52aa81c2011-07-15 11:07:06 -07001601
Jim Tanga881a252018-06-19 16:34:41 +08001602validate_current_shell
1603source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001604addcompletions