blob: 0eeb7f491a0ec87b597b9494ad7c12ea696ceb6c [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).
13- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
14 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000015- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080016- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070017- mm: Builds and installs all of the modules in the current directory, and their
18 dependencies.
19- mmm: Builds and installs all of the modules in the supplied directories, and their
20 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080021 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070022- mma: Same as 'mm'
23- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080024- provision: Flash device with all required partitions. Options will be passed on to fastboot.
25- cgrep: Greps on all local C/C++ files.
26- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010027- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080028- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090029- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- resgrep: Greps on all local res/*.xml files.
31- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070032- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060033- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080034- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080035- sepgrep: Greps on all local sepolicy files.
36- sgrep: Greps on all local source files.
37- godir: Go to the directory containing a file.
38- allmod: List all modules.
39- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080040- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000041- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080042- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000043- installmod: Adb installs a module's built APK.
44- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000045- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046
Roland Levillain39341922015-10-20 12:48:19 +010047Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070048- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080049- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070050
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070051Look at the source to view more functions. The complete list is:
52EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070053 local T=$(gettop)
54 local A=""
55 local i
Jacky Cao89483b82015-05-15 22:12:53 +080056 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 -070057 A="$A $i"
58 done
59 echo $A
60}
61
Ying Wang08800fd2016-03-03 20:57:21 -080062# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000063function build_build_var_cache()
64{
Christopher Ferris55257d22017-03-23 11:08:58 -070065 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080066 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080067 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' ' '`)
68 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 -080069 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080070 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080071 --vars="${cached_vars[*]}" \
72 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070073 --var-prefix=var_cache_ \
74 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080075 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000076 if [ $ret -ne 0 ]
77 then
Ying Wang08800fd2016-03-03 20:57:21 -080078 unset build_dicts_script
79 return $ret
80 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070081 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080082 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080083 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080084 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000085 if [ $ret -ne 0 ]
86 then
Ying Wang08800fd2016-03-03 20:57:21 -080087 return $ret
88 fi
89 BUILD_VAR_CACHE_READY="true"
90}
91
Ying Wangf0cb3972016-03-04 13:56:23 -080092# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080093# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000094function destroy_build_var_cache()
95{
Ying Wang08800fd2016-03-03 20:57:21 -080096 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070097 local v
Ying Wang08800fd2016-03-03 20:57:21 -080098 for v in $cached_vars; do
99 unset var_cache_$v
100 done
101 unset cached_vars
102 for v in $cached_abs_vars; do
103 unset abs_var_cache_$v
104 done
105 unset cached_abs_vars
106}
107
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000109function get_abs_build_var()
110{
111 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
112 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800113 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200114 return
Ying Wang08800fd2016-03-03 20:57:21 -0800115 fi
116
Christopher Ferris55257d22017-03-23 11:08:58 -0700117 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700118 if [ ! "$T" ]; then
119 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
120 return
121 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700122 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700123}
124
125# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000126function get_build_var()
127{
128 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
129 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800130 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100131 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800132 fi
133
Christopher Ferris55257d22017-03-23 11:08:58 -0700134 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700135 if [ ! "$T" ]; then
136 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100137 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700139 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140}
141
142# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000143function check_product()
144{
Christopher Ferris55257d22017-03-23 11:08:58 -0700145 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 if [ ! "$T" ]; then
147 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
148 return
149 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700150 TARGET_PRODUCT=$1 \
151 TARGET_BUILD_VARIANT= \
152 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700153 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800154 get_build_var TARGET_DEVICE > /dev/null
155 # hide successful answers, but allow the errors to show
156}
157
158VARIANT_CHOICES=(user userdebug eng)
159
160# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000161function check_variant()
162{
Christopher Ferris55257d22017-03-23 11:08:58 -0700163 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800164 for v in ${VARIANT_CHOICES[@]}
165 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166 if [ "$v" = "$1" ]
167 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800168 return 0
169 fi
170 done
171 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700172}
173
Matt Alexanderd9c56562020-05-21 10:49:17 +0000174function setpaths()
175{
Christopher Ferris55257d22017-03-23 11:08:58 -0700176 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700177 if [ ! "$T" ]; then
178 echo "Couldn't locate the top of the tree. Try setting TOP."
179 return
180 fi
181
182 ##################################################################
183 # #
184 # Read me before you modify this code #
185 # #
186 # This function sets ANDROID_BUILD_PATHS to what it is adding #
187 # to PATH, and the next time it is run, it removes that from #
188 # PATH. This is required so lunch can be run more than once #
189 # and still have working paths. #
190 # #
191 ##################################################################
192
Raphael Mollc639c782011-06-20 17:25:01 -0700193 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
194 # due to "C:\Program Files" being in the path.
195
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000197 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
199 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000200 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700201 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800202 # strip leading ':', if any
203 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500204 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700205
206 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local prebuiltdir=$(getprebuilt)
208 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700209
Ben Cheng8bc4c432012-11-16 13:29:13 -0800210 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700211 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
212 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800213 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800214
Raphael Mollc639c782011-06-20 17:25:01 -0700215 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800216 export ANDROID_TOOLCHAIN=
217 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200218 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700219 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700222 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400224 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000225 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200226 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000227 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
228 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700229 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200230 *)
231 echo "Can't find toolchain for unknown architecture: $ARCH"
232 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700233 ;;
234 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700235 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800236 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700237 fi
Raphael732936d2011-06-22 14:35:32 -0700238
Christopher Ferris55257d22017-03-23 11:08:58 -0700239 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800240 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
241 fi
242
Dan Willemsen838dcec2021-09-30 22:40:34 +0000243 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700244
245 # add kernel specific binaries
246 case $(uname -s) in
247 Linux)
248 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
249 ;;
250 *)
251 ;;
252 esac
253
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400254 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
Martin Stjernholm6a012262021-11-24 14:56:15 +0000255 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
Yi Kongdfd00b12019-05-21 16:00:04 -0700256 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
257
258 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
259 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
260 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200261
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800262 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
263 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
264
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200265 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
266 # to ensure that the corresponding 'emulator' binaries are used.
267 case $(uname -s) in
268 Darwin)
269 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
270 ;;
271 Linux)
272 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
273 ;;
274 *)
275 ANDROID_EMULATOR_PREBUILTS=
276 ;;
277 esac
278 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700279 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200280 export ANDROID_EMULATOR_PREBUILTS
281 fi
282
Jim Tangb3fda302018-12-22 10:24:55 +0800283 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
284 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700285 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
286 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
287 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000288 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800289
Martin Stjernholm6a012262021-11-24 14:56:15 +0000290 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700291 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800292
293 # out with the duplicate old
294 if [ -n $ANDROID_PYTHONPATH ]; then
295 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
296 fi
297 # and in with the new
298 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800299 if [ -n $VENDOR_PYTHONPATH ]; then
300 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
301 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800302 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800303
Colin Crosse97e6932017-06-30 16:01:45 -0700304 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
305 export JAVA_HOME=$ANDROID_JAVA_HOME
306 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
307 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
308 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500309
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800310 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
312 export OUT=$ANDROID_PRODUCT_OUT
313
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700314 unset ANDROID_HOST_OUT
315 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
316
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900317 unset ANDROID_SOONG_HOST_OUT
318 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
319
Simran Basidd050ed2017-02-13 13:46:48 -0800320 unset ANDROID_HOST_OUT_TESTCASES
321 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
322
323 unset ANDROID_TARGET_OUT_TESTCASES
324 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327 # TODO: fix the path
328 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
329}
330
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000331function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000332{
Jingwen Chen17d56992021-06-10 08:17:54 +0000333 if which bazel &>/dev/null; then
334 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
335 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000336 fi
337
Jingwen Chen17d56992021-06-10 08:17:54 +0000338 local T="$(gettop)"
339 if [ ! "$T" ]; then
340 >&2 echo "Couldn't locate the top of the Android tree. Try setting TOP. This bazel() function cannot be used outside of the AOSP directory."
341 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000342 fi
343
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000344 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000345}
346
Matt Alexanderd9c56562020-05-21 10:49:17 +0000347function printconfig()
348{
Christopher Ferris55257d22017-03-23 11:08:58 -0700349 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800350 if [ ! "$T" ]; then
351 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
352 return
353 fi
354 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700355}
356
Matt Alexanderd9c56562020-05-21 10:49:17 +0000357function set_stuff_for_environment()
358{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800359 setpaths
360 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700361
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800362 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700363 # With this environment variable new GCC can apply colors to warnings/errors
364 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700365}
366
Matt Alexanderd9c56562020-05-21 10:49:17 +0000367function set_sequence_number()
368{
Colin Cross88737132017-03-21 17:41:03 -0700369 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700370}
371
Makoto Onukida971062018-06-18 10:15:19 -0700372# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
373function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800374 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700375 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800376 *:"$cmd":*)
377 return 1
378 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700379 esac
380 return 0
381}
382
Matt Alexanderd9c56562020-05-21 10:49:17 +0000383function addcompletions()
384{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700385 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700386
Jim Tanga881a252018-06-19 16:34:41 +0800387 # Keep us from trying to run in something that's neither bash nor zsh.
388 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700389 return
390 fi
391
392 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800393 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700394 return
395 fi
396
Jim Tanga881a252018-06-19 16:34:41 +0800397 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000398 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800399 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800400 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400401 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800402 )
Makoto Onukida971062018-06-18 10:15:19 -0700403 # Completion can be disabled selectively to allow users to use non-standard completion.
404 # e.g.
405 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
406 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400407 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800408 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400409 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000410 if [ ! -f "$f" ]; then
411 echo "Warning: completion file $f not found"
412 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700413 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700414 fi
415 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700416
Matt Alexanderd9c56562020-05-21 10:49:17 +0000417 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700418 complete -C "bit --tab" bit
419 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000420 if [ -z "$ZSH_VERSION" ]; then
421 # Doesn't work in zsh.
422 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400423 # TODO(b/244559459): Support b autocompletion for zsh
424 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000425 fi
Jim Tanga881a252018-06-19 16:34:41 +0800426 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800427
Cole Faust24c36db2021-01-23 02:39:37 +0000428 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100429 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000430 complete -F _complete_android_module_names outmod
431 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100432 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700433}
434
Joe Onorato824608c2022-04-08 11:06:16 -0700435function multitree_lunch_help()
436{
437 echo "usage: lunch PRODUCT-VARIANT" 1>&2
438 echo " Set up android build environment based on a product short name and variant" 1>&2
439 echo 1>&2
440 echo "lunch COMBO_FILE VARIANT" 1>&2
441 echo " Set up android build environment based on a specific lunch combo file" 1>&2
442 echo " and variant." 1>&2
443 echo 1>&2
444 echo "lunch --print [CONFIG]" 1>&2
445 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
446 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
447 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
448 echo 1>&2
449 echo "lunch --list" 1>&2
450 echo " List all possible combo files available in the current tree" 1>&2
451 echo 1>&2
452 echo "lunch --help" 1>&2
453 echo "lunch -h" 1>&2
454 echo " Prints this message." 1>&2
455}
456
457function multitree_lunch()
458{
459 local code
460 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000461 # Lunch must be run in the topdir, but this way we get a clear error
462 # message, instead of FileNotFound.
463 local T=$(multitree_gettop)
464 if [ -n "$T" ]; then
465 "$T/build/build/make/orchestrator/core/orchestrator.py" "$@"
466 else
467 _multitree_lunch_error
468 return 1
469 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700470 if $(echo "$1" | grep -q '^-') ; then
471 # Calls starting with a -- argument are passed directly and the function
472 # returns with the lunch.py exit code.
LaMont Jonesc39e5022022-06-23 19:09:06 +0000473 "${T}/build/build/make/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700474 code=$?
475 if [[ $code -eq 2 ]] ; then
476 echo 1>&2
477 multitree_lunch_help
478 return $code
479 elif [[ $code -ne 0 ]] ; then
480 return $code
481 fi
482 else
483 # All other calls go through the --lunch variant of lunch.py
LaMont Jonesc39e5022022-06-23 19:09:06 +0000484 results=($(${T}/build/build/make/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700485 code=$?
486 if [[ $code -eq 2 ]] ; then
487 echo 1>&2
488 multitree_lunch_help
489 return $code
490 elif [[ $code -ne 0 ]] ; then
491 return $code
492 fi
493
494 export TARGET_BUILD_COMBO=${results[0]}
495 export TARGET_BUILD_VARIANT=${results[1]}
496 fi
497}
498
Matt Alexanderd9c56562020-05-21 10:49:17 +0000499function choosetype()
500{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700501 echo "Build type choices are:"
502 echo " 1. release"
503 echo " 2. debug"
504 echo
505
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800506 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700507 DEFAULT_NUM=1
508 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800510 export TARGET_BUILD_TYPE=
511 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512 while [ -z $TARGET_BUILD_TYPE ]
513 do
514 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000515 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800516 read ANSWER
517 else
518 echo $1
519 ANSWER=$1
520 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700521 case $ANSWER in
522 "")
523 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
524 ;;
525 1)
526 export TARGET_BUILD_TYPE=release
527 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528 release)
529 export TARGET_BUILD_TYPE=release
530 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700531 2)
532 export TARGET_BUILD_TYPE=debug
533 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800534 debug)
535 export TARGET_BUILD_TYPE=debug
536 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700537 *)
538 echo
539 echo "I didn't understand your response. Please try again."
540 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700541 ;;
542 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000543 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800544 break
545 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546 done
547
Ying Wang08800fd2016-03-03 20:57:21 -0800548 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800550 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700551}
552
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800553#
554# This function isn't really right: It chooses a TARGET_PRODUCT
555# based on the list of boards. Usually, that gets you something
556# that kinda works with a generic product, but really, you should
557# pick a product by name.
558#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000559function chooseproduct()
560{
Christopher Ferris55257d22017-03-23 11:08:58 -0700561 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000562 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 default_value=$TARGET_PRODUCT
564 else
Ying Wang0a76df52015-06-08 11:57:26 -0700565 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 fi
567
Ying Wang08800fd2016-03-03 20:57:21 -0800568 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800569 export TARGET_PRODUCT=
570 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571 while [ -z "$TARGET_PRODUCT" ]
572 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700573 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000574 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575 read ANSWER
576 else
577 echo $1
578 ANSWER=$1
579 fi
580
Matt Alexanderd9c56562020-05-21 10:49:17 +0000581 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800583 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000584 if check_product $ANSWER
585 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 export TARGET_PRODUCT=$ANSWER
587 else
588 echo "** Not a valid product: $ANSWER"
589 fi
590 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000591 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700593 fi
594 done
595
Ying Wang08800fd2016-03-03 20:57:21 -0800596 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700597 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800598 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599}
600
Matt Alexanderd9c56562020-05-21 10:49:17 +0000601function choosevariant()
602{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 echo "Variant choices are:"
604 local index=1
605 local v
606 for v in ${VARIANT_CHOICES[@]}
607 do
608 # The product name is the name of the directory containing
609 # the makefile we found, above.
610 echo " $index. $v"
611 index=$(($index+1))
612 done
613
614 local default_value=eng
615 local ANSWER
616
617 export TARGET_BUILD_VARIANT=
618 while [ -z "$TARGET_BUILD_VARIANT" ]
619 do
620 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000621 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800622 read ANSWER
623 else
624 echo $1
625 ANSWER=$1
626 fi
627
Matt Alexanderd9c56562020-05-21 10:49:17 +0000628 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800629 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000630 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
631 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200632 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633 fi
634 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000635 if check_variant $ANSWER
636 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800637 export TARGET_BUILD_VARIANT=$ANSWER
638 else
639 echo "** Not a valid variant: $ANSWER"
640 fi
641 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000642 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800643 break
644 fi
645 done
646}
647
Matt Alexanderd9c56562020-05-21 10:49:17 +0000648function choosecombo()
649{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700650 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651
652 echo
653 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700654 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700655
656 echo
657 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700658 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800659
660 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800661 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800663 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800664 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665}
666
Matt Alexanderd9c56562020-05-21 10:49:17 +0000667function add_lunch_combo()
668{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800669 if [ -n "$ZSH_VERSION" ]; then
670 echo -n "${funcfiletrace[1]}: "
671 else
672 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
673 fi
674 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 -0800675}
676
Matt Alexanderd9c56562020-05-21 10:49:17 +0000677function print_lunch_menu()
678{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700679 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100680 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700681 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 +0100682 local ret=$?
683
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700684 echo
685 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700686 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100687
Matt Alexanderd9c56562020-05-21 10:49:17 +0000688 if [ $ret -ne 0 ]
689 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100690 echo "Warning: Cannot display lunch menu."
691 echo
692 echo "Note: You can invoke lunch with an explicit target:"
693 echo
694 echo " usage: lunch [target]" >&2
695 echo
696 return
697 fi
698
Will Burr40401202022-02-07 12:12:01 +0000699 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800700
701 local i=1
702 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700703 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800704 do
705 echo " $i. $choice"
706 i=$(($i+1))
707 done
708
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700709 echo
710}
711
Matt Alexanderd9c56562020-05-21 10:49:17 +0000712function lunch()
713{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800714 local answer
715
Steven Moreland92793dc2020-02-25 18:30:18 -0800716 if [[ $# -gt 1 ]]; then
717 echo "usage: lunch [target]" >&2
718 return 1
719 fi
720
Will Burr40401202022-02-07 12:12:01 +0000721 local used_lunch_menu=0
722
Steven Moreland92793dc2020-02-25 18:30:18 -0800723 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800724 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700725 else
726 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000727 echo "Which would you like? [aosp_arm-eng]"
728 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 -0800729 read answer
Will Burr40401202022-02-07 12:12:01 +0000730 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700731 fi
732
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800733 local selection=
734
Matt Alexanderd9c56562020-05-21 10:49:17 +0000735 if [ -z "$answer" ]
736 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700737 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000738 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
739 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800740 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000741 if [ $answer -le ${#choices[@]} ]
742 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800743 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000744 if [ -n "$ZSH_VERSION" ]
745 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800746 selection=${choices[$(($answer))]}
747 else
748 selection=${choices[$(($answer-1))]}
749 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800750 fi
Colin Cross88737132017-03-21 17:41:03 -0700751 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800752 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700753 fi
754
Joe Onoratoda12daf2010-06-09 18:18:31 -0700755 export TARGET_BUILD_APPS=
756
Colin Cross88737132017-03-21 17:41:03 -0700757 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700758 product=${selection%%-*} # Trim everything after first dash
759 variant_and_version=${selection#*-} # Trim everything up to first dash
760 if [ "$variant_and_version" != "$selection" ]; then
761 variant=${variant_and_version%%-*}
762 if [ "$variant" != "$variant_and_version" ]; then
763 version=${variant_and_version#*-}
764 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700765 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800766
Matt Alexanderd9c56562020-05-21 10:49:17 +0000767 if [ -z "$product" ]
768 then
Ying Wang08800fd2016-03-03 20:57:21 -0800769 echo
Colin Cross88737132017-03-21 17:41:03 -0700770 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700771 return 1
772 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800773
Colin Cross88737132017-03-21 17:41:03 -0700774 TARGET_PRODUCT=$product \
775 TARGET_BUILD_VARIANT=$variant \
776 TARGET_PLATFORM_VERSION=$version \
777 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000778 if [ $? -ne 0 ]
779 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100780 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
781 then
782 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
783 fi
Colin Cross88737132017-03-21 17:41:03 -0700784 return 1
785 fi
Colin Cross88737132017-03-21 17:41:03 -0700786 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
787 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700788 if [ -n "$version" ]; then
789 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
790 else
791 unset TARGET_PLATFORM_VERSION
792 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700793 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700794
Will Burr40401202022-02-07 12:12:01 +0000795 if [ $used_lunch_menu -eq 1 ]; then
796 echo
797 echo "Hint: next time you can simply run 'lunch $selection'"
798 fi
799
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700800 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800801
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700802 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700803 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800804 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400805
806 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
807 check_mu_config
808 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700809}
810
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700811unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700812# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000813function _lunch()
814{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700815 local cur prev opts
816 COMPREPLY=()
817 cur="${COMP_WORDS[COMP_CWORD]}"
818 prev="${COMP_WORDS[COMP_CWORD-1]}"
819
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700820 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800821 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700822 fi
823
824 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700825 return 0
826}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700827
Joe Onoratoda12daf2010-06-09 18:18:31 -0700828# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700829# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000830function tapas()
831{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700832 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800833 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700834 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700835 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 -0800836 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
837 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)"
838
Joe Onoratoda12daf2010-06-09 18:18:31 -0700839
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700840 if [ "$showHelp" != "" ]; then
841 $(gettop)/build/make/tapasHelp.sh
842 return
843 fi
844
Ying Wang67f02922012-08-22 10:25:20 -0700845 if [ $(echo $arch | wc -w) -gt 1 ]; then
846 echo "tapas: Error: Multiple build archs supplied: $arch"
847 return
848 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700849 if [ $(echo $variant | wc -w) -gt 1 ]; then
850 echo "tapas: Error: Multiple build variants supplied: $variant"
851 return
852 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700853 if [ $(echo $density | wc -w) -gt 1 ]; then
854 echo "tapas: Error: Multiple densities supplied: $density"
855 return
856 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800857 if [ $(echo $keys | wc -w) -gt 1 ]; then
858 echo "tapas: Error: Multiple keys supplied: $keys"
859 return
860 fi
Ying Wang67f02922012-08-22 10:25:20 -0700861
Ying Wang0a76df52015-06-08 11:57:26 -0700862 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700863 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000864 x86) product=aosp_x86;;
865 arm64) product=aosp_arm64;;
866 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700867 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800868 if [ -n "$keys" ]; then
869 product=${product/aosp_/aosp_${keys}_}
870 fi;
871
Joe Onoratoda12daf2010-06-09 18:18:31 -0700872 if [ -z "$variant" ]; then
873 variant=eng
874 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700875 if [ -z "$apps" ]; then
876 apps=all
877 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600878 if [ -z "$density" ]; then
879 density=alldpi
880 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700881
Ying Wang67f02922012-08-22 10:25:20 -0700882 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700883 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700884 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700885 export TARGET_BUILD_TYPE=release
886 export TARGET_BUILD_APPS=$apps
887
Ying Wang08800fd2016-03-03 20:57:21 -0800888 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700889 set_stuff_for_environment
890 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800891 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700892}
893
Martin Stjernholmf692c752021-04-12 00:01:10 +0100894# Configures the build to build unbundled Android modules (APEXes).
895# Run banchan with one or more module names (from apex{} modules).
896function banchan()
897{
898 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100899 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100900 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100901 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 +0100902
903 if [ "$showHelp" != "" ]; then
904 $(gettop)/build/make/banchanHelp.sh
905 return
906 fi
907
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100908 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000909 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100910 elif [ $(echo $product | wc -w) -gt 1 ]; then
911 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100912 return
913 fi
914 if [ $(echo $variant | wc -w) -gt 1 ]; then
915 echo "banchan: Error: Multiple build variants supplied: $variant"
916 return
917 fi
918 if [ -z "$apps" ]; then
919 echo "banchan: Error: No modules supplied"
920 return
921 fi
922
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100923 case $product in
924 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100925 x86) product=module_x86;;
926 arm64) product=module_arm64;;
927 x86_64) product=module_x86_64;;
928 esac
929 if [ -z "$variant" ]; then
930 variant=eng
931 fi
932
933 export TARGET_PRODUCT=$product
934 export TARGET_BUILD_VARIANT=$variant
935 export TARGET_BUILD_DENSITY=alldpi
936 export TARGET_BUILD_TYPE=release
937
938 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
939 # case is different and it may diverge in the future.
940 export TARGET_BUILD_APPS=$apps
941
942 build_build_var_cache
943 set_stuff_for_environment
944 printconfig
945 destroy_build_var_cache
946}
947
Matt Alexanderd9c56562020-05-21 10:49:17 +0000948function gettop
949{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700950 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000951 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700952 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000953 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700954 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000955 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800956 # The following circumlocution (repeated below as well) ensures
957 # that we record the true directory name and not one that is
958 # faked up with symlink names.
959 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700960 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800961 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700962 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000963 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800964 \cd ..
synergyb112a402013-07-05 19:47:47 -0700965 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700966 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000967 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700968 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000969 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700970 fi
971 fi
972 fi
973}
974
Joe Onorato7cf6f972022-05-11 21:39:57 -0700975# TODO: Merge into gettop as part of launching multitree
976function multitree_gettop
977{
978 local TOPFILE=build/build/make/core/envsetup.mk
979 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
980 # The following circumlocution ensures we remove symlinks from TOP.
981 (cd "$TOP"; PWD= /bin/pwd)
982 else
983 if [ -f $TOPFILE ] ; then
984 # The following circumlocution (repeated below as well) ensures
985 # that we record the true directory name and not one that is
986 # faked up with symlink names.
987 PWD= /bin/pwd
988 else
989 local HERE=$PWD
990 local T=
991 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
992 \cd ..
993 T=`PWD= /bin/pwd -P`
994 done
995 \cd "$HERE"
996 if [ -f "$T/$TOPFILE" ]; then
997 echo "$T"
998 fi
999 fi
1000 fi
1001}
1002
Matt Alexanderd9c56562020-05-21 10:49:17 +00001003function croot()
1004{
Christopher Ferris55257d22017-03-23 11:08:58 -07001005 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001006 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001007 if [ "$1" ]; then
1008 \cd $(gettop)/$1
1009 else
1010 \cd $(gettop)
1011 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001012 else
1013 echo "Couldn't locate the top of the tree. Try setting TOP."
1014 fi
1015}
1016
Matt Alexanderd9c56562020-05-21 10:49:17 +00001017function _croot()
1018{
Anton Hanssonece9c482019-02-04 18:15:39 +00001019 local T=$(gettop)
1020 if [ "$T" ]; then
1021 local cur="${COMP_WORDS[COMP_CWORD]}"
1022 k=0
1023 for c in $(compgen -d ${T}/${cur}); do
1024 COMPREPLY[k++]=${c#${T}/}/
1025 done
1026 fi
1027}
1028
Matt Alexanderd9c56562020-05-21 10:49:17 +00001029function cproj()
1030{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001031 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001032 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001033 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001034 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1035 T=$PWD
1036 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001037 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001038 return
1039 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001040 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001041 done
Ying Wang9cd17642012-12-13 10:52:07 -08001042 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001043 echo "can't find Android.mk"
1044}
1045
Daniel Sandler47e0a882013-07-30 13:23:52 -04001046# simplified version of ps; output in the form
1047# <pid> <procname>
1048function qpid() {
1049 local prepend=''
1050 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001051 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001052 prepend=' '
1053 append='$'
1054 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001055 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001056 echo "usage: qpid [[--exact] <process name|pid>"
1057 return 255
1058 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001059
1060 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001062 qpid | \grep "$prepend$EXE$append"
1063 else
1064 adb shell ps \
1065 | tr -d '\r' \
1066 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1067 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001068}
1069
Steven Moreland74114f12020-09-10 01:23:32 +00001070# syswrite - disable verity, reboot if needed, and remount image
1071#
1072# Easy way to make system.img/etc writable
1073function syswrite() {
1074 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001075 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001076 echo "rebooting"
1077 adb reboot && adb wait-for-device && adb root || return 1
1078 fi
1079 adb wait-for-device && adb remount || return 1
1080}
1081
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001082# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001083# that has the core-file-size limit set correctly
1084#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001085# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001086# if its core-file-size limit is not set already.
1087# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1088
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089function coredump_setup()
1090{
1091 echo "Getting root...";
1092 adb root;
1093 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001094
Matt Alexanderd9c56562020-05-21 10:49:17 +00001095 echo "Remounting root partition read-write...";
1096 adb shell mount -w -o remount -t rootfs rootfs;
1097 sleep 1;
1098 adb wait-for-device;
1099 adb shell mkdir -p /cores;
1100 adb shell mount -t tmpfs tmpfs /cores;
1101 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001102
Matt Alexanderd9c56562020-05-21 10:49:17 +00001103 echo "Granting SELinux permission to dump in /cores...";
1104 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001105
Matt Alexanderd9c56562020-05-21 10:49:17 +00001106 echo "Set core pattern.";
1107 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001108
Ying Wang08800fd2016-03-03 20:57:21 -08001109 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001110}
1111
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001112# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001113# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001115# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116# dump to actually be generated.
1117
Matt Alexanderd9c56562020-05-21 10:49:17 +00001118function coredump_enable()
1119{
1120 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001121 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122 printf "Expecting a PID!\n";
1123 return;
1124 fi;
1125 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001126 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001127}
1128
1129# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001130# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001131#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001132# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001133# enabled globally.
1134
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135function core()
1136{
1137 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001138
Ying Wang08800fd2016-03-03 20:57:21 -08001139 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140 printf "Expecting a PID!\n";
1141 return;
1142 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001143
Matt Alexanderd9c56562020-05-21 10:49:17 +00001144 local CORENAME=core.$PID;
1145 local COREPATH=/cores/$CORENAME;
1146 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001147
Matt Alexanderd9c56562020-05-21 10:49:17 +00001148 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001149
Matt Alexanderd9c56562020-05-21 10:49:17 +00001150 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001151 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001152 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1153 adb shell kill -$SIG $PID;
1154 sleep 1;
1155 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001156
Matt Alexanderd9c56562020-05-21 10:49:17 +00001157 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1158 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001159}
1160
Christopher Tate744ee802009-11-12 15:33:08 -08001161# systemstack - dump the current stack trace of all threads in the system process
1162# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001163function systemstack()
1164{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001165 stacks system_server
1166}
1167
Michael Wrightaeed7212014-06-19 19:58:12 -07001168# Read the ELF header from /proc/$PID/exe to determine if the process is
1169# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001170function is64bit()
1171{
Ben Chengfba67bf2014-02-25 10:27:07 -08001172 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001173 if [ "$PID" ] ; then
1174 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001175 echo "64"
1176 else
1177 echo ""
1178 fi
1179 else
1180 echo ""
1181 fi
1182}
1183
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001184case `uname -s` in
1185 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001186 function sgrep()
1187 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001188 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 -04001189 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001191
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001192 ;;
1193 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001194 function sgrep()
1195 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001196 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 -04001197 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001198 }
1199 ;;
1200esac
1201
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202function gettargetarch
1203{
Raghu Gandham8da43102012-07-25 19:57:22 -07001204 get_build_var TARGET_ARCH
1205}
1206
Matt Alexanderd9c56562020-05-21 10:49:17 +00001207function ggrep()
1208{
Mike Frysinger5e479732015-09-22 18:13:48 -04001209 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1210 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001211}
1212
Matt Alexanderd9c56562020-05-21 10:49:17 +00001213function gogrep()
1214{
Orion Hodson831472d2019-10-25 11:35:15 +01001215 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1216 -exec grep --color -n "$@" {} +
1217}
1218
Matt Alexanderd9c56562020-05-21 10:49:17 +00001219function jgrep()
1220{
Mike Frysinger5e479732015-09-22 18:13:48 -04001221 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1222 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223}
1224
Alistair Delva176e5342021-02-22 13:31:26 -08001225function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001226{
1227 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1228 -exec grep --color -n "$@" {} +
1229}
1230
Taesu Leeea0cecd2020-10-28 11:05:18 +09001231function ktgrep()
1232{
1233 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1234 -exec grep --color -n "$@" {} +
1235}
1236
Matt Alexanderd9c56562020-05-21 10:49:17 +00001237function cgrep()
1238{
Mike Frysinger5e479732015-09-22 18:13:48 -04001239 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' \) \
1240 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001241}
1242
Matt Alexanderd9c56562020-05-21 10:49:17 +00001243function resgrep()
1244{
Christopher Ferris55257d22017-03-23 11:08:58 -07001245 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001246 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1247 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1248 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function mangrep()
1252{
Mike Frysinger5e479732015-09-22 18:13:48 -04001253 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1254 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001255}
1256
Matt Alexanderd9c56562020-05-21 10:49:17 +00001257function owngrep()
1258{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001259 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1260 -exec grep --color -n "$@" {} +
1261}
1262
Matt Alexanderd9c56562020-05-21 10:49:17 +00001263function sepgrep()
1264{
Mike Frysinger5e479732015-09-22 18:13:48 -04001265 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1266 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001267}
1268
Matt Alexanderd9c56562020-05-21 10:49:17 +00001269function rcgrep()
1270{
Mike Frysinger5e479732015-09-22 18:13:48 -04001271 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1272 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001273}
1274
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001275case `uname -s` in
1276 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001277 function mgrep()
1278 {
Orion Hodson831472d2019-10-25 11:35:15 +01001279 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 -04001280 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001281 }
1282
Matt Alexanderd9c56562020-05-21 10:49:17 +00001283 function treegrep()
1284 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001285 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 -04001286 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001287 }
1288
1289 ;;
1290 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001291 function mgrep()
1292 {
Orion Hodson831472d2019-10-25 11:35:15 +01001293 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 -04001294 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001295 }
1296
Matt Alexanderd9c56562020-05-21 10:49:17 +00001297 function treegrep()
1298 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001299 find . -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 -04001300 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301 }
1302
1303 ;;
1304esac
1305
Matt Alexanderd9c56562020-05-21 10:49:17 +00001306function getprebuilt
1307{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001308 get_abs_build_var ANDROID_PREBUILTS
1309}
1310
Matt Alexanderd9c56562020-05-21 10:49:17 +00001311function tracedmdump()
1312{
Christopher Ferris55257d22017-03-23 11:08:58 -07001313 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001314 if [ ! "$T" ]; then
1315 echo "Couldn't locate the top of the tree. Try setting TOP."
1316 return
1317 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001318 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001319 local arch=$(gettargetarch)
1320 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001322 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001323 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001324 echo "usage: tracedmdump tracename"
1325 return
1326 fi
1327
Matt Alexanderd9c56562020-05-21 10:49:17 +00001328 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001329 echo "Error: cannot find kernel: '$KERNEL'"
1330 return
1331 fi
1332
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001333 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001334 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001335 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1336 fi
1337
1338 echo "post-processing traces..."
1339 rm -f $TRACE/qtrace.dexlist
1340 post_trace $TRACE
1341 if [ $? -ne 0 ]; then
1342 echo "***"
1343 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1344 echo "***"
1345 return
1346 fi
1347 echo "generating dexlist output..."
1348 /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
1349 echo "generating dmtrace data..."
1350 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1351 echo "generating html file..."
1352 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1353 echo "done, see $TRACE/dmtrace.html for details"
1354 echo "or run:"
1355 echo " traceview $TRACE/dmtrace"
1356}
1357
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001358# communicate with a running device or emulator, set up necessary state,
1359# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001360function runhat()
1361{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001362 # process standard adb options
1363 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001364 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001365 adbTarget=$1
1366 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001367 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001368 adbTarget="$1 $2"
1369 shift 2
1370 fi
1371 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001372 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001373
1374 # runhat options
1375 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001376
Matt Alexanderd9c56562020-05-21 10:49:17 +00001377 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001378 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001379 return
1380 fi
1381
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001382 # confirm hat is available
1383 if [ -z $(which hat) ]; then
1384 echo "hat is not available in this configuration."
1385 return
1386 fi
1387
Andy McFaddenb6289852010-07-12 08:00:19 -07001388 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001389 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001390 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001391 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001392 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001393 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394 echo -n "> "
1395 read
1396
The Android Open Source Project88b60792009-03-03 19:28:42 -08001397 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001398
The Android Open Source Project88b60792009-03-03 19:28:42 -08001399 echo "Retrieving file $devFile..."
1400 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001401
The Android Open Source Project88b60792009-03-03 19:28:42 -08001402 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001403
The Android Open Source Project88b60792009-03-03 19:28:42 -08001404 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001405 echo "View the output by pointing your browser at http://localhost:7000/"
1406 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001407 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001408}
1409
Matt Alexanderd9c56562020-05-21 10:49:17 +00001410function getbugreports()
1411{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001412 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001413
1414 if [ ! "$reports" ]; then
1415 echo "Could not locate any bugreports."
1416 return
1417 fi
1418
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001419 local report
1420 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001421 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001422 echo "/sdcard/bugreports/${report}"
1423 adb pull /sdcard/bugreports/${report} ${report}
1424 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001425 done
1426}
1427
Matt Alexanderd9c56562020-05-21 10:49:17 +00001428function getsdcardpath()
1429{
Victoria Lease1b296b42012-08-21 15:44:06 -07001430 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1431}
1432
Matt Alexanderd9c56562020-05-21 10:49:17 +00001433function getscreenshotpath()
1434{
Victoria Lease1b296b42012-08-21 15:44:06 -07001435 echo "$(getsdcardpath)/Pictures/Screenshots"
1436}
1437
Matt Alexanderd9c56562020-05-21 10:49:17 +00001438function getlastscreenshot()
1439{
Victoria Lease1b296b42012-08-21 15:44:06 -07001440 local screenshot_path=$(getscreenshotpath)
1441 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 +00001442 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001443 echo "No screenshots found."
1444 return
1445 fi
1446 echo "${screenshot}"
1447 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1448}
1449
Matt Alexanderd9c56562020-05-21 10:49:17 +00001450function startviewserver()
1451{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001452 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001453 if [ $# -gt 0 ]; then
1454 port=$1
1455 fi
1456 adb shell service call window 1 i32 $port
1457}
1458
Matt Alexanderd9c56562020-05-21 10:49:17 +00001459function stopviewserver()
1460{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001461 adb shell service call window 2
1462}
1463
Matt Alexanderd9c56562020-05-21 10:49:17 +00001464function isviewserverstarted()
1465{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001466 adb shell service call window 3
1467}
1468
Matt Alexanderd9c56562020-05-21 10:49:17 +00001469function key_home()
1470{
Romain Guyb84049a2010-10-04 16:56:11 -07001471 adb shell input keyevent 3
1472}
1473
Matt Alexanderd9c56562020-05-21 10:49:17 +00001474function key_back()
1475{
Romain Guyb84049a2010-10-04 16:56:11 -07001476 adb shell input keyevent 4
1477}
1478
Matt Alexanderd9c56562020-05-21 10:49:17 +00001479function key_menu()
1480{
Romain Guyb84049a2010-10-04 16:56:11 -07001481 adb shell input keyevent 82
1482}
1483
Matt Alexanderd9c56562020-05-21 10:49:17 +00001484function smoketest()
1485{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001486 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1487 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1488 return
1489 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001490 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001491 if [ ! "$T" ]; then
1492 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1493 return
1494 fi
1495
Ying Wang9cd17642012-12-13 10:52:07 -08001496 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001497 adb uninstall com.android.smoketest > /dev/null &&
1498 adb uninstall com.android.smoketest.tests > /dev/null &&
1499 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1500 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1501 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1502}
1503
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001504# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001505function runtest()
1506{
Christopher Ferris55257d22017-03-23 11:08:58 -07001507 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001508 if [ ! "$T" ]; then
1509 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1510 return
1511 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001512 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001513}
1514
The Android Open Source Project88b60792009-03-03 19:28:42 -08001515function godir () {
1516 if [[ -z "$1" ]]; then
1517 echo "Usage: godir <regex>"
1518 return
1519 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001520 local T=$(gettop)
1521 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001522 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001523 mkdir -p $OUT_DIR
1524 FILELIST=$OUT_DIR/filelist
1525 else
1526 FILELIST=$T/filelist
1527 fi
1528 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001529 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001530 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001531 echo " Done"
1532 echo ""
1533 fi
1534 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001535 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001536 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001537 echo "Not found"
1538 return
1539 fi
1540 local pathname
1541 local choice
1542 if [[ ${#lines[@]} > 1 ]]; then
1543 while [[ -z "$pathname" ]]; do
1544 local index=1
1545 local line
1546 for line in ${lines[@]}; do
1547 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001548 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001549 done
1550 echo
1551 echo -n "Select one: "
1552 unset choice
1553 read choice
1554 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1555 echo "Invalid choice"
1556 continue
1557 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001558 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001559 done
1560 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001561 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001562 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001563 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001564}
1565
Steven Moreland62054a42018-12-06 10:11:40 -08001566# Update module-info.json in out.
1567function refreshmod() {
1568 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1569 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1570 return 1
1571 fi
1572
1573 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1574
1575 # for the output of the next command
1576 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1577
1578 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001579 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001580 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1581}
1582
Cole Faust3e192382021-10-25 13:29:15 -07001583# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001584function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001585 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001586 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1587 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1588 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001589 return 1
1590 fi
1591
1592 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001593 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001594 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001595 fi
1596 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001597 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001598}
1599
1600# List all modules for the current device, as cached in module-info.json. If any build change is
1601# made and it should be reflected in the output, you should run 'refreshmod' first.
1602function allmod() {
1603 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001604
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001605 python3 -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
Steven Moreland62054a42018-12-06 10:11:40 -08001606}
1607
Joe Onorato2c1aa472021-02-25 16:42:39 -08001608# Get the path of a specific module in the android tree, as cached in module-info.json.
1609# If any build change is made, and it should be reflected in the output, you should run
1610# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001611function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001612 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001613 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001614 return 1
1615 fi
1616
Cole Faust24c36db2021-01-23 02:39:37 +00001617 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001618
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001619 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001620module = '$1'
1621module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1622if module not in module_info:
1623 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001624print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001625
1626 if [ -z "$relpath" ]; then
1627 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1628 return 1
1629 else
Rett Berg78d1c932019-01-24 14:34:23 -08001630 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001631 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 pathmod.
1637function dirmods() {
1638 if [[ $# -ne 1 ]]; then
1639 echo "usage: dirmods <path>" >&2
1640 return 1
1641 fi
1642
1643 verifymodinfo || return 1
1644
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001645 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001646dir = '$1'
1647while dir.endswith('/'):
1648 dir = dir[:-1]
1649prefix = dir + '/'
1650module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1651results = set()
1652for m in module_info.values():
1653 for path in m.get(u'path', []):
1654 if path == dir or path.startswith(prefix):
1655 name = m.get(u'module_name')
1656 if name:
1657 results.add(name)
1658for name in sorted(results):
1659 print(name)
1660"
1661}
1662
1663
Rett Berg78d1c932019-01-24 14:34:23 -08001664# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1665# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1666function gomod() {
1667 if [[ $# -ne 1 ]]; then
1668 echo "usage: gomod <module>" >&2
1669 return 1
1670 fi
1671
1672 local path="$(pathmod $@)"
1673 if [ -z "$path" ]; then
1674 return 1
1675 fi
1676 cd $path
1677}
1678
Cole Faust24c36db2021-01-23 02:39:37 +00001679# Gets the list of a module's installed outputs, as cached in module-info.json.
1680# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1681function outmod() {
1682 if [[ $# -ne 1 ]]; then
1683 echo "usage: outmod <module>" >&2
1684 return 1
1685 fi
1686
1687 verifymodinfo || return 1
1688
1689 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001690 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001691module = '$1'
1692module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1693if module not in module_info:
1694 exit(1)
1695for output in module_info[module]['installed']:
1696 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1697
1698 if [ $? -ne 0 ]; then
1699 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1700 return 1
1701 elif [ ! -z "$relpath" ]; then
1702 echo "$relpath"
1703 fi
1704}
1705
1706# adb install a module's apk, as cached in module-info.json. If any build change
1707# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1708# Usage: installmod [adb install arguments] <module>
1709# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1710function installmod() {
1711 if [[ $# -eq 0 ]]; then
1712 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001713 echo "" >&2
1714 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1715 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1716 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001717 return 1
1718 fi
1719
1720 local _path
1721 _path=$(outmod ${@:$#:1})
1722 if [ $? -ne 0 ]; then
1723 return 1
1724 fi
1725
1726 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1727 if [ -z "$_path" ]; then
1728 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1729 return 1
1730 fi
Cole Faust3e192382021-10-25 13:29:15 -07001731 local serial_device=""
1732 if [[ "$1" == "-s" ]]; then
1733 if [[ $# -le 2 ]]; then
1734 echo "-s requires an argument" >&2
1735 return 1
1736 fi
1737 serial_device="-s $2"
1738 shift 2
1739 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001740 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001741 echo adb $serial_device install ${@:1:$length} $_path
1742 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001743}
1744
dimitry73b84812018-12-11 18:06:00 +01001745function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001746 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001747 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001748}
1749
Alex Rayf0d08eb2013-03-08 15:15:06 -08001750# Print colored exit condition
1751function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001752 "$@"
1753 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001754 if [ $retval -ne 0 ]
1755 then
Jacky Cao89483b82015-05-15 22:12:53 +08001756 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001757 else
Jacky Cao89483b82015-05-15 22:12:53 +08001758 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001759 fi
1760 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001761}
1762
Matt Alexanderd9c56562020-05-21 10:49:17 +00001763function get_make_command()
1764{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001765 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1766 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001767 # Always use the real make if -C is passed in
1768 for arg in "$@"; do
1769 if [[ $arg == -C* ]]; then
1770 echo command make
1771 return
1772 fi
1773 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001774 echo build/soong/soong_ui.bash --make-mode
1775 else
1776 echo command make
1777 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001778}
1779
Matt Alexanderd9c56562020-05-21 10:49:17 +00001780function _wrap_build()
1781{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001782 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1783 "$@"
1784 return $?
1785 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001786 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001787 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001788 local ret=$?
1789 local end_time=$(date +"%s")
1790 local tdiff=$(($end_time-$start_time))
1791 local hours=$(($tdiff / 3600 ))
1792 local mins=$((($tdiff % 3600) / 60))
1793 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001794 local ncolors=$(tput colors 2>/dev/null)
1795 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001796 color_failed=$'\E'"[0;31m"
1797 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001798 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001799 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001800 else
1801 color_failed=""
1802 color_success=""
1803 color_reset=""
1804 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001805
1806 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1807 echo
1808 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1809 fi
1810
Ed Heylcc6be0a2014-06-18 14:55:58 -07001811 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001812 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001813 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001814 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001815 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001816 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001817 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001818 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001819 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001820 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001821 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001822 printf "(%s seconds)" $secs
1823 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001824 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001825 echo
1826 return $ret
1827}
1828
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001829function _trigger_build()
1830(
1831 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001832 local T=$(gettop)
1833 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001834 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1835 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001836 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1837 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001838 fi
1839)
1840
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001841# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001842function b()
1843(
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001844 # Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
1845 local bazel_args=""
1846 local skip_tests="--skip-soong-tests"
1847 for i in $@; do
1848 if [[ $i != "--run-soong-tests" ]]; then
1849 bazel_args+="$i "
1850 else
1851 skip_tests=""
1852 fi
1853 done
Jingwen Chend728ee12021-05-18 06:02:53 +00001854 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Jingwen Chen28a22982022-08-31 09:47:33 +00001855 _trigger_build "all-modules" bp2build $skip_tests USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001856 # Then, run Bazel using the synthetic workspace as the --package_path.
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001857 if [[ -z "$bazel_args" ]]; then
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001858 # If there are no args, show help.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001859 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001860 else
1861 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Cole Fausta68f5162022-08-18 17:33:51 -07001862 # Add the --config=bp2build after the first argument that doesn't start with a dash. That should be the bazel
1863 # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work with commands like:
1864 # b run //foo -- --args-for-foo
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001865 local config_set=0
1866 local bazel_args_with_config=""
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001867 for arg in $bazel_args; do
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001868 if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
Cole Fausta68f5162022-08-18 17:33:51 -07001869 then
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001870 bazel_args_with_config+="--config=bp2build -- "
1871 config_set=1
1872 else
1873 bazel_args_with_config+="$arg "
Cole Fausta68f5162022-08-18 17:33:51 -07001874 fi
1875 done
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001876 if [[ $config_set -ne 1 ]]; then
1877 bazel_args_with_config+="--config=bp2build "
1878 fi
Jingwen Chen1ea787a2022-09-01 05:56:44 +00001879 bazel $bazel_args_with_config
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001880 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001881)
1882
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001883function m()
1884(
1885 _trigger_build "all-modules" "$@"
1886)
1887
1888function mm()
1889(
1890 _trigger_build "modules-in-a-dir-no-deps" "$@"
1891)
1892
1893function mmm()
1894(
1895 _trigger_build "modules-in-dirs-no-deps" "$@"
1896)
1897
1898function mma()
1899(
1900 _trigger_build "modules-in-a-dir" "$@"
1901)
1902
1903function mmma()
1904(
1905 _trigger_build "modules-in-dirs" "$@"
1906)
1907
Matt Alexanderd9c56562020-05-21 10:49:17 +00001908function make()
1909{
Dan Willemsene9842242017-07-28 13:00:13 -07001910 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001911}
1912
Joe Onorato7cf6f972022-05-11 21:39:57 -07001913function _multitree_lunch_error()
1914{
1915 >&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."
1916}
1917
1918function multitree_build()
1919{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001920 local T=$(multitree_gettop)
1921 if [ -n "$T" ]; then
1922 "$T/build/build/make/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001923 else
1924 _multitree_lunch_error
1925 return 1
1926 fi
1927}
1928
Matt Alexanderd9c56562020-05-21 10:49:17 +00001929function provision()
1930{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001931 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1932 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1933 return 1
1934 fi
1935 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1936 echo "There is no provisioning script for the device." >&2
1937 return 1
1938 fi
1939
1940 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001941 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001942 shift 1
1943 else
1944 echo "This action will reflash your device."
1945 echo ""
1946 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1947 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001948 echo -n "Are you sure you want to do this (yes/no)? "
1949 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001950 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001951 echo "Not taking any action. Exiting." >&2
1952 return 1
1953 fi
1954 fi
1955 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1956}
1957
Jim Tanga881a252018-06-19 16:34:41 +08001958# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001959function enable_zsh_completion() {
1960 # Don't override user's options if bash-style completion is already enabled.
1961 if ! declare -f complete >/dev/null; then
1962 autoload -U compinit && compinit
1963 autoload -U bashcompinit && bashcompinit
1964 fi
Jim Tanga881a252018-06-19 16:34:41 +08001965}
1966
1967function validate_current_shell() {
1968 local current_sh="$(ps -o command -p $$)"
1969 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001970 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001971 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001972 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001973 *zsh*)
1974 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001975 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001976 *)
Jim Tanga881a252018-06-19 16:34:41 +08001977 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 -07001978 ;;
1979 esac
Jim Tanga881a252018-06-19 16:34:41 +08001980}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001981
1982# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001983# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1984# load those.
1985#
1986# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001987function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001988 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001989 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001990 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001991 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 -08001992 if [ -n "$allowed" ]; then
1993 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1994 echo " $allowed"
1995 echo " $f"
1996 return
1997 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001998 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001999 done
2000
2001 allowed_files=
2002 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002003 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002004 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002005 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002006
2007 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002008 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002009 else
2010 echo "ignoring $f, not in $allowed"
2011 fi
Jim Tanga881a252018-06-19 16:34:41 +08002012 done
2013 done
2014}
Kenny Root52aa81c2011-07-15 11:07:06 -07002015
Dan Albertbab814f2020-08-26 15:34:53 -07002016function showcommands() {
2017 local T=$(gettop)
2018 if [[ -z "$TARGET_PRODUCT" ]]; then
2019 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2020 return
2021 fi
2022 case $(uname -s) in
2023 Darwin)
2024 PREBUILT_NAME=darwin-x86
2025 ;;
2026 Linux)
2027 PREBUILT_NAME=linux-x86
2028 ;;
2029 *)
2030 >&2 echo Unknown host $(uname -s)
2031 return
2032 ;;
2033 esac
2034 if [[ -z "$OUT_DIR" ]]; then
2035 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
2036 OUT_DIR=out
2037 else
2038 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
2039 fi
2040 fi
2041 if [[ "$1" == "--regenerate" ]]; then
2042 shift 1
2043 NINJA_ARGS="-t commands $@" m
2044 else
2045 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2046 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2047 -t commands "$@")
2048 fi
2049}
2050
Cole Faust45844ab2022-08-30 13:59:07 -07002051function avbtool() {
2052 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2053 m avbtool
2054 fi
2055 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2056}
2057
Jim Tanga881a252018-06-19 16:34:41 +08002058validate_current_shell
2059source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002060addcompletions