blob: 3b76980566ccc05e29e55f7cb4f1db91a5d5b97d [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).
Ulya Trafimovich08c381b2023-06-12 15:33:09 +010065- banchan: banchan <module1> [<module2> ...] \
66 [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
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800257 # Handle compat with the old ANDROID_BUILD_PATHS variable.
258 # 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...
369 ANDROID_GLOBAL_BUILD_PATHS=$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800370 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
371 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800372
373 # add kernel specific binaries
374 if [ $(uname -s) = Linux ] ; then
375 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
376 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
377 fi
378
379 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
380 # to ensure that the corresponding 'emulator' binaries are used.
381 case $(uname -s) in
382 Darwin)
383 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
384 ;;
385 Linux)
386 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
387 ;;
388 *)
389 ANDROID_EMULATOR_PREBUILTS=
390 ;;
391 esac
392 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
393 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
394 export ANDROID_EMULATOR_PREBUILTS
395 fi
396
397 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800398 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700399}
400
Matt Alexanderd9c56562020-05-21 10:49:17 +0000401function printconfig()
402{
Christopher Ferris55257d22017-03-23 11:08:58 -0700403 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800404 if [ ! "$T" ]; then
405 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
406 return
407 fi
408 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700409}
410
Matt Alexanderd9c56562020-05-21 10:49:17 +0000411function set_stuff_for_environment()
412{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800413 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800414 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700415
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800416 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700417}
418
Matt Alexanderd9c56562020-05-21 10:49:17 +0000419function set_sequence_number()
420{
Colin Cross88737132017-03-21 17:41:03 -0700421 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700422}
423
Makoto Onukida971062018-06-18 10:15:19 -0700424# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
425function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800426 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700427 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800428 *:"$cmd":*)
429 return 1
430 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700431 esac
432 return 0
433}
434
Matt Alexanderd9c56562020-05-21 10:49:17 +0000435function addcompletions()
436{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700437 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700438
Jim Tanga881a252018-06-19 16:34:41 +0800439 # Keep us from trying to run in something that's neither bash nor zsh.
440 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700441 return
442 fi
443
444 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800445 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700446 return
447 fi
448
Jim Tanga881a252018-06-19 16:34:41 +0800449 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000450 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800451 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800452 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400453 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800454 )
Makoto Onukida971062018-06-18 10:15:19 -0700455 # Completion can be disabled selectively to allow users to use non-standard completion.
456 # e.g.
457 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
458 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400459 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800460 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400461 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000462 if [ ! -f "$f" ]; then
463 echo "Warning: completion file $f not found"
464 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700465 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700466 fi
467 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700468
Matt Alexanderd9c56562020-05-21 10:49:17 +0000469 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700470 complete -C "bit --tab" bit
471 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000472 if [ -z "$ZSH_VERSION" ]; then
473 # Doesn't work in zsh.
474 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400475 # TODO(b/244559459): Support b autocompletion for zsh
476 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000477 fi
Jim Tanga881a252018-06-19 16:34:41 +0800478 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800479
Cole Faust24c36db2021-01-23 02:39:37 +0000480 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100481 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000482 complete -F _complete_android_module_names outmod
483 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000484 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100485 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700486}
487
Joe Onorato824608c2022-04-08 11:06:16 -0700488function multitree_lunch_help()
489{
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600490 echo "usage: lunch PRODUCT-RELEASE-VARIANT" 1>&2
Joe Onorato824608c2022-04-08 11:06:16 -0700491 echo " Set up android build environment based on a product short name and variant" 1>&2
492 echo 1>&2
493 echo "lunch COMBO_FILE VARIANT" 1>&2
494 echo " Set up android build environment based on a specific lunch combo file" 1>&2
495 echo " and variant." 1>&2
496 echo 1>&2
497 echo "lunch --print [CONFIG]" 1>&2
498 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
499 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
500 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
501 echo 1>&2
502 echo "lunch --list" 1>&2
503 echo " List all possible combo files available in the current tree" 1>&2
504 echo 1>&2
505 echo "lunch --help" 1>&2
506 echo "lunch -h" 1>&2
507 echo " Prints this message." 1>&2
508}
509
510function multitree_lunch()
511{
512 local code
513 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000514 # Lunch must be run in the topdir, but this way we get a clear error
515 # message, instead of FileNotFound.
516 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000517 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000518 _multitree_lunch_error
519 return 1
520 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700521 if $(echo "$1" | grep -q '^-') ; then
522 # Calls starting with a -- argument are passed directly and the function
523 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000524 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700525 code=$?
526 if [[ $code -eq 2 ]] ; then
527 echo 1>&2
528 multitree_lunch_help
529 return $code
530 elif [[ $code -ne 0 ]] ; then
531 return $code
532 fi
533 else
534 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000535 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700536 code=$?
537 if [[ $code -eq 2 ]] ; then
538 echo 1>&2
539 multitree_lunch_help
540 return $code
541 elif [[ $code -ne 0 ]] ; then
542 return $code
543 fi
544
545 export TARGET_BUILD_COMBO=${results[0]}
546 export TARGET_BUILD_VARIANT=${results[1]}
547 fi
548}
549
Matt Alexanderd9c56562020-05-21 10:49:17 +0000550function choosetype()
551{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552 echo "Build type choices are:"
553 echo " 1. release"
554 echo " 2. debug"
555 echo
556
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700558 DEFAULT_NUM=1
559 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561 export TARGET_BUILD_TYPE=
562 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 while [ -z $TARGET_BUILD_TYPE ]
564 do
565 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000566 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567 read ANSWER
568 else
569 echo $1
570 ANSWER=$1
571 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700572 case $ANSWER in
573 "")
574 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
575 ;;
576 1)
577 export TARGET_BUILD_TYPE=release
578 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800579 release)
580 export TARGET_BUILD_TYPE=release
581 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582 2)
583 export TARGET_BUILD_TYPE=debug
584 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 debug)
586 export TARGET_BUILD_TYPE=debug
587 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700588 *)
589 echo
590 echo "I didn't understand your response. Please try again."
591 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700592 ;;
593 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000594 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 break
596 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700597 done
598
Ying Wang08800fd2016-03-03 20:57:21 -0800599 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800601 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602}
603
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800604#
605# This function isn't really right: It chooses a TARGET_PRODUCT
606# based on the list of boards. Usually, that gets you something
607# that kinda works with a generic product, but really, you should
608# pick a product by name.
609#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000610function chooseproduct()
611{
Christopher Ferris55257d22017-03-23 11:08:58 -0700612 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000613 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700614 default_value=$TARGET_PRODUCT
615 else
Ying Wang0a76df52015-06-08 11:57:26 -0700616 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700617 fi
618
Ying Wang08800fd2016-03-03 20:57:21 -0800619 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800620 export TARGET_PRODUCT=
621 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700622 while [ -z "$TARGET_PRODUCT" ]
623 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700624 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000625 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626 read ANSWER
627 else
628 echo $1
629 ANSWER=$1
630 fi
631
Matt Alexanderd9c56562020-05-21 10:49:17 +0000632 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700633 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800634 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000635 if check_product $ANSWER
636 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800637 export TARGET_PRODUCT=$ANSWER
638 else
639 echo "** Not a valid product: $ANSWER"
640 fi
641 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000642 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800643 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700644 fi
645 done
646
Ying Wang08800fd2016-03-03 20:57:21 -0800647 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800649 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700650}
651
Matt Alexanderd9c56562020-05-21 10:49:17 +0000652function choosevariant()
653{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800654 echo "Variant choices are:"
655 local index=1
656 local v
657 for v in ${VARIANT_CHOICES[@]}
658 do
659 # The product name is the name of the directory containing
660 # the makefile we found, above.
661 echo " $index. $v"
662 index=$(($index+1))
663 done
664
665 local default_value=eng
666 local ANSWER
667
668 export TARGET_BUILD_VARIANT=
669 while [ -z "$TARGET_BUILD_VARIANT" ]
670 do
671 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000672 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800673 read ANSWER
674 else
675 echo $1
676 ANSWER=$1
677 fi
678
Matt Alexanderd9c56562020-05-21 10:49:17 +0000679 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800680 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000681 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
682 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200683 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800684 fi
685 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000686 if check_variant $ANSWER
687 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800688 export TARGET_BUILD_VARIANT=$ANSWER
689 else
690 echo "** Not a valid variant: $ANSWER"
691 fi
692 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800694 break
695 fi
696 done
697}
698
Matt Alexanderd9c56562020-05-21 10:49:17 +0000699function choosecombo()
700{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700701 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700702
703 echo
704 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700705 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700706
707 echo
708 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700709 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800710
711 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800712 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700713 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800714 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800715 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716}
717
Matt Alexanderd9c56562020-05-21 10:49:17 +0000718function add_lunch_combo()
719{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800720 if [ -n "$ZSH_VERSION" ]; then
721 echo -n "${funcfiletrace[1]}: "
722 else
723 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
724 fi
725 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 -0800726}
727
Matt Alexanderd9c56562020-05-21 10:49:17 +0000728function print_lunch_menu()
729{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700730 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100731 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600732 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 +0100733 local ret=$?
734
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700735 echo
736 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700737 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100738
Matt Alexanderd9c56562020-05-21 10:49:17 +0000739 if [ $ret -ne 0 ]
740 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100741 echo "Warning: Cannot display lunch menu."
742 echo
743 echo "Note: You can invoke lunch with an explicit target:"
744 echo
745 echo " usage: lunch [target]" >&2
746 echo
747 return
748 fi
749
Will Burr40401202022-02-07 12:12:01 +0000750 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800751
752 local i=1
753 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700754 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800755 do
756 echo " $i. $choice"
757 i=$(($i+1))
758 done
759
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700760 echo
761}
762
Matt Alexanderd9c56562020-05-21 10:49:17 +0000763function lunch()
764{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800765 local answer
766
Steven Moreland92793dc2020-02-25 18:30:18 -0800767 if [[ $# -gt 1 ]]; then
768 echo "usage: lunch [target]" >&2
769 return 1
770 fi
771
Will Burr40401202022-02-07 12:12:01 +0000772 local used_lunch_menu=0
773
Steven Moreland92793dc2020-02-25 18:30:18 -0800774 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800775 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776 else
777 print_lunch_menu
Greg Kaisera13cd282023-10-25 16:17:40 +0000778 echo "Which would you like? [aosp_arm-trunk_staging-eng]"
Will Burr40401202022-02-07 12:12:01 +0000779 echo -n "Pick from common choices above (e.g. 13) or specify your own (e.g. aosp_barbet-eng): "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800780 read answer
Will Burr40401202022-02-07 12:12:01 +0000781 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700782 fi
783
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800784 local selection=
785
Matt Alexanderd9c56562020-05-21 10:49:17 +0000786 if [ -z "$answer" ]
787 then
Greg Kaisera13cd282023-10-25 16:17:40 +0000788 selection=aosp_arm-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000789 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
790 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800791 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000792 if [ $answer -le ${#choices[@]} ]
793 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800794 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000795 if [ -n "$ZSH_VERSION" ]
796 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800797 selection=${choices[$(($answer))]}
798 else
799 selection=${choices[$(($answer-1))]}
800 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800801 fi
Colin Cross88737132017-03-21 17:41:03 -0700802 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800803 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700804 fi
805
Joe Onoratoda12daf2010-06-09 18:18:31 -0700806 export TARGET_BUILD_APPS=
807
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600808 # This must be <product>-<release>-<variant>
809 local product release variant
810 # Split string on the '-' character.
811 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800812
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600813 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000814 then
Ying Wang08800fd2016-03-03 20:57:21 -0800815 echo
Colin Cross88737132017-03-21 17:41:03 -0700816 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600817 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700818 return 1
819 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800820
Colin Cross88737132017-03-21 17:41:03 -0700821 TARGET_PRODUCT=$product \
822 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000823 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700824 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000825 if [ $? -ne 0 ]
826 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100827 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
828 then
829 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
830 fi
Colin Cross88737132017-03-21 17:41:03 -0700831 return 1
832 fi
Colin Cross88737132017-03-21 17:41:03 -0700833 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
834 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600835 export TARGET_RELEASE=$release
836 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700837 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700838
Will Burr40401202022-02-07 12:12:01 +0000839 if [ $used_lunch_menu -eq 1 ]; then
840 echo
841 echo "Hint: next time you can simply run 'lunch $selection'"
842 fi
843
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700844 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800845
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700846 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700847 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800848 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400849
850 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
851 check_mu_config
852 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700853}
854
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700855unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700856# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000857function _lunch()
858{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700859 local cur prev opts
860 COMPREPLY=()
861 cur="${COMP_WORDS[COMP_CWORD]}"
862 prev="${COMP_WORDS[COMP_CWORD-1]}"
863
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700864 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800865 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700866 fi
867
868 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700869 return 0
870}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700871
Joe Onoratoda12daf2010-06-09 18:18:31 -0700872# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700873# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000874function tapas()
875{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700876 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800877 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000878 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
879 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700880 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700881 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 -0800882 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
883 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)"
884
Joe Onoratoda12daf2010-06-09 18:18:31 -0700885
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700886 if [ "$showHelp" != "" ]; then
887 $(gettop)/build/make/tapasHelp.sh
888 return
889 fi
890
Ying Wang67f02922012-08-22 10:25:20 -0700891 if [ $(echo $arch | wc -w) -gt 1 ]; then
892 echo "tapas: Error: Multiple build archs supplied: $arch"
893 return
894 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000895 if [ $(echo $release | wc -w) -gt 1 ]; then
896 echo "tapas: Error: Multiple build releases supplied: $release"
897 return
898 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700899 if [ $(echo $variant | wc -w) -gt 1 ]; then
900 echo "tapas: Error: Multiple build variants supplied: $variant"
901 return
902 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700903 if [ $(echo $density | wc -w) -gt 1 ]; then
904 echo "tapas: Error: Multiple densities supplied: $density"
905 return
906 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800907 if [ $(echo $keys | wc -w) -gt 1 ]; then
908 echo "tapas: Error: Multiple keys supplied: $keys"
909 return
910 fi
Ying Wang67f02922012-08-22 10:25:20 -0700911
Ying Wang0a76df52015-06-08 11:57:26 -0700912 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700913 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000914 x86) product=aosp_x86;;
915 arm64) product=aosp_arm64;;
916 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700917 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800918 if [ -n "$keys" ]; then
919 product=${product/aosp_/aosp_${keys}_}
920 fi;
921
Joe Onoratoda12daf2010-06-09 18:18:31 -0700922 if [ -z "$variant" ]; then
923 variant=eng
924 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700925 if [ -z "$apps" ]; then
926 apps=all
927 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600928 if [ -z "$density" ]; then
929 density=alldpi
930 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700931
Ying Wang67f02922012-08-22 10:25:20 -0700932 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000933 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700934 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700935 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700936 export TARGET_BUILD_TYPE=release
937 export TARGET_BUILD_APPS=$apps
938
Ying Wang08800fd2016-03-03 20:57:21 -0800939 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700940 set_stuff_for_environment
941 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800942 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700943}
944
Martin Stjernholmf692c752021-04-12 00:01:10 +0100945# Configures the build to build unbundled Android modules (APEXes).
946# Run banchan with one or more module names (from apex{} modules).
947function banchan()
948{
949 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100950 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 -0600951 # TODO: Expand banchan to take release arguments (and update hmm() usage).
952 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100953 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100954 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 +0100955
956 if [ "$showHelp" != "" ]; then
957 $(gettop)/build/make/banchanHelp.sh
958 return
959 fi
960
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100961 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000962 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100963 elif [ $(echo $product | wc -w) -gt 1 ]; then
964 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100965 return
966 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600967 if [ $(echo $release | wc -w) -gt 1 ]; then
968 echo "banchan: Error: Multiple build releases supplied: $release"
969 return
970 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100971 if [ $(echo $variant | wc -w) -gt 1 ]; then
972 echo "banchan: Error: Multiple build variants supplied: $variant"
973 return
974 fi
975 if [ -z "$apps" ]; then
976 echo "banchan: Error: No modules supplied"
977 return
978 fi
979
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100980 case $product in
981 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100982 x86) product=module_x86;;
983 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100984 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100985 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000986 arm64only) product=module_arm64only;;
987 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100988 esac
989 if [ -z "$variant" ]; then
990 variant=eng
991 fi
992
993 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -0600994 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +0100995 export TARGET_BUILD_VARIANT=$variant
996 export TARGET_BUILD_DENSITY=alldpi
997 export TARGET_BUILD_TYPE=release
998
999 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
1000 # case is different and it may diverge in the future.
1001 export TARGET_BUILD_APPS=$apps
1002
1003 build_build_var_cache
1004 set_stuff_for_environment
1005 printconfig
1006 destroy_build_var_cache
1007}
1008
Joe Onorato7cf6f972022-05-11 21:39:57 -07001009# TODO: Merge into gettop as part of launching multitree
1010function multitree_gettop
1011{
Spandan Dasca762052022-09-21 00:08:34 +00001012 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -07001013 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
1014 # The following circumlocution ensures we remove symlinks from TOP.
1015 (cd "$TOP"; PWD= /bin/pwd)
1016 else
1017 if [ -f $TOPFILE ] ; then
1018 # The following circumlocution (repeated below as well) ensures
1019 # that we record the true directory name and not one that is
1020 # faked up with symlink names.
1021 PWD= /bin/pwd
1022 else
1023 local HERE=$PWD
1024 local T=
1025 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1026 \cd ..
1027 T=`PWD= /bin/pwd -P`
1028 done
1029 \cd "$HERE"
1030 if [ -f "$T/$TOPFILE" ]; then
1031 echo "$T"
1032 fi
1033 fi
1034 fi
1035}
1036
Matt Alexanderd9c56562020-05-21 10:49:17 +00001037function croot()
1038{
Christopher Ferris55257d22017-03-23 11:08:58 -07001039 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001040 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001041 if [ "$1" ]; then
1042 \cd $(gettop)/$1
1043 else
1044 \cd $(gettop)
1045 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001046 else
1047 echo "Couldn't locate the top of the tree. Try setting TOP."
1048 fi
1049}
1050
Matt Alexanderd9c56562020-05-21 10:49:17 +00001051function _croot()
1052{
Anton Hanssonece9c482019-02-04 18:15:39 +00001053 local T=$(gettop)
1054 if [ "$T" ]; then
1055 local cur="${COMP_WORDS[COMP_CWORD]}"
1056 k=0
1057 for c in $(compgen -d ${T}/${cur}); do
1058 COMPREPLY[k++]=${c#${T}/}/
1059 done
1060 fi
1061}
1062
Matt Alexanderd9c56562020-05-21 10:49:17 +00001063function cproj()
1064{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001065 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001066 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001067 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001068 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1069 T=$PWD
1070 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001071 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001072 return
1073 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001074 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001075 done
Ying Wang9cd17642012-12-13 10:52:07 -08001076 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001077 echo "can't find Android.mk"
1078}
1079
Shaju Mathew21439002023-07-10 00:53:22 +00001080function adb() {
Saagar Jha11f188c2023-07-13 23:31:59 +00001081 command adb "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +00001082}
1083
Daniel Sandler47e0a882013-07-30 13:23:52 -04001084# simplified version of ps; output in the form
1085# <pid> <procname>
1086function qpid() {
1087 local prepend=''
1088 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001090 prepend=' '
1091 append='$'
1092 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001093 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001094 echo "usage: qpid [[--exact] <process name|pid>"
1095 return 255
1096 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001097
1098 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001099 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001100 qpid | \grep "$prepend$EXE$append"
1101 else
1102 adb shell ps \
1103 | tr -d '\r' \
1104 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1105 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001106}
1107
Steven Moreland74114f12020-09-10 01:23:32 +00001108# syswrite - disable verity, reboot if needed, and remount image
1109#
1110# Easy way to make system.img/etc writable
1111function syswrite() {
Steven Moreland7275a192023-04-17 17:21:01 +00001112 adb wait-for-device && adb root && adb wait-for-device || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001113 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001114 echo "rebooting"
Steven Moreland7275a192023-04-17 17:21:01 +00001115 adb reboot && adb wait-for-device && adb root && adb wait-for-device || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001116 fi
Steven Moreland7275a192023-04-17 17:21:01 +00001117 adb remount || return 1
Steven Moreland74114f12020-09-10 01:23:32 +00001118}
1119
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001120# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001121# that has the core-file-size limit set correctly
1122#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001123# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124# if its core-file-size limit is not set already.
1125# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1126
Matt Alexanderd9c56562020-05-21 10:49:17 +00001127function coredump_setup()
1128{
1129 echo "Getting root...";
1130 adb root;
1131 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001132
Matt Alexanderd9c56562020-05-21 10:49:17 +00001133 echo "Remounting root partition read-write...";
1134 adb shell mount -w -o remount -t rootfs rootfs;
1135 sleep 1;
1136 adb wait-for-device;
1137 adb shell mkdir -p /cores;
1138 adb shell mount -t tmpfs tmpfs /cores;
1139 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001140
Matt Alexanderd9c56562020-05-21 10:49:17 +00001141 echo "Granting SELinux permission to dump in /cores...";
1142 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001143
Matt Alexanderd9c56562020-05-21 10:49:17 +00001144 echo "Set core pattern.";
1145 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001146
Ying Wang08800fd2016-03-03 20:57:21 -08001147 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001148}
1149
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001150# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001151# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001152#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001153# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001154# dump to actually be generated.
1155
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156function coredump_enable()
1157{
1158 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001159 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001160 printf "Expecting a PID!\n";
1161 return;
1162 fi;
1163 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001164 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001165}
1166
1167# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001168# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001169#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001170# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001171# enabled globally.
1172
Matt Alexanderd9c56562020-05-21 10:49:17 +00001173function core()
1174{
1175 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001176
Ying Wang08800fd2016-03-03 20:57:21 -08001177 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178 printf "Expecting a PID!\n";
1179 return;
1180 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001181
Matt Alexanderd9c56562020-05-21 10:49:17 +00001182 local CORENAME=core.$PID;
1183 local COREPATH=/cores/$CORENAME;
1184 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001185
Matt Alexanderd9c56562020-05-21 10:49:17 +00001186 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001187
Matt Alexanderd9c56562020-05-21 10:49:17 +00001188 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001189 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001190 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1191 adb shell kill -$SIG $PID;
1192 sleep 1;
1193 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001194
Matt Alexanderd9c56562020-05-21 10:49:17 +00001195 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1196 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001197}
1198
Christopher Tate744ee802009-11-12 15:33:08 -08001199# systemstack - dump the current stack trace of all threads in the system process
1200# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001201function systemstack()
1202{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001203 stacks system_server
1204}
1205
Michael Wrightaeed7212014-06-19 19:58:12 -07001206# Read the ELF header from /proc/$PID/exe to determine if the process is
1207# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001208function is64bit()
1209{
Ben Chengfba67bf2014-02-25 10:27:07 -08001210 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001211 if [ "$PID" ] ; then
1212 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001213 echo "64"
1214 else
1215 echo ""
1216 fi
1217 else
1218 echo ""
1219 fi
1220}
1221
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001222case `uname -s` in
1223 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001224 function sgrep()
1225 {
Joe Onorato95670782023-02-27 15:53:09 -08001226 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 -04001227 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001228 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001229
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001230 ;;
1231 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001232 function sgrep()
1233 {
Joe Onorato95670782023-02-27 15:53:09 -08001234 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 -04001235 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001236 }
1237 ;;
1238esac
1239
Matt Alexanderd9c56562020-05-21 10:49:17 +00001240function gettargetarch
1241{
Raghu Gandham8da43102012-07-25 19:57:22 -07001242 get_build_var TARGET_ARCH
1243}
1244
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245function ggrep()
1246{
Mike Frysinger5e479732015-09-22 18:13:48 -04001247 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1248 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function gogrep()
1252{
Orion Hodson831472d2019-10-25 11:35:15 +01001253 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1254 -exec grep --color -n "$@" {} +
1255}
1256
Matt Alexanderd9c56562020-05-21 10:49:17 +00001257function jgrep()
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 "*\.java" \
1260 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001261}
1262
Alistair Delva176e5342021-02-22 13:31:26 -08001263function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001264{
1265 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1266 -exec grep --color -n "$@" {} +
1267}
1268
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001269function jsongrep()
1270{
1271 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1272 -exec grep --color -n "$@" {} +
1273}
1274
1275function tomlgrep()
1276{
1277 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1278 -exec grep --color -n "$@" {} +
1279}
1280
Taesu Leeea0cecd2020-10-28 11:05:18 +09001281function ktgrep()
1282{
1283 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1284 -exec grep --color -n "$@" {} +
1285}
1286
Matt Alexanderd9c56562020-05-21 10:49:17 +00001287function cgrep()
1288{
Mike Frysinger5e479732015-09-22 18:13:48 -04001289 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' \) \
1290 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001291}
1292
Matt Alexanderd9c56562020-05-21 10:49:17 +00001293function resgrep()
1294{
Christopher Ferris55257d22017-03-23 11:08:58 -07001295 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001296 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1297 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1298 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001299}
1300
Matt Alexanderd9c56562020-05-21 10:49:17 +00001301function mangrep()
1302{
Mike Frysinger5e479732015-09-22 18:13:48 -04001303 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1304 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001305}
1306
Matt Alexanderd9c56562020-05-21 10:49:17 +00001307function owngrep()
1308{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001309 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1310 -exec grep --color -n "$@" {} +
1311}
1312
Matt Alexanderd9c56562020-05-21 10:49:17 +00001313function sepgrep()
1314{
Mike Frysinger5e479732015-09-22 18:13:48 -04001315 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1316 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001317}
1318
Matt Alexanderd9c56562020-05-21 10:49:17 +00001319function rcgrep()
1320{
Mike Frysinger5e479732015-09-22 18:13:48 -04001321 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1322 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001323}
1324
DroidFreak32a2781982020-11-26 11:30:19 -05001325function pygrep()
1326{
1327 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1328 -exec grep --color -n "$@" {} +
1329}
1330
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001331case `uname -s` in
1332 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001333 function mgrep()
1334 {
Orion Hodson831472d2019-10-25 11:35:15 +01001335 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 -04001336 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001337 }
1338
Matt Alexanderd9c56562020-05-21 10:49:17 +00001339 function treegrep()
1340 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001341 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 -04001342 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001343 }
1344
1345 ;;
1346 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001347 function mgrep()
1348 {
Orion Hodson831472d2019-10-25 11:35:15 +01001349 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 -04001350 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001351 }
1352
Matt Alexanderd9c56562020-05-21 10:49:17 +00001353 function treegrep()
1354 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001355 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 -04001356 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001357 }
1358
1359 ;;
1360esac
1361
Matt Alexanderd9c56562020-05-21 10:49:17 +00001362function getprebuilt
1363{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001364 get_abs_build_var ANDROID_PREBUILTS
1365}
1366
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001367# communicate with a running device or emulator, set up necessary state,
1368# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001369function runhat()
1370{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001371 # process standard adb options
1372 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001373 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001374 adbTarget=$1
1375 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001376 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001377 adbTarget="$1 $2"
1378 shift 2
1379 fi
1380 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001381 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001382
1383 # runhat options
1384 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001385
Matt Alexanderd9c56562020-05-21 10:49:17 +00001386 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001387 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001388 return
1389 fi
1390
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001391 # confirm hat is available
1392 if [ -z $(which hat) ]; then
1393 echo "hat is not available in this configuration."
1394 return
1395 fi
1396
Andy McFaddenb6289852010-07-12 08:00:19 -07001397 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001398 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001399 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001400 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001401 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001402 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001403 echo -n "> "
1404 read
1405
The Android Open Source Project88b60792009-03-03 19:28:42 -08001406 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001407
The Android Open Source Project88b60792009-03-03 19:28:42 -08001408 echo "Retrieving file $devFile..."
1409 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001410
The Android Open Source Project88b60792009-03-03 19:28:42 -08001411 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001412
The Android Open Source Project88b60792009-03-03 19:28:42 -08001413 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001414 echo "View the output by pointing your browser at http://localhost:7000/"
1415 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001416 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001417}
1418
Matt Alexanderd9c56562020-05-21 10:49:17 +00001419function getbugreports()
1420{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001421 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001422
1423 if [ ! "$reports" ]; then
1424 echo "Could not locate any bugreports."
1425 return
1426 fi
1427
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001428 local report
1429 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001430 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001431 echo "/sdcard/bugreports/${report}"
1432 adb pull /sdcard/bugreports/${report} ${report}
1433 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001434 done
1435}
1436
Matt Alexanderd9c56562020-05-21 10:49:17 +00001437function getsdcardpath()
1438{
Victoria Lease1b296b42012-08-21 15:44:06 -07001439 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1440}
1441
Matt Alexanderd9c56562020-05-21 10:49:17 +00001442function getscreenshotpath()
1443{
Victoria Lease1b296b42012-08-21 15:44:06 -07001444 echo "$(getsdcardpath)/Pictures/Screenshots"
1445}
1446
Matt Alexanderd9c56562020-05-21 10:49:17 +00001447function getlastscreenshot()
1448{
Victoria Lease1b296b42012-08-21 15:44:06 -07001449 local screenshot_path=$(getscreenshotpath)
1450 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 +00001451 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001452 echo "No screenshots found."
1453 return
1454 fi
1455 echo "${screenshot}"
1456 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1457}
1458
Matt Alexanderd9c56562020-05-21 10:49:17 +00001459function startviewserver()
1460{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001461 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001462 if [ $# -gt 0 ]; then
1463 port=$1
1464 fi
1465 adb shell service call window 1 i32 $port
1466}
1467
Matt Alexanderd9c56562020-05-21 10:49:17 +00001468function stopviewserver()
1469{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001470 adb shell service call window 2
1471}
1472
Matt Alexanderd9c56562020-05-21 10:49:17 +00001473function isviewserverstarted()
1474{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001475 adb shell service call window 3
1476}
1477
Matt Alexanderd9c56562020-05-21 10:49:17 +00001478function key_home()
1479{
Romain Guyb84049a2010-10-04 16:56:11 -07001480 adb shell input keyevent 3
1481}
1482
Matt Alexanderd9c56562020-05-21 10:49:17 +00001483function key_back()
1484{
Romain Guyb84049a2010-10-04 16:56:11 -07001485 adb shell input keyevent 4
1486}
1487
Matt Alexanderd9c56562020-05-21 10:49:17 +00001488function key_menu()
1489{
Romain Guyb84049a2010-10-04 16:56:11 -07001490 adb shell input keyevent 82
1491}
1492
Matt Alexanderd9c56562020-05-21 10:49:17 +00001493function smoketest()
1494{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001495 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1496 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1497 return
1498 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001499 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001500 if [ ! "$T" ]; then
1501 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1502 return
1503 fi
1504
Ying Wang9cd17642012-12-13 10:52:07 -08001505 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001506 adb uninstall com.android.smoketest > /dev/null &&
1507 adb uninstall com.android.smoketest.tests > /dev/null &&
1508 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1509 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1510 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1511}
1512
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001513# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001514function runtest()
1515{
Christopher Ferris55257d22017-03-23 11:08:58 -07001516 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001517 if [ ! "$T" ]; then
1518 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1519 return
1520 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001521 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001522}
1523
The Android Open Source Project88b60792009-03-03 19:28:42 -08001524function godir () {
1525 if [[ -z "$1" ]]; then
1526 echo "Usage: godir <regex>"
1527 return
1528 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001529 local T=$(gettop)
1530 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001531 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001532 mkdir -p $OUT_DIR
1533 FILELIST=$OUT_DIR/filelist
1534 else
1535 FILELIST=$T/filelist
1536 fi
1537 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001538 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001539 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001540 echo " Done"
1541 echo ""
1542 fi
1543 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001544 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001545 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001546 echo "Not found"
1547 return
1548 fi
1549 local pathname
1550 local choice
1551 if [[ ${#lines[@]} > 1 ]]; then
1552 while [[ -z "$pathname" ]]; do
1553 local index=1
1554 local line
1555 for line in ${lines[@]}; do
1556 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001557 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001558 done
1559 echo
1560 echo -n "Select one: "
1561 unset choice
1562 read choice
1563 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1564 echo "Invalid choice"
1565 continue
1566 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001567 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001568 done
1569 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001570 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001571 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001572 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001573}
1574
Steven Moreland62054a42018-12-06 10:11:40 -08001575# Update module-info.json in out.
1576function refreshmod() {
1577 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1578 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1579 return 1
1580 fi
1581
1582 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1583
1584 # for the output of the next command
1585 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1586
1587 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001588 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001589 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1590}
1591
Cole Faust3e192382021-10-25 13:29:15 -07001592# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001593function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001594 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001595 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1596 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1597 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001598 return 1
1599 fi
1600
1601 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001602 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001603 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001604 fi
1605 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001606 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001607}
1608
Cole Faust5d825b72022-10-26 18:16:44 -07001609# List all modules for the current device, as cached in all_modules.txt. If any build change is
1610# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001611function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001612 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001613}
1614
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001615# Return the Bazel label of a Soong module if it is converted with bp2build.
1616function bmod()
1617(
Jingwen Chen688e5d62023-10-16 09:52:27 +00001618 if [ $# -eq 0 ]; then
1619 echo "usage: bmod <module 1> <module 2> ... <module n>" >&2
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001620 return 1
1621 fi
1622
1623 # We could run bp2build here, but it might trigger bp2build invalidation
1624 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1625 # time overhead.
1626 #
1627 # For a snappy result, use the latest generated version in soong_injection,
1628 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001629 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001630
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001631 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001632 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1633 return 1
1634 fi
1635
Jingwen Chen688e5d62023-10-16 09:52:27 +00001636 modules=()
1637 for m in "$@"; do
1638 modules+=("\"$m\",")
1639 done
1640 local res=$(python3 -c "import json
1641modules = [${modules[*]}]
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001642converted_json='$converted_json'
1643bp2build_converted_map = json.load(open(converted_json))
Jingwen Chen688e5d62023-10-16 09:52:27 +00001644for module in modules:
1645 if module not in bp2build_converted_map:
1646 print(module + ' is not converted to Bazel.')
1647 else:
1648 print(bp2build_converted_map[module] + ':' + module)")
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001649
Jingwen Chen688e5d62023-10-16 09:52:27 +00001650 echo "${res}"
1651 unconverted_count=$(echo "${res}" | grep -c "not converted to Bazel")
1652 if [[ ${unconverted_count} -ne 0 ]]; then
1653 return 1
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001654 fi
1655)
1656
Joe Onorato2c1aa472021-02-25 16:42:39 -08001657# Get the path of a specific module in the android tree, as cached in module-info.json.
1658# If any build change is made, and it should be reflected in the output, you should run
1659# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001660function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001661 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001662 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001663 return 1
1664 fi
1665
Cole Faust24c36db2021-01-23 02:39:37 +00001666 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001667
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001668 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001669module = '$1'
1670module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1671if module not in module_info:
1672 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001673print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001674
1675 if [ -z "$relpath" ]; then
1676 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1677 return 1
1678 else
Rett Berg78d1c932019-01-24 14:34:23 -08001679 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001680 fi
1681}
1682
Joe Onorato2c1aa472021-02-25 16:42:39 -08001683# Get the path of a specific module in the android tree, as cached in module-info.json.
1684# If any build change is made, and it should be reflected in the output, you should run
1685# 'refreshmod' first. Note: This is the inverse of pathmod.
1686function dirmods() {
1687 if [[ $# -ne 1 ]]; then
1688 echo "usage: dirmods <path>" >&2
1689 return 1
1690 fi
1691
1692 verifymodinfo || return 1
1693
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001694 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001695dir = '$1'
1696while dir.endswith('/'):
1697 dir = dir[:-1]
1698prefix = dir + '/'
1699module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1700results = set()
1701for m in module_info.values():
1702 for path in m.get(u'path', []):
1703 if path == dir or path.startswith(prefix):
1704 name = m.get(u'module_name')
1705 if name:
1706 results.add(name)
1707for name in sorted(results):
1708 print(name)
1709"
1710}
1711
1712
Rett Berg78d1c932019-01-24 14:34:23 -08001713# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1714# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1715function gomod() {
1716 if [[ $# -ne 1 ]]; then
1717 echo "usage: gomod <module>" >&2
1718 return 1
1719 fi
1720
1721 local path="$(pathmod $@)"
1722 if [ -z "$path" ]; then
1723 return 1
1724 fi
1725 cd $path
1726}
1727
Cole Faust24c36db2021-01-23 02:39:37 +00001728# Gets the list of a module's installed outputs, as cached in module-info.json.
1729# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1730function outmod() {
1731 if [[ $# -ne 1 ]]; then
1732 echo "usage: outmod <module>" >&2
1733 return 1
1734 fi
1735
1736 verifymodinfo || return 1
1737
1738 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001739 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001740module = '$1'
1741module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1742if module not in module_info:
1743 exit(1)
1744for output in module_info[module]['installed']:
1745 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1746
1747 if [ $? -ne 0 ]; then
1748 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1749 return 1
1750 elif [ ! -z "$relpath" ]; then
1751 echo "$relpath"
1752 fi
1753}
1754
1755# adb install a module's apk, as cached in module-info.json. If any build change
1756# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1757# Usage: installmod [adb install arguments] <module>
1758# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1759function installmod() {
1760 if [[ $# -eq 0 ]]; then
1761 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001762 echo "" >&2
1763 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1764 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1765 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001766 return 1
1767 fi
1768
1769 local _path
1770 _path=$(outmod ${@:$#:1})
1771 if [ $? -ne 0 ]; then
1772 return 1
1773 fi
1774
1775 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1776 if [ -z "$_path" ]; then
1777 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1778 return 1
1779 fi
Cole Faust3e192382021-10-25 13:29:15 -07001780 local serial_device=""
1781 if [[ "$1" == "-s" ]]; then
1782 if [[ $# -le 2 ]]; then
1783 echo "-s requires an argument" >&2
1784 return 1
1785 fi
1786 serial_device="-s $2"
1787 shift 2
1788 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001789 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001790 echo adb $serial_device install ${@:1:$length} $_path
1791 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001792}
1793
dimitry73b84812018-12-11 18:06:00 +01001794function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001795 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001796 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001797}
1798
Alex Rayf0d08eb2013-03-08 15:15:06 -08001799# Print colored exit condition
1800function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001801 "$@"
1802 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001803 if [ $retval -ne 0 ]
1804 then
Jacky Cao89483b82015-05-15 22:12:53 +08001805 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001806 else
Jacky Cao89483b82015-05-15 22:12:53 +08001807 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001808 fi
1809 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001810}
1811
Matt Alexanderd9c56562020-05-21 10:49:17 +00001812function get_make_command()
1813{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001814 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1815 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001816 # Always use the real make if -C is passed in
1817 for arg in "$@"; do
1818 if [[ $arg == -C* ]]; then
1819 echo command make
1820 return
1821 fi
1822 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001823 echo build/soong/soong_ui.bash --make-mode
1824 else
1825 echo command make
1826 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001827}
1828
Matt Alexanderd9c56562020-05-21 10:49:17 +00001829function _wrap_build()
1830{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001831 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1832 "$@"
1833 return $?
1834 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001835 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001836 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001837 local ret=$?
1838 local end_time=$(date +"%s")
1839 local tdiff=$(($end_time-$start_time))
1840 local hours=$(($tdiff / 3600 ))
1841 local mins=$((($tdiff % 3600) / 60))
1842 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001843 local ncolors=$(tput colors 2>/dev/null)
1844 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001845 color_failed=$'\E'"[0;31m"
1846 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001847 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001848 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001849 else
1850 color_failed=""
1851 color_success=""
1852 color_reset=""
1853 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001854
Ed Heylcc6be0a2014-06-18 14:55:58 -07001855 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001856 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001857 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001858 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001859 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001860 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001861 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001862 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001863 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001864 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001865 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001866 printf "(%s seconds)" $secs
1867 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001868 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001869 echo
1870 return $ret
1871}
1872
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001873function _trigger_build()
1874(
1875 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001876 local T=$(gettop)
1877 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001878 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1879 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001880 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1881 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001882 fi
Ronald Braunstein6d10bbd2023-10-30 12:03:40 -07001883
1884 if [[ -z "${ANDROID_QUIET_BUILD:-}" && -n "${ANDROID_BUILD_BANNER}" ]]; then
1885 echo "$ANDROID_BUILD_BANNER"
1886 fi
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001887)
1888
1889function m()
1890(
1891 _trigger_build "all-modules" "$@"
1892)
1893
1894function mm()
1895(
1896 _trigger_build "modules-in-a-dir-no-deps" "$@"
1897)
1898
1899function mmm()
1900(
1901 _trigger_build "modules-in-dirs-no-deps" "$@"
1902)
1903
1904function mma()
1905(
1906 _trigger_build "modules-in-a-dir" "$@"
1907)
1908
1909function mmma()
1910(
1911 _trigger_build "modules-in-dirs" "$@"
1912)
1913
Matt Alexanderd9c56562020-05-21 10:49:17 +00001914function make()
1915{
Dan Willemsene9842242017-07-28 13:00:13 -07001916 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001917}
1918
Joe Onorato7cf6f972022-05-11 21:39:57 -07001919function _multitree_lunch_error()
1920{
1921 >&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."
1922}
1923
1924function multitree_build()
1925{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001926 local T=$(multitree_gettop)
1927 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001928 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001929 else
1930 _multitree_lunch_error
1931 return 1
1932 fi
1933}
1934
Matt Alexanderd9c56562020-05-21 10:49:17 +00001935function provision()
1936{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001937 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1938 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1939 return 1
1940 fi
1941 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1942 echo "There is no provisioning script for the device." >&2
1943 return 1
1944 fi
1945
1946 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001947 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001948 shift 1
1949 else
1950 echo "This action will reflash your device."
1951 echo ""
1952 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1953 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001954 echo -n "Are you sure you want to do this (yes/no)? "
1955 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001956 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001957 echo "Not taking any action. Exiting." >&2
1958 return 1
1959 fi
1960 fi
1961 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1962}
1963
Jim Tanga881a252018-06-19 16:34:41 +08001964# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001965function enable_zsh_completion() {
1966 # Don't override user's options if bash-style completion is already enabled.
1967 if ! declare -f complete >/dev/null; then
1968 autoload -U compinit && compinit
1969 autoload -U bashcompinit && bashcompinit
1970 fi
Jim Tanga881a252018-06-19 16:34:41 +08001971}
1972
1973function validate_current_shell() {
1974 local current_sh="$(ps -o command -p $$)"
1975 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001976 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001977 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001978 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001979 *zsh*)
1980 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001981 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001982 *)
Jim Tanga881a252018-06-19 16:34:41 +08001983 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 -07001984 ;;
1985 esac
Jim Tanga881a252018-06-19 16:34:41 +08001986}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001987
1988# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001989# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1990# load those.
1991#
1992# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001993function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001994 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001995 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001996 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001997 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 -08001998 if [ -n "$allowed" ]; then
1999 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2000 echo " $allowed"
2001 echo " $f"
2002 return
2003 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002004 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002005 done
2006
2007 allowed_files=
2008 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002009 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002010 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002011 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002012
2013 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002014 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002015 else
2016 echo "ignoring $f, not in $allowed"
2017 fi
Jim Tanga881a252018-06-19 16:34:41 +08002018 done
2019 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00002020
2021 if [[ "${PWD}" == /google/cog/* ]]; then
2022 f="build/make/cogsetup.sh"
2023 echo "including $f"; . "$T/$f"
2024 fi
Jim Tanga881a252018-06-19 16:34:41 +08002025}
Kenny Root52aa81c2011-07-15 11:07:06 -07002026
Dan Albertbab814f2020-08-26 15:34:53 -07002027function showcommands() {
2028 local T=$(gettop)
2029 if [[ -z "$TARGET_PRODUCT" ]]; then
2030 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2031 return
2032 fi
2033 case $(uname -s) in
2034 Darwin)
2035 PREBUILT_NAME=darwin-x86
2036 ;;
2037 Linux)
2038 PREBUILT_NAME=linux-x86
2039 ;;
2040 *)
2041 >&2 echo Unknown host $(uname -s)
2042 return
2043 ;;
2044 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002045 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002046 if [[ "$1" == "--regenerate" ]]; then
2047 shift 1
2048 NINJA_ARGS="-t commands $@" m
2049 else
2050 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2051 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2052 -t commands "$@")
2053 fi
2054}
2055
Cole Faust45844ab2022-08-30 13:59:07 -07002056function avbtool() {
2057 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2058 m avbtool
2059 fi
2060 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2061}
2062
Zhi Dou77c9f0c2023-10-11 19:49:56 +00002063function overrideflags() {
2064 local T="$(gettop)"
2065 (\cd "${T}" && build/make/tools/overrideflags.sh "$@")
2066}
2067
Colin Cross71319722023-10-24 10:58:46 -07002068function aninja() {
2069 local T="$(gettop)"
2070 (\cd "${T}" && prebuilts/build-tools/linux-x86/bin/ninja -f out/combined-${TARGET_PRODUCT}.ninja "$@")
2071}
2072
Jim Tanga881a252018-06-19 16:34:41 +08002073validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002074set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002075source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002076addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002077