blob: 49908003cf47b5dacd5d62ab1f78f5716358ad19 [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010012 Sets up the build environment for building unbundled apps (APKs).
Anton Hansson90ac61c2022-09-06 14:36:00 +000013- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64|arm64_only|x86_64only] \
14 [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010015 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000016- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080017- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070018- mm: Builds and installs all of the modules in the current directory, and their
19 dependencies.
20- mmm: Builds and installs all of the modules in the supplied directories, and their
21 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080022 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070023- mma: Same as 'mm'
24- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080025- provision: Flash device with all required partitions. Options will be passed on to fastboot.
26- cgrep: Greps on all local C/C++ files.
27- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010028- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080029- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090030- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080031- resgrep: Greps on all local res/*.xml files.
32- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070033- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060034- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080035- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080036- sepgrep: Greps on all local sepolicy files.
37- sgrep: Greps on all local source files.
DroidFreak32a2781982020-11-26 11:30:19 -050038- pygrep: Greps on all local Python files.
Steven Moreland62054a42018-12-06 10:11:40 -080039- godir: Go to the directory containing a file.
40- allmod: List all modules.
41- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000042- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080043- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000044- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080045- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000046- installmod: Adb installs a module's built APK.
47- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000048- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070049
Roland Levillain39341922015-10-20 12:48:19 +010050Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070051- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080052- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070053
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070054Look at the source to view more functions. The complete list is:
55EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070056 local T=$(gettop)
57 local A=""
58 local i
Jacky Cao89483b82015-05-15 22:12:53 +080059 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 -070060 A="$A $i"
61 done
62 echo $A
63}
64
Ying Wang08800fd2016-03-03 20:57:21 -080065# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000066function build_build_var_cache()
67{
Christopher Ferris55257d22017-03-23 11:08:58 -070068 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080069 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080070 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' ' '`)
71 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 -080072 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080073 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080074 --vars="${cached_vars[*]}" \
75 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070076 --var-prefix=var_cache_ \
77 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080078 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000079 if [ $ret -ne 0 ]
80 then
Ying Wang08800fd2016-03-03 20:57:21 -080081 unset build_dicts_script
82 return $ret
83 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070084 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080085 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080086 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080087 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000088 if [ $ret -ne 0 ]
89 then
Ying Wang08800fd2016-03-03 20:57:21 -080090 return $ret
91 fi
92 BUILD_VAR_CACHE_READY="true"
93}
94
Ying Wangf0cb3972016-03-04 13:56:23 -080095# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080096# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000097function destroy_build_var_cache()
98{
Ying Wang08800fd2016-03-03 20:57:21 -080099 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -0700100 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800101 for v in $cached_vars; do
102 unset var_cache_$v
103 done
104 unset cached_vars
105 for v in $cached_abs_vars; do
106 unset abs_var_cache_$v
107 done
108 unset cached_abs_vars
109}
110
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700111# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000112function get_abs_build_var()
113{
114 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
115 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800116 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200117 return
Ying Wang08800fd2016-03-03 20:57:21 -0800118 fi
119
Christopher Ferris55257d22017-03-23 11:08:58 -0700120 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700121 if [ ! "$T" ]; then
122 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
123 return
124 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700125 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700126}
127
128# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000129function get_build_var()
130{
131 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
132 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800133 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100134 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800135 fi
136
Christopher Ferris55257d22017-03-23 11:08:58 -0700137 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138 if [ ! "$T" ]; then
139 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100140 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700141 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700142 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800143}
144
145# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000146function check_product()
147{
Christopher Ferris55257d22017-03-23 11:08:58 -0700148 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800149 if [ ! "$T" ]; then
150 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
151 return
152 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700153 TARGET_PRODUCT=$1 \
154 TARGET_BUILD_VARIANT= \
155 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700156 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800157 get_build_var TARGET_DEVICE > /dev/null
158 # hide successful answers, but allow the errors to show
159}
160
161VARIANT_CHOICES=(user userdebug eng)
162
163# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000164function check_variant()
165{
Christopher Ferris55257d22017-03-23 11:08:58 -0700166 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800167 for v in ${VARIANT_CHOICES[@]}
168 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000169 if [ "$v" = "$1" ]
170 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800171 return 0
172 fi
173 done
174 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700175}
176
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800177
178# Add directories to PATH that are dependent on the lunch target.
179# For directories that are not lunch-specific, add them in set_global_paths
180function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000181{
Christopher Ferris55257d22017-03-23 11:08:58 -0700182 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700183 if [ ! "$T" ]; then
184 echo "Couldn't locate the top of the tree. Try setting TOP."
185 return
186 fi
187
188 ##################################################################
189 # #
190 # Read me before you modify this code #
191 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800192 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
193 # adding to PATH, and the next time it is run, it removes that #
194 # from PATH. This is required so lunch can be run more than #
195 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196 # #
197 ##################################################################
198
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800199 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700200 # due to "C:\Program Files" being in the path.
201
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800202 # Handle compat with the old ANDROID_BUILD_PATHS variable.
203 # TODO: Remove this after we think everyone has lunched again.
204 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
205 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
206 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700207 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000208 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700209 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800210 # strip leading ':', if any
211 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800212 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500213 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700214
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800215 # Out with the old...
216 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
217 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
218 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800219
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800220 # And in with the new...
221 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
222 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700223
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800224 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700225 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800226 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200227
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800228 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
229 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
230
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800231 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800232 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800233 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
234 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
235 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800236
Colin Crosse97e6932017-06-30 16:01:45 -0700237 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
238 export JAVA_HOME=$ANDROID_JAVA_HOME
239 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800240 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
241
242 # Fix up PYTHONPATH
243 if [ -n $ANDROID_PYTHONPATH ]; then
244 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
245 fi
246 export ANDROID_PYTHONPATH=$T/development/python-packages:
247 if [ -n $VENDOR_PYTHONPATH ]; then
248 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
249 fi
250 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500251
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800252 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700253 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
254 export OUT=$ANDROID_PRODUCT_OUT
255
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700256 unset ANDROID_HOST_OUT
257 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
258
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900259 unset ANDROID_SOONG_HOST_OUT
260 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
261
Simran Basidd050ed2017-02-13 13:46:48 -0800262 unset ANDROID_HOST_OUT_TESTCASES
263 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
264
265 unset ANDROID_TARGET_OUT_TESTCASES
266 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
267
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800268 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800269 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800270}
271
272# Add directories to PATH that are NOT dependent on the lunch target.
273# For directories that are lunch-specific, add them in set_lunch_paths
274function set_global_paths()
275{
276 local T=$(gettop)
277 if [ ! "$T" ]; then
278 echo "Couldn't locate the top of the tree. Try setting TOP."
279 return
280 fi
281
282 ##################################################################
283 # #
284 # Read me before you modify this code #
285 # #
286 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
287 # adding to PATH, and the next time it is run, it removes that #
288 # from PATH. This is required so envsetup.sh can be sourced #
289 # more than once and still have working paths. #
290 # #
291 ##################################################################
292
293 # Out with the old...
294 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
295 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
296 fi
297
298 # And in with the new...
299 ANDROID_GLOBAL_BUILD_PATHS=$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800300 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
301 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800302
303 # add kernel specific binaries
304 if [ $(uname -s) = Linux ] ; then
305 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
306 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
307 fi
308
309 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
310 # to ensure that the corresponding 'emulator' binaries are used.
311 case $(uname -s) in
312 Darwin)
313 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
314 ;;
315 Linux)
316 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
317 ;;
318 *)
319 ANDROID_EMULATOR_PREBUILTS=
320 ;;
321 esac
322 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
323 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
324 export ANDROID_EMULATOR_PREBUILTS
325 fi
326
327 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800328 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700329}
330
Matt Alexanderd9c56562020-05-21 10:49:17 +0000331function printconfig()
332{
Christopher Ferris55257d22017-03-23 11:08:58 -0700333 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800334 if [ ! "$T" ]; then
335 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
336 return
337 fi
338 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700339}
340
Matt Alexanderd9c56562020-05-21 10:49:17 +0000341function set_stuff_for_environment()
342{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800343 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800344 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700345
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800346 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700347}
348
Matt Alexanderd9c56562020-05-21 10:49:17 +0000349function set_sequence_number()
350{
Colin Cross88737132017-03-21 17:41:03 -0700351 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700352}
353
Makoto Onukida971062018-06-18 10:15:19 -0700354# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
355function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800356 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700357 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800358 *:"$cmd":*)
359 return 1
360 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700361 esac
362 return 0
363}
364
Matt Alexanderd9c56562020-05-21 10:49:17 +0000365function addcompletions()
366{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700367 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700368
Jim Tanga881a252018-06-19 16:34:41 +0800369 # Keep us from trying to run in something that's neither bash nor zsh.
370 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700371 return
372 fi
373
374 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800375 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700376 return
377 fi
378
Jim Tanga881a252018-06-19 16:34:41 +0800379 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000380 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800381 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800382 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400383 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800384 )
Makoto Onukida971062018-06-18 10:15:19 -0700385 # Completion can be disabled selectively to allow users to use non-standard completion.
386 # e.g.
387 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
388 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400389 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800390 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400391 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000392 if [ ! -f "$f" ]; then
393 echo "Warning: completion file $f not found"
394 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700395 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700396 fi
397 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700398
Matt Alexanderd9c56562020-05-21 10:49:17 +0000399 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700400 complete -C "bit --tab" bit
401 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000402 if [ -z "$ZSH_VERSION" ]; then
403 # Doesn't work in zsh.
404 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400405 # TODO(b/244559459): Support b autocompletion for zsh
406 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000407 fi
Jim Tanga881a252018-06-19 16:34:41 +0800408 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800409
Cole Faust24c36db2021-01-23 02:39:37 +0000410 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100411 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000412 complete -F _complete_android_module_names outmod
413 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000414 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100415 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700416}
417
Joe Onorato824608c2022-04-08 11:06:16 -0700418function multitree_lunch_help()
419{
420 echo "usage: lunch PRODUCT-VARIANT" 1>&2
421 echo " Set up android build environment based on a product short name and variant" 1>&2
422 echo 1>&2
423 echo "lunch COMBO_FILE VARIANT" 1>&2
424 echo " Set up android build environment based on a specific lunch combo file" 1>&2
425 echo " and variant." 1>&2
426 echo 1>&2
427 echo "lunch --print [CONFIG]" 1>&2
428 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
429 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
430 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
431 echo 1>&2
432 echo "lunch --list" 1>&2
433 echo " List all possible combo files available in the current tree" 1>&2
434 echo 1>&2
435 echo "lunch --help" 1>&2
436 echo "lunch -h" 1>&2
437 echo " Prints this message." 1>&2
438}
439
440function multitree_lunch()
441{
442 local code
443 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000444 # Lunch must be run in the topdir, but this way we get a clear error
445 # message, instead of FileNotFound.
446 local T=$(multitree_gettop)
LaMont Jones51670eb2022-12-07 21:55:58 +0000447 if [ -z "$T" ]; then
LaMont Jonesc39e5022022-06-23 19:09:06 +0000448 _multitree_lunch_error
449 return 1
450 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700451 if $(echo "$1" | grep -q '^-') ; then
452 # Calls starting with a -- argument are passed directly and the function
453 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000454 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700455 code=$?
456 if [[ $code -eq 2 ]] ; then
457 echo 1>&2
458 multitree_lunch_help
459 return $code
460 elif [[ $code -ne 0 ]] ; then
461 return $code
462 fi
463 else
464 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000465 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700466 code=$?
467 if [[ $code -eq 2 ]] ; then
468 echo 1>&2
469 multitree_lunch_help
470 return $code
471 elif [[ $code -ne 0 ]] ; then
472 return $code
473 fi
474
475 export TARGET_BUILD_COMBO=${results[0]}
476 export TARGET_BUILD_VARIANT=${results[1]}
477 fi
478}
479
Matt Alexanderd9c56562020-05-21 10:49:17 +0000480function choosetype()
481{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 echo "Build type choices are:"
483 echo " 1. release"
484 echo " 2. debug"
485 echo
486
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800487 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700488 DEFAULT_NUM=1
489 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491 export TARGET_BUILD_TYPE=
492 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493 while [ -z $TARGET_BUILD_TYPE ]
494 do
495 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000496 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800497 read ANSWER
498 else
499 echo $1
500 ANSWER=$1
501 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700502 case $ANSWER in
503 "")
504 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
505 ;;
506 1)
507 export TARGET_BUILD_TYPE=release
508 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800509 release)
510 export TARGET_BUILD_TYPE=release
511 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512 2)
513 export TARGET_BUILD_TYPE=debug
514 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800515 debug)
516 export TARGET_BUILD_TYPE=debug
517 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700518 *)
519 echo
520 echo "I didn't understand your response. Please try again."
521 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700522 ;;
523 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000524 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800525 break
526 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700527 done
528
Ying Wang08800fd2016-03-03 20:57:21 -0800529 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700530 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800531 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700532}
533
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800534#
535# This function isn't really right: It chooses a TARGET_PRODUCT
536# based on the list of boards. Usually, that gets you something
537# that kinda works with a generic product, but really, you should
538# pick a product by name.
539#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000540function chooseproduct()
541{
Christopher Ferris55257d22017-03-23 11:08:58 -0700542 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000543 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544 default_value=$TARGET_PRODUCT
545 else
Ying Wang0a76df52015-06-08 11:57:26 -0700546 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700547 fi
548
Ying Wang08800fd2016-03-03 20:57:21 -0800549 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800550 export TARGET_PRODUCT=
551 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552 while [ -z "$TARGET_PRODUCT" ]
553 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700554 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000555 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556 read ANSWER
557 else
558 echo $1
559 ANSWER=$1
560 fi
561
Matt Alexanderd9c56562020-05-21 10:49:17 +0000562 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800564 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000565 if check_product $ANSWER
566 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567 export TARGET_PRODUCT=$ANSWER
568 else
569 echo "** Not a valid product: $ANSWER"
570 fi
571 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000572 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800573 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 fi
575 done
576
Ying Wang08800fd2016-03-03 20:57:21 -0800577 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800579 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700580}
581
Matt Alexanderd9c56562020-05-21 10:49:17 +0000582function choosevariant()
583{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584 echo "Variant choices are:"
585 local index=1
586 local v
587 for v in ${VARIANT_CHOICES[@]}
588 do
589 # The product name is the name of the directory containing
590 # the makefile we found, above.
591 echo " $index. $v"
592 index=$(($index+1))
593 done
594
595 local default_value=eng
596 local ANSWER
597
598 export TARGET_BUILD_VARIANT=
599 while [ -z "$TARGET_BUILD_VARIANT" ]
600 do
601 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000602 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 read ANSWER
604 else
605 echo $1
606 ANSWER=$1
607 fi
608
Matt Alexanderd9c56562020-05-21 10:49:17 +0000609 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800610 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000611 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
612 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200613 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800614 fi
615 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000616 if check_variant $ANSWER
617 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800618 export TARGET_BUILD_VARIANT=$ANSWER
619 else
620 echo "** Not a valid variant: $ANSWER"
621 fi
622 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000623 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624 break
625 fi
626 done
627}
628
Matt Alexanderd9c56562020-05-21 10:49:17 +0000629function choosecombo()
630{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700631 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700632
633 echo
634 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700635 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700636
637 echo
638 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700639 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640
641 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800642 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700643 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800644 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800645 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700646}
647
Matt Alexanderd9c56562020-05-21 10:49:17 +0000648function add_lunch_combo()
649{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800650 if [ -n "$ZSH_VERSION" ]; then
651 echo -n "${funcfiletrace[1]}: "
652 else
653 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
654 fi
655 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 -0800656}
657
Matt Alexanderd9c56562020-05-21 10:49:17 +0000658function print_lunch_menu()
659{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700660 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100661 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700662 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 +0100663 local ret=$?
664
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665 echo
666 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100668
Matt Alexanderd9c56562020-05-21 10:49:17 +0000669 if [ $ret -ne 0 ]
670 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100671 echo "Warning: Cannot display lunch menu."
672 echo
673 echo "Note: You can invoke lunch with an explicit target:"
674 echo
675 echo " usage: lunch [target]" >&2
676 echo
677 return
678 fi
679
Will Burr40401202022-02-07 12:12:01 +0000680 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800681
682 local i=1
683 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700684 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800685 do
686 echo " $i. $choice"
687 i=$(($i+1))
688 done
689
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700690 echo
691}
692
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693function lunch()
694{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800695 local answer
696
Steven Moreland92793dc2020-02-25 18:30:18 -0800697 if [[ $# -gt 1 ]]; then
698 echo "usage: lunch [target]" >&2
699 return 1
700 fi
701
Will Burr40401202022-02-07 12:12:01 +0000702 local used_lunch_menu=0
703
Steven Moreland92793dc2020-02-25 18:30:18 -0800704 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800705 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700706 else
707 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000708 echo "Which would you like? [aosp_arm-eng]"
709 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 -0800710 read answer
Will Burr40401202022-02-07 12:12:01 +0000711 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700712 fi
713
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800714 local selection=
715
Matt Alexanderd9c56562020-05-21 10:49:17 +0000716 if [ -z "$answer" ]
717 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700718 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000719 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
720 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800721 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000722 if [ $answer -le ${#choices[@]} ]
723 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800724 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000725 if [ -n "$ZSH_VERSION" ]
726 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800727 selection=${choices[$(($answer))]}
728 else
729 selection=${choices[$(($answer-1))]}
730 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800731 fi
Colin Cross88737132017-03-21 17:41:03 -0700732 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800733 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700734 fi
735
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736 export TARGET_BUILD_APPS=
737
Colin Cross88737132017-03-21 17:41:03 -0700738 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700739 product=${selection%%-*} # Trim everything after first dash
740 variant_and_version=${selection#*-} # Trim everything up to first dash
741 if [ "$variant_and_version" != "$selection" ]; then
742 variant=${variant_and_version%%-*}
743 if [ "$variant" != "$variant_and_version" ]; then
744 version=${variant_and_version#*-}
745 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700746 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800747
Matt Alexanderd9c56562020-05-21 10:49:17 +0000748 if [ -z "$product" ]
749 then
Ying Wang08800fd2016-03-03 20:57:21 -0800750 echo
Colin Cross88737132017-03-21 17:41:03 -0700751 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700752 return 1
753 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800754
Colin Cross88737132017-03-21 17:41:03 -0700755 TARGET_PRODUCT=$product \
756 TARGET_BUILD_VARIANT=$variant \
757 TARGET_PLATFORM_VERSION=$version \
758 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000759 if [ $? -ne 0 ]
760 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100761 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
762 then
763 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
764 fi
Colin Cross88737132017-03-21 17:41:03 -0700765 return 1
766 fi
Colin Cross88737132017-03-21 17:41:03 -0700767 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
768 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700769 if [ -n "$version" ]; then
770 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
771 else
772 unset TARGET_PLATFORM_VERSION
773 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700774 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700775
Will Burr40401202022-02-07 12:12:01 +0000776 if [ $used_lunch_menu -eq 1 ]; then
777 echo
778 echo "Hint: next time you can simply run 'lunch $selection'"
779 fi
780
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700781 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800782
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700784 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800785 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400786
787 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
788 check_mu_config
789 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790}
791
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700792unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700793# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000794function _lunch()
795{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700796 local cur prev opts
797 COMPREPLY=()
798 cur="${COMP_WORDS[COMP_CWORD]}"
799 prev="${COMP_WORDS[COMP_CWORD-1]}"
800
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700801 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800802 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700803 fi
804
805 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700806 return 0
807}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700808
Joe Onoratoda12daf2010-06-09 18:18:31 -0700809# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700810# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000811function tapas()
812{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700813 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800814 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700815 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700816 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 -0800817 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
818 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)"
819
Joe Onoratoda12daf2010-06-09 18:18:31 -0700820
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700821 if [ "$showHelp" != "" ]; then
822 $(gettop)/build/make/tapasHelp.sh
823 return
824 fi
825
Ying Wang67f02922012-08-22 10:25:20 -0700826 if [ $(echo $arch | wc -w) -gt 1 ]; then
827 echo "tapas: Error: Multiple build archs supplied: $arch"
828 return
829 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700830 if [ $(echo $variant | wc -w) -gt 1 ]; then
831 echo "tapas: Error: Multiple build variants supplied: $variant"
832 return
833 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700834 if [ $(echo $density | wc -w) -gt 1 ]; then
835 echo "tapas: Error: Multiple densities supplied: $density"
836 return
837 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800838 if [ $(echo $keys | wc -w) -gt 1 ]; then
839 echo "tapas: Error: Multiple keys supplied: $keys"
840 return
841 fi
Ying Wang67f02922012-08-22 10:25:20 -0700842
Ying Wang0a76df52015-06-08 11:57:26 -0700843 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700844 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000845 x86) product=aosp_x86;;
846 arm64) product=aosp_arm64;;
847 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700848 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800849 if [ -n "$keys" ]; then
850 product=${product/aosp_/aosp_${keys}_}
851 fi;
852
Joe Onoratoda12daf2010-06-09 18:18:31 -0700853 if [ -z "$variant" ]; then
854 variant=eng
855 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700856 if [ -z "$apps" ]; then
857 apps=all
858 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600859 if [ -z "$density" ]; then
860 density=alldpi
861 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700862
Ying Wang67f02922012-08-22 10:25:20 -0700863 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700864 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700865 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700866 export TARGET_BUILD_TYPE=release
867 export TARGET_BUILD_APPS=$apps
868
Ying Wang08800fd2016-03-03 20:57:21 -0800869 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700870 set_stuff_for_environment
871 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800872 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700873}
874
Martin Stjernholmf692c752021-04-12 00:01:10 +0100875# Configures the build to build unbundled Android modules (APEXes).
876# Run banchan with one or more module names (from apex{} modules).
877function banchan()
878{
879 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000880 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 +0100881 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100882 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 +0100883
884 if [ "$showHelp" != "" ]; then
885 $(gettop)/build/make/banchanHelp.sh
886 return
887 fi
888
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100889 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000890 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100891 elif [ $(echo $product | wc -w) -gt 1 ]; then
892 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100893 return
894 fi
895 if [ $(echo $variant | wc -w) -gt 1 ]; then
896 echo "banchan: Error: Multiple build variants supplied: $variant"
897 return
898 fi
899 if [ -z "$apps" ]; then
900 echo "banchan: Error: No modules supplied"
901 return
902 fi
903
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100904 case $product in
905 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100906 x86) product=module_x86;;
907 arm64) product=module_arm64;;
908 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000909 arm64only) product=module_arm64only;;
910 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100911 esac
912 if [ -z "$variant" ]; then
913 variant=eng
914 fi
915
916 export TARGET_PRODUCT=$product
917 export TARGET_BUILD_VARIANT=$variant
918 export TARGET_BUILD_DENSITY=alldpi
919 export TARGET_BUILD_TYPE=release
920
921 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
922 # case is different and it may diverge in the future.
923 export TARGET_BUILD_APPS=$apps
924
925 build_build_var_cache
926 set_stuff_for_environment
927 printconfig
928 destroy_build_var_cache
929}
930
Matt Alexanderd9c56562020-05-21 10:49:17 +0000931function gettop
932{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700933 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000934 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700935 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000936 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700937 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000938 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800939 # The following circumlocution (repeated below as well) ensures
940 # that we record the true directory name and not one that is
941 # faked up with symlink names.
942 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700943 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800944 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700945 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000946 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800947 \cd ..
synergyb112a402013-07-05 19:47:47 -0700948 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700949 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000950 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700951 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000952 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700953 fi
954 fi
955 fi
956}
957
Joe Onorato7cf6f972022-05-11 21:39:57 -0700958# TODO: Merge into gettop as part of launching multitree
959function multitree_gettop
960{
Spandan Dasca762052022-09-21 00:08:34 +0000961 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700962 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
963 # The following circumlocution ensures we remove symlinks from TOP.
964 (cd "$TOP"; PWD= /bin/pwd)
965 else
966 if [ -f $TOPFILE ] ; then
967 # The following circumlocution (repeated below as well) ensures
968 # that we record the true directory name and not one that is
969 # faked up with symlink names.
970 PWD= /bin/pwd
971 else
972 local HERE=$PWD
973 local T=
974 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
975 \cd ..
976 T=`PWD= /bin/pwd -P`
977 done
978 \cd "$HERE"
979 if [ -f "$T/$TOPFILE" ]; then
980 echo "$T"
981 fi
982 fi
983 fi
984}
985
Matt Alexanderd9c56562020-05-21 10:49:17 +0000986function croot()
987{
Christopher Ferris55257d22017-03-23 11:08:58 -0700988 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700989 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700990 if [ "$1" ]; then
991 \cd $(gettop)/$1
992 else
993 \cd $(gettop)
994 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700995 else
996 echo "Couldn't locate the top of the tree. Try setting TOP."
997 fi
998}
999
Matt Alexanderd9c56562020-05-21 10:49:17 +00001000function _croot()
1001{
Anton Hanssonece9c482019-02-04 18:15:39 +00001002 local T=$(gettop)
1003 if [ "$T" ]; then
1004 local cur="${COMP_WORDS[COMP_CWORD]}"
1005 k=0
1006 for c in $(compgen -d ${T}/${cur}); do
1007 COMPREPLY[k++]=${c#${T}/}/
1008 done
1009 fi
1010}
1011
Matt Alexanderd9c56562020-05-21 10:49:17 +00001012function cproj()
1013{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001014 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001015 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001016 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001017 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1018 T=$PWD
1019 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001020 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001021 return
1022 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001023 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001024 done
Ying Wang9cd17642012-12-13 10:52:07 -08001025 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001026 echo "can't find Android.mk"
1027}
1028
Daniel Sandler47e0a882013-07-30 13:23:52 -04001029# simplified version of ps; output in the form
1030# <pid> <procname>
1031function qpid() {
1032 local prepend=''
1033 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001034 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001035 prepend=' '
1036 append='$'
1037 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001038 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001039 echo "usage: qpid [[--exact] <process name|pid>"
1040 return 255
1041 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001042
1043 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001044 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001045 qpid | \grep "$prepend$EXE$append"
1046 else
1047 adb shell ps \
1048 | tr -d '\r' \
1049 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1050 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001051}
1052
Steven Moreland74114f12020-09-10 01:23:32 +00001053# syswrite - disable verity, reboot if needed, and remount image
1054#
1055# Easy way to make system.img/etc writable
1056function syswrite() {
1057 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001058 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001059 echo "rebooting"
1060 adb reboot && adb wait-for-device && adb root || return 1
1061 fi
1062 adb wait-for-device && adb remount || return 1
1063}
1064
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001065# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001066# that has the core-file-size limit set correctly
1067#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001068# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001069# if its core-file-size limit is not set already.
1070# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1071
Matt Alexanderd9c56562020-05-21 10:49:17 +00001072function coredump_setup()
1073{
1074 echo "Getting root...";
1075 adb root;
1076 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001077
Matt Alexanderd9c56562020-05-21 10:49:17 +00001078 echo "Remounting root partition read-write...";
1079 adb shell mount -w -o remount -t rootfs rootfs;
1080 sleep 1;
1081 adb wait-for-device;
1082 adb shell mkdir -p /cores;
1083 adb shell mount -t tmpfs tmpfs /cores;
1084 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001085
Matt Alexanderd9c56562020-05-21 10:49:17 +00001086 echo "Granting SELinux permission to dump in /cores...";
1087 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001088
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089 echo "Set core pattern.";
1090 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001091
Ying Wang08800fd2016-03-03 20:57:21 -08001092 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001093}
1094
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001095# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001096# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001097#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001098# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001099# dump to actually be generated.
1100
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101function coredump_enable()
1102{
1103 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001104 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001105 printf "Expecting a PID!\n";
1106 return;
1107 fi;
1108 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001109 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001110}
1111
1112# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001113# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001115# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116# enabled globally.
1117
Matt Alexanderd9c56562020-05-21 10:49:17 +00001118function core()
1119{
1120 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001121
Ying Wang08800fd2016-03-03 20:57:21 -08001122 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123 printf "Expecting a PID!\n";
1124 return;
1125 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126
Matt Alexanderd9c56562020-05-21 10:49:17 +00001127 local CORENAME=core.$PID;
1128 local COREPATH=/cores/$CORENAME;
1129 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001130
Matt Alexanderd9c56562020-05-21 10:49:17 +00001131 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001132
Matt Alexanderd9c56562020-05-21 10:49:17 +00001133 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001134 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1136 adb shell kill -$SIG $PID;
1137 sleep 1;
1138 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001139
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1141 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001142}
1143
Christopher Tate744ee802009-11-12 15:33:08 -08001144# systemstack - dump the current stack trace of all threads in the system process
1145# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001146function systemstack()
1147{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001148 stacks system_server
1149}
1150
Michael Wrightaeed7212014-06-19 19:58:12 -07001151# Read the ELF header from /proc/$PID/exe to determine if the process is
1152# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001153function is64bit()
1154{
Ben Chengfba67bf2014-02-25 10:27:07 -08001155 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156 if [ "$PID" ] ; then
1157 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001158 echo "64"
1159 else
1160 echo ""
1161 fi
1162 else
1163 echo ""
1164 fi
1165}
1166
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001167case `uname -s` in
1168 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001169 function sgrep()
1170 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001171 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 -04001172 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001173 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001174
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001175 ;;
1176 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001177 function sgrep()
1178 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001179 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 -04001180 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001181 }
1182 ;;
1183esac
1184
Matt Alexanderd9c56562020-05-21 10:49:17 +00001185function gettargetarch
1186{
Raghu Gandham8da43102012-07-25 19:57:22 -07001187 get_build_var TARGET_ARCH
1188}
1189
Matt Alexanderd9c56562020-05-21 10:49:17 +00001190function ggrep()
1191{
Mike Frysinger5e479732015-09-22 18:13:48 -04001192 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1193 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001194}
1195
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196function gogrep()
1197{
Orion Hodson831472d2019-10-25 11:35:15 +01001198 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1199 -exec grep --color -n "$@" {} +
1200}
1201
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202function jgrep()
1203{
Mike Frysinger5e479732015-09-22 18:13:48 -04001204 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1205 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206}
1207
Alistair Delva176e5342021-02-22 13:31:26 -08001208function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001209{
1210 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1211 -exec grep --color -n "$@" {} +
1212}
1213
Taesu Leeea0cecd2020-10-28 11:05:18 +09001214function ktgrep()
1215{
1216 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1217 -exec grep --color -n "$@" {} +
1218}
1219
Matt Alexanderd9c56562020-05-21 10:49:17 +00001220function cgrep()
1221{
Mike Frysinger5e479732015-09-22 18:13:48 -04001222 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' \) \
1223 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001224}
1225
Matt Alexanderd9c56562020-05-21 10:49:17 +00001226function resgrep()
1227{
Christopher Ferris55257d22017-03-23 11:08:58 -07001228 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001229 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1230 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1231 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001232}
1233
Matt Alexanderd9c56562020-05-21 10:49:17 +00001234function mangrep()
1235{
Mike Frysinger5e479732015-09-22 18:13:48 -04001236 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1237 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001238}
1239
Matt Alexanderd9c56562020-05-21 10:49:17 +00001240function owngrep()
1241{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001242 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1243 -exec grep --color -n "$@" {} +
1244}
1245
Matt Alexanderd9c56562020-05-21 10:49:17 +00001246function sepgrep()
1247{
Mike Frysinger5e479732015-09-22 18:13:48 -04001248 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1249 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001250}
1251
Matt Alexanderd9c56562020-05-21 10:49:17 +00001252function rcgrep()
1253{
Mike Frysinger5e479732015-09-22 18:13:48 -04001254 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1255 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001256}
1257
DroidFreak32a2781982020-11-26 11:30:19 -05001258function pygrep()
1259{
1260 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1261 -exec grep --color -n "$@" {} +
1262}
1263
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264case `uname -s` in
1265 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001266 function mgrep()
1267 {
Orion Hodson831472d2019-10-25 11:35:15 +01001268 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 -04001269 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001270 }
1271
Matt Alexanderd9c56562020-05-21 10:49:17 +00001272 function treegrep()
1273 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001274 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 -04001275 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001276 }
1277
1278 ;;
1279 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001280 function mgrep()
1281 {
Orion Hodson831472d2019-10-25 11:35:15 +01001282 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 -04001283 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001284 }
1285
Matt Alexanderd9c56562020-05-21 10:49:17 +00001286 function treegrep()
1287 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001288 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 -04001289 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001290 }
1291
1292 ;;
1293esac
1294
Matt Alexanderd9c56562020-05-21 10:49:17 +00001295function getprebuilt
1296{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001297 get_abs_build_var ANDROID_PREBUILTS
1298}
1299
Matt Alexanderd9c56562020-05-21 10:49:17 +00001300function tracedmdump()
1301{
Christopher Ferris55257d22017-03-23 11:08:58 -07001302 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001303 if [ ! "$T" ]; then
1304 echo "Couldn't locate the top of the tree. Try setting TOP."
1305 return
1306 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001307 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001308 local arch=$(gettargetarch)
1309 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001310
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001311 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001312 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001313 echo "usage: tracedmdump tracename"
1314 return
1315 fi
1316
Matt Alexanderd9c56562020-05-21 10:49:17 +00001317 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001318 echo "Error: cannot find kernel: '$KERNEL'"
1319 return
1320 fi
1321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001322 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001323 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001324 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1325 fi
1326
1327 echo "post-processing traces..."
1328 rm -f $TRACE/qtrace.dexlist
1329 post_trace $TRACE
1330 if [ $? -ne 0 ]; then
1331 echo "***"
1332 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1333 echo "***"
1334 return
1335 fi
1336 echo "generating dexlist output..."
1337 /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
1338 echo "generating dmtrace data..."
1339 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1340 echo "generating html file..."
1341 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1342 echo "done, see $TRACE/dmtrace.html for details"
1343 echo "or run:"
1344 echo " traceview $TRACE/dmtrace"
1345}
1346
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001347# communicate with a running device or emulator, set up necessary state,
1348# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001349function runhat()
1350{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001351 # process standard adb options
1352 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001353 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001354 adbTarget=$1
1355 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001356 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001357 adbTarget="$1 $2"
1358 shift 2
1359 fi
1360 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001361 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001362
1363 # runhat options
1364 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001365
Matt Alexanderd9c56562020-05-21 10:49:17 +00001366 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001367 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001368 return
1369 fi
1370
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001371 # confirm hat is available
1372 if [ -z $(which hat) ]; then
1373 echo "hat is not available in this configuration."
1374 return
1375 fi
1376
Andy McFaddenb6289852010-07-12 08:00:19 -07001377 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001378 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001379 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001380 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001381 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001382 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001383 echo -n "> "
1384 read
1385
The Android Open Source Project88b60792009-03-03 19:28:42 -08001386 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001387
The Android Open Source Project88b60792009-03-03 19:28:42 -08001388 echo "Retrieving file $devFile..."
1389 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001390
The Android Open Source Project88b60792009-03-03 19:28:42 -08001391 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001392
The Android Open Source Project88b60792009-03-03 19:28:42 -08001393 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394 echo "View the output by pointing your browser at http://localhost:7000/"
1395 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001396 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001397}
1398
Matt Alexanderd9c56562020-05-21 10:49:17 +00001399function getbugreports()
1400{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001401 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001402
1403 if [ ! "$reports" ]; then
1404 echo "Could not locate any bugreports."
1405 return
1406 fi
1407
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001408 local report
1409 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001410 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001411 echo "/sdcard/bugreports/${report}"
1412 adb pull /sdcard/bugreports/${report} ${report}
1413 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001414 done
1415}
1416
Matt Alexanderd9c56562020-05-21 10:49:17 +00001417function getsdcardpath()
1418{
Victoria Lease1b296b42012-08-21 15:44:06 -07001419 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1420}
1421
Matt Alexanderd9c56562020-05-21 10:49:17 +00001422function getscreenshotpath()
1423{
Victoria Lease1b296b42012-08-21 15:44:06 -07001424 echo "$(getsdcardpath)/Pictures/Screenshots"
1425}
1426
Matt Alexanderd9c56562020-05-21 10:49:17 +00001427function getlastscreenshot()
1428{
Victoria Lease1b296b42012-08-21 15:44:06 -07001429 local screenshot_path=$(getscreenshotpath)
1430 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 +00001431 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001432 echo "No screenshots found."
1433 return
1434 fi
1435 echo "${screenshot}"
1436 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1437}
1438
Matt Alexanderd9c56562020-05-21 10:49:17 +00001439function startviewserver()
1440{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001441 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001442 if [ $# -gt 0 ]; then
1443 port=$1
1444 fi
1445 adb shell service call window 1 i32 $port
1446}
1447
Matt Alexanderd9c56562020-05-21 10:49:17 +00001448function stopviewserver()
1449{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001450 adb shell service call window 2
1451}
1452
Matt Alexanderd9c56562020-05-21 10:49:17 +00001453function isviewserverstarted()
1454{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001455 adb shell service call window 3
1456}
1457
Matt Alexanderd9c56562020-05-21 10:49:17 +00001458function key_home()
1459{
Romain Guyb84049a2010-10-04 16:56:11 -07001460 adb shell input keyevent 3
1461}
1462
Matt Alexanderd9c56562020-05-21 10:49:17 +00001463function key_back()
1464{
Romain Guyb84049a2010-10-04 16:56:11 -07001465 adb shell input keyevent 4
1466}
1467
Matt Alexanderd9c56562020-05-21 10:49:17 +00001468function key_menu()
1469{
Romain Guyb84049a2010-10-04 16:56:11 -07001470 adb shell input keyevent 82
1471}
1472
Matt Alexanderd9c56562020-05-21 10:49:17 +00001473function smoketest()
1474{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001475 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1476 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1477 return
1478 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001479 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001480 if [ ! "$T" ]; then
1481 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1482 return
1483 fi
1484
Ying Wang9cd17642012-12-13 10:52:07 -08001485 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001486 adb uninstall com.android.smoketest > /dev/null &&
1487 adb uninstall com.android.smoketest.tests > /dev/null &&
1488 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1489 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1490 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1491}
1492
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001493# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001494function runtest()
1495{
Christopher Ferris55257d22017-03-23 11:08:58 -07001496 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001497 if [ ! "$T" ]; then
1498 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1499 return
1500 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001501 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001502}
1503
The Android Open Source Project88b60792009-03-03 19:28:42 -08001504function godir () {
1505 if [[ -z "$1" ]]; then
1506 echo "Usage: godir <regex>"
1507 return
1508 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001509 local T=$(gettop)
1510 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001511 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001512 mkdir -p $OUT_DIR
1513 FILELIST=$OUT_DIR/filelist
1514 else
1515 FILELIST=$T/filelist
1516 fi
1517 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001518 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001519 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001520 echo " Done"
1521 echo ""
1522 fi
1523 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001524 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001525 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001526 echo "Not found"
1527 return
1528 fi
1529 local pathname
1530 local choice
1531 if [[ ${#lines[@]} > 1 ]]; then
1532 while [[ -z "$pathname" ]]; do
1533 local index=1
1534 local line
1535 for line in ${lines[@]}; do
1536 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001537 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001538 done
1539 echo
1540 echo -n "Select one: "
1541 unset choice
1542 read choice
1543 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1544 echo "Invalid choice"
1545 continue
1546 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001547 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001548 done
1549 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001550 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001551 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001552 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001553}
1554
Steven Moreland62054a42018-12-06 10:11:40 -08001555# Update module-info.json in out.
1556function refreshmod() {
1557 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1558 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1559 return 1
1560 fi
1561
1562 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1563
1564 # for the output of the next command
1565 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1566
1567 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001568 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001569 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1570}
1571
Cole Faust3e192382021-10-25 13:29:15 -07001572# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001573function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001574 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001575 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1576 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1577 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001578 return 1
1579 fi
1580
1581 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001582 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001583 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001584 fi
1585 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001586 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001587}
1588
Cole Faust5d825b72022-10-26 18:16:44 -07001589# List all modules for the current device, as cached in all_modules.txt. If any build change is
1590# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001591function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001592 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001593}
1594
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001595# Return the Bazel label of a Soong module if it is converted with bp2build.
1596function bmod()
1597(
1598 if [ $# -ne 1 ]; then
1599 echo "usage: bmod <module>" >&2
1600 return 1
1601 fi
1602
1603 # We could run bp2build here, but it might trigger bp2build invalidation
1604 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1605 # time overhead.
1606 #
1607 # For a snappy result, use the latest generated version in soong_injection,
1608 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001609 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001610
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001611 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001612 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1613 return 1
1614 fi
1615
1616 local target_label=$(python3 -c "import json
1617module = '$1'
1618converted_json='$converted_json'
1619bp2build_converted_map = json.load(open(converted_json))
1620if module not in bp2build_converted_map:
1621 exit(1)
1622print(bp2build_converted_map[module] + ':' + module)")
1623
1624 if [ -z "${target_label}" ]; then
1625 echo "$1 is not converted to Bazel." >&2
1626 return 1
1627 else
1628 echo "${target_label}"
1629 fi
1630)
1631
Joe Onorato2c1aa472021-02-25 16:42:39 -08001632# Get the path of a specific module in the android tree, as cached in module-info.json.
1633# If any build change is made, and it should be reflected in the output, you should run
1634# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001635function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001636 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001637 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001638 return 1
1639 fi
1640
Cole Faust24c36db2021-01-23 02:39:37 +00001641 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001642
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001643 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001644module = '$1'
1645module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1646if module not in module_info:
1647 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001648print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001649
1650 if [ -z "$relpath" ]; then
1651 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1652 return 1
1653 else
Rett Berg78d1c932019-01-24 14:34:23 -08001654 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001655 fi
1656}
1657
Joe Onorato2c1aa472021-02-25 16:42:39 -08001658# Get the path of a specific module in the android tree, as cached in module-info.json.
1659# If any build change is made, and it should be reflected in the output, you should run
1660# 'refreshmod' first. Note: This is the inverse of pathmod.
1661function dirmods() {
1662 if [[ $# -ne 1 ]]; then
1663 echo "usage: dirmods <path>" >&2
1664 return 1
1665 fi
1666
1667 verifymodinfo || return 1
1668
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001669 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001670dir = '$1'
1671while dir.endswith('/'):
1672 dir = dir[:-1]
1673prefix = dir + '/'
1674module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1675results = set()
1676for m in module_info.values():
1677 for path in m.get(u'path', []):
1678 if path == dir or path.startswith(prefix):
1679 name = m.get(u'module_name')
1680 if name:
1681 results.add(name)
1682for name in sorted(results):
1683 print(name)
1684"
1685}
1686
1687
Rett Berg78d1c932019-01-24 14:34:23 -08001688# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1689# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1690function gomod() {
1691 if [[ $# -ne 1 ]]; then
1692 echo "usage: gomod <module>" >&2
1693 return 1
1694 fi
1695
1696 local path="$(pathmod $@)"
1697 if [ -z "$path" ]; then
1698 return 1
1699 fi
1700 cd $path
1701}
1702
Cole Faust24c36db2021-01-23 02:39:37 +00001703# Gets the list of a module's installed outputs, as cached in module-info.json.
1704# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1705function outmod() {
1706 if [[ $# -ne 1 ]]; then
1707 echo "usage: outmod <module>" >&2
1708 return 1
1709 fi
1710
1711 verifymodinfo || return 1
1712
1713 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001714 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001715module = '$1'
1716module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1717if module not in module_info:
1718 exit(1)
1719for output in module_info[module]['installed']:
1720 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1721
1722 if [ $? -ne 0 ]; then
1723 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1724 return 1
1725 elif [ ! -z "$relpath" ]; then
1726 echo "$relpath"
1727 fi
1728}
1729
1730# adb install a module's apk, as cached in module-info.json. If any build change
1731# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1732# Usage: installmod [adb install arguments] <module>
1733# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1734function installmod() {
1735 if [[ $# -eq 0 ]]; then
1736 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001737 echo "" >&2
1738 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1739 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1740 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001741 return 1
1742 fi
1743
1744 local _path
1745 _path=$(outmod ${@:$#:1})
1746 if [ $? -ne 0 ]; then
1747 return 1
1748 fi
1749
1750 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1751 if [ -z "$_path" ]; then
1752 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1753 return 1
1754 fi
Cole Faust3e192382021-10-25 13:29:15 -07001755 local serial_device=""
1756 if [[ "$1" == "-s" ]]; then
1757 if [[ $# -le 2 ]]; then
1758 echo "-s requires an argument" >&2
1759 return 1
1760 fi
1761 serial_device="-s $2"
1762 shift 2
1763 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001764 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001765 echo adb $serial_device install ${@:1:$length} $_path
1766 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001767}
1768
dimitry73b84812018-12-11 18:06:00 +01001769function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001770 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001771 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001772}
1773
Alex Rayf0d08eb2013-03-08 15:15:06 -08001774# Print colored exit condition
1775function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001776 "$@"
1777 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001778 if [ $retval -ne 0 ]
1779 then
Jacky Cao89483b82015-05-15 22:12:53 +08001780 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001781 else
Jacky Cao89483b82015-05-15 22:12:53 +08001782 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001783 fi
1784 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001785}
1786
Matt Alexanderd9c56562020-05-21 10:49:17 +00001787function get_make_command()
1788{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001789 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1790 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001791 # Always use the real make if -C is passed in
1792 for arg in "$@"; do
1793 if [[ $arg == -C* ]]; then
1794 echo command make
1795 return
1796 fi
1797 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001798 echo build/soong/soong_ui.bash --make-mode
1799 else
1800 echo command make
1801 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001802}
1803
Matt Alexanderd9c56562020-05-21 10:49:17 +00001804function _wrap_build()
1805{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001806 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1807 "$@"
1808 return $?
1809 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001810 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001811 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001812 local ret=$?
1813 local end_time=$(date +"%s")
1814 local tdiff=$(($end_time-$start_time))
1815 local hours=$(($tdiff / 3600 ))
1816 local mins=$((($tdiff % 3600) / 60))
1817 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001818 local ncolors=$(tput colors 2>/dev/null)
1819 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001820 color_failed=$'\E'"[0;31m"
1821 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001822 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001823 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001824 else
1825 color_failed=""
1826 color_success=""
1827 color_reset=""
1828 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001829
1830 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1831 echo
1832 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1833 fi
1834
Ed Heylcc6be0a2014-06-18 14:55:58 -07001835 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001836 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001837 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001838 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001839 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001840 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001841 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001842 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001843 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001844 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001845 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001846 printf "(%s seconds)" $secs
1847 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001848 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001849 echo
1850 return $ret
1851}
1852
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001853function _trigger_build()
1854(
1855 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001856 local T=$(gettop)
1857 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001858 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1859 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001860 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1861 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001862 fi
1863)
1864
1865function m()
1866(
1867 _trigger_build "all-modules" "$@"
1868)
1869
1870function mm()
1871(
1872 _trigger_build "modules-in-a-dir-no-deps" "$@"
1873)
1874
1875function mmm()
1876(
1877 _trigger_build "modules-in-dirs-no-deps" "$@"
1878)
1879
1880function mma()
1881(
1882 _trigger_build "modules-in-a-dir" "$@"
1883)
1884
1885function mmma()
1886(
1887 _trigger_build "modules-in-dirs" "$@"
1888)
1889
Matt Alexanderd9c56562020-05-21 10:49:17 +00001890function make()
1891{
Dan Willemsene9842242017-07-28 13:00:13 -07001892 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001893}
1894
Joe Onorato7cf6f972022-05-11 21:39:57 -07001895function _multitree_lunch_error()
1896{
1897 >&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."
1898}
1899
1900function multitree_build()
1901{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001902 local T=$(multitree_gettop)
1903 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001904 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001905 else
1906 _multitree_lunch_error
1907 return 1
1908 fi
1909}
1910
Matt Alexanderd9c56562020-05-21 10:49:17 +00001911function provision()
1912{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001913 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1914 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1915 return 1
1916 fi
1917 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1918 echo "There is no provisioning script for the device." >&2
1919 return 1
1920 fi
1921
1922 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001923 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001924 shift 1
1925 else
1926 echo "This action will reflash your device."
1927 echo ""
1928 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1929 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001930 echo -n "Are you sure you want to do this (yes/no)? "
1931 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001932 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001933 echo "Not taking any action. Exiting." >&2
1934 return 1
1935 fi
1936 fi
1937 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1938}
1939
Jim Tanga881a252018-06-19 16:34:41 +08001940# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001941function enable_zsh_completion() {
1942 # Don't override user's options if bash-style completion is already enabled.
1943 if ! declare -f complete >/dev/null; then
1944 autoload -U compinit && compinit
1945 autoload -U bashcompinit && bashcompinit
1946 fi
Jim Tanga881a252018-06-19 16:34:41 +08001947}
1948
1949function validate_current_shell() {
1950 local current_sh="$(ps -o command -p $$)"
1951 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001952 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001953 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001954 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001955 *zsh*)
1956 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001957 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001958 *)
Jim Tanga881a252018-06-19 16:34:41 +08001959 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 -07001960 ;;
1961 esac
Jim Tanga881a252018-06-19 16:34:41 +08001962}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001963
1964# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001965# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1966# load those.
1967#
1968# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001969function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001970 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001971 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001972 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001973 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 -08001974 if [ -n "$allowed" ]; then
1975 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1976 echo " $allowed"
1977 echo " $f"
1978 return
1979 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001980 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001981 done
1982
1983 allowed_files=
1984 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001985 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001986 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001987 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001988
1989 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001990 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001991 else
1992 echo "ignoring $f, not in $allowed"
1993 fi
Jim Tanga881a252018-06-19 16:34:41 +08001994 done
1995 done
1996}
Kenny Root52aa81c2011-07-15 11:07:06 -07001997
Dan Albertbab814f2020-08-26 15:34:53 -07001998function showcommands() {
1999 local T=$(gettop)
2000 if [[ -z "$TARGET_PRODUCT" ]]; then
2001 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2002 return
2003 fi
2004 case $(uname -s) in
2005 Darwin)
2006 PREBUILT_NAME=darwin-x86
2007 ;;
2008 Linux)
2009 PREBUILT_NAME=linux-x86
2010 ;;
2011 *)
2012 >&2 echo Unknown host $(uname -s)
2013 return
2014 ;;
2015 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002016 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002017 if [[ "$1" == "--regenerate" ]]; then
2018 shift 1
2019 NINJA_ARGS="-t commands $@" m
2020 else
2021 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2022 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2023 -t commands "$@")
2024 fi
2025}
2026
Cole Faust45844ab2022-08-30 13:59:07 -07002027function avbtool() {
2028 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2029 m avbtool
2030 fi
2031 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2032}
2033
Jim Tanga881a252018-06-19 16:34:41 +08002034validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002035set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002036source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002037addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002038