blob: a30f6bd278c7a0fded3db8ac7d2af75b5ca91290 [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.
Taesu Leeea0cecd2020-10-28 11:05:18 +090082- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080083- resgrep: Greps on all local res/*.xml files.
84- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070085- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060086- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080087- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080088- sepgrep: Greps on all local sepolicy files.
89- sgrep: Greps on all local source files.
DroidFreak32a2781982020-11-26 11:30:19 -050090- pygrep: Greps on all local Python files.
Steven Moreland62054a42018-12-06 10:11:40 -080091- godir: Go to the directory containing a file.
92- allmod: List all modules.
93- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000094- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080095- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000096- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080097- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000098- installmod: Adb installs a module's built APK.
99- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +0000100- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700101
Roland Levillain39341922015-10-20 12:48:19 +0100102Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -0700103- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -0800104- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700105
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700106Look at the source to view more functions. The complete list is:
107EOF
Christopher Ferris55257d22017-03-23 11:08:58 -0700108 local T=$(gettop)
109 local A=""
110 local i
Jacky Cao89483b82015-05-15 22:12:53 +0800111 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 -0700112 A="$A $i"
113 done
114 echo $A
115}
116
Ying Wang08800fd2016-03-03 20:57:21 -0800117# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000118function build_build_var_cache()
119{
Christopher Ferris55257d22017-03-23 11:08:58 -0700120 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -0800121 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +0800122 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' ' '`)
123 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 -0800124 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -0800125 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +0800126 --vars="${cached_vars[*]}" \
127 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -0700128 --var-prefix=var_cache_ \
129 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -0800130 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +0000131 if [ $ret -ne 0 ]
132 then
Ying Wang08800fd2016-03-03 20:57:21 -0800133 unset build_dicts_script
134 return $ret
135 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700136 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -0800137 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -0800138 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -0800139 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +0000140 if [ $ret -ne 0 ]
141 then
Ying Wang08800fd2016-03-03 20:57:21 -0800142 return $ret
143 fi
144 BUILD_VAR_CACHE_READY="true"
145}
146
Ying Wangf0cb3972016-03-04 13:56:23 -0800147# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -0800148# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000149function destroy_build_var_cache()
150{
Ying Wang08800fd2016-03-03 20:57:21 -0800151 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -0700152 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800153 for v in $cached_vars; do
154 unset var_cache_$v
155 done
156 unset cached_vars
157 for v in $cached_abs_vars; do
158 unset abs_var_cache_$v
159 done
160 unset cached_abs_vars
161}
162
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700163# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000164function get_abs_build_var()
165{
166 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
167 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800168 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200169 return
Ying Wang08800fd2016-03-03 20:57:21 -0800170 fi
171
Christopher Ferris55257d22017-03-23 11:08:58 -0700172 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700173 if [ ! "$T" ]; then
174 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
175 return
176 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700177 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700178}
179
180# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000181function get_build_var()
182{
183 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
184 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800185 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100186 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800187 fi
188
Christopher Ferris55257d22017-03-23 11:08:58 -0700189 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700190 if [ ! "$T" ]; then
191 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100192 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700193 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700194 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800195}
196
197# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000198function check_product()
199{
Christopher Ferris55257d22017-03-23 11:08:58 -0700200 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800201 if [ ! "$T" ]; then
202 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
203 return
204 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700205 TARGET_PRODUCT=$1 \
206 TARGET_BUILD_VARIANT= \
207 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700208 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800209 get_build_var TARGET_DEVICE > /dev/null
210 # hide successful answers, but allow the errors to show
211}
212
213VARIANT_CHOICES=(user userdebug eng)
214
215# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000216function check_variant()
217{
Christopher Ferris55257d22017-03-23 11:08:58 -0700218 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800219 for v in ${VARIANT_CHOICES[@]}
220 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 if [ "$v" = "$1" ]
222 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800223 return 0
224 fi
225 done
226 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700227}
228
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800229
230# Add directories to PATH that are dependent on the lunch target.
231# For directories that are not lunch-specific, add them in set_global_paths
232function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000233{
Christopher Ferris55257d22017-03-23 11:08:58 -0700234 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700235 if [ ! "$T" ]; then
236 echo "Couldn't locate the top of the tree. Try setting TOP."
237 return
238 fi
239
240 ##################################################################
241 # #
242 # Read me before you modify this code #
243 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800244 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
245 # adding to PATH, and the next time it is run, it removes that #
246 # from PATH. This is required so lunch can be run more than #
247 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700248 # #
249 ##################################################################
250
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800251 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700252 # due to "C:\Program Files" being in the path.
253
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800254 # Handle compat with the old ANDROID_BUILD_PATHS variable.
255 # TODO: Remove this after we think everyone has lunched again.
256 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
257 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
258 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700259 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000260 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700261 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800262 # strip leading ':', if any
263 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800264 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500265 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700266
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800267 # Out with the old...
268 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
269 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
270 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800271
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800272 # And in with the new...
273 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
274 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700275
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800276 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700277 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800278 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200279
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800280 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
281 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
282
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800283 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800284 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800285 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
286 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
287 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800288
Colin Crosse97e6932017-06-30 16:01:45 -0700289 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
290 export JAVA_HOME=$ANDROID_JAVA_HOME
291 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800292 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
293
294 # Fix up PYTHONPATH
295 if [ -n $ANDROID_PYTHONPATH ]; then
296 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
297 fi
298 export ANDROID_PYTHONPATH=$T/development/python-packages:
299 if [ -n $VENDOR_PYTHONPATH ]; then
300 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
301 fi
302 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500303
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800304 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700305 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
306 export OUT=$ANDROID_PRODUCT_OUT
307
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700308 unset ANDROID_HOST_OUT
309 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
310
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900311 unset ANDROID_SOONG_HOST_OUT
312 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
313
Simran Basidd050ed2017-02-13 13:46:48 -0800314 unset ANDROID_HOST_OUT_TESTCASES
315 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
316
317 unset ANDROID_TARGET_OUT_TESTCASES
318 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
319
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800320 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800321 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800322}
323
324# Add directories to PATH that are NOT dependent on the lunch target.
325# For directories that are lunch-specific, add them in set_lunch_paths
326function set_global_paths()
327{
328 local T=$(gettop)
329 if [ ! "$T" ]; then
330 echo "Couldn't locate the top of the tree. Try setting TOP."
331 return
332 fi
333
334 ##################################################################
335 # #
336 # Read me before you modify this code #
337 # #
338 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
339 # adding to PATH, and the next time it is run, it removes that #
340 # from PATH. This is required so envsetup.sh can be sourced #
341 # more than once and still have working paths. #
342 # #
343 ##################################################################
344
345 # Out with the old...
346 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
347 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
348 fi
349
350 # And in with the new...
351 ANDROID_GLOBAL_BUILD_PATHS=$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800352 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
353 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800354
355 # add kernel specific binaries
356 if [ $(uname -s) = Linux ] ; then
357 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
358 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
359 fi
360
361 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
362 # to ensure that the corresponding 'emulator' binaries are used.
363 case $(uname -s) in
364 Darwin)
365 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
366 ;;
367 Linux)
368 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
369 ;;
370 *)
371 ANDROID_EMULATOR_PREBUILTS=
372 ;;
373 esac
374 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
375 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
376 export ANDROID_EMULATOR_PREBUILTS
377 fi
378
379 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800380 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700381}
382
Matt Alexanderd9c56562020-05-21 10:49:17 +0000383function printconfig()
384{
Christopher Ferris55257d22017-03-23 11:08:58 -0700385 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800386 if [ ! "$T" ]; then
387 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
388 return
389 fi
390 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700391}
392
Matt Alexanderd9c56562020-05-21 10:49:17 +0000393function set_stuff_for_environment()
394{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800395 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800396 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700397
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800398 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700399}
400
Matt Alexanderd9c56562020-05-21 10:49:17 +0000401function set_sequence_number()
402{
Colin Cross88737132017-03-21 17:41:03 -0700403 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700404}
405
Makoto Onukida971062018-06-18 10:15:19 -0700406# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
407function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800408 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700409 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800410 *:"$cmd":*)
411 return 1
412 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700413 esac
414 return 0
415}
416
Matt Alexanderd9c56562020-05-21 10:49:17 +0000417function addcompletions()
418{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700419 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700420
Jim Tanga881a252018-06-19 16:34:41 +0800421 # Keep us from trying to run in something that's neither bash nor zsh.
422 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700423 return
424 fi
425
426 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800427 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700428 return
429 fi
430
Jim Tanga881a252018-06-19 16:34:41 +0800431 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000432 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800433 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800434 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400435 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800436 )
Makoto Onukida971062018-06-18 10:15:19 -0700437 # Completion can be disabled selectively to allow users to use non-standard completion.
438 # e.g.
439 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
440 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400441 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800442 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400443 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000444 if [ ! -f "$f" ]; then
445 echo "Warning: completion file $f not found"
446 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700447 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700448 fi
449 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700450
Matt Alexanderd9c56562020-05-21 10:49:17 +0000451 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700452 complete -C "bit --tab" bit
453 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000454 if [ -z "$ZSH_VERSION" ]; then
455 # Doesn't work in zsh.
456 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400457 # TODO(b/244559459): Support b autocompletion for zsh
458 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000459 fi
Jim Tanga881a252018-06-19 16:34:41 +0800460 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800461
Cole Faust24c36db2021-01-23 02:39:37 +0000462 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100463 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000464 complete -F _complete_android_module_names outmod
465 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000466 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100467 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700468}
469
Joe Onorato824608c2022-04-08 11:06:16 -0700470function multitree_lunch_help()
471{
472 echo "usage: lunch PRODUCT-VARIANT" 1>&2
473 echo " Set up android build environment based on a product short name and variant" 1>&2
474 echo 1>&2
475 echo "lunch COMBO_FILE VARIANT" 1>&2
476 echo " Set up android build environment based on a specific lunch combo file" 1>&2
477 echo " and variant." 1>&2
478 echo 1>&2
479 echo "lunch --print [CONFIG]" 1>&2
480 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
481 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
482 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
483 echo 1>&2
484 echo "lunch --list" 1>&2
485 echo " List all possible combo files available in the current tree" 1>&2
486 echo 1>&2
487 echo "lunch --help" 1>&2
488 echo "lunch -h" 1>&2
489 echo " Prints this message." 1>&2
490}
491
492function multitree_lunch()
493{
494 local code
495 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000496 # Lunch must be run in the topdir, but this way we get a clear error
497 # message, instead of FileNotFound.
498 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000499 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000500 _multitree_lunch_error
501 return 1
502 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700503 if $(echo "$1" | grep -q '^-') ; then
504 # Calls starting with a -- argument are passed directly and the function
505 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000506 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700507 code=$?
508 if [[ $code -eq 2 ]] ; then
509 echo 1>&2
510 multitree_lunch_help
511 return $code
512 elif [[ $code -ne 0 ]] ; then
513 return $code
514 fi
515 else
516 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000517 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700518 code=$?
519 if [[ $code -eq 2 ]] ; then
520 echo 1>&2
521 multitree_lunch_help
522 return $code
523 elif [[ $code -ne 0 ]] ; then
524 return $code
525 fi
526
527 export TARGET_BUILD_COMBO=${results[0]}
528 export TARGET_BUILD_VARIANT=${results[1]}
529 fi
530}
531
Matt Alexanderd9c56562020-05-21 10:49:17 +0000532function choosetype()
533{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534 echo "Build type choices are:"
535 echo " 1. release"
536 echo " 2. debug"
537 echo
538
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800539 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700540 DEFAULT_NUM=1
541 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700542
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800543 export TARGET_BUILD_TYPE=
544 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700545 while [ -z $TARGET_BUILD_TYPE ]
546 do
547 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000548 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800549 read ANSWER
550 else
551 echo $1
552 ANSWER=$1
553 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700554 case $ANSWER in
555 "")
556 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
557 ;;
558 1)
559 export TARGET_BUILD_TYPE=release
560 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561 release)
562 export TARGET_BUILD_TYPE=release
563 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700564 2)
565 export TARGET_BUILD_TYPE=debug
566 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567 debug)
568 export TARGET_BUILD_TYPE=debug
569 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700570 *)
571 echo
572 echo "I didn't understand your response. Please try again."
573 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 ;;
575 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000576 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800577 break
578 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 done
580
Ying Wang08800fd2016-03-03 20:57:21 -0800581 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800583 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584}
585
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586#
587# This function isn't really right: It chooses a TARGET_PRODUCT
588# based on the list of boards. Usually, that gets you something
589# that kinda works with a generic product, but really, you should
590# pick a product by name.
591#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000592function chooseproduct()
593{
Christopher Ferris55257d22017-03-23 11:08:58 -0700594 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000595 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 default_value=$TARGET_PRODUCT
597 else
Ying Wang0a76df52015-06-08 11:57:26 -0700598 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 fi
600
Ying Wang08800fd2016-03-03 20:57:21 -0800601 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 export TARGET_PRODUCT=
603 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604 while [ -z "$TARGET_PRODUCT" ]
605 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700606 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000607 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800608 read ANSWER
609 else
610 echo $1
611 ANSWER=$1
612 fi
613
Matt Alexanderd9c56562020-05-21 10:49:17 +0000614 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700615 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000617 if check_product $ANSWER
618 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 export TARGET_PRODUCT=$ANSWER
620 else
621 echo "** Not a valid product: $ANSWER"
622 fi
623 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000624 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800625 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700626 fi
627 done
628
Ying Wang08800fd2016-03-03 20:57:21 -0800629 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700630 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800631 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700632}
633
Matt Alexanderd9c56562020-05-21 10:49:17 +0000634function choosevariant()
635{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636 echo "Variant choices are:"
637 local index=1
638 local v
639 for v in ${VARIANT_CHOICES[@]}
640 do
641 # The product name is the name of the directory containing
642 # the makefile we found, above.
643 echo " $index. $v"
644 index=$(($index+1))
645 done
646
647 local default_value=eng
648 local ANSWER
649
650 export TARGET_BUILD_VARIANT=
651 while [ -z "$TARGET_BUILD_VARIANT" ]
652 do
653 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000654 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800655 read ANSWER
656 else
657 echo $1
658 ANSWER=$1
659 fi
660
Matt Alexanderd9c56562020-05-21 10:49:17 +0000661 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000663 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
664 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200665 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800666 fi
667 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000668 if check_variant $ANSWER
669 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800670 export TARGET_BUILD_VARIANT=$ANSWER
671 else
672 echo "** Not a valid variant: $ANSWER"
673 fi
674 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000675 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800676 break
677 fi
678 done
679}
680
Matt Alexanderd9c56562020-05-21 10:49:17 +0000681function choosecombo()
682{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700683 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700684
685 echo
686 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700687 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700688
689 echo
690 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700691 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800692
693 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800694 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700695 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800696 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800697 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700698}
699
Matt Alexanderd9c56562020-05-21 10:49:17 +0000700function add_lunch_combo()
701{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800702 if [ -n "$ZSH_VERSION" ]; then
703 echo -n "${funcfiletrace[1]}: "
704 else
705 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
706 fi
707 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 -0800708}
709
Matt Alexanderd9c56562020-05-21 10:49:17 +0000710function print_lunch_menu()
711{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700712 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100713 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700714 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 +0100715 local ret=$?
716
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700717 echo
718 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700719 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100720
Matt Alexanderd9c56562020-05-21 10:49:17 +0000721 if [ $ret -ne 0 ]
722 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100723 echo "Warning: Cannot display lunch menu."
724 echo
725 echo "Note: You can invoke lunch with an explicit target:"
726 echo
727 echo " usage: lunch [target]" >&2
728 echo
729 return
730 fi
731
Will Burr40401202022-02-07 12:12:01 +0000732 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800733
734 local i=1
735 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700736 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800737 do
738 echo " $i. $choice"
739 i=$(($i+1))
740 done
741
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700742 echo
743}
744
Matt Alexanderd9c56562020-05-21 10:49:17 +0000745function lunch()
746{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800747 local answer
748
Steven Moreland92793dc2020-02-25 18:30:18 -0800749 if [[ $# -gt 1 ]]; then
750 echo "usage: lunch [target]" >&2
751 return 1
752 fi
753
Will Burr40401202022-02-07 12:12:01 +0000754 local used_lunch_menu=0
755
Steven Moreland92793dc2020-02-25 18:30:18 -0800756 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800757 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700758 else
759 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000760 echo "Which would you like? [aosp_arm-eng]"
761 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 -0800762 read answer
Will Burr40401202022-02-07 12:12:01 +0000763 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 fi
765
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800766 local selection=
767
Matt Alexanderd9c56562020-05-21 10:49:17 +0000768 if [ -z "$answer" ]
769 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700770 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000771 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
772 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800773 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000774 if [ $answer -le ${#choices[@]} ]
775 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800776 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000777 if [ -n "$ZSH_VERSION" ]
778 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800779 selection=${choices[$(($answer))]}
780 else
781 selection=${choices[$(($answer-1))]}
782 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800783 fi
Colin Cross88737132017-03-21 17:41:03 -0700784 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800785 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700786 fi
787
Joe Onoratoda12daf2010-06-09 18:18:31 -0700788 export TARGET_BUILD_APPS=
789
Colin Cross88737132017-03-21 17:41:03 -0700790 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700791 product=${selection%%-*} # Trim everything after first dash
792 variant_and_version=${selection#*-} # Trim everything up to first dash
793 if [ "$variant_and_version" != "$selection" ]; then
794 variant=${variant_and_version%%-*}
795 if [ "$variant" != "$variant_and_version" ]; then
796 version=${variant_and_version#*-}
797 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700798 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800799
Matt Alexanderd9c56562020-05-21 10:49:17 +0000800 if [ -z "$product" ]
801 then
Ying Wang08800fd2016-03-03 20:57:21 -0800802 echo
Colin Cross88737132017-03-21 17:41:03 -0700803 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700804 return 1
805 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800806
Colin Cross88737132017-03-21 17:41:03 -0700807 TARGET_PRODUCT=$product \
808 TARGET_BUILD_VARIANT=$variant \
809 TARGET_PLATFORM_VERSION=$version \
810 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000811 if [ $? -ne 0 ]
812 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100813 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
814 then
815 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
816 fi
Colin Cross88737132017-03-21 17:41:03 -0700817 return 1
818 fi
Colin Cross88737132017-03-21 17:41:03 -0700819 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
820 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700821 if [ -n "$version" ]; then
822 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
823 else
824 unset TARGET_PLATFORM_VERSION
825 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700826 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700827
Will Burr40401202022-02-07 12:12:01 +0000828 if [ $used_lunch_menu -eq 1 ]; then
829 echo
830 echo "Hint: next time you can simply run 'lunch $selection'"
831 fi
832
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700833 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800834
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700835 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700836 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800837 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400838
839 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
840 check_mu_config
841 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700842}
843
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700844unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700845# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000846function _lunch()
847{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700848 local cur prev opts
849 COMPREPLY=()
850 cur="${COMP_WORDS[COMP_CWORD]}"
851 prev="${COMP_WORDS[COMP_CWORD-1]}"
852
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700853 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800854 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700855 fi
856
857 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700858 return 0
859}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700860
Joe Onoratoda12daf2010-06-09 18:18:31 -0700861# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700862# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000863function tapas()
864{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700865 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800866 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700867 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700868 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 -0800869 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
870 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)"
871
Joe Onoratoda12daf2010-06-09 18:18:31 -0700872
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700873 if [ "$showHelp" != "" ]; then
874 $(gettop)/build/make/tapasHelp.sh
875 return
876 fi
877
Ying Wang67f02922012-08-22 10:25:20 -0700878 if [ $(echo $arch | wc -w) -gt 1 ]; then
879 echo "tapas: Error: Multiple build archs supplied: $arch"
880 return
881 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700882 if [ $(echo $variant | wc -w) -gt 1 ]; then
883 echo "tapas: Error: Multiple build variants supplied: $variant"
884 return
885 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700886 if [ $(echo $density | wc -w) -gt 1 ]; then
887 echo "tapas: Error: Multiple densities supplied: $density"
888 return
889 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800890 if [ $(echo $keys | wc -w) -gt 1 ]; then
891 echo "tapas: Error: Multiple keys supplied: $keys"
892 return
893 fi
Ying Wang67f02922012-08-22 10:25:20 -0700894
Ying Wang0a76df52015-06-08 11:57:26 -0700895 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700896 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000897 x86) product=aosp_x86;;
898 arm64) product=aosp_arm64;;
899 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700900 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800901 if [ -n "$keys" ]; then
902 product=${product/aosp_/aosp_${keys}_}
903 fi;
904
Joe Onoratoda12daf2010-06-09 18:18:31 -0700905 if [ -z "$variant" ]; then
906 variant=eng
907 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700908 if [ -z "$apps" ]; then
909 apps=all
910 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600911 if [ -z "$density" ]; then
912 density=alldpi
913 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700914
Ying Wang67f02922012-08-22 10:25:20 -0700915 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700916 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700917 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700918 export TARGET_BUILD_TYPE=release
919 export TARGET_BUILD_APPS=$apps
920
Ying Wang08800fd2016-03-03 20:57:21 -0800921 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700922 set_stuff_for_environment
923 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800924 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700925}
926
Martin Stjernholmf692c752021-04-12 00:01:10 +0100927# Configures the build to build unbundled Android modules (APEXes).
928# Run banchan with one or more module names (from apex{} modules).
929function banchan()
930{
931 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000932 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 +0100933 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100934 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 +0100935
936 if [ "$showHelp" != "" ]; then
937 $(gettop)/build/make/banchanHelp.sh
938 return
939 fi
940
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100941 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000942 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100943 elif [ $(echo $product | wc -w) -gt 1 ]; then
944 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100945 return
946 fi
947 if [ $(echo $variant | wc -w) -gt 1 ]; then
948 echo "banchan: Error: Multiple build variants supplied: $variant"
949 return
950 fi
951 if [ -z "$apps" ]; then
952 echo "banchan: Error: No modules supplied"
953 return
954 fi
955
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100956 case $product in
957 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100958 x86) product=module_x86;;
959 arm64) product=module_arm64;;
960 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000961 arm64only) product=module_arm64only;;
962 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100963 esac
964 if [ -z "$variant" ]; then
965 variant=eng
966 fi
967
968 export TARGET_PRODUCT=$product
969 export TARGET_BUILD_VARIANT=$variant
970 export TARGET_BUILD_DENSITY=alldpi
971 export TARGET_BUILD_TYPE=release
972
973 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
974 # case is different and it may diverge in the future.
975 export TARGET_BUILD_APPS=$apps
976
977 build_build_var_cache
978 set_stuff_for_environment
979 printconfig
980 destroy_build_var_cache
981}
982
Joe Onorato7cf6f972022-05-11 21:39:57 -0700983# TODO: Merge into gettop as part of launching multitree
984function multitree_gettop
985{
Spandan Dasca762052022-09-21 00:08:34 +0000986 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700987 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
988 # The following circumlocution ensures we remove symlinks from TOP.
989 (cd "$TOP"; PWD= /bin/pwd)
990 else
991 if [ -f $TOPFILE ] ; then
992 # The following circumlocution (repeated below as well) ensures
993 # that we record the true directory name and not one that is
994 # faked up with symlink names.
995 PWD= /bin/pwd
996 else
997 local HERE=$PWD
998 local T=
999 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
1000 \cd ..
1001 T=`PWD= /bin/pwd -P`
1002 done
1003 \cd "$HERE"
1004 if [ -f "$T/$TOPFILE" ]; then
1005 echo "$T"
1006 fi
1007 fi
1008 fi
1009}
1010
Matt Alexanderd9c56562020-05-21 10:49:17 +00001011function croot()
1012{
Christopher Ferris55257d22017-03-23 11:08:58 -07001013 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001014 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001015 if [ "$1" ]; then
1016 \cd $(gettop)/$1
1017 else
1018 \cd $(gettop)
1019 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001020 else
1021 echo "Couldn't locate the top of the tree. Try setting TOP."
1022 fi
1023}
1024
Matt Alexanderd9c56562020-05-21 10:49:17 +00001025function _croot()
1026{
Anton Hanssonece9c482019-02-04 18:15:39 +00001027 local T=$(gettop)
1028 if [ "$T" ]; then
1029 local cur="${COMP_WORDS[COMP_CWORD]}"
1030 k=0
1031 for c in $(compgen -d ${T}/${cur}); do
1032 COMPREPLY[k++]=${c#${T}/}/
1033 done
1034 fi
1035}
1036
Matt Alexanderd9c56562020-05-21 10:49:17 +00001037function cproj()
1038{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001039 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001040 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001041 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001042 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1043 T=$PWD
1044 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001045 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001046 return
1047 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001048 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001049 done
Ying Wang9cd17642012-12-13 10:52:07 -08001050 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001051 echo "can't find Android.mk"
1052}
1053
Daniel Sandler47e0a882013-07-30 13:23:52 -04001054# simplified version of ps; output in the form
1055# <pid> <procname>
1056function qpid() {
1057 local prepend=''
1058 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001059 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001060 prepend=' '
1061 append='$'
1062 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001063 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001064 echo "usage: qpid [[--exact] <process name|pid>"
1065 return 255
1066 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001067
1068 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001069 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001070 qpid | \grep "$prepend$EXE$append"
1071 else
1072 adb shell ps \
1073 | tr -d '\r' \
1074 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1075 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001076}
1077
Steven Moreland74114f12020-09-10 01:23:32 +00001078# syswrite - disable verity, reboot if needed, and remount image
1079#
1080# Easy way to make system.img/etc writable
1081function syswrite() {
1082 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001083 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001084 echo "rebooting"
1085 adb reboot && adb wait-for-device && adb root || return 1
1086 fi
1087 adb wait-for-device && adb remount || return 1
1088}
1089
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001090# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001091# that has the core-file-size limit set correctly
1092#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001093# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001094# if its core-file-size limit is not set already.
1095# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1096
Matt Alexanderd9c56562020-05-21 10:49:17 +00001097function coredump_setup()
1098{
1099 echo "Getting root...";
1100 adb root;
1101 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001102
Matt Alexanderd9c56562020-05-21 10:49:17 +00001103 echo "Remounting root partition read-write...";
1104 adb shell mount -w -o remount -t rootfs rootfs;
1105 sleep 1;
1106 adb wait-for-device;
1107 adb shell mkdir -p /cores;
1108 adb shell mount -t tmpfs tmpfs /cores;
1109 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001110
Matt Alexanderd9c56562020-05-21 10:49:17 +00001111 echo "Granting SELinux permission to dump in /cores...";
1112 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001113
Matt Alexanderd9c56562020-05-21 10:49:17 +00001114 echo "Set core pattern.";
1115 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116
Ying Wang08800fd2016-03-03 20:57:21 -08001117 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001118}
1119
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001120# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001121# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001122#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001123# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124# dump to actually be generated.
1125
Matt Alexanderd9c56562020-05-21 10:49:17 +00001126function coredump_enable()
1127{
1128 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001129 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001130 printf "Expecting a PID!\n";
1131 return;
1132 fi;
1133 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001134 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001135}
1136
1137# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001138# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001139#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001140# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001141# enabled globally.
1142
Matt Alexanderd9c56562020-05-21 10:49:17 +00001143function core()
1144{
1145 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001146
Ying Wang08800fd2016-03-03 20:57:21 -08001147 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001148 printf "Expecting a PID!\n";
1149 return;
1150 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001151
Matt Alexanderd9c56562020-05-21 10:49:17 +00001152 local CORENAME=core.$PID;
1153 local COREPATH=/cores/$CORENAME;
1154 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001155
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001157
Matt Alexanderd9c56562020-05-21 10:49:17 +00001158 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001159 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001160 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1161 adb shell kill -$SIG $PID;
1162 sleep 1;
1163 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001164
Matt Alexanderd9c56562020-05-21 10:49:17 +00001165 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1166 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001167}
1168
Christopher Tate744ee802009-11-12 15:33:08 -08001169# systemstack - dump the current stack trace of all threads in the system process
1170# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001171function systemstack()
1172{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001173 stacks system_server
1174}
1175
Michael Wrightaeed7212014-06-19 19:58:12 -07001176# Read the ELF header from /proc/$PID/exe to determine if the process is
1177# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178function is64bit()
1179{
Ben Chengfba67bf2014-02-25 10:27:07 -08001180 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001181 if [ "$PID" ] ; then
1182 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001183 echo "64"
1184 else
1185 echo ""
1186 fi
1187 else
1188 echo ""
1189 fi
1190}
1191
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001192case `uname -s` in
1193 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001194 function sgrep()
1195 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001196 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 -04001197 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001198 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001199
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 ;;
1201 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202 function sgrep()
1203 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001204 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 -04001205 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206 }
1207 ;;
1208esac
1209
Matt Alexanderd9c56562020-05-21 10:49:17 +00001210function gettargetarch
1211{
Raghu Gandham8da43102012-07-25 19:57:22 -07001212 get_build_var TARGET_ARCH
1213}
1214
Matt Alexanderd9c56562020-05-21 10:49:17 +00001215function ggrep()
1216{
Mike Frysinger5e479732015-09-22 18:13:48 -04001217 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1218 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001219}
1220
Matt Alexanderd9c56562020-05-21 10:49:17 +00001221function gogrep()
1222{
Orion Hodson831472d2019-10-25 11:35:15 +01001223 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1224 -exec grep --color -n "$@" {} +
1225}
1226
Matt Alexanderd9c56562020-05-21 10:49:17 +00001227function jgrep()
1228{
Mike Frysinger5e479732015-09-22 18:13:48 -04001229 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1230 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231}
1232
Alistair Delva176e5342021-02-22 13:31:26 -08001233function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001234{
1235 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1236 -exec grep --color -n "$@" {} +
1237}
1238
Taesu Leeea0cecd2020-10-28 11:05:18 +09001239function ktgrep()
1240{
1241 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1242 -exec grep --color -n "$@" {} +
1243}
1244
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245function cgrep()
1246{
Mike Frysinger5e479732015-09-22 18:13:48 -04001247 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
1248 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function resgrep()
1252{
Christopher Ferris55257d22017-03-23 11:08:58 -07001253 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001254 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1255 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1256 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001257}
1258
Matt Alexanderd9c56562020-05-21 10:49:17 +00001259function mangrep()
1260{
Mike Frysinger5e479732015-09-22 18:13:48 -04001261 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1262 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001263}
1264
Matt Alexanderd9c56562020-05-21 10:49:17 +00001265function owngrep()
1266{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001267 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1268 -exec grep --color -n "$@" {} +
1269}
1270
Matt Alexanderd9c56562020-05-21 10:49:17 +00001271function sepgrep()
1272{
Mike Frysinger5e479732015-09-22 18:13:48 -04001273 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1274 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001275}
1276
Matt Alexanderd9c56562020-05-21 10:49:17 +00001277function rcgrep()
1278{
Mike Frysinger5e479732015-09-22 18:13:48 -04001279 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1280 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001281}
1282
DroidFreak32a2781982020-11-26 11:30:19 -05001283function pygrep()
1284{
1285 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1286 -exec grep --color -n "$@" {} +
1287}
1288
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001289case `uname -s` in
1290 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001291 function mgrep()
1292 {
Orion Hodson831472d2019-10-25 11:35:15 +01001293 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 -04001294 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001295 }
1296
Matt Alexanderd9c56562020-05-21 10:49:17 +00001297 function treegrep()
1298 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001299 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 -04001300 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301 }
1302
1303 ;;
1304 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001305 function mgrep()
1306 {
Orion Hodson831472d2019-10-25 11:35:15 +01001307 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 -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 . -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 -04001314 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001315 }
1316
1317 ;;
1318esac
1319
Matt Alexanderd9c56562020-05-21 10:49:17 +00001320function getprebuilt
1321{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001322 get_abs_build_var ANDROID_PREBUILTS
1323}
1324
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325function tracedmdump()
1326{
Christopher Ferris55257d22017-03-23 11:08:58 -07001327 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001328 if [ ! "$T" ]; then
1329 echo "Couldn't locate the top of the tree. Try setting TOP."
1330 return
1331 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001332 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001333 local arch=$(gettargetarch)
1334 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001335
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001336 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001337 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001338 echo "usage: tracedmdump tracename"
1339 return
1340 fi
1341
Matt Alexanderd9c56562020-05-21 10:49:17 +00001342 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001343 echo "Error: cannot find kernel: '$KERNEL'"
1344 return
1345 fi
1346
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001347 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001348 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001349 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1350 fi
1351
1352 echo "post-processing traces..."
1353 rm -f $TRACE/qtrace.dexlist
1354 post_trace $TRACE
1355 if [ $? -ne 0 ]; then
1356 echo "***"
1357 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1358 echo "***"
1359 return
1360 fi
1361 echo "generating dexlist output..."
1362 /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
1363 echo "generating dmtrace data..."
1364 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1365 echo "generating html file..."
1366 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1367 echo "done, see $TRACE/dmtrace.html for details"
1368 echo "or run:"
1369 echo " traceview $TRACE/dmtrace"
1370}
1371
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001372# communicate with a running device or emulator, set up necessary state,
1373# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001374function runhat()
1375{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001376 # process standard adb options
1377 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001378 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001379 adbTarget=$1
1380 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001381 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001382 adbTarget="$1 $2"
1383 shift 2
1384 fi
1385 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001386 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001387
1388 # runhat options
1389 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001390
Matt Alexanderd9c56562020-05-21 10:49:17 +00001391 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001392 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001393 return
1394 fi
1395
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001396 # confirm hat is available
1397 if [ -z $(which hat) ]; then
1398 echo "hat is not available in this configuration."
1399 return
1400 fi
1401
Andy McFaddenb6289852010-07-12 08:00:19 -07001402 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001403 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001404 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001405 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001406 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001407 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001408 echo -n "> "
1409 read
1410
The Android Open Source Project88b60792009-03-03 19:28:42 -08001411 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001412
The Android Open Source Project88b60792009-03-03 19:28:42 -08001413 echo "Retrieving file $devFile..."
1414 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001415
The Android Open Source Project88b60792009-03-03 19:28:42 -08001416 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001417
The Android Open Source Project88b60792009-03-03 19:28:42 -08001418 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419 echo "View the output by pointing your browser at http://localhost:7000/"
1420 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001421 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001422}
1423
Matt Alexanderd9c56562020-05-21 10:49:17 +00001424function getbugreports()
1425{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001426 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001427
1428 if [ ! "$reports" ]; then
1429 echo "Could not locate any bugreports."
1430 return
1431 fi
1432
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001433 local report
1434 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001435 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001436 echo "/sdcard/bugreports/${report}"
1437 adb pull /sdcard/bugreports/${report} ${report}
1438 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001439 done
1440}
1441
Matt Alexanderd9c56562020-05-21 10:49:17 +00001442function getsdcardpath()
1443{
Victoria Lease1b296b42012-08-21 15:44:06 -07001444 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1445}
1446
Matt Alexanderd9c56562020-05-21 10:49:17 +00001447function getscreenshotpath()
1448{
Victoria Lease1b296b42012-08-21 15:44:06 -07001449 echo "$(getsdcardpath)/Pictures/Screenshots"
1450}
1451
Matt Alexanderd9c56562020-05-21 10:49:17 +00001452function getlastscreenshot()
1453{
Victoria Lease1b296b42012-08-21 15:44:06 -07001454 local screenshot_path=$(getscreenshotpath)
1455 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 +00001456 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001457 echo "No screenshots found."
1458 return
1459 fi
1460 echo "${screenshot}"
1461 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1462}
1463
Matt Alexanderd9c56562020-05-21 10:49:17 +00001464function startviewserver()
1465{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001466 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001467 if [ $# -gt 0 ]; then
1468 port=$1
1469 fi
1470 adb shell service call window 1 i32 $port
1471}
1472
Matt Alexanderd9c56562020-05-21 10:49:17 +00001473function stopviewserver()
1474{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001475 adb shell service call window 2
1476}
1477
Matt Alexanderd9c56562020-05-21 10:49:17 +00001478function isviewserverstarted()
1479{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001480 adb shell service call window 3
1481}
1482
Matt Alexanderd9c56562020-05-21 10:49:17 +00001483function key_home()
1484{
Romain Guyb84049a2010-10-04 16:56:11 -07001485 adb shell input keyevent 3
1486}
1487
Matt Alexanderd9c56562020-05-21 10:49:17 +00001488function key_back()
1489{
Romain Guyb84049a2010-10-04 16:56:11 -07001490 adb shell input keyevent 4
1491}
1492
Matt Alexanderd9c56562020-05-21 10:49:17 +00001493function key_menu()
1494{
Romain Guyb84049a2010-10-04 16:56:11 -07001495 adb shell input keyevent 82
1496}
1497
Matt Alexanderd9c56562020-05-21 10:49:17 +00001498function smoketest()
1499{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001500 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1501 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1502 return
1503 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001504 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001505 if [ ! "$T" ]; then
1506 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1507 return
1508 fi
1509
Ying Wang9cd17642012-12-13 10:52:07 -08001510 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001511 adb uninstall com.android.smoketest > /dev/null &&
1512 adb uninstall com.android.smoketest.tests > /dev/null &&
1513 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1514 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1515 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1516}
1517
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001518# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001519function runtest()
1520{
Christopher Ferris55257d22017-03-23 11:08:58 -07001521 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001522 if [ ! "$T" ]; then
1523 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1524 return
1525 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001526 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001527}
1528
The Android Open Source Project88b60792009-03-03 19:28:42 -08001529function godir () {
1530 if [[ -z "$1" ]]; then
1531 echo "Usage: godir <regex>"
1532 return
1533 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001534 local T=$(gettop)
1535 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001536 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001537 mkdir -p $OUT_DIR
1538 FILELIST=$OUT_DIR/filelist
1539 else
1540 FILELIST=$T/filelist
1541 fi
1542 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001543 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001544 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001545 echo " Done"
1546 echo ""
1547 fi
1548 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001549 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001550 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001551 echo "Not found"
1552 return
1553 fi
1554 local pathname
1555 local choice
1556 if [[ ${#lines[@]} > 1 ]]; then
1557 while [[ -z "$pathname" ]]; do
1558 local index=1
1559 local line
1560 for line in ${lines[@]}; do
1561 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001562 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001563 done
1564 echo
1565 echo -n "Select one: "
1566 unset choice
1567 read choice
1568 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1569 echo "Invalid choice"
1570 continue
1571 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001572 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001573 done
1574 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001575 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001576 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001577 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001578}
1579
Steven Moreland62054a42018-12-06 10:11:40 -08001580# Update module-info.json in out.
1581function refreshmod() {
1582 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1583 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1584 return 1
1585 fi
1586
1587 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1588
1589 # for the output of the next command
1590 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1591
1592 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001593 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001594 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1595}
1596
Cole Faust3e192382021-10-25 13:29:15 -07001597# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001598function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001599 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001600 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1601 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1602 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001603 return 1
1604 fi
1605
1606 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001607 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001608 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001609 fi
1610 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001611 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001612}
1613
Cole Faust5d825b72022-10-26 18:16:44 -07001614# List all modules for the current device, as cached in all_modules.txt. If any build change is
1615# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001616function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001617 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001618}
1619
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001620# Return the Bazel label of a Soong module if it is converted with bp2build.
1621function bmod()
1622(
1623 if [ $# -ne 1 ]; then
1624 echo "usage: bmod <module>" >&2
1625 return 1
1626 fi
1627
1628 # We could run bp2build here, but it might trigger bp2build invalidation
1629 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1630 # time overhead.
1631 #
1632 # For a snappy result, use the latest generated version in soong_injection,
1633 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001634 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001635
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001636 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001637 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1638 return 1
1639 fi
1640
1641 local target_label=$(python3 -c "import json
1642module = '$1'
1643converted_json='$converted_json'
1644bp2build_converted_map = json.load(open(converted_json))
1645if module not in bp2build_converted_map:
1646 exit(1)
1647print(bp2build_converted_map[module] + ':' + module)")
1648
1649 if [ -z "${target_label}" ]; then
1650 echo "$1 is not converted to Bazel." >&2
1651 return 1
1652 else
1653 echo "${target_label}"
1654 fi
1655)
1656
Joe Onorato2c1aa472021-02-25 16:42:39 -08001657# Get the path of a specific module in the android tree, as cached in module-info.json.
1658# If any build change is made, and it should be reflected in the output, you should run
1659# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001660function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001661 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001662 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001663 return 1
1664 fi
1665
Cole Faust24c36db2021-01-23 02:39:37 +00001666 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001667
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001668 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001669module = '$1'
1670module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1671if module not in module_info:
1672 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001673print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001674
1675 if [ -z "$relpath" ]; then
1676 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1677 return 1
1678 else
Rett Berg78d1c932019-01-24 14:34:23 -08001679 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001680 fi
1681}
1682
Joe Onorato2c1aa472021-02-25 16:42:39 -08001683# Get the path of a specific module in the android tree, as cached in module-info.json.
1684# If any build change is made, and it should be reflected in the output, you should run
1685# 'refreshmod' first. Note: This is the inverse of pathmod.
1686function dirmods() {
1687 if [[ $# -ne 1 ]]; then
1688 echo "usage: dirmods <path>" >&2
1689 return 1
1690 fi
1691
1692 verifymodinfo || return 1
1693
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001694 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001695dir = '$1'
1696while dir.endswith('/'):
1697 dir = dir[:-1]
1698prefix = dir + '/'
1699module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1700results = set()
1701for m in module_info.values():
1702 for path in m.get(u'path', []):
1703 if path == dir or path.startswith(prefix):
1704 name = m.get(u'module_name')
1705 if name:
1706 results.add(name)
1707for name in sorted(results):
1708 print(name)
1709"
1710}
1711
1712
Rett Berg78d1c932019-01-24 14:34:23 -08001713# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1714# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1715function gomod() {
1716 if [[ $# -ne 1 ]]; then
1717 echo "usage: gomod <module>" >&2
1718 return 1
1719 fi
1720
1721 local path="$(pathmod $@)"
1722 if [ -z "$path" ]; then
1723 return 1
1724 fi
1725 cd $path
1726}
1727
Cole Faust24c36db2021-01-23 02:39:37 +00001728# Gets the list of a module's installed outputs, as cached in module-info.json.
1729# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1730function outmod() {
1731 if [[ $# -ne 1 ]]; then
1732 echo "usage: outmod <module>" >&2
1733 return 1
1734 fi
1735
1736 verifymodinfo || return 1
1737
1738 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001739 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001740module = '$1'
1741module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1742if module not in module_info:
1743 exit(1)
1744for output in module_info[module]['installed']:
1745 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1746
1747 if [ $? -ne 0 ]; then
1748 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1749 return 1
1750 elif [ ! -z "$relpath" ]; then
1751 echo "$relpath"
1752 fi
1753}
1754
1755# adb install a module's apk, as cached in module-info.json. If any build change
1756# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1757# Usage: installmod [adb install arguments] <module>
1758# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1759function installmod() {
1760 if [[ $# -eq 0 ]]; then
1761 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001762 echo "" >&2
1763 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1764 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1765 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001766 return 1
1767 fi
1768
1769 local _path
1770 _path=$(outmod ${@:$#:1})
1771 if [ $? -ne 0 ]; then
1772 return 1
1773 fi
1774
1775 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1776 if [ -z "$_path" ]; then
1777 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1778 return 1
1779 fi
Cole Faust3e192382021-10-25 13:29:15 -07001780 local serial_device=""
1781 if [[ "$1" == "-s" ]]; then
1782 if [[ $# -le 2 ]]; then
1783 echo "-s requires an argument" >&2
1784 return 1
1785 fi
1786 serial_device="-s $2"
1787 shift 2
1788 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001789 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001790 echo adb $serial_device install ${@:1:$length} $_path
1791 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001792}
1793
dimitry73b84812018-12-11 18:06:00 +01001794function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001795 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001796 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001797}
1798
Alex Rayf0d08eb2013-03-08 15:15:06 -08001799# Print colored exit condition
1800function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001801 "$@"
1802 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001803 if [ $retval -ne 0 ]
1804 then
Jacky Cao89483b82015-05-15 22:12:53 +08001805 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001806 else
Jacky Cao89483b82015-05-15 22:12:53 +08001807 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001808 fi
1809 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001810}
1811
Matt Alexanderd9c56562020-05-21 10:49:17 +00001812function get_make_command()
1813{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001814 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1815 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001816 # Always use the real make if -C is passed in
1817 for arg in "$@"; do
1818 if [[ $arg == -C* ]]; then
1819 echo command make
1820 return
1821 fi
1822 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001823 echo build/soong/soong_ui.bash --make-mode
1824 else
1825 echo command make
1826 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001827}
1828
Matt Alexanderd9c56562020-05-21 10:49:17 +00001829function _wrap_build()
1830{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001831 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1832 "$@"
1833 return $?
1834 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001835 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001836 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001837 local ret=$?
1838 local end_time=$(date +"%s")
1839 local tdiff=$(($end_time-$start_time))
1840 local hours=$(($tdiff / 3600 ))
1841 local mins=$((($tdiff % 3600) / 60))
1842 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001843 local ncolors=$(tput colors 2>/dev/null)
1844 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001845 color_failed=$'\E'"[0;31m"
1846 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001847 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001848 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001849 else
1850 color_failed=""
1851 color_success=""
1852 color_reset=""
1853 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001854
1855 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1856 echo
1857 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1858 fi
1859
Ed Heylcc6be0a2014-06-18 14:55:58 -07001860 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001861 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001862 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001863 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001864 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001865 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001866 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001867 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001868 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001869 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001870 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001871 printf "(%s seconds)" $secs
1872 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001873 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001874 echo
1875 return $ret
1876}
1877
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001878function _trigger_build()
1879(
1880 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001881 local T=$(gettop)
1882 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001883 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1884 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001885 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1886 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001887 fi
1888)
1889
1890function m()
1891(
1892 _trigger_build "all-modules" "$@"
1893)
1894
1895function mm()
1896(
1897 _trigger_build "modules-in-a-dir-no-deps" "$@"
1898)
1899
1900function mmm()
1901(
1902 _trigger_build "modules-in-dirs-no-deps" "$@"
1903)
1904
1905function mma()
1906(
1907 _trigger_build "modules-in-a-dir" "$@"
1908)
1909
1910function mmma()
1911(
1912 _trigger_build "modules-in-dirs" "$@"
1913)
1914
Matt Alexanderd9c56562020-05-21 10:49:17 +00001915function make()
1916{
Dan Willemsene9842242017-07-28 13:00:13 -07001917 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001918}
1919
Joe Onorato7cf6f972022-05-11 21:39:57 -07001920function _multitree_lunch_error()
1921{
1922 >&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."
1923}
1924
1925function multitree_build()
1926{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001927 local T=$(multitree_gettop)
1928 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001929 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001930 else
1931 _multitree_lunch_error
1932 return 1
1933 fi
1934}
1935
Matt Alexanderd9c56562020-05-21 10:49:17 +00001936function provision()
1937{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001938 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1939 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1940 return 1
1941 fi
1942 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1943 echo "There is no provisioning script for the device." >&2
1944 return 1
1945 fi
1946
1947 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001948 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001949 shift 1
1950 else
1951 echo "This action will reflash your device."
1952 echo ""
1953 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1954 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001955 echo -n "Are you sure you want to do this (yes/no)? "
1956 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001957 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001958 echo "Not taking any action. Exiting." >&2
1959 return 1
1960 fi
1961 fi
1962 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1963}
1964
Jim Tanga881a252018-06-19 16:34:41 +08001965# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001966function enable_zsh_completion() {
1967 # Don't override user's options if bash-style completion is already enabled.
1968 if ! declare -f complete >/dev/null; then
1969 autoload -U compinit && compinit
1970 autoload -U bashcompinit && bashcompinit
1971 fi
Jim Tanga881a252018-06-19 16:34:41 +08001972}
1973
1974function validate_current_shell() {
1975 local current_sh="$(ps -o command -p $$)"
1976 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001977 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001978 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001979 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001980 *zsh*)
1981 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001982 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001983 *)
Jim Tanga881a252018-06-19 16:34:41 +08001984 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 -07001985 ;;
1986 esac
Jim Tanga881a252018-06-19 16:34:41 +08001987}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001988
1989# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001990# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1991# load those.
1992#
1993# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001994function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001995 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001996 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001997 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001998 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 -08001999 if [ -n "$allowed" ]; then
2000 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2001 echo " $allowed"
2002 echo " $f"
2003 return
2004 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002005 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002006 done
2007
2008 allowed_files=
2009 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002010 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002011 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002012 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002013
2014 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002015 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002016 else
2017 echo "ignoring $f, not in $allowed"
2018 fi
Jim Tanga881a252018-06-19 16:34:41 +08002019 done
2020 done
2021}
Kenny Root52aa81c2011-07-15 11:07:06 -07002022
Dan Albertbab814f2020-08-26 15:34:53 -07002023function showcommands() {
2024 local T=$(gettop)
2025 if [[ -z "$TARGET_PRODUCT" ]]; then
2026 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2027 return
2028 fi
2029 case $(uname -s) in
2030 Darwin)
2031 PREBUILT_NAME=darwin-x86
2032 ;;
2033 Linux)
2034 PREBUILT_NAME=linux-x86
2035 ;;
2036 *)
2037 >&2 echo Unknown host $(uname -s)
2038 return
2039 ;;
2040 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002041 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002042 if [[ "$1" == "--regenerate" ]]; then
2043 shift 1
2044 NINJA_ARGS="-t commands $@" m
2045 else
2046 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2047 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2048 -t commands "$@")
2049 fi
2050}
2051
Cole Faust45844ab2022-08-30 13:59:07 -07002052function avbtool() {
2053 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2054 m avbtool
2055 fi
2056 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2057}
2058
Jim Tanga881a252018-06-19 16:34:41 +08002059validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002060set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002061source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002062addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002063