blob: f4755dd336cb45da105f0f338630fe289fa2f307 [file] [log] [blame]
Joe Onorato344e4042022-12-05 15:15:36 -08001# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# gettop is duplicated here and in shell_utils.mk, because it's difficult
16# to find shell_utils.make without it for all the novel ways this file can be
17# sourced. Other common functions should only be in one place or the other.
18function _gettop_once
19{
20 local TOPFILE=build/make/core/envsetup.mk
21 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
22 # The following circumlocution ensures we remove symlinks from TOP.
23 (cd "$TOP"; PWD= /bin/pwd)
24 else
25 if [ -f $TOPFILE ] ; then
26 # The following circumlocution (repeated below as well) ensures
27 # that we record the true directory name and not one that is
28 # faked up with symlink names.
29 PWD= /bin/pwd
30 else
31 local HERE=$PWD
32 local T=
33 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
34 \cd ..
35 T=`PWD= /bin/pwd -P`
36 done
37 \cd "$HERE"
38 if [ -f "$T/$TOPFILE" ]; then
39 echo "$T"
40 fi
41 fi
42 fi
43}
44T=$(_gettop_once)
45if [ ! "$T" ]; then
46 echo "Couldn't locate the top of the tree. Always source build/envsetup.sh from the root of the tree." >&2
47 return 1
48fi
49IMPORTING_ENVSETUP=true source $T/build/make/shell_utils.sh
50
51
52# Help
Scott Anderson1a5fc952012-03-07 17:15:06 -080053function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070054cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -070055
56Run "m help" for help with the build system itself.
57
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080058Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -080059- lunch: lunch <product_name>-<build_variant>
60 Selects <product_name> as the product to build, and <build_variant> as the variant to
61 build, and stores those selections in the environment to be read by subsequent
62 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080063- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010064 Sets up the build environment for building unbundled apps (APKs).
Ulya Trafimovich08c381b2023-06-12 15:33:09 +010065- banchan: banchan <module1> [<module2> ...] \
66 [arm|x86|arm64|riscv64|x86_64|arm64_only|x86_64only] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010067 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000068- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080069- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070070- mm: Builds and installs all of the modules in the current directory, and their
71 dependencies.
72- mmm: Builds and installs all of the modules in the supplied directories, and their
73 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080074 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070075- mma: Same as 'mm'
76- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080077- provision: Flash device with all required partitions. Options will be passed on to fastboot.
78- cgrep: Greps on all local C/C++ files.
79- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010080- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080081- jgrep: Greps on all local Java files.
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +010082- jsongrep: Greps on all local Json files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090083- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080084- resgrep: Greps on all local res/*.xml files.
85- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070086- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060087- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080088- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080089- sepgrep: Greps on all local sepolicy files.
90- sgrep: Greps on all local source files.
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +010091- tomlgrep: Greps on all local Toml files.
DroidFreak32a2781982020-11-26 11:30:19 -050092- pygrep: Greps on all local Python files.
Steven Moreland62054a42018-12-06 10:11:40 -080093- godir: Go to the directory containing a file.
94- allmod: List all modules.
95- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000096- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080097- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000098- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080099- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +0000100- installmod: Adb installs a module's built APK.
101- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +0000102- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700103
Roland Levillain39341922015-10-20 12:48:19 +0100104Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -0700105- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -0800106- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700107
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108Look at the source to view more functions. The complete list is:
109EOF
Christopher Ferris55257d22017-03-23 11:08:58 -0700110 local T=$(gettop)
111 local A=""
112 local i
Jacky Cao89483b82015-05-15 22:12:53 +0800113 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 -0700114 A="$A $i"
115 done
116 echo $A
117}
118
Ying Wang08800fd2016-03-03 20:57:21 -0800119# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000120function build_build_var_cache()
121{
Christopher Ferris55257d22017-03-23 11:08:58 -0700122 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -0800123 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +0800124 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' ' '`)
125 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 -0800126 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -0800127 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +0800128 --vars="${cached_vars[*]}" \
129 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -0700130 --var-prefix=var_cache_ \
131 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -0800132 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +0000133 if [ $ret -ne 0 ]
134 then
Ying Wang08800fd2016-03-03 20:57:21 -0800135 unset build_dicts_script
136 return $ret
137 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700138 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -0800139 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -0800140 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -0800141 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +0000142 if [ $ret -ne 0 ]
143 then
Ying Wang08800fd2016-03-03 20:57:21 -0800144 return $ret
145 fi
146 BUILD_VAR_CACHE_READY="true"
147}
148
Ying Wangf0cb3972016-03-04 13:56:23 -0800149# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -0800150# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000151function destroy_build_var_cache()
152{
Ying Wang08800fd2016-03-03 20:57:21 -0800153 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -0700154 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800155 for v in $cached_vars; do
156 unset var_cache_$v
157 done
158 unset cached_vars
159 for v in $cached_abs_vars; do
160 unset abs_var_cache_$v
161 done
162 unset cached_abs_vars
163}
164
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700165# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166function get_abs_build_var()
167{
168 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
169 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800170 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200171 return
Ying Wang08800fd2016-03-03 20:57:21 -0800172 fi
173
Christopher Ferris55257d22017-03-23 11:08:58 -0700174 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700175 if [ ! "$T" ]; then
176 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
177 return
178 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700179 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700180}
181
182# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000183function get_build_var()
184{
185 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
186 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800187 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100188 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800189 fi
190
Christopher Ferris55257d22017-03-23 11:08:58 -0700191 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700192 if [ ! "$T" ]; then
193 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100194 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700196 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800197}
198
199# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000200function check_product()
201{
Christopher Ferris55257d22017-03-23 11:08:58 -0700202 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800203 if [ ! "$T" ]; then
204 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
205 return
206 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700207 TARGET_PRODUCT=$1 \
208 TARGET_BUILD_VARIANT= \
209 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700210 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800211 get_build_var TARGET_DEVICE > /dev/null
212 # hide successful answers, but allow the errors to show
213}
214
215VARIANT_CHOICES=(user userdebug eng)
216
217# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000218function check_variant()
219{
Christopher Ferris55257d22017-03-23 11:08:58 -0700220 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800221 for v in ${VARIANT_CHOICES[@]}
222 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 if [ "$v" = "$1" ]
224 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800225 return 0
226 fi
227 done
228 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700229}
230
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800231
232# Add directories to PATH that are dependent on the lunch target.
233# For directories that are not lunch-specific, add them in set_global_paths
234function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000235{
Christopher Ferris55257d22017-03-23 11:08:58 -0700236 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700237 if [ ! "$T" ]; then
238 echo "Couldn't locate the top of the tree. Try setting TOP."
239 return
240 fi
241
242 ##################################################################
243 # #
244 # Read me before you modify this code #
245 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800246 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
247 # adding to PATH, and the next time it is run, it removes that #
248 # from PATH. This is required so lunch can be run more than #
249 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700250 # #
251 ##################################################################
252
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800253 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700254 # due to "C:\Program Files" being in the path.
255
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800256 # Handle compat with the old ANDROID_BUILD_PATHS variable.
257 # TODO: Remove this after we think everyone has lunched again.
258 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
259 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
260 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700261 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000262 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700263 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800264 # strip leading ':', if any
265 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800266 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500267 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700268
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800269 # Out with the old...
270 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
271 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
272 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800273
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800274 # And in with the new...
275 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
276 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700277
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800278 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700279 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800280 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200281
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800282 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
283 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
284
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800285 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800286 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800287 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
288 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
289 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800290
Colin Crosse97e6932017-06-30 16:01:45 -0700291 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
292 export JAVA_HOME=$ANDROID_JAVA_HOME
293 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800294 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
295
296 # Fix up PYTHONPATH
297 if [ -n $ANDROID_PYTHONPATH ]; then
298 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
299 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000300 # //development/python-packages contains both a pseudo-PYTHONPATH which
301 # mimics an already assembled venv, but also contains real Python packages
302 # that are not in that layout until they are installed. We can fake it for
303 # the latter type by adding the package source directories to the PYTHONPATH
304 # directly. For the former group, we only need to add the python-packages
305 # directory itself.
306 #
307 # This could be cleaned up by converting the remaining packages that are in
308 # the first category into a typical python source layout (that is, another
309 # layer of directory nesting) and automatically adding all subdirectories of
310 # python-packages to the PYTHONPATH instead of manually curating this. We
311 # can't convert the packages like adb to the other style because doing so
312 # would prevent exporting type info from those packages.
313 #
314 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000315 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800316 if [ -n $VENDOR_PYTHONPATH ]; then
317 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
318 fi
319 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500320
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800321 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
323 export OUT=$ANDROID_PRODUCT_OUT
324
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700325 unset ANDROID_HOST_OUT
326 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
327
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900328 unset ANDROID_SOONG_HOST_OUT
329 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
330
Simran Basidd050ed2017-02-13 13:46:48 -0800331 unset ANDROID_HOST_OUT_TESTCASES
332 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
333
334 unset ANDROID_TARGET_OUT_TESTCASES
335 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
336
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800337 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800338 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800339}
340
341# Add directories to PATH that are NOT dependent on the lunch target.
342# For directories that are lunch-specific, add them in set_lunch_paths
343function set_global_paths()
344{
345 local T=$(gettop)
346 if [ ! "$T" ]; then
347 echo "Couldn't locate the top of the tree. Try setting TOP."
348 return
349 fi
350
351 ##################################################################
352 # #
353 # Read me before you modify this code #
354 # #
355 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
356 # adding to PATH, and the next time it is run, it removes that #
357 # from PATH. This is required so envsetup.sh can be sourced #
358 # more than once and still have working paths. #
359 # #
360 ##################################################################
361
362 # Out with the old...
363 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
364 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
365 fi
366
367 # And in with the new...
368 ANDROID_GLOBAL_BUILD_PATHS=$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800369 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
370 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800371
372 # add kernel specific binaries
373 if [ $(uname -s) = Linux ] ; then
374 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
375 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
376 fi
377
378 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
379 # to ensure that the corresponding 'emulator' binaries are used.
380 case $(uname -s) in
381 Darwin)
382 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
383 ;;
384 Linux)
385 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
386 ;;
387 *)
388 ANDROID_EMULATOR_PREBUILTS=
389 ;;
390 esac
391 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
392 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
393 export ANDROID_EMULATOR_PREBUILTS
394 fi
395
396 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800397 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700398}
399
Matt Alexanderd9c56562020-05-21 10:49:17 +0000400function printconfig()
401{
Christopher Ferris55257d22017-03-23 11:08:58 -0700402 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800403 if [ ! "$T" ]; then
404 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
405 return
406 fi
407 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700408}
409
Matt Alexanderd9c56562020-05-21 10:49:17 +0000410function set_stuff_for_environment()
411{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800412 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800413 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700414
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800415 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700416}
417
Matt Alexanderd9c56562020-05-21 10:49:17 +0000418function set_sequence_number()
419{
Colin Cross88737132017-03-21 17:41:03 -0700420 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700421}
422
Makoto Onukida971062018-06-18 10:15:19 -0700423# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
424function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800425 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700426 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800427 *:"$cmd":*)
428 return 1
429 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700430 esac
431 return 0
432}
433
Matt Alexanderd9c56562020-05-21 10:49:17 +0000434function addcompletions()
435{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700436 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700437
Jim Tanga881a252018-06-19 16:34:41 +0800438 # Keep us from trying to run in something that's neither bash nor zsh.
439 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700440 return
441 fi
442
443 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800444 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700445 return
446 fi
447
Jim Tanga881a252018-06-19 16:34:41 +0800448 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000449 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800450 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800451 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400452 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800453 )
Makoto Onukida971062018-06-18 10:15:19 -0700454 # Completion can be disabled selectively to allow users to use non-standard completion.
455 # e.g.
456 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
457 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400458 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800459 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400460 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000461 if [ ! -f "$f" ]; then
462 echo "Warning: completion file $f not found"
463 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700464 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700465 fi
466 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700467
Matt Alexanderd9c56562020-05-21 10:49:17 +0000468 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700469 complete -C "bit --tab" bit
470 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000471 if [ -z "$ZSH_VERSION" ]; then
472 # Doesn't work in zsh.
473 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400474 # TODO(b/244559459): Support b autocompletion for zsh
475 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000476 fi
Jim Tanga881a252018-06-19 16:34:41 +0800477 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800478
Cole Faust24c36db2021-01-23 02:39:37 +0000479 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100480 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000481 complete -F _complete_android_module_names outmod
482 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000483 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100484 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700485}
486
Joe Onorato824608c2022-04-08 11:06:16 -0700487function multitree_lunch_help()
488{
489 echo "usage: lunch PRODUCT-VARIANT" 1>&2
490 echo " Set up android build environment based on a product short name and variant" 1>&2
491 echo 1>&2
492 echo "lunch COMBO_FILE VARIANT" 1>&2
493 echo " Set up android build environment based on a specific lunch combo file" 1>&2
494 echo " and variant." 1>&2
495 echo 1>&2
496 echo "lunch --print [CONFIG]" 1>&2
497 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
498 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
499 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
500 echo 1>&2
501 echo "lunch --list" 1>&2
502 echo " List all possible combo files available in the current tree" 1>&2
503 echo 1>&2
504 echo "lunch --help" 1>&2
505 echo "lunch -h" 1>&2
506 echo " Prints this message." 1>&2
507}
508
509function multitree_lunch()
510{
511 local code
512 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000513 # Lunch must be run in the topdir, but this way we get a clear error
514 # message, instead of FileNotFound.
515 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000516 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000517 _multitree_lunch_error
518 return 1
519 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700520 if $(echo "$1" | grep -q '^-') ; then
521 # Calls starting with a -- argument are passed directly and the function
522 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000523 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700524 code=$?
525 if [[ $code -eq 2 ]] ; then
526 echo 1>&2
527 multitree_lunch_help
528 return $code
529 elif [[ $code -ne 0 ]] ; then
530 return $code
531 fi
532 else
533 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000534 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700535 code=$?
536 if [[ $code -eq 2 ]] ; then
537 echo 1>&2
538 multitree_lunch_help
539 return $code
540 elif [[ $code -ne 0 ]] ; then
541 return $code
542 fi
543
544 export TARGET_BUILD_COMBO=${results[0]}
545 export TARGET_BUILD_VARIANT=${results[1]}
546 fi
547}
548
Matt Alexanderd9c56562020-05-21 10:49:17 +0000549function choosetype()
550{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700551 echo "Build type choices are:"
552 echo " 1. release"
553 echo " 2. debug"
554 echo
555
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700557 DEFAULT_NUM=1
558 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700559
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800560 export TARGET_BUILD_TYPE=
561 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700562 while [ -z $TARGET_BUILD_TYPE ]
563 do
564 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000565 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 read ANSWER
567 else
568 echo $1
569 ANSWER=$1
570 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571 case $ANSWER in
572 "")
573 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
574 ;;
575 1)
576 export TARGET_BUILD_TYPE=release
577 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578 release)
579 export TARGET_BUILD_TYPE=release
580 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581 2)
582 export TARGET_BUILD_TYPE=debug
583 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 debug)
585 export TARGET_BUILD_TYPE=debug
586 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 *)
588 echo
589 echo "I didn't understand your response. Please try again."
590 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700591 ;;
592 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000593 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800594 break
595 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 done
597
Ying Wang08800fd2016-03-03 20:57:21 -0800598 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800600 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700601}
602
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603#
604# This function isn't really right: It chooses a TARGET_PRODUCT
605# based on the list of boards. Usually, that gets you something
606# that kinda works with a generic product, but really, you should
607# pick a product by name.
608#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000609function chooseproduct()
610{
Christopher Ferris55257d22017-03-23 11:08:58 -0700611 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000612 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700613 default_value=$TARGET_PRODUCT
614 else
Ying Wang0a76df52015-06-08 11:57:26 -0700615 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700616 fi
617
Ying Wang08800fd2016-03-03 20:57:21 -0800618 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 export TARGET_PRODUCT=
620 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700621 while [ -z "$TARGET_PRODUCT" ]
622 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700623 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000624 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800625 read ANSWER
626 else
627 echo $1
628 ANSWER=$1
629 fi
630
Matt Alexanderd9c56562020-05-21 10:49:17 +0000631 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700632 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000634 if check_product $ANSWER
635 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636 export TARGET_PRODUCT=$ANSWER
637 else
638 echo "** Not a valid product: $ANSWER"
639 fi
640 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000641 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700643 fi
644 done
645
Ying Wang08800fd2016-03-03 20:57:21 -0800646 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700647 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800648 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700649}
650
Matt Alexanderd9c56562020-05-21 10:49:17 +0000651function choosevariant()
652{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800653 echo "Variant choices are:"
654 local index=1
655 local v
656 for v in ${VARIANT_CHOICES[@]}
657 do
658 # The product name is the name of the directory containing
659 # the makefile we found, above.
660 echo " $index. $v"
661 index=$(($index+1))
662 done
663
664 local default_value=eng
665 local ANSWER
666
667 export TARGET_BUILD_VARIANT=
668 while [ -z "$TARGET_BUILD_VARIANT" ]
669 do
670 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000671 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800672 read ANSWER
673 else
674 echo $1
675 ANSWER=$1
676 fi
677
Matt Alexanderd9c56562020-05-21 10:49:17 +0000678 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800679 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000680 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
681 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200682 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800683 fi
684 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000685 if check_variant $ANSWER
686 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800687 export TARGET_BUILD_VARIANT=$ANSWER
688 else
689 echo "** Not a valid variant: $ANSWER"
690 fi
691 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000692 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800693 break
694 fi
695 done
696}
697
Matt Alexanderd9c56562020-05-21 10:49:17 +0000698function choosecombo()
699{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700700 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700701
702 echo
703 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700704 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700705
706 echo
707 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700708 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800709
710 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800711 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700712 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800713 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800714 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700715}
716
Matt Alexanderd9c56562020-05-21 10:49:17 +0000717function add_lunch_combo()
718{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800719 if [ -n "$ZSH_VERSION" ]; then
720 echo -n "${funcfiletrace[1]}: "
721 else
722 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
723 fi
724 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 -0800725}
726
Matt Alexanderd9c56562020-05-21 10:49:17 +0000727function print_lunch_menu()
728{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700729 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100730 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700731 choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100732 local ret=$?
733
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700734 echo
735 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700736 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100737
Matt Alexanderd9c56562020-05-21 10:49:17 +0000738 if [ $ret -ne 0 ]
739 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100740 echo "Warning: Cannot display lunch menu."
741 echo
742 echo "Note: You can invoke lunch with an explicit target:"
743 echo
744 echo " usage: lunch [target]" >&2
745 echo
746 return
747 fi
748
Will Burr40401202022-02-07 12:12:01 +0000749 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800750
751 local i=1
752 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700753 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800754 do
755 echo " $i. $choice"
756 i=$(($i+1))
757 done
758
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 echo
760}
761
Matt Alexanderd9c56562020-05-21 10:49:17 +0000762function lunch()
763{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800764 local answer
765
Steven Moreland92793dc2020-02-25 18:30:18 -0800766 if [[ $# -gt 1 ]]; then
767 echo "usage: lunch [target]" >&2
768 return 1
769 fi
770
Will Burr40401202022-02-07 12:12:01 +0000771 local used_lunch_menu=0
772
Steven Moreland92793dc2020-02-25 18:30:18 -0800773 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800774 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700775 else
776 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000777 echo "Which would you like? [aosp_arm-eng]"
778 echo -n "Pick from common choices above (e.g. 13) or specify your own (e.g. aosp_barbet-eng): "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800779 read answer
Will Burr40401202022-02-07 12:12:01 +0000780 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700781 fi
782
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800783 local selection=
784
Matt Alexanderd9c56562020-05-21 10:49:17 +0000785 if [ -z "$answer" ]
786 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700787 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000788 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
789 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800790 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000791 if [ $answer -le ${#choices[@]} ]
792 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800793 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000794 if [ -n "$ZSH_VERSION" ]
795 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800796 selection=${choices[$(($answer))]}
797 else
798 selection=${choices[$(($answer-1))]}
799 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800800 fi
Colin Cross88737132017-03-21 17:41:03 -0700801 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800802 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700803 fi
804
Joe Onoratoda12daf2010-06-09 18:18:31 -0700805 export TARGET_BUILD_APPS=
806
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000807 # Support either <product>-<variant> or <product>-<release>-<variant>
808 local product release_and_variant release variant
Colin Cross88737132017-03-21 17:41:03 -0700809 product=${selection%%-*} # Trim everything after first dash
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000810 release_and_variant=${selection#*-} # Trim everything up to first dash
811 if [ "$release_and_variant" != "$selection" ]; then
812 local first=${release_and_variant%%-*} # Trim everything after first dash
813 if [ "$first" != "$release_and_variant" ]; then
814 # There is a 2nd dash, split into release-variant
815 release=$first # Everything up to the dash
816 variant=${release_and_variant#*-} # Trim everything up to dash
817 else
818 # There is not a 2nd dash, default to variant as the second param
819 variant=$first
Colin Cross88737132017-03-21 17:41:03 -0700820 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700821 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800822
Matt Alexanderd9c56562020-05-21 10:49:17 +0000823 if [ -z "$product" ]
824 then
Ying Wang08800fd2016-03-03 20:57:21 -0800825 echo
Colin Cross88737132017-03-21 17:41:03 -0700826 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700827 return 1
828 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800829
Colin Cross88737132017-03-21 17:41:03 -0700830 TARGET_PRODUCT=$product \
831 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000832 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700833 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000834 if [ $? -ne 0 ]
835 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100836 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
837 then
838 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
839 fi
Colin Cross88737132017-03-21 17:41:03 -0700840 return 1
841 fi
Colin Cross88737132017-03-21 17:41:03 -0700842 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
843 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000844 if [ -n "$release" ]; then
Joe Onorato964f4012023-05-06 12:29:01 -0700845 export TARGET_RELEASE=$release
Colin Crossb105e362017-05-01 14:21:28 -0700846 else
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000847 unset TARGET_RELEASE
Colin Crossb105e362017-05-01 14:21:28 -0700848 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700849 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700850
Will Burr40401202022-02-07 12:12:01 +0000851 if [ $used_lunch_menu -eq 1 ]; then
852 echo
853 echo "Hint: next time you can simply run 'lunch $selection'"
854 fi
855
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700856 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800857
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700858 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700859 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800860 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400861
862 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
863 check_mu_config
864 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700865}
866
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700867unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700868# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000869function _lunch()
870{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700871 local cur prev opts
872 COMPREPLY=()
873 cur="${COMP_WORDS[COMP_CWORD]}"
874 prev="${COMP_WORDS[COMP_CWORD-1]}"
875
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700876 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800877 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700878 fi
879
880 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700881 return 0
882}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700883
Joe Onoratoda12daf2010-06-09 18:18:31 -0700884# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700885# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000886function tapas()
887{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700888 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800889 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700890 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700891 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Colin Cross7f49a672022-01-27 18:15:53 -0800892 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
893 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|devkeys)$' | xargs)"
894
Joe Onoratoda12daf2010-06-09 18:18:31 -0700895
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700896 if [ "$showHelp" != "" ]; then
897 $(gettop)/build/make/tapasHelp.sh
898 return
899 fi
900
Ying Wang67f02922012-08-22 10:25:20 -0700901 if [ $(echo $arch | wc -w) -gt 1 ]; then
902 echo "tapas: Error: Multiple build archs supplied: $arch"
903 return
904 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700905 if [ $(echo $variant | wc -w) -gt 1 ]; then
906 echo "tapas: Error: Multiple build variants supplied: $variant"
907 return
908 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700909 if [ $(echo $density | wc -w) -gt 1 ]; then
910 echo "tapas: Error: Multiple densities supplied: $density"
911 return
912 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800913 if [ $(echo $keys | wc -w) -gt 1 ]; then
914 echo "tapas: Error: Multiple keys supplied: $keys"
915 return
916 fi
Ying Wang67f02922012-08-22 10:25:20 -0700917
Ying Wang0a76df52015-06-08 11:57:26 -0700918 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700919 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920 x86) product=aosp_x86;;
921 arm64) product=aosp_arm64;;
922 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700923 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800924 if [ -n "$keys" ]; then
925 product=${product/aosp_/aosp_${keys}_}
926 fi;
927
Joe Onoratoda12daf2010-06-09 18:18:31 -0700928 if [ -z "$variant" ]; then
929 variant=eng
930 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700931 if [ -z "$apps" ]; then
932 apps=all
933 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600934 if [ -z "$density" ]; then
935 density=alldpi
936 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700937
Ying Wang67f02922012-08-22 10:25:20 -0700938 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700939 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700940 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700941 export TARGET_BUILD_TYPE=release
942 export TARGET_BUILD_APPS=$apps
943
Ying Wang08800fd2016-03-03 20:57:21 -0800944 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700945 set_stuff_for_environment
946 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800947 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700948}
949
Martin Stjernholmf692c752021-04-12 00:01:10 +0100950# Configures the build to build unbundled Android modules (APEXes).
951# Run banchan with one or more module names (from apex{} modules).
952function banchan()
953{
954 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100955 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|riscv64|x86_64|arm64only|x86_64only)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100956 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100957 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|(.*_)?(arm|x86|arm64|riscv64|x86_64))$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100958
959 if [ "$showHelp" != "" ]; then
960 $(gettop)/build/make/banchanHelp.sh
961 return
962 fi
963
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100964 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000965 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100966 elif [ $(echo $product | wc -w) -gt 1 ]; then
967 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100968 return
969 fi
970 if [ $(echo $variant | wc -w) -gt 1 ]; then
971 echo "banchan: Error: Multiple build variants supplied: $variant"
972 return
973 fi
974 if [ -z "$apps" ]; then
975 echo "banchan: Error: No modules supplied"
976 return
977 fi
978
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100979 case $product in
980 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100981 x86) product=module_x86;;
982 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100983 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100984 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000985 arm64only) product=module_arm64only;;
986 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100987 esac
988 if [ -z "$variant" ]; then
989 variant=eng
990 fi
991
992 export TARGET_PRODUCT=$product
993 export TARGET_BUILD_VARIANT=$variant
994 export TARGET_BUILD_DENSITY=alldpi
995 export TARGET_BUILD_TYPE=release
996
997 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
998 # case is different and it may diverge in the future.
999 export TARGET_BUILD_APPS=$apps
1000
1001 build_build_var_cache
1002 set_stuff_for_environment
1003 printconfig
1004 destroy_build_var_cache
1005}
1006
Joe Onorato7cf6f972022-05-11 21:39:57 -07001007# TODO: Merge into gettop as part of launching multitree
1008function multitree_gettop
1009{
Spandan Dasca762052022-09-21 00:08:34 +00001010 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -07001011 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
1012 # The following circumlocution ensures we remove symlinks from TOP.
1013 (cd "$TOP"; PWD= /bin/pwd)
1014 else
1015 if [ -f $TOPFILE ] ; then
1016 # The following circumlocution (repeated below as well) ensures
1017 # that we record the true directory name and not one that is
1018 # faked up with symlink names.
1019 PWD= /bin/pwd
1020 else
1021 local HERE=$PWD
1022 local T=
1023 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1024 \cd ..
1025 T=`PWD= /bin/pwd -P`
1026 done
1027 \cd "$HERE"
1028 if [ -f "$T/$TOPFILE" ]; then
1029 echo "$T"
1030 fi
1031 fi
1032 fi
1033}
1034
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035function croot()
1036{
Christopher Ferris55257d22017-03-23 11:08:58 -07001037 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001038 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001039 if [ "$1" ]; then
1040 \cd $(gettop)/$1
1041 else
1042 \cd $(gettop)
1043 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044 else
1045 echo "Couldn't locate the top of the tree. Try setting TOP."
1046 fi
1047}
1048
Matt Alexanderd9c56562020-05-21 10:49:17 +00001049function _croot()
1050{
Anton Hanssonece9c482019-02-04 18:15:39 +00001051 local T=$(gettop)
1052 if [ "$T" ]; then
1053 local cur="${COMP_WORDS[COMP_CWORD]}"
1054 k=0
1055 for c in $(compgen -d ${T}/${cur}); do
1056 COMPREPLY[k++]=${c#${T}/}/
1057 done
1058 fi
1059}
1060
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061function cproj()
1062{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001063 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001064 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001065 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001066 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1067 T=$PWD
1068 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001069 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001070 return
1071 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001072 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001073 done
Ying Wang9cd17642012-12-13 10:52:07 -08001074 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001075 echo "can't find Android.mk"
1076}
1077
Daniel Sandler47e0a882013-07-30 13:23:52 -04001078# simplified version of ps; output in the form
1079# <pid> <procname>
1080function qpid() {
1081 local prepend=''
1082 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001083 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001084 prepend=' '
1085 append='$'
1086 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001087 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001088 echo "usage: qpid [[--exact] <process name|pid>"
1089 return 255
1090 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001091
1092 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001093 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001094 qpid | \grep "$prepend$EXE$append"
1095 else
1096 adb shell ps \
1097 | tr -d '\r' \
1098 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1099 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001100}
1101
Steven Moreland74114f12020-09-10 01:23:32 +00001102# syswrite - disable verity, reboot if needed, and remount image
1103#
1104# Easy way to make system.img/etc writable
1105function syswrite() {
Steven Moreland7275a192023-04-17 17:21:01 +00001106 adb wait-for-device && adb root && adb wait-for-device || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001107 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001108 echo "rebooting"
Steven Moreland7275a192023-04-17 17:21:01 +00001109 adb reboot && adb wait-for-device && adb root && adb wait-for-device || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001110 fi
Steven Moreland7275a192023-04-17 17:21:01 +00001111 adb remount || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001112}
1113
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001114# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001115# that has the core-file-size limit set correctly
1116#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001117# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001118# if its core-file-size limit is not set already.
1119# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1120
Matt Alexanderd9c56562020-05-21 10:49:17 +00001121function coredump_setup()
1122{
1123 echo "Getting root...";
1124 adb root;
1125 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126
Matt Alexanderd9c56562020-05-21 10:49:17 +00001127 echo "Remounting root partition read-write...";
1128 adb shell mount -w -o remount -t rootfs rootfs;
1129 sleep 1;
1130 adb wait-for-device;
1131 adb shell mkdir -p /cores;
1132 adb shell mount -t tmpfs tmpfs /cores;
1133 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001134
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135 echo "Granting SELinux permission to dump in /cores...";
1136 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001137
Matt Alexanderd9c56562020-05-21 10:49:17 +00001138 echo "Set core pattern.";
1139 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001140
Ying Wang08800fd2016-03-03 20:57:21 -08001141 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001142}
1143
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001144# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001146#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001147# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001148# dump to actually be generated.
1149
Matt Alexanderd9c56562020-05-21 10:49:17 +00001150function coredump_enable()
1151{
1152 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001153 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001154 printf "Expecting a PID!\n";
1155 return;
1156 fi;
1157 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001158 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001159}
1160
1161# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001163#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001164# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001165# enabled globally.
1166
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167function core()
1168{
1169 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001170
Ying Wang08800fd2016-03-03 20:57:21 -08001171 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001172 printf "Expecting a PID!\n";
1173 return;
1174 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001175
Matt Alexanderd9c56562020-05-21 10:49:17 +00001176 local CORENAME=core.$PID;
1177 local COREPATH=/cores/$CORENAME;
1178 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001179
Matt Alexanderd9c56562020-05-21 10:49:17 +00001180 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001181
Matt Alexanderd9c56562020-05-21 10:49:17 +00001182 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001183 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001184 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1185 adb shell kill -$SIG $PID;
1186 sleep 1;
1187 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001188
Matt Alexanderd9c56562020-05-21 10:49:17 +00001189 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1190 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001191}
1192
Christopher Tate744ee802009-11-12 15:33:08 -08001193# systemstack - dump the current stack trace of all threads in the system process
1194# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001195function systemstack()
1196{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001197 stacks system_server
1198}
1199
Michael Wrightaeed7212014-06-19 19:58:12 -07001200# Read the ELF header from /proc/$PID/exe to determine if the process is
1201# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202function is64bit()
1203{
Ben Chengfba67bf2014-02-25 10:27:07 -08001204 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001205 if [ "$PID" ] ; then
1206 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001207 echo "64"
1208 else
1209 echo ""
1210 fi
1211 else
1212 echo ""
1213 fi
1214}
1215
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216case `uname -s` in
1217 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001218 function sgrep()
1219 {
Joe Onorato95670782023-02-27 15:53:09 -08001220 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|kt|xml|sh|mk|aidl|vts|proto|rs|go)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001221 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001222 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001223
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001224 ;;
1225 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001226 function sgrep()
1227 {
Joe Onorato95670782023-02-27 15:53:09 -08001228 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|kt\|xml\|sh\|mk\|aidl\|vts\|proto\|rs\|go\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001229 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001230 }
1231 ;;
1232esac
1233
Matt Alexanderd9c56562020-05-21 10:49:17 +00001234function gettargetarch
1235{
Raghu Gandham8da43102012-07-25 19:57:22 -07001236 get_build_var TARGET_ARCH
1237}
1238
Matt Alexanderd9c56562020-05-21 10:49:17 +00001239function ggrep()
1240{
Mike Frysinger5e479732015-09-22 18:13:48 -04001241 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1242 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001243}
1244
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245function gogrep()
1246{
Orion Hodson831472d2019-10-25 11:35:15 +01001247 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1248 -exec grep --color -n "$@" {} +
1249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function jgrep()
1252{
Mike Frysinger5e479732015-09-22 18:13:48 -04001253 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1254 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001255}
1256
Alistair Delva176e5342021-02-22 13:31:26 -08001257function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001258{
1259 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1260 -exec grep --color -n "$@" {} +
1261}
1262
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001263function jsongrep()
1264{
1265 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1266 -exec grep --color -n "$@" {} +
1267}
1268
1269function tomlgrep()
1270{
1271 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1272 -exec grep --color -n "$@" {} +
1273}
1274
Taesu Leeea0cecd2020-10-28 11:05:18 +09001275function ktgrep()
1276{
1277 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1278 -exec grep --color -n "$@" {} +
1279}
1280
Matt Alexanderd9c56562020-05-21 10:49:17 +00001281function cgrep()
1282{
Mike Frysinger5e479732015-09-22 18:13:48 -04001283 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' \) \
1284 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001285}
1286
Matt Alexanderd9c56562020-05-21 10:49:17 +00001287function resgrep()
1288{
Christopher Ferris55257d22017-03-23 11:08:58 -07001289 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001290 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1291 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1292 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001293}
1294
Matt Alexanderd9c56562020-05-21 10:49:17 +00001295function mangrep()
1296{
Mike Frysinger5e479732015-09-22 18:13:48 -04001297 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1298 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001299}
1300
Matt Alexanderd9c56562020-05-21 10:49:17 +00001301function owngrep()
1302{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001303 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1304 -exec grep --color -n "$@" {} +
1305}
1306
Matt Alexanderd9c56562020-05-21 10:49:17 +00001307function sepgrep()
1308{
Mike Frysinger5e479732015-09-22 18:13:48 -04001309 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1310 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001311}
1312
Matt Alexanderd9c56562020-05-21 10:49:17 +00001313function rcgrep()
1314{
Mike Frysinger5e479732015-09-22 18:13:48 -04001315 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1316 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001317}
1318
DroidFreak32a2781982020-11-26 11:30:19 -05001319function pygrep()
1320{
1321 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1322 -exec grep --color -n "$@" {} +
1323}
1324
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001325case `uname -s` in
1326 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001327 function mgrep()
1328 {
Orion Hodson831472d2019-10-25 11:35:15 +01001329 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 -04001330 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001331 }
1332
Matt Alexanderd9c56562020-05-21 10:49:17 +00001333 function treegrep()
1334 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001335 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001336 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001337 }
1338
1339 ;;
1340 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001341 function mgrep()
1342 {
Orion Hodson831472d2019-10-25 11:35:15 +01001343 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 -04001344 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001345 }
1346
Matt Alexanderd9c56562020-05-21 10:49:17 +00001347 function treegrep()
1348 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001349 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001350 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001351 }
1352
1353 ;;
1354esac
1355
Matt Alexanderd9c56562020-05-21 10:49:17 +00001356function getprebuilt
1357{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001358 get_abs_build_var ANDROID_PREBUILTS
1359}
1360
Matt Alexanderd9c56562020-05-21 10:49:17 +00001361function tracedmdump()
1362{
Christopher Ferris55257d22017-03-23 11:08:58 -07001363 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001364 if [ ! "$T" ]; then
1365 echo "Couldn't locate the top of the tree. Try setting TOP."
1366 return
1367 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001368 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001369 local arch=$(gettargetarch)
1370 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001371
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001372 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001373 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001374 echo "usage: tracedmdump tracename"
1375 return
1376 fi
1377
Matt Alexanderd9c56562020-05-21 10:49:17 +00001378 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001379 echo "Error: cannot find kernel: '$KERNEL'"
1380 return
1381 fi
1382
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001383 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001384 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001385 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1386 fi
1387
1388 echo "post-processing traces..."
1389 rm -f $TRACE/qtrace.dexlist
1390 post_trace $TRACE
1391 if [ $? -ne 0 ]; then
1392 echo "***"
1393 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1394 echo "***"
1395 return
1396 fi
1397 echo "generating dexlist output..."
1398 /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
1399 echo "generating dmtrace data..."
1400 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1401 echo "generating html file..."
1402 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1403 echo "done, see $TRACE/dmtrace.html for details"
1404 echo "or run:"
1405 echo " traceview $TRACE/dmtrace"
1406}
1407
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001408# communicate with a running device or emulator, set up necessary state,
1409# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001410function runhat()
1411{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001412 # process standard adb options
1413 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001414 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001415 adbTarget=$1
1416 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001417 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001418 adbTarget="$1 $2"
1419 shift 2
1420 fi
1421 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001422 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001423
1424 # runhat options
1425 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001426
Matt Alexanderd9c56562020-05-21 10:49:17 +00001427 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001428 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429 return
1430 fi
1431
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001432 # confirm hat is available
1433 if [ -z $(which hat) ]; then
1434 echo "hat is not available in this configuration."
1435 return
1436 fi
1437
Andy McFaddenb6289852010-07-12 08:00:19 -07001438 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001439 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001440 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001441 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001442 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001443 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001444 echo -n "> "
1445 read
1446
The Android Open Source Project88b60792009-03-03 19:28:42 -08001447 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001448
The Android Open Source Project88b60792009-03-03 19:28:42 -08001449 echo "Retrieving file $devFile..."
1450 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001451
The Android Open Source Project88b60792009-03-03 19:28:42 -08001452 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001453
The Android Open Source Project88b60792009-03-03 19:28:42 -08001454 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001455 echo "View the output by pointing your browser at http://localhost:7000/"
1456 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001457 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001458}
1459
Matt Alexanderd9c56562020-05-21 10:49:17 +00001460function getbugreports()
1461{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001462 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001463
1464 if [ ! "$reports" ]; then
1465 echo "Could not locate any bugreports."
1466 return
1467 fi
1468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001469 local report
1470 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001471 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001472 echo "/sdcard/bugreports/${report}"
1473 adb pull /sdcard/bugreports/${report} ${report}
1474 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001475 done
1476}
1477
Matt Alexanderd9c56562020-05-21 10:49:17 +00001478function getsdcardpath()
1479{
Victoria Lease1b296b42012-08-21 15:44:06 -07001480 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1481}
1482
Matt Alexanderd9c56562020-05-21 10:49:17 +00001483function getscreenshotpath()
1484{
Victoria Lease1b296b42012-08-21 15:44:06 -07001485 echo "$(getsdcardpath)/Pictures/Screenshots"
1486}
1487
Matt Alexanderd9c56562020-05-21 10:49:17 +00001488function getlastscreenshot()
1489{
Victoria Lease1b296b42012-08-21 15:44:06 -07001490 local screenshot_path=$(getscreenshotpath)
1491 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
Matt Alexanderd9c56562020-05-21 10:49:17 +00001492 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001493 echo "No screenshots found."
1494 return
1495 fi
1496 echo "${screenshot}"
1497 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1498}
1499
Matt Alexanderd9c56562020-05-21 10:49:17 +00001500function startviewserver()
1501{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001502 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001503 if [ $# -gt 0 ]; then
1504 port=$1
1505 fi
1506 adb shell service call window 1 i32 $port
1507}
1508
Matt Alexanderd9c56562020-05-21 10:49:17 +00001509function stopviewserver()
1510{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001511 adb shell service call window 2
1512}
1513
Matt Alexanderd9c56562020-05-21 10:49:17 +00001514function isviewserverstarted()
1515{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001516 adb shell service call window 3
1517}
1518
Matt Alexanderd9c56562020-05-21 10:49:17 +00001519function key_home()
1520{
Romain Guyb84049a2010-10-04 16:56:11 -07001521 adb shell input keyevent 3
1522}
1523
Matt Alexanderd9c56562020-05-21 10:49:17 +00001524function key_back()
1525{
Romain Guyb84049a2010-10-04 16:56:11 -07001526 adb shell input keyevent 4
1527}
1528
Matt Alexanderd9c56562020-05-21 10:49:17 +00001529function key_menu()
1530{
Romain Guyb84049a2010-10-04 16:56:11 -07001531 adb shell input keyevent 82
1532}
1533
Matt Alexanderd9c56562020-05-21 10:49:17 +00001534function smoketest()
1535{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001536 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1537 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1538 return
1539 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001540 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001541 if [ ! "$T" ]; then
1542 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1543 return
1544 fi
1545
Ying Wang9cd17642012-12-13 10:52:07 -08001546 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001547 adb uninstall com.android.smoketest > /dev/null &&
1548 adb uninstall com.android.smoketest.tests > /dev/null &&
1549 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1550 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1551 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1552}
1553
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001554# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001555function runtest()
1556{
Christopher Ferris55257d22017-03-23 11:08:58 -07001557 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001558 if [ ! "$T" ]; then
1559 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1560 return
1561 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001562 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001563}
1564
The Android Open Source Project88b60792009-03-03 19:28:42 -08001565function godir () {
1566 if [[ -z "$1" ]]; then
1567 echo "Usage: godir <regex>"
1568 return
1569 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001570 local T=$(gettop)
1571 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001572 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001573 mkdir -p $OUT_DIR
1574 FILELIST=$OUT_DIR/filelist
1575 else
1576 FILELIST=$T/filelist
1577 fi
1578 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001579 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001580 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001581 echo " Done"
1582 echo ""
1583 fi
1584 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001585 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001586 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001587 echo "Not found"
1588 return
1589 fi
1590 local pathname
1591 local choice
1592 if [[ ${#lines[@]} > 1 ]]; then
1593 while [[ -z "$pathname" ]]; do
1594 local index=1
1595 local line
1596 for line in ${lines[@]}; do
1597 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001598 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001599 done
1600 echo
1601 echo -n "Select one: "
1602 unset choice
1603 read choice
1604 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1605 echo "Invalid choice"
1606 continue
1607 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001608 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001609 done
1610 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001611 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001612 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001613 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001614}
1615
Steven Moreland62054a42018-12-06 10:11:40 -08001616# Update module-info.json in out.
1617function refreshmod() {
1618 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1619 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1620 return 1
1621 fi
1622
1623 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1624
1625 # for the output of the next command
1626 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1627
1628 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001629 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001630 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1631}
1632
Cole Faust3e192382021-10-25 13:29:15 -07001633# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001634function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001635 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001636 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1637 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1638 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001639 return 1
1640 fi
1641
1642 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001643 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001644 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001645 fi
1646 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001647 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001648}
1649
Cole Faust5d825b72022-10-26 18:16:44 -07001650# List all modules for the current device, as cached in all_modules.txt. If any build change is
1651# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001652function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001653 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001654}
1655
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001656# Return the Bazel label of a Soong module if it is converted with bp2build.
1657function bmod()
1658(
1659 if [ $# -ne 1 ]; then
1660 echo "usage: bmod <module>" >&2
1661 return 1
1662 fi
1663
1664 # We could run bp2build here, but it might trigger bp2build invalidation
1665 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1666 # time overhead.
1667 #
1668 # For a snappy result, use the latest generated version in soong_injection,
1669 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001670 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001671
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001672 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001673 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1674 return 1
1675 fi
1676
1677 local target_label=$(python3 -c "import json
1678module = '$1'
1679converted_json='$converted_json'
1680bp2build_converted_map = json.load(open(converted_json))
1681if module not in bp2build_converted_map:
1682 exit(1)
1683print(bp2build_converted_map[module] + ':' + module)")
1684
1685 if [ -z "${target_label}" ]; then
1686 echo "$1 is not converted to Bazel." >&2
1687 return 1
1688 else
1689 echo "${target_label}"
1690 fi
1691)
1692
Joe Onorato2c1aa472021-02-25 16:42:39 -08001693# Get the path of a specific module in the android tree, as cached in module-info.json.
1694# If any build change is made, and it should be reflected in the output, you should run
1695# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001696function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001697 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001698 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001699 return 1
1700 fi
1701
Cole Faust24c36db2021-01-23 02:39:37 +00001702 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001703
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001704 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001705module = '$1'
1706module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1707if module not in module_info:
1708 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001709print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001710
1711 if [ -z "$relpath" ]; then
1712 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1713 return 1
1714 else
Rett Berg78d1c932019-01-24 14:34:23 -08001715 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001716 fi
1717}
1718
Joe Onorato2c1aa472021-02-25 16:42:39 -08001719# Get the path of a specific module in the android tree, as cached in module-info.json.
1720# If any build change is made, and it should be reflected in the output, you should run
1721# 'refreshmod' first. Note: This is the inverse of pathmod.
1722function dirmods() {
1723 if [[ $# -ne 1 ]]; then
1724 echo "usage: dirmods <path>" >&2
1725 return 1
1726 fi
1727
1728 verifymodinfo || return 1
1729
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001730 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001731dir = '$1'
1732while dir.endswith('/'):
1733 dir = dir[:-1]
1734prefix = dir + '/'
1735module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1736results = set()
1737for m in module_info.values():
1738 for path in m.get(u'path', []):
1739 if path == dir or path.startswith(prefix):
1740 name = m.get(u'module_name')
1741 if name:
1742 results.add(name)
1743for name in sorted(results):
1744 print(name)
1745"
1746}
1747
1748
Rett Berg78d1c932019-01-24 14:34:23 -08001749# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1750# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1751function gomod() {
1752 if [[ $# -ne 1 ]]; then
1753 echo "usage: gomod <module>" >&2
1754 return 1
1755 fi
1756
1757 local path="$(pathmod $@)"
1758 if [ -z "$path" ]; then
1759 return 1
1760 fi
1761 cd $path
1762}
1763
Cole Faust24c36db2021-01-23 02:39:37 +00001764# Gets the list of a module's installed outputs, as cached in module-info.json.
1765# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1766function outmod() {
1767 if [[ $# -ne 1 ]]; then
1768 echo "usage: outmod <module>" >&2
1769 return 1
1770 fi
1771
1772 verifymodinfo || return 1
1773
1774 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001775 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001776module = '$1'
1777module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1778if module not in module_info:
1779 exit(1)
1780for output in module_info[module]['installed']:
1781 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1782
1783 if [ $? -ne 0 ]; then
1784 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1785 return 1
1786 elif [ ! -z "$relpath" ]; then
1787 echo "$relpath"
1788 fi
1789}
1790
1791# adb install a module's apk, as cached in module-info.json. If any build change
1792# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1793# Usage: installmod [adb install arguments] <module>
1794# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1795function installmod() {
1796 if [[ $# -eq 0 ]]; then
1797 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001798 echo "" >&2
1799 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1800 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1801 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001802 return 1
1803 fi
1804
1805 local _path
1806 _path=$(outmod ${@:$#:1})
1807 if [ $? -ne 0 ]; then
1808 return 1
1809 fi
1810
1811 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1812 if [ -z "$_path" ]; then
1813 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1814 return 1
1815 fi
Cole Faust3e192382021-10-25 13:29:15 -07001816 local serial_device=""
1817 if [[ "$1" == "-s" ]]; then
1818 if [[ $# -le 2 ]]; then
1819 echo "-s requires an argument" >&2
1820 return 1
1821 fi
1822 serial_device="-s $2"
1823 shift 2
1824 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001825 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001826 echo adb $serial_device install ${@:1:$length} $_path
1827 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001828}
1829
dimitry73b84812018-12-11 18:06:00 +01001830function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001831 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001832 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001833}
1834
Alex Rayf0d08eb2013-03-08 15:15:06 -08001835# Print colored exit condition
1836function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001837 "$@"
1838 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001839 if [ $retval -ne 0 ]
1840 then
Jacky Cao89483b82015-05-15 22:12:53 +08001841 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001842 else
Jacky Cao89483b82015-05-15 22:12:53 +08001843 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001844 fi
1845 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001846}
1847
Matt Alexanderd9c56562020-05-21 10:49:17 +00001848function get_make_command()
1849{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001850 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1851 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001852 # Always use the real make if -C is passed in
1853 for arg in "$@"; do
1854 if [[ $arg == -C* ]]; then
1855 echo command make
1856 return
1857 fi
1858 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001859 echo build/soong/soong_ui.bash --make-mode
1860 else
1861 echo command make
1862 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001863}
1864
Matt Alexanderd9c56562020-05-21 10:49:17 +00001865function _wrap_build()
1866{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001867 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1868 "$@"
1869 return $?
1870 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001871 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001872 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001873 local ret=$?
1874 local end_time=$(date +"%s")
1875 local tdiff=$(($end_time-$start_time))
1876 local hours=$(($tdiff / 3600 ))
1877 local mins=$((($tdiff % 3600) / 60))
1878 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001879 local ncolors=$(tput colors 2>/dev/null)
1880 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001881 color_failed=$'\E'"[0;31m"
1882 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001883 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001884 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001885 else
1886 color_failed=""
1887 color_success=""
1888 color_reset=""
1889 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001890
Ed Heylcc6be0a2014-06-18 14:55:58 -07001891 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001892 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001893 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001894 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001895 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001896 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001897 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001898 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001899 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001900 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001901 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001902 printf "(%s seconds)" $secs
1903 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001904 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001905 echo
1906 return $ret
1907}
1908
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001909function _trigger_build()
1910(
1911 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001912 local T=$(gettop)
1913 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001914 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1915 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001916 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1917 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001918 fi
1919)
1920
1921function m()
1922(
1923 _trigger_build "all-modules" "$@"
1924)
1925
1926function mm()
1927(
1928 _trigger_build "modules-in-a-dir-no-deps" "$@"
1929)
1930
1931function mmm()
1932(
1933 _trigger_build "modules-in-dirs-no-deps" "$@"
1934)
1935
1936function mma()
1937(
1938 _trigger_build "modules-in-a-dir" "$@"
1939)
1940
1941function mmma()
1942(
1943 _trigger_build "modules-in-dirs" "$@"
1944)
1945
Matt Alexanderd9c56562020-05-21 10:49:17 +00001946function make()
1947{
Dan Willemsene9842242017-07-28 13:00:13 -07001948 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001949}
1950
Joe Onorato7cf6f972022-05-11 21:39:57 -07001951function _multitree_lunch_error()
1952{
1953 >&2 echo "Couldn't locate the top of the tree. Please run \'source build/envsetup.sh\' and multitree_lunch from the root of your workspace."
1954}
1955
1956function multitree_build()
1957{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001958 local T=$(multitree_gettop)
1959 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001960 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001961 else
1962 _multitree_lunch_error
1963 return 1
1964 fi
1965}
1966
Matt Alexanderd9c56562020-05-21 10:49:17 +00001967function provision()
1968{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001969 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1970 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1971 return 1
1972 fi
1973 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1974 echo "There is no provisioning script for the device." >&2
1975 return 1
1976 fi
1977
1978 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001979 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001980 shift 1
1981 else
1982 echo "This action will reflash your device."
1983 echo ""
1984 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1985 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001986 echo -n "Are you sure you want to do this (yes/no)? "
1987 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001988 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001989 echo "Not taking any action. Exiting." >&2
1990 return 1
1991 fi
1992 fi
1993 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1994}
1995
Jim Tanga881a252018-06-19 16:34:41 +08001996# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001997function enable_zsh_completion() {
1998 # Don't override user's options if bash-style completion is already enabled.
1999 if ! declare -f complete >/dev/null; then
2000 autoload -U compinit && compinit
2001 autoload -U bashcompinit && bashcompinit
2002 fi
Jim Tanga881a252018-06-19 16:34:41 +08002003}
2004
2005function validate_current_shell() {
2006 local current_sh="$(ps -o command -p $$)"
2007 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07002008 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08002009 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07002010 ;;
Jim Tanga881a252018-06-19 16:34:41 +08002011 *zsh*)
2012 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00002013 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002014 *)
Jim Tanga881a252018-06-19 16:34:41 +08002015 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 -07002016 ;;
2017 esac
Jim Tanga881a252018-06-19 16:34:41 +08002018}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002019
2020# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08002021# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
2022# load those.
2023#
2024# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002025function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002026 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002027 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002028 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002029 for f in $(cd "$T" && find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002030 if [ -n "$allowed" ]; then
2031 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2032 echo " $allowed"
2033 echo " $f"
2034 return
2035 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002036 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002037 done
2038
2039 allowed_files=
2040 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002041 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002042 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002043 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002044
2045 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002046 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002047 else
2048 echo "ignoring $f, not in $allowed"
2049 fi
Jim Tanga881a252018-06-19 16:34:41 +08002050 done
2051 done
2052}
Kenny Root52aa81c2011-07-15 11:07:06 -07002053
Dan Albertbab814f2020-08-26 15:34:53 -07002054function showcommands() {
2055 local T=$(gettop)
2056 if [[ -z "$TARGET_PRODUCT" ]]; then
2057 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2058 return
2059 fi
2060 case $(uname -s) in
2061 Darwin)
2062 PREBUILT_NAME=darwin-x86
2063 ;;
2064 Linux)
2065 PREBUILT_NAME=linux-x86
2066 ;;
2067 *)
2068 >&2 echo Unknown host $(uname -s)
2069 return
2070 ;;
2071 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002072 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002073 if [[ "$1" == "--regenerate" ]]; then
2074 shift 1
2075 NINJA_ARGS="-t commands $@" m
2076 else
2077 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2078 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2079 -t commands "$@")
2080 fi
2081}
2082
Cole Faust45844ab2022-08-30 13:59:07 -07002083function avbtool() {
2084 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2085 m avbtool
2086 fi
2087 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2088}
2089
Jim Tanga881a252018-06-19 16:34:41 +08002090validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002091set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002092source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002093addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002094