blob: 8699a1988d084e95fa9a940ab8e7080686be38d4 [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.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [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 Morelandab25c7a2020-04-09 12:05:34 -070036- refreshmod: Refresh list of modules for allmod/gomod/pathmod.
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.
Matt Alexanderd9271832020-05-19 00:34:20 +000054function build_build_var_cache() {
Christopher Ferris55257d22017-03-23 11:08:58 -070055 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080056 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080057 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' ' '`)
58 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 -080059 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080060 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080061 --vars="${cached_vars[*]}" \
62 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070063 --var-prefix=var_cache_ \
64 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080065 local ret=$?
Matt Alexanderd9271832020-05-19 00:34:20 +000066 if [ $ret -ne 0 ]; then
Ying Wang08800fd2016-03-03 20:57:21 -080067 unset build_dicts_script
68 return $ret
69 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070070 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080071 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080072 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080073 unset build_dicts_script
Matt Alexanderd9271832020-05-19 00:34:20 +000074 if [ $ret -ne 0 ]; then
Ying Wang08800fd2016-03-03 20:57:21 -080075 return $ret
76 fi
77 BUILD_VAR_CACHE_READY="true"
78}
79
Ying Wangf0cb3972016-03-04 13:56:23 -080080# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080081# to get build variables not listed in this script.
Matt Alexanderd9271832020-05-19 00:34:20 +000082function destroy_build_var_cache() {
Ying Wang08800fd2016-03-03 20:57:21 -080083 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070084 local v
Ying Wang08800fd2016-03-03 20:57:21 -080085 for v in $cached_vars; do
86 unset var_cache_$v
87 done
88 unset cached_vars
89 for v in $cached_abs_vars; do
90 unset abs_var_cache_$v
91 done
92 unset cached_abs_vars
93}
94
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070095# Get the value of a build variable as an absolute path.
Matt Alexanderd9271832020-05-19 00:34:20 +000096function get_abs_build_var() {
97 if [ "$BUILD_VAR_CACHE_READY" == "true" ]; then
Vishwath Mohan7d35f002016-03-11 10:00:40 -080098 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9271832020-05-19 00:34:20 +000099 return
Ying Wang08800fd2016-03-03 20:57:21 -0800100 fi
101
Christopher Ferris55257d22017-03-23 11:08:58 -0700102 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700103 if [ ! "$T" ]; then
104 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
105 return
106 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700107 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108}
109
110# Get the exact value of a build variable.
Matt Alexanderd9271832020-05-19 00:34:20 +0000111function get_build_var() {
112 if [ "$BUILD_VAR_CACHE_READY" == "true" ]; then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800113 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100114 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800115 fi
116
Christopher Ferris55257d22017-03-23 11:08:58 -0700117 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700118 if [ ! "$T" ]; then
119 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100120 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700121 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700122 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800123}
124
125# check to see if the supplied product is one we can build
Matt Alexanderd9271832020-05-19 00:34:20 +0000126function check_product() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700127 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800128 if [ ! "$T" ]; then
129 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
130 return
131 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700132 TARGET_PRODUCT=$1 \
133 TARGET_BUILD_VARIANT= \
134 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700135 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800136 get_build_var TARGET_DEVICE > /dev/null
137 # hide successful answers, but allow the errors to show
138}
139
140VARIANT_CHOICES=(user userdebug eng)
141
142# check to see if the supplied variant is valid
Matt Alexanderd9271832020-05-19 00:34:20 +0000143function check_variant() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700144 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800145 for v in ${VARIANT_CHOICES[@]}
146 do
Matt Alexanderd9271832020-05-19 00:34:20 +0000147 if [ "$v" == "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800148 return 0
149 fi
150 done
151 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700152}
153
Matt Alexanderd9271832020-05-19 00:34:20 +0000154function setpaths() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700155 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700156 if [ ! "$T" ]; then
157 echo "Couldn't locate the top of the tree. Try setting TOP."
158 return
159 fi
160
161 ##################################################################
162 # #
163 # Read me before you modify this code #
164 # #
165 # This function sets ANDROID_BUILD_PATHS to what it is adding #
166 # to PATH, and the next time it is run, it removes that from #
167 # PATH. This is required so lunch can be run more than once #
168 # and still have working paths. #
169 # #
170 ##################################################################
171
Raphael Mollc639c782011-06-20 17:25:01 -0700172 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
173 # due to "C:\Program Files" being in the path.
174
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700175 # out with the old
Matt Alexanderd9271832020-05-19 00:34:20 +0000176 if [ -n "$ANDROID_BUILD_PATHS" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700177 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
178 fi
Matt Alexanderd9271832020-05-19 00:34:20 +0000179 if [ -n "$ANDROID_PRE_BUILD_PATHS" ]; then
Doug Zongker29034982011-04-22 08:16:56 -0700180 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800181 # strip leading ':', if any
182 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500183 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700184
185 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700186 local prebuiltdir=$(getprebuilt)
187 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700188
Ben Cheng8bc4c432012-11-16 13:29:13 -0800189 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700190 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
191 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800192 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800193
Raphael Mollc639c782011-06-20 17:25:01 -0700194 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800195 export ANDROID_TOOLCHAIN=
196 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200197 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700198 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200199 case $ARCH in
Matt Alexanderd9271832020-05-19 00:34:20 +0000200 x86)
201 toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700202 ;;
Matt Alexanderd9271832020-05-19 00:34:20 +0000203 x86_64)
204 toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400205 ;;
Matt Alexanderd9271832020-05-19 00:34:20 +0000206 arm)
207 toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200208 ;;
Matt Alexanderd9271832020-05-19 00:34:20 +0000209 arm64)
210 toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin
211 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700212 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200213 *)
214 echo "Can't find toolchain for unknown architecture: $ARCH"
215 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700216 ;;
217 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700218 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800219 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700220 fi
Raphael732936d2011-06-22 14:35:32 -0700221
Christopher Ferris55257d22017-03-23 11:08:58 -0700222 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800223 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
224 fi
225
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700226 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700227
228 # add kernel specific binaries
229 case $(uname -s) in
230 Linux)
231 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
232 ;;
233 *)
234 ;;
235 esac
236
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400237 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
238 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
239 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
240 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700241 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
242
243 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
244 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
245 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200246
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800247 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
248 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
249
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200250 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
251 # to ensure that the corresponding 'emulator' binaries are used.
252 case $(uname -s) in
253 Darwin)
254 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
255 ;;
256 Linux)
257 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
258 ;;
259 *)
260 ANDROID_EMULATOR_PREBUILTS=
261 ;;
262 esac
263 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700264 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200265 export ANDROID_EMULATOR_PREBUILTS
266 fi
267
Jim Tangb3fda302018-12-22 10:24:55 +0800268 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
269 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700270 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
271 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
272 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
273 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800274
Ryan Prichard8426a192019-07-15 18:05:29 -0700275 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800276
277 # out with the duplicate old
278 if [ -n $ANDROID_PYTHONPATH ]; then
279 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
280 fi
281 # and in with the new
282 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800283 if [ -n $VENDOR_PYTHONPATH ]; then
284 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
285 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800286 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800287
Colin Crosse97e6932017-06-30 16:01:45 -0700288 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
289 export JAVA_HOME=$ANDROID_JAVA_HOME
290 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
291 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
292 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500293
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800294 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700295 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
296 export OUT=$ANDROID_PRODUCT_OUT
297
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700298 unset ANDROID_HOST_OUT
299 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
300
Simran Basidd050ed2017-02-13 13:46:48 -0800301 unset ANDROID_HOST_OUT_TESTCASES
302 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
303
304 unset ANDROID_TARGET_OUT_TESTCASES
305 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
306
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800307 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700308 # TODO: fix the path
309 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
310}
311
Matt Alexanderd9271832020-05-19 00:34:20 +0000312function printconfig() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700313 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800314 if [ ! "$T" ]; then
315 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
316 return
317 fi
318 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700319}
320
Matt Alexanderd9271832020-05-19 00:34:20 +0000321function set_stuff_for_environment() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 setpaths
323 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700324
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700326 # With this environment variable new GCC can apply colors to warnings/errors
327 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 -0700328}
329
Matt Alexanderd9271832020-05-19 00:34:20 +0000330function set_sequence_number() {
Colin Cross88737132017-03-21 17:41:03 -0700331 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700332}
333
Makoto Onukida971062018-06-18 10:15:19 -0700334# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
335function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800336 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700337 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800338 *:"$cmd":*)
339 return 1
340 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700341 esac
342 return 0
343}
344
Matt Alexanderd9271832020-05-19 00:34:20 +0000345function addcompletions() {
Kenny Root52aa81c2011-07-15 11:07:06 -0700346 local T dir f
347
Jim Tanga881a252018-06-19 16:34:41 +0800348 # Keep us from trying to run in something that's neither bash nor zsh.
349 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700350 return
351 fi
352
353 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800354 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700355 return
356 fi
357
Jim Tanga881a252018-06-19 16:34:41 +0800358 local completion_files=(
359 system/core/adb/adb.bash
360 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800361 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800362 )
Makoto Onukida971062018-06-18 10:15:19 -0700363 # Completion can be disabled selectively to allow users to use non-standard completion.
364 # e.g.
365 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
366 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800367 for f in ${completion_files[*]}; do
368 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700369 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700370 fi
371 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700372
Matt Alexanderd9271832020-05-19 00:34:20 +0000373 if should_add_completion bit; then
Makoto Onukida971062018-06-18 10:15:19 -0700374 complete -C "bit --tab" bit
375 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000376 if [ -z "$ZSH_VERSION" ]; then
377 # Doesn't work in zsh.
378 complete -o nospace -F _croot croot
379 fi
Jim Tanga881a252018-06-19 16:34:41 +0800380 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800381
dimitry73b84812018-12-11 18:06:00 +0100382 complete -F _complete_android_module_names gomod
383 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700384}
385
Matt Alexanderd9271832020-05-19 00:34:20 +0000386function choosetype() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700387 echo "Build type choices are:"
388 echo " 1. release"
389 echo " 2. debug"
390 echo
391
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800392 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700393 DEFAULT_NUM=1
394 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700395
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800396 export TARGET_BUILD_TYPE=
397 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700398 while [ -z $TARGET_BUILD_TYPE ]
399 do
400 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9271832020-05-19 00:34:20 +0000401 if [ -z "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800402 read ANSWER
403 else
404 echo $1
405 ANSWER=$1
406 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700407 case $ANSWER in
408 "")
409 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
410 ;;
411 1)
412 export TARGET_BUILD_TYPE=release
413 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800414 release)
415 export TARGET_BUILD_TYPE=release
416 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700417 2)
418 export TARGET_BUILD_TYPE=debug
419 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800420 debug)
421 export TARGET_BUILD_TYPE=debug
422 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700423 *)
424 echo
425 echo "I didn't understand your response. Please try again."
426 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700427 ;;
428 esac
Matt Alexanderd9271832020-05-19 00:34:20 +0000429 if [ -n "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800430 break
431 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 done
433
Ying Wang08800fd2016-03-03 20:57:21 -0800434 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800436 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700437}
438
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800439#
440# This function isn't really right: It chooses a TARGET_PRODUCT
441# based on the list of boards. Usually, that gets you something
442# that kinda works with a generic product, but really, you should
443# pick a product by name.
444#
Matt Alexanderd9271832020-05-19 00:34:20 +0000445function chooseproduct() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700446 local default_value
Matt Alexanderd9271832020-05-19 00:34:20 +0000447 if [ "x$TARGET_PRODUCT" != x ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700448 default_value=$TARGET_PRODUCT
449 else
Ying Wang0a76df52015-06-08 11:57:26 -0700450 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700451 fi
452
Ying Wang08800fd2016-03-03 20:57:21 -0800453 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800454 export TARGET_PRODUCT=
455 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456 while [ -z "$TARGET_PRODUCT" ]
457 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700458 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9271832020-05-19 00:34:20 +0000459 if [ -z "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800460 read ANSWER
461 else
462 echo $1
463 ANSWER=$1
464 fi
465
Matt Alexanderd9271832020-05-19 00:34:20 +0000466 if [ -z "$ANSWER" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468 else
Matt Alexanderd9271832020-05-19 00:34:20 +0000469 if check_product $ANSWER; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800470 export TARGET_PRODUCT=$ANSWER
471 else
472 echo "** Not a valid product: $ANSWER"
473 fi
474 fi
Matt Alexanderd9271832020-05-19 00:34:20 +0000475 if [ -n "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800476 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 fi
478 done
479
Ying Wang08800fd2016-03-03 20:57:21 -0800480 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800482 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700483}
484
Matt Alexanderd9271832020-05-19 00:34:20 +0000485function choosevariant() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800486 echo "Variant choices are:"
487 local index=1
488 local v
489 for v in ${VARIANT_CHOICES[@]}
490 do
491 # The product name is the name of the directory containing
492 # the makefile we found, above.
493 echo " $index. $v"
494 index=$(($index+1))
495 done
496
497 local default_value=eng
498 local ANSWER
499
500 export TARGET_BUILD_VARIANT=
501 while [ -z "$TARGET_BUILD_VARIANT" ]
502 do
503 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9271832020-05-19 00:34:20 +0000504 if [ -z "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800505 read ANSWER
506 else
507 echo $1
508 ANSWER=$1
509 fi
510
Matt Alexanderd9271832020-05-19 00:34:20 +0000511 if [ -z "$ANSWER" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9271832020-05-19 00:34:20 +0000513 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$"); then
514 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ]; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200515 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800516 fi
517 else
Matt Alexanderd9271832020-05-19 00:34:20 +0000518 if check_variant $ANSWER; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800519 export TARGET_BUILD_VARIANT=$ANSWER
520 else
521 echo "** Not a valid variant: $ANSWER"
522 fi
523 fi
Matt Alexanderd9271832020-05-19 00:34:20 +0000524 if [ -n "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800525 break
526 fi
527 done
528}
529
Matt Alexanderd9271832020-05-19 00:34:20 +0000530function choosecombo() {
Jeff Browne33ba4c2011-07-11 22:11:46 -0700531 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700532
533 echo
534 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700535 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700536
537 echo
538 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700539 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800540
541 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800542 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700543 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800544 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800545 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546}
547
Matt Alexanderd9271832020-05-19 00:34:20 +0000548function add_lunch_combo() {
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800549 if [ -n "$ZSH_VERSION" ]; then
550 echo -n "${funcfiletrace[1]}: "
551 else
552 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
553 fi
554 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 -0800555}
556
Matt Alexanderd9271832020-05-19 00:34:20 +0000557function print_lunch_menu() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700558 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100559 local choices
560 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
561 local ret=$?
562
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 echo
564 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700565 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100566
Matt Alexanderd9271832020-05-19 00:34:20 +0000567 if [ $ret -ne 0 ]; then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100568 echo "Warning: Cannot display lunch menu."
569 echo
570 echo "Note: You can invoke lunch with an explicit target:"
571 echo
572 echo " usage: lunch [target]" >&2
573 echo
574 return
575 fi
576
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578
579 local i=1
580 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700581 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 do
583 echo " $i. $choice"
584 i=$(($i+1))
585 done
586
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 echo
588}
589
Matt Alexanderd9271832020-05-19 00:34:20 +0000590function lunch() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800591 local answer
592
Steven Moreland92793dc2020-02-25 18:30:18 -0800593 if [[ $# -gt 1 ]]; then
594 echo "usage: lunch [target]" >&2
595 return 1
596 fi
597
598 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 else
601 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700602 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604 fi
605
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800606 local selection=
607
Matt Alexanderd9271832020-05-19 00:34:20 +0000608 if [ -z "$answer" ]; then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700609 selection=aosp_arm-eng
Matt Alexanderd9271832020-05-19 00:34:20 +0000610 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$"); then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800611 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9271832020-05-19 00:34:20 +0000612 if [ $answer -le ${#choices[@]} ]; then
Jim Tang0e3397b2018-10-03 18:25:50 +0800613 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9271832020-05-19 00:34:20 +0000614 if [ -n "$ZSH_VERSION" ]; then
Jim Tang0e3397b2018-10-03 18:25:50 +0800615 selection=${choices[$(($answer))]}
616 else
617 selection=${choices[$(($answer-1))]}
618 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 fi
Colin Cross88737132017-03-21 17:41:03 -0700620 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700622 fi
623
Joe Onoratoda12daf2010-06-09 18:18:31 -0700624 export TARGET_BUILD_APPS=
625
Colin Cross88737132017-03-21 17:41:03 -0700626 local product variant_and_version variant version
627
628 product=${selection%%-*} # Trim everything after first dash
629 variant_and_version=${selection#*-} # Trim everything up to first dash
630 if [ "$variant_and_version" != "$selection" ]; then
631 variant=${variant_and_version%%-*}
632 if [ "$variant" != "$variant_and_version" ]; then
633 version=${variant_and_version#*-}
634 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Matt Alexanderd9271832020-05-19 00:34:20 +0000637 if [ -z "$product" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800638 echo
Colin Cross88737132017-03-21 17:41:03 -0700639 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700640 return 1
641 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642
Colin Cross88737132017-03-21 17:41:03 -0700643 TARGET_PRODUCT=$product \
644 TARGET_BUILD_VARIANT=$variant \
645 TARGET_PLATFORM_VERSION=$version \
646 build_build_var_cache
Matt Alexanderd9271832020-05-19 00:34:20 +0000647 if [ $? -ne 0 ]; then
Colin Cross88737132017-03-21 17:41:03 -0700648 return 1
649 fi
650
651 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
652 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700653 if [ -n "$version" ]; then
654 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
655 else
656 unset TARGET_PLATFORM_VERSION
657 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700658 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700659
660 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800661
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800663 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800664 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665}
666
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700667unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700668# Tab completion for lunch.
Matt Alexanderd9271832020-05-19 00:34:20 +0000669function _lunch() {
Jeff Davidson513d7a42010-08-02 10:00:44 -0700670 local cur prev opts
671 COMPREPLY=()
672 cur="${COMP_WORDS[COMP_CWORD]}"
673 prev="${COMP_WORDS[COMP_CWORD-1]}"
674
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700675 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800676 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700677 fi
678
679 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700680 return 0
681}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700682
Joe Onoratoda12daf2010-06-09 18:18:31 -0700683# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700684# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9271832020-05-19 00:34:20 +0000685function tapas() {
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700686 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800687 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700688 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700689 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800690 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|arm64|x86_64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700691
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700692 if [ "$showHelp" != "" ]; then
693 $(gettop)/build/make/tapasHelp.sh
694 return
695 fi
696
Ying Wang67f02922012-08-22 10:25:20 -0700697 if [ $(echo $arch | wc -w) -gt 1 ]; then
698 echo "tapas: Error: Multiple build archs supplied: $arch"
699 return
700 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700701 if [ $(echo $variant | wc -w) -gt 1 ]; then
702 echo "tapas: Error: Multiple build variants supplied: $variant"
703 return
704 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700705 if [ $(echo $density | wc -w) -gt 1 ]; then
706 echo "tapas: Error: Multiple densities supplied: $density"
707 return
708 fi
Ying Wang67f02922012-08-22 10:25:20 -0700709
Ying Wang0a76df52015-06-08 11:57:26 -0700710 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700711 case $arch in
Matt Alexanderd9271832020-05-19 00:34:20 +0000712 x86)
713 product=aosp_x86
714 ;;
715 arm64)
716 product=aosp_arm64
717 ;;
718 x86_64)
719 product=aosp_x86_64
720 ;;
Ying Wang67f02922012-08-22 10:25:20 -0700721 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700722 if [ -z "$variant" ]; then
723 variant=eng
724 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700725 if [ -z "$apps" ]; then
726 apps=all
727 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600728 if [ -z "$density" ]; then
729 density=alldpi
730 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700731
Ying Wang67f02922012-08-22 10:25:20 -0700732 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700733 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700734 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700735 export TARGET_BUILD_TYPE=release
736 export TARGET_BUILD_APPS=$apps
737
Ying Wang08800fd2016-03-03 20:57:21 -0800738 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700739 set_stuff_for_environment
740 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800741 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700742}
743
Matt Alexanderd9271832020-05-19 00:34:20 +0000744function gettop {
Colin Cross6cdc5d22017-10-20 11:37:33 -0700745 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9271832020-05-19 00:34:20 +0000746 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ]; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700747 # The following circumlocution ensures we remove symlinks from TOP.
748 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700749 else
Matt Alexanderd9271832020-05-19 00:34:20 +0000750 if [ -f $TOPFILE ]; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800751 # The following circumlocution (repeated below as well) ensures
752 # that we record the true directory name and not one that is
753 # faked up with symlink names.
754 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700755 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800756 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700757 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700758 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800759 \cd ..
synergyb112a402013-07-05 19:47:47 -0700760 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761 done
Ying Wang9cd17642012-12-13 10:52:07 -0800762 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700763 if [ -f "$T/$TOPFILE" ]; then
764 echo $T
765 fi
766 fi
767 fi
768}
769
Matt Alexanderd9271832020-05-19 00:34:20 +0000770function croot() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700771 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700772 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700773 if [ "$1" ]; then
774 \cd $(gettop)/$1
775 else
776 \cd $(gettop)
777 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700778 else
779 echo "Couldn't locate the top of the tree. Try setting TOP."
780 fi
781}
782
Matt Alexanderd9271832020-05-19 00:34:20 +0000783function _croot() {
Anton Hanssonece9c482019-02-04 18:15:39 +0000784 local T=$(gettop)
785 if [ "$T" ]; then
786 local cur="${COMP_WORDS[COMP_CWORD]}"
787 k=0
788 for c in $(compgen -d ${T}/${cur}); do
789 COMPREPLY[k++]=${c#${T}/}/
790 done
791 fi
792}
793
Matt Alexanderd9271832020-05-19 00:34:20 +0000794function cproj() {
Colin Cross6cdc5d22017-10-20 11:37:33 -0700795 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700796 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700797 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700798 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
799 T=$PWD
800 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800801 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700802 return
803 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800804 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700805 done
Ying Wang9cd17642012-12-13 10:52:07 -0800806 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700807 echo "can't find Android.mk"
808}
809
Daniel Sandler47e0a882013-07-30 13:23:52 -0400810# simplified version of ps; output in the form
811# <pid> <procname>
812function qpid() {
813 local prepend=''
814 local append=''
Matt Alexanderd9271832020-05-19 00:34:20 +0000815 if [ "$1" == "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400816 prepend=' '
817 append='$'
818 shift
Matt Alexanderd9271832020-05-19 00:34:20 +0000819 elif [ "$1" == "--help" -o "$1" == "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800820 echo "usage: qpid [[--exact] <process name|pid>"
821 return 255
822 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400823
824 local EXE="$1"
Matt Alexanderd9271832020-05-19 00:34:20 +0000825 if [ "$EXE" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800826 qpid | \grep "$prepend$EXE$append"
827 else
828 adb shell ps \
829 | tr -d '\r' \
830 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
831 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400832}
833
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800834# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700835# that has the core-file-size limit set correctly
836#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800837# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700838# if its core-file-size limit is not set already.
839# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
840
Matt Alexanderd9271832020-05-19 00:34:20 +0000841function coredump_setup() {
842 echo "Getting root..."
843 adb root
844 adb wait-for-device
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700845
Matt Alexanderd9271832020-05-19 00:34:20 +0000846 echo "Remounting root partition read-write..."
847 adb shell mount -w -o remount -t rootfs rootfs
848 sleep 1
849 adb wait-for-device
850 adb shell mkdir -p /cores
851 adb shell mount -t tmpfs tmpfs /cores
852 adb shell chmod 0777 /cores
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700853
Matt Alexanderd9271832020-05-19 00:34:20 +0000854 echo "Granting SELinux permission to dump in /cores..."
855 adb shell restorecon -R /cores
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700856
Matt Alexanderd9271832020-05-19 00:34:20 +0000857 echo "Set core pattern."
858 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern'
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700859
Ying Wang08800fd2016-03-03 20:57:21 -0800860 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700861}
862
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800863# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9271832020-05-19 00:34:20 +0000864# $1 == PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700865#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800866# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700867# dump to actually be generated.
868
Matt Alexanderd9271832020-05-19 00:34:20 +0000869function coredump_enable() {
870 local PID=$1
Ying Wang08800fd2016-03-03 20:57:21 -0800871 if [ -z "$PID" ]; then
Matt Alexanderd9271832020-05-19 00:34:20 +0000872 printf "Expecting a PID!\n"
873 return
874 fi
875 echo "Setting core limit for $PID to infinite..."
Elliott Hughes910a3552016-03-07 13:53:53 -0800876 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700877}
878
879# core - send SIGV and pull the core for process
Matt Alexanderd9271832020-05-19 00:34:20 +0000880# $1 == PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700881#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800882# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700883# enabled globally.
884
Matt Alexanderd9271832020-05-19 00:34:20 +0000885function core() {
886 local PID=$1
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700887
Ying Wang08800fd2016-03-03 20:57:21 -0800888 if [ -z "$PID" ]; then
Matt Alexanderd9271832020-05-19 00:34:20 +0000889 printf "Expecting a PID!\n"
890 return
891 fi
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700892
Matt Alexanderd9271832020-05-19 00:34:20 +0000893 local CORENAME=core.$PID
894 local COREPATH=/cores/$CORENAME
895 local SIG=SEGV
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700896
Matt Alexanderd9271832020-05-19 00:34:20 +0000897 coredump_enable $1
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700898
Matt Alexanderd9271832020-05-19 00:34:20 +0000899 local done=0
Ying Wang08800fd2016-03-03 20:57:21 -0800900 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9271832020-05-19 00:34:20 +0000901 printf "\tSending SIG%s to %d...\n" $SIG $PID
902 adb shell kill -$SIG $PID
903 sleep 1
904 done
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700905
Matt Alexanderd9271832020-05-19 00:34:20 +0000906 adb shell "while [ ! -f $COREPATH ]; do echo waiting for $COREPATH to be generated; sleep 1; done"
907 echo "Done: core is under $COREPATH on device."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700908}
909
Christopher Tate744ee802009-11-12 15:33:08 -0800910# systemstack - dump the current stack trace of all threads in the system process
911# to the usual ANR traces file
Matt Alexanderd9271832020-05-19 00:34:20 +0000912function systemstack() {
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800913 stacks system_server
914}
915
Michael Wrightaeed7212014-06-19 19:58:12 -0700916# Read the ELF header from /proc/$PID/exe to determine if the process is
917# 64-bit.
Matt Alexanderd9271832020-05-19 00:34:20 +0000918function is64bit() {
Ben Chengfba67bf2014-02-25 10:27:07 -0800919 local PID="$1"
Matt Alexanderd9271832020-05-19 00:34:20 +0000920 if [ "$PID" ]; then
921 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800922 echo "64"
923 else
924 echo ""
925 fi
926 else
927 echo ""
928 fi
929}
930
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700931case `uname -s` in
932 Darwin)
Matt Alexanderd9271832020-05-19 00:34:20 +0000933 function sgrep() {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800934 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|proto)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400935 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700936 }
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700937 ;;
938 *)
Matt Alexanderd9271832020-05-19 00:34:20 +0000939 function sgrep() {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800940 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\|proto\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400941 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700942 }
943 ;;
944esac
945
Matt Alexanderd9271832020-05-19 00:34:20 +0000946function gettargetarch {
Raghu Gandham8da43102012-07-25 19:57:22 -0700947 get_build_var TARGET_ARCH
948}
949
Matt Alexanderd9271832020-05-19 00:34:20 +0000950function ggrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400951 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
952 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700953}
954
Matt Alexanderd9271832020-05-19 00:34:20 +0000955function gogrep() {
Orion Hodson831472d2019-10-25 11:35:15 +0100956 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
957 -exec grep --color -n "$@" {} +
958}
959
Matt Alexanderd9271832020-05-19 00:34:20 +0000960function jgrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400961 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
962 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700963}
964
Matt Alexanderd9271832020-05-19 00:34:20 +0000965function cgrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400966 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' \) \
967 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700968}
969
Matt Alexanderd9271832020-05-19 00:34:20 +0000970function resgrep() {
Christopher Ferris55257d22017-03-23 11:08:58 -0700971 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400972 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
973 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
974 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700975}
976
Matt Alexanderd9271832020-05-19 00:34:20 +0000977function mangrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400978 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
979 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800980}
981
Matt Alexanderd9271832020-05-19 00:34:20 +0000982function owngrep() {
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -0600983 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
984 -exec grep --color -n "$@" {} +
985}
986
Matt Alexanderd9271832020-05-19 00:34:20 +0000987function sepgrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400988 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
989 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -0700990}
991
Matt Alexanderd9271832020-05-19 00:34:20 +0000992function rcgrep() {
Mike Frysinger5e479732015-09-22 18:13:48 -0400993 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
994 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -0800995}
996
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700997case `uname -s` in
998 Darwin)
Matt Alexanderd9271832020-05-19 00:34:20 +0000999 function mgrep() {
Orion Hodson831472d2019-10-25 11:35:15 +01001000 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 -04001001 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001002 }
1003
Matt Alexanderd9271832020-05-19 00:34:20 +00001004 function treegrep() {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001005 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 -04001006 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001007 }
1008
1009 ;;
1010 *)
Matt Alexanderd9271832020-05-19 00:34:20 +00001011 function mgrep() {
Orion Hodson831472d2019-10-25 11:35:15 +01001012 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 -04001013 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001014 }
1015
Matt Alexanderd9271832020-05-19 00:34:20 +00001016 function treegrep() {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001017 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 -04001018 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001019 }
1020
1021 ;;
1022esac
1023
Matt Alexanderd9271832020-05-19 00:34:20 +00001024function getprebuilt {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001025 get_abs_build_var ANDROID_PREBUILTS
1026}
1027
Matt Alexanderd9271832020-05-19 00:34:20 +00001028function tracedmdump() {
Christopher Ferris55257d22017-03-23 11:08:58 -07001029 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001030 if [ ! "$T" ]; then
1031 echo "Couldn't locate the top of the tree. Try setting TOP."
1032 return
1033 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001034 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001035 local arch=$(gettargetarch)
1036 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001037
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001038 local TRACE=$1
Matt Alexanderd9271832020-05-19 00:34:20 +00001039 if [ ! "$TRACE" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001040 echo "usage: tracedmdump tracename"
1041 return
1042 fi
1043
Matt Alexanderd9271832020-05-19 00:34:20 +00001044 if [ ! -r "$KERNEL" ]; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001045 echo "Error: cannot find kernel: '$KERNEL'"
1046 return
1047 fi
1048
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001049 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9271832020-05-19 00:34:20 +00001050 if [ "$BASETRACE" == "$TRACE" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001051 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1052 fi
1053
1054 echo "post-processing traces..."
1055 rm -f $TRACE/qtrace.dexlist
1056 post_trace $TRACE
1057 if [ $? -ne 0 ]; then
1058 echo "***"
1059 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1060 echo "***"
1061 return
1062 fi
1063 echo "generating dexlist output..."
1064 /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
1065 echo "generating dmtrace data..."
1066 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1067 echo "generating html file..."
1068 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1069 echo "done, see $TRACE/dmtrace.html for details"
1070 echo "or run:"
1071 echo " traceview $TRACE/dmtrace"
1072}
1073
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001074# communicate with a running device or emulator, set up necessary state,
1075# and run the hat command.
Matt Alexanderd9271832020-05-19 00:34:20 +00001076function runhat() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001077 # process standard adb options
1078 local adbTarget=""
Matt Alexanderd9271832020-05-19 00:34:20 +00001079 if [ "$1" == "-d" -o "$1" == "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001080 adbTarget=$1
1081 shift 1
Matt Alexanderd9271832020-05-19 00:34:20 +00001082 elif [ "$1" == "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001083 adbTarget="$1 $2"
1084 shift 2
1085 fi
1086 local adbOptions=${adbTarget}
Matt Alexanderd9271832020-05-19 00:34:20 +00001087 #echo adbOptions == ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001088
1089 # runhat options
1090 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001091
Matt Alexanderd9271832020-05-19 00:34:20 +00001092 if [ "$targetPid" == "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001093 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001094 return
1095 fi
1096
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001097 # confirm hat is available
1098 if [ -z $(which hat) ]; then
1099 echo "hat is not available in this configuration."
1100 return
1101 fi
1102
Andy McFaddenb6289852010-07-12 08:00:19 -07001103 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001104 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001105 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001106 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001107 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001108 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001109 echo -n "> "
1110 read
1111
The Android Open Source Project88b60792009-03-03 19:28:42 -08001112 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001113
The Android Open Source Project88b60792009-03-03 19:28:42 -08001114 echo "Retrieving file $devFile..."
1115 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001116
The Android Open Source Project88b60792009-03-03 19:28:42 -08001117 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001118
The Android Open Source Project88b60792009-03-03 19:28:42 -08001119 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001120 echo "View the output by pointing your browser at http://localhost:7000/"
1121 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001122 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001123}
1124
Matt Alexanderd9271832020-05-19 00:34:20 +00001125function getbugreports() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001126 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001127
1128 if [ ! "$reports" ]; then
1129 echo "Could not locate any bugreports."
1130 return
1131 fi
1132
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001133 local report
1134 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001135 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001136 echo "/sdcard/bugreports/${report}"
1137 adb pull /sdcard/bugreports/${report} ${report}
1138 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001139 done
1140}
1141
Matt Alexanderd9271832020-05-19 00:34:20 +00001142function getsdcardpath() {
Victoria Lease1b296b42012-08-21 15:44:06 -07001143 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1144}
1145
Matt Alexanderd9271832020-05-19 00:34:20 +00001146function getscreenshotpath() {
Victoria Lease1b296b42012-08-21 15:44:06 -07001147 echo "$(getsdcardpath)/Pictures/Screenshots"
1148}
1149
Matt Alexanderd9271832020-05-19 00:34:20 +00001150function getlastscreenshot() {
Victoria Lease1b296b42012-08-21 15:44:06 -07001151 local screenshot_path=$(getscreenshotpath)
1152 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
Matt Alexanderd9271832020-05-19 00:34:20 +00001153 if [ "$screenshot" == "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001154 echo "No screenshots found."
1155 return
1156 fi
1157 echo "${screenshot}"
1158 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1159}
1160
Matt Alexanderd9271832020-05-19 00:34:20 +00001161function startviewserver() {
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001162 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001163 if [ $# -gt 0 ]; then
1164 port=$1
1165 fi
1166 adb shell service call window 1 i32 $port
1167}
1168
Matt Alexanderd9271832020-05-19 00:34:20 +00001169function stopviewserver() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001170 adb shell service call window 2
1171}
1172
Matt Alexanderd9271832020-05-19 00:34:20 +00001173function isviewserverstarted() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001174 adb shell service call window 3
1175}
1176
Matt Alexanderd9271832020-05-19 00:34:20 +00001177function key_home() {
Romain Guyb84049a2010-10-04 16:56:11 -07001178 adb shell input keyevent 3
1179}
1180
Matt Alexanderd9271832020-05-19 00:34:20 +00001181function key_back() {
Romain Guyb84049a2010-10-04 16:56:11 -07001182 adb shell input keyevent 4
1183}
1184
Matt Alexanderd9271832020-05-19 00:34:20 +00001185function key_menu() {
Romain Guyb84049a2010-10-04 16:56:11 -07001186 adb shell input keyevent 82
1187}
1188
Matt Alexanderd9271832020-05-19 00:34:20 +00001189function smoketest() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1191 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1192 return
1193 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001194 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001195 if [ ! "$T" ]; then
1196 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1197 return
1198 fi
1199
Ying Wang9cd17642012-12-13 10:52:07 -08001200 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001201 adb uninstall com.android.smoketest > /dev/null &&
1202 adb uninstall com.android.smoketest.tests > /dev/null &&
1203 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1204 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1205 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1206}
1207
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001208# simple shortcut to the runtest command
Matt Alexanderd9271832020-05-19 00:34:20 +00001209function runtest() {
Christopher Ferris55257d22017-03-23 11:08:58 -07001210 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001211 if [ ! "$T" ]; then
1212 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1213 return
1214 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001215 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001216}
1217
The Android Open Source Project88b60792009-03-03 19:28:42 -08001218function godir () {
1219 if [[ -z "$1" ]]; then
1220 echo "Usage: godir <regex>"
1221 return
1222 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001223 local T=$(gettop)
1224 local FILELIST
Matt Alexanderd9271832020-05-19 00:34:20 +00001225 if [ ! "$OUT_DIR" == "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001226 mkdir -p $OUT_DIR
1227 FILELIST=$OUT_DIR/filelist
1228 else
1229 FILELIST=$T/filelist
1230 fi
1231 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001232 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001233 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001234 echo " Done"
1235 echo ""
1236 fi
1237 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001238 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9271832020-05-19 00:34:20 +00001239 if [[ ${#lines[@]} == 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001240 echo "Not found"
1241 return
1242 fi
1243 local pathname
1244 local choice
1245 if [[ ${#lines[@]} > 1 ]]; then
1246 while [[ -z "$pathname" ]]; do
1247 local index=1
1248 local line
1249 for line in ${lines[@]}; do
1250 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001251 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001252 done
1253 echo
1254 echo -n "Select one: "
1255 unset choice
1256 read choice
1257 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1258 echo "Invalid choice"
1259 continue
1260 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001261 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001262 done
1263 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001264 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001265 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001266 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001267}
1268
Steven Moreland62054a42018-12-06 10:11:40 -08001269# Update module-info.json in out.
1270function refreshmod() {
1271 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1272 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1273 return 1
1274 fi
1275
1276 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1277
1278 # for the output of the next command
1279 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1280
1281 # Note, can't use absolute path because of the way make works.
1282 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1283 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1284}
1285
1286# List all modules for the current device, as cached in module-info.json. If any build change is
1287# made and it should be reflected in the output, you should run 'refreshmod' first.
1288function allmod() {
1289 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1290 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1291 return 1
1292 fi
1293
1294 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1295 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1296 refreshmod || return 1
1297 fi
1298
LuK1337b6a78192020-01-12 03:12:17 +01001299 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 -08001300}
1301
Rett Berg78d1c932019-01-24 14:34:23 -08001302# 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 -08001303# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001304function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001305 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1306 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1307 return 1
1308 fi
1309
1310 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001311 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001312 return 1
1313 fi
1314
1315 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1316 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1317 refreshmod || return 1
1318 fi
1319
1320 local relpath=$(python -c "import json, os
1321module = '$1'
1322module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1323if module not in module_info:
1324 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001325print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001326
1327 if [ -z "$relpath" ]; then
1328 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1329 return 1
1330 else
Rett Berg78d1c932019-01-24 14:34:23 -08001331 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001332 fi
1333}
1334
Rett Berg78d1c932019-01-24 14:34:23 -08001335# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1336# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1337function gomod() {
1338 if [[ $# -ne 1 ]]; then
1339 echo "usage: gomod <module>" >&2
1340 return 1
1341 fi
1342
1343 local path="$(pathmod $@)"
1344 if [ -z "$path" ]; then
1345 return 1
1346 fi
1347 cd $path
1348}
1349
dimitry73b84812018-12-11 18:06:00 +01001350function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001351 local word=${COMP_WORDS[COMP_CWORD]}
1352 COMPREPLY=( $(allmod | grep -E "^$word") )
1353}
1354
Alex Rayf0d08eb2013-03-08 15:15:06 -08001355# Print colored exit condition
1356function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001357 "$@"
1358 local retval=$?
Matt Alexanderd9271832020-05-19 00:34:20 +00001359 if [ $retval -ne 0 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001360 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001361 else
Jacky Cao89483b82015-05-15 22:12:53 +08001362 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001363 fi
1364 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001365}
1366
Matt Alexanderd9271832020-05-19 00:34:20 +00001367function get_make_command() {
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001368 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1369 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001370 # Always use the real make if -C is passed in
1371 for arg in "$@"; do
1372 if [[ $arg == -C* ]]; then
1373 echo command make
1374 return
1375 fi
1376 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001377 echo build/soong/soong_ui.bash --make-mode
1378 else
1379 echo command make
1380 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001381}
1382
Matt Alexanderd9271832020-05-19 00:34:20 +00001383function _wrap_build() {
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001384 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1385 "$@"
1386 return $?
1387 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001388 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001389 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001390 local ret=$?
1391 local end_time=$(date +"%s")
1392 local tdiff=$(($end_time-$start_time))
1393 local hours=$(($tdiff / 3600 ))
1394 local mins=$((($tdiff % 3600) / 60))
1395 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001396 local ncolors=$(tput colors 2>/dev/null)
1397 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001398 color_failed=$'\E'"[0;31m"
1399 color_success=$'\E'"[0;32m"
1400 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001401 else
1402 color_failed=""
1403 color_success=""
1404 color_reset=""
1405 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001406 echo
Matt Alexanderd9271832020-05-19 00:34:20 +00001407 if [ $ret -eq 0 ]; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001408 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001409 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001410 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001411 fi
Matt Alexanderd9271832020-05-19 00:34:20 +00001412 if [ $hours -gt 0 ]; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001413 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9271832020-05-19 00:34:20 +00001414 elif [ $mins -gt 0 ]; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001415 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9271832020-05-19 00:34:20 +00001416 elif [ $secs -gt 0 ]; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001417 printf "(%s seconds)" $secs
1418 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001419 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001420 echo
1421 return $ret
1422}
1423
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001424function _trigger_build()
1425(
1426 local -r bc="$1"; shift
1427 if T="$(gettop)"; then
1428 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1429 else
1430 echo "Couldn't locate the top of the tree. Try setting TOP."
1431 fi
1432)
1433
1434function m()
1435(
1436 _trigger_build "all-modules" "$@"
1437)
1438
1439function mm()
1440(
1441 _trigger_build "modules-in-a-dir-no-deps" "$@"
1442)
1443
1444function mmm()
1445(
1446 _trigger_build "modules-in-dirs-no-deps" "$@"
1447)
1448
1449function mma()
1450(
1451 _trigger_build "modules-in-a-dir" "$@"
1452)
1453
1454function mmma()
1455(
1456 _trigger_build "modules-in-dirs" "$@"
1457)
1458
Matt Alexanderd9271832020-05-19 00:34:20 +00001459function make() {
Dan Willemsene9842242017-07-28 13:00:13 -07001460 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001461}
1462
Matt Alexanderd9271832020-05-19 00:34:20 +00001463function provision() {
David Zeuthen1b126ff2015-09-30 17:10:48 -04001464 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1465 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1466 return 1
1467 fi
1468 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1469 echo "There is no provisioning script for the device." >&2
1470 return 1
1471 fi
1472
1473 # Check if user really wants to do this.
Matt Alexanderd9271832020-05-19 00:34:20 +00001474 if [ "$1" == "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001475 shift 1
1476 else
1477 echo "This action will reflash your device."
1478 echo ""
1479 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1480 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001481 echo -n "Are you sure you want to do this (yes/no)? "
1482 read
Matt Alexanderd9271832020-05-19 00:34:20 +00001483 if [[ "${REPLY}" != "yes" ]]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001484 echo "Not taking any action. Exiting." >&2
1485 return 1
1486 fi
1487 fi
1488 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1489}
1490
Jim Tanga881a252018-06-19 16:34:41 +08001491# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001492function enable_zsh_completion() {
1493 # Don't override user's options if bash-style completion is already enabled.
1494 if ! declare -f complete >/dev/null; then
1495 autoload -U compinit && compinit
1496 autoload -U bashcompinit && bashcompinit
1497 fi
Jim Tanga881a252018-06-19 16:34:41 +08001498}
1499
1500function validate_current_shell() {
1501 local current_sh="$(ps -o command -p $$)"
1502 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001503 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001504 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001505 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001506 *zsh*)
1507 function check_type() { type "$1"; }
Matt Alexanderd9271832020-05-19 00:34:20 +00001508 enable_zsh_completion
1509 ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001510 *)
Jim Tanga881a252018-06-19 16:34:41 +08001511 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 -07001512 ;;
1513 esac
Jim Tanga881a252018-06-19 16:34:41 +08001514}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001515
1516# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001517# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1518# load those.
1519#
1520# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001521function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001522 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001523 allowed=
1524 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1525 if [ -n "$allowed" ]; then
1526 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1527 echo " $allowed"
1528 echo " $f"
1529 return
1530 fi
1531 allowed="$f"
1532 done
1533
1534 allowed_files=
1535 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001536 for dir in device vendor product; do
1537 for f in $(test -d $dir && \
1538 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001539
1540 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1541 echo "including $f"; . "$f"
1542 else
1543 echo "ignoring $f, not in $allowed"
1544 fi
Jim Tanga881a252018-06-19 16:34:41 +08001545 done
1546 done
1547}
Kenny Root52aa81c2011-07-15 11:07:06 -07001548
Jim Tanga881a252018-06-19 16:34:41 +08001549validate_current_shell
1550source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001551addcompletions