blob: 89b89f7d20155bc613d8dbd49286adcac732d1ed [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.
Rett Berg78d1c932019-01-24 14:34:23 -080096- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000097- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080098- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000099- installmod: Adb installs a module's built APK.
100- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +0000101- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700102
Roland Levillain39341922015-10-20 12:48:19 +0100103Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -0700104- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -0800105- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700106
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700107Look at the source to view more functions. The complete list is:
108EOF
Christopher Ferris55257d22017-03-23 11:08:58 -0700109 local T=$(gettop)
110 local A=""
111 local i
Jacky Cao89483b82015-05-15 22:12:53 +0800112 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 -0700113 A="$A $i"
114 done
115 echo $A
116}
117
Ying Wang08800fd2016-03-03 20:57:21 -0800118# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000119function build_build_var_cache()
120{
Christopher Ferris55257d22017-03-23 11:08:58 -0700121 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -0800122 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +0800123 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' ' '`)
124 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 -0800125 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -0800126 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +0800127 --vars="${cached_vars[*]}" \
128 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -0700129 --var-prefix=var_cache_ \
130 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -0800131 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +0000132 if [ $ret -ne 0 ]
133 then
Ying Wang08800fd2016-03-03 20:57:21 -0800134 unset build_dicts_script
135 return $ret
136 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700137 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -0800138 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -0800139 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -0800140 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +0000141 if [ $ret -ne 0 ]
142 then
Ying Wang08800fd2016-03-03 20:57:21 -0800143 return $ret
144 fi
145 BUILD_VAR_CACHE_READY="true"
146}
147
Ying Wangf0cb3972016-03-04 13:56:23 -0800148# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -0800149# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000150function destroy_build_var_cache()
151{
Ying Wang08800fd2016-03-03 20:57:21 -0800152 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -0700153 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800154 for v in $cached_vars; do
155 unset var_cache_$v
156 done
157 unset cached_vars
158 for v in $cached_abs_vars; do
159 unset abs_var_cache_$v
160 done
161 unset cached_abs_vars
162}
163
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700164# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000165function get_abs_build_var()
166{
167 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
168 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800169 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200170 return
Ying Wang08800fd2016-03-03 20:57:21 -0800171 fi
172
Christopher Ferris55257d22017-03-23 11:08:58 -0700173 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700174 if [ ! "$T" ]; then
175 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
176 return
177 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700178 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700179}
180
181# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000182function get_build_var()
183{
184 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
185 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800186 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100187 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800188 fi
189
Christopher Ferris55257d22017-03-23 11:08:58 -0700190 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700191 if [ ! "$T" ]; then
192 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100193 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700194 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700195 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800196}
197
Joe Onorato32b2aa32024-05-14 13:54:13 -0700198# This logic matches envsetup.mk
199function get_host_prebuilt_prefix
200{
201 local un=$(uname)
202 if [[ $un == "Linux" ]] ; then
203 echo linux-x86
204 elif [[ $un == "Darwin" ]] ; then
205 echo darwin-x86
206 else
207 echo "Error: Invalid host operating system: $un" 1>&2
208 fi
209}
210
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800211# Add directories to PATH that are dependent on the lunch target.
212# For directories that are not lunch-specific, add them in set_global_paths
213function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000214{
Christopher Ferris55257d22017-03-23 11:08:58 -0700215 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700216 if [ ! "$T" ]; then
217 echo "Couldn't locate the top of the tree. Try setting TOP."
218 return
219 fi
220
221 ##################################################################
222 # #
223 # Read me before you modify this code #
224 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800225 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
226 # adding to PATH, and the next time it is run, it removes that #
227 # from PATH. This is required so lunch can be run more than #
228 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700229 # #
230 ##################################################################
231
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800232 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700233 # due to "C:\Program Files" being in the path.
234
Kevin Dagostino185109b2024-01-11 17:39:02 +0000235 # Handle compat with the old ANDROID_BUILD_PATHS variable.
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800236 # TODO: Remove this after we think everyone has lunched again.
237 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
238 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
239 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700240 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000241 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700242 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800243 # strip leading ':', if any
244 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800245 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500246 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700247
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800248 # Out with the old...
249 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
250 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
251 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800252
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800253 # And in with the new...
254 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
255 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700256
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800257 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700258 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800259 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200260
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800261 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
262 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
263
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800264 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800265 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800266 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
267 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
268 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800269
Colin Crosse97e6932017-06-30 16:01:45 -0700270 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
271 export JAVA_HOME=$ANDROID_JAVA_HOME
272 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800273 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
274
275 # Fix up PYTHONPATH
276 if [ -n $ANDROID_PYTHONPATH ]; then
277 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
278 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000279 # //development/python-packages contains both a pseudo-PYTHONPATH which
280 # mimics an already assembled venv, but also contains real Python packages
281 # that are not in that layout until they are installed. We can fake it for
282 # the latter type by adding the package source directories to the PYTHONPATH
283 # directly. For the former group, we only need to add the python-packages
284 # directory itself.
285 #
286 # This could be cleaned up by converting the remaining packages that are in
287 # the first category into a typical python source layout (that is, another
288 # layer of directory nesting) and automatically adding all subdirectories of
289 # python-packages to the PYTHONPATH instead of manually curating this. We
290 # can't convert the packages like adb to the other style because doing so
291 # would prevent exporting type info from those packages.
292 #
293 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000294 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800295 if [ -n $VENDOR_PYTHONPATH ]; then
296 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
297 fi
298 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500299
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800300 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700301 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
302 export OUT=$ANDROID_PRODUCT_OUT
303
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700304 unset ANDROID_HOST_OUT
305 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
306
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900307 unset ANDROID_SOONG_HOST_OUT
308 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
309
Simran Basidd050ed2017-02-13 13:46:48 -0800310 unset ANDROID_HOST_OUT_TESTCASES
311 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
312
313 unset ANDROID_TARGET_OUT_TESTCASES
314 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
315
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800316 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800317 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800318}
319
320# Add directories to PATH that are NOT dependent on the lunch target.
321# For directories that are lunch-specific, add them in set_lunch_paths
322function set_global_paths()
323{
324 local T=$(gettop)
325 if [ ! "$T" ]; then
326 echo "Couldn't locate the top of the tree. Try setting TOP."
327 return
328 fi
329
330 ##################################################################
331 # #
332 # Read me before you modify this code #
333 # #
334 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
335 # adding to PATH, and the next time it is run, it removes that #
336 # from PATH. This is required so envsetup.sh can be sourced #
337 # more than once and still have working paths. #
338 # #
339 ##################################################################
340
341 # Out with the old...
342 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
343 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
344 fi
345
346 # And in with the new...
Joe Onorato84e61d02024-02-02 22:53:39 -0800347 ANDROID_GLOBAL_BUILD_PATHS=$T/build/soong/bin
LaMont Jonese8a3be22024-02-12 09:55:41 -0800348 ANDROID_GLOBAL_BUILD_PATHS+=:$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800349 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
350 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800351
352 # add kernel specific binaries
353 if [ $(uname -s) = Linux ] ; then
354 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
355 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
356 fi
357
358 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
359 # to ensure that the corresponding 'emulator' binaries are used.
360 case $(uname -s) in
361 Darwin)
362 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
363 ;;
364 Linux)
365 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
366 ;;
367 *)
368 ANDROID_EMULATOR_PREBUILTS=
369 ;;
370 esac
371 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
372 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
373 export ANDROID_EMULATOR_PREBUILTS
374 fi
375
376 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800377 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700378}
379
Matt Alexanderd9c56562020-05-21 10:49:17 +0000380function printconfig()
381{
Christopher Ferris55257d22017-03-23 11:08:58 -0700382 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800383 if [ ! "$T" ]; then
384 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
385 return
386 fi
387 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700388}
389
Matt Alexanderd9c56562020-05-21 10:49:17 +0000390function set_stuff_for_environment()
391{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800392 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800393 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700394
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800395 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700396}
397
Matt Alexanderd9c56562020-05-21 10:49:17 +0000398function set_sequence_number()
399{
Colin Cross88737132017-03-21 17:41:03 -0700400 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700401}
402
Makoto Onukida971062018-06-18 10:15:19 -0700403# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
404function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800405 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700406 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800407 *:"$cmd":*)
408 return 1
409 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700410 esac
411 return 0
412}
413
Matt Alexanderd9c56562020-05-21 10:49:17 +0000414function addcompletions()
415{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700416 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700417
Jim Tanga881a252018-06-19 16:34:41 +0800418 # Keep us from trying to run in something that's neither bash nor zsh.
419 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700420 return
421 fi
422
423 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800424 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700425 return
426 fi
427
Jim Tanga881a252018-06-19 16:34:41 +0800428 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000429 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800430 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800431 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400432 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800433 )
Makoto Onukida971062018-06-18 10:15:19 -0700434 # Completion can be disabled selectively to allow users to use non-standard completion.
435 # e.g.
436 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
437 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400438 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800439 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400440 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000441 if [ ! -f "$f" ]; then
442 echo "Warning: completion file $f not found"
443 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700444 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700445 fi
446 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700447
Anton Hanssonece9c482019-02-04 18:15:39 +0000448 if [ -z "$ZSH_VERSION" ]; then
449 # Doesn't work in zsh.
450 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400451 # TODO(b/244559459): Support b autocompletion for zsh
452 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000453 fi
Jim Tanga881a252018-06-19 16:34:41 +0800454 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800455
Cole Faust24c36db2021-01-23 02:39:37 +0000456 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100457 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000458 complete -F _complete_android_module_names outmod
459 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100460 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700461}
462
Matt Alexanderd9c56562020-05-21 10:49:17 +0000463function add_lunch_combo()
464{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800465 if [ -n "$ZSH_VERSION" ]; then
466 echo -n "${funcfiletrace[1]}: "
467 else
468 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
469 fi
470 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 -0800471}
472
Matt Alexanderd9c56562020-05-21 10:49:17 +0000473function print_lunch_menu()
474{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100476 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600477 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 +0100478 local ret=$?
479
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700480 echo
481 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100483
Matt Alexanderd9c56562020-05-21 10:49:17 +0000484 if [ $ret -ne 0 ]
485 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100486 echo "Warning: Cannot display lunch menu."
487 echo
488 echo "Note: You can invoke lunch with an explicit target:"
489 echo
490 echo " usage: lunch [target]" >&2
491 echo
492 return
493 fi
494
Will Burr40401202022-02-07 12:12:01 +0000495 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800496
497 local i=1
498 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700499 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800500 do
501 echo " $i. $choice"
502 i=$(($i+1))
503 done
504
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700505 echo
506}
507
Matt Alexanderd9c56562020-05-21 10:49:17 +0000508function lunch()
509{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800510 local answer
511
Steven Moreland92793dc2020-02-25 18:30:18 -0800512 if [[ $# -gt 1 ]]; then
513 echo "usage: lunch [target]" >&2
514 return 1
515 fi
516
Will Burr40401202022-02-07 12:12:01 +0000517 local used_lunch_menu=0
518
Steven Moreland92793dc2020-02-25 18:30:18 -0800519 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800520 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700521 else
522 print_lunch_menu
Greg Kaiserbae4c572024-01-04 15:57:54 -0700523 echo "Which would you like? [aosp_cf_x86_64_phone-trunk_staging-eng]"
Greg Kaiser9a5a5262023-11-02 16:54:27 +0000524 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 -0800525 read answer
Will Burr40401202022-02-07 12:12:01 +0000526 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700527 fi
528
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800529 local selection=
530
Matt Alexanderd9c56562020-05-21 10:49:17 +0000531 if [ -z "$answer" ]
532 then
Greg Kaiserbae4c572024-01-04 15:57:54 -0700533 selection=aosp_cf_x86_64_phone-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000534 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
535 then
Fabián Cañas03dd0282024-01-05 16:57:39 -0500536 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 +0000537 if [ $answer -le ${#choices[@]} ]
538 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800539 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000540 if [ -n "$ZSH_VERSION" ]
541 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800542 selection=${choices[$(($answer))]}
543 else
544 selection=${choices[$(($answer-1))]}
545 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800546 fi
Colin Cross88737132017-03-21 17:41:03 -0700547 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549 fi
550
Joe Onoratoda12daf2010-06-09 18:18:31 -0700551 export TARGET_BUILD_APPS=
552
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600553 # This must be <product>-<release>-<variant>
554 local product release variant
555 # Split string on the '-' character.
556 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600558 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000559 then
Ying Wang08800fd2016-03-03 20:57:21 -0800560 echo
Colin Cross88737132017-03-21 17:41:03 -0700561 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600562 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700563 return 1
564 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800565
Colin Cross88737132017-03-21 17:41:03 -0700566 TARGET_PRODUCT=$product \
567 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000568 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700569 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000570 if [ $? -ne 0 ]
571 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100572 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
573 then
574 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
575 fi
Colin Cross88737132017-03-21 17:41:03 -0700576 return 1
577 fi
Colin Cross88737132017-03-21 17:41:03 -0700578 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
579 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600580 export TARGET_RELEASE=$release
581 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700582 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583
Mark White2c807512023-10-25 17:27:07 +0000584 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
585
586 set_stuff_for_environment
587 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
588
589 if [ "${TARGET_BUILD_VARIANT}" = "userdebug" ] && [[ -z "${ANDROID_QUIET_BUILD}" ]]; then
590 echo
591 echo "Want FASTER LOCAL BUILDS? Use -eng instead of -userdebug (however for" \
592 "performance benchmarking continue to use userdebug)"
593 fi
Will Burr40401202022-02-07 12:12:01 +0000594 if [ $used_lunch_menu -eq 1 ]; then
595 echo
596 echo "Hint: next time you can simply run 'lunch $selection'"
597 fi
598
Ying Wang08800fd2016-03-03 20:57:21 -0800599 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400600
601 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
602 check_mu_config
603 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604}
605
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700606unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700607# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000608function _lunch()
609{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700610 local cur prev opts
611 COMPREPLY=()
612 cur="${COMP_WORDS[COMP_CWORD]}"
613 prev="${COMP_WORDS[COMP_CWORD-1]}"
614
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700615 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800616 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700617 fi
618
619 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700620 return 0
621}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700622
Joe Onoratoda12daf2010-06-09 18:18:31 -0700623# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700624# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000625function tapas()
626{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700627 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800628 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000629 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
630 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700631 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700632 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 -0800633 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
634 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)"
635
Joe Onoratoda12daf2010-06-09 18:18:31 -0700636
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700637 if [ "$showHelp" != "" ]; then
638 $(gettop)/build/make/tapasHelp.sh
639 return
640 fi
641
Ying Wang67f02922012-08-22 10:25:20 -0700642 if [ $(echo $arch | wc -w) -gt 1 ]; then
643 echo "tapas: Error: Multiple build archs supplied: $arch"
644 return
645 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000646 if [ $(echo $release | wc -w) -gt 1 ]; then
647 echo "tapas: Error: Multiple build releases supplied: $release"
648 return
649 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700650 if [ $(echo $variant | wc -w) -gt 1 ]; then
651 echo "tapas: Error: Multiple build variants supplied: $variant"
652 return
653 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700654 if [ $(echo $density | wc -w) -gt 1 ]; then
655 echo "tapas: Error: Multiple densities supplied: $density"
656 return
657 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800658 if [ $(echo $keys | wc -w) -gt 1 ]; then
659 echo "tapas: Error: Multiple keys supplied: $keys"
660 return
661 fi
Ying Wang67f02922012-08-22 10:25:20 -0700662
Ying Wang0a76df52015-06-08 11:57:26 -0700663 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700664 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000665 x86) product=aosp_x86;;
666 arm64) product=aosp_arm64;;
667 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700668 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800669 if [ -n "$keys" ]; then
670 product=${product/aosp_/aosp_${keys}_}
671 fi;
672
Joe Onoratoda12daf2010-06-09 18:18:31 -0700673 if [ -z "$variant" ]; then
674 variant=eng
675 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700676 if [ -z "$apps" ]; then
677 apps=all
678 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600679 if [ -z "$density" ]; then
680 density=alldpi
681 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700682
Ying Wang67f02922012-08-22 10:25:20 -0700683 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000684 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700685 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700686 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700687 export TARGET_BUILD_TYPE=release
688 export TARGET_BUILD_APPS=$apps
689
Ying Wang08800fd2016-03-03 20:57:21 -0800690 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700691 set_stuff_for_environment
692 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800693 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700694}
695
Martin Stjernholmf692c752021-04-12 00:01:10 +0100696# Configures the build to build unbundled Android modules (APEXes).
697# Run banchan with one or more module names (from apex{} modules).
698function banchan()
699{
700 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100701 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 -0600702 # TODO: Expand banchan to take release arguments (and update hmm() usage).
703 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100704 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100705 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 +0100706
707 if [ "$showHelp" != "" ]; then
708 $(gettop)/build/make/banchanHelp.sh
709 return
710 fi
711
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100712 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000713 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100714 elif [ $(echo $product | wc -w) -gt 1 ]; then
715 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100716 return
717 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600718 if [ $(echo $release | wc -w) -gt 1 ]; then
719 echo "banchan: Error: Multiple build releases supplied: $release"
720 return
721 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100722 if [ $(echo $variant | wc -w) -gt 1 ]; then
723 echo "banchan: Error: Multiple build variants supplied: $variant"
724 return
725 fi
726 if [ -z "$apps" ]; then
727 echo "banchan: Error: No modules supplied"
728 return
729 fi
730
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100731 case $product in
732 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100733 x86) product=module_x86;;
734 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100735 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100736 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000737 arm64only) product=module_arm64only;;
738 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100739 esac
740 if [ -z "$variant" ]; then
741 variant=eng
742 fi
743
744 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -0600745 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +0100746 export TARGET_BUILD_VARIANT=$variant
747 export TARGET_BUILD_DENSITY=alldpi
748 export TARGET_BUILD_TYPE=release
749
750 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
751 # case is different and it may diverge in the future.
752 export TARGET_BUILD_APPS=$apps
753
754 build_build_var_cache
755 set_stuff_for_environment
756 printconfig
757 destroy_build_var_cache
758}
759
Matt Alexanderd9c56562020-05-21 10:49:17 +0000760function croot()
761{
Christopher Ferris55257d22017-03-23 11:08:58 -0700762 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700763 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700764 if [ "$1" ]; then
765 \cd $(gettop)/$1
766 else
767 \cd $(gettop)
768 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700769 else
770 echo "Couldn't locate the top of the tree. Try setting TOP."
771 fi
772}
773
Matt Alexanderd9c56562020-05-21 10:49:17 +0000774function _croot()
775{
Anton Hanssonece9c482019-02-04 18:15:39 +0000776 local T=$(gettop)
777 if [ "$T" ]; then
778 local cur="${COMP_WORDS[COMP_CWORD]}"
779 k=0
780 for c in $(compgen -d ${T}/${cur}); do
781 COMPREPLY[k++]=${c#${T}/}/
782 done
783 fi
784}
785
Matt Alexanderd9c56562020-05-21 10:49:17 +0000786function cproj()
787{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700788 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700789 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700790 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700791 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
792 T=$PWD
793 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800794 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700795 return
796 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800797 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700798 done
Ying Wang9cd17642012-12-13 10:52:07 -0800799 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700800 echo "can't find Android.mk"
801}
802
Elliott Hughes86e99172024-03-21 16:55:08 +0000803# Ensure that we're always using the adb in the tree. This works around the fact
804# that bash caches $PATH lookups, so if you use adb before lunching/building the
805# one in your tree, you'll continue to get /usr/bin/adb or whatever even after
806# you have the one from your current tree on your path. Historically this would
807# cause confusion because glinux had adb in /usr/bin/ by default, though that
808# doesn't appear to be the case on my rodete hosts; it is however still the case
809# that my Mac has /usr/local/bin/adb installed by default and on the default
810# path.
Shaju Mathew21439002023-07-10 00:53:22 +0000811function adb() {
Elliott Hughes57c47b72024-03-22 15:46:59 +0000812 # We need `command which` because zsh has a built-in `which` that's more
813 # like `type`.
814 local ADB=$(command which adb)
Elliott Hughes7e7ff752024-03-20 17:23:40 -0700815 if [ -z "$ADB" ]; then
816 echo "Command adb not found; try lunch (and building) first?"
817 return 1
818 fi
Zhuoyao Zhang60dd9dd2024-04-19 00:07:59 +0000819 run_tool_with_logging "ADB" $ADB "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +0000820}
821
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000822function run_tool_with_logging() {
823 # Run commands in a subshell for us to handle forced terminations with a trap
824 # handler.
825 (
826 local tool_tag="$1"
827 shift
828 local tool_binary="$1"
829 shift
830
Zhuoyao Zhangef1c03f2024-05-07 22:18:37 +0000831 # If the logger is not configured, run the original command and return.
832 if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000833 "${tool_binary}" "${@}"
834 return $?
835 fi
836
837 # Otherwise, run the original command and call the logger when done.
838 local start_time
839 start_time=$(date +%s.%N)
840 local logger=${ANDROID_TOOL_LOGGER}
841
842 # Install a trap to call the logger even when the process terminates abnormally.
843 # The logger is run in the background and its output suppressed to avoid
844 # interference with the user flow.
845 trap '
846 exit_code=$?;
847 # Remove the trap to prevent duplicate log.
848 trap - EXIT;
849 "${logger}" \
Zhuoyao Zhangdfdf19f2024-05-20 18:31:12 +0000850 --tool_tag="${tool_tag}" \
851 --start_timestamp="${start_time}" \
852 --end_timestamp="$(date +%s.%N)" \
853 --tool_args="$*" \
854 --exit_code="${exit_code}" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +0000855 ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000856 > /dev/null 2>&1 &
857 exit ${exit_code}
858 ' SIGINT SIGTERM SIGQUIT EXIT
859
860 # Run the original command.
861 "${tool_binary}" "${@}"
862 )
863}
864
Daniel Sandler47e0a882013-07-30 13:23:52 -0400865# simplified version of ps; output in the form
866# <pid> <procname>
867function qpid() {
868 local prepend=''
869 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000870 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400871 prepend=' '
872 append='$'
873 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000874 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800875 echo "usage: qpid [[--exact] <process name|pid>"
876 return 255
877 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400878
879 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000880 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800881 qpid | \grep "$prepend$EXE$append"
882 else
883 adb shell ps \
884 | tr -d '\r' \
885 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
886 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400887}
888
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800889# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700890# that has the core-file-size limit set correctly
891#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800892# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700893# if its core-file-size limit is not set already.
894# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
895
Matt Alexanderd9c56562020-05-21 10:49:17 +0000896function coredump_setup()
897{
898 echo "Getting root...";
899 adb root;
900 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700901
Matt Alexanderd9c56562020-05-21 10:49:17 +0000902 echo "Remounting root partition read-write...";
903 adb shell mount -w -o remount -t rootfs rootfs;
904 sleep 1;
905 adb wait-for-device;
906 adb shell mkdir -p /cores;
907 adb shell mount -t tmpfs tmpfs /cores;
908 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700909
Matt Alexanderd9c56562020-05-21 10:49:17 +0000910 echo "Granting SELinux permission to dump in /cores...";
911 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700912
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913 echo "Set core pattern.";
914 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Ying Wang08800fd2016-03-03 20:57:21 -0800916 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700917}
918
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800919# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700921#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800922# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923# dump to actually be generated.
924
Matt Alexanderd9c56562020-05-21 10:49:17 +0000925function coredump_enable()
926{
927 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800928 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000929 printf "Expecting a PID!\n";
930 return;
931 fi;
932 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +0000933 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700934}
935
936# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000937# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700938#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800939# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700940# enabled globally.
941
Matt Alexanderd9c56562020-05-21 10:49:17 +0000942function core()
943{
944 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700945
Ying Wang08800fd2016-03-03 20:57:21 -0800946 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000947 printf "Expecting a PID!\n";
948 return;
949 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700950
Matt Alexanderd9c56562020-05-21 10:49:17 +0000951 local CORENAME=core.$PID;
952 local COREPATH=/cores/$CORENAME;
953 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700954
Matt Alexanderd9c56562020-05-21 10:49:17 +0000955 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700956
Matt Alexanderd9c56562020-05-21 10:49:17 +0000957 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800958 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000959 printf "\tSending SIG%s to %d...\n" $SIG $PID;
960 adb shell kill -$SIG $PID;
961 sleep 1;
962 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700963
Matt Alexanderd9c56562020-05-21 10:49:17 +0000964 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
965 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700966}
967
Christopher Tate744ee802009-11-12 15:33:08 -0800968# systemstack - dump the current stack trace of all threads in the system process
969# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000970function systemstack()
971{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800972 stacks system_server
973}
974
Michael Wrightaeed7212014-06-19 19:58:12 -0700975# Read the ELF header from /proc/$PID/exe to determine if the process is
976# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000977function is64bit()
978{
Ben Chengfba67bf2014-02-25 10:27:07 -0800979 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000980 if [ "$PID" ] ; then
981 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800982 echo "64"
983 else
984 echo ""
985 fi
986 else
987 echo ""
988 fi
989}
990
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991function gettargetarch
992{
Raghu Gandham8da43102012-07-25 19:57:22 -0700993 get_build_var TARGET_ARCH
994}
995
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996function getprebuilt
997{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700998 get_abs_build_var ANDROID_PREBUILTS
999}
1000
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001001# communicate with a running device or emulator, set up necessary state,
1002# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001003function runhat()
1004{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001005 # process standard adb options
1006 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001007 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001008 adbTarget=$1
1009 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001010 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001011 adbTarget="$1 $2"
1012 shift 2
1013 fi
1014 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001015 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001016
1017 # runhat options
1018 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001019
Matt Alexanderd9c56562020-05-21 10:49:17 +00001020 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001021 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001022 return
1023 fi
1024
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001025 # confirm hat is available
1026 if [ -z $(which hat) ]; then
1027 echo "hat is not available in this configuration."
1028 return
1029 fi
1030
Andy McFaddenb6289852010-07-12 08:00:19 -07001031 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001032 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001033 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001034 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001035 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001036 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001037 echo -n "> "
1038 read
1039
The Android Open Source Project88b60792009-03-03 19:28:42 -08001040 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001041
The Android Open Source Project88b60792009-03-03 19:28:42 -08001042 echo "Retrieving file $devFile..."
1043 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044
The Android Open Source Project88b60792009-03-03 19:28:42 -08001045 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001046
The Android Open Source Project88b60792009-03-03 19:28:42 -08001047 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001048 echo "View the output by pointing your browser at http://localhost:7000/"
1049 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001050 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001051}
1052
Matt Alexanderd9c56562020-05-21 10:49:17 +00001053function getbugreports()
1054{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001055 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001056
1057 if [ ! "$reports" ]; then
1058 echo "Could not locate any bugreports."
1059 return
1060 fi
1061
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001062 local report
1063 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001064 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001065 echo "/sdcard/bugreports/${report}"
1066 adb pull /sdcard/bugreports/${report} ${report}
1067 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001068 done
1069}
1070
Matt Alexanderd9c56562020-05-21 10:49:17 +00001071function getsdcardpath()
1072{
Victoria Lease1b296b42012-08-21 15:44:06 -07001073 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1074}
1075
Matt Alexanderd9c56562020-05-21 10:49:17 +00001076function getscreenshotpath()
1077{
Victoria Lease1b296b42012-08-21 15:44:06 -07001078 echo "$(getsdcardpath)/Pictures/Screenshots"
1079}
1080
Matt Alexanderd9c56562020-05-21 10:49:17 +00001081function getlastscreenshot()
1082{
Victoria Lease1b296b42012-08-21 15:44:06 -07001083 local screenshot_path=$(getscreenshotpath)
1084 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 +00001085 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001086 echo "No screenshots found."
1087 return
1088 fi
1089 echo "${screenshot}"
1090 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1091}
1092
Matt Alexanderd9c56562020-05-21 10:49:17 +00001093function startviewserver()
1094{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001095 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001096 if [ $# -gt 0 ]; then
1097 port=$1
1098 fi
1099 adb shell service call window 1 i32 $port
1100}
1101
Matt Alexanderd9c56562020-05-21 10:49:17 +00001102function stopviewserver()
1103{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001104 adb shell service call window 2
1105}
1106
Matt Alexanderd9c56562020-05-21 10:49:17 +00001107function isviewserverstarted()
1108{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001109 adb shell service call window 3
1110}
1111
Matt Alexanderd9c56562020-05-21 10:49:17 +00001112function key_home()
1113{
Romain Guyb84049a2010-10-04 16:56:11 -07001114 adb shell input keyevent 3
1115}
1116
Matt Alexanderd9c56562020-05-21 10:49:17 +00001117function key_back()
1118{
Romain Guyb84049a2010-10-04 16:56:11 -07001119 adb shell input keyevent 4
1120}
1121
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122function key_menu()
1123{
Romain Guyb84049a2010-10-04 16:56:11 -07001124 adb shell input keyevent 82
1125}
1126
Matt Alexanderd9c56562020-05-21 10:49:17 +00001127function smoketest()
1128{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001129 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1130 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1131 return
1132 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001133 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134 if [ ! "$T" ]; then
1135 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1136 return
1137 fi
1138
Ying Wang9cd17642012-12-13 10:52:07 -08001139 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001140 adb uninstall com.android.smoketest > /dev/null &&
1141 adb uninstall com.android.smoketest.tests > /dev/null &&
1142 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1143 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1144 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1145}
1146
The Android Open Source Project88b60792009-03-03 19:28:42 -08001147function godir () {
1148 if [[ -z "$1" ]]; then
1149 echo "Usage: godir <regex>"
1150 return
1151 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001152 local T=$(gettop)
1153 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001154 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001155 mkdir -p $OUT_DIR
1156 FILELIST=$OUT_DIR/filelist
1157 else
1158 FILELIST=$T/filelist
1159 fi
1160 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001161 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001162 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001163 echo " Done"
1164 echo ""
1165 fi
1166 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001167 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001168 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001169 echo "Not found"
1170 return
1171 fi
1172 local pathname
1173 local choice
1174 if [[ ${#lines[@]} > 1 ]]; then
1175 while [[ -z "$pathname" ]]; do
1176 local index=1
1177 local line
1178 for line in ${lines[@]}; do
1179 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001180 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001181 done
1182 echo
1183 echo -n "Select one: "
1184 unset choice
1185 read choice
1186 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1187 echo "Invalid choice"
1188 continue
1189 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001190 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001191 done
1192 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001193 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001194 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001195 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001196}
1197
Cole Faust3e192382021-10-25 13:29:15 -07001198# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001199function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001200 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001201 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1202 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1203 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001204 return 1
1205 fi
1206
1207 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001208 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001209 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001210 fi
1211 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001212 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001213}
1214
Cole Faust5d825b72022-10-26 18:16:44 -07001215# List all modules for the current device, as cached in all_modules.txt. If any build change is
1216# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001217function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001218 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001219}
1220
Joe Onorato2c1aa472021-02-25 16:42:39 -08001221# Get the path of a specific module in the android tree, as cached in module-info.json.
1222# If any build change is made, and it should be reflected in the output, you should run
1223# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001224function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001225 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001226 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001227 return 1
1228 fi
1229
Cole Faust24c36db2021-01-23 02:39:37 +00001230 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001231
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001232 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001233module = '$1'
1234module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1235if module not in module_info:
1236 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001237print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001238
1239 if [ -z "$relpath" ]; then
1240 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1241 return 1
1242 else
Rett Berg78d1c932019-01-24 14:34:23 -08001243 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001244 fi
1245}
1246
Joe Onorato2c1aa472021-02-25 16:42:39 -08001247# Get the path of a specific module in the android tree, as cached in module-info.json.
1248# If any build change is made, and it should be reflected in the output, you should run
1249# 'refreshmod' first. Note: This is the inverse of pathmod.
1250function dirmods() {
1251 if [[ $# -ne 1 ]]; then
1252 echo "usage: dirmods <path>" >&2
1253 return 1
1254 fi
1255
1256 verifymodinfo || return 1
1257
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001258 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001259dir = '$1'
1260while dir.endswith('/'):
1261 dir = dir[:-1]
1262prefix = dir + '/'
1263module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1264results = set()
1265for m in module_info.values():
1266 for path in m.get(u'path', []):
1267 if path == dir or path.startswith(prefix):
1268 name = m.get(u'module_name')
1269 if name:
1270 results.add(name)
1271for name in sorted(results):
1272 print(name)
1273"
1274}
1275
1276
Rett Berg78d1c932019-01-24 14:34:23 -08001277# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1278# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1279function gomod() {
1280 if [[ $# -ne 1 ]]; then
1281 echo "usage: gomod <module>" >&2
1282 return 1
1283 fi
1284
1285 local path="$(pathmod $@)"
1286 if [ -z "$path" ]; then
1287 return 1
1288 fi
1289 cd $path
1290}
1291
Cole Faust24c36db2021-01-23 02:39:37 +00001292# Gets the list of a module's installed outputs, as cached in module-info.json.
1293# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1294function outmod() {
1295 if [[ $# -ne 1 ]]; then
1296 echo "usage: outmod <module>" >&2
1297 return 1
1298 fi
1299
1300 verifymodinfo || return 1
1301
1302 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001303 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001304module = '$1'
1305module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1306if module not in module_info:
1307 exit(1)
1308for output in module_info[module]['installed']:
1309 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1310
1311 if [ $? -ne 0 ]; then
1312 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1313 return 1
1314 elif [ ! -z "$relpath" ]; then
1315 echo "$relpath"
1316 fi
1317}
1318
1319# adb install a module's apk, as cached in module-info.json. If any build change
1320# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1321# Usage: installmod [adb install arguments] <module>
1322# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1323function installmod() {
1324 if [[ $# -eq 0 ]]; then
1325 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001326 echo "" >&2
1327 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1328 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1329 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001330 return 1
1331 fi
1332
1333 local _path
1334 _path=$(outmod ${@:$#:1})
1335 if [ $? -ne 0 ]; then
1336 return 1
1337 fi
1338
1339 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1340 if [ -z "$_path" ]; then
1341 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1342 return 1
1343 fi
Cole Faust3e192382021-10-25 13:29:15 -07001344 local serial_device=""
1345 if [[ "$1" == "-s" ]]; then
1346 if [[ $# -le 2 ]]; then
1347 echo "-s requires an argument" >&2
1348 return 1
1349 fi
1350 serial_device="-s $2"
1351 shift 2
1352 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001353 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001354 echo adb $serial_device install ${@:1:$length} $_path
1355 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001356}
1357
dimitry73b84812018-12-11 18:06:00 +01001358function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001359 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001360 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001361}
1362
Alex Rayf0d08eb2013-03-08 15:15:06 -08001363# Print colored exit condition
1364function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001365 "$@"
1366 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001367 if [ $retval -ne 0 ]
1368 then
Jacky Cao89483b82015-05-15 22:12:53 +08001369 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001370 else
Jacky Cao89483b82015-05-15 22:12:53 +08001371 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001372 fi
1373 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001374}
1375
Matt Alexanderd9c56562020-05-21 10:49:17 +00001376function get_make_command()
1377{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001378 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1379 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001380 # Always use the real make if -C is passed in
1381 for arg in "$@"; do
1382 if [[ $arg == -C* ]]; then
1383 echo command make
1384 return
1385 fi
1386 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001387 echo build/soong/soong_ui.bash --make-mode
1388 else
1389 echo command make
1390 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001391}
1392
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001393function _trigger_build()
1394(
1395 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001396 local T=$(gettop)
1397 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001398 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1399 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001400 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1401 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001402 fi
Kevin Dagostino185109b2024-01-11 17:39:02 +00001403 local ret=$?
1404 if [[ ret -eq 0 && -z "${ANDROID_QUIET_BUILD:-}" && -n "${ANDROID_BUILD_BANNER}" ]]; then
1405 echo "${ANDROID_BUILD_BANNER}"
1406 fi
1407 return $ret
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001408)
1409
Matt Alexanderd9c56562020-05-21 10:49:17 +00001410function make()
1411{
Dan Willemsene9842242017-07-28 13:00:13 -07001412 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001413}
1414
Jim Tanga881a252018-06-19 16:34:41 +08001415# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001416function enable_zsh_completion() {
1417 # Don't override user's options if bash-style completion is already enabled.
1418 if ! declare -f complete >/dev/null; then
1419 autoload -U compinit && compinit
1420 autoload -U bashcompinit && bashcompinit
1421 fi
Jim Tanga881a252018-06-19 16:34:41 +08001422}
1423
1424function validate_current_shell() {
1425 local current_sh="$(ps -o command -p $$)"
1426 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001427 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001428 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001429 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001430 *zsh*)
1431 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001432 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001433 *)
Jim Tanga881a252018-06-19 16:34:41 +08001434 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 -07001435 ;;
1436 esac
Jim Tanga881a252018-06-19 16:34:41 +08001437}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001438
1439# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001440# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1441# load those.
1442#
1443# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001444function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001445 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001446 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001447 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001448 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 -08001449 if [ -n "$allowed" ]; then
1450 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1451 echo " $allowed"
1452 echo " $f"
1453 return
1454 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001455 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001456 done
1457
1458 allowed_files=
1459 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001460 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001461 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001462 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001463
1464 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001465 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001466 else
1467 echo "ignoring $f, not in $allowed"
1468 fi
Jim Tanga881a252018-06-19 16:34:41 +08001469 done
1470 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00001471
1472 if [[ "${PWD}" == /google/cog/* ]]; then
1473 f="build/make/cogsetup.sh"
1474 echo "including $f"; . "$T/$f"
1475 fi
Jim Tanga881a252018-06-19 16:34:41 +08001476}
Kenny Root52aa81c2011-07-15 11:07:06 -07001477
Dan Albertbab814f2020-08-26 15:34:53 -07001478function showcommands() {
1479 local T=$(gettop)
1480 if [[ -z "$TARGET_PRODUCT" ]]; then
1481 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1482 return
1483 fi
1484 case $(uname -s) in
1485 Darwin)
1486 PREBUILT_NAME=darwin-x86
1487 ;;
1488 Linux)
1489 PREBUILT_NAME=linux-x86
1490 ;;
1491 *)
1492 >&2 echo Unknown host $(uname -s)
1493 return
1494 ;;
1495 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001496 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07001497 if [[ "$1" == "--regenerate" ]]; then
1498 shift 1
1499 NINJA_ARGS="-t commands $@" m
1500 else
1501 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1502 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1503 -t commands "$@")
1504 fi
1505}
1506
Joe Onorato1b9ab292024-05-17 12:16:43 -07001507# These functions used to be here but are now standalone scripts
1508# in build/soong/bin. Unset these for the time being so the real
1509# script is picked up.
Joe Onorato23124752024-05-14 15:06:48 -07001510# TODO: Remove this some time after a suitable delay (maybe 2025?)
1511unset aninja
1512unset overrideflags
Joe Onorato1b9ab292024-05-17 12:16:43 -07001513unset m
1514unset mm
1515unset mmm
1516unset mma
1517unset mmma
1518unset cgrep
1519unset ggrep
1520unset gogrep
1521unset jgrep
1522unset jsongrep
1523unset ktgrep
1524unset mangrep
1525unset mgrep
1526unset owngrep
1527unset pygrep
1528unset rcgrep
1529unset resgrep
1530unset rsgrep
1531unset sepgrep
1532unset sgrep
1533unset tomlgrep
1534unset treegrep
1535unset syswrite
Joe Onorato143f9e02024-05-17 12:18:24 -07001536unset refreshmod
Cole Faust45844ab2022-08-30 13:59:07 -07001537
Colin Cross71319722023-10-24 10:58:46 -07001538
Jim Tanga881a252018-06-19 16:34:41 +08001539validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001540set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08001541source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001542addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001543
Joe Onorato23124752024-05-14 15:06:48 -07001544
1545