blob: d711b696341492e8b3e8025ceeba1871a32511e2 [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
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800865# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866# that has the core-file-size limit set correctly
867#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800868# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869# if its core-file-size limit is not set already.
870# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
871
Matt Alexanderd9c56562020-05-21 10:49:17 +0000872function coredump_setup()
873{
874 echo "Getting root...";
875 adb root;
876 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700877
Matt Alexanderd9c56562020-05-21 10:49:17 +0000878 echo "Remounting root partition read-write...";
879 adb shell mount -w -o remount -t rootfs rootfs;
880 sleep 1;
881 adb wait-for-device;
882 adb shell mkdir -p /cores;
883 adb shell mount -t tmpfs tmpfs /cores;
884 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700885
Matt Alexanderd9c56562020-05-21 10:49:17 +0000886 echo "Granting SELinux permission to dump in /cores...";
887 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700888
Matt Alexanderd9c56562020-05-21 10:49:17 +0000889 echo "Set core pattern.";
890 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700891
Ying Wang08800fd2016-03-03 20:57:21 -0800892 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700893}
894
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800895# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000896# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700897#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800898# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700899# dump to actually be generated.
900
Matt Alexanderd9c56562020-05-21 10:49:17 +0000901function coredump_enable()
902{
903 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800904 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000905 printf "Expecting a PID!\n";
906 return;
907 fi;
908 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +0000909 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700910}
911
912# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700914#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800915# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700916# enabled globally.
917
Matt Alexanderd9c56562020-05-21 10:49:17 +0000918function core()
919{
920 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700921
Ying Wang08800fd2016-03-03 20:57:21 -0800922 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000923 printf "Expecting a PID!\n";
924 return;
925 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700926
Matt Alexanderd9c56562020-05-21 10:49:17 +0000927 local CORENAME=core.$PID;
928 local COREPATH=/cores/$CORENAME;
929 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700930
Matt Alexanderd9c56562020-05-21 10:49:17 +0000931 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700932
Matt Alexanderd9c56562020-05-21 10:49:17 +0000933 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800934 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000935 printf "\tSending SIG%s to %d...\n" $SIG $PID;
936 adb shell kill -$SIG $PID;
937 sleep 1;
938 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700939
Matt Alexanderd9c56562020-05-21 10:49:17 +0000940 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
941 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700942}
943
Christopher Tate744ee802009-11-12 15:33:08 -0800944# systemstack - dump the current stack trace of all threads in the system process
945# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000946function systemstack()
947{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800948 stacks system_server
949}
950
Michael Wrightaeed7212014-06-19 19:58:12 -0700951# Read the ELF header from /proc/$PID/exe to determine if the process is
952# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000953function is64bit()
954{
Ben Chengfba67bf2014-02-25 10:27:07 -0800955 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000956 if [ "$PID" ] ; then
957 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800958 echo "64"
959 else
960 echo ""
961 fi
962 else
963 echo ""
964 fi
965}
966
Matt Alexanderd9c56562020-05-21 10:49:17 +0000967function gettargetarch
968{
Raghu Gandham8da43102012-07-25 19:57:22 -0700969 get_build_var TARGET_ARCH
970}
971
Matt Alexanderd9c56562020-05-21 10:49:17 +0000972function getprebuilt
973{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700974 get_abs_build_var ANDROID_PREBUILTS
975}
976
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800977# communicate with a running device or emulator, set up necessary state,
978# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000979function runhat()
980{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800981 # process standard adb options
982 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000983 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800984 adbTarget=$1
985 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +0000986 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800987 adbTarget="$1 $2"
988 shift 2
989 fi
990 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800992
993 # runhat options
994 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700995
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -0700997 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700998 return
999 fi
1000
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001001 # confirm hat is available
1002 if [ -z $(which hat) ]; then
1003 echo "hat is not available in this configuration."
1004 return
1005 fi
1006
Andy McFaddenb6289852010-07-12 08:00:19 -07001007 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001008 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001009 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001010 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001011 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001012 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001013 echo -n "> "
1014 read
1015
The Android Open Source Project88b60792009-03-03 19:28:42 -08001016 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001017
The Android Open Source Project88b60792009-03-03 19:28:42 -08001018 echo "Retrieving file $devFile..."
1019 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001020
The Android Open Source Project88b60792009-03-03 19:28:42 -08001021 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001022
The Android Open Source Project88b60792009-03-03 19:28:42 -08001023 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001024 echo "View the output by pointing your browser at http://localhost:7000/"
1025 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001026 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001027}
1028
Matt Alexanderd9c56562020-05-21 10:49:17 +00001029function getbugreports()
1030{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001031 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001032
1033 if [ ! "$reports" ]; then
1034 echo "Could not locate any bugreports."
1035 return
1036 fi
1037
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001038 local report
1039 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001040 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001041 echo "/sdcard/bugreports/${report}"
1042 adb pull /sdcard/bugreports/${report} ${report}
1043 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044 done
1045}
1046
Matt Alexanderd9c56562020-05-21 10:49:17 +00001047function getsdcardpath()
1048{
Victoria Lease1b296b42012-08-21 15:44:06 -07001049 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1050}
1051
Matt Alexanderd9c56562020-05-21 10:49:17 +00001052function getscreenshotpath()
1053{
Victoria Lease1b296b42012-08-21 15:44:06 -07001054 echo "$(getsdcardpath)/Pictures/Screenshots"
1055}
1056
Matt Alexanderd9c56562020-05-21 10:49:17 +00001057function getlastscreenshot()
1058{
Victoria Lease1b296b42012-08-21 15:44:06 -07001059 local screenshot_path=$(getscreenshotpath)
1060 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 +00001061 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001062 echo "No screenshots found."
1063 return
1064 fi
1065 echo "${screenshot}"
1066 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1067}
1068
Matt Alexanderd9c56562020-05-21 10:49:17 +00001069function startviewserver()
1070{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001071 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001072 if [ $# -gt 0 ]; then
1073 port=$1
1074 fi
1075 adb shell service call window 1 i32 $port
1076}
1077
Matt Alexanderd9c56562020-05-21 10:49:17 +00001078function stopviewserver()
1079{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001080 adb shell service call window 2
1081}
1082
Matt Alexanderd9c56562020-05-21 10:49:17 +00001083function isviewserverstarted()
1084{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001085 adb shell service call window 3
1086}
1087
Matt Alexanderd9c56562020-05-21 10:49:17 +00001088function key_home()
1089{
Romain Guyb84049a2010-10-04 16:56:11 -07001090 adb shell input keyevent 3
1091}
1092
Matt Alexanderd9c56562020-05-21 10:49:17 +00001093function key_back()
1094{
Romain Guyb84049a2010-10-04 16:56:11 -07001095 adb shell input keyevent 4
1096}
1097
Matt Alexanderd9c56562020-05-21 10:49:17 +00001098function key_menu()
1099{
Romain Guyb84049a2010-10-04 16:56:11 -07001100 adb shell input keyevent 82
1101}
1102
Matt Alexanderd9c56562020-05-21 10:49:17 +00001103function smoketest()
1104{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001105 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1106 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1107 return
1108 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001109 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001110 if [ ! "$T" ]; then
1111 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1112 return
1113 fi
1114
Ying Wang9cd17642012-12-13 10:52:07 -08001115 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001116 adb uninstall com.android.smoketest > /dev/null &&
1117 adb uninstall com.android.smoketest.tests > /dev/null &&
1118 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1119 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1120 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1121}
1122
The Android Open Source Project88b60792009-03-03 19:28:42 -08001123function godir () {
1124 if [[ -z "$1" ]]; then
1125 echo "Usage: godir <regex>"
1126 return
1127 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001128 local T=$(gettop)
1129 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001130 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001131 mkdir -p $OUT_DIR
1132 FILELIST=$OUT_DIR/filelist
1133 else
1134 FILELIST=$T/filelist
1135 fi
1136 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001137 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001138 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001139 echo " Done"
1140 echo ""
1141 fi
1142 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001143 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001144 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001145 echo "Not found"
1146 return
1147 fi
1148 local pathname
1149 local choice
1150 if [[ ${#lines[@]} > 1 ]]; then
1151 while [[ -z "$pathname" ]]; do
1152 local index=1
1153 local line
1154 for line in ${lines[@]}; do
1155 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001156 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001157 done
1158 echo
1159 echo -n "Select one: "
1160 unset choice
1161 read choice
1162 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1163 echo "Invalid choice"
1164 continue
1165 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001166 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001167 done
1168 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001169 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001170 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001171 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001172}
1173
Rett Berg78d1c932019-01-24 14:34:23 -08001174# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1175# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Joe Onorato6b543832024-05-23 12:26:50 -07001176# Note: This function is in envsetup because changing the directory needs to happen in the current
1177# shell. All other functions that use module-info.json should be in build/soong/bin.
Rett Berg78d1c932019-01-24 14:34:23 -08001178function gomod() {
1179 if [[ $# -ne 1 ]]; then
1180 echo "usage: gomod <module>" >&2
1181 return 1
1182 fi
1183
1184 local path="$(pathmod $@)"
1185 if [ -z "$path" ]; then
1186 return 1
1187 fi
1188 cd $path
1189}
1190
dimitry73b84812018-12-11 18:06:00 +01001191function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001192 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001193 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001194}
1195
Alex Rayf0d08eb2013-03-08 15:15:06 -08001196# Print colored exit condition
1197function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001198 "$@"
1199 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001200 if [ $retval -ne 0 ]
1201 then
Jacky Cao89483b82015-05-15 22:12:53 +08001202 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001203 else
Jacky Cao89483b82015-05-15 22:12:53 +08001204 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001205 fi
1206 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001207}
1208
Matt Alexanderd9c56562020-05-21 10:49:17 +00001209function get_make_command()
1210{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001211 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1212 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001213 # Always use the real make if -C is passed in
1214 for arg in "$@"; do
1215 if [[ $arg == -C* ]]; then
1216 echo command make
1217 return
1218 fi
1219 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001220 echo build/soong/soong_ui.bash --make-mode
1221 else
1222 echo command make
1223 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001224}
1225
Matt Alexanderd9c56562020-05-21 10:49:17 +00001226function make()
1227{
Dan Willemsene9842242017-07-28 13:00:13 -07001228 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001229}
1230
Jim Tanga881a252018-06-19 16:34:41 +08001231# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001232function enable_zsh_completion() {
1233 # Don't override user's options if bash-style completion is already enabled.
1234 if ! declare -f complete >/dev/null; then
1235 autoload -U compinit && compinit
1236 autoload -U bashcompinit && bashcompinit
1237 fi
Jim Tanga881a252018-06-19 16:34:41 +08001238}
1239
1240function validate_current_shell() {
1241 local current_sh="$(ps -o command -p $$)"
1242 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001243 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001244 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001245 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001246 *zsh*)
1247 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001249 *)
Jim Tanga881a252018-06-19 16:34:41 +08001250 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 -07001251 ;;
1252 esac
Jim Tanga881a252018-06-19 16:34:41 +08001253}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001254
1255# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001256# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1257# load those.
1258#
1259# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001260function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001261 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001262 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001263 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001264 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 -08001265 if [ -n "$allowed" ]; then
1266 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1267 echo " $allowed"
1268 echo " $f"
1269 return
1270 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001271 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001272 done
1273
1274 allowed_files=
1275 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001276 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001277 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001278 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001279
1280 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001281 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001282 else
1283 echo "ignoring $f, not in $allowed"
1284 fi
Jim Tanga881a252018-06-19 16:34:41 +08001285 done
1286 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00001287
1288 if [[ "${PWD}" == /google/cog/* ]]; then
1289 f="build/make/cogsetup.sh"
1290 echo "including $f"; . "$T/$f"
1291 fi
Jim Tanga881a252018-06-19 16:34:41 +08001292}
Kenny Root52aa81c2011-07-15 11:07:06 -07001293
Dan Albertbab814f2020-08-26 15:34:53 -07001294function showcommands() {
1295 local T=$(gettop)
1296 if [[ -z "$TARGET_PRODUCT" ]]; then
1297 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1298 return
1299 fi
1300 case $(uname -s) in
1301 Darwin)
1302 PREBUILT_NAME=darwin-x86
1303 ;;
1304 Linux)
1305 PREBUILT_NAME=linux-x86
1306 ;;
1307 *)
1308 >&2 echo Unknown host $(uname -s)
1309 return
1310 ;;
1311 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001312 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07001313 if [[ "$1" == "--regenerate" ]]; then
1314 shift 1
1315 NINJA_ARGS="-t commands $@" m
1316 else
1317 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1318 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1319 -t commands "$@")
1320 fi
1321}
1322
Joe Onorato1b9ab292024-05-17 12:16:43 -07001323# These functions used to be here but are now standalone scripts
1324# in build/soong/bin. Unset these for the time being so the real
1325# script is picked up.
Joe Onorato23124752024-05-14 15:06:48 -07001326# TODO: Remove this some time after a suitable delay (maybe 2025?)
Joe Onorato6b543832024-05-23 12:26:50 -07001327unset allmod
Joe Onorato23124752024-05-14 15:06:48 -07001328unset aninja
Joe Onorato1b9ab292024-05-17 12:16:43 -07001329unset cgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001330unset dirmods
Joe Onorato1b9ab292024-05-17 12:16:43 -07001331unset ggrep
1332unset gogrep
Joe Onorato6b543832024-05-23 12:26:50 -07001333unset installmod
Joe Onorato1b9ab292024-05-17 12:16:43 -07001334unset jgrep
1335unset jsongrep
1336unset ktgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001337unset m
Joe Onorato1b9ab292024-05-17 12:16:43 -07001338unset mangrep
1339unset mgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001340unset mm
1341unset mma
1342unset mmm
1343unset mmma
1344unset outmod
1345unset overrideflags
Joe Onorato1b9ab292024-05-17 12:16:43 -07001346unset owngrep
Joe Onorato6b543832024-05-23 12:26:50 -07001347unset pathmod
Joe Onorato1b9ab292024-05-17 12:16:43 -07001348unset pygrep
Joe Onorato6b543832024-05-23 12:26:50 -07001349unset qpid
Joe Onorato1b9ab292024-05-17 12:16:43 -07001350unset rcgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001351unset refreshmod
Joe Onorato1b9ab292024-05-17 12:16:43 -07001352unset resgrep
1353unset rsgrep
1354unset sepgrep
1355unset sgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001356unset syswrite
Joe Onorato1b9ab292024-05-17 12:16:43 -07001357unset tomlgrep
1358unset treegrep
Cole Faust45844ab2022-08-30 13:59:07 -07001359
Colin Cross71319722023-10-24 10:58:46 -07001360
Jim Tanga881a252018-06-19 16:34:41 +08001361validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001362set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08001363source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001364addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001365
Joe Onorato23124752024-05-14 15:06:48 -07001366
1367