blob: 1907e4c53a6babc845c15faad2011ae9edbccbdc [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
199# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000200function check_product()
201{
Christopher Ferris55257d22017-03-23 11:08:58 -0700202 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800203 if [ ! "$T" ]; then
204 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
205 return
206 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700207 TARGET_PRODUCT=$1 \
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600208 TARGET_RELEASE= \
Jeff Browne33ba4c2011-07-11 22:11:46 -0700209 TARGET_BUILD_VARIANT= \
210 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700211 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800212 get_build_var TARGET_DEVICE > /dev/null
213 # hide successful answers, but allow the errors to show
214}
215
216VARIANT_CHOICES=(user userdebug eng)
217
218# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000219function check_variant()
220{
Christopher Ferris55257d22017-03-23 11:08:58 -0700221 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800222 for v in ${VARIANT_CHOICES[@]}
223 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000224 if [ "$v" = "$1" ]
225 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800226 return 0
227 fi
228 done
229 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700230}
231
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800232
233# Add directories to PATH that are dependent on the lunch target.
234# For directories that are not lunch-specific, add them in set_global_paths
235function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000236{
Christopher Ferris55257d22017-03-23 11:08:58 -0700237 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700238 if [ ! "$T" ]; then
239 echo "Couldn't locate the top of the tree. Try setting TOP."
240 return
241 fi
242
243 ##################################################################
244 # #
245 # Read me before you modify this code #
246 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800247 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
248 # adding to PATH, and the next time it is run, it removes that #
249 # from PATH. This is required so lunch can be run more than #
250 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700251 # #
252 ##################################################################
253
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800254 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700255 # due to "C:\Program Files" being in the path.
256
Kevin Dagostino185109b2024-01-11 17:39:02 +0000257 # Handle compat with the old ANDROID_BUILD_PATHS variable.
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800258 # TODO: Remove this after we think everyone has lunched again.
259 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
260 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
261 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700262 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000263 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700264 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800265 # strip leading ':', if any
266 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800267 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500268 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700269
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800270 # Out with the old...
271 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
272 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
273 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800274
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800275 # And in with the new...
276 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
277 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700278
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800279 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700280 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800281 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200282
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800283 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
284 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
285
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800286 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800287 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800288 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
289 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
290 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800291
Colin Crosse97e6932017-06-30 16:01:45 -0700292 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
293 export JAVA_HOME=$ANDROID_JAVA_HOME
294 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800295 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
296
297 # Fix up PYTHONPATH
298 if [ -n $ANDROID_PYTHONPATH ]; then
299 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
300 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000301 # //development/python-packages contains both a pseudo-PYTHONPATH which
302 # mimics an already assembled venv, but also contains real Python packages
303 # that are not in that layout until they are installed. We can fake it for
304 # the latter type by adding the package source directories to the PYTHONPATH
305 # directly. For the former group, we only need to add the python-packages
306 # directory itself.
307 #
308 # This could be cleaned up by converting the remaining packages that are in
309 # the first category into a typical python source layout (that is, another
310 # layer of directory nesting) and automatically adding all subdirectories of
311 # python-packages to the PYTHONPATH instead of manually curating this. We
312 # can't convert the packages like adb to the other style because doing so
313 # would prevent exporting type info from those packages.
314 #
315 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000316 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800317 if [ -n $VENDOR_PYTHONPATH ]; then
318 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
319 fi
320 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700323 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
324 export OUT=$ANDROID_PRODUCT_OUT
325
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700326 unset ANDROID_HOST_OUT
327 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
328
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900329 unset ANDROID_SOONG_HOST_OUT
330 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
331
Simran Basidd050ed2017-02-13 13:46:48 -0800332 unset ANDROID_HOST_OUT_TESTCASES
333 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
334
335 unset ANDROID_TARGET_OUT_TESTCASES
336 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
337
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800338 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800339 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800340}
341
342# Add directories to PATH that are NOT dependent on the lunch target.
343# For directories that are lunch-specific, add them in set_lunch_paths
344function set_global_paths()
345{
346 local T=$(gettop)
347 if [ ! "$T" ]; then
348 echo "Couldn't locate the top of the tree. Try setting TOP."
349 return
350 fi
351
352 ##################################################################
353 # #
354 # Read me before you modify this code #
355 # #
356 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
357 # adding to PATH, and the next time it is run, it removes that #
358 # from PATH. This is required so envsetup.sh can be sourced #
359 # more than once and still have working paths. #
360 # #
361 ##################################################################
362
363 # Out with the old...
364 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
365 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
366 fi
367
368 # And in with the new...
Joe Onorato84e61d02024-02-02 22:53:39 -0800369 ANDROID_GLOBAL_BUILD_PATHS=$T/build/soong/bin
LaMont Jonese8a3be22024-02-12 09:55:41 -0800370 ANDROID_GLOBAL_BUILD_PATHS+=:$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800371 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
372 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800373
374 # add kernel specific binaries
375 if [ $(uname -s) = Linux ] ; then
376 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
377 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
378 fi
379
380 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
381 # to ensure that the corresponding 'emulator' binaries are used.
382 case $(uname -s) in
383 Darwin)
384 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
385 ;;
386 Linux)
387 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
388 ;;
389 *)
390 ANDROID_EMULATOR_PREBUILTS=
391 ;;
392 esac
393 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
394 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
395 export ANDROID_EMULATOR_PREBUILTS
396 fi
397
398 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800399 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400}
401
Matt Alexanderd9c56562020-05-21 10:49:17 +0000402function printconfig()
403{
Christopher Ferris55257d22017-03-23 11:08:58 -0700404 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405 if [ ! "$T" ]; then
406 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
407 return
408 fi
409 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700410}
411
Matt Alexanderd9c56562020-05-21 10:49:17 +0000412function set_stuff_for_environment()
413{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800414 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800415 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700416
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800417 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700418}
419
Matt Alexanderd9c56562020-05-21 10:49:17 +0000420function set_sequence_number()
421{
Colin Cross88737132017-03-21 17:41:03 -0700422 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700423}
424
Makoto Onukida971062018-06-18 10:15:19 -0700425# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
426function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800427 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700428 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800429 *:"$cmd":*)
430 return 1
431 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700432 esac
433 return 0
434}
435
Matt Alexanderd9c56562020-05-21 10:49:17 +0000436function addcompletions()
437{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700438 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700439
Jim Tanga881a252018-06-19 16:34:41 +0800440 # Keep us from trying to run in something that's neither bash nor zsh.
441 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700442 return
443 fi
444
445 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800446 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700447 return
448 fi
449
Jim Tanga881a252018-06-19 16:34:41 +0800450 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000451 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800452 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800453 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400454 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800455 )
Makoto Onukida971062018-06-18 10:15:19 -0700456 # Completion can be disabled selectively to allow users to use non-standard completion.
457 # e.g.
458 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
459 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400460 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800461 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400462 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000463 if [ ! -f "$f" ]; then
464 echo "Warning: completion file $f not found"
465 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700466 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700467 fi
468 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700469
Matt Alexanderd9c56562020-05-21 10:49:17 +0000470 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700471 complete -C "bit --tab" bit
472 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000473 if [ -z "$ZSH_VERSION" ]; then
474 # Doesn't work in zsh.
475 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400476 # TODO(b/244559459): Support b autocompletion for zsh
477 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000478 fi
Jim Tanga881a252018-06-19 16:34:41 +0800479 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800480
Cole Faust24c36db2021-01-23 02:39:37 +0000481 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100482 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000483 complete -F _complete_android_module_names outmod
484 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000485 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100486 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700487}
488
Joe Onorato824608c2022-04-08 11:06:16 -0700489function multitree_lunch_help()
490{
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600491 echo "usage: lunch PRODUCT-RELEASE-VARIANT" 1>&2
Joe Onorato824608c2022-04-08 11:06:16 -0700492 echo " Set up android build environment based on a product short name and variant" 1>&2
493 echo 1>&2
494 echo "lunch COMBO_FILE VARIANT" 1>&2
495 echo " Set up android build environment based on a specific lunch combo file" 1>&2
496 echo " and variant." 1>&2
497 echo 1>&2
498 echo "lunch --print [CONFIG]" 1>&2
499 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
500 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
501 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
502 echo 1>&2
503 echo "lunch --list" 1>&2
504 echo " List all possible combo files available in the current tree" 1>&2
505 echo 1>&2
506 echo "lunch --help" 1>&2
507 echo "lunch -h" 1>&2
508 echo " Prints this message." 1>&2
509}
510
511function multitree_lunch()
512{
513 local code
514 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000515 # Lunch must be run in the topdir, but this way we get a clear error
516 # message, instead of FileNotFound.
517 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000518 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000519 _multitree_lunch_error
520 return 1
521 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700522 if $(echo "$1" | grep -q '^-') ; then
523 # Calls starting with a -- argument are passed directly and the function
524 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000525 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700526 code=$?
527 if [[ $code -eq 2 ]] ; then
528 echo 1>&2
529 multitree_lunch_help
530 return $code
531 elif [[ $code -ne 0 ]] ; then
532 return $code
533 fi
534 else
535 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000536 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700537 code=$?
538 if [[ $code -eq 2 ]] ; then
539 echo 1>&2
540 multitree_lunch_help
541 return $code
542 elif [[ $code -ne 0 ]] ; then
543 return $code
544 fi
545
546 export TARGET_BUILD_COMBO=${results[0]}
547 export TARGET_BUILD_VARIANT=${results[1]}
548 fi
549}
550
Matt Alexanderd9c56562020-05-21 10:49:17 +0000551function choosetype()
552{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700553 echo "Build type choices are:"
554 echo " 1. release"
555 echo " 2. debug"
556 echo
557
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700559 DEFAULT_NUM=1
560 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700561
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562 export TARGET_BUILD_TYPE=
563 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700564 while [ -z $TARGET_BUILD_TYPE ]
565 do
566 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000567 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800568 read ANSWER
569 else
570 echo $1
571 ANSWER=$1
572 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700573 case $ANSWER in
574 "")
575 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
576 ;;
577 1)
578 export TARGET_BUILD_TYPE=release
579 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800580 release)
581 export TARGET_BUILD_TYPE=release
582 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583 2)
584 export TARGET_BUILD_TYPE=debug
585 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 debug)
587 export TARGET_BUILD_TYPE=debug
588 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589 *)
590 echo
591 echo "I didn't understand your response. Please try again."
592 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700593 ;;
594 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000595 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 break
597 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 done
599
Ying Wang08800fd2016-03-03 20:57:21 -0800600 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700601 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800602 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700603}
604
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800605#
606# This function isn't really right: It chooses a TARGET_PRODUCT
607# based on the list of boards. Usually, that gets you something
608# that kinda works with a generic product, but really, you should
609# pick a product by name.
610#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000611function chooseproduct()
612{
Christopher Ferris55257d22017-03-23 11:08:58 -0700613 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000614 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700615 default_value=$TARGET_PRODUCT
616 else
Ying Wang0a76df52015-06-08 11:57:26 -0700617 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700618 fi
619
Ying Wang08800fd2016-03-03 20:57:21 -0800620 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 export TARGET_PRODUCT=
622 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700623 while [ -z "$TARGET_PRODUCT" ]
624 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700625 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000626 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800627 read ANSWER
628 else
629 echo $1
630 ANSWER=$1
631 fi
632
Matt Alexanderd9c56562020-05-21 10:49:17 +0000633 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700634 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800635 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000636 if check_product $ANSWER
637 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800638 export TARGET_PRODUCT=$ANSWER
639 else
640 echo "** Not a valid product: $ANSWER"
641 fi
642 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000643 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800644 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700645 fi
646 done
647
Ying Wang08800fd2016-03-03 20:57:21 -0800648 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700649 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800650 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651}
652
Matt Alexanderd9c56562020-05-21 10:49:17 +0000653function choosevariant()
654{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800655 echo "Variant choices are:"
656 local index=1
657 local v
658 for v in ${VARIANT_CHOICES[@]}
659 do
660 # The product name is the name of the directory containing
661 # the makefile we found, above.
662 echo " $index. $v"
663 index=$(($index+1))
664 done
665
666 local default_value=eng
667 local ANSWER
668
669 export TARGET_BUILD_VARIANT=
670 while [ -z "$TARGET_BUILD_VARIANT" ]
671 do
672 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000673 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800674 read ANSWER
675 else
676 echo $1
677 ANSWER=$1
678 fi
679
Matt Alexanderd9c56562020-05-21 10:49:17 +0000680 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800681 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000682 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
683 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200684 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800685 fi
686 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000687 if check_variant $ANSWER
688 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800689 export TARGET_BUILD_VARIANT=$ANSWER
690 else
691 echo "** Not a valid variant: $ANSWER"
692 fi
693 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000694 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800695 break
696 fi
697 done
698}
699
Matt Alexanderd9c56562020-05-21 10:49:17 +0000700function choosecombo()
701{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700702 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700703
704 echo
705 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700706 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700707
708 echo
709 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700710 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800711
712 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800713 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700714 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800715 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800716 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700717}
718
Matt Alexanderd9c56562020-05-21 10:49:17 +0000719function add_lunch_combo()
720{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800721 if [ -n "$ZSH_VERSION" ]; then
722 echo -n "${funcfiletrace[1]}: "
723 else
724 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
725 fi
726 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 -0800727}
728
Matt Alexanderd9c56562020-05-21 10:49:17 +0000729function print_lunch_menu()
730{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700731 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100732 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600733 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 +0100734 local ret=$?
735
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700736 echo
737 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700738 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100739
Matt Alexanderd9c56562020-05-21 10:49:17 +0000740 if [ $ret -ne 0 ]
741 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100742 echo "Warning: Cannot display lunch menu."
743 echo
744 echo "Note: You can invoke lunch with an explicit target:"
745 echo
746 echo " usage: lunch [target]" >&2
747 echo
748 return
749 fi
750
Will Burr40401202022-02-07 12:12:01 +0000751 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800752
753 local i=1
754 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700755 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800756 do
757 echo " $i. $choice"
758 i=$(($i+1))
759 done
760
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761 echo
762}
763
Matt Alexanderd9c56562020-05-21 10:49:17 +0000764function lunch()
765{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800766 local answer
767
Steven Moreland92793dc2020-02-25 18:30:18 -0800768 if [[ $# -gt 1 ]]; then
769 echo "usage: lunch [target]" >&2
770 return 1
771 fi
772
Will Burr40401202022-02-07 12:12:01 +0000773 local used_lunch_menu=0
774
Steven Moreland92793dc2020-02-25 18:30:18 -0800775 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800776 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700777 else
778 print_lunch_menu
Greg Kaiserbae4c572024-01-04 15:57:54 -0700779 echo "Which would you like? [aosp_cf_x86_64_phone-trunk_staging-eng]"
Greg Kaiser9a5a5262023-11-02 16:54:27 +0000780 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 -0800781 read answer
Will Burr40401202022-02-07 12:12:01 +0000782 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 fi
784
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800785 local selection=
786
Matt Alexanderd9c56562020-05-21 10:49:17 +0000787 if [ -z "$answer" ]
788 then
Greg Kaiserbae4c572024-01-04 15:57:54 -0700789 selection=aosp_cf_x86_64_phone-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000790 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
791 then
Fabián Cañas03dd0282024-01-05 16:57:39 -0500792 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 +0000793 if [ $answer -le ${#choices[@]} ]
794 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800795 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000796 if [ -n "$ZSH_VERSION" ]
797 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800798 selection=${choices[$(($answer))]}
799 else
800 selection=${choices[$(($answer-1))]}
801 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800802 fi
Colin Cross88737132017-03-21 17:41:03 -0700803 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800804 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700805 fi
806
Joe Onoratoda12daf2010-06-09 18:18:31 -0700807 export TARGET_BUILD_APPS=
808
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600809 # This must be <product>-<release>-<variant>
810 local product release variant
811 # Split string on the '-' character.
812 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800813
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600814 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000815 then
Ying Wang08800fd2016-03-03 20:57:21 -0800816 echo
Colin Cross88737132017-03-21 17:41:03 -0700817 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600818 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700819 return 1
820 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800821
Colin Cross88737132017-03-21 17:41:03 -0700822 TARGET_PRODUCT=$product \
823 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000824 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700825 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000826 if [ $? -ne 0 ]
827 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100828 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
829 then
830 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
831 fi
Colin Cross88737132017-03-21 17:41:03 -0700832 return 1
833 fi
Colin Cross88737132017-03-21 17:41:03 -0700834 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
835 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600836 export TARGET_RELEASE=$release
837 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700838 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700839
Mark White2c807512023-10-25 17:27:07 +0000840 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
841
842 set_stuff_for_environment
843 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
844
845 if [ "${TARGET_BUILD_VARIANT}" = "userdebug" ] && [[ -z "${ANDROID_QUIET_BUILD}" ]]; then
846 echo
847 echo "Want FASTER LOCAL BUILDS? Use -eng instead of -userdebug (however for" \
848 "performance benchmarking continue to use userdebug)"
849 fi
Will Burr40401202022-02-07 12:12:01 +0000850 if [ $used_lunch_menu -eq 1 ]; then
851 echo
852 echo "Hint: next time you can simply run 'lunch $selection'"
853 fi
854
Ying Wang08800fd2016-03-03 20:57:21 -0800855 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400856
857 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
858 check_mu_config
859 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700860}
861
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700862unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700863# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000864function _lunch()
865{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700866 local cur prev opts
867 COMPREPLY=()
868 cur="${COMP_WORDS[COMP_CWORD]}"
869 prev="${COMP_WORDS[COMP_CWORD-1]}"
870
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700871 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800872 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700873 fi
874
875 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700876 return 0
877}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700878
Joe Onoratoda12daf2010-06-09 18:18:31 -0700879# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700880# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000881function tapas()
882{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700883 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800884 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000885 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
886 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700887 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700888 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 -0800889 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
890 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)"
891
Joe Onoratoda12daf2010-06-09 18:18:31 -0700892
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700893 if [ "$showHelp" != "" ]; then
894 $(gettop)/build/make/tapasHelp.sh
895 return
896 fi
897
Ying Wang67f02922012-08-22 10:25:20 -0700898 if [ $(echo $arch | wc -w) -gt 1 ]; then
899 echo "tapas: Error: Multiple build archs supplied: $arch"
900 return
901 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000902 if [ $(echo $release | wc -w) -gt 1 ]; then
903 echo "tapas: Error: Multiple build releases supplied: $release"
904 return
905 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700906 if [ $(echo $variant | wc -w) -gt 1 ]; then
907 echo "tapas: Error: Multiple build variants supplied: $variant"
908 return
909 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700910 if [ $(echo $density | wc -w) -gt 1 ]; then
911 echo "tapas: Error: Multiple densities supplied: $density"
912 return
913 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800914 if [ $(echo $keys | wc -w) -gt 1 ]; then
915 echo "tapas: Error: Multiple keys supplied: $keys"
916 return
917 fi
Ying Wang67f02922012-08-22 10:25:20 -0700918
Ying Wang0a76df52015-06-08 11:57:26 -0700919 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700920 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000921 x86) product=aosp_x86;;
922 arm64) product=aosp_arm64;;
923 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700924 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800925 if [ -n "$keys" ]; then
926 product=${product/aosp_/aosp_${keys}_}
927 fi;
928
Joe Onoratoda12daf2010-06-09 18:18:31 -0700929 if [ -z "$variant" ]; then
930 variant=eng
931 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700932 if [ -z "$apps" ]; then
933 apps=all
934 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600935 if [ -z "$density" ]; then
936 density=alldpi
937 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700938
Ying Wang67f02922012-08-22 10:25:20 -0700939 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000940 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700941 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700942 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700943 export TARGET_BUILD_TYPE=release
944 export TARGET_BUILD_APPS=$apps
945
Ying Wang08800fd2016-03-03 20:57:21 -0800946 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700947 set_stuff_for_environment
948 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800949 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700950}
951
Martin Stjernholmf692c752021-04-12 00:01:10 +0100952# Configures the build to build unbundled Android modules (APEXes).
953# Run banchan with one or more module names (from apex{} modules).
954function banchan()
955{
956 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100957 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 -0600958 # TODO: Expand banchan to take release arguments (and update hmm() usage).
959 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100960 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100961 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 +0100962
963 if [ "$showHelp" != "" ]; then
964 $(gettop)/build/make/banchanHelp.sh
965 return
966 fi
967
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100968 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000969 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100970 elif [ $(echo $product | wc -w) -gt 1 ]; then
971 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100972 return
973 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600974 if [ $(echo $release | wc -w) -gt 1 ]; then
975 echo "banchan: Error: Multiple build releases supplied: $release"
976 return
977 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100978 if [ $(echo $variant | wc -w) -gt 1 ]; then
979 echo "banchan: Error: Multiple build variants supplied: $variant"
980 return
981 fi
982 if [ -z "$apps" ]; then
983 echo "banchan: Error: No modules supplied"
984 return
985 fi
986
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100987 case $product in
988 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100989 x86) product=module_x86;;
990 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100991 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100992 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000993 arm64only) product=module_arm64only;;
994 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100995 esac
996 if [ -z "$variant" ]; then
997 variant=eng
998 fi
999
1000 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -06001001 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +01001002 export TARGET_BUILD_VARIANT=$variant
1003 export TARGET_BUILD_DENSITY=alldpi
1004 export TARGET_BUILD_TYPE=release
1005
1006 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
1007 # case is different and it may diverge in the future.
1008 export TARGET_BUILD_APPS=$apps
1009
1010 build_build_var_cache
1011 set_stuff_for_environment
1012 printconfig
1013 destroy_build_var_cache
1014}
1015
Joe Onorato7cf6f972022-05-11 21:39:57 -07001016# TODO: Merge into gettop as part of launching multitree
1017function multitree_gettop
1018{
Spandan Dasca762052022-09-21 00:08:34 +00001019 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -07001020 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
1021 # The following circumlocution ensures we remove symlinks from TOP.
1022 (cd "$TOP"; PWD= /bin/pwd)
1023 else
1024 if [ -f $TOPFILE ] ; then
1025 # The following circumlocution (repeated below as well) ensures
1026 # that we record the true directory name and not one that is
1027 # faked up with symlink names.
1028 PWD= /bin/pwd
1029 else
1030 local HERE=$PWD
1031 local T=
1032 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1033 \cd ..
1034 T=`PWD= /bin/pwd -P`
1035 done
1036 \cd "$HERE"
1037 if [ -f "$T/$TOPFILE" ]; then
1038 echo "$T"
1039 fi
1040 fi
1041 fi
1042}
1043
Matt Alexanderd9c56562020-05-21 10:49:17 +00001044function croot()
1045{
Christopher Ferris55257d22017-03-23 11:08:58 -07001046 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001047 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001048 if [ "$1" ]; then
1049 \cd $(gettop)/$1
1050 else
1051 \cd $(gettop)
1052 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001053 else
1054 echo "Couldn't locate the top of the tree. Try setting TOP."
1055 fi
1056}
1057
Matt Alexanderd9c56562020-05-21 10:49:17 +00001058function _croot()
1059{
Anton Hanssonece9c482019-02-04 18:15:39 +00001060 local T=$(gettop)
1061 if [ "$T" ]; then
1062 local cur="${COMP_WORDS[COMP_CWORD]}"
1063 k=0
1064 for c in $(compgen -d ${T}/${cur}); do
1065 COMPREPLY[k++]=${c#${T}/}/
1066 done
1067 fi
1068}
1069
Matt Alexanderd9c56562020-05-21 10:49:17 +00001070function cproj()
1071{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001072 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001073 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001074 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001075 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1076 T=$PWD
1077 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001078 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001079 return
1080 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001081 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001082 done
Ying Wang9cd17642012-12-13 10:52:07 -08001083 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001084 echo "can't find Android.mk"
1085}
1086
Shaju Mathew21439002023-07-10 00:53:22 +00001087function adb() {
Elliott Hughes7e7ff752024-03-20 17:23:40 -07001088 local ADB=$(which adb)
1089 if [ -z "$ADB" ]; then
1090 echo "Command adb not found; try lunch (and building) first?"
1091 return 1
1092 fi
1093 $ADB "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +00001094}
1095
Daniel Sandler47e0a882013-07-30 13:23:52 -04001096# simplified version of ps; output in the form
1097# <pid> <procname>
1098function qpid() {
1099 local prepend=''
1100 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001102 prepend=' '
1103 append='$'
1104 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001105 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001106 echo "usage: qpid [[--exact] <process name|pid>"
1107 return 255
1108 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001109
1110 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001111 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001112 qpid | \grep "$prepend$EXE$append"
1113 else
1114 adb shell ps \
1115 | tr -d '\r' \
1116 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1117 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001118}
1119
Steven Moreland74114f12020-09-10 01:23:32 +00001120# syswrite - disable verity, reboot if needed, and remount image
1121#
1122# Easy way to make system.img/etc writable
1123function syswrite() {
Steven Moreland7275a192023-04-17 17:21:01 +00001124 adb wait-for-device && adb root && adb wait-for-device || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001125 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001126 echo "rebooting"
Steven Moreland7275a192023-04-17 17:21:01 +00001127 adb reboot && adb wait-for-device && adb root && adb wait-for-device || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001128 fi
Steven Moreland7275a192023-04-17 17:21:01 +00001129 adb remount || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001130}
1131
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001132# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001133# that has the core-file-size limit set correctly
1134#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001135# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001136# if its core-file-size limit is not set already.
1137# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1138
Matt Alexanderd9c56562020-05-21 10:49:17 +00001139function coredump_setup()
1140{
1141 echo "Getting root...";
1142 adb root;
1143 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001144
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145 echo "Remounting root partition read-write...";
1146 adb shell mount -w -o remount -t rootfs rootfs;
1147 sleep 1;
1148 adb wait-for-device;
1149 adb shell mkdir -p /cores;
1150 adb shell mount -t tmpfs tmpfs /cores;
1151 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001152
Matt Alexanderd9c56562020-05-21 10:49:17 +00001153 echo "Granting SELinux permission to dump in /cores...";
1154 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001155
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156 echo "Set core pattern.";
1157 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001158
Ying Wang08800fd2016-03-03 20:57:21 -08001159 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001160}
1161
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001162# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001163# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001164#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001165# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001166# dump to actually be generated.
1167
Matt Alexanderd9c56562020-05-21 10:49:17 +00001168function coredump_enable()
1169{
1170 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001171 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001172 printf "Expecting a PID!\n";
1173 return;
1174 fi;
1175 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001176 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001177}
1178
1179# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001180# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001181#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001182# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001183# enabled globally.
1184
Matt Alexanderd9c56562020-05-21 10:49:17 +00001185function core()
1186{
1187 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001188
Ying Wang08800fd2016-03-03 20:57:21 -08001189 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001190 printf "Expecting a PID!\n";
1191 return;
1192 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001193
Matt Alexanderd9c56562020-05-21 10:49:17 +00001194 local CORENAME=core.$PID;
1195 local COREPATH=/cores/$CORENAME;
1196 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001197
Matt Alexanderd9c56562020-05-21 10:49:17 +00001198 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001199
Matt Alexanderd9c56562020-05-21 10:49:17 +00001200 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001201 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1203 adb shell kill -$SIG $PID;
1204 sleep 1;
1205 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001206
Matt Alexanderd9c56562020-05-21 10:49:17 +00001207 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1208 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001209}
1210
Christopher Tate744ee802009-11-12 15:33:08 -08001211# systemstack - dump the current stack trace of all threads in the system process
1212# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001213function systemstack()
1214{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001215 stacks system_server
1216}
1217
Michael Wrightaeed7212014-06-19 19:58:12 -07001218# Read the ELF header from /proc/$PID/exe to determine if the process is
1219# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001220function is64bit()
1221{
Ben Chengfba67bf2014-02-25 10:27:07 -08001222 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001223 if [ "$PID" ] ; then
1224 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001225 echo "64"
1226 else
1227 echo ""
1228 fi
1229 else
1230 echo ""
1231 fi
1232}
1233
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001234case `uname -s` in
1235 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236 function sgrep()
1237 {
Joe Onorato95670782023-02-27 15:53:09 -08001238 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 -04001239 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001240 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001241
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001242 ;;
1243 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001244 function sgrep()
1245 {
Joe Onorato95670782023-02-27 15:53:09 -08001246 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 -04001247 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001248 }
1249 ;;
1250esac
1251
Matt Alexanderd9c56562020-05-21 10:49:17 +00001252function gettargetarch
1253{
Raghu Gandham8da43102012-07-25 19:57:22 -07001254 get_build_var TARGET_ARCH
1255}
1256
Matt Alexanderd9c56562020-05-21 10:49:17 +00001257function ggrep()
1258{
Mike Frysinger5e479732015-09-22 18:13:48 -04001259 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1260 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001261}
1262
Matt Alexanderd9c56562020-05-21 10:49:17 +00001263function gogrep()
1264{
Orion Hodson831472d2019-10-25 11:35:15 +01001265 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1266 -exec grep --color -n "$@" {} +
1267}
1268
Matt Alexanderd9c56562020-05-21 10:49:17 +00001269function jgrep()
1270{
Mike Frysinger5e479732015-09-22 18:13:48 -04001271 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1272 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001273}
1274
Alistair Delva176e5342021-02-22 13:31:26 -08001275function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001276{
1277 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1278 -exec grep --color -n "$@" {} +
1279}
1280
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001281function jsongrep()
1282{
1283 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1284 -exec grep --color -n "$@" {} +
1285}
1286
1287function tomlgrep()
1288{
1289 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1290 -exec grep --color -n "$@" {} +
1291}
1292
Taesu Leeea0cecd2020-10-28 11:05:18 +09001293function ktgrep()
1294{
1295 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1296 -exec grep --color -n "$@" {} +
1297}
1298
Matt Alexanderd9c56562020-05-21 10:49:17 +00001299function cgrep()
1300{
Mike Frysinger5e479732015-09-22 18:13:48 -04001301 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' \) \
1302 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001303}
1304
Matt Alexanderd9c56562020-05-21 10:49:17 +00001305function resgrep()
1306{
Christopher Ferris55257d22017-03-23 11:08:58 -07001307 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001308 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1309 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1310 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001311}
1312
Matt Alexanderd9c56562020-05-21 10:49:17 +00001313function mangrep()
1314{
Mike Frysinger5e479732015-09-22 18:13:48 -04001315 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1316 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001317}
1318
Matt Alexanderd9c56562020-05-21 10:49:17 +00001319function owngrep()
1320{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001321 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1322 -exec grep --color -n "$@" {} +
1323}
1324
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325function sepgrep()
1326{
Mike Frysinger5e479732015-09-22 18:13:48 -04001327 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1328 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001329}
1330
Matt Alexanderd9c56562020-05-21 10:49:17 +00001331function rcgrep()
1332{
Mike Frysinger5e479732015-09-22 18:13:48 -04001333 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1334 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001335}
1336
DroidFreak32a2781982020-11-26 11:30:19 -05001337function pygrep()
1338{
1339 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1340 -exec grep --color -n "$@" {} +
1341}
1342
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001343case `uname -s` in
1344 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001345 function mgrep()
1346 {
Orion Hodson831472d2019-10-25 11:35:15 +01001347 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 -04001348 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001349 }
1350
Matt Alexanderd9c56562020-05-21 10:49:17 +00001351 function treegrep()
1352 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001353 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 -04001354 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001355 }
1356
1357 ;;
1358 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001359 function mgrep()
1360 {
Orion Hodson831472d2019-10-25 11:35:15 +01001361 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 -04001362 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001363 }
1364
Matt Alexanderd9c56562020-05-21 10:49:17 +00001365 function treegrep()
1366 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001367 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 -04001368 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001369 }
1370
1371 ;;
1372esac
1373
Matt Alexanderd9c56562020-05-21 10:49:17 +00001374function getprebuilt
1375{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001376 get_abs_build_var ANDROID_PREBUILTS
1377}
1378
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001379# communicate with a running device or emulator, set up necessary state,
1380# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001381function runhat()
1382{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001383 # process standard adb options
1384 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001385 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001386 adbTarget=$1
1387 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001388 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001389 adbTarget="$1 $2"
1390 shift 2
1391 fi
1392 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001393 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001394
1395 # runhat options
1396 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001397
Matt Alexanderd9c56562020-05-21 10:49:17 +00001398 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001399 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001400 return
1401 fi
1402
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001403 # confirm hat is available
1404 if [ -z $(which hat) ]; then
1405 echo "hat is not available in this configuration."
1406 return
1407 fi
1408
Andy McFaddenb6289852010-07-12 08:00:19 -07001409 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001410 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001411 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001412 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001413 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001414 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001415 echo -n "> "
1416 read
1417
The Android Open Source Project88b60792009-03-03 19:28:42 -08001418 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419
The Android Open Source Project88b60792009-03-03 19:28:42 -08001420 echo "Retrieving file $devFile..."
1421 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001422
The Android Open Source Project88b60792009-03-03 19:28:42 -08001423 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001424
The Android Open Source Project88b60792009-03-03 19:28:42 -08001425 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001426 echo "View the output by pointing your browser at http://localhost:7000/"
1427 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001428 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429}
1430
Matt Alexanderd9c56562020-05-21 10:49:17 +00001431function getbugreports()
1432{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001433 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001434
1435 if [ ! "$reports" ]; then
1436 echo "Could not locate any bugreports."
1437 return
1438 fi
1439
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001440 local report
1441 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001442 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001443 echo "/sdcard/bugreports/${report}"
1444 adb pull /sdcard/bugreports/${report} ${report}
1445 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001446 done
1447}
1448
Matt Alexanderd9c56562020-05-21 10:49:17 +00001449function getsdcardpath()
1450{
Victoria Lease1b296b42012-08-21 15:44:06 -07001451 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1452}
1453
Matt Alexanderd9c56562020-05-21 10:49:17 +00001454function getscreenshotpath()
1455{
Victoria Lease1b296b42012-08-21 15:44:06 -07001456 echo "$(getsdcardpath)/Pictures/Screenshots"
1457}
1458
Matt Alexanderd9c56562020-05-21 10:49:17 +00001459function getlastscreenshot()
1460{
Victoria Lease1b296b42012-08-21 15:44:06 -07001461 local screenshot_path=$(getscreenshotpath)
1462 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 +00001463 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001464 echo "No screenshots found."
1465 return
1466 fi
1467 echo "${screenshot}"
1468 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1469}
1470
Matt Alexanderd9c56562020-05-21 10:49:17 +00001471function startviewserver()
1472{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001473 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001474 if [ $# -gt 0 ]; then
1475 port=$1
1476 fi
1477 adb shell service call window 1 i32 $port
1478}
1479
Matt Alexanderd9c56562020-05-21 10:49:17 +00001480function stopviewserver()
1481{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001482 adb shell service call window 2
1483}
1484
Matt Alexanderd9c56562020-05-21 10:49:17 +00001485function isviewserverstarted()
1486{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001487 adb shell service call window 3
1488}
1489
Matt Alexanderd9c56562020-05-21 10:49:17 +00001490function key_home()
1491{
Romain Guyb84049a2010-10-04 16:56:11 -07001492 adb shell input keyevent 3
1493}
1494
Matt Alexanderd9c56562020-05-21 10:49:17 +00001495function key_back()
1496{
Romain Guyb84049a2010-10-04 16:56:11 -07001497 adb shell input keyevent 4
1498}
1499
Matt Alexanderd9c56562020-05-21 10:49:17 +00001500function key_menu()
1501{
Romain Guyb84049a2010-10-04 16:56:11 -07001502 adb shell input keyevent 82
1503}
1504
Matt Alexanderd9c56562020-05-21 10:49:17 +00001505function smoketest()
1506{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001507 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1508 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1509 return
1510 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001511 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001512 if [ ! "$T" ]; then
1513 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1514 return
1515 fi
1516
Ying Wang9cd17642012-12-13 10:52:07 -08001517 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001518 adb uninstall com.android.smoketest > /dev/null &&
1519 adb uninstall com.android.smoketest.tests > /dev/null &&
1520 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1521 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1522 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1523}
1524
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001525# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001526function runtest()
1527{
Christopher Ferris55257d22017-03-23 11:08:58 -07001528 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001529 if [ ! "$T" ]; then
1530 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1531 return
1532 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001533 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001534}
1535
The Android Open Source Project88b60792009-03-03 19:28:42 -08001536function godir () {
1537 if [[ -z "$1" ]]; then
1538 echo "Usage: godir <regex>"
1539 return
1540 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001541 local T=$(gettop)
1542 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001543 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001544 mkdir -p $OUT_DIR
1545 FILELIST=$OUT_DIR/filelist
1546 else
1547 FILELIST=$T/filelist
1548 fi
1549 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001550 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001551 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001552 echo " Done"
1553 echo ""
1554 fi
1555 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001556 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001557 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001558 echo "Not found"
1559 return
1560 fi
1561 local pathname
1562 local choice
1563 if [[ ${#lines[@]} > 1 ]]; then
1564 while [[ -z "$pathname" ]]; do
1565 local index=1
1566 local line
1567 for line in ${lines[@]}; do
1568 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001569 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001570 done
1571 echo
1572 echo -n "Select one: "
1573 unset choice
1574 read choice
1575 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1576 echo "Invalid choice"
1577 continue
1578 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001579 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001580 done
1581 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001582 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001583 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001584 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001585}
1586
Steven Moreland62054a42018-12-06 10:11:40 -08001587# Update module-info.json in out.
1588function refreshmod() {
1589 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1590 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1591 return 1
1592 fi
1593
1594 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1595
1596 # for the output of the next command
1597 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1598
1599 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001600 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001601 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1602}
1603
Cole Faust3e192382021-10-25 13:29:15 -07001604# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001605function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001606 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001607 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1608 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1609 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001610 return 1
1611 fi
1612
1613 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001614 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001615 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001616 fi
1617 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001618 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001619}
1620
Cole Faust5d825b72022-10-26 18:16:44 -07001621# List all modules for the current device, as cached in all_modules.txt. If any build change is
1622# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001623function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001624 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001625}
1626
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001627# Return the Bazel label of a Soong module if it is converted with bp2build.
1628function bmod()
1629(
Jingwen Chen688e5d62023-10-16 09:52:27 +00001630 if [ $# -eq 0 ]; then
1631 echo "usage: bmod <module 1> <module 2> ... <module n>" >&2
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001632 return 1
1633 fi
1634
1635 # We could run bp2build here, but it might trigger bp2build invalidation
1636 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1637 # time overhead.
1638 #
1639 # For a snappy result, use the latest generated version in soong_injection,
1640 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001641 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001642
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001643 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001644 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1645 return 1
1646 fi
1647
Jingwen Chen688e5d62023-10-16 09:52:27 +00001648 modules=()
1649 for m in "$@"; do
1650 modules+=("\"$m\",")
1651 done
1652 local res=$(python3 -c "import json
1653modules = [${modules[*]}]
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001654converted_json='$converted_json'
1655bp2build_converted_map = json.load(open(converted_json))
Jingwen Chen688e5d62023-10-16 09:52:27 +00001656for module in modules:
1657 if module not in bp2build_converted_map:
1658 print(module + ' is not converted to Bazel.')
1659 else:
1660 print(bp2build_converted_map[module] + ':' + module)")
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001661
Jingwen Chen688e5d62023-10-16 09:52:27 +00001662 echo "${res}"
1663 unconverted_count=$(echo "${res}" | grep -c "not converted to Bazel")
1664 if [[ ${unconverted_count} -ne 0 ]]; then
1665 return 1
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001666 fi
1667)
1668
Joe Onorato2c1aa472021-02-25 16:42:39 -08001669# Get the path of a specific module in the android tree, as cached in module-info.json.
1670# If any build change is made, and it should be reflected in the output, you should run
1671# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001672function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001673 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001674 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001675 return 1
1676 fi
1677
Cole Faust24c36db2021-01-23 02:39:37 +00001678 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001679
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001680 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001681module = '$1'
1682module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1683if module not in module_info:
1684 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001685print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001686
1687 if [ -z "$relpath" ]; then
1688 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1689 return 1
1690 else
Rett Berg78d1c932019-01-24 14:34:23 -08001691 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001692 fi
1693}
1694
Joe Onorato2c1aa472021-02-25 16:42:39 -08001695# Get the path of a specific module in the android tree, as cached in module-info.json.
1696# If any build change is made, and it should be reflected in the output, you should run
1697# 'refreshmod' first. Note: This is the inverse of pathmod.
1698function dirmods() {
1699 if [[ $# -ne 1 ]]; then
1700 echo "usage: dirmods <path>" >&2
1701 return 1
1702 fi
1703
1704 verifymodinfo || return 1
1705
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001706 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001707dir = '$1'
1708while dir.endswith('/'):
1709 dir = dir[:-1]
1710prefix = dir + '/'
1711module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1712results = set()
1713for m in module_info.values():
1714 for path in m.get(u'path', []):
1715 if path == dir or path.startswith(prefix):
1716 name = m.get(u'module_name')
1717 if name:
1718 results.add(name)
1719for name in sorted(results):
1720 print(name)
1721"
1722}
1723
1724
Rett Berg78d1c932019-01-24 14:34:23 -08001725# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1726# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1727function gomod() {
1728 if [[ $# -ne 1 ]]; then
1729 echo "usage: gomod <module>" >&2
1730 return 1
1731 fi
1732
1733 local path="$(pathmod $@)"
1734 if [ -z "$path" ]; then
1735 return 1
1736 fi
1737 cd $path
1738}
1739
Cole Faust24c36db2021-01-23 02:39:37 +00001740# Gets the list of a module's installed outputs, as cached in module-info.json.
1741# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1742function outmod() {
1743 if [[ $# -ne 1 ]]; then
1744 echo "usage: outmod <module>" >&2
1745 return 1
1746 fi
1747
1748 verifymodinfo || return 1
1749
1750 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001751 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001752module = '$1'
1753module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1754if module not in module_info:
1755 exit(1)
1756for output in module_info[module]['installed']:
1757 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1758
1759 if [ $? -ne 0 ]; then
1760 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1761 return 1
1762 elif [ ! -z "$relpath" ]; then
1763 echo "$relpath"
1764 fi
1765}
1766
1767# adb install a module's apk, as cached in module-info.json. If any build change
1768# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1769# Usage: installmod [adb install arguments] <module>
1770# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1771function installmod() {
1772 if [[ $# -eq 0 ]]; then
1773 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001774 echo "" >&2
1775 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1776 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1777 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001778 return 1
1779 fi
1780
1781 local _path
1782 _path=$(outmod ${@:$#:1})
1783 if [ $? -ne 0 ]; then
1784 return 1
1785 fi
1786
1787 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1788 if [ -z "$_path" ]; then
1789 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1790 return 1
1791 fi
Cole Faust3e192382021-10-25 13:29:15 -07001792 local serial_device=""
1793 if [[ "$1" == "-s" ]]; then
1794 if [[ $# -le 2 ]]; then
1795 echo "-s requires an argument" >&2
1796 return 1
1797 fi
1798 serial_device="-s $2"
1799 shift 2
1800 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001801 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001802 echo adb $serial_device install ${@:1:$length} $_path
1803 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001804}
1805
dimitry73b84812018-12-11 18:06:00 +01001806function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001807 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001808 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001809}
1810
Alex Rayf0d08eb2013-03-08 15:15:06 -08001811# Print colored exit condition
1812function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001813 "$@"
1814 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001815 if [ $retval -ne 0 ]
1816 then
Jacky Cao89483b82015-05-15 22:12:53 +08001817 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001818 else
Jacky Cao89483b82015-05-15 22:12:53 +08001819 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001820 fi
1821 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001822}
1823
Matt Alexanderd9c56562020-05-21 10:49:17 +00001824function get_make_command()
1825{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001826 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1827 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001828 # Always use the real make if -C is passed in
1829 for arg in "$@"; do
1830 if [[ $arg == -C* ]]; then
1831 echo command make
1832 return
1833 fi
1834 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001835 echo build/soong/soong_ui.bash --make-mode
1836 else
1837 echo command make
1838 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001839}
1840
Matt Alexanderd9c56562020-05-21 10:49:17 +00001841function _wrap_build()
1842{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001843 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1844 "$@"
1845 return $?
1846 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001847 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001848 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001849 local ret=$?
1850 local end_time=$(date +"%s")
1851 local tdiff=$(($end_time-$start_time))
1852 local hours=$(($tdiff / 3600 ))
1853 local mins=$((($tdiff % 3600) / 60))
1854 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001855 local ncolors=$(tput colors 2>/dev/null)
1856 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001857 color_failed=$'\E'"[0;31m"
1858 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001859 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001860 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001861 else
1862 color_failed=""
1863 color_success=""
1864 color_reset=""
1865 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001866
Ed Heylcc6be0a2014-06-18 14:55:58 -07001867 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001868 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001869 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001870 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001871 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001872 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001873 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001874 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001875 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001876 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001877 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001878 printf "(%s seconds)" $secs
1879 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001880 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001881 echo
1882 return $ret
1883}
1884
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001885function _trigger_build()
1886(
1887 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001888 local T=$(gettop)
1889 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001890 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1891 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001892 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1893 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001894 fi
Kevin Dagostino185109b2024-01-11 17:39:02 +00001895 local ret=$?
1896 if [[ ret -eq 0 && -z "${ANDROID_QUIET_BUILD:-}" && -n "${ANDROID_BUILD_BANNER}" ]]; then
1897 echo "${ANDROID_BUILD_BANNER}"
1898 fi
1899 return $ret
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001900)
1901
1902function m()
1903(
1904 _trigger_build "all-modules" "$@"
1905)
1906
1907function mm()
1908(
1909 _trigger_build "modules-in-a-dir-no-deps" "$@"
1910)
1911
1912function mmm()
1913(
1914 _trigger_build "modules-in-dirs-no-deps" "$@"
1915)
1916
1917function mma()
1918(
1919 _trigger_build "modules-in-a-dir" "$@"
1920)
1921
1922function mmma()
1923(
1924 _trigger_build "modules-in-dirs" "$@"
1925)
1926
Matt Alexanderd9c56562020-05-21 10:49:17 +00001927function make()
1928{
Dan Willemsene9842242017-07-28 13:00:13 -07001929 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001930}
1931
Joe Onorato7cf6f972022-05-11 21:39:57 -07001932function _multitree_lunch_error()
1933{
1934 >&2 echo "Couldn't locate the top of the tree. Please run \'source build/envsetup.sh\' and multitree_lunch from the root of your workspace."
1935}
1936
1937function multitree_build()
1938{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001939 local T=$(multitree_gettop)
1940 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001941 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001942 else
1943 _multitree_lunch_error
1944 return 1
1945 fi
1946}
1947
Matt Alexanderd9c56562020-05-21 10:49:17 +00001948function provision()
1949{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001950 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1951 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1952 return 1
1953 fi
1954 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1955 echo "There is no provisioning script for the device." >&2
1956 return 1
1957 fi
1958
1959 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001960 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001961 shift 1
1962 else
1963 echo "This action will reflash your device."
1964 echo ""
1965 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1966 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001967 echo -n "Are you sure you want to do this (yes/no)? "
1968 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001969 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001970 echo "Not taking any action. Exiting." >&2
1971 return 1
1972 fi
1973 fi
1974 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1975}
1976
Jim Tanga881a252018-06-19 16:34:41 +08001977# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001978function enable_zsh_completion() {
1979 # Don't override user's options if bash-style completion is already enabled.
1980 if ! declare -f complete >/dev/null; then
1981 autoload -U compinit && compinit
1982 autoload -U bashcompinit && bashcompinit
1983 fi
Jim Tanga881a252018-06-19 16:34:41 +08001984}
1985
1986function validate_current_shell() {
1987 local current_sh="$(ps -o command -p $$)"
1988 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001989 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001990 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001991 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001992 *zsh*)
1993 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001994 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001995 *)
Jim Tanga881a252018-06-19 16:34:41 +08001996 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 -07001997 ;;
1998 esac
Jim Tanga881a252018-06-19 16:34:41 +08001999}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002000
2001# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08002002# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
2003# load those.
2004#
2005# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002006function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002007 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002008 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002009 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002010 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 -08002011 if [ -n "$allowed" ]; then
2012 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2013 echo " $allowed"
2014 echo " $f"
2015 return
2016 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002017 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002018 done
2019
2020 allowed_files=
2021 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002022 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002023 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002024 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002025
2026 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002027 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002028 else
2029 echo "ignoring $f, not in $allowed"
2030 fi
Jim Tanga881a252018-06-19 16:34:41 +08002031 done
2032 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00002033
2034 if [[ "${PWD}" == /google/cog/* ]]; then
2035 f="build/make/cogsetup.sh"
2036 echo "including $f"; . "$T/$f"
2037 fi
Jim Tanga881a252018-06-19 16:34:41 +08002038}
Kenny Root52aa81c2011-07-15 11:07:06 -07002039
Dan Albertbab814f2020-08-26 15:34:53 -07002040function showcommands() {
2041 local T=$(gettop)
2042 if [[ -z "$TARGET_PRODUCT" ]]; then
2043 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2044 return
2045 fi
2046 case $(uname -s) in
2047 Darwin)
2048 PREBUILT_NAME=darwin-x86
2049 ;;
2050 Linux)
2051 PREBUILT_NAME=linux-x86
2052 ;;
2053 *)
2054 >&2 echo Unknown host $(uname -s)
2055 return
2056 ;;
2057 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002058 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002059 if [[ "$1" == "--regenerate" ]]; then
2060 shift 1
2061 NINJA_ARGS="-t commands $@" m
2062 else
2063 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2064 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2065 -t commands "$@")
2066 fi
2067}
2068
Cole Faust45844ab2022-08-30 13:59:07 -07002069function avbtool() {
2070 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2071 m avbtool
2072 fi
2073 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2074}
2075
Zhi Dou77c9f0c2023-10-11 19:49:56 +00002076function overrideflags() {
2077 local T="$(gettop)"
2078 (\cd "${T}" && build/make/tools/overrideflags.sh "$@")
2079}
2080
Colin Cross71319722023-10-24 10:58:46 -07002081function aninja() {
2082 local T="$(gettop)"
2083 (\cd "${T}" && prebuilts/build-tools/linux-x86/bin/ninja -f out/combined-${TARGET_PRODUCT}.ninja "$@")
2084}
2085
Jim Tanga881a252018-06-19 16:34:41 +08002086validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002087set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002088source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002089addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002090