blob: ab86d5ff9f4b001012d22a04a58a20eb2e8df17d [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:
Steven Moreland62054a42018-12-06 10:11:40 -080059- lunch: lunch <product_name>-<build_variant>
60 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).
Anton Hansson90ac61c2022-09-06 14:36:00 +000065- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64|arm64_only|x86_64only] \
66 [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 \
208 TARGET_BUILD_VARIANT= \
209 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700210 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800211 get_build_var TARGET_DEVICE > /dev/null
212 # hide successful answers, but allow the errors to show
213}
214
215VARIANT_CHOICES=(user userdebug eng)
216
217# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000218function check_variant()
219{
Christopher Ferris55257d22017-03-23 11:08:58 -0700220 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800221 for v in ${VARIANT_CHOICES[@]}
222 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 if [ "$v" = "$1" ]
224 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800225 return 0
226 fi
227 done
228 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700229}
230
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800231
232# Add directories to PATH that are dependent on the lunch target.
233# For directories that are not lunch-specific, add them in set_global_paths
234function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000235{
Christopher Ferris55257d22017-03-23 11:08:58 -0700236 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700237 if [ ! "$T" ]; then
238 echo "Couldn't locate the top of the tree. Try setting TOP."
239 return
240 fi
241
242 ##################################################################
243 # #
244 # Read me before you modify this code #
245 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800246 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
247 # adding to PATH, and the next time it is run, it removes that #
248 # from PATH. This is required so lunch can be run more than #
249 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700250 # #
251 ##################################################################
252
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800253 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700254 # due to "C:\Program Files" being in the path.
255
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800256 # Handle compat with the old ANDROID_BUILD_PATHS variable.
257 # TODO: Remove this after we think everyone has lunched again.
258 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
259 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
260 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700261 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000262 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700263 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800264 # strip leading ':', if any
265 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800266 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500267 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700268
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800269 # Out with the old...
270 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
271 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
272 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800273
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800274 # And in with the new...
275 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
276 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700277
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800278 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700279 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800280 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200281
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800282 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
283 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
284
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800285 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800286 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800287 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
288 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
289 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800290
Colin Crosse97e6932017-06-30 16:01:45 -0700291 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
292 export JAVA_HOME=$ANDROID_JAVA_HOME
293 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800294 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
295
296 # Fix up PYTHONPATH
297 if [ -n $ANDROID_PYTHONPATH ]; then
298 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
299 fi
300 export ANDROID_PYTHONPATH=$T/development/python-packages:
301 if [ -n $VENDOR_PYTHONPATH ]; then
302 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
303 fi
304 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500305
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800306 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700307 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
308 export OUT=$ANDROID_PRODUCT_OUT
309
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700310 unset ANDROID_HOST_OUT
311 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
312
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900313 unset ANDROID_SOONG_HOST_OUT
314 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
315
Simran Basidd050ed2017-02-13 13:46:48 -0800316 unset ANDROID_HOST_OUT_TESTCASES
317 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
318
319 unset ANDROID_TARGET_OUT_TESTCASES
320 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
321
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800322 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800323 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800324}
325
326# Add directories to PATH that are NOT dependent on the lunch target.
327# For directories that are lunch-specific, add them in set_lunch_paths
328function set_global_paths()
329{
330 local T=$(gettop)
331 if [ ! "$T" ]; then
332 echo "Couldn't locate the top of the tree. Try setting TOP."
333 return
334 fi
335
336 ##################################################################
337 # #
338 # Read me before you modify this code #
339 # #
340 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
341 # adding to PATH, and the next time it is run, it removes that #
342 # from PATH. This is required so envsetup.sh can be sourced #
343 # more than once and still have working paths. #
344 # #
345 ##################################################################
346
347 # Out with the old...
348 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
349 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
350 fi
351
352 # And in with the new...
353 ANDROID_GLOBAL_BUILD_PATHS=$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800354 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
355 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800356
357 # add kernel specific binaries
358 if [ $(uname -s) = Linux ] ; then
359 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
360 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
361 fi
362
363 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
364 # to ensure that the corresponding 'emulator' binaries are used.
365 case $(uname -s) in
366 Darwin)
367 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
368 ;;
369 Linux)
370 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
371 ;;
372 *)
373 ANDROID_EMULATOR_PREBUILTS=
374 ;;
375 esac
376 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
377 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
378 export ANDROID_EMULATOR_PREBUILTS
379 fi
380
381 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800382 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700383}
384
Matt Alexanderd9c56562020-05-21 10:49:17 +0000385function printconfig()
386{
Christopher Ferris55257d22017-03-23 11:08:58 -0700387 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800388 if [ ! "$T" ]; then
389 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
390 return
391 fi
392 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700393}
394
Matt Alexanderd9c56562020-05-21 10:49:17 +0000395function set_stuff_for_environment()
396{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800397 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800398 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700399
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800400 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700401}
402
Matt Alexanderd9c56562020-05-21 10:49:17 +0000403function set_sequence_number()
404{
Colin Cross88737132017-03-21 17:41:03 -0700405 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700406}
407
Makoto Onukida971062018-06-18 10:15:19 -0700408# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
409function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800410 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700411 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800412 *:"$cmd":*)
413 return 1
414 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700415 esac
416 return 0
417}
418
Matt Alexanderd9c56562020-05-21 10:49:17 +0000419function addcompletions()
420{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700421 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700422
Jim Tanga881a252018-06-19 16:34:41 +0800423 # Keep us from trying to run in something that's neither bash nor zsh.
424 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700425 return
426 fi
427
428 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800429 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700430 return
431 fi
432
Jim Tanga881a252018-06-19 16:34:41 +0800433 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000434 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800435 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800436 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400437 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800438 )
Makoto Onukida971062018-06-18 10:15:19 -0700439 # Completion can be disabled selectively to allow users to use non-standard completion.
440 # e.g.
441 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
442 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400443 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800444 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400445 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000446 if [ ! -f "$f" ]; then
447 echo "Warning: completion file $f not found"
448 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700449 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700450 fi
451 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700452
Matt Alexanderd9c56562020-05-21 10:49:17 +0000453 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700454 complete -C "bit --tab" bit
455 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000456 if [ -z "$ZSH_VERSION" ]; then
457 # Doesn't work in zsh.
458 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400459 # TODO(b/244559459): Support b autocompletion for zsh
460 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000461 fi
Jim Tanga881a252018-06-19 16:34:41 +0800462 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800463
Cole Faust24c36db2021-01-23 02:39:37 +0000464 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100465 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000466 complete -F _complete_android_module_names outmod
467 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000468 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100469 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700470}
471
Joe Onorato824608c2022-04-08 11:06:16 -0700472function multitree_lunch_help()
473{
474 echo "usage: lunch PRODUCT-VARIANT" 1>&2
475 echo " Set up android build environment based on a product short name and variant" 1>&2
476 echo 1>&2
477 echo "lunch COMBO_FILE VARIANT" 1>&2
478 echo " Set up android build environment based on a specific lunch combo file" 1>&2
479 echo " and variant." 1>&2
480 echo 1>&2
481 echo "lunch --print [CONFIG]" 1>&2
482 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
483 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
484 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
485 echo 1>&2
486 echo "lunch --list" 1>&2
487 echo " List all possible combo files available in the current tree" 1>&2
488 echo 1>&2
489 echo "lunch --help" 1>&2
490 echo "lunch -h" 1>&2
491 echo " Prints this message." 1>&2
492}
493
494function multitree_lunch()
495{
496 local code
497 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000498 # Lunch must be run in the topdir, but this way we get a clear error
499 # message, instead of FileNotFound.
500 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000501 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000502 _multitree_lunch_error
503 return 1
504 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700505 if $(echo "$1" | grep -q '^-') ; then
506 # Calls starting with a -- argument are passed directly and the function
507 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000508 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700509 code=$?
510 if [[ $code -eq 2 ]] ; then
511 echo 1>&2
512 multitree_lunch_help
513 return $code
514 elif [[ $code -ne 0 ]] ; then
515 return $code
516 fi
517 else
518 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000519 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700520 code=$?
521 if [[ $code -eq 2 ]] ; then
522 echo 1>&2
523 multitree_lunch_help
524 return $code
525 elif [[ $code -ne 0 ]] ; then
526 return $code
527 fi
528
529 export TARGET_BUILD_COMBO=${results[0]}
530 export TARGET_BUILD_VARIANT=${results[1]}
531 fi
532}
533
Matt Alexanderd9c56562020-05-21 10:49:17 +0000534function choosetype()
535{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700536 echo "Build type choices are:"
537 echo " 1. release"
538 echo " 2. debug"
539 echo
540
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800541 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700542 DEFAULT_NUM=1
543 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800545 export TARGET_BUILD_TYPE=
546 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700547 while [ -z $TARGET_BUILD_TYPE ]
548 do
549 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000550 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800551 read ANSWER
552 else
553 echo $1
554 ANSWER=$1
555 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556 case $ANSWER in
557 "")
558 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
559 ;;
560 1)
561 export TARGET_BUILD_TYPE=release
562 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800563 release)
564 export TARGET_BUILD_TYPE=release
565 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 2)
567 export TARGET_BUILD_TYPE=debug
568 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800569 debug)
570 export TARGET_BUILD_TYPE=debug
571 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700572 *)
573 echo
574 echo "I didn't understand your response. Please try again."
575 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576 ;;
577 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000578 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800579 break
580 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581 done
582
Ying Wang08800fd2016-03-03 20:57:21 -0800583 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800585 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700586}
587
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800588#
589# This function isn't really right: It chooses a TARGET_PRODUCT
590# based on the list of boards. Usually, that gets you something
591# that kinda works with a generic product, but really, you should
592# pick a product by name.
593#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000594function chooseproduct()
595{
Christopher Ferris55257d22017-03-23 11:08:58 -0700596 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000597 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 default_value=$TARGET_PRODUCT
599 else
Ying Wang0a76df52015-06-08 11:57:26 -0700600 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700601 fi
602
Ying Wang08800fd2016-03-03 20:57:21 -0800603 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800604 export TARGET_PRODUCT=
605 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700606 while [ -z "$TARGET_PRODUCT" ]
607 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700608 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000609 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800610 read ANSWER
611 else
612 echo $1
613 ANSWER=$1
614 fi
615
Matt Alexanderd9c56562020-05-21 10:49:17 +0000616 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700617 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800618 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000619 if check_product $ANSWER
620 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 export TARGET_PRODUCT=$ANSWER
622 else
623 echo "** Not a valid product: $ANSWER"
624 fi
625 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000626 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800627 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700628 fi
629 done
630
Ying Wang08800fd2016-03-03 20:57:21 -0800631 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700632 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800633 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700634}
635
Matt Alexanderd9c56562020-05-21 10:49:17 +0000636function choosevariant()
637{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800638 echo "Variant choices are:"
639 local index=1
640 local v
641 for v in ${VARIANT_CHOICES[@]}
642 do
643 # The product name is the name of the directory containing
644 # the makefile we found, above.
645 echo " $index. $v"
646 index=$(($index+1))
647 done
648
649 local default_value=eng
650 local ANSWER
651
652 export TARGET_BUILD_VARIANT=
653 while [ -z "$TARGET_BUILD_VARIANT" ]
654 do
655 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000656 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800657 read ANSWER
658 else
659 echo $1
660 ANSWER=$1
661 fi
662
Matt Alexanderd9c56562020-05-21 10:49:17 +0000663 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800664 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000665 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
666 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200667 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800668 fi
669 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000670 if check_variant $ANSWER
671 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800672 export TARGET_BUILD_VARIANT=$ANSWER
673 else
674 echo "** Not a valid variant: $ANSWER"
675 fi
676 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000677 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800678 break
679 fi
680 done
681}
682
Matt Alexanderd9c56562020-05-21 10:49:17 +0000683function choosecombo()
684{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700685 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700686
687 echo
688 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700689 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700690
691 echo
692 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700693 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800694
695 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800696 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700697 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800698 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800699 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700700}
701
Matt Alexanderd9c56562020-05-21 10:49:17 +0000702function add_lunch_combo()
703{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800704 if [ -n "$ZSH_VERSION" ]; then
705 echo -n "${funcfiletrace[1]}: "
706 else
707 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
708 fi
709 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 -0800710}
711
Matt Alexanderd9c56562020-05-21 10:49:17 +0000712function print_lunch_menu()
713{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700714 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100715 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700716 choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100717 local ret=$?
718
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700719 echo
720 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700721 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100722
Matt Alexanderd9c56562020-05-21 10:49:17 +0000723 if [ $ret -ne 0 ]
724 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100725 echo "Warning: Cannot display lunch menu."
726 echo
727 echo "Note: You can invoke lunch with an explicit target:"
728 echo
729 echo " usage: lunch [target]" >&2
730 echo
731 return
732 fi
733
Will Burr40401202022-02-07 12:12:01 +0000734 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800735
736 local i=1
737 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700738 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800739 do
740 echo " $i. $choice"
741 i=$(($i+1))
742 done
743
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700744 echo
745}
746
Matt Alexanderd9c56562020-05-21 10:49:17 +0000747function lunch()
748{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800749 local answer
750
Steven Moreland92793dc2020-02-25 18:30:18 -0800751 if [[ $# -gt 1 ]]; then
752 echo "usage: lunch [target]" >&2
753 return 1
754 fi
755
Will Burr40401202022-02-07 12:12:01 +0000756 local used_lunch_menu=0
757
Steven Moreland92793dc2020-02-25 18:30:18 -0800758 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800759 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700760 else
761 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000762 echo "Which would you like? [aosp_arm-eng]"
763 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 -0800764 read answer
Will Burr40401202022-02-07 12:12:01 +0000765 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700766 fi
767
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800768 local selection=
769
Matt Alexanderd9c56562020-05-21 10:49:17 +0000770 if [ -z "$answer" ]
771 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700772 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000773 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
774 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800775 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000776 if [ $answer -le ${#choices[@]} ]
777 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800778 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000779 if [ -n "$ZSH_VERSION" ]
780 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800781 selection=${choices[$(($answer))]}
782 else
783 selection=${choices[$(($answer-1))]}
784 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800785 fi
Colin Cross88737132017-03-21 17:41:03 -0700786 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800787 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 fi
789
Joe Onoratoda12daf2010-06-09 18:18:31 -0700790 export TARGET_BUILD_APPS=
791
Colin Cross88737132017-03-21 17:41:03 -0700792 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700793 product=${selection%%-*} # Trim everything after first dash
794 variant_and_version=${selection#*-} # Trim everything up to first dash
795 if [ "$variant_and_version" != "$selection" ]; then
796 variant=${variant_and_version%%-*}
797 if [ "$variant" != "$variant_and_version" ]; then
798 version=${variant_and_version#*-}
799 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700800 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800801
Matt Alexanderd9c56562020-05-21 10:49:17 +0000802 if [ -z "$product" ]
803 then
Ying Wang08800fd2016-03-03 20:57:21 -0800804 echo
Colin Cross88737132017-03-21 17:41:03 -0700805 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700806 return 1
807 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800808
Colin Cross88737132017-03-21 17:41:03 -0700809 TARGET_PRODUCT=$product \
810 TARGET_BUILD_VARIANT=$variant \
811 TARGET_PLATFORM_VERSION=$version \
812 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000813 if [ $? -ne 0 ]
814 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100815 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
816 then
817 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
818 fi
Colin Cross88737132017-03-21 17:41:03 -0700819 return 1
820 fi
Colin Cross88737132017-03-21 17:41:03 -0700821 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
822 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700823 if [ -n "$version" ]; then
824 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
825 else
826 unset TARGET_PLATFORM_VERSION
827 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700828 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700829
Will Burr40401202022-02-07 12:12:01 +0000830 if [ $used_lunch_menu -eq 1 ]; then
831 echo
832 echo "Hint: next time you can simply run 'lunch $selection'"
833 fi
834
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700835 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800836
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700837 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700838 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800839 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400840
841 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
842 check_mu_config
843 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700844}
845
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700846unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700847# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000848function _lunch()
849{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700850 local cur prev opts
851 COMPREPLY=()
852 cur="${COMP_WORDS[COMP_CWORD]}"
853 prev="${COMP_WORDS[COMP_CWORD-1]}"
854
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700855 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800856 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700857 fi
858
859 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700860 return 0
861}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700862
Joe Onoratoda12daf2010-06-09 18:18:31 -0700863# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700864# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000865function tapas()
866{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700867 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800868 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700869 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700870 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 -0800871 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
872 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)"
873
Joe Onoratoda12daf2010-06-09 18:18:31 -0700874
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700875 if [ "$showHelp" != "" ]; then
876 $(gettop)/build/make/tapasHelp.sh
877 return
878 fi
879
Ying Wang67f02922012-08-22 10:25:20 -0700880 if [ $(echo $arch | wc -w) -gt 1 ]; then
881 echo "tapas: Error: Multiple build archs supplied: $arch"
882 return
883 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700884 if [ $(echo $variant | wc -w) -gt 1 ]; then
885 echo "tapas: Error: Multiple build variants supplied: $variant"
886 return
887 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700888 if [ $(echo $density | wc -w) -gt 1 ]; then
889 echo "tapas: Error: Multiple densities supplied: $density"
890 return
891 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800892 if [ $(echo $keys | wc -w) -gt 1 ]; then
893 echo "tapas: Error: Multiple keys supplied: $keys"
894 return
895 fi
Ying Wang67f02922012-08-22 10:25:20 -0700896
Ying Wang0a76df52015-06-08 11:57:26 -0700897 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700898 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000899 x86) product=aosp_x86;;
900 arm64) product=aosp_arm64;;
901 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700902 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800903 if [ -n "$keys" ]; then
904 product=${product/aosp_/aosp_${keys}_}
905 fi;
906
Joe Onoratoda12daf2010-06-09 18:18:31 -0700907 if [ -z "$variant" ]; then
908 variant=eng
909 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700910 if [ -z "$apps" ]; then
911 apps=all
912 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600913 if [ -z "$density" ]; then
914 density=alldpi
915 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700916
Ying Wang67f02922012-08-22 10:25:20 -0700917 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700918 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700919 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700920 export TARGET_BUILD_TYPE=release
921 export TARGET_BUILD_APPS=$apps
922
Ying Wang08800fd2016-03-03 20:57:21 -0800923 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700924 set_stuff_for_environment
925 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800926 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700927}
928
Martin Stjernholmf692c752021-04-12 00:01:10 +0100929# Configures the build to build unbundled Android modules (APEXes).
930# Run banchan with one or more module names (from apex{} modules).
931function banchan()
932{
933 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000934 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64|arm64only|x86_64only)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100935 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100936 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|(.*_)?(arm|x86|arm64|x86_64))$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100937
938 if [ "$showHelp" != "" ]; then
939 $(gettop)/build/make/banchanHelp.sh
940 return
941 fi
942
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100943 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000944 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100945 elif [ $(echo $product | wc -w) -gt 1 ]; then
946 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100947 return
948 fi
949 if [ $(echo $variant | wc -w) -gt 1 ]; then
950 echo "banchan: Error: Multiple build variants supplied: $variant"
951 return
952 fi
953 if [ -z "$apps" ]; then
954 echo "banchan: Error: No modules supplied"
955 return
956 fi
957
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100958 case $product in
959 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100960 x86) product=module_x86;;
961 arm64) product=module_arm64;;
962 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000963 arm64only) product=module_arm64only;;
964 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100965 esac
966 if [ -z "$variant" ]; then
967 variant=eng
968 fi
969
970 export TARGET_PRODUCT=$product
971 export TARGET_BUILD_VARIANT=$variant
972 export TARGET_BUILD_DENSITY=alldpi
973 export TARGET_BUILD_TYPE=release
974
975 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
976 # case is different and it may diverge in the future.
977 export TARGET_BUILD_APPS=$apps
978
979 build_build_var_cache
980 set_stuff_for_environment
981 printconfig
982 destroy_build_var_cache
983}
984
Joe Onorato7cf6f972022-05-11 21:39:57 -0700985# TODO: Merge into gettop as part of launching multitree
986function multitree_gettop
987{
Spandan Dasca762052022-09-21 00:08:34 +0000988 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700989 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
990 # The following circumlocution ensures we remove symlinks from TOP.
991 (cd "$TOP"; PWD= /bin/pwd)
992 else
993 if [ -f $TOPFILE ] ; then
994 # The following circumlocution (repeated below as well) ensures
995 # that we record the true directory name and not one that is
996 # faked up with symlink names.
997 PWD= /bin/pwd
998 else
999 local HERE=$PWD
1000 local T=
1001 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1002 \cd ..
1003 T=`PWD= /bin/pwd -P`
1004 done
1005 \cd "$HERE"
1006 if [ -f "$T/$TOPFILE" ]; then
1007 echo "$T"
1008 fi
1009 fi
1010 fi
1011}
1012
Matt Alexanderd9c56562020-05-21 10:49:17 +00001013function croot()
1014{
Christopher Ferris55257d22017-03-23 11:08:58 -07001015 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001016 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001017 if [ "$1" ]; then
1018 \cd $(gettop)/$1
1019 else
1020 \cd $(gettop)
1021 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001022 else
1023 echo "Couldn't locate the top of the tree. Try setting TOP."
1024 fi
1025}
1026
Matt Alexanderd9c56562020-05-21 10:49:17 +00001027function _croot()
1028{
Anton Hanssonece9c482019-02-04 18:15:39 +00001029 local T=$(gettop)
1030 if [ "$T" ]; then
1031 local cur="${COMP_WORDS[COMP_CWORD]}"
1032 k=0
1033 for c in $(compgen -d ${T}/${cur}); do
1034 COMPREPLY[k++]=${c#${T}/}/
1035 done
1036 fi
1037}
1038
Matt Alexanderd9c56562020-05-21 10:49:17 +00001039function cproj()
1040{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001041 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001042 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001043 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001044 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1045 T=$PWD
1046 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001047 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001048 return
1049 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001050 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001051 done
Ying Wang9cd17642012-12-13 10:52:07 -08001052 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001053 echo "can't find Android.mk"
1054}
1055
Daniel Sandler47e0a882013-07-30 13:23:52 -04001056# simplified version of ps; output in the form
1057# <pid> <procname>
1058function qpid() {
1059 local prepend=''
1060 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001062 prepend=' '
1063 append='$'
1064 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001065 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001066 echo "usage: qpid [[--exact] <process name|pid>"
1067 return 255
1068 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001069
1070 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001071 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001072 qpid | \grep "$prepend$EXE$append"
1073 else
1074 adb shell ps \
1075 | tr -d '\r' \
1076 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1077 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001078}
1079
Steven Moreland74114f12020-09-10 01:23:32 +00001080# syswrite - disable verity, reboot if needed, and remount image
1081#
1082# Easy way to make system.img/etc writable
1083function syswrite() {
1084 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001085 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001086 echo "rebooting"
1087 adb reboot && adb wait-for-device && adb root || return 1
1088 fi
1089 adb wait-for-device && adb remount || return 1
1090}
1091
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001092# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001093# that has the core-file-size limit set correctly
1094#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001095# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001096# if its core-file-size limit is not set already.
1097# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1098
Matt Alexanderd9c56562020-05-21 10:49:17 +00001099function coredump_setup()
1100{
1101 echo "Getting root...";
1102 adb root;
1103 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001104
Matt Alexanderd9c56562020-05-21 10:49:17 +00001105 echo "Remounting root partition read-write...";
1106 adb shell mount -w -o remount -t rootfs rootfs;
1107 sleep 1;
1108 adb wait-for-device;
1109 adb shell mkdir -p /cores;
1110 adb shell mount -t tmpfs tmpfs /cores;
1111 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001112
Matt Alexanderd9c56562020-05-21 10:49:17 +00001113 echo "Granting SELinux permission to dump in /cores...";
1114 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001115
Matt Alexanderd9c56562020-05-21 10:49:17 +00001116 echo "Set core pattern.";
1117 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001118
Ying Wang08800fd2016-03-03 20:57:21 -08001119 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001120}
1121
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001122# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001125# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126# dump to actually be generated.
1127
Matt Alexanderd9c56562020-05-21 10:49:17 +00001128function coredump_enable()
1129{
1130 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001131 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001132 printf "Expecting a PID!\n";
1133 return;
1134 fi;
1135 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001136 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001137}
1138
1139# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001141#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001142# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001143# enabled globally.
1144
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145function core()
1146{
1147 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001148
Ying Wang08800fd2016-03-03 20:57:21 -08001149 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001150 printf "Expecting a PID!\n";
1151 return;
1152 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001153
Matt Alexanderd9c56562020-05-21 10:49:17 +00001154 local CORENAME=core.$PID;
1155 local COREPATH=/cores/$CORENAME;
1156 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001157
Matt Alexanderd9c56562020-05-21 10:49:17 +00001158 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001159
Matt Alexanderd9c56562020-05-21 10:49:17 +00001160 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001161 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1163 adb shell kill -$SIG $PID;
1164 sleep 1;
1165 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001166
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1168 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001169}
1170
Christopher Tate744ee802009-11-12 15:33:08 -08001171# systemstack - dump the current stack trace of all threads in the system process
1172# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001173function systemstack()
1174{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001175 stacks system_server
1176}
1177
Michael Wrightaeed7212014-06-19 19:58:12 -07001178# Read the ELF header from /proc/$PID/exe to determine if the process is
1179# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001180function is64bit()
1181{
Ben Chengfba67bf2014-02-25 10:27:07 -08001182 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001183 if [ "$PID" ] ; then
1184 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001185 echo "64"
1186 else
1187 echo ""
1188 fi
1189 else
1190 echo ""
1191 fi
1192}
1193
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001194case `uname -s` in
1195 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196 function sgrep()
1197 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001198 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)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001199 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001201
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001202 ;;
1203 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001204 function sgrep()
1205 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001206 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\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001207 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208 }
1209 ;;
1210esac
1211
Matt Alexanderd9c56562020-05-21 10:49:17 +00001212function gettargetarch
1213{
Raghu Gandham8da43102012-07-25 19:57:22 -07001214 get_build_var TARGET_ARCH
1215}
1216
Matt Alexanderd9c56562020-05-21 10:49:17 +00001217function ggrep()
1218{
Mike Frysinger5e479732015-09-22 18:13:48 -04001219 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1220 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001221}
1222
Matt Alexanderd9c56562020-05-21 10:49:17 +00001223function gogrep()
1224{
Orion Hodson831472d2019-10-25 11:35:15 +01001225 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1226 -exec grep --color -n "$@" {} +
1227}
1228
Matt Alexanderd9c56562020-05-21 10:49:17 +00001229function jgrep()
1230{
Mike Frysinger5e479732015-09-22 18:13:48 -04001231 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1232 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001233}
1234
Alistair Delva176e5342021-02-22 13:31:26 -08001235function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001236{
1237 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1238 -exec grep --color -n "$@" {} +
1239}
1240
Jeff Vander Stoepf5f51462023-01-10 14:09:45 +01001241function jsongrep()
1242{
1243 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.json" \
1244 -exec grep --color -n "$@" {} +
1245}
1246
1247function tomlgrep()
1248{
1249 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.toml" \
1250 -exec grep --color -n "$@" {} +
1251}
1252
Taesu Leeea0cecd2020-10-28 11:05:18 +09001253function ktgrep()
1254{
1255 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1256 -exec grep --color -n "$@" {} +
1257}
1258
Matt Alexanderd9c56562020-05-21 10:49:17 +00001259function cgrep()
1260{
Mike Frysinger5e479732015-09-22 18:13:48 -04001261 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' \) \
1262 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001263}
1264
Matt Alexanderd9c56562020-05-21 10:49:17 +00001265function resgrep()
1266{
Christopher Ferris55257d22017-03-23 11:08:58 -07001267 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001268 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1269 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1270 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001271}
1272
Matt Alexanderd9c56562020-05-21 10:49:17 +00001273function mangrep()
1274{
Mike Frysinger5e479732015-09-22 18:13:48 -04001275 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1276 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001277}
1278
Matt Alexanderd9c56562020-05-21 10:49:17 +00001279function owngrep()
1280{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001281 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1282 -exec grep --color -n "$@" {} +
1283}
1284
Matt Alexanderd9c56562020-05-21 10:49:17 +00001285function sepgrep()
1286{
Mike Frysinger5e479732015-09-22 18:13:48 -04001287 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1288 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001289}
1290
Matt Alexanderd9c56562020-05-21 10:49:17 +00001291function rcgrep()
1292{
Mike Frysinger5e479732015-09-22 18:13:48 -04001293 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1294 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001295}
1296
DroidFreak32a2781982020-11-26 11:30:19 -05001297function pygrep()
1298{
1299 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1300 -exec grep --color -n "$@" {} +
1301}
1302
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001303case `uname -s` in
1304 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001305 function mgrep()
1306 {
Orion Hodson831472d2019-10-25 11:35:15 +01001307 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 -04001308 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001309 }
1310
Matt Alexanderd9c56562020-05-21 10:49:17 +00001311 function treegrep()
1312 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001313 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 -04001314 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001315 }
1316
1317 ;;
1318 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001319 function mgrep()
1320 {
Orion Hodson831472d2019-10-25 11:35:15 +01001321 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 -04001322 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001323 }
1324
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325 function treegrep()
1326 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001327 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 -04001328 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001329 }
1330
1331 ;;
1332esac
1333
Matt Alexanderd9c56562020-05-21 10:49:17 +00001334function getprebuilt
1335{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001336 get_abs_build_var ANDROID_PREBUILTS
1337}
1338
Matt Alexanderd9c56562020-05-21 10:49:17 +00001339function tracedmdump()
1340{
Christopher Ferris55257d22017-03-23 11:08:58 -07001341 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001342 if [ ! "$T" ]; then
1343 echo "Couldn't locate the top of the tree. Try setting TOP."
1344 return
1345 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001346 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001347 local arch=$(gettargetarch)
1348 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001349
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001350 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001351 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001352 echo "usage: tracedmdump tracename"
1353 return
1354 fi
1355
Matt Alexanderd9c56562020-05-21 10:49:17 +00001356 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001357 echo "Error: cannot find kernel: '$KERNEL'"
1358 return
1359 fi
1360
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001361 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001362 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001363 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1364 fi
1365
1366 echo "post-processing traces..."
1367 rm -f $TRACE/qtrace.dexlist
1368 post_trace $TRACE
1369 if [ $? -ne 0 ]; then
1370 echo "***"
1371 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1372 echo "***"
1373 return
1374 fi
1375 echo "generating dexlist output..."
1376 /bin/ls $ANDROID_PRODUCT_OUT/system/framework/*.jar $ANDROID_PRODUCT_OUT/system/app/*.apk $ANDROID_PRODUCT_OUT/data/app/*.apk 2>/dev/null | xargs dexlist > $TRACE/qtrace.dexlist
1377 echo "generating dmtrace data..."
1378 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1379 echo "generating html file..."
1380 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1381 echo "done, see $TRACE/dmtrace.html for details"
1382 echo "or run:"
1383 echo " traceview $TRACE/dmtrace"
1384}
1385
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001386# communicate with a running device or emulator, set up necessary state,
1387# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001388function runhat()
1389{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001390 # process standard adb options
1391 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001392 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001393 adbTarget=$1
1394 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001395 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001396 adbTarget="$1 $2"
1397 shift 2
1398 fi
1399 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001400 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001401
1402 # runhat options
1403 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001404
Matt Alexanderd9c56562020-05-21 10:49:17 +00001405 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001406 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001407 return
1408 fi
1409
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001410 # confirm hat is available
1411 if [ -z $(which hat) ]; then
1412 echo "hat is not available in this configuration."
1413 return
1414 fi
1415
Andy McFaddenb6289852010-07-12 08:00:19 -07001416 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001417 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001418 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001419 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001420 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001421 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001422 echo -n "> "
1423 read
1424
The Android Open Source Project88b60792009-03-03 19:28:42 -08001425 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001426
The Android Open Source Project88b60792009-03-03 19:28:42 -08001427 echo "Retrieving file $devFile..."
1428 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001429
The Android Open Source Project88b60792009-03-03 19:28:42 -08001430 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001431
The Android Open Source Project88b60792009-03-03 19:28:42 -08001432 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001433 echo "View the output by pointing your browser at http://localhost:7000/"
1434 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001435 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001436}
1437
Matt Alexanderd9c56562020-05-21 10:49:17 +00001438function getbugreports()
1439{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001440 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001441
1442 if [ ! "$reports" ]; then
1443 echo "Could not locate any bugreports."
1444 return
1445 fi
1446
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001447 local report
1448 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001449 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001450 echo "/sdcard/bugreports/${report}"
1451 adb pull /sdcard/bugreports/${report} ${report}
1452 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001453 done
1454}
1455
Matt Alexanderd9c56562020-05-21 10:49:17 +00001456function getsdcardpath()
1457{
Victoria Lease1b296b42012-08-21 15:44:06 -07001458 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1459}
1460
Matt Alexanderd9c56562020-05-21 10:49:17 +00001461function getscreenshotpath()
1462{
Victoria Lease1b296b42012-08-21 15:44:06 -07001463 echo "$(getsdcardpath)/Pictures/Screenshots"
1464}
1465
Matt Alexanderd9c56562020-05-21 10:49:17 +00001466function getlastscreenshot()
1467{
Victoria Lease1b296b42012-08-21 15:44:06 -07001468 local screenshot_path=$(getscreenshotpath)
1469 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 +00001470 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001471 echo "No screenshots found."
1472 return
1473 fi
1474 echo "${screenshot}"
1475 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1476}
1477
Matt Alexanderd9c56562020-05-21 10:49:17 +00001478function startviewserver()
1479{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001480 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001481 if [ $# -gt 0 ]; then
1482 port=$1
1483 fi
1484 adb shell service call window 1 i32 $port
1485}
1486
Matt Alexanderd9c56562020-05-21 10:49:17 +00001487function stopviewserver()
1488{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001489 adb shell service call window 2
1490}
1491
Matt Alexanderd9c56562020-05-21 10:49:17 +00001492function isviewserverstarted()
1493{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001494 adb shell service call window 3
1495}
1496
Matt Alexanderd9c56562020-05-21 10:49:17 +00001497function key_home()
1498{
Romain Guyb84049a2010-10-04 16:56:11 -07001499 adb shell input keyevent 3
1500}
1501
Matt Alexanderd9c56562020-05-21 10:49:17 +00001502function key_back()
1503{
Romain Guyb84049a2010-10-04 16:56:11 -07001504 adb shell input keyevent 4
1505}
1506
Matt Alexanderd9c56562020-05-21 10:49:17 +00001507function key_menu()
1508{
Romain Guyb84049a2010-10-04 16:56:11 -07001509 adb shell input keyevent 82
1510}
1511
Matt Alexanderd9c56562020-05-21 10:49:17 +00001512function smoketest()
1513{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001514 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1515 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1516 return
1517 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001518 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001519 if [ ! "$T" ]; then
1520 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1521 return
1522 fi
1523
Ying Wang9cd17642012-12-13 10:52:07 -08001524 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001525 adb uninstall com.android.smoketest > /dev/null &&
1526 adb uninstall com.android.smoketest.tests > /dev/null &&
1527 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1528 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1529 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1530}
1531
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001532# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001533function runtest()
1534{
Christopher Ferris55257d22017-03-23 11:08:58 -07001535 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001536 if [ ! "$T" ]; then
1537 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1538 return
1539 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001540 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001541}
1542
The Android Open Source Project88b60792009-03-03 19:28:42 -08001543function godir () {
1544 if [[ -z "$1" ]]; then
1545 echo "Usage: godir <regex>"
1546 return
1547 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001548 local T=$(gettop)
1549 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001550 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001551 mkdir -p $OUT_DIR
1552 FILELIST=$OUT_DIR/filelist
1553 else
1554 FILELIST=$T/filelist
1555 fi
1556 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001557 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001558 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001559 echo " Done"
1560 echo ""
1561 fi
1562 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001563 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001564 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001565 echo "Not found"
1566 return
1567 fi
1568 local pathname
1569 local choice
1570 if [[ ${#lines[@]} > 1 ]]; then
1571 while [[ -z "$pathname" ]]; do
1572 local index=1
1573 local line
1574 for line in ${lines[@]}; do
1575 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001576 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001577 done
1578 echo
1579 echo -n "Select one: "
1580 unset choice
1581 read choice
1582 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1583 echo "Invalid choice"
1584 continue
1585 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001586 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001587 done
1588 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001589 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001590 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001591 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001592}
1593
Steven Moreland62054a42018-12-06 10:11:40 -08001594# Update module-info.json in out.
1595function refreshmod() {
1596 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1597 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1598 return 1
1599 fi
1600
1601 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1602
1603 # for the output of the next command
1604 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1605
1606 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001607 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001608 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1609}
1610
Cole Faust3e192382021-10-25 13:29:15 -07001611# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001612function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001613 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001614 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1615 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1616 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001617 return 1
1618 fi
1619
1620 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001621 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001622 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001623 fi
1624 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001625 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001626}
1627
Cole Faust5d825b72022-10-26 18:16:44 -07001628# List all modules for the current device, as cached in all_modules.txt. If any build change is
1629# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001630function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001631 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001632}
1633
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001634# Return the Bazel label of a Soong module if it is converted with bp2build.
1635function bmod()
1636(
1637 if [ $# -ne 1 ]; then
1638 echo "usage: bmod <module>" >&2
1639 return 1
1640 fi
1641
1642 # We could run bp2build here, but it might trigger bp2build invalidation
1643 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1644 # time overhead.
1645 #
1646 # For a snappy result, use the latest generated version in soong_injection,
1647 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001648 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001649
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001650 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001651 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1652 return 1
1653 fi
1654
1655 local target_label=$(python3 -c "import json
1656module = '$1'
1657converted_json='$converted_json'
1658bp2build_converted_map = json.load(open(converted_json))
1659if module not in bp2build_converted_map:
1660 exit(1)
1661print(bp2build_converted_map[module] + ':' + module)")
1662
1663 if [ -z "${target_label}" ]; then
1664 echo "$1 is not converted to Bazel." >&2
1665 return 1
1666 else
1667 echo "${target_label}"
1668 fi
1669)
1670
Joe Onorato2c1aa472021-02-25 16:42:39 -08001671# Get the path of a specific module in the android tree, as cached in module-info.json.
1672# If any build change is made, and it should be reflected in the output, you should run
1673# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001674function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001675 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001676 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001677 return 1
1678 fi
1679
Cole Faust24c36db2021-01-23 02:39:37 +00001680 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001681
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001682 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001683module = '$1'
1684module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1685if module not in module_info:
1686 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001687print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001688
1689 if [ -z "$relpath" ]; then
1690 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1691 return 1
1692 else
Rett Berg78d1c932019-01-24 14:34:23 -08001693 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001694 fi
1695}
1696
Joe Onorato2c1aa472021-02-25 16:42:39 -08001697# Get the path of a specific module in the android tree, as cached in module-info.json.
1698# If any build change is made, and it should be reflected in the output, you should run
1699# 'refreshmod' first. Note: This is the inverse of pathmod.
1700function dirmods() {
1701 if [[ $# -ne 1 ]]; then
1702 echo "usage: dirmods <path>" >&2
1703 return 1
1704 fi
1705
1706 verifymodinfo || return 1
1707
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001708 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001709dir = '$1'
1710while dir.endswith('/'):
1711 dir = dir[:-1]
1712prefix = dir + '/'
1713module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1714results = set()
1715for m in module_info.values():
1716 for path in m.get(u'path', []):
1717 if path == dir or path.startswith(prefix):
1718 name = m.get(u'module_name')
1719 if name:
1720 results.add(name)
1721for name in sorted(results):
1722 print(name)
1723"
1724}
1725
1726
Rett Berg78d1c932019-01-24 14:34:23 -08001727# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1728# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1729function gomod() {
1730 if [[ $# -ne 1 ]]; then
1731 echo "usage: gomod <module>" >&2
1732 return 1
1733 fi
1734
1735 local path="$(pathmod $@)"
1736 if [ -z "$path" ]; then
1737 return 1
1738 fi
1739 cd $path
1740}
1741
Cole Faust24c36db2021-01-23 02:39:37 +00001742# Gets the list of a module's installed outputs, as cached in module-info.json.
1743# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1744function outmod() {
1745 if [[ $# -ne 1 ]]; then
1746 echo "usage: outmod <module>" >&2
1747 return 1
1748 fi
1749
1750 verifymodinfo || return 1
1751
1752 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001753 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001754module = '$1'
1755module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1756if module not in module_info:
1757 exit(1)
1758for output in module_info[module]['installed']:
1759 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1760
1761 if [ $? -ne 0 ]; then
1762 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1763 return 1
1764 elif [ ! -z "$relpath" ]; then
1765 echo "$relpath"
1766 fi
1767}
1768
1769# adb install a module's apk, as cached in module-info.json. If any build change
1770# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1771# Usage: installmod [adb install arguments] <module>
1772# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1773function installmod() {
1774 if [[ $# -eq 0 ]]; then
1775 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001776 echo "" >&2
1777 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1778 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1779 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001780 return 1
1781 fi
1782
1783 local _path
1784 _path=$(outmod ${@:$#:1})
1785 if [ $? -ne 0 ]; then
1786 return 1
1787 fi
1788
1789 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1790 if [ -z "$_path" ]; then
1791 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1792 return 1
1793 fi
Cole Faust3e192382021-10-25 13:29:15 -07001794 local serial_device=""
1795 if [[ "$1" == "-s" ]]; then
1796 if [[ $# -le 2 ]]; then
1797 echo "-s requires an argument" >&2
1798 return 1
1799 fi
1800 serial_device="-s $2"
1801 shift 2
1802 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001803 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001804 echo adb $serial_device install ${@:1:$length} $_path
1805 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001806}
1807
dimitry73b84812018-12-11 18:06:00 +01001808function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001809 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001810 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001811}
1812
Alex Rayf0d08eb2013-03-08 15:15:06 -08001813# Print colored exit condition
1814function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001815 "$@"
1816 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001817 if [ $retval -ne 0 ]
1818 then
Jacky Cao89483b82015-05-15 22:12:53 +08001819 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001820 else
Jacky Cao89483b82015-05-15 22:12:53 +08001821 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001822 fi
1823 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001824}
1825
Matt Alexanderd9c56562020-05-21 10:49:17 +00001826function get_make_command()
1827{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001828 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1829 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001830 # Always use the real make if -C is passed in
1831 for arg in "$@"; do
1832 if [[ $arg == -C* ]]; then
1833 echo command make
1834 return
1835 fi
1836 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001837 echo build/soong/soong_ui.bash --make-mode
1838 else
1839 echo command make
1840 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001841}
1842
Matt Alexanderd9c56562020-05-21 10:49:17 +00001843function _wrap_build()
1844{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001845 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1846 "$@"
1847 return $?
1848 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001849 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001850 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001851 local ret=$?
1852 local end_time=$(date +"%s")
1853 local tdiff=$(($end_time-$start_time))
1854 local hours=$(($tdiff / 3600 ))
1855 local mins=$((($tdiff % 3600) / 60))
1856 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001857 local ncolors=$(tput colors 2>/dev/null)
1858 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001859 color_failed=$'\E'"[0;31m"
1860 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001861 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001862 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001863 else
1864 color_failed=""
1865 color_success=""
1866 color_reset=""
1867 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001868
1869 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1870 echo
1871 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1872 fi
1873
Ed Heylcc6be0a2014-06-18 14:55:58 -07001874 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001875 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001876 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001877 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001878 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001879 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001880 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001881 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001882 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001883 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001884 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001885 printf "(%s seconds)" $secs
1886 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001887 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001888 echo
1889 return $ret
1890}
1891
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001892function _trigger_build()
1893(
1894 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001895 local T=$(gettop)
1896 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001897 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1898 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001899 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1900 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001901 fi
1902)
1903
1904function m()
1905(
1906 _trigger_build "all-modules" "$@"
1907)
1908
1909function mm()
1910(
1911 _trigger_build "modules-in-a-dir-no-deps" "$@"
1912)
1913
1914function mmm()
1915(
1916 _trigger_build "modules-in-dirs-no-deps" "$@"
1917)
1918
1919function mma()
1920(
1921 _trigger_build "modules-in-a-dir" "$@"
1922)
1923
1924function mmma()
1925(
1926 _trigger_build "modules-in-dirs" "$@"
1927)
1928
Matt Alexanderd9c56562020-05-21 10:49:17 +00001929function make()
1930{
Dan Willemsene9842242017-07-28 13:00:13 -07001931 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001932}
1933
Joe Onorato7cf6f972022-05-11 21:39:57 -07001934function _multitree_lunch_error()
1935{
1936 >&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."
1937}
1938
1939function multitree_build()
1940{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001941 local T=$(multitree_gettop)
1942 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001943 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001944 else
1945 _multitree_lunch_error
1946 return 1
1947 fi
1948}
1949
Matt Alexanderd9c56562020-05-21 10:49:17 +00001950function provision()
1951{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001952 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1953 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1954 return 1
1955 fi
1956 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1957 echo "There is no provisioning script for the device." >&2
1958 return 1
1959 fi
1960
1961 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001962 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001963 shift 1
1964 else
1965 echo "This action will reflash your device."
1966 echo ""
1967 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1968 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001969 echo -n "Are you sure you want to do this (yes/no)? "
1970 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001971 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001972 echo "Not taking any action. Exiting." >&2
1973 return 1
1974 fi
1975 fi
1976 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1977}
1978
Jim Tanga881a252018-06-19 16:34:41 +08001979# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001980function enable_zsh_completion() {
1981 # Don't override user's options if bash-style completion is already enabled.
1982 if ! declare -f complete >/dev/null; then
1983 autoload -U compinit && compinit
1984 autoload -U bashcompinit && bashcompinit
1985 fi
Jim Tanga881a252018-06-19 16:34:41 +08001986}
1987
1988function validate_current_shell() {
1989 local current_sh="$(ps -o command -p $$)"
1990 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001991 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001992 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001993 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001994 *zsh*)
1995 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001996 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001997 *)
Jim Tanga881a252018-06-19 16:34:41 +08001998 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 -07001999 ;;
2000 esac
Jim Tanga881a252018-06-19 16:34:41 +08002001}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002002
2003# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08002004# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
2005# load those.
2006#
2007# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002008function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002009 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002010 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002011 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002012 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 -08002013 if [ -n "$allowed" ]; then
2014 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2015 echo " $allowed"
2016 echo " $f"
2017 return
2018 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002019 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002020 done
2021
2022 allowed_files=
2023 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002024 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002025 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002026 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002027
2028 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002029 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002030 else
2031 echo "ignoring $f, not in $allowed"
2032 fi
Jim Tanga881a252018-06-19 16:34:41 +08002033 done
2034 done
2035}
Kenny Root52aa81c2011-07-15 11:07:06 -07002036
Dan Albertbab814f2020-08-26 15:34:53 -07002037function showcommands() {
2038 local T=$(gettop)
2039 if [[ -z "$TARGET_PRODUCT" ]]; then
2040 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2041 return
2042 fi
2043 case $(uname -s) in
2044 Darwin)
2045 PREBUILT_NAME=darwin-x86
2046 ;;
2047 Linux)
2048 PREBUILT_NAME=linux-x86
2049 ;;
2050 *)
2051 >&2 echo Unknown host $(uname -s)
2052 return
2053 ;;
2054 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002055 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002056 if [[ "$1" == "--regenerate" ]]; then
2057 shift 1
2058 NINJA_ARGS="-t commands $@" m
2059 else
2060 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2061 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2062 -t commands "$@")
2063 fi
2064}
2065
Cole Faust45844ab2022-08-30 13:59:07 -07002066function avbtool() {
2067 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2068 m avbtool
2069 fi
2070 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
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