blob: af3317439bf44205c40387b467a29c40f0ab8afb [file] [log] [blame]
Joe Onorato344e4042022-12-05 15:15:36 -08001# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# gettop is duplicated here and in shell_utils.mk, because it's difficult
16# to find shell_utils.make without it for all the novel ways this file can be
17# sourced. Other common functions should only be in one place or the other.
18function _gettop_once
19{
20 local TOPFILE=build/make/core/envsetup.mk
21 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
22 # The following circumlocution ensures we remove symlinks from TOP.
23 (cd "$TOP"; PWD= /bin/pwd)
24 else
25 if [ -f $TOPFILE ] ; then
26 # The following circumlocution (repeated below as well) ensures
27 # that we record the true directory name and not one that is
28 # faked up with symlink names.
29 PWD= /bin/pwd
30 else
31 local HERE=$PWD
32 local T=
33 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
34 \cd ..
35 T=`PWD= /bin/pwd -P`
36 done
37 \cd "$HERE"
38 if [ -f "$T/$TOPFILE" ]; then
39 echo "$T"
40 fi
41 fi
42 fi
43}
44T=$(_gettop_once)
45if [ ! "$T" ]; then
46 echo "Couldn't locate the top of the tree. Always source build/envsetup.sh from the root of the tree." >&2
47 return 1
48fi
49IMPORTING_ENVSETUP=true source $T/build/make/shell_utils.sh
50
51
52# Help
Scott Anderson1a5fc952012-03-07 17:15:06 -080053function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070054cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -070055
56Run "m help" for help with the build system itself.
57
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -080058Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Greg Kaiser5e2d3392023-10-27 14:15:48 -060059- lunch: lunch <product_name>-<release_type>-<build_variant>
Steven Moreland62054a42018-12-06 10:11:40 -080060 Selects <product_name> as the product to build, and <build_variant> as the variant to
61 build, and stores those selections in the environment to be read by subsequent
62 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080063- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010064 Sets up the build environment for building unbundled apps (APKs).
Martin Stjernholma73fe8d2023-12-22 13:32:42 +000065- banchan: banchan <module1> [<module2> ...] \\
Ulya Trafimovich08c381b2023-06-12 15:33:09 +010066 [arm|x86|arm64|riscv64|x86_64|arm64_only|x86_64only] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010067 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000068- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080069- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070070- mm: Builds and installs all of the modules in the current directory, and their
71 dependencies.
72- mmm: Builds and installs all of the modules in the supplied directories, and their
73 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080074 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070075- mma: Same as 'mm'
76- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080077- provision: Flash device with all required partitions. Options will be passed on to fastboot.
78- cgrep: Greps on all local C/C++ files.
79- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010080- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080081- jgrep: Greps on all local Java files.
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +010082- jsongrep: Greps on all local Json files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090083- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080084- resgrep: Greps on all local res/*.xml files.
85- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070086- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060087- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080088- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080089- sepgrep: Greps on all local sepolicy files.
90- sgrep: Greps on all local source files.
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +010091- tomlgrep: Greps on all local Toml files.
DroidFreak32a2781982020-11-26 11:30:19 -050092- pygrep: Greps on all local Python files.
Steven Moreland62054a42018-12-06 10:11:40 -080093- godir: Go to the directory containing a file.
94- allmod: List all modules.
95- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000096- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080097- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000098- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080099- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +0000100- installmod: Adb installs a module's built APK.
101- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +0000102- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700103
Roland Levillain39341922015-10-20 12:48:19 +0100104Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -0700105- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -0800106- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700107
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108Look at the source to view more functions. The complete list is:
109EOF
Christopher Ferris55257d22017-03-23 11:08:58 -0700110 local T=$(gettop)
111 local A=""
112 local i
Jacky Cao89483b82015-05-15 22:12:53 +0800113 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700114 A="$A $i"
115 done
116 echo $A
117}
118
Ying Wang08800fd2016-03-03 20:57:21 -0800119# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000120function build_build_var_cache()
121{
Christopher Ferris55257d22017-03-23 11:08:58 -0700122 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -0800123 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +0800124 cached_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
125 cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
Ying Wang08800fd2016-03-03 20:57:21 -0800126 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -0800127 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +0800128 --vars="${cached_vars[*]}" \
129 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -0700130 --var-prefix=var_cache_ \
131 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -0800132 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +0000133 if [ $ret -ne 0 ]
134 then
Ying Wang08800fd2016-03-03 20:57:21 -0800135 unset build_dicts_script
136 return $ret
137 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700138 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -0800139 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -0800140 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -0800141 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +0000142 if [ $ret -ne 0 ]
143 then
Ying Wang08800fd2016-03-03 20:57:21 -0800144 return $ret
145 fi
146 BUILD_VAR_CACHE_READY="true"
147}
148
Ying Wangf0cb3972016-03-04 13:56:23 -0800149# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -0800150# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000151function destroy_build_var_cache()
152{
Ying Wang08800fd2016-03-03 20:57:21 -0800153 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -0700154 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800155 for v in $cached_vars; do
156 unset var_cache_$v
157 done
158 unset cached_vars
159 for v in $cached_abs_vars; do
160 unset abs_var_cache_$v
161 done
162 unset cached_abs_vars
163}
164
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700165# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166function get_abs_build_var()
167{
168 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
169 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800170 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200171 return
Ying Wang08800fd2016-03-03 20:57:21 -0800172 fi
173
Christopher Ferris55257d22017-03-23 11:08:58 -0700174 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700175 if [ ! "$T" ]; then
176 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
177 return
178 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700179 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700180}
181
182# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000183function get_build_var()
184{
185 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
186 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800187 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100188 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800189 fi
190
Christopher Ferris55257d22017-03-23 11:08:58 -0700191 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700192 if [ ! "$T" ]; then
193 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100194 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700196 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800197}
198
Joe Onorato32b2aa32024-05-14 13:54:13 -0700199# This logic matches envsetup.mk
200function get_host_prebuilt_prefix
201{
202 local un=$(uname)
203 if [[ $un == "Linux" ]] ; then
204 echo linux-x86
205 elif [[ $un == "Darwin" ]] ; then
206 echo darwin-x86
207 else
208 echo "Error: Invalid host operating system: $un" 1>&2
209 fi
210}
211
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800212# Add directories to PATH that are dependent on the lunch target.
213# For directories that are not lunch-specific, add them in set_global_paths
214function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000215{
Christopher Ferris55257d22017-03-23 11:08:58 -0700216 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700217 if [ ! "$T" ]; then
218 echo "Couldn't locate the top of the tree. Try setting TOP."
219 return
220 fi
221
222 ##################################################################
223 # #
224 # Read me before you modify this code #
225 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800226 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
227 # adding to PATH, and the next time it is run, it removes that #
228 # from PATH. This is required so lunch can be run more than #
229 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700230 # #
231 ##################################################################
232
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800233 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700234 # due to "C:\Program Files" being in the path.
235
Kevin Dagostino185109b2024-01-11 17:39:02 +0000236 # Handle compat with the old ANDROID_BUILD_PATHS variable.
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800237 # TODO: Remove this after we think everyone has lunched again.
238 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
239 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
240 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700241 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000242 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700243 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800244 # strip leading ':', if any
245 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800246 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500247 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700248
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800249 # Out with the old...
250 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
251 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
252 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800253
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800254 # And in with the new...
255 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
256 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700257
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800258 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700259 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800260 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200261
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800262 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
263 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
264
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800265 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800266 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800267 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
268 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
269 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800270
Colin Crosse97e6932017-06-30 16:01:45 -0700271 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
272 export JAVA_HOME=$ANDROID_JAVA_HOME
273 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800274 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
275
276 # Fix up PYTHONPATH
277 if [ -n $ANDROID_PYTHONPATH ]; then
278 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
279 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000280 # //development/python-packages contains both a pseudo-PYTHONPATH which
281 # mimics an already assembled venv, but also contains real Python packages
282 # that are not in that layout until they are installed. We can fake it for
283 # the latter type by adding the package source directories to the PYTHONPATH
284 # directly. For the former group, we only need to add the python-packages
285 # directory itself.
286 #
287 # This could be cleaned up by converting the remaining packages that are in
288 # the first category into a typical python source layout (that is, another
289 # layer of directory nesting) and automatically adding all subdirectories of
290 # python-packages to the PYTHONPATH instead of manually curating this. We
291 # can't convert the packages like adb to the other style because doing so
292 # would prevent exporting type info from those packages.
293 #
294 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000295 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800296 if [ -n $VENDOR_PYTHONPATH ]; then
297 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
298 fi
299 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500300
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800301 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700302 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
303 export OUT=$ANDROID_PRODUCT_OUT
304
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700305 unset ANDROID_HOST_OUT
306 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
307
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900308 unset ANDROID_SOONG_HOST_OUT
309 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
310
Simran Basidd050ed2017-02-13 13:46:48 -0800311 unset ANDROID_HOST_OUT_TESTCASES
312 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
313
314 unset ANDROID_TARGET_OUT_TESTCASES
315 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
316
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800317 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800318 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800319}
320
321# Add directories to PATH that are NOT dependent on the lunch target.
322# For directories that are lunch-specific, add them in set_lunch_paths
323function set_global_paths()
324{
325 local T=$(gettop)
326 if [ ! "$T" ]; then
327 echo "Couldn't locate the top of the tree. Try setting TOP."
328 return
329 fi
330
331 ##################################################################
332 # #
333 # Read me before you modify this code #
334 # #
335 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
336 # adding to PATH, and the next time it is run, it removes that #
337 # from PATH. This is required so envsetup.sh can be sourced #
338 # more than once and still have working paths. #
339 # #
340 ##################################################################
341
342 # Out with the old...
343 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
344 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
345 fi
346
347 # And in with the new...
Joe Onorato84e61d02024-02-02 22:53:39 -0800348 ANDROID_GLOBAL_BUILD_PATHS=$T/build/soong/bin
LaMont Jonese8a3be22024-02-12 09:55:41 -0800349 ANDROID_GLOBAL_BUILD_PATHS+=:$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800350 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
351 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800352
353 # add kernel specific binaries
354 if [ $(uname -s) = Linux ] ; then
355 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
356 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
357 fi
358
359 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
360 # to ensure that the corresponding 'emulator' binaries are used.
361 case $(uname -s) in
362 Darwin)
363 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
364 ;;
365 Linux)
366 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
367 ;;
368 *)
369 ANDROID_EMULATOR_PREBUILTS=
370 ;;
371 esac
372 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
373 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
374 export ANDROID_EMULATOR_PREBUILTS
375 fi
376
377 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800378 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700379}
380
Matt Alexanderd9c56562020-05-21 10:49:17 +0000381function printconfig()
382{
Christopher Ferris55257d22017-03-23 11:08:58 -0700383 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800384 if [ ! "$T" ]; then
385 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
386 return
387 fi
388 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700389}
390
Matt Alexanderd9c56562020-05-21 10:49:17 +0000391function set_stuff_for_environment()
392{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800393 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800394 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700395
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800396 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700397}
398
Matt Alexanderd9c56562020-05-21 10:49:17 +0000399function set_sequence_number()
400{
Colin Cross88737132017-03-21 17:41:03 -0700401 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700402}
403
Makoto Onukida971062018-06-18 10:15:19 -0700404# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
405function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800406 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700407 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800408 *:"$cmd":*)
409 return 1
410 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700411 esac
412 return 0
413}
414
Matt Alexanderd9c56562020-05-21 10:49:17 +0000415function addcompletions()
416{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700417 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700418
Jim Tanga881a252018-06-19 16:34:41 +0800419 # Keep us from trying to run in something that's neither bash nor zsh.
420 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700421 return
422 fi
423
424 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800425 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700426 return
427 fi
428
Jim Tanga881a252018-06-19 16:34:41 +0800429 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000430 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800431 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800432 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400433 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800434 )
Makoto Onukida971062018-06-18 10:15:19 -0700435 # Completion can be disabled selectively to allow users to use non-standard completion.
436 # e.g.
437 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
438 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400439 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800440 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400441 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000442 if [ ! -f "$f" ]; then
443 echo "Warning: completion file $f not found"
444 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700445 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700446 fi
447 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700448
Anton Hanssonece9c482019-02-04 18:15:39 +0000449 if [ -z "$ZSH_VERSION" ]; then
450 # Doesn't work in zsh.
451 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400452 # TODO(b/244559459): Support b autocompletion for zsh
453 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000454 fi
Jim Tanga881a252018-06-19 16:34:41 +0800455 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800456
Cole Faust24c36db2021-01-23 02:39:37 +0000457 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100458 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000459 complete -F _complete_android_module_names outmod
460 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000461 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100462 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700463}
464
Matt Alexanderd9c56562020-05-21 10:49:17 +0000465function add_lunch_combo()
466{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800467 if [ -n "$ZSH_VERSION" ]; then
468 echo -n "${funcfiletrace[1]}: "
469 else
470 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
471 fi
472 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 -0800473}
474
Matt Alexanderd9c56562020-05-21 10:49:17 +0000475function print_lunch_menu()
476{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100478 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600479 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 +0100480 local ret=$?
481
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 echo
483 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100485
Matt Alexanderd9c56562020-05-21 10:49:17 +0000486 if [ $ret -ne 0 ]
487 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100488 echo "Warning: Cannot display lunch menu."
489 echo
490 echo "Note: You can invoke lunch with an explicit target:"
491 echo
492 echo " usage: lunch [target]" >&2
493 echo
494 return
495 fi
496
Will Burr40401202022-02-07 12:12:01 +0000497 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800498
499 local i=1
500 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700501 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502 do
503 echo " $i. $choice"
504 i=$(($i+1))
505 done
506
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700507 echo
508}
509
Matt Alexanderd9c56562020-05-21 10:49:17 +0000510function lunch()
511{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 local answer
513
Steven Moreland92793dc2020-02-25 18:30:18 -0800514 if [[ $# -gt 1 ]]; then
515 echo "usage: lunch [target]" >&2
516 return 1
517 fi
518
Will Burr40401202022-02-07 12:12:01 +0000519 local used_lunch_menu=0
520
Steven Moreland92793dc2020-02-25 18:30:18 -0800521 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800522 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700523 else
524 print_lunch_menu
Greg Kaiserbae4c572024-01-04 15:57:54 -0700525 echo "Which would you like? [aosp_cf_x86_64_phone-trunk_staging-eng]"
Greg Kaiser9a5a5262023-11-02 16:54:27 +0000526 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 -0800527 read answer
Will Burr40401202022-02-07 12:12:01 +0000528 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700529 fi
530
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800531 local selection=
532
Matt Alexanderd9c56562020-05-21 10:49:17 +0000533 if [ -z "$answer" ]
534 then
Greg Kaiserbae4c572024-01-04 15:57:54 -0700535 selection=aosp_cf_x86_64_phone-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000536 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
537 then
Fabián Cañas03dd0282024-01-05 16:57:39 -0500538 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 +0000539 if [ $answer -le ${#choices[@]} ]
540 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800541 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000542 if [ -n "$ZSH_VERSION" ]
543 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800544 selection=${choices[$(($answer))]}
545 else
546 selection=${choices[$(($answer-1))]}
547 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 fi
Colin Cross88737132017-03-21 17:41:03 -0700549 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800550 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700551 fi
552
Joe Onoratoda12daf2010-06-09 18:18:31 -0700553 export TARGET_BUILD_APPS=
554
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600555 # This must be <product>-<release>-<variant>
556 local product release variant
557 # Split string on the '-' character.
558 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600560 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000561 then
Ying Wang08800fd2016-03-03 20:57:21 -0800562 echo
Colin Cross88737132017-03-21 17:41:03 -0700563 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600564 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700565 return 1
566 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567
Colin Cross88737132017-03-21 17:41:03 -0700568 TARGET_PRODUCT=$product \
569 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000570 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700571 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000572 if [ $? -ne 0 ]
573 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100574 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
575 then
576 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
577 fi
Colin Cross88737132017-03-21 17:41:03 -0700578 return 1
579 fi
Colin Cross88737132017-03-21 17:41:03 -0700580 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
581 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600582 export TARGET_RELEASE=$release
583 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700584 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585
Mark White2c807512023-10-25 17:27:07 +0000586 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
587
588 set_stuff_for_environment
589 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
590
591 if [ "${TARGET_BUILD_VARIANT}" = "userdebug" ] && [[ -z "${ANDROID_QUIET_BUILD}" ]]; then
592 echo
593 echo "Want FASTER LOCAL BUILDS? Use -eng instead of -userdebug (however for" \
594 "performance benchmarking continue to use userdebug)"
595 fi
Will Burr40401202022-02-07 12:12:01 +0000596 if [ $used_lunch_menu -eq 1 ]; then
597 echo
598 echo "Hint: next time you can simply run 'lunch $selection'"
599 fi
600
Ying Wang08800fd2016-03-03 20:57:21 -0800601 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400602
603 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
604 check_mu_config
605 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700606}
607
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700608unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700609# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000610function _lunch()
611{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700612 local cur prev opts
613 COMPREPLY=()
614 cur="${COMP_WORDS[COMP_CWORD]}"
615 prev="${COMP_WORDS[COMP_CWORD-1]}"
616
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700617 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800618 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700619 fi
620
621 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700622 return 0
623}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700624
Joe Onoratoda12daf2010-06-09 18:18:31 -0700625# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700626# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000627function tapas()
628{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700629 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800630 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000631 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
632 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700633 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700634 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 -0800635 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
636 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)"
637
Joe Onoratoda12daf2010-06-09 18:18:31 -0700638
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700639 if [ "$showHelp" != "" ]; then
640 $(gettop)/build/make/tapasHelp.sh
641 return
642 fi
643
Ying Wang67f02922012-08-22 10:25:20 -0700644 if [ $(echo $arch | wc -w) -gt 1 ]; then
645 echo "tapas: Error: Multiple build archs supplied: $arch"
646 return
647 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000648 if [ $(echo $release | wc -w) -gt 1 ]; then
649 echo "tapas: Error: Multiple build releases supplied: $release"
650 return
651 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700652 if [ $(echo $variant | wc -w) -gt 1 ]; then
653 echo "tapas: Error: Multiple build variants supplied: $variant"
654 return
655 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700656 if [ $(echo $density | wc -w) -gt 1 ]; then
657 echo "tapas: Error: Multiple densities supplied: $density"
658 return
659 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800660 if [ $(echo $keys | wc -w) -gt 1 ]; then
661 echo "tapas: Error: Multiple keys supplied: $keys"
662 return
663 fi
Ying Wang67f02922012-08-22 10:25:20 -0700664
Ying Wang0a76df52015-06-08 11:57:26 -0700665 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700666 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000667 x86) product=aosp_x86;;
668 arm64) product=aosp_arm64;;
669 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700670 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800671 if [ -n "$keys" ]; then
672 product=${product/aosp_/aosp_${keys}_}
673 fi;
674
Joe Onoratoda12daf2010-06-09 18:18:31 -0700675 if [ -z "$variant" ]; then
676 variant=eng
677 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700678 if [ -z "$apps" ]; then
679 apps=all
680 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600681 if [ -z "$density" ]; then
682 density=alldpi
683 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700684
Ying Wang67f02922012-08-22 10:25:20 -0700685 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000686 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700687 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700688 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700689 export TARGET_BUILD_TYPE=release
690 export TARGET_BUILD_APPS=$apps
691
Ying Wang08800fd2016-03-03 20:57:21 -0800692 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700693 set_stuff_for_environment
694 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800695 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700696}
697
Martin Stjernholmf692c752021-04-12 00:01:10 +0100698# Configures the build to build unbundled Android modules (APEXes).
699# Run banchan with one or more module names (from apex{} modules).
700function banchan()
701{
702 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100703 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 -0600704 # TODO: Expand banchan to take release arguments (and update hmm() usage).
705 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100706 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100707 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 +0100708
709 if [ "$showHelp" != "" ]; then
710 $(gettop)/build/make/banchanHelp.sh
711 return
712 fi
713
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100714 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000715 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100716 elif [ $(echo $product | wc -w) -gt 1 ]; then
717 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100718 return
719 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600720 if [ $(echo $release | wc -w) -gt 1 ]; then
721 echo "banchan: Error: Multiple build releases supplied: $release"
722 return
723 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100724 if [ $(echo $variant | wc -w) -gt 1 ]; then
725 echo "banchan: Error: Multiple build variants supplied: $variant"
726 return
727 fi
728 if [ -z "$apps" ]; then
729 echo "banchan: Error: No modules supplied"
730 return
731 fi
732
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100733 case $product in
734 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100735 x86) product=module_x86;;
736 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100737 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100738 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000739 arm64only) product=module_arm64only;;
740 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100741 esac
742 if [ -z "$variant" ]; then
743 variant=eng
744 fi
745
746 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -0600747 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +0100748 export TARGET_BUILD_VARIANT=$variant
749 export TARGET_BUILD_DENSITY=alldpi
750 export TARGET_BUILD_TYPE=release
751
752 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
753 # case is different and it may diverge in the future.
754 export TARGET_BUILD_APPS=$apps
755
756 build_build_var_cache
757 set_stuff_for_environment
758 printconfig
759 destroy_build_var_cache
760}
761
Matt Alexanderd9c56562020-05-21 10:49:17 +0000762function croot()
763{
Christopher Ferris55257d22017-03-23 11:08:58 -0700764 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700765 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700766 if [ "$1" ]; then
767 \cd $(gettop)/$1
768 else
769 \cd $(gettop)
770 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700771 else
772 echo "Couldn't locate the top of the tree. Try setting TOP."
773 fi
774}
775
Matt Alexanderd9c56562020-05-21 10:49:17 +0000776function _croot()
777{
Anton Hanssonece9c482019-02-04 18:15:39 +0000778 local T=$(gettop)
779 if [ "$T" ]; then
780 local cur="${COMP_WORDS[COMP_CWORD]}"
781 k=0
782 for c in $(compgen -d ${T}/${cur}); do
783 COMPREPLY[k++]=${c#${T}/}/
784 done
785 fi
786}
787
Matt Alexanderd9c56562020-05-21 10:49:17 +0000788function cproj()
789{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700790 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700791 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700792 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700793 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
794 T=$PWD
795 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800796 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700797 return
798 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800799 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700800 done
Ying Wang9cd17642012-12-13 10:52:07 -0800801 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700802 echo "can't find Android.mk"
803}
804
Elliott Hughes86e99172024-03-21 16:55:08 +0000805# Ensure that we're always using the adb in the tree. This works around the fact
806# that bash caches $PATH lookups, so if you use adb before lunching/building the
807# one in your tree, you'll continue to get /usr/bin/adb or whatever even after
808# you have the one from your current tree on your path. Historically this would
809# cause confusion because glinux had adb in /usr/bin/ by default, though that
810# doesn't appear to be the case on my rodete hosts; it is however still the case
811# that my Mac has /usr/local/bin/adb installed by default and on the default
812# path.
Shaju Mathew21439002023-07-10 00:53:22 +0000813function adb() {
Elliott Hughes57c47b72024-03-22 15:46:59 +0000814 # We need `command which` because zsh has a built-in `which` that's more
815 # like `type`.
816 local ADB=$(command which adb)
Elliott Hughes7e7ff752024-03-20 17:23:40 -0700817 if [ -z "$ADB" ]; then
818 echo "Command adb not found; try lunch (and building) first?"
819 return 1
820 fi
Zhuoyao Zhang60dd9dd2024-04-19 00:07:59 +0000821 run_tool_with_logging "ADB" $ADB "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +0000822}
823
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000824function run_tool_with_logging() {
825 # Run commands in a subshell for us to handle forced terminations with a trap
826 # handler.
827 (
828 local tool_tag="$1"
829 shift
830 local tool_binary="$1"
831 shift
832
Zhuoyao Zhangef1c03f2024-05-07 22:18:37 +0000833 # If the logger is not configured, run the original command and return.
834 if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000835 "${tool_binary}" "${@}"
836 return $?
837 fi
838
839 # Otherwise, run the original command and call the logger when done.
840 local start_time
841 start_time=$(date +%s.%N)
842 local logger=${ANDROID_TOOL_LOGGER}
843
844 # Install a trap to call the logger even when the process terminates abnormally.
845 # The logger is run in the background and its output suppressed to avoid
846 # interference with the user flow.
847 trap '
848 exit_code=$?;
849 # Remove the trap to prevent duplicate log.
850 trap - EXIT;
851 "${logger}" \
852 --tool_tag "${tool_tag}" \
853 --start_timestamp "${start_time}" \
854 --end_timestamp "$(date +%s.%N)" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +0000855 --tool_args "$*" \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000856 --exit_code "${exit_code}" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +0000857 ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000858 > /dev/null 2>&1 &
859 exit ${exit_code}
860 ' SIGINT SIGTERM SIGQUIT EXIT
861
862 # Run the original command.
863 "${tool_binary}" "${@}"
864 )
865}
866
Daniel Sandler47e0a882013-07-30 13:23:52 -0400867# simplified version of ps; output in the form
868# <pid> <procname>
869function qpid() {
870 local prepend=''
871 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000872 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400873 prepend=' '
874 append='$'
875 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000876 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800877 echo "usage: qpid [[--exact] <process name|pid>"
878 return 255
879 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400880
881 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000882 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800883 qpid | \grep "$prepend$EXE$append"
884 else
885 adb shell ps \
886 | tr -d '\r' \
887 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
888 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400889}
890
Steven Moreland74114f12020-09-10 01:23:32 +0000891# syswrite - disable verity, reboot if needed, and remount image
892#
893# Easy way to make system.img/etc writable
894function syswrite() {
Steven Moreland7275a192023-04-17 17:21:01 +0000895 adb wait-for-device && adb root && adb wait-for-device || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +0000896 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +0000897 echo "rebooting"
Steven Moreland7275a192023-04-17 17:21:01 +0000898 adb reboot && adb wait-for-device && adb root && adb wait-for-device || return 1
Steven Moreland74114f12020-09-10 01:23:32 +0000899 fi
Steven Moreland7275a192023-04-17 17:21:01 +0000900 adb remount || return 1
Steven Moreland74114f12020-09-10 01:23:32 +0000901}
902
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800903# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700904# that has the core-file-size limit set correctly
905#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800906# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700907# if its core-file-size limit is not set already.
908# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
909
Matt Alexanderd9c56562020-05-21 10:49:17 +0000910function coredump_setup()
911{
912 echo "Getting root...";
913 adb root;
914 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Matt Alexanderd9c56562020-05-21 10:49:17 +0000916 echo "Remounting root partition read-write...";
917 adb shell mount -w -o remount -t rootfs rootfs;
918 sleep 1;
919 adb wait-for-device;
920 adb shell mkdir -p /cores;
921 adb shell mount -t tmpfs tmpfs /cores;
922 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923
Matt Alexanderd9c56562020-05-21 10:49:17 +0000924 echo "Granting SELinux permission to dump in /cores...";
925 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700926
Matt Alexanderd9c56562020-05-21 10:49:17 +0000927 echo "Set core pattern.";
928 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700929
Ying Wang08800fd2016-03-03 20:57:21 -0800930 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700931}
932
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800933# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000934# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700935#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800936# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700937# dump to actually be generated.
938
Matt Alexanderd9c56562020-05-21 10:49:17 +0000939function coredump_enable()
940{
941 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800942 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000943 printf "Expecting a PID!\n";
944 return;
945 fi;
946 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +0000947 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700948}
949
950# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000951# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700952#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800953# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700954# enabled globally.
955
Matt Alexanderd9c56562020-05-21 10:49:17 +0000956function core()
957{
958 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700959
Ying Wang08800fd2016-03-03 20:57:21 -0800960 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000961 printf "Expecting a PID!\n";
962 return;
963 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700964
Matt Alexanderd9c56562020-05-21 10:49:17 +0000965 local CORENAME=core.$PID;
966 local COREPATH=/cores/$CORENAME;
967 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700968
Matt Alexanderd9c56562020-05-21 10:49:17 +0000969 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700970
Matt Alexanderd9c56562020-05-21 10:49:17 +0000971 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800972 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000973 printf "\tSending SIG%s to %d...\n" $SIG $PID;
974 adb shell kill -$SIG $PID;
975 sleep 1;
976 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700977
Matt Alexanderd9c56562020-05-21 10:49:17 +0000978 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
979 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700980}
981
Christopher Tate744ee802009-11-12 15:33:08 -0800982# systemstack - dump the current stack trace of all threads in the system process
983# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000984function systemstack()
985{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800986 stacks system_server
987}
988
Michael Wrightaeed7212014-06-19 19:58:12 -0700989# Read the ELF header from /proc/$PID/exe to determine if the process is
990# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991function is64bit()
992{
Ben Chengfba67bf2014-02-25 10:27:07 -0800993 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000994 if [ "$PID" ] ; then
995 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800996 echo "64"
997 else
998 echo ""
999 fi
1000 else
1001 echo ""
1002 fi
1003}
1004
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001005case `uname -s` in
1006 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001007 function sgrep()
1008 {
Joe Onorato95670782023-02-27 15:53:09 -08001009 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|kt|xml|sh|mk|aidl|vts|proto|rs|go)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001010 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001011 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001012
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001013 ;;
1014 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001015 function sgrep()
1016 {
Joe Onorato95670782023-02-27 15:53:09 -08001017 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|kt\|xml\|sh\|mk\|aidl\|vts\|proto\|rs\|go\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001018 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001019 }
1020 ;;
1021esac
1022
Matt Alexanderd9c56562020-05-21 10:49:17 +00001023function gettargetarch
1024{
Raghu Gandham8da43102012-07-25 19:57:22 -07001025 get_build_var TARGET_ARCH
1026}
1027
Matt Alexanderd9c56562020-05-21 10:49:17 +00001028function ggrep()
1029{
Mike Frysinger5e479732015-09-22 18:13:48 -04001030 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1031 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001032}
1033
Matt Alexanderd9c56562020-05-21 10:49:17 +00001034function gogrep()
1035{
Orion Hodson831472d2019-10-25 11:35:15 +01001036 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1037 -exec grep --color -n "$@" {} +
1038}
1039
Matt Alexanderd9c56562020-05-21 10:49:17 +00001040function jgrep()
1041{
Mike Frysinger5e479732015-09-22 18:13:48 -04001042 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1043 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044}
1045
Alistair Delva176e5342021-02-22 13:31:26 -08001046function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001047{
1048 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1049 -exec grep --color -n "$@" {} +
1050}
1051
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001052function jsongrep()
1053{
1054 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1055 -exec grep --color -n "$@" {} +
1056}
1057
1058function tomlgrep()
1059{
1060 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1061 -exec grep --color -n "$@" {} +
1062}
1063
Taesu Leeea0cecd2020-10-28 11:05:18 +09001064function ktgrep()
1065{
1066 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1067 -exec grep --color -n "$@" {} +
1068}
1069
Matt Alexanderd9c56562020-05-21 10:49:17 +00001070function cgrep()
1071{
Mike Frysinger5e479732015-09-22 18:13:48 -04001072 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
1073 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001074}
1075
Matt Alexanderd9c56562020-05-21 10:49:17 +00001076function resgrep()
1077{
Christopher Ferris55257d22017-03-23 11:08:58 -07001078 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001079 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1080 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1081 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001082}
1083
Matt Alexanderd9c56562020-05-21 10:49:17 +00001084function mangrep()
1085{
Mike Frysinger5e479732015-09-22 18:13:48 -04001086 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1087 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001088}
1089
Matt Alexanderd9c56562020-05-21 10:49:17 +00001090function owngrep()
1091{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001092 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1093 -exec grep --color -n "$@" {} +
1094}
1095
Matt Alexanderd9c56562020-05-21 10:49:17 +00001096function sepgrep()
1097{
Mike Frysinger5e479732015-09-22 18:13:48 -04001098 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1099 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001100}
1101
Matt Alexanderd9c56562020-05-21 10:49:17 +00001102function rcgrep()
1103{
Mike Frysinger5e479732015-09-22 18:13:48 -04001104 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1105 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001106}
1107
DroidFreak32a2781982020-11-26 11:30:19 -05001108function pygrep()
1109{
1110 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1111 -exec grep --color -n "$@" {} +
1112}
1113
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001114case `uname -s` in
1115 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001116 function mgrep()
1117 {
Orion Hodson831472d2019-10-25 11:35:15 +01001118 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001119 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001120 }
1121
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122 function treegrep()
1123 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001124 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001125 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001126 }
1127
1128 ;;
1129 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001130 function mgrep()
1131 {
Orion Hodson831472d2019-10-25 11:35:15 +01001132 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regextype posix-extended -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001133 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134 }
1135
Matt Alexanderd9c56562020-05-21 10:49:17 +00001136 function treegrep()
1137 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001138 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001139 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001140 }
1141
1142 ;;
1143esac
1144
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145function getprebuilt
1146{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001147 get_abs_build_var ANDROID_PREBUILTS
1148}
1149
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001150# communicate with a running device or emulator, set up necessary state,
1151# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001152function runhat()
1153{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001154 # process standard adb options
1155 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001157 adbTarget=$1
1158 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001159 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001160 adbTarget="$1 $2"
1161 shift 2
1162 fi
1163 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001164 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001165
1166 # runhat options
1167 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001168
Matt Alexanderd9c56562020-05-21 10:49:17 +00001169 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001170 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001171 return
1172 fi
1173
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001174 # confirm hat is available
1175 if [ -z $(which hat) ]; then
1176 echo "hat is not available in this configuration."
1177 return
1178 fi
1179
Andy McFaddenb6289852010-07-12 08:00:19 -07001180 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001181 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001182 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001183 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001184 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001185 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001186 echo -n "> "
1187 read
1188
The Android Open Source Project88b60792009-03-03 19:28:42 -08001189 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190
The Android Open Source Project88b60792009-03-03 19:28:42 -08001191 echo "Retrieving file $devFile..."
1192 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193
The Android Open Source Project88b60792009-03-03 19:28:42 -08001194 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001195
The Android Open Source Project88b60792009-03-03 19:28:42 -08001196 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197 echo "View the output by pointing your browser at http://localhost:7000/"
1198 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001199 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200}
1201
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202function getbugreports()
1203{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001204 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001205
1206 if [ ! "$reports" ]; then
1207 echo "Could not locate any bugreports."
1208 return
1209 fi
1210
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001211 local report
1212 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001213 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001214 echo "/sdcard/bugreports/${report}"
1215 adb pull /sdcard/bugreports/${report} ${report}
1216 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001217 done
1218}
1219
Matt Alexanderd9c56562020-05-21 10:49:17 +00001220function getsdcardpath()
1221{
Victoria Lease1b296b42012-08-21 15:44:06 -07001222 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1223}
1224
Matt Alexanderd9c56562020-05-21 10:49:17 +00001225function getscreenshotpath()
1226{
Victoria Lease1b296b42012-08-21 15:44:06 -07001227 echo "$(getsdcardpath)/Pictures/Screenshots"
1228}
1229
Matt Alexanderd9c56562020-05-21 10:49:17 +00001230function getlastscreenshot()
1231{
Victoria Lease1b296b42012-08-21 15:44:06 -07001232 local screenshot_path=$(getscreenshotpath)
1233 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 +00001234 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001235 echo "No screenshots found."
1236 return
1237 fi
1238 echo "${screenshot}"
1239 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1240}
1241
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242function startviewserver()
1243{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001244 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245 if [ $# -gt 0 ]; then
1246 port=$1
1247 fi
1248 adb shell service call window 1 i32 $port
1249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function stopviewserver()
1252{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001253 adb shell service call window 2
1254}
1255
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256function isviewserverstarted()
1257{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001258 adb shell service call window 3
1259}
1260
Matt Alexanderd9c56562020-05-21 10:49:17 +00001261function key_home()
1262{
Romain Guyb84049a2010-10-04 16:56:11 -07001263 adb shell input keyevent 3
1264}
1265
Matt Alexanderd9c56562020-05-21 10:49:17 +00001266function key_back()
1267{
Romain Guyb84049a2010-10-04 16:56:11 -07001268 adb shell input keyevent 4
1269}
1270
Matt Alexanderd9c56562020-05-21 10:49:17 +00001271function key_menu()
1272{
Romain Guyb84049a2010-10-04 16:56:11 -07001273 adb shell input keyevent 82
1274}
1275
Matt Alexanderd9c56562020-05-21 10:49:17 +00001276function smoketest()
1277{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001278 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1279 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1280 return
1281 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001282 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001283 if [ ! "$T" ]; then
1284 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1285 return
1286 fi
1287
Ying Wang9cd17642012-12-13 10:52:07 -08001288 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001289 adb uninstall com.android.smoketest > /dev/null &&
1290 adb uninstall com.android.smoketest.tests > /dev/null &&
1291 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1292 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1293 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1294}
1295
The Android Open Source Project88b60792009-03-03 19:28:42 -08001296function godir () {
1297 if [[ -z "$1" ]]; then
1298 echo "Usage: godir <regex>"
1299 return
1300 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001301 local T=$(gettop)
1302 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001303 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001304 mkdir -p $OUT_DIR
1305 FILELIST=$OUT_DIR/filelist
1306 else
1307 FILELIST=$T/filelist
1308 fi
1309 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001310 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001311 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001312 echo " Done"
1313 echo ""
1314 fi
1315 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001316 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001317 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001318 echo "Not found"
1319 return
1320 fi
1321 local pathname
1322 local choice
1323 if [[ ${#lines[@]} > 1 ]]; then
1324 while [[ -z "$pathname" ]]; do
1325 local index=1
1326 local line
1327 for line in ${lines[@]}; do
1328 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001329 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001330 done
1331 echo
1332 echo -n "Select one: "
1333 unset choice
1334 read choice
1335 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1336 echo "Invalid choice"
1337 continue
1338 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001339 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001340 done
1341 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001342 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001343 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001344 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001345}
1346
Steven Moreland62054a42018-12-06 10:11:40 -08001347# Update module-info.json in out.
1348function refreshmod() {
1349 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1350 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1351 return 1
1352 fi
1353
1354 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1355
1356 # for the output of the next command
1357 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1358
1359 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001360 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001361 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1362}
1363
Cole Faust3e192382021-10-25 13:29:15 -07001364# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001365function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001366 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001367 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1368 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1369 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001370 return 1
1371 fi
1372
1373 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001374 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001375 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001376 fi
1377 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001378 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001379}
1380
Cole Faust5d825b72022-10-26 18:16:44 -07001381# List all modules for the current device, as cached in all_modules.txt. If any build change is
1382# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001383function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001384 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001385}
1386
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001387# Return the Bazel label of a Soong module if it is converted with bp2build.
1388function bmod()
1389(
Jingwen Chen688e5d62023-10-16 09:52:27 +00001390 if [ $# -eq 0 ]; then
1391 echo "usage: bmod <module 1> <module 2> ... <module n>" >&2
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001392 return 1
1393 fi
1394
1395 # We could run bp2build here, but it might trigger bp2build invalidation
1396 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1397 # time overhead.
1398 #
1399 # For a snappy result, use the latest generated version in soong_injection,
1400 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001401 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001402
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001403 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001404 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1405 return 1
1406 fi
1407
Jingwen Chen688e5d62023-10-16 09:52:27 +00001408 modules=()
1409 for m in "$@"; do
1410 modules+=("\"$m\",")
1411 done
1412 local res=$(python3 -c "import json
1413modules = [${modules[*]}]
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001414converted_json='$converted_json'
1415bp2build_converted_map = json.load(open(converted_json))
Jingwen Chen688e5d62023-10-16 09:52:27 +00001416for module in modules:
1417 if module not in bp2build_converted_map:
1418 print(module + ' is not converted to Bazel.')
1419 else:
1420 print(bp2build_converted_map[module] + ':' + module)")
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001421
Jingwen Chen688e5d62023-10-16 09:52:27 +00001422 echo "${res}"
1423 unconverted_count=$(echo "${res}" | grep -c "not converted to Bazel")
1424 if [[ ${unconverted_count} -ne 0 ]]; then
1425 return 1
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001426 fi
1427)
1428
Joe Onorato2c1aa472021-02-25 16:42:39 -08001429# Get the path of a specific module in the android tree, as cached in module-info.json.
1430# If any build change is made, and it should be reflected in the output, you should run
1431# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001432function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001433 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001434 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001435 return 1
1436 fi
1437
Cole Faust24c36db2021-01-23 02:39:37 +00001438 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001439
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001440 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001441module = '$1'
1442module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1443if module not in module_info:
1444 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001445print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001446
1447 if [ -z "$relpath" ]; then
1448 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1449 return 1
1450 else
Rett Berg78d1c932019-01-24 14:34:23 -08001451 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001452 fi
1453}
1454
Joe Onorato2c1aa472021-02-25 16:42:39 -08001455# Get the path of a specific module in the android tree, as cached in module-info.json.
1456# If any build change is made, and it should be reflected in the output, you should run
1457# 'refreshmod' first. Note: This is the inverse of pathmod.
1458function dirmods() {
1459 if [[ $# -ne 1 ]]; then
1460 echo "usage: dirmods <path>" >&2
1461 return 1
1462 fi
1463
1464 verifymodinfo || return 1
1465
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001466 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001467dir = '$1'
1468while dir.endswith('/'):
1469 dir = dir[:-1]
1470prefix = dir + '/'
1471module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1472results = set()
1473for m in module_info.values():
1474 for path in m.get(u'path', []):
1475 if path == dir or path.startswith(prefix):
1476 name = m.get(u'module_name')
1477 if name:
1478 results.add(name)
1479for name in sorted(results):
1480 print(name)
1481"
1482}
1483
1484
Rett Berg78d1c932019-01-24 14:34:23 -08001485# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1486# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1487function gomod() {
1488 if [[ $# -ne 1 ]]; then
1489 echo "usage: gomod <module>" >&2
1490 return 1
1491 fi
1492
1493 local path="$(pathmod $@)"
1494 if [ -z "$path" ]; then
1495 return 1
1496 fi
1497 cd $path
1498}
1499
Cole Faust24c36db2021-01-23 02:39:37 +00001500# Gets the list of a module's installed outputs, as cached in module-info.json.
1501# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1502function outmod() {
1503 if [[ $# -ne 1 ]]; then
1504 echo "usage: outmod <module>" >&2
1505 return 1
1506 fi
1507
1508 verifymodinfo || return 1
1509
1510 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001511 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001512module = '$1'
1513module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1514if module not in module_info:
1515 exit(1)
1516for output in module_info[module]['installed']:
1517 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1518
1519 if [ $? -ne 0 ]; then
1520 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1521 return 1
1522 elif [ ! -z "$relpath" ]; then
1523 echo "$relpath"
1524 fi
1525}
1526
1527# adb install a module's apk, as cached in module-info.json. If any build change
1528# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1529# Usage: installmod [adb install arguments] <module>
1530# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1531function installmod() {
1532 if [[ $# -eq 0 ]]; then
1533 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001534 echo "" >&2
1535 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1536 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1537 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001538 return 1
1539 fi
1540
1541 local _path
1542 _path=$(outmod ${@:$#:1})
1543 if [ $? -ne 0 ]; then
1544 return 1
1545 fi
1546
1547 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1548 if [ -z "$_path" ]; then
1549 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1550 return 1
1551 fi
Cole Faust3e192382021-10-25 13:29:15 -07001552 local serial_device=""
1553 if [[ "$1" == "-s" ]]; then
1554 if [[ $# -le 2 ]]; then
1555 echo "-s requires an argument" >&2
1556 return 1
1557 fi
1558 serial_device="-s $2"
1559 shift 2
1560 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001561 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001562 echo adb $serial_device install ${@:1:$length} $_path
1563 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001564}
1565
dimitry73b84812018-12-11 18:06:00 +01001566function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001567 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001568 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001569}
1570
Alex Rayf0d08eb2013-03-08 15:15:06 -08001571# Print colored exit condition
1572function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001573 "$@"
1574 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001575 if [ $retval -ne 0 ]
1576 then
Jacky Cao89483b82015-05-15 22:12:53 +08001577 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001578 else
Jacky Cao89483b82015-05-15 22:12:53 +08001579 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001580 fi
1581 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001582}
1583
Matt Alexanderd9c56562020-05-21 10:49:17 +00001584function get_make_command()
1585{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001586 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1587 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001588 # Always use the real make if -C is passed in
1589 for arg in "$@"; do
1590 if [[ $arg == -C* ]]; then
1591 echo command make
1592 return
1593 fi
1594 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001595 echo build/soong/soong_ui.bash --make-mode
1596 else
1597 echo command make
1598 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001599}
1600
Matt Alexanderd9c56562020-05-21 10:49:17 +00001601function _wrap_build()
1602{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001603 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1604 "$@"
1605 return $?
1606 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001607 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001608 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001609 local ret=$?
1610 local end_time=$(date +"%s")
1611 local tdiff=$(($end_time-$start_time))
1612 local hours=$(($tdiff / 3600 ))
1613 local mins=$((($tdiff % 3600) / 60))
1614 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001615 local ncolors=$(tput colors 2>/dev/null)
1616 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001617 color_failed=$'\E'"[0;31m"
1618 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001619 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001620 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001621 else
1622 color_failed=""
1623 color_success=""
1624 color_reset=""
1625 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001626
Ed Heylcc6be0a2014-06-18 14:55:58 -07001627 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001628 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001629 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001630 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001631 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001632 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001633 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001634 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001635 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001636 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001637 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001638 printf "(%s seconds)" $secs
1639 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001640 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001641 echo
1642 return $ret
1643}
1644
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001645function _trigger_build()
1646(
1647 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001648 local T=$(gettop)
1649 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001650 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1651 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001652 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1653 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001654 fi
Kevin Dagostino185109b2024-01-11 17:39:02 +00001655 local ret=$?
1656 if [[ ret -eq 0 && -z "${ANDROID_QUIET_BUILD:-}" && -n "${ANDROID_BUILD_BANNER}" ]]; then
1657 echo "${ANDROID_BUILD_BANNER}"
1658 fi
1659 return $ret
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001660)
1661
1662function m()
1663(
1664 _trigger_build "all-modules" "$@"
1665)
1666
1667function mm()
1668(
1669 _trigger_build "modules-in-a-dir-no-deps" "$@"
1670)
1671
1672function mmm()
1673(
1674 _trigger_build "modules-in-dirs-no-deps" "$@"
1675)
1676
1677function mma()
1678(
1679 _trigger_build "modules-in-a-dir" "$@"
1680)
1681
1682function mmma()
1683(
1684 _trigger_build "modules-in-dirs" "$@"
1685)
1686
Matt Alexanderd9c56562020-05-21 10:49:17 +00001687function make()
1688{
Dan Willemsene9842242017-07-28 13:00:13 -07001689 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001690}
1691
Matt Alexanderd9c56562020-05-21 10:49:17 +00001692function provision()
1693{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001694 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1695 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1696 return 1
1697 fi
1698 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1699 echo "There is no provisioning script for the device." >&2
1700 return 1
1701 fi
1702
1703 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001704 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001705 shift 1
1706 else
1707 echo "This action will reflash your device."
1708 echo ""
1709 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1710 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001711 echo -n "Are you sure you want to do this (yes/no)? "
1712 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001713 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001714 echo "Not taking any action. Exiting." >&2
1715 return 1
1716 fi
1717 fi
1718 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1719}
1720
Jim Tanga881a252018-06-19 16:34:41 +08001721# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001722function enable_zsh_completion() {
1723 # Don't override user's options if bash-style completion is already enabled.
1724 if ! declare -f complete >/dev/null; then
1725 autoload -U compinit && compinit
1726 autoload -U bashcompinit && bashcompinit
1727 fi
Jim Tanga881a252018-06-19 16:34:41 +08001728}
1729
1730function validate_current_shell() {
1731 local current_sh="$(ps -o command -p $$)"
1732 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001733 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001734 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001735 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001736 *zsh*)
1737 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001738 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001739 *)
Jim Tanga881a252018-06-19 16:34:41 +08001740 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 -07001741 ;;
1742 esac
Jim Tanga881a252018-06-19 16:34:41 +08001743}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001744
1745# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001746# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1747# load those.
1748#
1749# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001750function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001751 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001752 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001753 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001754 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 -08001755 if [ -n "$allowed" ]; then
1756 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1757 echo " $allowed"
1758 echo " $f"
1759 return
1760 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001761 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001762 done
1763
1764 allowed_files=
1765 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001766 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001767 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001768 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001769
1770 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001771 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001772 else
1773 echo "ignoring $f, not in $allowed"
1774 fi
Jim Tanga881a252018-06-19 16:34:41 +08001775 done
1776 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00001777
1778 if [[ "${PWD}" == /google/cog/* ]]; then
1779 f="build/make/cogsetup.sh"
1780 echo "including $f"; . "$T/$f"
1781 fi
Jim Tanga881a252018-06-19 16:34:41 +08001782}
Kenny Root52aa81c2011-07-15 11:07:06 -07001783
Dan Albertbab814f2020-08-26 15:34:53 -07001784function showcommands() {
1785 local T=$(gettop)
1786 if [[ -z "$TARGET_PRODUCT" ]]; then
1787 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1788 return
1789 fi
1790 case $(uname -s) in
1791 Darwin)
1792 PREBUILT_NAME=darwin-x86
1793 ;;
1794 Linux)
1795 PREBUILT_NAME=linux-x86
1796 ;;
1797 *)
1798 >&2 echo Unknown host $(uname -s)
1799 return
1800 ;;
1801 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001802 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07001803 if [[ "$1" == "--regenerate" ]]; then
1804 shift 1
1805 NINJA_ARGS="-t commands $@" m
1806 else
1807 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1808 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1809 -t commands "$@")
1810 fi
1811}
1812
Joe Onorato23124752024-05-14 15:06:48 -07001813# These functions used to be here but are now standalone scripts.
1814# Unset these for the time being so the real script is picked up.
1815# TODO: Remove this some time after a suitable delay (maybe 2025?)
1816unset aninja
1817unset overrideflags
Cole Faust45844ab2022-08-30 13:59:07 -07001818
Colin Cross71319722023-10-24 10:58:46 -07001819
Jim Tanga881a252018-06-19 16:34:41 +08001820validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001821set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08001822source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001823addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001824
Joe Onorato23124752024-05-14 15:06:48 -07001825
1826