blob: 51a11879526b89e8d22edaec2931843bf5b095d8 [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)
447 if [ -n "$T" ]; then
LaMont Jones5151ddc2022-10-13 23:05:15 +0000448 "$T/orchestrator/build/orchestrator/core/lunch.py" "$@"
LaMont Jonesc39e5022022-06-23 19:09:06 +0000449 else
450 _multitree_lunch_error
451 return 1
452 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700453 if $(echo "$1" | grep -q '^-') ; then
454 # Calls starting with a -- argument are passed directly and the function
455 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000456 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700457 code=$?
458 if [[ $code -eq 2 ]] ; then
459 echo 1>&2
460 multitree_lunch_help
461 return $code
462 elif [[ $code -ne 0 ]] ; then
463 return $code
464 fi
465 else
466 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000467 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700468 code=$?
469 if [[ $code -eq 2 ]] ; then
470 echo 1>&2
471 multitree_lunch_help
472 return $code
473 elif [[ $code -ne 0 ]] ; then
474 return $code
475 fi
476
477 export TARGET_BUILD_COMBO=${results[0]}
478 export TARGET_BUILD_VARIANT=${results[1]}
479 fi
480}
481
Matt Alexanderd9c56562020-05-21 10:49:17 +0000482function choosetype()
483{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 echo "Build type choices are:"
485 echo " 1. release"
486 echo " 2. debug"
487 echo
488
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800489 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700490 DEFAULT_NUM=1
491 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800493 export TARGET_BUILD_TYPE=
494 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 while [ -z $TARGET_BUILD_TYPE ]
496 do
497 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000498 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499 read ANSWER
500 else
501 echo $1
502 ANSWER=$1
503 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700504 case $ANSWER in
505 "")
506 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
507 ;;
508 1)
509 export TARGET_BUILD_TYPE=release
510 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800511 release)
512 export TARGET_BUILD_TYPE=release
513 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700514 2)
515 export TARGET_BUILD_TYPE=debug
516 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800517 debug)
518 export TARGET_BUILD_TYPE=debug
519 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700520 *)
521 echo
522 echo "I didn't understand your response. Please try again."
523 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700524 ;;
525 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000526 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800527 break
528 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700529 done
530
Ying Wang08800fd2016-03-03 20:57:21 -0800531 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700532 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800533 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534}
535
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800536#
537# This function isn't really right: It chooses a TARGET_PRODUCT
538# based on the list of boards. Usually, that gets you something
539# that kinda works with a generic product, but really, you should
540# pick a product by name.
541#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000542function chooseproduct()
543{
Christopher Ferris55257d22017-03-23 11:08:58 -0700544 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000545 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546 default_value=$TARGET_PRODUCT
547 else
Ying Wang0a76df52015-06-08 11:57:26 -0700548 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549 fi
550
Ying Wang08800fd2016-03-03 20:57:21 -0800551 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800552 export TARGET_PRODUCT=
553 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700554 while [ -z "$TARGET_PRODUCT" ]
555 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700556 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000557 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558 read ANSWER
559 else
560 echo $1
561 ANSWER=$1
562 fi
563
Matt Alexanderd9c56562020-05-21 10:49:17 +0000564 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700565 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000567 if check_product $ANSWER
568 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800569 export TARGET_PRODUCT=$ANSWER
570 else
571 echo "** Not a valid product: $ANSWER"
572 fi
573 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000574 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576 fi
577 done
578
Ying Wang08800fd2016-03-03 20:57:21 -0800579 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700580 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800581 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582}
583
Matt Alexanderd9c56562020-05-21 10:49:17 +0000584function choosevariant()
585{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 echo "Variant choices are:"
587 local index=1
588 local v
589 for v in ${VARIANT_CHOICES[@]}
590 do
591 # The product name is the name of the directory containing
592 # the makefile we found, above.
593 echo " $index. $v"
594 index=$(($index+1))
595 done
596
597 local default_value=eng
598 local ANSWER
599
600 export TARGET_BUILD_VARIANT=
601 while [ -z "$TARGET_BUILD_VARIANT" ]
602 do
603 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000604 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800605 read ANSWER
606 else
607 echo $1
608 ANSWER=$1
609 fi
610
Matt Alexanderd9c56562020-05-21 10:49:17 +0000611 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800612 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000613 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
614 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200615 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616 fi
617 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000618 if check_variant $ANSWER
619 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800620 export TARGET_BUILD_VARIANT=$ANSWER
621 else
622 echo "** Not a valid variant: $ANSWER"
623 fi
624 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000625 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626 break
627 fi
628 done
629}
630
Matt Alexanderd9c56562020-05-21 10:49:17 +0000631function choosecombo()
632{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700633 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700634
635 echo
636 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700637 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700638
639 echo
640 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700641 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642
643 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800644 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700645 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800647 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648}
649
Matt Alexanderd9c56562020-05-21 10:49:17 +0000650function add_lunch_combo()
651{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800652 if [ -n "$ZSH_VERSION" ]; then
653 echo -n "${funcfiletrace[1]}: "
654 else
655 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
656 fi
657 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 -0800658}
659
Matt Alexanderd9c56562020-05-21 10:49:17 +0000660function print_lunch_menu()
661{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100663 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700664 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 +0100665 local ret=$?
666
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667 echo
668 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700669 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100670
Matt Alexanderd9c56562020-05-21 10:49:17 +0000671 if [ $ret -ne 0 ]
672 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100673 echo "Warning: Cannot display lunch menu."
674 echo
675 echo "Note: You can invoke lunch with an explicit target:"
676 echo
677 echo " usage: lunch [target]" >&2
678 echo
679 return
680 fi
681
Will Burr40401202022-02-07 12:12:01 +0000682 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800683
684 local i=1
685 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700686 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800687 do
688 echo " $i. $choice"
689 i=$(($i+1))
690 done
691
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700692 echo
693}
694
Matt Alexanderd9c56562020-05-21 10:49:17 +0000695function lunch()
696{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800697 local answer
698
Steven Moreland92793dc2020-02-25 18:30:18 -0800699 if [[ $# -gt 1 ]]; then
700 echo "usage: lunch [target]" >&2
701 return 1
702 fi
703
Will Burr40401202022-02-07 12:12:01 +0000704 local used_lunch_menu=0
705
Steven Moreland92793dc2020-02-25 18:30:18 -0800706 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800707 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700708 else
709 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000710 echo "Which would you like? [aosp_arm-eng]"
711 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 -0800712 read answer
Will Burr40401202022-02-07 12:12:01 +0000713 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700714 fi
715
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800716 local selection=
717
Matt Alexanderd9c56562020-05-21 10:49:17 +0000718 if [ -z "$answer" ]
719 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700720 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000721 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
722 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800723 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000724 if [ $answer -le ${#choices[@]} ]
725 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800726 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000727 if [ -n "$ZSH_VERSION" ]
728 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800729 selection=${choices[$(($answer))]}
730 else
731 selection=${choices[$(($answer-1))]}
732 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800733 fi
Colin Cross88737132017-03-21 17:41:03 -0700734 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800735 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700736 fi
737
Joe Onoratoda12daf2010-06-09 18:18:31 -0700738 export TARGET_BUILD_APPS=
739
Colin Cross88737132017-03-21 17:41:03 -0700740 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700741 product=${selection%%-*} # Trim everything after first dash
742 variant_and_version=${selection#*-} # Trim everything up to first dash
743 if [ "$variant_and_version" != "$selection" ]; then
744 variant=${variant_and_version%%-*}
745 if [ "$variant" != "$variant_and_version" ]; then
746 version=${variant_and_version#*-}
747 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700748 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800749
Matt Alexanderd9c56562020-05-21 10:49:17 +0000750 if [ -z "$product" ]
751 then
Ying Wang08800fd2016-03-03 20:57:21 -0800752 echo
Colin Cross88737132017-03-21 17:41:03 -0700753 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700754 return 1
755 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800756
Colin Cross88737132017-03-21 17:41:03 -0700757 TARGET_PRODUCT=$product \
758 TARGET_BUILD_VARIANT=$variant \
759 TARGET_PLATFORM_VERSION=$version \
760 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000761 if [ $? -ne 0 ]
762 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100763 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
764 then
765 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
766 fi
Colin Cross88737132017-03-21 17:41:03 -0700767 return 1
768 fi
Colin Cross88737132017-03-21 17:41:03 -0700769 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
770 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700771 if [ -n "$version" ]; then
772 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
773 else
774 unset TARGET_PLATFORM_VERSION
775 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700776 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700777
Will Burr40401202022-02-07 12:12:01 +0000778 if [ $used_lunch_menu -eq 1 ]; then
779 echo
780 echo "Hint: next time you can simply run 'lunch $selection'"
781 fi
782
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700783 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800784
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700785 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700786 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800787 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400788
789 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
790 check_mu_config
791 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700792}
793
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700794unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700795# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000796function _lunch()
797{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700798 local cur prev opts
799 COMPREPLY=()
800 cur="${COMP_WORDS[COMP_CWORD]}"
801 prev="${COMP_WORDS[COMP_CWORD-1]}"
802
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700803 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800804 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700805 fi
806
807 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700808 return 0
809}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700810
Joe Onoratoda12daf2010-06-09 18:18:31 -0700811# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700812# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000813function tapas()
814{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700815 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800816 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700817 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700818 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 -0800819 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
820 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)"
821
Joe Onoratoda12daf2010-06-09 18:18:31 -0700822
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700823 if [ "$showHelp" != "" ]; then
824 $(gettop)/build/make/tapasHelp.sh
825 return
826 fi
827
Ying Wang67f02922012-08-22 10:25:20 -0700828 if [ $(echo $arch | wc -w) -gt 1 ]; then
829 echo "tapas: Error: Multiple build archs supplied: $arch"
830 return
831 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700832 if [ $(echo $variant | wc -w) -gt 1 ]; then
833 echo "tapas: Error: Multiple build variants supplied: $variant"
834 return
835 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700836 if [ $(echo $density | wc -w) -gt 1 ]; then
837 echo "tapas: Error: Multiple densities supplied: $density"
838 return
839 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800840 if [ $(echo $keys | wc -w) -gt 1 ]; then
841 echo "tapas: Error: Multiple keys supplied: $keys"
842 return
843 fi
Ying Wang67f02922012-08-22 10:25:20 -0700844
Ying Wang0a76df52015-06-08 11:57:26 -0700845 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700846 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000847 x86) product=aosp_x86;;
848 arm64) product=aosp_arm64;;
849 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700850 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800851 if [ -n "$keys" ]; then
852 product=${product/aosp_/aosp_${keys}_}
853 fi;
854
Joe Onoratoda12daf2010-06-09 18:18:31 -0700855 if [ -z "$variant" ]; then
856 variant=eng
857 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700858 if [ -z "$apps" ]; then
859 apps=all
860 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600861 if [ -z "$density" ]; then
862 density=alldpi
863 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700864
Ying Wang67f02922012-08-22 10:25:20 -0700865 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700866 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700867 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700868 export TARGET_BUILD_TYPE=release
869 export TARGET_BUILD_APPS=$apps
870
Ying Wang08800fd2016-03-03 20:57:21 -0800871 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700872 set_stuff_for_environment
873 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800874 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700875}
876
Martin Stjernholmf692c752021-04-12 00:01:10 +0100877# Configures the build to build unbundled Android modules (APEXes).
878# Run banchan with one or more module names (from apex{} modules).
879function banchan()
880{
881 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000882 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 +0100883 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100884 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 +0100885
886 if [ "$showHelp" != "" ]; then
887 $(gettop)/build/make/banchanHelp.sh
888 return
889 fi
890
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100891 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000892 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100893 elif [ $(echo $product | wc -w) -gt 1 ]; then
894 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100895 return
896 fi
897 if [ $(echo $variant | wc -w) -gt 1 ]; then
898 echo "banchan: Error: Multiple build variants supplied: $variant"
899 return
900 fi
901 if [ -z "$apps" ]; then
902 echo "banchan: Error: No modules supplied"
903 return
904 fi
905
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100906 case $product in
907 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100908 x86) product=module_x86;;
909 arm64) product=module_arm64;;
910 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000911 arm64only) product=module_arm64only;;
912 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100913 esac
914 if [ -z "$variant" ]; then
915 variant=eng
916 fi
917
918 export TARGET_PRODUCT=$product
919 export TARGET_BUILD_VARIANT=$variant
920 export TARGET_BUILD_DENSITY=alldpi
921 export TARGET_BUILD_TYPE=release
922
923 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
924 # case is different and it may diverge in the future.
925 export TARGET_BUILD_APPS=$apps
926
927 build_build_var_cache
928 set_stuff_for_environment
929 printconfig
930 destroy_build_var_cache
931}
932
Matt Alexanderd9c56562020-05-21 10:49:17 +0000933function gettop
934{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700935 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000936 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700937 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000938 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700939 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000940 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800941 # The following circumlocution (repeated below as well) ensures
942 # that we record the true directory name and not one that is
943 # faked up with symlink names.
944 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700945 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800946 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700947 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000948 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800949 \cd ..
synergyb112a402013-07-05 19:47:47 -0700950 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700951 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000952 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700953 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000954 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700955 fi
956 fi
957 fi
958}
959
Joe Onorato7cf6f972022-05-11 21:39:57 -0700960# TODO: Merge into gettop as part of launching multitree
961function multitree_gettop
962{
Spandan Dasca762052022-09-21 00:08:34 +0000963 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700964 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
965 # The following circumlocution ensures we remove symlinks from TOP.
966 (cd "$TOP"; PWD= /bin/pwd)
967 else
968 if [ -f $TOPFILE ] ; then
969 # The following circumlocution (repeated below as well) ensures
970 # that we record the true directory name and not one that is
971 # faked up with symlink names.
972 PWD= /bin/pwd
973 else
974 local HERE=$PWD
975 local T=
976 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
977 \cd ..
978 T=`PWD= /bin/pwd -P`
979 done
980 \cd "$HERE"
981 if [ -f "$T/$TOPFILE" ]; then
982 echo "$T"
983 fi
984 fi
985 fi
986}
987
Matt Alexanderd9c56562020-05-21 10:49:17 +0000988function croot()
989{
Christopher Ferris55257d22017-03-23 11:08:58 -0700990 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700991 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700992 if [ "$1" ]; then
993 \cd $(gettop)/$1
994 else
995 \cd $(gettop)
996 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700997 else
998 echo "Couldn't locate the top of the tree. Try setting TOP."
999 fi
1000}
1001
Matt Alexanderd9c56562020-05-21 10:49:17 +00001002function _croot()
1003{
Anton Hanssonece9c482019-02-04 18:15:39 +00001004 local T=$(gettop)
1005 if [ "$T" ]; then
1006 local cur="${COMP_WORDS[COMP_CWORD]}"
1007 k=0
1008 for c in $(compgen -d ${T}/${cur}); do
1009 COMPREPLY[k++]=${c#${T}/}/
1010 done
1011 fi
1012}
1013
Matt Alexanderd9c56562020-05-21 10:49:17 +00001014function cproj()
1015{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001016 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001017 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001018 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001019 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1020 T=$PWD
1021 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001022 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001023 return
1024 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001025 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001026 done
Ying Wang9cd17642012-12-13 10:52:07 -08001027 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001028 echo "can't find Android.mk"
1029}
1030
Daniel Sandler47e0a882013-07-30 13:23:52 -04001031# simplified version of ps; output in the form
1032# <pid> <procname>
1033function qpid() {
1034 local prepend=''
1035 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001036 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001037 prepend=' '
1038 append='$'
1039 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001040 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001041 echo "usage: qpid [[--exact] <process name|pid>"
1042 return 255
1043 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001044
1045 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001046 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001047 qpid | \grep "$prepend$EXE$append"
1048 else
1049 adb shell ps \
1050 | tr -d '\r' \
1051 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1052 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001053}
1054
Steven Moreland74114f12020-09-10 01:23:32 +00001055# syswrite - disable verity, reboot if needed, and remount image
1056#
1057# Easy way to make system.img/etc writable
1058function syswrite() {
1059 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001060 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001061 echo "rebooting"
1062 adb reboot && adb wait-for-device && adb root || return 1
1063 fi
1064 adb wait-for-device && adb remount || return 1
1065}
1066
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001067# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001068# that has the core-file-size limit set correctly
1069#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001070# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001071# if its core-file-size limit is not set already.
1072# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1073
Matt Alexanderd9c56562020-05-21 10:49:17 +00001074function coredump_setup()
1075{
1076 echo "Getting root...";
1077 adb root;
1078 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001079
Matt Alexanderd9c56562020-05-21 10:49:17 +00001080 echo "Remounting root partition read-write...";
1081 adb shell mount -w -o remount -t rootfs rootfs;
1082 sleep 1;
1083 adb wait-for-device;
1084 adb shell mkdir -p /cores;
1085 adb shell mount -t tmpfs tmpfs /cores;
1086 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001087
Matt Alexanderd9c56562020-05-21 10:49:17 +00001088 echo "Granting SELinux permission to dump in /cores...";
1089 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001090
Matt Alexanderd9c56562020-05-21 10:49:17 +00001091 echo "Set core pattern.";
1092 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001093
Ying Wang08800fd2016-03-03 20:57:21 -08001094 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001095}
1096
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001097# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001098# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001099#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001100# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001101# dump to actually be generated.
1102
Matt Alexanderd9c56562020-05-21 10:49:17 +00001103function coredump_enable()
1104{
1105 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001106 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001107 printf "Expecting a PID!\n";
1108 return;
1109 fi;
1110 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001111 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001112}
1113
1114# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001117# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001118# enabled globally.
1119
Matt Alexanderd9c56562020-05-21 10:49:17 +00001120function core()
1121{
1122 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001123
Ying Wang08800fd2016-03-03 20:57:21 -08001124 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001125 printf "Expecting a PID!\n";
1126 return;
1127 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001128
Matt Alexanderd9c56562020-05-21 10:49:17 +00001129 local CORENAME=core.$PID;
1130 local COREPATH=/cores/$CORENAME;
1131 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001132
Matt Alexanderd9c56562020-05-21 10:49:17 +00001133 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001134
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001136 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001137 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1138 adb shell kill -$SIG $PID;
1139 sleep 1;
1140 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001141
Matt Alexanderd9c56562020-05-21 10:49:17 +00001142 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1143 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001144}
1145
Christopher Tate744ee802009-11-12 15:33:08 -08001146# systemstack - dump the current stack trace of all threads in the system process
1147# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001148function systemstack()
1149{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001150 stacks system_server
1151}
1152
Michael Wrightaeed7212014-06-19 19:58:12 -07001153# Read the ELF header from /proc/$PID/exe to determine if the process is
1154# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001155function is64bit()
1156{
Ben Chengfba67bf2014-02-25 10:27:07 -08001157 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001158 if [ "$PID" ] ; then
1159 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001160 echo "64"
1161 else
1162 echo ""
1163 fi
1164 else
1165 echo ""
1166 fi
1167}
1168
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001169case `uname -s` in
1170 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001171 function sgrep()
1172 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001173 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 -04001174 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001175 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001176
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001177 ;;
1178 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001179 function sgrep()
1180 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001181 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 -04001182 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001183 }
1184 ;;
1185esac
1186
Matt Alexanderd9c56562020-05-21 10:49:17 +00001187function gettargetarch
1188{
Raghu Gandham8da43102012-07-25 19:57:22 -07001189 get_build_var TARGET_ARCH
1190}
1191
Matt Alexanderd9c56562020-05-21 10:49:17 +00001192function ggrep()
1193{
Mike Frysinger5e479732015-09-22 18:13:48 -04001194 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1195 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001196}
1197
Matt Alexanderd9c56562020-05-21 10:49:17 +00001198function gogrep()
1199{
Orion Hodson831472d2019-10-25 11:35:15 +01001200 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1201 -exec grep --color -n "$@" {} +
1202}
1203
Matt Alexanderd9c56562020-05-21 10:49:17 +00001204function jgrep()
1205{
Mike Frysinger5e479732015-09-22 18:13:48 -04001206 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1207 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208}
1209
Alistair Delva176e5342021-02-22 13:31:26 -08001210function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001211{
1212 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1213 -exec grep --color -n "$@" {} +
1214}
1215
Taesu Leeea0cecd2020-10-28 11:05:18 +09001216function ktgrep()
1217{
1218 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1219 -exec grep --color -n "$@" {} +
1220}
1221
Matt Alexanderd9c56562020-05-21 10:49:17 +00001222function cgrep()
1223{
Mike Frysinger5e479732015-09-22 18:13:48 -04001224 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' \) \
1225 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001226}
1227
Matt Alexanderd9c56562020-05-21 10:49:17 +00001228function resgrep()
1229{
Christopher Ferris55257d22017-03-23 11:08:58 -07001230 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001231 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1232 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1233 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001234}
1235
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236function mangrep()
1237{
Mike Frysinger5e479732015-09-22 18:13:48 -04001238 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1239 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001240}
1241
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242function owngrep()
1243{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001244 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1245 -exec grep --color -n "$@" {} +
1246}
1247
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248function sepgrep()
1249{
Mike Frysinger5e479732015-09-22 18:13:48 -04001250 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1251 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001252}
1253
Matt Alexanderd9c56562020-05-21 10:49:17 +00001254function rcgrep()
1255{
Mike Frysinger5e479732015-09-22 18:13:48 -04001256 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1257 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001258}
1259
DroidFreak32a2781982020-11-26 11:30:19 -05001260function pygrep()
1261{
1262 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.py" \
1263 -exec grep --color -n "$@" {} +
1264}
1265
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001266case `uname -s` in
1267 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001268 function mgrep()
1269 {
Orion Hodson831472d2019-10-25 11:35:15 +01001270 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 -04001271 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001272 }
1273
Matt Alexanderd9c56562020-05-21 10:49:17 +00001274 function treegrep()
1275 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001276 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 -04001277 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001278 }
1279
1280 ;;
1281 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282 function mgrep()
1283 {
Orion Hodson831472d2019-10-25 11:35:15 +01001284 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 -04001285 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286 }
1287
Matt Alexanderd9c56562020-05-21 10:49:17 +00001288 function treegrep()
1289 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001290 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 -04001291 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001292 }
1293
1294 ;;
1295esac
1296
Matt Alexanderd9c56562020-05-21 10:49:17 +00001297function getprebuilt
1298{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001299 get_abs_build_var ANDROID_PREBUILTS
1300}
1301
Matt Alexanderd9c56562020-05-21 10:49:17 +00001302function tracedmdump()
1303{
Christopher Ferris55257d22017-03-23 11:08:58 -07001304 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001305 if [ ! "$T" ]; then
1306 echo "Couldn't locate the top of the tree. Try setting TOP."
1307 return
1308 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001309 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001310 local arch=$(gettargetarch)
1311 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001312
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001313 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001314 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001315 echo "usage: tracedmdump tracename"
1316 return
1317 fi
1318
Matt Alexanderd9c56562020-05-21 10:49:17 +00001319 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001320 echo "Error: cannot find kernel: '$KERNEL'"
1321 return
1322 fi
1323
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001324 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001326 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1327 fi
1328
1329 echo "post-processing traces..."
1330 rm -f $TRACE/qtrace.dexlist
1331 post_trace $TRACE
1332 if [ $? -ne 0 ]; then
1333 echo "***"
1334 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1335 echo "***"
1336 return
1337 fi
1338 echo "generating dexlist output..."
1339 /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
1340 echo "generating dmtrace data..."
1341 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1342 echo "generating html file..."
1343 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1344 echo "done, see $TRACE/dmtrace.html for details"
1345 echo "or run:"
1346 echo " traceview $TRACE/dmtrace"
1347}
1348
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001349# communicate with a running device or emulator, set up necessary state,
1350# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001351function runhat()
1352{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001353 # process standard adb options
1354 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001355 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001356 adbTarget=$1
1357 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001358 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001359 adbTarget="$1 $2"
1360 shift 2
1361 fi
1362 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001363 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001364
1365 # runhat options
1366 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001367
Matt Alexanderd9c56562020-05-21 10:49:17 +00001368 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001369 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001370 return
1371 fi
1372
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001373 # confirm hat is available
1374 if [ -z $(which hat) ]; then
1375 echo "hat is not available in this configuration."
1376 return
1377 fi
1378
Andy McFaddenb6289852010-07-12 08:00:19 -07001379 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001380 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001381 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001382 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001383 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001384 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001385 echo -n "> "
1386 read
1387
The Android Open Source Project88b60792009-03-03 19:28:42 -08001388 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001389
The Android Open Source Project88b60792009-03-03 19:28:42 -08001390 echo "Retrieving file $devFile..."
1391 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001392
The Android Open Source Project88b60792009-03-03 19:28:42 -08001393 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394
The Android Open Source Project88b60792009-03-03 19:28:42 -08001395 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001396 echo "View the output by pointing your browser at http://localhost:7000/"
1397 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001398 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001399}
1400
Matt Alexanderd9c56562020-05-21 10:49:17 +00001401function getbugreports()
1402{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001403 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001404
1405 if [ ! "$reports" ]; then
1406 echo "Could not locate any bugreports."
1407 return
1408 fi
1409
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001410 local report
1411 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001412 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001413 echo "/sdcard/bugreports/${report}"
1414 adb pull /sdcard/bugreports/${report} ${report}
1415 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001416 done
1417}
1418
Matt Alexanderd9c56562020-05-21 10:49:17 +00001419function getsdcardpath()
1420{
Victoria Lease1b296b42012-08-21 15:44:06 -07001421 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1422}
1423
Matt Alexanderd9c56562020-05-21 10:49:17 +00001424function getscreenshotpath()
1425{
Victoria Lease1b296b42012-08-21 15:44:06 -07001426 echo "$(getsdcardpath)/Pictures/Screenshots"
1427}
1428
Matt Alexanderd9c56562020-05-21 10:49:17 +00001429function getlastscreenshot()
1430{
Victoria Lease1b296b42012-08-21 15:44:06 -07001431 local screenshot_path=$(getscreenshotpath)
1432 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 +00001433 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001434 echo "No screenshots found."
1435 return
1436 fi
1437 echo "${screenshot}"
1438 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1439}
1440
Matt Alexanderd9c56562020-05-21 10:49:17 +00001441function startviewserver()
1442{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001443 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001444 if [ $# -gt 0 ]; then
1445 port=$1
1446 fi
1447 adb shell service call window 1 i32 $port
1448}
1449
Matt Alexanderd9c56562020-05-21 10:49:17 +00001450function stopviewserver()
1451{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001452 adb shell service call window 2
1453}
1454
Matt Alexanderd9c56562020-05-21 10:49:17 +00001455function isviewserverstarted()
1456{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001457 adb shell service call window 3
1458}
1459
Matt Alexanderd9c56562020-05-21 10:49:17 +00001460function key_home()
1461{
Romain Guyb84049a2010-10-04 16:56:11 -07001462 adb shell input keyevent 3
1463}
1464
Matt Alexanderd9c56562020-05-21 10:49:17 +00001465function key_back()
1466{
Romain Guyb84049a2010-10-04 16:56:11 -07001467 adb shell input keyevent 4
1468}
1469
Matt Alexanderd9c56562020-05-21 10:49:17 +00001470function key_menu()
1471{
Romain Guyb84049a2010-10-04 16:56:11 -07001472 adb shell input keyevent 82
1473}
1474
Matt Alexanderd9c56562020-05-21 10:49:17 +00001475function smoketest()
1476{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001477 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1478 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1479 return
1480 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001481 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001482 if [ ! "$T" ]; then
1483 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1484 return
1485 fi
1486
Ying Wang9cd17642012-12-13 10:52:07 -08001487 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001488 adb uninstall com.android.smoketest > /dev/null &&
1489 adb uninstall com.android.smoketest.tests > /dev/null &&
1490 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1491 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1492 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1493}
1494
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001495# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001496function runtest()
1497{
Christopher Ferris55257d22017-03-23 11:08:58 -07001498 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001499 if [ ! "$T" ]; then
1500 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1501 return
1502 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001503 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001504}
1505
The Android Open Source Project88b60792009-03-03 19:28:42 -08001506function godir () {
1507 if [[ -z "$1" ]]; then
1508 echo "Usage: godir <regex>"
1509 return
1510 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001511 local T=$(gettop)
1512 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001513 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001514 mkdir -p $OUT_DIR
1515 FILELIST=$OUT_DIR/filelist
1516 else
1517 FILELIST=$T/filelist
1518 fi
1519 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001520 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001521 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001522 echo " Done"
1523 echo ""
1524 fi
1525 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001526 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001527 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001528 echo "Not found"
1529 return
1530 fi
1531 local pathname
1532 local choice
1533 if [[ ${#lines[@]} > 1 ]]; then
1534 while [[ -z "$pathname" ]]; do
1535 local index=1
1536 local line
1537 for line in ${lines[@]}; do
1538 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001539 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001540 done
1541 echo
1542 echo -n "Select one: "
1543 unset choice
1544 read choice
1545 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1546 echo "Invalid choice"
1547 continue
1548 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001549 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001550 done
1551 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001552 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001553 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001554 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001555}
1556
Steven Moreland62054a42018-12-06 10:11:40 -08001557# Update module-info.json in out.
1558function refreshmod() {
1559 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1560 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1561 return 1
1562 fi
1563
1564 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1565
1566 # for the output of the next command
1567 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1568
1569 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001570 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001571 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1572}
1573
Cole Faust3e192382021-10-25 13:29:15 -07001574# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001575function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001576 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001577 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1578 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1579 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001580 return 1
1581 fi
1582
1583 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001584 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001585 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001586 fi
1587 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001588 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001589}
1590
Cole Faust5d825b72022-10-26 18:16:44 -07001591# List all modules for the current device, as cached in all_modules.txt. If any build change is
1592# made and it should be reflected in the output, you should run `m nothing` first.
Cole Faust24c36db2021-01-23 02:39:37 +00001593function allmod() {
Cole Faust5d825b72022-10-26 18:16:44 -07001594 cat $ANDROID_PRODUCT_OUT/all_modules.txt 2>/dev/null
Steven Moreland62054a42018-12-06 10:11:40 -08001595}
1596
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001597# Return the Bazel label of a Soong module if it is converted with bp2build.
1598function bmod()
1599(
1600 if [ $# -ne 1 ]; then
1601 echo "usage: bmod <module>" >&2
1602 return 1
1603 fi
1604
1605 # We could run bp2build here, but it might trigger bp2build invalidation
1606 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1607 # time overhead.
1608 #
1609 # For a snappy result, use the latest generated version in soong_injection,
1610 # and ask users to run m bp2build if it doesn't exist.
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001611 converted_json="$(get_abs_build_var OUT_DIR)/soong/soong_injection/metrics/converted_modules_path_map.json"
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001612
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001613 if [ ! -f ${converted_json} ]; then
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001614 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1615 return 1
1616 fi
1617
1618 local target_label=$(python3 -c "import json
1619module = '$1'
1620converted_json='$converted_json'
1621bp2build_converted_map = json.load(open(converted_json))
1622if module not in bp2build_converted_map:
1623 exit(1)
1624print(bp2build_converted_map[module] + ':' + module)")
1625
1626 if [ -z "${target_label}" ]; then
1627 echo "$1 is not converted to Bazel." >&2
1628 return 1
1629 else
1630 echo "${target_label}"
1631 fi
1632)
1633
Joe Onorato2c1aa472021-02-25 16:42:39 -08001634# Get the path of a specific module in the android tree, as cached in module-info.json.
1635# If any build change is made, and it should be reflected in the output, you should run
1636# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001637function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001638 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001639 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001640 return 1
1641 fi
1642
Cole Faust24c36db2021-01-23 02:39:37 +00001643 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001644
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001645 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001646module = '$1'
1647module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1648if module not in module_info:
1649 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001650print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001651
1652 if [ -z "$relpath" ]; then
1653 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1654 return 1
1655 else
Rett Berg78d1c932019-01-24 14:34:23 -08001656 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001657 fi
1658}
1659
Joe Onorato2c1aa472021-02-25 16:42:39 -08001660# Get the path of a specific module in the android tree, as cached in module-info.json.
1661# If any build change is made, and it should be reflected in the output, you should run
1662# 'refreshmod' first. Note: This is the inverse of pathmod.
1663function dirmods() {
1664 if [[ $# -ne 1 ]]; then
1665 echo "usage: dirmods <path>" >&2
1666 return 1
1667 fi
1668
1669 verifymodinfo || return 1
1670
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001671 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001672dir = '$1'
1673while dir.endswith('/'):
1674 dir = dir[:-1]
1675prefix = dir + '/'
1676module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1677results = set()
1678for m in module_info.values():
1679 for path in m.get(u'path', []):
1680 if path == dir or path.startswith(prefix):
1681 name = m.get(u'module_name')
1682 if name:
1683 results.add(name)
1684for name in sorted(results):
1685 print(name)
1686"
1687}
1688
1689
Rett Berg78d1c932019-01-24 14:34:23 -08001690# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1691# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1692function gomod() {
1693 if [[ $# -ne 1 ]]; then
1694 echo "usage: gomod <module>" >&2
1695 return 1
1696 fi
1697
1698 local path="$(pathmod $@)"
1699 if [ -z "$path" ]; then
1700 return 1
1701 fi
1702 cd $path
1703}
1704
Cole Faust24c36db2021-01-23 02:39:37 +00001705# Gets the list of a module's installed outputs, as cached in module-info.json.
1706# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1707function outmod() {
1708 if [[ $# -ne 1 ]]; then
1709 echo "usage: outmod <module>" >&2
1710 return 1
1711 fi
1712
1713 verifymodinfo || return 1
1714
1715 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001716 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001717module = '$1'
1718module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1719if module not in module_info:
1720 exit(1)
1721for output in module_info[module]['installed']:
1722 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1723
1724 if [ $? -ne 0 ]; then
1725 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1726 return 1
1727 elif [ ! -z "$relpath" ]; then
1728 echo "$relpath"
1729 fi
1730}
1731
1732# adb install a module's apk, as cached in module-info.json. If any build change
1733# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1734# Usage: installmod [adb install arguments] <module>
1735# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1736function installmod() {
1737 if [[ $# -eq 0 ]]; then
1738 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001739 echo "" >&2
1740 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1741 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1742 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001743 return 1
1744 fi
1745
1746 local _path
1747 _path=$(outmod ${@:$#:1})
1748 if [ $? -ne 0 ]; then
1749 return 1
1750 fi
1751
1752 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1753 if [ -z "$_path" ]; then
1754 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1755 return 1
1756 fi
Cole Faust3e192382021-10-25 13:29:15 -07001757 local serial_device=""
1758 if [[ "$1" == "-s" ]]; then
1759 if [[ $# -le 2 ]]; then
1760 echo "-s requires an argument" >&2
1761 return 1
1762 fi
1763 serial_device="-s $2"
1764 shift 2
1765 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001766 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001767 echo adb $serial_device install ${@:1:$length} $_path
1768 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001769}
1770
dimitry73b84812018-12-11 18:06:00 +01001771function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001772 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -07001773 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001774}
1775
Alex Rayf0d08eb2013-03-08 15:15:06 -08001776# Print colored exit condition
1777function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001778 "$@"
1779 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001780 if [ $retval -ne 0 ]
1781 then
Jacky Cao89483b82015-05-15 22:12:53 +08001782 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001783 else
Jacky Cao89483b82015-05-15 22:12:53 +08001784 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001785 fi
1786 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001787}
1788
Matt Alexanderd9c56562020-05-21 10:49:17 +00001789function get_make_command()
1790{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001791 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1792 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001793 # Always use the real make if -C is passed in
1794 for arg in "$@"; do
1795 if [[ $arg == -C* ]]; then
1796 echo command make
1797 return
1798 fi
1799 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001800 echo build/soong/soong_ui.bash --make-mode
1801 else
1802 echo command make
1803 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001804}
1805
Matt Alexanderd9c56562020-05-21 10:49:17 +00001806function _wrap_build()
1807{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001808 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1809 "$@"
1810 return $?
1811 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001812 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001813 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001814 local ret=$?
1815 local end_time=$(date +"%s")
1816 local tdiff=$(($end_time-$start_time))
1817 local hours=$(($tdiff / 3600 ))
1818 local mins=$((($tdiff % 3600) / 60))
1819 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001820 local ncolors=$(tput colors 2>/dev/null)
1821 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001822 color_failed=$'\E'"[0;31m"
1823 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001824 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001825 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001826 else
1827 color_failed=""
1828 color_success=""
1829 color_reset=""
1830 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001831
1832 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1833 echo
1834 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1835 fi
1836
Ed Heylcc6be0a2014-06-18 14:55:58 -07001837 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001838 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001839 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001840 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001841 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001842 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001843 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001844 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001845 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001846 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001847 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001848 printf "(%s seconds)" $secs
1849 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001850 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001851 echo
1852 return $ret
1853}
1854
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001855function _trigger_build()
1856(
1857 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001858 local T=$(gettop)
1859 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001860 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1861 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001862 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1863 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001864 fi
1865)
1866
1867function m()
1868(
1869 _trigger_build "all-modules" "$@"
1870)
1871
1872function mm()
1873(
1874 _trigger_build "modules-in-a-dir-no-deps" "$@"
1875)
1876
1877function mmm()
1878(
1879 _trigger_build "modules-in-dirs-no-deps" "$@"
1880)
1881
1882function mma()
1883(
1884 _trigger_build "modules-in-a-dir" "$@"
1885)
1886
1887function mmma()
1888(
1889 _trigger_build "modules-in-dirs" "$@"
1890)
1891
Matt Alexanderd9c56562020-05-21 10:49:17 +00001892function make()
1893{
Dan Willemsene9842242017-07-28 13:00:13 -07001894 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001895}
1896
Joe Onorato7cf6f972022-05-11 21:39:57 -07001897function _multitree_lunch_error()
1898{
1899 >&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."
1900}
1901
1902function multitree_build()
1903{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001904 local T=$(multitree_gettop)
1905 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001906 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001907 else
1908 _multitree_lunch_error
1909 return 1
1910 fi
1911}
1912
Matt Alexanderd9c56562020-05-21 10:49:17 +00001913function provision()
1914{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001915 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1916 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1917 return 1
1918 fi
1919 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1920 echo "There is no provisioning script for the device." >&2
1921 return 1
1922 fi
1923
1924 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001925 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001926 shift 1
1927 else
1928 echo "This action will reflash your device."
1929 echo ""
1930 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1931 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001932 echo -n "Are you sure you want to do this (yes/no)? "
1933 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001934 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001935 echo "Not taking any action. Exiting." >&2
1936 return 1
1937 fi
1938 fi
1939 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1940}
1941
Jim Tanga881a252018-06-19 16:34:41 +08001942# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001943function enable_zsh_completion() {
1944 # Don't override user's options if bash-style completion is already enabled.
1945 if ! declare -f complete >/dev/null; then
1946 autoload -U compinit && compinit
1947 autoload -U bashcompinit && bashcompinit
1948 fi
Jim Tanga881a252018-06-19 16:34:41 +08001949}
1950
1951function validate_current_shell() {
1952 local current_sh="$(ps -o command -p $$)"
1953 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001954 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001955 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001956 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001957 *zsh*)
1958 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001959 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001960 *)
Jim Tanga881a252018-06-19 16:34:41 +08001961 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 -07001962 ;;
1963 esac
Jim Tanga881a252018-06-19 16:34:41 +08001964}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001965
1966# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001967# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1968# load those.
1969#
1970# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001971function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001972 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001973 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001974 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001975 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 -08001976 if [ -n "$allowed" ]; then
1977 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1978 echo " $allowed"
1979 echo " $f"
1980 return
1981 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001982 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001983 done
1984
1985 allowed_files=
1986 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001987 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001988 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001989 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001990
1991 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001992 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001993 else
1994 echo "ignoring $f, not in $allowed"
1995 fi
Jim Tanga881a252018-06-19 16:34:41 +08001996 done
1997 done
1998}
Kenny Root52aa81c2011-07-15 11:07:06 -07001999
Dan Albertbab814f2020-08-26 15:34:53 -07002000function showcommands() {
2001 local T=$(gettop)
2002 if [[ -z "$TARGET_PRODUCT" ]]; then
2003 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2004 return
2005 fi
2006 case $(uname -s) in
2007 Darwin)
2008 PREBUILT_NAME=darwin-x86
2009 ;;
2010 Linux)
2011 PREBUILT_NAME=linux-x86
2012 ;;
2013 *)
2014 >&2 echo Unknown host $(uname -s)
2015 return
2016 ;;
2017 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00002018 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07002019 if [[ "$1" == "--regenerate" ]]; then
2020 shift 1
2021 NINJA_ARGS="-t commands $@" m
2022 else
2023 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2024 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2025 -t commands "$@")
2026 fi
2027}
2028
Cole Faust45844ab2022-08-30 13:59:07 -07002029function avbtool() {
2030 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2031 m avbtool
2032 fi
2033 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2034}
2035
Jim Tanga881a252018-06-19 16:34:41 +08002036validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002037set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08002038source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002039addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08002040