blob: 389fbb7d767ddeea3ad0da48a9d1631f06fcb9de [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
257 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
258 # to ensure that the corresponding 'emulator' binaries are used.
259 case $(uname -s) in
260 Darwin)
261 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
262 ;;
263 Linux)
264 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
265 ;;
266 *)
267 ANDROID_EMULATOR_PREBUILTS=
268 ;;
269 esac
270 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700271 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200272 export ANDROID_EMULATOR_PREBUILTS
273 fi
274
Jim Tangb3fda302018-12-22 10:24:55 +0800275 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
276 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700277 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
278 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
279 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
280 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800281
Ryan Prichard8426a192019-07-15 18:05:29 -0700282 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800283
284 # out with the duplicate old
285 if [ -n $ANDROID_PYTHONPATH ]; then
286 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
287 fi
288 # and in with the new
289 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800290 if [ -n $VENDOR_PYTHONPATH ]; then
291 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
292 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800293 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800294
Colin Crosse97e6932017-06-30 16:01:45 -0700295 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
296 export JAVA_HOME=$ANDROID_JAVA_HOME
297 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
298 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
299 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500300
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800301 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700302 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
303 export OUT=$ANDROID_PRODUCT_OUT
304
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700305 unset ANDROID_HOST_OUT
306 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
307
Simran Basidd050ed2017-02-13 13:46:48 -0800308 unset ANDROID_HOST_OUT_TESTCASES
309 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
310
311 unset ANDROID_TARGET_OUT_TESTCASES
312 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
313
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800314 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700315 # TODO: fix the path
316 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
317}
318
319function printconfig()
320{
Christopher Ferris55257d22017-03-23 11:08:58 -0700321 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 if [ ! "$T" ]; then
323 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
324 return
325 fi
326 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327}
328
329function set_stuff_for_environment()
330{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800331 setpaths
332 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800334 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700335 # With this environment variable new GCC can apply colors to warnings/errors
336 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 -0700337}
338
339function set_sequence_number()
340{
Colin Cross88737132017-03-21 17:41:03 -0700341 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700342}
343
Makoto Onukida971062018-06-18 10:15:19 -0700344# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
345function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800346 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700347 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800348 *:"$cmd":*)
349 return 1
350 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700351 esac
352 return 0
353}
354
Kenny Root52aa81c2011-07-15 11:07:06 -0700355function addcompletions()
356{
357 local T dir f
358
Jim Tanga881a252018-06-19 16:34:41 +0800359 # Keep us from trying to run in something that's neither bash nor zsh.
360 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700361 return
362 fi
363
364 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800365 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700366 return
367 fi
368
Jim Tanga881a252018-06-19 16:34:41 +0800369 local completion_files=(
370 system/core/adb/adb.bash
371 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800372 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800373 )
Makoto Onukida971062018-06-18 10:15:19 -0700374 # Completion can be disabled selectively to allow users to use non-standard completion.
375 # e.g.
376 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
377 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800378 for f in ${completion_files[*]}; do
379 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700380 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700381 fi
382 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700383
Makoto Onukida971062018-06-18 10:15:19 -0700384 if should_add_completion bit ; then
385 complete -C "bit --tab" bit
386 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000387 if [ -z "$ZSH_VERSION" ]; then
388 # Doesn't work in zsh.
389 complete -o nospace -F _croot croot
390 fi
Jim Tanga881a252018-06-19 16:34:41 +0800391 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800392
dimitry73b84812018-12-11 18:06:00 +0100393 complete -F _complete_android_module_names gomod
394 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700395}
396
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700397function choosetype()
398{
399 echo "Build type choices are:"
400 echo " 1. release"
401 echo " 2. debug"
402 echo
403
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800404 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700405 DEFAULT_NUM=1
406 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700407
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800408 export TARGET_BUILD_TYPE=
409 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700410 while [ -z $TARGET_BUILD_TYPE ]
411 do
412 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800413 if [ -z "$1" ] ; then
414 read ANSWER
415 else
416 echo $1
417 ANSWER=$1
418 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700419 case $ANSWER in
420 "")
421 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
422 ;;
423 1)
424 export TARGET_BUILD_TYPE=release
425 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800426 release)
427 export TARGET_BUILD_TYPE=release
428 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700429 2)
430 export TARGET_BUILD_TYPE=debug
431 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800432 debug)
433 export TARGET_BUILD_TYPE=debug
434 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435 *)
436 echo
437 echo "I didn't understand your response. Please try again."
438 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700439 ;;
440 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800441 if [ -n "$1" ] ; then
442 break
443 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700444 done
445
Ying Wang08800fd2016-03-03 20:57:21 -0800446 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700447 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800448 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700449}
450
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800451#
452# This function isn't really right: It chooses a TARGET_PRODUCT
453# based on the list of boards. Usually, that gets you something
454# that kinda works with a generic product, but really, you should
455# pick a product by name.
456#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700457function chooseproduct()
458{
Christopher Ferris55257d22017-03-23 11:08:58 -0700459 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 if [ "x$TARGET_PRODUCT" != x ] ; then
461 default_value=$TARGET_PRODUCT
462 else
Ying Wang0a76df52015-06-08 11:57:26 -0700463 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700464 fi
465
Ying Wang08800fd2016-03-03 20:57:21 -0800466 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800467 export TARGET_PRODUCT=
468 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700469 while [ -z "$TARGET_PRODUCT" ]
470 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700471 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800472 if [ -z "$1" ] ; then
473 read ANSWER
474 else
475 echo $1
476 ANSWER=$1
477 fi
478
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700479 if [ -z "$ANSWER" ] ; then
480 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800481 else
482 if check_product $ANSWER
483 then
484 export TARGET_PRODUCT=$ANSWER
485 else
486 echo "** Not a valid product: $ANSWER"
487 fi
488 fi
489 if [ -n "$1" ] ; then
490 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700491 fi
492 done
493
Ying Wang08800fd2016-03-03 20:57:21 -0800494 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800496 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700497}
498
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499function choosevariant()
500{
501 echo "Variant choices are:"
502 local index=1
503 local v
504 for v in ${VARIANT_CHOICES[@]}
505 do
506 # The product name is the name of the directory containing
507 # the makefile we found, above.
508 echo " $index. $v"
509 index=$(($index+1))
510 done
511
512 local default_value=eng
513 local ANSWER
514
515 export TARGET_BUILD_VARIANT=
516 while [ -z "$TARGET_BUILD_VARIANT" ]
517 do
518 echo -n "Which would you like? [$default_value] "
519 if [ -z "$1" ] ; then
520 read ANSWER
521 else
522 echo $1
523 ANSWER=$1
524 fi
525
526 if [ -z "$ANSWER" ] ; then
527 export TARGET_BUILD_VARIANT=$default_value
528 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
529 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200530 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800531 fi
532 else
533 if check_variant $ANSWER
534 then
535 export TARGET_BUILD_VARIANT=$ANSWER
536 else
537 echo "** Not a valid variant: $ANSWER"
538 fi
539 fi
540 if [ -n "$1" ] ; then
541 break
542 fi
543 done
544}
545
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546function choosecombo()
547{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700548 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549
550 echo
551 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700552 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700553
554 echo
555 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700556 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557
558 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800559 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800562 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563}
564
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800565function add_lunch_combo()
566{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800567 if [ -n "$ZSH_VERSION" ]; then
568 echo -n "${funcfiletrace[1]}: "
569 else
570 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
571 fi
572 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 -0800573}
574
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700575function print_lunch_menu()
576{
577 local uname=$(uname)
Colin Crosscb8337d2019-09-23 12:52:32 -0700578 local choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 echo
580 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581 echo
582 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800583
584 local i=1
585 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700586 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800587 do
588 echo " $i. $choice"
589 i=$(($i+1))
590 done
591
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700592 echo
593}
594
595function lunch()
596{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 local answer
598
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800600 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700601 else
602 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700603 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800604 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 fi
606
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800607 local selection=
608
609 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700610 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700611 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800612 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
613 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800614 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700615 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800617 # array in zsh starts from 1 instead of 0.
618 if [ -n "$ZSH_VERSION" ]
619 then
620 selection=${choices[$(($answer))]}
621 else
622 selection=${choices[$(($answer-1))]}
623 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624 fi
Colin Cross88737132017-03-21 17:41:03 -0700625 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700627 fi
628
Joe Onoratoda12daf2010-06-09 18:18:31 -0700629 export TARGET_BUILD_APPS=
630
Colin Cross88737132017-03-21 17:41:03 -0700631 local product variant_and_version variant version
632
633 product=${selection%%-*} # Trim everything after first dash
634 variant_and_version=${selection#*-} # Trim everything up to first dash
635 if [ "$variant_and_version" != "$selection" ]; then
636 variant=${variant_and_version%%-*}
637 if [ "$variant" != "$variant_and_version" ]; then
638 version=${variant_and_version#*-}
639 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700640 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800641
Colin Cross88737132017-03-21 17:41:03 -0700642 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800643 then
644 echo
Colin Cross88737132017-03-21 17:41:03 -0700645 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700646 return 1
647 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648
Colin Cross88737132017-03-21 17:41:03 -0700649 TARGET_PRODUCT=$product \
650 TARGET_BUILD_VARIANT=$variant \
651 TARGET_PLATFORM_VERSION=$version \
652 build_build_var_cache
653 if [ $? -ne 0 ]
654 then
655 return 1
656 fi
657
658 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
659 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700660 if [ -n "$version" ]; then
661 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
662 else
663 unset TARGET_PLATFORM_VERSION
664 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700665 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700666
667 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800668
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700669 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800670 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800671 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700672}
673
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700674unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700675# Tab completion for lunch.
676function _lunch()
677{
678 local cur prev opts
679 COMPREPLY=()
680 cur="${COMP_WORDS[COMP_CWORD]}"
681 prev="${COMP_WORDS[COMP_CWORD-1]}"
682
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700683 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800684 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700685 fi
686
687 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700688 return 0
689}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700690
Joe Onoratoda12daf2010-06-09 18:18:31 -0700691# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700692# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700693function tapas()
694{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700695 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800696 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700697 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700698 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 -0800699 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 -0700700
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700701 if [ "$showHelp" != "" ]; then
702 $(gettop)/build/make/tapasHelp.sh
703 return
704 fi
705
Ying Wang67f02922012-08-22 10:25:20 -0700706 if [ $(echo $arch | wc -w) -gt 1 ]; then
707 echo "tapas: Error: Multiple build archs supplied: $arch"
708 return
709 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700710 if [ $(echo $variant | wc -w) -gt 1 ]; then
711 echo "tapas: Error: Multiple build variants supplied: $variant"
712 return
713 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700714 if [ $(echo $density | wc -w) -gt 1 ]; then
715 echo "tapas: Error: Multiple densities supplied: $density"
716 return
717 fi
Ying Wang67f02922012-08-22 10:25:20 -0700718
Ying Wang0a76df52015-06-08 11:57:26 -0700719 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700720 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700721 x86) product=aosp_x86;;
722 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700723 arm64) product=aosp_arm64;;
724 x86_64) product=aosp_x86_64;;
725 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700726 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700727 if [ -z "$variant" ]; then
728 variant=eng
729 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700730 if [ -z "$apps" ]; then
731 apps=all
732 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600733 if [ -z "$density" ]; then
734 density=alldpi
735 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736
Ying Wang67f02922012-08-22 10:25:20 -0700737 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700738 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700739 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700740 export TARGET_BUILD_TYPE=release
741 export TARGET_BUILD_APPS=$apps
742
Ying Wang08800fd2016-03-03 20:57:21 -0800743 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700744 set_stuff_for_environment
745 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800746 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700747}
748
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700749function gettop
750{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700751 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700752 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700753 # The following circumlocution ensures we remove symlinks from TOP.
754 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700755 else
756 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800757 # The following circumlocution (repeated below as well) ensures
758 # that we record the true directory name and not one that is
759 # faked up with symlink names.
760 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800762 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700763 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800765 \cd ..
synergyb112a402013-07-05 19:47:47 -0700766 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 done
Ying Wang9cd17642012-12-13 10:52:07 -0800768 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700769 if [ -f "$T/$TOPFILE" ]; then
770 echo $T
771 fi
772 fi
773 fi
774}
775
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776function croot()
777{
Christopher Ferris55257d22017-03-23 11:08:58 -0700778 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700779 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700780 if [ "$1" ]; then
781 \cd $(gettop)/$1
782 else
783 \cd $(gettop)
784 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700785 else
786 echo "Couldn't locate the top of the tree. Try setting TOP."
787 fi
788}
789
Anton Hanssonece9c482019-02-04 18:15:39 +0000790function _croot()
791{
792 local T=$(gettop)
793 if [ "$T" ]; then
794 local cur="${COMP_WORDS[COMP_CWORD]}"
795 k=0
796 for c in $(compgen -d ${T}/${cur}); do
797 COMPREPLY[k++]=${c#${T}/}/
798 done
799 fi
800}
801
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700802function cproj()
803{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700804 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700805 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700806 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700807 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
808 T=$PWD
809 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800810 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700811 return
812 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800813 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700814 done
Ying Wang9cd17642012-12-13 10:52:07 -0800815 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700816 echo "can't find Android.mk"
817}
818
Daniel Sandler47e0a882013-07-30 13:23:52 -0400819# simplified version of ps; output in the form
820# <pid> <procname>
821function qpid() {
822 local prepend=''
823 local append=''
824 if [ "$1" = "--exact" ]; then
825 prepend=' '
826 append='$'
827 shift
828 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800829 echo "usage: qpid [[--exact] <process name|pid>"
830 return 255
831 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400832
833 local EXE="$1"
834 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800835 qpid | \grep "$prepend$EXE$append"
836 else
837 adb shell ps \
838 | tr -d '\r' \
839 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
840 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400841}
842
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800843# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700844# that has the core-file-size limit set correctly
845#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800846# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700847# if its core-file-size limit is not set already.
848# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
849
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800850function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700851{
Ying Wang08800fd2016-03-03 20:57:21 -0800852 echo "Getting root...";
853 adb root;
854 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700855
Ying Wang08800fd2016-03-03 20:57:21 -0800856 echo "Remounting root partition read-write...";
857 adb shell mount -w -o remount -t rootfs rootfs;
858 sleep 1;
859 adb wait-for-device;
860 adb shell mkdir -p /cores;
861 adb shell mount -t tmpfs tmpfs /cores;
862 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700863
Ying Wang08800fd2016-03-03 20:57:21 -0800864 echo "Granting SELinux permission to dump in /cores...";
865 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866
Ying Wang08800fd2016-03-03 20:57:21 -0800867 echo "Set core pattern.";
868 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869
Ying Wang08800fd2016-03-03 20:57:21 -0800870 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700871}
872
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800873# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700874# $1 = PID of process (e.g., $(pid mediaserver))
875#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800876# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700877# dump to actually be generated.
878
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800879function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700880{
Ying Wang08800fd2016-03-03 20:57:21 -0800881 local PID=$1;
882 if [ -z "$PID" ]; then
883 printf "Expecting a PID!\n";
884 return;
885 fi;
886 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800887 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700888}
889
890# core - send SIGV and pull the core for process
891# $1 = PID of process (e.g., $(pid mediaserver))
892#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800893# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700894# enabled globally.
895
896function core()
897{
Ying Wang08800fd2016-03-03 20:57:21 -0800898 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700899
Ying Wang08800fd2016-03-03 20:57:21 -0800900 if [ -z "$PID" ]; then
901 printf "Expecting a PID!\n";
902 return;
903 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700904
Ying Wang08800fd2016-03-03 20:57:21 -0800905 local CORENAME=core.$PID;
906 local COREPATH=/cores/$CORENAME;
907 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700908
Ying Wang08800fd2016-03-03 20:57:21 -0800909 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700910
Ying Wang08800fd2016-03-03 20:57:21 -0800911 local done=0;
912 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
913 printf "\tSending SIG%s to %d...\n" $SIG $PID;
914 adb shell kill -$SIG $PID;
915 sleep 1;
916 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700917
Ying Wang08800fd2016-03-03 20:57:21 -0800918 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
919 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700920}
921
Christopher Tate744ee802009-11-12 15:33:08 -0800922# systemstack - dump the current stack trace of all threads in the system process
923# to the usual ANR traces file
924function systemstack()
925{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800926 stacks system_server
927}
928
Michael Wrightaeed7212014-06-19 19:58:12 -0700929# Read the ELF header from /proc/$PID/exe to determine if the process is
930# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800931function is64bit()
932{
933 local PID="$1"
934 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800935 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800936 echo "64"
937 else
938 echo ""
939 fi
940 else
941 echo ""
942 fi
943}
944
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700945case `uname -s` in
946 Darwin)
947 function sgrep()
948 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100949 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 -0400950 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700951 }
952
953 ;;
954 *)
955 function sgrep()
956 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100957 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 -0400958 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700959 }
960 ;;
961esac
962
Raghu Gandham8da43102012-07-25 19:57:22 -0700963function gettargetarch
964{
965 get_build_var TARGET_ARCH
966}
967
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700968function ggrep()
969{
Mike Frysinger5e479732015-09-22 18:13:48 -0400970 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
971 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700972}
973
Orion Hodson831472d2019-10-25 11:35:15 +0100974function gogrep()
975{
976 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
977 -exec grep --color -n "$@" {} +
978}
979
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700980function jgrep()
981{
Mike Frysinger5e479732015-09-22 18:13:48 -0400982 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
983 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700984}
985
986function cgrep()
987{
Mike Frysinger5e479732015-09-22 18:13:48 -0400988 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' \) \
989 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700990}
991
992function resgrep()
993{
Christopher Ferris55257d22017-03-23 11:08:58 -0700994 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400995 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
996 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
997 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700998}
999
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001000function mangrep()
1001{
Mike Frysinger5e479732015-09-22 18:13:48 -04001002 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1003 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001004}
1005
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001006function owngrep()
1007{
1008 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1009 -exec grep --color -n "$@" {} +
1010}
1011
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001012function sepgrep()
1013{
Mike Frysinger5e479732015-09-22 18:13:48 -04001014 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1015 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001016}
1017
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001018function rcgrep()
1019{
Mike Frysinger5e479732015-09-22 18:13:48 -04001020 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1021 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001022}
1023
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001024case `uname -s` in
1025 Darwin)
1026 function mgrep()
1027 {
Orion Hodson831472d2019-10-25 11:35:15 +01001028 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 -04001029 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001030 }
1031
1032 function treegrep()
1033 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001034 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 -04001035 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001036 }
1037
1038 ;;
1039 *)
1040 function mgrep()
1041 {
Orion Hodson831472d2019-10-25 11:35:15 +01001042 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 -04001043 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044 }
1045
1046 function treegrep()
1047 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001048 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 -04001049 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001050 }
1051
1052 ;;
1053esac
1054
1055function getprebuilt
1056{
1057 get_abs_build_var ANDROID_PREBUILTS
1058}
1059
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001060function tracedmdump()
1061{
Christopher Ferris55257d22017-03-23 11:08:58 -07001062 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001063 if [ ! "$T" ]; then
1064 echo "Couldn't locate the top of the tree. Try setting TOP."
1065 return
1066 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001067 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001068 local arch=$(gettargetarch)
1069 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001070
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001071 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001072 if [ ! "$TRACE" ] ; then
1073 echo "usage: tracedmdump tracename"
1074 return
1075 fi
1076
Jack Veenstra60116fc2009-04-09 18:12:34 -07001077 if [ ! -r "$KERNEL" ] ; then
1078 echo "Error: cannot find kernel: '$KERNEL'"
1079 return
1080 fi
1081
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001082 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001083 if [ "$BASETRACE" = "$TRACE" ] ; then
1084 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1085 fi
1086
1087 echo "post-processing traces..."
1088 rm -f $TRACE/qtrace.dexlist
1089 post_trace $TRACE
1090 if [ $? -ne 0 ]; then
1091 echo "***"
1092 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1093 echo "***"
1094 return
1095 fi
1096 echo "generating dexlist output..."
1097 /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
1098 echo "generating dmtrace data..."
1099 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1100 echo "generating html file..."
1101 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1102 echo "done, see $TRACE/dmtrace.html for details"
1103 echo "or run:"
1104 echo " traceview $TRACE/dmtrace"
1105}
1106
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001107# communicate with a running device or emulator, set up necessary state,
1108# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001109function runhat()
1110{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001111 # process standard adb options
1112 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001113 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001114 adbTarget=$1
1115 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001116 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001117 adbTarget="$1 $2"
1118 shift 2
1119 fi
1120 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001121 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001122
1123 # runhat options
1124 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001125
1126 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001127 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001128 return
1129 fi
1130
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001131 # confirm hat is available
1132 if [ -z $(which hat) ]; then
1133 echo "hat is not available in this configuration."
1134 return
1135 fi
1136
Andy McFaddenb6289852010-07-12 08:00:19 -07001137 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001138 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001139 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001140 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001141 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001142 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001143 echo -n "> "
1144 read
1145
The Android Open Source Project88b60792009-03-03 19:28:42 -08001146 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001147
The Android Open Source Project88b60792009-03-03 19:28:42 -08001148 echo "Retrieving file $devFile..."
1149 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001150
The Android Open Source Project88b60792009-03-03 19:28:42 -08001151 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001152
The Android Open Source Project88b60792009-03-03 19:28:42 -08001153 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001154 echo "View the output by pointing your browser at http://localhost:7000/"
1155 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001156 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001157}
1158
1159function getbugreports()
1160{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001161 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001162
1163 if [ ! "$reports" ]; then
1164 echo "Could not locate any bugreports."
1165 return
1166 fi
1167
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001168 local report
1169 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001170 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001171 echo "/sdcard/bugreports/${report}"
1172 adb pull /sdcard/bugreports/${report} ${report}
1173 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001174 done
1175}
1176
Victoria Lease1b296b42012-08-21 15:44:06 -07001177function getsdcardpath()
1178{
1179 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1180}
1181
1182function getscreenshotpath()
1183{
1184 echo "$(getsdcardpath)/Pictures/Screenshots"
1185}
1186
1187function getlastscreenshot()
1188{
1189 local screenshot_path=$(getscreenshotpath)
1190 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1191 if [ "$screenshot" = "" ]; then
1192 echo "No screenshots found."
1193 return
1194 fi
1195 echo "${screenshot}"
1196 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1197}
1198
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001199function startviewserver()
1200{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001201 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001202 if [ $# -gt 0 ]; then
1203 port=$1
1204 fi
1205 adb shell service call window 1 i32 $port
1206}
1207
1208function stopviewserver()
1209{
1210 adb shell service call window 2
1211}
1212
1213function isviewserverstarted()
1214{
1215 adb shell service call window 3
1216}
1217
Romain Guyb84049a2010-10-04 16:56:11 -07001218function key_home()
1219{
1220 adb shell input keyevent 3
1221}
1222
1223function key_back()
1224{
1225 adb shell input keyevent 4
1226}
1227
1228function key_menu()
1229{
1230 adb shell input keyevent 82
1231}
1232
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001233function smoketest()
1234{
1235 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1236 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1237 return
1238 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001239 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001240 if [ ! "$T" ]; then
1241 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1242 return
1243 fi
1244
Ying Wang9cd17642012-12-13 10:52:07 -08001245 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001246 adb uninstall com.android.smoketest > /dev/null &&
1247 adb uninstall com.android.smoketest.tests > /dev/null &&
1248 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1249 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1250 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1251}
1252
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001253# simple shortcut to the runtest command
1254function runtest()
1255{
Christopher Ferris55257d22017-03-23 11:08:58 -07001256 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001257 if [ ! "$T" ]; then
1258 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1259 return
1260 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001261 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001262}
1263
The Android Open Source Project88b60792009-03-03 19:28:42 -08001264function godir () {
1265 if [[ -z "$1" ]]; then
1266 echo "Usage: godir <regex>"
1267 return
1268 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001269 local T=$(gettop)
1270 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001271 if [ ! "$OUT_DIR" = "" ]; then
1272 mkdir -p $OUT_DIR
1273 FILELIST=$OUT_DIR/filelist
1274 else
1275 FILELIST=$T/filelist
1276 fi
1277 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001278 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001279 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001280 echo " Done"
1281 echo ""
1282 fi
1283 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001284 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001285 if [[ ${#lines[@]} = 0 ]]; then
1286 echo "Not found"
1287 return
1288 fi
1289 local pathname
1290 local choice
1291 if [[ ${#lines[@]} > 1 ]]; then
1292 while [[ -z "$pathname" ]]; do
1293 local index=1
1294 local line
1295 for line in ${lines[@]}; do
1296 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001297 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001298 done
1299 echo
1300 echo -n "Select one: "
1301 unset choice
1302 read choice
1303 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1304 echo "Invalid choice"
1305 continue
1306 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001307 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001308 done
1309 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001310 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001311 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001312 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001313}
1314
Steven Moreland62054a42018-12-06 10:11:40 -08001315# Update module-info.json in out.
1316function refreshmod() {
1317 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1318 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1319 return 1
1320 fi
1321
1322 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1323
1324 # for the output of the next command
1325 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1326
1327 # Note, can't use absolute path because of the way make works.
1328 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1329 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1330}
1331
1332# List all modules for the current device, as cached in module-info.json. If any build change is
1333# made and it should be reflected in the output, you should run 'refreshmod' first.
1334function allmod() {
1335 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1336 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1337 return 1
1338 fi
1339
1340 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1341 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1342 refreshmod || return 1
1343 fi
1344
LuK1337b6a78192020-01-12 03:12:17 +01001345 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 -08001346}
1347
Rett Berg78d1c932019-01-24 14:34:23 -08001348# 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 -08001349# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001350function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001351 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1352 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1353 return 1
1354 fi
1355
1356 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001357 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001358 return 1
1359 fi
1360
1361 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1362 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1363 refreshmod || return 1
1364 fi
1365
1366 local relpath=$(python -c "import json, os
1367module = '$1'
1368module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1369if module not in module_info:
1370 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001371print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001372
1373 if [ -z "$relpath" ]; then
1374 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1375 return 1
1376 else
Rett Berg78d1c932019-01-24 14:34:23 -08001377 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001378 fi
1379}
1380
Rett Berg78d1c932019-01-24 14:34:23 -08001381# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1382# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1383function gomod() {
1384 if [[ $# -ne 1 ]]; then
1385 echo "usage: gomod <module>" >&2
1386 return 1
1387 fi
1388
1389 local path="$(pathmod $@)"
1390 if [ -z "$path" ]; then
1391 return 1
1392 fi
1393 cd $path
1394}
1395
dimitry73b84812018-12-11 18:06:00 +01001396function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001397 local word=${COMP_WORDS[COMP_CWORD]}
1398 COMPREPLY=( $(allmod | grep -E "^$word") )
1399}
1400
Alex Rayf0d08eb2013-03-08 15:15:06 -08001401# Print colored exit condition
1402function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001403 "$@"
1404 local retval=$?
1405 if [ $retval -ne 0 ]
1406 then
Jacky Cao89483b82015-05-15 22:12:53 +08001407 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001408 else
Jacky Cao89483b82015-05-15 22:12:53 +08001409 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001410 fi
1411 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001412}
1413
Ying Wanged21d4c2014-08-24 22:14:19 -07001414function get_make_command()
1415{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001416 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1417 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001418 # Always use the real make if -C is passed in
1419 for arg in "$@"; do
1420 if [[ $arg == -C* ]]; then
1421 echo command make
1422 return
1423 fi
1424 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001425 echo build/soong/soong_ui.bash --make-mode
1426 else
1427 echo command make
1428 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001429}
1430
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001431function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001432{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001433 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1434 "$@"
1435 return $?
1436 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001437 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001438 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001439 local ret=$?
1440 local end_time=$(date +"%s")
1441 local tdiff=$(($end_time-$start_time))
1442 local hours=$(($tdiff / 3600 ))
1443 local mins=$((($tdiff % 3600) / 60))
1444 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001445 local ncolors=$(tput colors 2>/dev/null)
1446 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001447 color_failed=$'\E'"[0;31m"
1448 color_success=$'\E'"[0;32m"
1449 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001450 else
1451 color_failed=""
1452 color_success=""
1453 color_reset=""
1454 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001455 echo
1456 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001457 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001458 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001459 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001460 fi
1461 if [ $hours -gt 0 ] ; then
1462 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1463 elif [ $mins -gt 0 ] ; then
1464 printf "(%02g:%02g (mm:ss))" $mins $secs
1465 elif [ $secs -gt 0 ] ; then
1466 printf "(%s seconds)" $secs
1467 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001468 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001469 echo
1470 return $ret
1471}
1472
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001473function _trigger_build()
1474(
1475 local -r bc="$1"; shift
1476 if T="$(gettop)"; then
1477 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1478 else
1479 echo "Couldn't locate the top of the tree. Try setting TOP."
1480 fi
1481)
1482
1483function m()
1484(
1485 _trigger_build "all-modules" "$@"
1486)
1487
1488function mm()
1489(
1490 _trigger_build "modules-in-a-dir-no-deps" "$@"
1491)
1492
1493function mmm()
1494(
1495 _trigger_build "modules-in-dirs-no-deps" "$@"
1496)
1497
1498function mma()
1499(
1500 _trigger_build "modules-in-a-dir" "$@"
1501)
1502
1503function mmma()
1504(
1505 _trigger_build "modules-in-dirs" "$@"
1506)
1507
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001508function make()
1509{
Dan Willemsene9842242017-07-28 13:00:13 -07001510 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001511}
1512
David Zeuthen1b126ff2015-09-30 17:10:48 -04001513function provision()
1514{
1515 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1516 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1517 return 1
1518 fi
1519 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1520 echo "There is no provisioning script for the device." >&2
1521 return 1
1522 fi
1523
1524 # Check if user really wants to do this.
1525 if [ "$1" = "--no-confirmation" ]; then
1526 shift 1
1527 else
1528 echo "This action will reflash your device."
1529 echo ""
1530 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1531 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001532 echo -n "Are you sure you want to do this (yes/no)? "
1533 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001534 if [[ "${REPLY}" != "yes" ]] ; then
1535 echo "Not taking any action. Exiting." >&2
1536 return 1
1537 fi
1538 fi
1539 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1540}
1541
Jim Tanga881a252018-06-19 16:34:41 +08001542# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001543function enable_zsh_completion() {
1544 # Don't override user's options if bash-style completion is already enabled.
1545 if ! declare -f complete >/dev/null; then
1546 autoload -U compinit && compinit
1547 autoload -U bashcompinit && bashcompinit
1548 fi
Jim Tanga881a252018-06-19 16:34:41 +08001549}
1550
1551function validate_current_shell() {
1552 local current_sh="$(ps -o command -p $$)"
1553 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001554 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001555 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001556 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001557 *zsh*)
1558 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001559 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001560 *)
Jim Tanga881a252018-06-19 16:34:41 +08001561 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 -07001562 ;;
1563 esac
Jim Tanga881a252018-06-19 16:34:41 +08001564}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001565
1566# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001567# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1568# load those.
1569#
1570# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001571function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001572 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001573 allowed=
1574 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1575 if [ -n "$allowed" ]; then
1576 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1577 echo " $allowed"
1578 echo " $f"
1579 return
1580 fi
1581 allowed="$f"
1582 done
1583
1584 allowed_files=
1585 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001586 for dir in device vendor product; do
1587 for f in $(test -d $dir && \
1588 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001589
1590 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1591 echo "including $f"; . "$f"
1592 else
1593 echo "ignoring $f, not in $allowed"
1594 fi
Jim Tanga881a252018-06-19 16:34:41 +08001595 done
1596 done
1597}
Kenny Root52aa81c2011-07-15 11:07:06 -07001598
Jim Tanga881a252018-06-19 16:34:41 +08001599validate_current_shell
1600source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001601addcompletions