blob: ff3c3a60eb48af6db750c7eccd1dc5408b8a5d95 [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:
Greg Kaiser5e2d3392023-10-27 14:15:48 -060059- lunch: lunch <product_name>-<release_type>-<build_variant>
Steven Moreland62054a42018-12-06 10:11:40 -080060 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).
Martin Stjernholma73fe8d2023-12-22 13:32:42 +000065- banchan: banchan <module1> [<module2> ...] \\
Ulya Trafimovich08c381b2023-06-12 15:33:09 +010066 [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
Joe Onorato32b2aa32024-05-14 13:54:13 -0700199# This logic matches envsetup.mk
200function get_host_prebuilt_prefix
201{
202 local un=$(uname)
203 if [[ $un == "Linux" ]] ; then
204 echo linux-x86
205 elif [[ $un == "Darwin" ]] ; then
206 echo darwin-x86
207 else
208 echo "Error: Invalid host operating system: $un" 1>&2
209 fi
210}
211
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800212# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000213function check_product()
214{
Christopher Ferris55257d22017-03-23 11:08:58 -0700215 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800216 if [ ! "$T" ]; then
217 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
218 return
219 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700220 TARGET_PRODUCT=$1 \
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600221 TARGET_RELEASE= \
Jeff Browne33ba4c2011-07-11 22:11:46 -0700222 TARGET_BUILD_VARIANT= \
223 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700224 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800225 get_build_var TARGET_DEVICE > /dev/null
226 # hide successful answers, but allow the errors to show
227}
228
229VARIANT_CHOICES=(user userdebug eng)
230
231# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000232function check_variant()
233{
Christopher Ferris55257d22017-03-23 11:08:58 -0700234 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800235 for v in ${VARIANT_CHOICES[@]}
236 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000237 if [ "$v" = "$1" ]
238 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800239 return 0
240 fi
241 done
242 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700243}
244
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800245
246# Add directories to PATH that are dependent on the lunch target.
247# For directories that are not lunch-specific, add them in set_global_paths
248function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000249{
Christopher Ferris55257d22017-03-23 11:08:58 -0700250 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700251 if [ ! "$T" ]; then
252 echo "Couldn't locate the top of the tree. Try setting TOP."
253 return
254 fi
255
256 ##################################################################
257 # #
258 # Read me before you modify this code #
259 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800260 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
261 # adding to PATH, and the next time it is run, it removes that #
262 # from PATH. This is required so lunch can be run more than #
263 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700264 # #
265 ##################################################################
266
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800267 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700268 # due to "C:\Program Files" being in the path.
269
Kevin Dagostino185109b2024-01-11 17:39:02 +0000270 # Handle compat with the old ANDROID_BUILD_PATHS variable.
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800271 # TODO: Remove this after we think everyone has lunched again.
272 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
273 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
274 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700275 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000276 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700277 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800278 # strip leading ':', if any
279 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800280 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500281 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700282
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800283 # Out with the old...
284 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
285 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
286 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800287
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800288 # And in with the new...
289 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
290 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700291
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800292 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700293 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800294 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200295
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800296 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
297 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
298
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800299 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800300 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800301 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
302 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
303 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800304
Colin Crosse97e6932017-06-30 16:01:45 -0700305 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
306 export JAVA_HOME=$ANDROID_JAVA_HOME
307 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800308 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
309
310 # Fix up PYTHONPATH
311 if [ -n $ANDROID_PYTHONPATH ]; then
312 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
313 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000314 # //development/python-packages contains both a pseudo-PYTHONPATH which
315 # mimics an already assembled venv, but also contains real Python packages
316 # that are not in that layout until they are installed. We can fake it for
317 # the latter type by adding the package source directories to the PYTHONPATH
318 # directly. For the former group, we only need to add the python-packages
319 # directory itself.
320 #
321 # This could be cleaned up by converting the remaining packages that are in
322 # the first category into a typical python source layout (that is, another
323 # layer of directory nesting) and automatically adding all subdirectories of
324 # python-packages to the PYTHONPATH instead of manually curating this. We
325 # can't convert the packages like adb to the other style because doing so
326 # would prevent exporting type info from those packages.
327 #
328 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000329 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800330 if [ -n $VENDOR_PYTHONPATH ]; then
331 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
332 fi
333 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500334
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800335 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700336 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
337 export OUT=$ANDROID_PRODUCT_OUT
338
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700339 unset ANDROID_HOST_OUT
340 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
341
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900342 unset ANDROID_SOONG_HOST_OUT
343 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
344
Simran Basidd050ed2017-02-13 13:46:48 -0800345 unset ANDROID_HOST_OUT_TESTCASES
346 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
347
348 unset ANDROID_TARGET_OUT_TESTCASES
349 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
350
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800351 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800352 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800353}
354
355# Add directories to PATH that are NOT dependent on the lunch target.
356# For directories that are lunch-specific, add them in set_lunch_paths
357function set_global_paths()
358{
359 local T=$(gettop)
360 if [ ! "$T" ]; then
361 echo "Couldn't locate the top of the tree. Try setting TOP."
362 return
363 fi
364
365 ##################################################################
366 # #
367 # Read me before you modify this code #
368 # #
369 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
370 # adding to PATH, and the next time it is run, it removes that #
371 # from PATH. This is required so envsetup.sh can be sourced #
372 # more than once and still have working paths. #
373 # #
374 ##################################################################
375
376 # Out with the old...
377 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
378 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
379 fi
380
381 # And in with the new...
Joe Onorato84e61d02024-02-02 22:53:39 -0800382 ANDROID_GLOBAL_BUILD_PATHS=$T/build/soong/bin
LaMont Jonese8a3be22024-02-12 09:55:41 -0800383 ANDROID_GLOBAL_BUILD_PATHS+=:$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800384 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
385 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800386
387 # add kernel specific binaries
388 if [ $(uname -s) = Linux ] ; then
389 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
390 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
391 fi
392
393 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
394 # to ensure that the corresponding 'emulator' binaries are used.
395 case $(uname -s) in
396 Darwin)
397 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
398 ;;
399 Linux)
400 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
401 ;;
402 *)
403 ANDROID_EMULATOR_PREBUILTS=
404 ;;
405 esac
406 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
407 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
408 export ANDROID_EMULATOR_PREBUILTS
409 fi
410
411 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800412 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700413}
414
Matt Alexanderd9c56562020-05-21 10:49:17 +0000415function printconfig()
416{
Christopher Ferris55257d22017-03-23 11:08:58 -0700417 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800418 if [ ! "$T" ]; then
419 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
420 return
421 fi
422 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700423}
424
Matt Alexanderd9c56562020-05-21 10:49:17 +0000425function set_stuff_for_environment()
426{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800427 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800428 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700429
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800430 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700431}
432
Matt Alexanderd9c56562020-05-21 10:49:17 +0000433function set_sequence_number()
434{
Colin Cross88737132017-03-21 17:41:03 -0700435 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436}
437
Makoto Onukida971062018-06-18 10:15:19 -0700438# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
439function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800440 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700441 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800442 *:"$cmd":*)
443 return 1
444 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700445 esac
446 return 0
447}
448
Matt Alexanderd9c56562020-05-21 10:49:17 +0000449function addcompletions()
450{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700451 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700452
Jim Tanga881a252018-06-19 16:34:41 +0800453 # Keep us from trying to run in something that's neither bash nor zsh.
454 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700455 return
456 fi
457
458 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800459 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700460 return
461 fi
462
Jim Tanga881a252018-06-19 16:34:41 +0800463 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000464 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800465 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800466 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400467 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800468 )
Makoto Onukida971062018-06-18 10:15:19 -0700469 # Completion can be disabled selectively to allow users to use non-standard completion.
470 # e.g.
471 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
472 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400473 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800474 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400475 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000476 if [ ! -f "$f" ]; then
477 echo "Warning: completion file $f not found"
478 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700479 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700480 fi
481 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700482
Matt Alexanderd9c56562020-05-21 10:49:17 +0000483 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700484 complete -C "bit --tab" bit
485 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000486 if [ -z "$ZSH_VERSION" ]; then
487 # Doesn't work in zsh.
488 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400489 # TODO(b/244559459): Support b autocompletion for zsh
490 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000491 fi
Jim Tanga881a252018-06-19 16:34:41 +0800492 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800493
Cole Faust24c36db2021-01-23 02:39:37 +0000494 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100495 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000496 complete -F _complete_android_module_names outmod
497 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000498 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100499 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700500}
501
Joe Onorato824608c2022-04-08 11:06:16 -0700502function multitree_lunch_help()
503{
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600504 echo "usage: lunch PRODUCT-RELEASE-VARIANT" 1>&2
Joe Onorato824608c2022-04-08 11:06:16 -0700505 echo " Set up android build environment based on a product short name and variant" 1>&2
506 echo 1>&2
507 echo "lunch COMBO_FILE VARIANT" 1>&2
508 echo " Set up android build environment based on a specific lunch combo file" 1>&2
509 echo " and variant." 1>&2
510 echo 1>&2
511 echo "lunch --print [CONFIG]" 1>&2
512 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
513 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
514 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
515 echo 1>&2
516 echo "lunch --list" 1>&2
517 echo " List all possible combo files available in the current tree" 1>&2
518 echo 1>&2
519 echo "lunch --help" 1>&2
520 echo "lunch -h" 1>&2
521 echo " Prints this message." 1>&2
522}
523
524function multitree_lunch()
525{
526 local code
527 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000528 # Lunch must be run in the topdir, but this way we get a clear error
529 # message, instead of FileNotFound.
530 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000531 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000532 _multitree_lunch_error
533 return 1
534 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700535 if $(echo "$1" | grep -q '^-') ; then
536 # Calls starting with a -- argument are passed directly and the function
537 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000538 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700539 code=$?
540 if [[ $code -eq 2 ]] ; then
541 echo 1>&2
542 multitree_lunch_help
543 return $code
544 elif [[ $code -ne 0 ]] ; then
545 return $code
546 fi
547 else
548 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000549 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700550 code=$?
551 if [[ $code -eq 2 ]] ; then
552 echo 1>&2
553 multitree_lunch_help
554 return $code
555 elif [[ $code -ne 0 ]] ; then
556 return $code
557 fi
558
559 export TARGET_BUILD_COMBO=${results[0]}
560 export TARGET_BUILD_VARIANT=${results[1]}
561 fi
562}
563
Matt Alexanderd9c56562020-05-21 10:49:17 +0000564function choosetype()
565{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 echo "Build type choices are:"
567 echo " 1. release"
568 echo " 2. debug"
569 echo
570
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800571 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700572 DEFAULT_NUM=1
573 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575 export TARGET_BUILD_TYPE=
576 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 while [ -z $TARGET_BUILD_TYPE ]
578 do
579 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000580 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581 read ANSWER
582 else
583 echo $1
584 ANSWER=$1
585 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700586 case $ANSWER in
587 "")
588 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
589 ;;
590 1)
591 export TARGET_BUILD_TYPE=release
592 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800593 release)
594 export TARGET_BUILD_TYPE=release
595 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 2)
597 export TARGET_BUILD_TYPE=debug
598 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599 debug)
600 export TARGET_BUILD_TYPE=debug
601 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602 *)
603 echo
604 echo "I didn't understand your response. Please try again."
605 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700606 ;;
607 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000608 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800609 break
610 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700611 done
612
Ying Wang08800fd2016-03-03 20:57:21 -0800613 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700614 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800615 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700616}
617
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800618#
619# This function isn't really right: It chooses a TARGET_PRODUCT
620# based on the list of boards. Usually, that gets you something
621# that kinda works with a generic product, but really, you should
622# pick a product by name.
623#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000624function chooseproduct()
625{
Christopher Ferris55257d22017-03-23 11:08:58 -0700626 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000627 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700628 default_value=$TARGET_PRODUCT
629 else
Ying Wang0a76df52015-06-08 11:57:26 -0700630 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700631 fi
632
Ying Wang08800fd2016-03-03 20:57:21 -0800633 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800634 export TARGET_PRODUCT=
635 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700636 while [ -z "$TARGET_PRODUCT" ]
637 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700638 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000639 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640 read ANSWER
641 else
642 echo $1
643 ANSWER=$1
644 fi
645
Matt Alexanderd9c56562020-05-21 10:49:17 +0000646 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700647 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000649 if check_product $ANSWER
650 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800651 export TARGET_PRODUCT=$ANSWER
652 else
653 echo "** Not a valid product: $ANSWER"
654 fi
655 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000656 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800657 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700658 fi
659 done
660
Ying Wang08800fd2016-03-03 20:57:21 -0800661 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800663 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664}
665
Matt Alexanderd9c56562020-05-21 10:49:17 +0000666function choosevariant()
667{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800668 echo "Variant choices are:"
669 local index=1
670 local v
671 for v in ${VARIANT_CHOICES[@]}
672 do
673 # The product name is the name of the directory containing
674 # the makefile we found, above.
675 echo " $index. $v"
676 index=$(($index+1))
677 done
678
679 local default_value=eng
680 local ANSWER
681
682 export TARGET_BUILD_VARIANT=
683 while [ -z "$TARGET_BUILD_VARIANT" ]
684 do
685 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000686 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800687 read ANSWER
688 else
689 echo $1
690 ANSWER=$1
691 fi
692
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800694 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000695 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
696 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200697 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800698 fi
699 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000700 if check_variant $ANSWER
701 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800702 export TARGET_BUILD_VARIANT=$ANSWER
703 else
704 echo "** Not a valid variant: $ANSWER"
705 fi
706 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000707 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800708 break
709 fi
710 done
711}
712
Matt Alexanderd9c56562020-05-21 10:49:17 +0000713function choosecombo()
714{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700715 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716
717 echo
718 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700719 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700720
721 echo
722 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700723 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800724
725 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800726 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700727 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800728 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800729 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700730}
731
Matt Alexanderd9c56562020-05-21 10:49:17 +0000732function add_lunch_combo()
733{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800734 if [ -n "$ZSH_VERSION" ]; then
735 echo -n "${funcfiletrace[1]}: "
736 else
737 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
738 fi
739 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 -0800740}
741
Matt Alexanderd9c56562020-05-21 10:49:17 +0000742function print_lunch_menu()
743{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700744 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100745 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600746 choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_RELEASE= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100747 local ret=$?
748
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700749 echo
750 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700751 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100752
Matt Alexanderd9c56562020-05-21 10:49:17 +0000753 if [ $ret -ne 0 ]
754 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100755 echo "Warning: Cannot display lunch menu."
756 echo
757 echo "Note: You can invoke lunch with an explicit target:"
758 echo
759 echo " usage: lunch [target]" >&2
760 echo
761 return
762 fi
763
Will Burr40401202022-02-07 12:12:01 +0000764 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800765
766 local i=1
767 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700768 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800769 do
770 echo " $i. $choice"
771 i=$(($i+1))
772 done
773
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774 echo
775}
776
Matt Alexanderd9c56562020-05-21 10:49:17 +0000777function lunch()
778{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800779 local answer
780
Steven Moreland92793dc2020-02-25 18:30:18 -0800781 if [[ $# -gt 1 ]]; then
782 echo "usage: lunch [target]" >&2
783 return 1
784 fi
785
Will Burr40401202022-02-07 12:12:01 +0000786 local used_lunch_menu=0
787
Steven Moreland92793dc2020-02-25 18:30:18 -0800788 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800789 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 else
791 print_lunch_menu
Greg Kaiserbae4c572024-01-04 15:57:54 -0700792 echo "Which would you like? [aosp_cf_x86_64_phone-trunk_staging-eng]"
Greg Kaiser9a5a5262023-11-02 16:54:27 +0000793 echo -n "Pick from common choices above (e.g. 13) or specify your own (e.g. aosp_barbet-trunk_staging-eng): "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800794 read answer
Will Burr40401202022-02-07 12:12:01 +0000795 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700796 fi
797
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800798 local selection=
799
Matt Alexanderd9c56562020-05-21 10:49:17 +0000800 if [ -z "$answer" ]
801 then
Greg Kaiserbae4c572024-01-04 15:57:54 -0700802 selection=aosp_cf_x86_64_phone-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000803 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
804 then
Fabián Cañas03dd0282024-01-05 16:57:39 -0500805 local choices=($(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_RELEASE= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000806 if [ $answer -le ${#choices[@]} ]
807 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800808 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000809 if [ -n "$ZSH_VERSION" ]
810 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800811 selection=${choices[$(($answer))]}
812 else
813 selection=${choices[$(($answer-1))]}
814 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800815 fi
Colin Cross88737132017-03-21 17:41:03 -0700816 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800817 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700818 fi
819
Joe Onoratoda12daf2010-06-09 18:18:31 -0700820 export TARGET_BUILD_APPS=
821
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600822 # This must be <product>-<release>-<variant>
823 local product release variant
824 # Split string on the '-' character.
825 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800826
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600827 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000828 then
Ying Wang08800fd2016-03-03 20:57:21 -0800829 echo
Colin Cross88737132017-03-21 17:41:03 -0700830 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600831 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700832 return 1
833 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800834
Colin Cross88737132017-03-21 17:41:03 -0700835 TARGET_PRODUCT=$product \
836 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000837 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700838 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000839 if [ $? -ne 0 ]
840 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100841 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
842 then
843 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
844 fi
Colin Cross88737132017-03-21 17:41:03 -0700845 return 1
846 fi
Colin Cross88737132017-03-21 17:41:03 -0700847 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
848 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600849 export TARGET_RELEASE=$release
850 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700851 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700852
Mark White2c807512023-10-25 17:27:07 +0000853 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
854
855 set_stuff_for_environment
856 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
857
858 if [ "${TARGET_BUILD_VARIANT}" = "userdebug" ] && [[ -z "${ANDROID_QUIET_BUILD}" ]]; then
859 echo
860 echo "Want FASTER LOCAL BUILDS? Use -eng instead of -userdebug (however for" \
861 "performance benchmarking continue to use userdebug)"
862 fi
Will Burr40401202022-02-07 12:12:01 +0000863 if [ $used_lunch_menu -eq 1 ]; then
864 echo
865 echo "Hint: next time you can simply run 'lunch $selection'"
866 fi
867
Ying Wang08800fd2016-03-03 20:57:21 -0800868 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400869
870 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
871 check_mu_config
872 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700873}
874
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700875unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700876# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000877function _lunch()
878{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700879 local cur prev opts
880 COMPREPLY=()
881 cur="${COMP_WORDS[COMP_CWORD]}"
882 prev="${COMP_WORDS[COMP_CWORD-1]}"
883
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700884 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800885 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700886 fi
887
888 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700889 return 0
890}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700891
Joe Onoratoda12daf2010-06-09 18:18:31 -0700892# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700893# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000894function tapas()
895{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700896 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800897 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000898 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
899 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700900 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700901 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 -0800902 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
903 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)"
904
Joe Onoratoda12daf2010-06-09 18:18:31 -0700905
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700906 if [ "$showHelp" != "" ]; then
907 $(gettop)/build/make/tapasHelp.sh
908 return
909 fi
910
Ying Wang67f02922012-08-22 10:25:20 -0700911 if [ $(echo $arch | wc -w) -gt 1 ]; then
912 echo "tapas: Error: Multiple build archs supplied: $arch"
913 return
914 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000915 if [ $(echo $release | wc -w) -gt 1 ]; then
916 echo "tapas: Error: Multiple build releases supplied: $release"
917 return
918 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700919 if [ $(echo $variant | wc -w) -gt 1 ]; then
920 echo "tapas: Error: Multiple build variants supplied: $variant"
921 return
922 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700923 if [ $(echo $density | wc -w) -gt 1 ]; then
924 echo "tapas: Error: Multiple densities supplied: $density"
925 return
926 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800927 if [ $(echo $keys | wc -w) -gt 1 ]; then
928 echo "tapas: Error: Multiple keys supplied: $keys"
929 return
930 fi
Ying Wang67f02922012-08-22 10:25:20 -0700931
Ying Wang0a76df52015-06-08 11:57:26 -0700932 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700933 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000934 x86) product=aosp_x86;;
935 arm64) product=aosp_arm64;;
936 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700937 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800938 if [ -n "$keys" ]; then
939 product=${product/aosp_/aosp_${keys}_}
940 fi;
941
Joe Onoratoda12daf2010-06-09 18:18:31 -0700942 if [ -z "$variant" ]; then
943 variant=eng
944 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700945 if [ -z "$apps" ]; then
946 apps=all
947 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600948 if [ -z "$density" ]; then
949 density=alldpi
950 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700951
Ying Wang67f02922012-08-22 10:25:20 -0700952 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000953 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700954 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700955 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700956 export TARGET_BUILD_TYPE=release
957 export TARGET_BUILD_APPS=$apps
958
Ying Wang08800fd2016-03-03 20:57:21 -0800959 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700960 set_stuff_for_environment
961 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800962 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700963}
964
Martin Stjernholmf692c752021-04-12 00:01:10 +0100965# Configures the build to build unbundled Android modules (APEXes).
966# Run banchan with one or more module names (from apex{} modules).
967function banchan()
968{
969 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100970 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|riscv64|x86_64|arm64only|x86_64only)$' | xargs)"
Greg Kaiserd35095e2023-10-27 16:04:30 -0600971 # TODO: Expand banchan to take release arguments (and update hmm() usage).
972 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100973 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100974 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 +0100975
976 if [ "$showHelp" != "" ]; then
977 $(gettop)/build/make/banchanHelp.sh
978 return
979 fi
980
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100981 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000982 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100983 elif [ $(echo $product | wc -w) -gt 1 ]; then
984 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100985 return
986 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600987 if [ $(echo $release | wc -w) -gt 1 ]; then
988 echo "banchan: Error: Multiple build releases supplied: $release"
989 return
990 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100991 if [ $(echo $variant | wc -w) -gt 1 ]; then
992 echo "banchan: Error: Multiple build variants supplied: $variant"
993 return
994 fi
995 if [ -z "$apps" ]; then
996 echo "banchan: Error: No modules supplied"
997 return
998 fi
999
Martin Stjernholm2b8d9232021-04-16 20:45:03 +01001000 case $product in
1001 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +01001002 x86) product=module_x86;;
1003 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +01001004 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +01001005 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +00001006 arm64only) product=module_arm64only;;
1007 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +01001008 esac
1009 if [ -z "$variant" ]; then
1010 variant=eng
1011 fi
1012
1013 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -06001014 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +01001015 export TARGET_BUILD_VARIANT=$variant
1016 export TARGET_BUILD_DENSITY=alldpi
1017 export TARGET_BUILD_TYPE=release
1018
1019 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
1020 # case is different and it may diverge in the future.
1021 export TARGET_BUILD_APPS=$apps
1022
1023 build_build_var_cache
1024 set_stuff_for_environment
1025 printconfig
1026 destroy_build_var_cache
1027}
1028
Joe Onorato7cf6f972022-05-11 21:39:57 -07001029# TODO: Merge into gettop as part of launching multitree
1030function multitree_gettop
1031{
Spandan Dasca762052022-09-21 00:08:34 +00001032 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -07001033 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
1034 # The following circumlocution ensures we remove symlinks from TOP.
1035 (cd "$TOP"; PWD= /bin/pwd)
1036 else
1037 if [ -f $TOPFILE ] ; then
1038 # The following circumlocution (repeated below as well) ensures
1039 # that we record the true directory name and not one that is
1040 # faked up with symlink names.
1041 PWD= /bin/pwd
1042 else
1043 local HERE=$PWD
1044 local T=
1045 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1046 \cd ..
1047 T=`PWD= /bin/pwd -P`
1048 done
1049 \cd "$HERE"
1050 if [ -f "$T/$TOPFILE" ]; then
1051 echo "$T"
1052 fi
1053 fi
1054 fi
1055}
1056
Matt Alexanderd9c56562020-05-21 10:49:17 +00001057function croot()
1058{
Christopher Ferris55257d22017-03-23 11:08:58 -07001059 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001060 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001061 if [ "$1" ]; then
1062 \cd $(gettop)/$1
1063 else
1064 \cd $(gettop)
1065 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001066 else
1067 echo "Couldn't locate the top of the tree. Try setting TOP."
1068 fi
1069}
1070
Matt Alexanderd9c56562020-05-21 10:49:17 +00001071function _croot()
1072{
Anton Hanssonece9c482019-02-04 18:15:39 +00001073 local T=$(gettop)
1074 if [ "$T" ]; then
1075 local cur="${COMP_WORDS[COMP_CWORD]}"
1076 k=0
1077 for c in $(compgen -d ${T}/${cur}); do
1078 COMPREPLY[k++]=${c#${T}/}/
1079 done
1080 fi
1081}
1082
Matt Alexanderd9c56562020-05-21 10:49:17 +00001083function cproj()
1084{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001085 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001086 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001087 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001088 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1089 T=$PWD
1090 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001091 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001092 return
1093 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001094 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001095 done
Ying Wang9cd17642012-12-13 10:52:07 -08001096 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001097 echo "can't find Android.mk"
1098}
1099
Elliott Hughes86e99172024-03-21 16:55:08 +00001100# Ensure that we're always using the adb in the tree. This works around the fact
1101# that bash caches $PATH lookups, so if you use adb before lunching/building the
1102# one in your tree, you'll continue to get /usr/bin/adb or whatever even after
1103# you have the one from your current tree on your path. Historically this would
1104# cause confusion because glinux had adb in /usr/bin/ by default, though that
1105# doesn't appear to be the case on my rodete hosts; it is however still the case
1106# that my Mac has /usr/local/bin/adb installed by default and on the default
1107# path.
Shaju Mathew21439002023-07-10 00:53:22 +00001108function adb() {
Elliott Hughes57c47b72024-03-22 15:46:59 +00001109 # We need `command which` because zsh has a built-in `which` that's more
1110 # like `type`.
1111 local ADB=$(command which adb)
Elliott Hughes7e7ff752024-03-20 17:23:40 -07001112 if [ -z "$ADB" ]; then
1113 echo "Command adb not found; try lunch (and building) first?"
1114 return 1
1115 fi
Zhuoyao Zhang60dd9dd2024-04-19 00:07:59 +00001116 run_tool_with_logging "ADB" $ADB "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +00001117}
1118
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +00001119function run_tool_with_logging() {
1120 # Run commands in a subshell for us to handle forced terminations with a trap
1121 # handler.
1122 (
1123 local tool_tag="$1"
1124 shift
1125 local tool_binary="$1"
1126 shift
1127
Zhuoyao Zhangef1c03f2024-05-07 22:18:37 +00001128 # If the logger is not configured, run the original command and return.
1129 if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +00001130 "${tool_binary}" "${@}"
1131 return $?
1132 fi
1133
1134 # Otherwise, run the original command and call the logger when done.
1135 local start_time
1136 start_time=$(date +%s.%N)
1137 local logger=${ANDROID_TOOL_LOGGER}
1138
1139 # Install a trap to call the logger even when the process terminates abnormally.
1140 # The logger is run in the background and its output suppressed to avoid
1141 # interference with the user flow.
1142 trap '
1143 exit_code=$?;
1144 # Remove the trap to prevent duplicate log.
1145 trap - EXIT;
1146 "${logger}" \
1147 --tool_tag "${tool_tag}" \
1148 --start_timestamp "${start_time}" \
1149 --end_timestamp "$(date +%s.%N)" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +00001150 --tool_args "$*" \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +00001151 --exit_code "${exit_code}" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +00001152 ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +00001153 > /dev/null 2>&1 &
1154 exit ${exit_code}
1155 ' SIGINT SIGTERM SIGQUIT EXIT
1156
1157 # Run the original command.
1158 "${tool_binary}" "${@}"
1159 )
1160}
1161
Daniel Sandler47e0a882013-07-30 13:23:52 -04001162# simplified version of ps; output in the form
1163# <pid> <procname>
1164function qpid() {
1165 local prepend=''
1166 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001168 prepend=' '
1169 append='$'
1170 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001171 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001172 echo "usage: qpid [[--exact] <process name|pid>"
1173 return 255
1174 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001175
1176 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001177 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001178 qpid | \grep "$prepend$EXE$append"
1179 else
1180 adb shell ps \
1181 | tr -d '\r' \
1182 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1183 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001184}
1185
Steven Moreland74114f12020-09-10 01:23:32 +00001186# syswrite - disable verity, reboot if needed, and remount image
1187#
1188# Easy way to make system.img/etc writable
1189function syswrite() {
Steven Moreland7275a192023-04-17 17:21:01 +00001190 adb wait-for-device && adb root && adb wait-for-device || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001191 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001192 echo "rebooting"
Steven Moreland7275a192023-04-17 17:21:01 +00001193 adb reboot && adb wait-for-device && adb root && adb wait-for-device || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001194 fi
Steven Moreland7275a192023-04-17 17:21:01 +00001195 adb remount || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001196}
1197
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001198# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001199# that has the core-file-size limit set correctly
1200#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001201# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001202# if its core-file-size limit is not set already.
1203# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1204
Matt Alexanderd9c56562020-05-21 10:49:17 +00001205function coredump_setup()
1206{
1207 echo "Getting root...";
1208 adb root;
1209 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001210
Matt Alexanderd9c56562020-05-21 10:49:17 +00001211 echo "Remounting root partition read-write...";
1212 adb shell mount -w -o remount -t rootfs rootfs;
1213 sleep 1;
1214 adb wait-for-device;
1215 adb shell mkdir -p /cores;
1216 adb shell mount -t tmpfs tmpfs /cores;
1217 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001218
Matt Alexanderd9c56562020-05-21 10:49:17 +00001219 echo "Granting SELinux permission to dump in /cores...";
1220 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001221
Matt Alexanderd9c56562020-05-21 10:49:17 +00001222 echo "Set core pattern.";
1223 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001224
Ying Wang08800fd2016-03-03 20:57:21 -08001225 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001226}
1227
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001228# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001229# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001230#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001231# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001232# dump to actually be generated.
1233
Matt Alexanderd9c56562020-05-21 10:49:17 +00001234function coredump_enable()
1235{
1236 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001237 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001238 printf "Expecting a PID!\n";
1239 return;
1240 fi;
1241 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001242 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001243}
1244
1245# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001246# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001247#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001248# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001249# enabled globally.
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function core()
1252{
1253 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001254
Ying Wang08800fd2016-03-03 20:57:21 -08001255 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256 printf "Expecting a PID!\n";
1257 return;
1258 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001259
Matt Alexanderd9c56562020-05-21 10:49:17 +00001260 local CORENAME=core.$PID;
1261 local COREPATH=/cores/$CORENAME;
1262 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001263
Matt Alexanderd9c56562020-05-21 10:49:17 +00001264 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001265
Matt Alexanderd9c56562020-05-21 10:49:17 +00001266 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001267 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001268 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1269 adb shell kill -$SIG $PID;
1270 sleep 1;
1271 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001272
Matt Alexanderd9c56562020-05-21 10:49:17 +00001273 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1274 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001275}
1276
Christopher Tate744ee802009-11-12 15:33:08 -08001277# systemstack - dump the current stack trace of all threads in the system process
1278# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001279function systemstack()
1280{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001281 stacks system_server
1282}
1283
Michael Wrightaeed7212014-06-19 19:58:12 -07001284# Read the ELF header from /proc/$PID/exe to determine if the process is
1285# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001286function is64bit()
1287{
Ben Chengfba67bf2014-02-25 10:27:07 -08001288 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001289 if [ "$PID" ] ; then
1290 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001291 echo "64"
1292 else
1293 echo ""
1294 fi
1295 else
1296 echo ""
1297 fi
1298}
1299
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001300case `uname -s` in
1301 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001302 function sgrep()
1303 {
Joe Onorato95670782023-02-27 15:53:09 -08001304 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 -04001305 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001306 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001307
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001308 ;;
1309 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001310 function sgrep()
1311 {
Joe Onorato95670782023-02-27 15:53:09 -08001312 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 -04001313 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001314 }
1315 ;;
1316esac
1317
Matt Alexanderd9c56562020-05-21 10:49:17 +00001318function gettargetarch
1319{
Raghu Gandham8da43102012-07-25 19:57:22 -07001320 get_build_var TARGET_ARCH
1321}
1322
Matt Alexanderd9c56562020-05-21 10:49:17 +00001323function ggrep()
1324{
Mike Frysinger5e479732015-09-22 18:13:48 -04001325 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1326 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001327}
1328
Matt Alexanderd9c56562020-05-21 10:49:17 +00001329function gogrep()
1330{
Orion Hodson831472d2019-10-25 11:35:15 +01001331 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1332 -exec grep --color -n "$@" {} +
1333}
1334
Matt Alexanderd9c56562020-05-21 10:49:17 +00001335function jgrep()
1336{
Mike Frysinger5e479732015-09-22 18:13:48 -04001337 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1338 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001339}
1340
Alistair Delva176e5342021-02-22 13:31:26 -08001341function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001342{
1343 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1344 -exec grep --color -n "$@" {} +
1345}
1346
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001347function jsongrep()
1348{
1349 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1350 -exec grep --color -n "$@" {} +
1351}
1352
1353function tomlgrep()
1354{
1355 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1356 -exec grep --color -n "$@" {} +
1357}
1358
Taesu Leeea0cecd2020-10-28 11:05:18 +09001359function ktgrep()
1360{
1361 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1362 -exec grep --color -n "$@" {} +
1363}
1364
Matt Alexanderd9c56562020-05-21 10:49:17 +00001365function cgrep()
1366{
Mike Frysinger5e479732015-09-22 18:13:48 -04001367 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' \) \
1368 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001369}
1370
Matt Alexanderd9c56562020-05-21 10:49:17 +00001371function resgrep()
1372{
Christopher Ferris55257d22017-03-23 11:08:58 -07001373 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001374 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1375 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1376 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001377}
1378
Matt Alexanderd9c56562020-05-21 10:49:17 +00001379function mangrep()
1380{
Mike Frysinger5e479732015-09-22 18:13:48 -04001381 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1382 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001383}
1384
Matt Alexanderd9c56562020-05-21 10:49:17 +00001385function owngrep()
1386{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001387 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1388 -exec grep --color -n "$@" {} +
1389}
1390
Matt Alexanderd9c56562020-05-21 10:49:17 +00001391function sepgrep()
1392{
Mike Frysinger5e479732015-09-22 18:13:48 -04001393 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1394 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001395}
1396
Matt Alexanderd9c56562020-05-21 10:49:17 +00001397function rcgrep()
1398{
Mike Frysinger5e479732015-09-22 18:13:48 -04001399 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1400 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001401}
1402
DroidFreak32a2781982020-11-26 11:30:19 -05001403function pygrep()
1404{
1405 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1406 -exec grep --color -n "$@" {} +
1407}
1408
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001409case `uname -s` in
1410 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001411 function mgrep()
1412 {
Orion Hodson831472d2019-10-25 11:35:15 +01001413 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 -04001414 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001415 }
1416
Matt Alexanderd9c56562020-05-21 10:49:17 +00001417 function treegrep()
1418 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001419 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 -04001420 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001421 }
1422
1423 ;;
1424 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001425 function mgrep()
1426 {
Orion Hodson831472d2019-10-25 11:35:15 +01001427 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 -04001428 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429 }
1430
Matt Alexanderd9c56562020-05-21 10:49:17 +00001431 function treegrep()
1432 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001433 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 -04001434 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001435 }
1436
1437 ;;
1438esac
1439
Matt Alexanderd9c56562020-05-21 10:49:17 +00001440function getprebuilt
1441{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001442 get_abs_build_var ANDROID_PREBUILTS
1443}
1444
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001445# communicate with a running device or emulator, set up necessary state,
1446# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001447function runhat()
1448{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001449 # process standard adb options
1450 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001451 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001452 adbTarget=$1
1453 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001454 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001455 adbTarget="$1 $2"
1456 shift 2
1457 fi
1458 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001459 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001460
1461 # runhat options
1462 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001463
Matt Alexanderd9c56562020-05-21 10:49:17 +00001464 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001465 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001466 return
1467 fi
1468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001469 # confirm hat is available
1470 if [ -z $(which hat) ]; then
1471 echo "hat is not available in this configuration."
1472 return
1473 fi
1474
Andy McFaddenb6289852010-07-12 08:00:19 -07001475 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001476 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001477 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001478 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001479 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001480 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001481 echo -n "> "
1482 read
1483
The Android Open Source Project88b60792009-03-03 19:28:42 -08001484 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001485
The Android Open Source Project88b60792009-03-03 19:28:42 -08001486 echo "Retrieving file $devFile..."
1487 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001488
The Android Open Source Project88b60792009-03-03 19:28:42 -08001489 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001490
The Android Open Source Project88b60792009-03-03 19:28:42 -08001491 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001492 echo "View the output by pointing your browser at http://localhost:7000/"
1493 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001494 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001495}
1496
Matt Alexanderd9c56562020-05-21 10:49:17 +00001497function getbugreports()
1498{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001499 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001500
1501 if [ ! "$reports" ]; then
1502 echo "Could not locate any bugreports."
1503 return
1504 fi
1505
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001506 local report
1507 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001508 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001509 echo "/sdcard/bugreports/${report}"
1510 adb pull /sdcard/bugreports/${report} ${report}
1511 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001512 done
1513}
1514
Matt Alexanderd9c56562020-05-21 10:49:17 +00001515function getsdcardpath()
1516{
Victoria Lease1b296b42012-08-21 15:44:06 -07001517 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1518}
1519
Matt Alexanderd9c56562020-05-21 10:49:17 +00001520function getscreenshotpath()
1521{
Victoria Lease1b296b42012-08-21 15:44:06 -07001522 echo "$(getsdcardpath)/Pictures/Screenshots"
1523}
1524
Matt Alexanderd9c56562020-05-21 10:49:17 +00001525function getlastscreenshot()
1526{
Victoria Lease1b296b42012-08-21 15:44:06 -07001527 local screenshot_path=$(getscreenshotpath)
1528 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 +00001529 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001530 echo "No screenshots found."
1531 return
1532 fi
1533 echo "${screenshot}"
1534 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1535}
1536
Matt Alexanderd9c56562020-05-21 10:49:17 +00001537function startviewserver()
1538{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001539 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001540 if [ $# -gt 0 ]; then
1541 port=$1
1542 fi
1543 adb shell service call window 1 i32 $port
1544}
1545
Matt Alexanderd9c56562020-05-21 10:49:17 +00001546function stopviewserver()
1547{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001548 adb shell service call window 2
1549}
1550
Matt Alexanderd9c56562020-05-21 10:49:17 +00001551function isviewserverstarted()
1552{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001553 adb shell service call window 3
1554}
1555
Matt Alexanderd9c56562020-05-21 10:49:17 +00001556function key_home()
1557{
Romain Guyb84049a2010-10-04 16:56:11 -07001558 adb shell input keyevent 3
1559}
1560
Matt Alexanderd9c56562020-05-21 10:49:17 +00001561function key_back()
1562{
Romain Guyb84049a2010-10-04 16:56:11 -07001563 adb shell input keyevent 4
1564}
1565
Matt Alexanderd9c56562020-05-21 10:49:17 +00001566function key_menu()
1567{
Romain Guyb84049a2010-10-04 16:56:11 -07001568 adb shell input keyevent 82
1569}
1570
Matt Alexanderd9c56562020-05-21 10:49:17 +00001571function smoketest()
1572{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001573 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1574 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1575 return
1576 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001577 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001578 if [ ! "$T" ]; then
1579 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1580 return
1581 fi
1582
Ying Wang9cd17642012-12-13 10:52:07 -08001583 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001584 adb uninstall com.android.smoketest > /dev/null &&
1585 adb uninstall com.android.smoketest.tests > /dev/null &&
1586 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1587 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1588 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1589}
1590
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001591# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001592function runtest()
1593{
Christopher Ferris55257d22017-03-23 11:08:58 -07001594 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001595 if [ ! "$T" ]; then
1596 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1597 return
1598 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001599 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001600}
1601
The Android Open Source Project88b60792009-03-03 19:28:42 -08001602function godir () {
1603 if [[ -z "$1" ]]; then
1604 echo "Usage: godir <regex>"
1605 return
1606 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001607 local T=$(gettop)
1608 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001609 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001610 mkdir -p $OUT_DIR
1611 FILELIST=$OUT_DIR/filelist
1612 else
1613 FILELIST=$T/filelist
1614 fi
1615 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001616 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001617 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001618 echo " Done"
1619 echo ""
1620 fi
1621 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001622 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001623 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001624 echo "Not found"
1625 return
1626 fi
1627 local pathname
1628 local choice
1629 if [[ ${#lines[@]} > 1 ]]; then
1630 while [[ -z "$pathname" ]]; do
1631 local index=1
1632 local line
1633 for line in ${lines[@]}; do
1634 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001635 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001636 done
1637 echo
1638 echo -n "Select one: "
1639 unset choice
1640 read choice
1641 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1642 echo "Invalid choice"
1643 continue
1644 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001645 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001646 done
1647 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001648 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001649 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001650 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001651}
1652
Steven Moreland62054a42018-12-06 10:11:40 -08001653# Update module-info.json in out.
1654function refreshmod() {
1655 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1656 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1657 return 1
1658 fi
1659
1660 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1661
1662 # for the output of the next command
1663 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1664
1665 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001666 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001667 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1668}
1669
Cole Faust3e192382021-10-25 13:29:15 -07001670# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001671function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001672 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001673 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1674 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1675 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001676 return 1
1677 fi
1678
1679 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001680 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001681 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001682 fi
1683 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001684 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001685}
1686
Cole Faust5d825b72022-10-26 18:16:44 -07001687# List all modules for the current device, as cached in all_modules.txt. If any build change is
1688# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001689function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001690 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001691}
1692
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001693# Return the Bazel label of a Soong module if it is converted with bp2build.
1694function bmod()
1695(
Jingwen Chen688e5d62023-10-16 09:52:27 +00001696 if [ $# -eq 0 ]; then
1697 echo "usage: bmod <module 1> <module 2> ... <module n>" >&2
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001698 return 1
1699 fi
1700
1701 # We could run bp2build here, but it might trigger bp2build invalidation
1702 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1703 # time overhead.
1704 #
1705 # For a snappy result, use the latest generated version in soong_injection,
1706 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001707 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001708
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001709 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001710 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1711 return 1
1712 fi
1713
Jingwen Chen688e5d62023-10-16 09:52:27 +00001714 modules=()
1715 for m in "$@"; do
1716 modules+=("\"$m\",")
1717 done
1718 local res=$(python3 -c "import json
1719modules = [${modules[*]}]
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001720converted_json='$converted_json'
1721bp2build_converted_map = json.load(open(converted_json))
Jingwen Chen688e5d62023-10-16 09:52:27 +00001722for module in modules:
1723 if module not in bp2build_converted_map:
1724 print(module + ' is not converted to Bazel.')
1725 else:
1726 print(bp2build_converted_map[module] + ':' + module)")
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001727
Jingwen Chen688e5d62023-10-16 09:52:27 +00001728 echo "${res}"
1729 unconverted_count=$(echo "${res}" | grep -c "not converted to Bazel")
1730 if [[ ${unconverted_count} -ne 0 ]]; then
1731 return 1
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001732 fi
1733)
1734
Joe Onorato2c1aa472021-02-25 16:42:39 -08001735# Get the path of a specific module in the android tree, as cached in module-info.json.
1736# If any build change is made, and it should be reflected in the output, you should run
1737# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001738function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001739 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001740 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001741 return 1
1742 fi
1743
Cole Faust24c36db2021-01-23 02:39:37 +00001744 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001745
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001746 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001747module = '$1'
1748module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1749if module not in module_info:
1750 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001751print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001752
1753 if [ -z "$relpath" ]; then
1754 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1755 return 1
1756 else
Rett Berg78d1c932019-01-24 14:34:23 -08001757 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001758 fi
1759}
1760
Joe Onorato2c1aa472021-02-25 16:42:39 -08001761# Get the path of a specific module in the android tree, as cached in module-info.json.
1762# If any build change is made, and it should be reflected in the output, you should run
1763# 'refreshmod' first. Note: This is the inverse of pathmod.
1764function dirmods() {
1765 if [[ $# -ne 1 ]]; then
1766 echo "usage: dirmods <path>" >&2
1767 return 1
1768 fi
1769
1770 verifymodinfo || return 1
1771
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001772 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001773dir = '$1'
1774while dir.endswith('/'):
1775 dir = dir[:-1]
1776prefix = dir + '/'
1777module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1778results = set()
1779for m in module_info.values():
1780 for path in m.get(u'path', []):
1781 if path == dir or path.startswith(prefix):
1782 name = m.get(u'module_name')
1783 if name:
1784 results.add(name)
1785for name in sorted(results):
1786 print(name)
1787"
1788}
1789
1790
Rett Berg78d1c932019-01-24 14:34:23 -08001791# Go to a specific module in the android tree, 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.
1793function gomod() {
1794 if [[ $# -ne 1 ]]; then
1795 echo "usage: gomod <module>" >&2
1796 return 1
1797 fi
1798
1799 local path="$(pathmod $@)"
1800 if [ -z "$path" ]; then
1801 return 1
1802 fi
1803 cd $path
1804}
1805
Cole Faust24c36db2021-01-23 02:39:37 +00001806# Gets the list of a module's installed outputs, as cached in module-info.json.
1807# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1808function outmod() {
1809 if [[ $# -ne 1 ]]; then
1810 echo "usage: outmod <module>" >&2
1811 return 1
1812 fi
1813
1814 verifymodinfo || return 1
1815
1816 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001817 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001818module = '$1'
1819module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1820if module not in module_info:
1821 exit(1)
1822for output in module_info[module]['installed']:
1823 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1824
1825 if [ $? -ne 0 ]; then
1826 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1827 return 1
1828 elif [ ! -z "$relpath" ]; then
1829 echo "$relpath"
1830 fi
1831}
1832
1833# adb install a module's apk, as cached in module-info.json. If any build change
1834# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1835# Usage: installmod [adb install arguments] <module>
1836# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1837function installmod() {
1838 if [[ $# -eq 0 ]]; then
1839 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001840 echo "" >&2
1841 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1842 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1843 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001844 return 1
1845 fi
1846
1847 local _path
1848 _path=$(outmod ${@:$#:1})
1849 if [ $? -ne 0 ]; then
1850 return 1
1851 fi
1852
1853 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1854 if [ -z "$_path" ]; then
1855 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1856 return 1
1857 fi
Cole Faust3e192382021-10-25 13:29:15 -07001858 local serial_device=""
1859 if [[ "$1" == "-s" ]]; then
1860 if [[ $# -le 2 ]]; then
1861 echo "-s requires an argument" >&2
1862 return 1
1863 fi
1864 serial_device="-s $2"
1865 shift 2
1866 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001867 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001868 echo adb $serial_device install ${@:1:$length} $_path
1869 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001870}
1871
dimitry73b84812018-12-11 18:06:00 +01001872function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001873 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001874 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001875}
1876
Alex Rayf0d08eb2013-03-08 15:15:06 -08001877# Print colored exit condition
1878function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001879 "$@"
1880 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001881 if [ $retval -ne 0 ]
1882 then
Jacky Cao89483b82015-05-15 22:12:53 +08001883 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001884 else
Jacky Cao89483b82015-05-15 22:12:53 +08001885 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001886 fi
1887 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001888}
1889
Matt Alexanderd9c56562020-05-21 10:49:17 +00001890function get_make_command()
1891{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001892 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1893 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001894 # Always use the real make if -C is passed in
1895 for arg in "$@"; do
1896 if [[ $arg == -C* ]]; then
1897 echo command make
1898 return
1899 fi
1900 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001901 echo build/soong/soong_ui.bash --make-mode
1902 else
1903 echo command make
1904 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001905}
1906
Matt Alexanderd9c56562020-05-21 10:49:17 +00001907function _wrap_build()
1908{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001909 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1910 "$@"
1911 return $?
1912 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001913 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001914 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001915 local ret=$?
1916 local end_time=$(date +"%s")
1917 local tdiff=$(($end_time-$start_time))
1918 local hours=$(($tdiff / 3600 ))
1919 local mins=$((($tdiff % 3600) / 60))
1920 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001921 local ncolors=$(tput colors 2>/dev/null)
1922 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001923 color_failed=$'\E'"[0;31m"
1924 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001925 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001926 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001927 else
1928 color_failed=""
1929 color_success=""
1930 color_reset=""
1931 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001932
Ed Heylcc6be0a2014-06-18 14:55:58 -07001933 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001934 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001935 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001936 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001937 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001938 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001939 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001940 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001941 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001942 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001943 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001944 printf "(%s seconds)" $secs
1945 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001946 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001947 echo
1948 return $ret
1949}
1950
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001951function _trigger_build()
1952(
1953 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001954 local T=$(gettop)
1955 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001956 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1957 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001958 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1959 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001960 fi
Kevin Dagostino185109b2024-01-11 17:39:02 +00001961 local ret=$?
1962 if [[ ret -eq 0 && -z "${ANDROID_QUIET_BUILD:-}" && -n "${ANDROID_BUILD_BANNER}" ]]; then
1963 echo "${ANDROID_BUILD_BANNER}"
1964 fi
1965 return $ret
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001966)
1967
1968function m()
1969(
1970 _trigger_build "all-modules" "$@"
1971)
1972
1973function mm()
1974(
1975 _trigger_build "modules-in-a-dir-no-deps" "$@"
1976)
1977
1978function mmm()
1979(
1980 _trigger_build "modules-in-dirs-no-deps" "$@"
1981)
1982
1983function mma()
1984(
1985 _trigger_build "modules-in-a-dir" "$@"
1986)
1987
1988function mmma()
1989(
1990 _trigger_build "modules-in-dirs" "$@"
1991)
1992
Matt Alexanderd9c56562020-05-21 10:49:17 +00001993function make()
1994{
Dan Willemsene9842242017-07-28 13:00:13 -07001995 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001996}
1997
Joe Onorato7cf6f972022-05-11 21:39:57 -07001998function _multitree_lunch_error()
1999{
2000 >&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."
2001}
2002
2003function multitree_build()
2004{
LaMont Jonesc39e5022022-06-23 19:09:06 +00002005 local T=$(multitree_gettop)
2006 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00002007 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07002008 else
2009 _multitree_lunch_error
2010 return 1
2011 fi
2012}
2013
Matt Alexanderd9c56562020-05-21 10:49:17 +00002014function provision()
2015{
David Zeuthen1b126ff2015-09-30 17:10:48 -04002016 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
2017 echo "Couldn't locate output files. Try running 'lunch' first." >&2
2018 return 1
2019 fi
2020 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
2021 echo "There is no provisioning script for the device." >&2
2022 return 1
2023 fi
2024
2025 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00002026 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04002027 shift 1
2028 else
2029 echo "This action will reflash your device."
2030 echo ""
2031 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
2032 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08002033 echo -n "Are you sure you want to do this (yes/no)? "
2034 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00002035 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04002036 echo "Not taking any action. Exiting." >&2
2037 return 1
2038 fi
2039 fi
2040 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
2041}
2042
Jim Tanga881a252018-06-19 16:34:41 +08002043# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02002044function enable_zsh_completion() {
2045 # Don't override user's options if bash-style completion is already enabled.
2046 if ! declare -f complete >/dev/null; then
2047 autoload -U compinit && compinit
2048 autoload -U bashcompinit && bashcompinit
2049 fi
Jim Tanga881a252018-06-19 16:34:41 +08002050}
2051
2052function validate_current_shell() {
2053 local current_sh="$(ps -o command -p $$)"
2054 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07002055 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08002056 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07002057 ;;
Jim Tanga881a252018-06-19 16:34:41 +08002058 *zsh*)
2059 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00002060 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002061 *)
Jim Tanga881a252018-06-19 16:34:41 +08002062 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 -07002063 ;;
2064 esac
Jim Tanga881a252018-06-19 16:34:41 +08002065}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002066
2067# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08002068# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
2069# load those.
2070#
2071# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002072function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002073 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002074 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002075 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002076 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 -08002077 if [ -n "$allowed" ]; then
2078 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2079 echo " $allowed"
2080 echo " $f"
2081 return
2082 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002083 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002084 done
2085
2086 allowed_files=
2087 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002088 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002089 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002090 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002091
2092 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002093 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002094 else
2095 echo "ignoring $f, not in $allowed"
2096 fi
Jim Tanga881a252018-06-19 16:34:41 +08002097 done
2098 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00002099
2100 if [[ "${PWD}" == /google/cog/* ]]; then
2101 f="build/make/cogsetup.sh"
2102 echo "including $f"; . "$T/$f"
2103 fi
Jim Tanga881a252018-06-19 16:34:41 +08002104}
Kenny Root52aa81c2011-07-15 11:07:06 -07002105
Dan Albertbab814f2020-08-26 15:34:53 -07002106function showcommands() {
2107 local T=$(gettop)
2108 if [[ -z "$TARGET_PRODUCT" ]]; then
2109 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2110 return
2111 fi
2112 case $(uname -s) in
2113 Darwin)
2114 PREBUILT_NAME=darwin-x86
2115 ;;
2116 Linux)
2117 PREBUILT_NAME=linux-x86
2118 ;;
2119 *)
2120 >&2 echo Unknown host $(uname -s)
2121 return
2122 ;;
2123 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002124 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002125 if [[ "$1" == "--regenerate" ]]; then
2126 shift 1
2127 NINJA_ARGS="-t commands $@" m
2128 else
2129 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2130 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2131 -t commands "$@")
2132 fi
2133}
2134
Cole Faust45844ab2022-08-30 13:59:07 -07002135function avbtool() {
2136 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2137 m avbtool
2138 fi
2139 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2140}
2141
Zhi Dou77c9f0c2023-10-11 19:49:56 +00002142function overrideflags() {
2143 local T="$(gettop)"
2144 (\cd "${T}" && build/make/tools/overrideflags.sh "$@")
2145}
2146
Colin Cross71319722023-10-24 10:58:46 -07002147function aninja() {
2148 local T="$(gettop)"
2149 (\cd "${T}" && prebuilts/build-tools/linux-x86/bin/ninja -f out/combined-${TARGET_PRODUCT}.ninja "$@")
2150}
2151
Jim Tanga881a252018-06-19 16:34:41 +08002152validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002153set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002154source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002155addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002156