blob: a8b224d55eb62a3ab96243399b035541f06a43e0 [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
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700243 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
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
255 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
256 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
257 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700258 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
259
260 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
261 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
262 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200263
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800264 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
265 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
266
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200267 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
268 # to ensure that the corresponding 'emulator' binaries are used.
269 case $(uname -s) in
270 Darwin)
271 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
272 ;;
273 Linux)
274 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
275 ;;
276 *)
277 ANDROID_EMULATOR_PREBUILTS=
278 ;;
279 esac
280 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700281 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200282 export ANDROID_EMULATOR_PREBUILTS
283 fi
284
Jim Tangb3fda302018-12-22 10:24:55 +0800285 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
286 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700287 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
288 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
289 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
290 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800291
Ryan Prichard8426a192019-07-15 18:05:29 -0700292 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800293
294 # out with the duplicate old
295 if [ -n $ANDROID_PYTHONPATH ]; then
296 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
297 fi
298 # and in with the new
299 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800300 if [ -n $VENDOR_PYTHONPATH ]; then
301 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
302 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800303 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800304
Colin Crosse97e6932017-06-30 16:01:45 -0700305 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
306 export JAVA_HOME=$ANDROID_JAVA_HOME
307 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
308 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
309 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500310
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800311 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700312 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
313 export OUT=$ANDROID_PRODUCT_OUT
314
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700315 unset ANDROID_HOST_OUT
316 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
317
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900318 unset ANDROID_SOONG_HOST_OUT
319 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
320
Simran Basidd050ed2017-02-13 13:46:48 -0800321 unset ANDROID_HOST_OUT_TESTCASES
322 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
323
324 unset ANDROID_TARGET_OUT_TESTCASES
325 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
326
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800327 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328 # TODO: fix the path
329 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
330}
331
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000332function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000333{
334 local T="$(gettop)"
335 if [ ! "$T" ]; then
336 echo "Couldn't locate the top of the tree. Try setting TOP."
337 return
338 fi
339
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000340 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500341 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
342 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000343 fi
344
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000345 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000346}
347
Matt Alexanderd9c56562020-05-21 10:49:17 +0000348function printconfig()
349{
Christopher Ferris55257d22017-03-23 11:08:58 -0700350 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800351 if [ ! "$T" ]; then
352 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
353 return
354 fi
355 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700356}
357
Matt Alexanderd9c56562020-05-21 10:49:17 +0000358function set_stuff_for_environment()
359{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800360 setpaths
361 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700362
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800363 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700364 # With this environment variable new GCC can apply colors to warnings/errors
365 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 -0700366}
367
Matt Alexanderd9c56562020-05-21 10:49:17 +0000368function set_sequence_number()
369{
Colin Cross88737132017-03-21 17:41:03 -0700370 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700371}
372
Makoto Onukida971062018-06-18 10:15:19 -0700373# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
374function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800375 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700376 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800377 *:"$cmd":*)
378 return 1
379 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700380 esac
381 return 0
382}
383
Matt Alexanderd9c56562020-05-21 10:49:17 +0000384function addcompletions()
385{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700386 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700387
Jim Tanga881a252018-06-19 16:34:41 +0800388 # Keep us from trying to run in something that's neither bash nor zsh.
389 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700390 return
391 fi
392
393 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800394 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700395 return
396 fi
397
Jim Tanga881a252018-06-19 16:34:41 +0800398 local completion_files=(
399 system/core/adb/adb.bash
400 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800401 tools/asuite/asuite.sh
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
Jim Tanga881a252018-06-19 16:34:41 +0800407 for f in ${completion_files[*]}; do
408 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700409 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700410 fi
411 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700412
Matt Alexanderd9c56562020-05-21 10:49:17 +0000413 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700414 complete -C "bit --tab" bit
415 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000416 if [ -z "$ZSH_VERSION" ]; then
417 # Doesn't work in zsh.
418 complete -o nospace -F _croot croot
419 fi
Jim Tanga881a252018-06-19 16:34:41 +0800420 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800421
Cole Faust24c36db2021-01-23 02:39:37 +0000422 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100423 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000424 complete -F _complete_android_module_names outmod
425 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100426 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700427}
428
Matt Alexanderd9c56562020-05-21 10:49:17 +0000429function choosetype()
430{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700431 echo "Build type choices are:"
432 echo " 1. release"
433 echo " 2. debug"
434 echo
435
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800436 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700437 DEFAULT_NUM=1
438 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700439
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800440 export TARGET_BUILD_TYPE=
441 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700442 while [ -z $TARGET_BUILD_TYPE ]
443 do
444 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000445 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800446 read ANSWER
447 else
448 echo $1
449 ANSWER=$1
450 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700451 case $ANSWER in
452 "")
453 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
454 ;;
455 1)
456 export TARGET_BUILD_TYPE=release
457 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800458 release)
459 export TARGET_BUILD_TYPE=release
460 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700461 2)
462 export TARGET_BUILD_TYPE=debug
463 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800464 debug)
465 export TARGET_BUILD_TYPE=debug
466 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 *)
468 echo
469 echo "I didn't understand your response. Please try again."
470 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700471 ;;
472 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000473 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800474 break
475 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700476 done
477
Ying Wang08800fd2016-03-03 20:57:21 -0800478 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700479 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800480 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481}
482
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800483#
484# This function isn't really right: It chooses a TARGET_PRODUCT
485# based on the list of boards. Usually, that gets you something
486# that kinda works with a generic product, but really, you should
487# pick a product by name.
488#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000489function chooseproduct()
490{
Christopher Ferris55257d22017-03-23 11:08:58 -0700491 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000492 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493 default_value=$TARGET_PRODUCT
494 else
Ying Wang0a76df52015-06-08 11:57:26 -0700495 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700496 fi
497
Ying Wang08800fd2016-03-03 20:57:21 -0800498 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499 export TARGET_PRODUCT=
500 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700501 while [ -z "$TARGET_PRODUCT" ]
502 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700503 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000504 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800505 read ANSWER
506 else
507 echo $1
508 ANSWER=$1
509 fi
510
Matt Alexanderd9c56562020-05-21 10:49:17 +0000511 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800513 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000514 if check_product $ANSWER
515 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800516 export TARGET_PRODUCT=$ANSWER
517 else
518 echo "** Not a valid product: $ANSWER"
519 fi
520 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000521 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800522 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700523 fi
524 done
525
Ying Wang08800fd2016-03-03 20:57:21 -0800526 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700527 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800528 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700529}
530
Matt Alexanderd9c56562020-05-21 10:49:17 +0000531function choosevariant()
532{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800533 echo "Variant choices are:"
534 local index=1
535 local v
536 for v in ${VARIANT_CHOICES[@]}
537 do
538 # The product name is the name of the directory containing
539 # the makefile we found, above.
540 echo " $index. $v"
541 index=$(($index+1))
542 done
543
544 local default_value=eng
545 local ANSWER
546
547 export TARGET_BUILD_VARIANT=
548 while [ -z "$TARGET_BUILD_VARIANT" ]
549 do
550 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000551 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800552 read ANSWER
553 else
554 echo $1
555 ANSWER=$1
556 fi
557
Matt Alexanderd9c56562020-05-21 10:49:17 +0000558 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000560 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
561 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200562 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800563 fi
564 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000565 if check_variant $ANSWER
566 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567 export TARGET_BUILD_VARIANT=$ANSWER
568 else
569 echo "** Not a valid variant: $ANSWER"
570 fi
571 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000572 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800573 break
574 fi
575 done
576}
577
Matt Alexanderd9c56562020-05-21 10:49:17 +0000578function choosecombo()
579{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700580 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581
582 echo
583 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700584 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585
586 echo
587 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700588 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589
590 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800591 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700592 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800593 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800594 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595}
596
Matt Alexanderd9c56562020-05-21 10:49:17 +0000597function add_lunch_combo()
598{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800599 if [ -n "$ZSH_VERSION" ]; then
600 echo -n "${funcfiletrace[1]}: "
601 else
602 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
603 fi
604 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 -0800605}
606
Matt Alexanderd9c56562020-05-21 10:49:17 +0000607function print_lunch_menu()
608{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700609 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100610 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700611 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 +0100612 local ret=$?
613
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700614 echo
615 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700616 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100617
Matt Alexanderd9c56562020-05-21 10:49:17 +0000618 if [ $ret -ne 0 ]
619 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100620 echo "Warning: Cannot display lunch menu."
621 echo
622 echo "Note: You can invoke lunch with an explicit target:"
623 echo
624 echo " usage: lunch [target]" >&2
625 echo
626 return
627 fi
628
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700629 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800630
631 local i=1
632 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700633 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800634 do
635 echo " $i. $choice"
636 i=$(($i+1))
637 done
638
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700639 echo
640}
641
Matt Alexanderd9c56562020-05-21 10:49:17 +0000642function lunch()
643{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800644 local answer
645
Steven Moreland92793dc2020-02-25 18:30:18 -0800646 if [[ $# -gt 1 ]]; then
647 echo "usage: lunch [target]" >&2
648 return 1
649 fi
650
651 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800652 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700653 else
654 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700655 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800656 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700657 fi
658
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800659 local selection=
660
Matt Alexanderd9c56562020-05-21 10:49:17 +0000661 if [ -z "$answer" ]
662 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700663 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000664 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
665 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800666 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000667 if [ $answer -le ${#choices[@]} ]
668 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800669 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000670 if [ -n "$ZSH_VERSION" ]
671 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800672 selection=${choices[$(($answer))]}
673 else
674 selection=${choices[$(($answer-1))]}
675 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800676 fi
Colin Cross88737132017-03-21 17:41:03 -0700677 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800678 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700679 fi
680
Joe Onoratoda12daf2010-06-09 18:18:31 -0700681 export TARGET_BUILD_APPS=
682
Colin Cross88737132017-03-21 17:41:03 -0700683 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700684 product=${selection%%-*} # Trim everything after first dash
685 variant_and_version=${selection#*-} # Trim everything up to first dash
686 if [ "$variant_and_version" != "$selection" ]; then
687 variant=${variant_and_version%%-*}
688 if [ "$variant" != "$variant_and_version" ]; then
689 version=${variant_and_version#*-}
690 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700691 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800692
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693 if [ -z "$product" ]
694 then
Ying Wang08800fd2016-03-03 20:57:21 -0800695 echo
Colin Cross88737132017-03-21 17:41:03 -0700696 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700697 return 1
698 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800699
Colin Cross88737132017-03-21 17:41:03 -0700700 TARGET_PRODUCT=$product \
701 TARGET_BUILD_VARIANT=$variant \
702 TARGET_PLATFORM_VERSION=$version \
703 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000704 if [ $? -ne 0 ]
705 then
Colin Cross88737132017-03-21 17:41:03 -0700706 return 1
707 fi
Colin Cross88737132017-03-21 17:41:03 -0700708 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
709 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700710 if [ -n "$version" ]; then
711 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
712 else
713 unset TARGET_PLATFORM_VERSION
714 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700715 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700717 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800718
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700719 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700720 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800721 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700722}
723
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700724unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700725# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000726function _lunch()
727{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700728 local cur prev opts
729 COMPREPLY=()
730 cur="${COMP_WORDS[COMP_CWORD]}"
731 prev="${COMP_WORDS[COMP_CWORD-1]}"
732
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700733 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800734 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700735 fi
736
737 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700738 return 0
739}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700740
Joe Onoratoda12daf2010-06-09 18:18:31 -0700741# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700742# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000743function tapas()
744{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700745 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800746 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700747 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700748 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Colin Cross5ff43802022-01-27 18:15:53 -0800749 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
750 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)"
751
Joe Onoratoda12daf2010-06-09 18:18:31 -0700752
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700753 if [ "$showHelp" != "" ]; then
754 $(gettop)/build/make/tapasHelp.sh
755 return
756 fi
757
Ying Wang67f02922012-08-22 10:25:20 -0700758 if [ $(echo $arch | wc -w) -gt 1 ]; then
759 echo "tapas: Error: Multiple build archs supplied: $arch"
760 return
761 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700762 if [ $(echo $variant | wc -w) -gt 1 ]; then
763 echo "tapas: Error: Multiple build variants supplied: $variant"
764 return
765 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700766 if [ $(echo $density | wc -w) -gt 1 ]; then
767 echo "tapas: Error: Multiple densities supplied: $density"
768 return
769 fi
Colin Cross5ff43802022-01-27 18:15:53 -0800770 if [ $(echo $keys | wc -w) -gt 1 ]; then
771 echo "tapas: Error: Multiple keys supplied: $keys"
772 return
773 fi
Ying Wang67f02922012-08-22 10:25:20 -0700774
Ying Wang0a76df52015-06-08 11:57:26 -0700775 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700776 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000777 x86) product=aosp_x86;;
778 arm64) product=aosp_arm64;;
779 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700780 esac
Colin Cross5ff43802022-01-27 18:15:53 -0800781 if [ -n "$keys" ]; then
782 product=${product/aosp_/aosp_${keys}_}
783 fi;
784
Joe Onoratoda12daf2010-06-09 18:18:31 -0700785 if [ -z "$variant" ]; then
786 variant=eng
787 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700788 if [ -z "$apps" ]; then
789 apps=all
790 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600791 if [ -z "$density" ]; then
792 density=alldpi
793 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700794
Ying Wang67f02922012-08-22 10:25:20 -0700795 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700796 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700797 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700798 export TARGET_BUILD_TYPE=release
799 export TARGET_BUILD_APPS=$apps
800
Ying Wang08800fd2016-03-03 20:57:21 -0800801 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700802 set_stuff_for_environment
803 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800804 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700805}
806
Martin Stjernholmf692c752021-04-12 00:01:10 +0100807# Configures the build to build unbundled Android modules (APEXes).
808# Run banchan with one or more module names (from apex{} modules).
809function banchan()
810{
811 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100812 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100813 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100814 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 +0100815
816 if [ "$showHelp" != "" ]; then
817 $(gettop)/build/make/banchanHelp.sh
818 return
819 fi
820
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100821 if [ -z "$product" ]; then
822 product=arm
823 elif [ $(echo $product | wc -w) -gt 1 ]; then
824 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100825 return
826 fi
827 if [ $(echo $variant | wc -w) -gt 1 ]; then
828 echo "banchan: Error: Multiple build variants supplied: $variant"
829 return
830 fi
831 if [ -z "$apps" ]; then
832 echo "banchan: Error: No modules supplied"
833 return
834 fi
835
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100836 case $product in
837 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100838 x86) product=module_x86;;
839 arm64) product=module_arm64;;
840 x86_64) product=module_x86_64;;
841 esac
842 if [ -z "$variant" ]; then
843 variant=eng
844 fi
845
846 export TARGET_PRODUCT=$product
847 export TARGET_BUILD_VARIANT=$variant
848 export TARGET_BUILD_DENSITY=alldpi
849 export TARGET_BUILD_TYPE=release
850
851 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
852 # case is different and it may diverge in the future.
853 export TARGET_BUILD_APPS=$apps
854
855 build_build_var_cache
856 set_stuff_for_environment
857 printconfig
858 destroy_build_var_cache
859}
860
Matt Alexanderd9c56562020-05-21 10:49:17 +0000861function gettop
862{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700863 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000864 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700865 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000866 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700867 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000868 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800869 # The following circumlocution (repeated below as well) ensures
870 # that we record the true directory name and not one that is
871 # faked up with symlink names.
872 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700873 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800874 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700875 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000876 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800877 \cd ..
synergyb112a402013-07-05 19:47:47 -0700878 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700879 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000880 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700881 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000882 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700883 fi
884 fi
885 fi
886}
887
Matt Alexanderd9c56562020-05-21 10:49:17 +0000888function croot()
889{
Christopher Ferris55257d22017-03-23 11:08:58 -0700890 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700891 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700892 if [ "$1" ]; then
893 \cd $(gettop)/$1
894 else
895 \cd $(gettop)
896 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700897 else
898 echo "Couldn't locate the top of the tree. Try setting TOP."
899 fi
900}
901
Matt Alexanderd9c56562020-05-21 10:49:17 +0000902function _croot()
903{
Anton Hanssonece9c482019-02-04 18:15:39 +0000904 local T=$(gettop)
905 if [ "$T" ]; then
906 local cur="${COMP_WORDS[COMP_CWORD]}"
907 k=0
908 for c in $(compgen -d ${T}/${cur}); do
909 COMPREPLY[k++]=${c#${T}/}/
910 done
911 fi
912}
913
Matt Alexanderd9c56562020-05-21 10:49:17 +0000914function cproj()
915{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700916 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700917 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700918 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700919 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
920 T=$PWD
921 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800922 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700923 return
924 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800925 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700926 done
Ying Wang9cd17642012-12-13 10:52:07 -0800927 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700928 echo "can't find Android.mk"
929}
930
Daniel Sandler47e0a882013-07-30 13:23:52 -0400931# simplified version of ps; output in the form
932# <pid> <procname>
933function qpid() {
934 local prepend=''
935 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000936 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400937 prepend=' '
938 append='$'
939 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000940 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800941 echo "usage: qpid [[--exact] <process name|pid>"
942 return 255
943 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400944
945 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000946 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800947 qpid | \grep "$prepend$EXE$append"
948 else
949 adb shell ps \
950 | tr -d '\r' \
951 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
952 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400953}
954
Steven Moreland74114f12020-09-10 01:23:32 +0000955# syswrite - disable verity, reboot if needed, and remount image
956#
957# Easy way to make system.img/etc writable
958function syswrite() {
959 adb wait-for-device && adb root || return 1
960 if [[ $(adb disable-verity | grep "reboot") ]]; then
961 echo "rebooting"
962 adb reboot && adb wait-for-device && adb root || return 1
963 fi
964 adb wait-for-device && adb remount || return 1
965}
966
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800967# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700968# that has the core-file-size limit set correctly
969#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800970# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700971# if its core-file-size limit is not set already.
972# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
973
Matt Alexanderd9c56562020-05-21 10:49:17 +0000974function coredump_setup()
975{
976 echo "Getting root...";
977 adb root;
978 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700979
Matt Alexanderd9c56562020-05-21 10:49:17 +0000980 echo "Remounting root partition read-write...";
981 adb shell mount -w -o remount -t rootfs rootfs;
982 sleep 1;
983 adb wait-for-device;
984 adb shell mkdir -p /cores;
985 adb shell mount -t tmpfs tmpfs /cores;
986 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700987
Matt Alexanderd9c56562020-05-21 10:49:17 +0000988 echo "Granting SELinux permission to dump in /cores...";
989 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700990
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991 echo "Set core pattern.";
992 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700993
Ying Wang08800fd2016-03-03 20:57:21 -0800994 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700995}
996
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800997# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000998# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700999#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001000# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001001# dump to actually be generated.
1002
Matt Alexanderd9c56562020-05-21 10:49:17 +00001003function coredump_enable()
1004{
1005 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001006 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001007 printf "Expecting a PID!\n";
1008 return;
1009 fi;
1010 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001011 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001012}
1013
1014# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001015# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001016#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001017# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001018# enabled globally.
1019
Matt Alexanderd9c56562020-05-21 10:49:17 +00001020function core()
1021{
1022 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001023
Ying Wang08800fd2016-03-03 20:57:21 -08001024 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001025 printf "Expecting a PID!\n";
1026 return;
1027 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001028
Matt Alexanderd9c56562020-05-21 10:49:17 +00001029 local CORENAME=core.$PID;
1030 local COREPATH=/cores/$CORENAME;
1031 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001032
Matt Alexanderd9c56562020-05-21 10:49:17 +00001033 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001034
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001036 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001037 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1038 adb shell kill -$SIG $PID;
1039 sleep 1;
1040 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001041
Matt Alexanderd9c56562020-05-21 10:49:17 +00001042 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1043 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001044}
1045
Christopher Tate744ee802009-11-12 15:33:08 -08001046# systemstack - dump the current stack trace of all threads in the system process
1047# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001048function systemstack()
1049{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001050 stacks system_server
1051}
1052
Michael Wrightaeed7212014-06-19 19:58:12 -07001053# Read the ELF header from /proc/$PID/exe to determine if the process is
1054# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001055function is64bit()
1056{
Ben Chengfba67bf2014-02-25 10:27:07 -08001057 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001058 if [ "$PID" ] ; then
1059 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001060 echo "64"
1061 else
1062 echo ""
1063 fi
1064 else
1065 echo ""
1066 fi
1067}
1068
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001069case `uname -s` in
1070 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001071 function sgrep()
1072 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001073 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 -04001074 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001075 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001076
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001077 ;;
1078 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001079 function sgrep()
1080 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001081 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 -04001082 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001083 }
1084 ;;
1085esac
1086
Matt Alexanderd9c56562020-05-21 10:49:17 +00001087function gettargetarch
1088{
Raghu Gandham8da43102012-07-25 19:57:22 -07001089 get_build_var TARGET_ARCH
1090}
1091
Matt Alexanderd9c56562020-05-21 10:49:17 +00001092function ggrep()
1093{
Mike Frysinger5e479732015-09-22 18:13:48 -04001094 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1095 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001096}
1097
Matt Alexanderd9c56562020-05-21 10:49:17 +00001098function gogrep()
1099{
Orion Hodson831472d2019-10-25 11:35:15 +01001100 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1101 -exec grep --color -n "$@" {} +
1102}
1103
Matt Alexanderd9c56562020-05-21 10:49:17 +00001104function jgrep()
1105{
Mike Frysinger5e479732015-09-22 18:13:48 -04001106 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1107 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001108}
1109
Alistair Delva176e5342021-02-22 13:31:26 -08001110function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001111{
1112 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1113 -exec grep --color -n "$@" {} +
1114}
1115
Taesu Leeea0cecd2020-10-28 11:05:18 +09001116function ktgrep()
1117{
1118 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1119 -exec grep --color -n "$@" {} +
1120}
1121
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122function cgrep()
1123{
Mike Frysinger5e479732015-09-22 18:13:48 -04001124 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' \) \
1125 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001126}
1127
Matt Alexanderd9c56562020-05-21 10:49:17 +00001128function resgrep()
1129{
Christopher Ferris55257d22017-03-23 11:08:58 -07001130 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001131 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1132 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1133 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134}
1135
Matt Alexanderd9c56562020-05-21 10:49:17 +00001136function mangrep()
1137{
Mike Frysinger5e479732015-09-22 18:13:48 -04001138 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1139 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001140}
1141
Matt Alexanderd9c56562020-05-21 10:49:17 +00001142function owngrep()
1143{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001144 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1145 -exec grep --color -n "$@" {} +
1146}
1147
Matt Alexanderd9c56562020-05-21 10:49:17 +00001148function sepgrep()
1149{
Mike Frysinger5e479732015-09-22 18:13:48 -04001150 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1151 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001152}
1153
Matt Alexanderd9c56562020-05-21 10:49:17 +00001154function rcgrep()
1155{
Mike Frysinger5e479732015-09-22 18:13:48 -04001156 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1157 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001158}
1159
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001160case `uname -s` in
1161 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162 function mgrep()
1163 {
Orion Hodson831472d2019-10-25 11:35:15 +01001164 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 -04001165 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001166 }
1167
Matt Alexanderd9c56562020-05-21 10:49:17 +00001168 function treegrep()
1169 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001170 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 -04001171 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001172 }
1173
1174 ;;
1175 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001176 function mgrep()
1177 {
Orion Hodson831472d2019-10-25 11:35:15 +01001178 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 -04001179 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001180 }
1181
Matt Alexanderd9c56562020-05-21 10:49:17 +00001182 function treegrep()
1183 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001184 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 -04001185 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001186 }
1187
1188 ;;
1189esac
1190
Matt Alexanderd9c56562020-05-21 10:49:17 +00001191function getprebuilt
1192{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193 get_abs_build_var ANDROID_PREBUILTS
1194}
1195
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196function tracedmdump()
1197{
Christopher Ferris55257d22017-03-23 11:08:58 -07001198 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001199 if [ ! "$T" ]; then
1200 echo "Couldn't locate the top of the tree. Try setting TOP."
1201 return
1202 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001203 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001204 local arch=$(gettargetarch)
1205 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001207 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001208 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001209 echo "usage: tracedmdump tracename"
1210 return
1211 fi
1212
Matt Alexanderd9c56562020-05-21 10:49:17 +00001213 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001214 echo "Error: cannot find kernel: '$KERNEL'"
1215 return
1216 fi
1217
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001218 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001219 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001220 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1221 fi
1222
1223 echo "post-processing traces..."
1224 rm -f $TRACE/qtrace.dexlist
1225 post_trace $TRACE
1226 if [ $? -ne 0 ]; then
1227 echo "***"
1228 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1229 echo "***"
1230 return
1231 fi
1232 echo "generating dexlist output..."
1233 /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
1234 echo "generating dmtrace data..."
1235 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1236 echo "generating html file..."
1237 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1238 echo "done, see $TRACE/dmtrace.html for details"
1239 echo "or run:"
1240 echo " traceview $TRACE/dmtrace"
1241}
1242
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001243# communicate with a running device or emulator, set up necessary state,
1244# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245function runhat()
1246{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001247 # process standard adb options
1248 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001249 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001250 adbTarget=$1
1251 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001252 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001253 adbTarget="$1 $2"
1254 shift 2
1255 fi
1256 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001257 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001258
1259 # runhat options
1260 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001263 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264 return
1265 fi
1266
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001267 # confirm hat is available
1268 if [ -z $(which hat) ]; then
1269 echo "hat is not available in this configuration."
1270 return
1271 fi
1272
Andy McFaddenb6289852010-07-12 08:00:19 -07001273 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001274 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001275 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001276 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001277 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001278 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001279 echo -n "> "
1280 read
1281
The Android Open Source Project88b60792009-03-03 19:28:42 -08001282 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001283
The Android Open Source Project88b60792009-03-03 19:28:42 -08001284 echo "Retrieving file $devFile..."
1285 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286
The Android Open Source Project88b60792009-03-03 19:28:42 -08001287 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001288
The Android Open Source Project88b60792009-03-03 19:28:42 -08001289 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001290 echo "View the output by pointing your browser at http://localhost:7000/"
1291 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001292 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001293}
1294
Matt Alexanderd9c56562020-05-21 10:49:17 +00001295function getbugreports()
1296{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001297 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001298
1299 if [ ! "$reports" ]; then
1300 echo "Could not locate any bugreports."
1301 return
1302 fi
1303
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001304 local report
1305 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001306 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001307 echo "/sdcard/bugreports/${report}"
1308 adb pull /sdcard/bugreports/${report} ${report}
1309 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001310 done
1311}
1312
Matt Alexanderd9c56562020-05-21 10:49:17 +00001313function getsdcardpath()
1314{
Victoria Lease1b296b42012-08-21 15:44:06 -07001315 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1316}
1317
Matt Alexanderd9c56562020-05-21 10:49:17 +00001318function getscreenshotpath()
1319{
Victoria Lease1b296b42012-08-21 15:44:06 -07001320 echo "$(getsdcardpath)/Pictures/Screenshots"
1321}
1322
Matt Alexanderd9c56562020-05-21 10:49:17 +00001323function getlastscreenshot()
1324{
Victoria Lease1b296b42012-08-21 15:44:06 -07001325 local screenshot_path=$(getscreenshotpath)
1326 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 +00001327 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001328 echo "No screenshots found."
1329 return
1330 fi
1331 echo "${screenshot}"
1332 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1333}
1334
Matt Alexanderd9c56562020-05-21 10:49:17 +00001335function startviewserver()
1336{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001337 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001338 if [ $# -gt 0 ]; then
1339 port=$1
1340 fi
1341 adb shell service call window 1 i32 $port
1342}
1343
Matt Alexanderd9c56562020-05-21 10:49:17 +00001344function stopviewserver()
1345{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001346 adb shell service call window 2
1347}
1348
Matt Alexanderd9c56562020-05-21 10:49:17 +00001349function isviewserverstarted()
1350{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001351 adb shell service call window 3
1352}
1353
Matt Alexanderd9c56562020-05-21 10:49:17 +00001354function key_home()
1355{
Romain Guyb84049a2010-10-04 16:56:11 -07001356 adb shell input keyevent 3
1357}
1358
Matt Alexanderd9c56562020-05-21 10:49:17 +00001359function key_back()
1360{
Romain Guyb84049a2010-10-04 16:56:11 -07001361 adb shell input keyevent 4
1362}
1363
Matt Alexanderd9c56562020-05-21 10:49:17 +00001364function key_menu()
1365{
Romain Guyb84049a2010-10-04 16:56:11 -07001366 adb shell input keyevent 82
1367}
1368
Matt Alexanderd9c56562020-05-21 10:49:17 +00001369function smoketest()
1370{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001371 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1372 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1373 return
1374 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001375 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001376 if [ ! "$T" ]; then
1377 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1378 return
1379 fi
1380
Ying Wang9cd17642012-12-13 10:52:07 -08001381 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001382 adb uninstall com.android.smoketest > /dev/null &&
1383 adb uninstall com.android.smoketest.tests > /dev/null &&
1384 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1385 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1386 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1387}
1388
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001389# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001390function runtest()
1391{
Christopher Ferris55257d22017-03-23 11:08:58 -07001392 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001393 if [ ! "$T" ]; then
1394 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1395 return
1396 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001397 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001398}
1399
The Android Open Source Project88b60792009-03-03 19:28:42 -08001400function godir () {
1401 if [[ -z "$1" ]]; then
1402 echo "Usage: godir <regex>"
1403 return
1404 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001405 local T=$(gettop)
1406 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001407 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001408 mkdir -p $OUT_DIR
1409 FILELIST=$OUT_DIR/filelist
1410 else
1411 FILELIST=$T/filelist
1412 fi
1413 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001414 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001415 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001416 echo " Done"
1417 echo ""
1418 fi
1419 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001420 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001421 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001422 echo "Not found"
1423 return
1424 fi
1425 local pathname
1426 local choice
1427 if [[ ${#lines[@]} > 1 ]]; then
1428 while [[ -z "$pathname" ]]; do
1429 local index=1
1430 local line
1431 for line in ${lines[@]}; do
1432 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001433 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001434 done
1435 echo
1436 echo -n "Select one: "
1437 unset choice
1438 read choice
1439 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1440 echo "Invalid choice"
1441 continue
1442 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001443 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001444 done
1445 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001446 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001447 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001448 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001449}
1450
Steven Moreland62054a42018-12-06 10:11:40 -08001451# Update module-info.json in out.
1452function refreshmod() {
1453 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1454 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1455 return 1
1456 fi
1457
1458 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1459
1460 # for the output of the next command
1461 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1462
1463 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001464 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001465 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1466}
1467
Cole Faust24c36db2021-01-23 02:39:37 +00001468# Verifies that module-info.txt exists, creating it if it doesn't.
1469function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001470 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001471 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1472 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1473 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001474 return 1
1475 fi
1476
1477 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001478 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1479 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1480 fi
1481 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001482 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001483}
1484
1485# List all modules for the current device, as cached in module-info.json. If any build change is
1486# made and it should be reflected in the output, you should run 'refreshmod' first.
1487function allmod() {
1488 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001489
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001490 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 -08001491}
1492
Joe Onorato2c1aa472021-02-25 16:42:39 -08001493# Get the path of a specific module in the android tree, as cached in module-info.json.
1494# If any build change is made, and it should be reflected in the output, you should run
1495# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001496function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001497 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001498 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001499 return 1
1500 fi
1501
Cole Faust24c36db2021-01-23 02:39:37 +00001502 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001503
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001504 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001505module = '$1'
1506module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1507if module not in module_info:
1508 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001509print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001510
1511 if [ -z "$relpath" ]; then
1512 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1513 return 1
1514 else
Rett Berg78d1c932019-01-24 14:34:23 -08001515 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001516 fi
1517}
1518
Joe Onorato2c1aa472021-02-25 16:42:39 -08001519# Get the path of a specific module in the android tree, as cached in module-info.json.
1520# If any build change is made, and it should be reflected in the output, you should run
1521# 'refreshmod' first. Note: This is the inverse of pathmod.
1522function dirmods() {
1523 if [[ $# -ne 1 ]]; then
1524 echo "usage: dirmods <path>" >&2
1525 return 1
1526 fi
1527
1528 verifymodinfo || return 1
1529
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001530 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001531dir = '$1'
1532while dir.endswith('/'):
1533 dir = dir[:-1]
1534prefix = dir + '/'
1535module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1536results = set()
1537for m in module_info.values():
1538 for path in m.get(u'path', []):
1539 if path == dir or path.startswith(prefix):
1540 name = m.get(u'module_name')
1541 if name:
1542 results.add(name)
1543for name in sorted(results):
1544 print(name)
1545"
1546}
1547
1548
Rett Berg78d1c932019-01-24 14:34:23 -08001549# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1550# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1551function gomod() {
1552 if [[ $# -ne 1 ]]; then
1553 echo "usage: gomod <module>" >&2
1554 return 1
1555 fi
1556
1557 local path="$(pathmod $@)"
1558 if [ -z "$path" ]; then
1559 return 1
1560 fi
1561 cd $path
1562}
1563
Cole Faust24c36db2021-01-23 02:39:37 +00001564# Gets the list of a module's installed outputs, as cached in module-info.json.
1565# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1566function outmod() {
1567 if [[ $# -ne 1 ]]; then
1568 echo "usage: outmod <module>" >&2
1569 return 1
1570 fi
1571
1572 verifymodinfo || return 1
1573
1574 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001575 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001576module = '$1'
1577module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1578if module not in module_info:
1579 exit(1)
1580for output in module_info[module]['installed']:
1581 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1582
1583 if [ $? -ne 0 ]; then
1584 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1585 return 1
1586 elif [ ! -z "$relpath" ]; then
1587 echo "$relpath"
1588 fi
1589}
1590
1591# adb install a module's apk, as cached in module-info.json. If any build change
1592# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1593# Usage: installmod [adb install arguments] <module>
1594# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1595function installmod() {
1596 if [[ $# -eq 0 ]]; then
1597 echo "usage: installmod [adb install arguments] <module>" >&2
1598 return 1
1599 fi
1600
1601 local _path
1602 _path=$(outmod ${@:$#:1})
1603 if [ $? -ne 0 ]; then
1604 return 1
1605 fi
1606
1607 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1608 if [ -z "$_path" ]; then
1609 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1610 return 1
1611 fi
1612 local length=$(( $# - 1 ))
1613 echo adb install ${@:1:$length} $_path
1614 adb install ${@:1:$length} $_path
1615}
1616
dimitry73b84812018-12-11 18:06:00 +01001617function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001618 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001619 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001620}
1621
Alex Rayf0d08eb2013-03-08 15:15:06 -08001622# Print colored exit condition
1623function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001624 "$@"
1625 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001626 if [ $retval -ne 0 ]
1627 then
Jacky Cao89483b82015-05-15 22:12:53 +08001628 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001629 else
Jacky Cao89483b82015-05-15 22:12:53 +08001630 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001631 fi
1632 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001633}
1634
Matt Alexanderd9c56562020-05-21 10:49:17 +00001635function get_make_command()
1636{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001637 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1638 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001639 # Always use the real make if -C is passed in
1640 for arg in "$@"; do
1641 if [[ $arg == -C* ]]; then
1642 echo command make
1643 return
1644 fi
1645 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001646 echo build/soong/soong_ui.bash --make-mode
1647 else
1648 echo command make
1649 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001650}
1651
Matt Alexanderd9c56562020-05-21 10:49:17 +00001652function _wrap_build()
1653{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001654 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1655 "$@"
1656 return $?
1657 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001658 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001659 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001660 local ret=$?
1661 local end_time=$(date +"%s")
1662 local tdiff=$(($end_time-$start_time))
1663 local hours=$(($tdiff / 3600 ))
1664 local mins=$((($tdiff % 3600) / 60))
1665 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001666 local ncolors=$(tput colors 2>/dev/null)
1667 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001668 color_failed=$'\E'"[0;31m"
1669 color_success=$'\E'"[0;32m"
1670 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001671 else
1672 color_failed=""
1673 color_success=""
1674 color_reset=""
1675 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001676 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001677 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001678 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001679 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001680 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001681 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001682 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001683 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001684 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001685 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001686 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001687 printf "(%s seconds)" $secs
1688 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001689 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001690 echo
1691 return $ret
1692}
1693
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001694function _trigger_build()
1695(
1696 local -r bc="$1"; shift
1697 if T="$(gettop)"; then
1698 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1699 else
1700 echo "Couldn't locate the top of the tree. Try setting TOP."
1701 fi
1702)
1703
1704function m()
1705(
1706 _trigger_build "all-modules" "$@"
1707)
1708
1709function mm()
1710(
1711 _trigger_build "modules-in-a-dir-no-deps" "$@"
1712)
1713
1714function mmm()
1715(
1716 _trigger_build "modules-in-dirs-no-deps" "$@"
1717)
1718
1719function mma()
1720(
1721 _trigger_build "modules-in-a-dir" "$@"
1722)
1723
1724function mmma()
1725(
1726 _trigger_build "modules-in-dirs" "$@"
1727)
1728
Matt Alexanderd9c56562020-05-21 10:49:17 +00001729function make()
1730{
Dan Willemsene9842242017-07-28 13:00:13 -07001731 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001732}
1733
Matt Alexanderd9c56562020-05-21 10:49:17 +00001734function provision()
1735{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001736 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1737 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1738 return 1
1739 fi
1740 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1741 echo "There is no provisioning script for the device." >&2
1742 return 1
1743 fi
1744
1745 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001746 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001747 shift 1
1748 else
1749 echo "This action will reflash your device."
1750 echo ""
1751 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1752 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001753 echo -n "Are you sure you want to do this (yes/no)? "
1754 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001755 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001756 echo "Not taking any action. Exiting." >&2
1757 return 1
1758 fi
1759 fi
1760 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1761}
1762
Jim Tanga881a252018-06-19 16:34:41 +08001763# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001764function enable_zsh_completion() {
1765 # Don't override user's options if bash-style completion is already enabled.
1766 if ! declare -f complete >/dev/null; then
1767 autoload -U compinit && compinit
1768 autoload -U bashcompinit && bashcompinit
1769 fi
Jim Tanga881a252018-06-19 16:34:41 +08001770}
1771
1772function validate_current_shell() {
1773 local current_sh="$(ps -o command -p $$)"
1774 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001775 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001776 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001777 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001778 *zsh*)
1779 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001780 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001781 *)
Jim Tanga881a252018-06-19 16:34:41 +08001782 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 -07001783 ;;
1784 esac
Jim Tanga881a252018-06-19 16:34:41 +08001785}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001786
1787# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001788# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1789# load those.
1790#
1791# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001792function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001793 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001794 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001795 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001796 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 -08001797 if [ -n "$allowed" ]; then
1798 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1799 echo " $allowed"
1800 echo " $f"
1801 return
1802 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001803 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001804 done
1805
1806 allowed_files=
1807 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001808 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001809 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001810 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001811
1812 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001813 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001814 else
1815 echo "ignoring $f, not in $allowed"
1816 fi
Jim Tanga881a252018-06-19 16:34:41 +08001817 done
1818 done
1819}
Kenny Root52aa81c2011-07-15 11:07:06 -07001820
Dan Albertbab814f2020-08-26 15:34:53 -07001821function showcommands() {
1822 local T=$(gettop)
1823 if [[ -z "$TARGET_PRODUCT" ]]; then
1824 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1825 return
1826 fi
1827 case $(uname -s) in
1828 Darwin)
1829 PREBUILT_NAME=darwin-x86
1830 ;;
1831 Linux)
1832 PREBUILT_NAME=linux-x86
1833 ;;
1834 *)
1835 >&2 echo Unknown host $(uname -s)
1836 return
1837 ;;
1838 esac
1839 if [[ -z "$OUT_DIR" ]]; then
1840 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1841 OUT_DIR=out
1842 else
1843 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1844 fi
1845 fi
1846 if [[ "$1" == "--regenerate" ]]; then
1847 shift 1
1848 NINJA_ARGS="-t commands $@" m
1849 else
1850 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1851 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1852 -t commands "$@")
1853 fi
1854}
1855
Jim Tanga881a252018-06-19 16:34:41 +08001856validate_current_shell
1857source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001858addcompletions