blob: e4afdb9bc2d108f5b2bdffabbbfff9af171c92f4 [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]
Anton Hanssonece9c482019-02-04 18:15:39 +000012- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080013- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070014- mm: Builds and installs all of the modules in the current directory, and their
15 dependencies.
16- mmm: Builds and installs all of the modules in the supplied directories, and their
17 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080018 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070019- mma: Same as 'mm'
20- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080021- provision: Flash device with all required partitions. Options will be passed on to fastboot.
22- cgrep: Greps on all local C/C++ files.
23- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010024- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080025- jgrep: Greps on all local Java files.
26- resgrep: Greps on all local res/*.xml files.
27- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070028- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060029- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- sepgrep: Greps on all local sepolicy files.
31- sgrep: Greps on all local source files.
32- godir: Go to the directory containing a file.
33- allmod: List all modules.
34- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080035- pathmod: Get the directory containing a module.
Steven Morelandab25c7a2020-04-09 12:05:34 -070036- refreshmod: Refresh list of modules for allmod/gomod/pathmod.
Steven Moreland74114f12020-09-10 01:23:32 +000037- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070038
Roland Levillain39341922015-10-20 12:48:19 +010039Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070040- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080041- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070042
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070043Look at the source to view more functions. The complete list is:
44EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070045 local T=$(gettop)
46 local A=""
47 local i
Jacky Cao89483b82015-05-15 22:12:53 +080048 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 -070049 A="$A $i"
50 done
51 echo $A
52}
53
Ying Wang08800fd2016-03-03 20:57:21 -080054# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000055function build_build_var_cache()
56{
Christopher Ferris55257d22017-03-23 11:08:58 -070057 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080058 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080059 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' ' '`)
60 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 -080061 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080062 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080063 --vars="${cached_vars[*]}" \
64 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070065 --var-prefix=var_cache_ \
66 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080067 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000068 if [ $ret -ne 0 ]
69 then
Ying Wang08800fd2016-03-03 20:57:21 -080070 unset build_dicts_script
71 return $ret
72 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070073 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080074 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080075 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080076 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000077 if [ $ret -ne 0 ]
78 then
Ying Wang08800fd2016-03-03 20:57:21 -080079 return $ret
80 fi
81 BUILD_VAR_CACHE_READY="true"
82}
83
Ying Wangf0cb3972016-03-04 13:56:23 -080084# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080085# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000086function destroy_build_var_cache()
87{
Ying Wang08800fd2016-03-03 20:57:21 -080088 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070089 local v
Ying Wang08800fd2016-03-03 20:57:21 -080090 for v in $cached_vars; do
91 unset var_cache_$v
92 done
93 unset cached_vars
94 for v in $cached_abs_vars; do
95 unset abs_var_cache_$v
96 done
97 unset cached_abs_vars
98}
99
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700100# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000101function get_abs_build_var()
102{
103 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
104 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800105 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000106 return
Ying Wang08800fd2016-03-03 20:57:21 -0800107 fi
108
Christopher Ferris55257d22017-03-23 11:08:58 -0700109 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700110 if [ ! "$T" ]; then
111 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
112 return
113 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700114 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115}
116
117# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000118function get_build_var()
119{
120 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
121 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800122 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100123 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800124 fi
125
Christopher Ferris55257d22017-03-23 11:08:58 -0700126 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700127 if [ ! "$T" ]; then
128 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100129 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700130 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700131 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800132}
133
134# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000135function check_product()
136{
Christopher Ferris55257d22017-03-23 11:08:58 -0700137 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800138 if [ ! "$T" ]; then
139 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
140 return
141 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700142 TARGET_PRODUCT=$1 \
143 TARGET_BUILD_VARIANT= \
144 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700145 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 get_build_var TARGET_DEVICE > /dev/null
147 # hide successful answers, but allow the errors to show
148}
149
150VARIANT_CHOICES=(user userdebug eng)
151
152# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000153function check_variant()
154{
Christopher Ferris55257d22017-03-23 11:08:58 -0700155 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800156 for v in ${VARIANT_CHOICES[@]}
157 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000158 if [ "$v" = "$1" ]
159 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800160 return 0
161 fi
162 done
163 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700164}
165
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166function setpaths()
167{
Christopher Ferris55257d22017-03-23 11:08:58 -0700168 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700169 if [ ! "$T" ]; then
170 echo "Couldn't locate the top of the tree. Try setting TOP."
171 return
172 fi
173
174 ##################################################################
175 # #
176 # Read me before you modify this code #
177 # #
178 # This function sets ANDROID_BUILD_PATHS to what it is adding #
179 # to PATH, and the next time it is run, it removes that from #
180 # PATH. This is required so lunch can be run more than once #
181 # and still have working paths. #
182 # #
183 ##################################################################
184
Raphael Mollc639c782011-06-20 17:25:01 -0700185 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
186 # due to "C:\Program Files" being in the path.
187
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700188 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000189 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700190 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
191 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000192 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700193 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800194 # strip leading ':', if any
195 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500196 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700197
198 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700199 local prebuiltdir=$(getprebuilt)
200 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700201
Ben Cheng8bc4c432012-11-16 13:29:13 -0800202 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700203 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
204 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800205 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800206
Raphael Mollc639c782011-06-20 17:25:01 -0700207 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800208 export ANDROID_TOOLCHAIN=
209 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200210 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700211 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200212 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000213 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700214 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000215 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400216 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000217 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200218 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000219 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
220 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700221 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200222 *)
223 echo "Can't find toolchain for unknown architecture: $ARCH"
224 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700225 ;;
226 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700227 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800228 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700229 fi
Raphael732936d2011-06-22 14:35:32 -0700230
Christopher Ferris55257d22017-03-23 11:08:58 -0700231 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800232 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
233 fi
234
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700235 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700236
237 # add kernel specific binaries
238 case $(uname -s) in
239 Linux)
240 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
241 ;;
242 *)
243 ;;
244 esac
245
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400246 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
247 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
248 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
249 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700250 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
251
252 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
253 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
254 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200255
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800256 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
257 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
258
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200259 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
260 # to ensure that the corresponding 'emulator' binaries are used.
261 case $(uname -s) in
262 Darwin)
263 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
264 ;;
265 Linux)
266 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
267 ;;
268 *)
269 ANDROID_EMULATOR_PREBUILTS=
270 ;;
271 esac
272 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700273 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200274 export ANDROID_EMULATOR_PREBUILTS
275 fi
276
Jim Tangb3fda302018-12-22 10:24:55 +0800277 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
278 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700279 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
280 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
281 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
282 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800283
Ryan Prichard8426a192019-07-15 18:05:29 -0700284 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800285
286 # out with the duplicate old
287 if [ -n $ANDROID_PYTHONPATH ]; then
288 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
289 fi
290 # and in with the new
291 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800292 if [ -n $VENDOR_PYTHONPATH ]; then
293 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
294 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800295 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800296
Colin Crosse97e6932017-06-30 16:01:45 -0700297 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
298 export JAVA_HOME=$ANDROID_JAVA_HOME
299 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
300 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
301 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500302
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800303 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700304 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
305 export OUT=$ANDROID_PRODUCT_OUT
306
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700307 unset ANDROID_HOST_OUT
308 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
309
Simran Basidd050ed2017-02-13 13:46:48 -0800310 unset ANDROID_HOST_OUT_TESTCASES
311 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
312
313 unset ANDROID_TARGET_OUT_TESTCASES
314 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
315
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700317 # TODO: fix the path
318 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
319}
320
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000321function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000322{
323 local T="$(gettop)"
324 if [ ! "$T" ]; then
325 echo "Couldn't locate the top of the tree. Try setting TOP."
326 return
327 fi
328
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000329 if which bazel &>/dev/null; then
330 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
331 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000332 fi
333
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000334 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000335}
336
Matt Alexanderd9c56562020-05-21 10:49:17 +0000337function printconfig()
338{
Christopher Ferris55257d22017-03-23 11:08:58 -0700339 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800340 if [ ! "$T" ]; then
341 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
342 return
343 fi
344 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700345}
346
Matt Alexanderd9c56562020-05-21 10:49:17 +0000347function set_stuff_for_environment()
348{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800349 setpaths
350 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700351
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800352 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700353 # With this environment variable new GCC can apply colors to warnings/errors
354 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 -0700355}
356
Matt Alexanderd9c56562020-05-21 10:49:17 +0000357function set_sequence_number()
358{
Colin Cross88737132017-03-21 17:41:03 -0700359 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700360}
361
Makoto Onukida971062018-06-18 10:15:19 -0700362# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
363function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800364 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700365 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800366 *:"$cmd":*)
367 return 1
368 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700369 esac
370 return 0
371}
372
Matt Alexanderd9c56562020-05-21 10:49:17 +0000373function addcompletions()
374{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700375 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700376
Jim Tanga881a252018-06-19 16:34:41 +0800377 # Keep us from trying to run in something that's neither bash nor zsh.
378 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700379 return
380 fi
381
382 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800383 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700384 return
385 fi
386
Jim Tanga881a252018-06-19 16:34:41 +0800387 local completion_files=(
388 system/core/adb/adb.bash
389 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800390 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800391 )
Makoto Onukida971062018-06-18 10:15:19 -0700392 # Completion can be disabled selectively to allow users to use non-standard completion.
393 # e.g.
394 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
395 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800396 for f in ${completion_files[*]}; do
397 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700398 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700399 fi
400 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700401
Matt Alexanderd9c56562020-05-21 10:49:17 +0000402 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700403 complete -C "bit --tab" bit
404 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000405 if [ -z "$ZSH_VERSION" ]; then
406 # Doesn't work in zsh.
407 complete -o nospace -F _croot croot
408 fi
Jim Tanga881a252018-06-19 16:34:41 +0800409 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800410
dimitry73b84812018-12-11 18:06:00 +0100411 complete -F _complete_android_module_names gomod
412 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700413}
414
Matt Alexanderd9c56562020-05-21 10:49:17 +0000415function choosetype()
416{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700417 echo "Build type choices are:"
418 echo " 1. release"
419 echo " 2. debug"
420 echo
421
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800422 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700423 DEFAULT_NUM=1
424 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800426 export TARGET_BUILD_TYPE=
427 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428 while [ -z $TARGET_BUILD_TYPE ]
429 do
430 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000431 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800432 read ANSWER
433 else
434 echo $1
435 ANSWER=$1
436 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700437 case $ANSWER in
438 "")
439 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
440 ;;
441 1)
442 export TARGET_BUILD_TYPE=release
443 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800444 release)
445 export TARGET_BUILD_TYPE=release
446 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700447 2)
448 export TARGET_BUILD_TYPE=debug
449 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800450 debug)
451 export TARGET_BUILD_TYPE=debug
452 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700453 *)
454 echo
455 echo "I didn't understand your response. Please try again."
456 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700457 ;;
458 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000459 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800460 break
461 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462 done
463
Ying Wang08800fd2016-03-03 20:57:21 -0800464 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800466 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467}
468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469#
470# This function isn't really right: It chooses a TARGET_PRODUCT
471# based on the list of boards. Usually, that gets you something
472# that kinda works with a generic product, but really, you should
473# pick a product by name.
474#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000475function chooseproduct()
476{
Christopher Ferris55257d22017-03-23 11:08:58 -0700477 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000478 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700479 default_value=$TARGET_PRODUCT
480 else
Ying Wang0a76df52015-06-08 11:57:26 -0700481 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 fi
483
Ying Wang08800fd2016-03-03 20:57:21 -0800484 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800485 export TARGET_PRODUCT=
486 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700487 while [ -z "$TARGET_PRODUCT" ]
488 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700489 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000490 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491 read ANSWER
492 else
493 echo $1
494 ANSWER=$1
495 fi
496
Matt Alexanderd9c56562020-05-21 10:49:17 +0000497 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000500 if check_product $ANSWER
501 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502 export TARGET_PRODUCT=$ANSWER
503 else
504 echo "** Not a valid product: $ANSWER"
505 fi
506 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000507 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800508 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 fi
510 done
511
Ying Wang08800fd2016-03-03 20:57:21 -0800512 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700513 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800514 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700515}
516
Matt Alexanderd9c56562020-05-21 10:49:17 +0000517function choosevariant()
518{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800519 echo "Variant choices are:"
520 local index=1
521 local v
522 for v in ${VARIANT_CHOICES[@]}
523 do
524 # The product name is the name of the directory containing
525 # the makefile we found, above.
526 echo " $index. $v"
527 index=$(($index+1))
528 done
529
530 local default_value=eng
531 local ANSWER
532
533 export TARGET_BUILD_VARIANT=
534 while [ -z "$TARGET_BUILD_VARIANT" ]
535 do
536 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000537 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800538 read ANSWER
539 else
540 echo $1
541 ANSWER=$1
542 fi
543
Matt Alexanderd9c56562020-05-21 10:49:17 +0000544 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800545 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000546 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
547 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200548 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800549 fi
550 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000551 if check_variant $ANSWER
552 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800553 export TARGET_BUILD_VARIANT=$ANSWER
554 else
555 echo "** Not a valid variant: $ANSWER"
556 fi
557 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000558 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559 break
560 fi
561 done
562}
563
Matt Alexanderd9c56562020-05-21 10:49:17 +0000564function choosecombo()
565{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700566 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700567
568 echo
569 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700570 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571
572 echo
573 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700574 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575
576 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800577 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800579 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800580 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581}
582
Matt Alexanderd9c56562020-05-21 10:49:17 +0000583function add_lunch_combo()
584{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800585 if [ -n "$ZSH_VERSION" ]; then
586 echo -n "${funcfiletrace[1]}: "
587 else
588 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
589 fi
590 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 -0800591}
592
Matt Alexanderd9c56562020-05-21 10:49:17 +0000593function print_lunch_menu()
594{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100596 local choices
597 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
598 local ret=$?
599
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 echo
601 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100603
Matt Alexanderd9c56562020-05-21 10:49:17 +0000604 if [ $ret -ne 0 ]
605 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100606 echo "Warning: Cannot display lunch menu."
607 echo
608 echo "Note: You can invoke lunch with an explicit target:"
609 echo
610 echo " usage: lunch [target]" >&2
611 echo
612 return
613 fi
614
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700615 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616
617 local i=1
618 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700619 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800620 do
621 echo " $i. $choice"
622 i=$(($i+1))
623 done
624
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700625 echo
626}
627
Matt Alexanderd9c56562020-05-21 10:49:17 +0000628function lunch()
629{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800630 local answer
631
Steven Moreland92793dc2020-02-25 18:30:18 -0800632 if [[ $# -gt 1 ]]; then
633 echo "usage: lunch [target]" >&2
634 return 1
635 fi
636
637 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800638 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700639 else
640 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700641 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800642 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700643 fi
644
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800645 local selection=
646
Matt Alexanderd9c56562020-05-21 10:49:17 +0000647 if [ -z "$answer" ]
648 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700649 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000650 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
651 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800652 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000653 if [ $answer -le ${#choices[@]} ]
654 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800655 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000656 if [ -n "$ZSH_VERSION" ]
657 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800658 selection=${choices[$(($answer))]}
659 else
660 selection=${choices[$(($answer-1))]}
661 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662 fi
Colin Cross88737132017-03-21 17:41:03 -0700663 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800664 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665 fi
666
Joe Onoratoda12daf2010-06-09 18:18:31 -0700667 export TARGET_BUILD_APPS=
668
Colin Cross88737132017-03-21 17:41:03 -0700669 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700670 product=${selection%%-*} # Trim everything after first dash
671 variant_and_version=${selection#*-} # Trim everything up to first dash
672 if [ "$variant_and_version" != "$selection" ]; then
673 variant=${variant_and_version%%-*}
674 if [ "$variant" != "$variant_and_version" ]; then
675 version=${variant_and_version#*-}
676 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700677 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800678
Matt Alexanderd9c56562020-05-21 10:49:17 +0000679 if [ -z "$product" ]
680 then
Ying Wang08800fd2016-03-03 20:57:21 -0800681 echo
Colin Cross88737132017-03-21 17:41:03 -0700682 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700683 return 1
684 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800685
Colin Cross88737132017-03-21 17:41:03 -0700686 TARGET_PRODUCT=$product \
687 TARGET_BUILD_VARIANT=$variant \
688 TARGET_PLATFORM_VERSION=$version \
689 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000690 if [ $? -ne 0 ]
691 then
Colin Cross88737132017-03-21 17:41:03 -0700692 return 1
693 fi
Colin Cross88737132017-03-21 17:41:03 -0700694 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
695 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700696 if [ -n "$version" ]; then
697 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
698 else
699 unset TARGET_PLATFORM_VERSION
700 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700701 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700702
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700703 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800704
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700705 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700706 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800707 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700708}
709
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700710unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700711# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000712function _lunch()
713{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700714 local cur prev opts
715 COMPREPLY=()
716 cur="${COMP_WORDS[COMP_CWORD]}"
717 prev="${COMP_WORDS[COMP_CWORD-1]}"
718
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700719 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800720 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700721 fi
722
723 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700724 return 0
725}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700726
Joe Onoratoda12daf2010-06-09 18:18:31 -0700727# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700728# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000729function tapas()
730{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700731 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800732 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700733 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700734 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800735 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)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700737 if [ "$showHelp" != "" ]; then
738 $(gettop)/build/make/tapasHelp.sh
739 return
740 fi
741
Ying Wang67f02922012-08-22 10:25:20 -0700742 if [ $(echo $arch | wc -w) -gt 1 ]; then
743 echo "tapas: Error: Multiple build archs supplied: $arch"
744 return
745 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700746 if [ $(echo $variant | wc -w) -gt 1 ]; then
747 echo "tapas: Error: Multiple build variants supplied: $variant"
748 return
749 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700750 if [ $(echo $density | wc -w) -gt 1 ]; then
751 echo "tapas: Error: Multiple densities supplied: $density"
752 return
753 fi
Ying Wang67f02922012-08-22 10:25:20 -0700754
Ying Wang0a76df52015-06-08 11:57:26 -0700755 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700756 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000757 x86) product=aosp_x86;;
758 arm64) product=aosp_arm64;;
759 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700760 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700761 if [ -z "$variant" ]; then
762 variant=eng
763 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700764 if [ -z "$apps" ]; then
765 apps=all
766 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600767 if [ -z "$density" ]; then
768 density=alldpi
769 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700770
Ying Wang67f02922012-08-22 10:25:20 -0700771 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700772 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700773 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700774 export TARGET_BUILD_TYPE=release
775 export TARGET_BUILD_APPS=$apps
776
Ying Wang08800fd2016-03-03 20:57:21 -0800777 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700778 set_stuff_for_environment
779 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800780 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700781}
782
Matt Alexanderd9c56562020-05-21 10:49:17 +0000783function gettop
784{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700785 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000786 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700787 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000788 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700789 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000790 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800791 # The following circumlocution (repeated below as well) ensures
792 # that we record the true directory name and not one that is
793 # faked up with symlink names.
794 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700795 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800796 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700797 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000798 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800799 \cd ..
synergyb112a402013-07-05 19:47:47 -0700800 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700801 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000802 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700803 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000804 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700805 fi
806 fi
807 fi
808}
809
Matt Alexanderd9c56562020-05-21 10:49:17 +0000810function croot()
811{
Christopher Ferris55257d22017-03-23 11:08:58 -0700812 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700813 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700814 if [ "$1" ]; then
815 \cd $(gettop)/$1
816 else
817 \cd $(gettop)
818 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700819 else
820 echo "Couldn't locate the top of the tree. Try setting TOP."
821 fi
822}
823
Matt Alexanderd9c56562020-05-21 10:49:17 +0000824function _croot()
825{
Anton Hanssonece9c482019-02-04 18:15:39 +0000826 local T=$(gettop)
827 if [ "$T" ]; then
828 local cur="${COMP_WORDS[COMP_CWORD]}"
829 k=0
830 for c in $(compgen -d ${T}/${cur}); do
831 COMPREPLY[k++]=${c#${T}/}/
832 done
833 fi
834}
835
Matt Alexanderd9c56562020-05-21 10:49:17 +0000836function cproj()
837{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700838 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700839 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700840 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700841 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
842 T=$PWD
843 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800844 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700845 return
846 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800847 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700848 done
Ying Wang9cd17642012-12-13 10:52:07 -0800849 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700850 echo "can't find Android.mk"
851}
852
Daniel Sandler47e0a882013-07-30 13:23:52 -0400853# simplified version of ps; output in the form
854# <pid> <procname>
855function qpid() {
856 local prepend=''
857 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000858 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400859 prepend=' '
860 append='$'
861 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000862 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800863 echo "usage: qpid [[--exact] <process name|pid>"
864 return 255
865 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400866
867 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000868 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800869 qpid | \grep "$prepend$EXE$append"
870 else
871 adb shell ps \
872 | tr -d '\r' \
873 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
874 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400875}
876
Steven Moreland74114f12020-09-10 01:23:32 +0000877# syswrite - disable verity, reboot if needed, and remount image
878#
879# Easy way to make system.img/etc writable
880function syswrite() {
881 adb wait-for-device && adb root || return 1
882 if [[ $(adb disable-verity | grep "reboot") ]]; then
883 echo "rebooting"
884 adb reboot && adb wait-for-device && adb root || return 1
885 fi
886 adb wait-for-device && adb remount || return 1
887}
888
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800889# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700890# that has the core-file-size limit set correctly
891#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800892# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700893# if its core-file-size limit is not set already.
894# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
895
Matt Alexanderd9c56562020-05-21 10:49:17 +0000896function coredump_setup()
897{
898 echo "Getting root...";
899 adb root;
900 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700901
Matt Alexanderd9c56562020-05-21 10:49:17 +0000902 echo "Remounting root partition read-write...";
903 adb shell mount -w -o remount -t rootfs rootfs;
904 sleep 1;
905 adb wait-for-device;
906 adb shell mkdir -p /cores;
907 adb shell mount -t tmpfs tmpfs /cores;
908 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700909
Matt Alexanderd9c56562020-05-21 10:49:17 +0000910 echo "Granting SELinux permission to dump in /cores...";
911 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700912
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913 echo "Set core pattern.";
914 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Ying Wang08800fd2016-03-03 20:57:21 -0800916 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700917}
918
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800919# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700921#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800922# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923# dump to actually be generated.
924
Matt Alexanderd9c56562020-05-21 10:49:17 +0000925function coredump_enable()
926{
927 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800928 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000929 printf "Expecting a PID!\n";
930 return;
931 fi;
932 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800933 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700934}
935
936# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000937# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700938#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800939# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700940# enabled globally.
941
Matt Alexanderd9c56562020-05-21 10:49:17 +0000942function core()
943{
944 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700945
Ying Wang08800fd2016-03-03 20:57:21 -0800946 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000947 printf "Expecting a PID!\n";
948 return;
949 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700950
Matt Alexanderd9c56562020-05-21 10:49:17 +0000951 local CORENAME=core.$PID;
952 local COREPATH=/cores/$CORENAME;
953 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700954
Matt Alexanderd9c56562020-05-21 10:49:17 +0000955 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700956
Matt Alexanderd9c56562020-05-21 10:49:17 +0000957 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800958 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000959 printf "\tSending SIG%s to %d...\n" $SIG $PID;
960 adb shell kill -$SIG $PID;
961 sleep 1;
962 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700963
Matt Alexanderd9c56562020-05-21 10:49:17 +0000964 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
965 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700966}
967
Christopher Tate744ee802009-11-12 15:33:08 -0800968# systemstack - dump the current stack trace of all threads in the system process
969# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000970function systemstack()
971{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800972 stacks system_server
973}
974
Michael Wrightaeed7212014-06-19 19:58:12 -0700975# Read the ELF header from /proc/$PID/exe to determine if the process is
976# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000977function is64bit()
978{
Ben Chengfba67bf2014-02-25 10:27:07 -0800979 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000980 if [ "$PID" ] ; then
981 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800982 echo "64"
983 else
984 echo ""
985 fi
986 else
987 echo ""
988 fi
989}
990
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700991case `uname -s` in
992 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000993 function sgrep()
994 {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800995 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|xml|sh|mk|aidl|vts|proto)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400996 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700997 }
Matt Alexanderd9c56562020-05-21 10:49:17 +0000998
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700999 ;;
1000 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001001 function sgrep()
1002 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001003 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\|proto\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001004 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001005 }
1006 ;;
1007esac
1008
Matt Alexanderd9c56562020-05-21 10:49:17 +00001009function gettargetarch
1010{
Raghu Gandham8da43102012-07-25 19:57:22 -07001011 get_build_var TARGET_ARCH
1012}
1013
Matt Alexanderd9c56562020-05-21 10:49:17 +00001014function ggrep()
1015{
Mike Frysinger5e479732015-09-22 18:13:48 -04001016 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1017 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001018}
1019
Matt Alexanderd9c56562020-05-21 10:49:17 +00001020function gogrep()
1021{
Orion Hodson831472d2019-10-25 11:35:15 +01001022 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1023 -exec grep --color -n "$@" {} +
1024}
1025
Matt Alexanderd9c56562020-05-21 10:49:17 +00001026function jgrep()
1027{
Mike Frysinger5e479732015-09-22 18:13:48 -04001028 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1029 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001030}
1031
Matt Alexanderd9c56562020-05-21 10:49:17 +00001032function cgrep()
1033{
Mike Frysinger5e479732015-09-22 18:13:48 -04001034 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' \) \
1035 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001036}
1037
Matt Alexanderd9c56562020-05-21 10:49:17 +00001038function resgrep()
1039{
Christopher Ferris55257d22017-03-23 11:08:58 -07001040 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001041 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1042 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1043 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044}
1045
Matt Alexanderd9c56562020-05-21 10:49:17 +00001046function mangrep()
1047{
Mike Frysinger5e479732015-09-22 18:13:48 -04001048 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1049 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001050}
1051
Matt Alexanderd9c56562020-05-21 10:49:17 +00001052function owngrep()
1053{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001054 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1055 -exec grep --color -n "$@" {} +
1056}
1057
Matt Alexanderd9c56562020-05-21 10:49:17 +00001058function sepgrep()
1059{
Mike Frysinger5e479732015-09-22 18:13:48 -04001060 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1061 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001062}
1063
Matt Alexanderd9c56562020-05-21 10:49:17 +00001064function rcgrep()
1065{
Mike Frysinger5e479732015-09-22 18:13:48 -04001066 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1067 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001068}
1069
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001070case `uname -s` in
1071 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001072 function mgrep()
1073 {
Orion Hodson831472d2019-10-25 11:35:15 +01001074 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 -04001075 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001076 }
1077
Matt Alexanderd9c56562020-05-21 10:49:17 +00001078 function treegrep()
1079 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001080 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001081 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001082 }
1083
1084 ;;
1085 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001086 function mgrep()
1087 {
Orion Hodson831472d2019-10-25 11:35:15 +01001088 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 -04001089 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001090 }
1091
Matt Alexanderd9c56562020-05-21 10:49:17 +00001092 function treegrep()
1093 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001094 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001095 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001096 }
1097
1098 ;;
1099esac
1100
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101function getprebuilt
1102{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001103 get_abs_build_var ANDROID_PREBUILTS
1104}
1105
Matt Alexanderd9c56562020-05-21 10:49:17 +00001106function tracedmdump()
1107{
Christopher Ferris55257d22017-03-23 11:08:58 -07001108 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001109 if [ ! "$T" ]; then
1110 echo "Couldn't locate the top of the tree. Try setting TOP."
1111 return
1112 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001113 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001114 local arch=$(gettargetarch)
1115 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001116
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001117 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001118 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119 echo "usage: tracedmdump tracename"
1120 return
1121 fi
1122
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001124 echo "Error: cannot find kernel: '$KERNEL'"
1125 return
1126 fi
1127
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001128 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001129 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001130 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1131 fi
1132
1133 echo "post-processing traces..."
1134 rm -f $TRACE/qtrace.dexlist
1135 post_trace $TRACE
1136 if [ $? -ne 0 ]; then
1137 echo "***"
1138 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1139 echo "***"
1140 return
1141 fi
1142 echo "generating dexlist output..."
1143 /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
1144 echo "generating dmtrace data..."
1145 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1146 echo "generating html file..."
1147 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1148 echo "done, see $TRACE/dmtrace.html for details"
1149 echo "or run:"
1150 echo " traceview $TRACE/dmtrace"
1151}
1152
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001153# communicate with a running device or emulator, set up necessary state,
1154# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001155function runhat()
1156{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001157 # process standard adb options
1158 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001159 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001160 adbTarget=$1
1161 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001163 adbTarget="$1 $2"
1164 shift 2
1165 fi
1166 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001168
1169 # runhat options
1170 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001171
Matt Alexanderd9c56562020-05-21 10:49:17 +00001172 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001173 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001174 return
1175 fi
1176
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001177 # confirm hat is available
1178 if [ -z $(which hat) ]; then
1179 echo "hat is not available in this configuration."
1180 return
1181 fi
1182
Andy McFaddenb6289852010-07-12 08:00:19 -07001183 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001184 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001185 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001186 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001187 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001188 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001189 echo -n "> "
1190 read
1191
The Android Open Source Project88b60792009-03-03 19:28:42 -08001192 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193
The Android Open Source Project88b60792009-03-03 19:28:42 -08001194 echo "Retrieving file $devFile..."
1195 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001196
The Android Open Source Project88b60792009-03-03 19:28:42 -08001197 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001198
The Android Open Source Project88b60792009-03-03 19:28:42 -08001199 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 echo "View the output by pointing your browser at http://localhost:7000/"
1201 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001202 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001203}
1204
Matt Alexanderd9c56562020-05-21 10:49:17 +00001205function getbugreports()
1206{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001207 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208
1209 if [ ! "$reports" ]; then
1210 echo "Could not locate any bugreports."
1211 return
1212 fi
1213
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001214 local report
1215 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001217 echo "/sdcard/bugreports/${report}"
1218 adb pull /sdcard/bugreports/${report} ${report}
1219 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001220 done
1221}
1222
Matt Alexanderd9c56562020-05-21 10:49:17 +00001223function getsdcardpath()
1224{
Victoria Lease1b296b42012-08-21 15:44:06 -07001225 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1226}
1227
Matt Alexanderd9c56562020-05-21 10:49:17 +00001228function getscreenshotpath()
1229{
Victoria Lease1b296b42012-08-21 15:44:06 -07001230 echo "$(getsdcardpath)/Pictures/Screenshots"
1231}
1232
Matt Alexanderd9c56562020-05-21 10:49:17 +00001233function getlastscreenshot()
1234{
Victoria Lease1b296b42012-08-21 15:44:06 -07001235 local screenshot_path=$(getscreenshotpath)
1236 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 +00001237 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001238 echo "No screenshots found."
1239 return
1240 fi
1241 echo "${screenshot}"
1242 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1243}
1244
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245function startviewserver()
1246{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001247 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001248 if [ $# -gt 0 ]; then
1249 port=$1
1250 fi
1251 adb shell service call window 1 i32 $port
1252}
1253
Matt Alexanderd9c56562020-05-21 10:49:17 +00001254function stopviewserver()
1255{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001256 adb shell service call window 2
1257}
1258
Matt Alexanderd9c56562020-05-21 10:49:17 +00001259function isviewserverstarted()
1260{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001261 adb shell service call window 3
1262}
1263
Matt Alexanderd9c56562020-05-21 10:49:17 +00001264function key_home()
1265{
Romain Guyb84049a2010-10-04 16:56:11 -07001266 adb shell input keyevent 3
1267}
1268
Matt Alexanderd9c56562020-05-21 10:49:17 +00001269function key_back()
1270{
Romain Guyb84049a2010-10-04 16:56:11 -07001271 adb shell input keyevent 4
1272}
1273
Matt Alexanderd9c56562020-05-21 10:49:17 +00001274function key_menu()
1275{
Romain Guyb84049a2010-10-04 16:56:11 -07001276 adb shell input keyevent 82
1277}
1278
Matt Alexanderd9c56562020-05-21 10:49:17 +00001279function smoketest()
1280{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001281 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1282 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1283 return
1284 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001285 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286 if [ ! "$T" ]; then
1287 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1288 return
1289 fi
1290
Ying Wang9cd17642012-12-13 10:52:07 -08001291 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001292 adb uninstall com.android.smoketest > /dev/null &&
1293 adb uninstall com.android.smoketest.tests > /dev/null &&
1294 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1295 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1296 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1297}
1298
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001299# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001300function runtest()
1301{
Christopher Ferris55257d22017-03-23 11:08:58 -07001302 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001303 if [ ! "$T" ]; then
1304 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1305 return
1306 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001307 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001308}
1309
The Android Open Source Project88b60792009-03-03 19:28:42 -08001310function godir () {
1311 if [[ -z "$1" ]]; then
1312 echo "Usage: godir <regex>"
1313 return
1314 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001315 local T=$(gettop)
1316 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001317 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001318 mkdir -p $OUT_DIR
1319 FILELIST=$OUT_DIR/filelist
1320 else
1321 FILELIST=$T/filelist
1322 fi
1323 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001324 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001325 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001326 echo " Done"
1327 echo ""
1328 fi
1329 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001330 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001331 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001332 echo "Not found"
1333 return
1334 fi
1335 local pathname
1336 local choice
1337 if [[ ${#lines[@]} > 1 ]]; then
1338 while [[ -z "$pathname" ]]; do
1339 local index=1
1340 local line
1341 for line in ${lines[@]}; do
1342 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001343 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001344 done
1345 echo
1346 echo -n "Select one: "
1347 unset choice
1348 read choice
1349 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1350 echo "Invalid choice"
1351 continue
1352 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001353 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001354 done
1355 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001356 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001357 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001358 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001359}
1360
Steven Moreland62054a42018-12-06 10:11:40 -08001361# Update module-info.json in out.
1362function refreshmod() {
1363 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1364 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1365 return 1
1366 fi
1367
1368 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1369
1370 # for the output of the next command
1371 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1372
1373 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001374 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001375 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1376}
1377
1378# List all modules for the current device, as cached in module-info.json. If any build change is
1379# made and it should be reflected in the output, you should run 'refreshmod' first.
1380function allmod() {
1381 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1382 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1383 return 1
1384 fi
1385
1386 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1387 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1388 refreshmod || return 1
1389 fi
1390
LuK1337b6a78192020-01-12 03:12:17 +01001391 python -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
Steven Moreland62054a42018-12-06 10:11:40 -08001392}
1393
Rett Berg78d1c932019-01-24 14:34:23 -08001394# Get the path of a specific module in the android tree, as cached in module-info.json. If any build change
Steven Moreland62054a42018-12-06 10:11:40 -08001395# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001396function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001397 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1398 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1399 return 1
1400 fi
1401
1402 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001403 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001404 return 1
1405 fi
1406
1407 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1408 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1409 refreshmod || return 1
1410 fi
1411
1412 local relpath=$(python -c "import json, os
1413module = '$1'
1414module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1415if module not in module_info:
1416 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001417print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001418
1419 if [ -z "$relpath" ]; then
1420 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1421 return 1
1422 else
Rett Berg78d1c932019-01-24 14:34:23 -08001423 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001424 fi
1425}
1426
Rett Berg78d1c932019-01-24 14:34:23 -08001427# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1428# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1429function gomod() {
1430 if [[ $# -ne 1 ]]; then
1431 echo "usage: gomod <module>" >&2
1432 return 1
1433 fi
1434
1435 local path="$(pathmod $@)"
1436 if [ -z "$path" ]; then
1437 return 1
1438 fi
1439 cd $path
1440}
1441
dimitry73b84812018-12-11 18:06:00 +01001442function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001443 local word=${COMP_WORDS[COMP_CWORD]}
1444 COMPREPLY=( $(allmod | grep -E "^$word") )
1445}
1446
Alex Rayf0d08eb2013-03-08 15:15:06 -08001447# Print colored exit condition
1448function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001449 "$@"
1450 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001451 if [ $retval -ne 0 ]
1452 then
Jacky Cao89483b82015-05-15 22:12:53 +08001453 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001454 else
Jacky Cao89483b82015-05-15 22:12:53 +08001455 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001456 fi
1457 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001458}
1459
Matt Alexanderd9c56562020-05-21 10:49:17 +00001460function get_make_command()
1461{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001462 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1463 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001464 # Always use the real make if -C is passed in
1465 for arg in "$@"; do
1466 if [[ $arg == -C* ]]; then
1467 echo command make
1468 return
1469 fi
1470 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001471 echo build/soong/soong_ui.bash --make-mode
1472 else
1473 echo command make
1474 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001475}
1476
Matt Alexanderd9c56562020-05-21 10:49:17 +00001477function _wrap_build()
1478{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001479 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1480 "$@"
1481 return $?
1482 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001483 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001484 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001485 local ret=$?
1486 local end_time=$(date +"%s")
1487 local tdiff=$(($end_time-$start_time))
1488 local hours=$(($tdiff / 3600 ))
1489 local mins=$((($tdiff % 3600) / 60))
1490 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001491 local ncolors=$(tput colors 2>/dev/null)
1492 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001493 color_failed=$'\E'"[0;31m"
1494 color_success=$'\E'"[0;32m"
1495 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001496 else
1497 color_failed=""
1498 color_success=""
1499 color_reset=""
1500 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001501 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001502 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001503 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001504 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001505 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001506 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001507 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001508 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001509 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001510 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001511 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001512 printf "(%s seconds)" $secs
1513 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001514 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001515 echo
1516 return $ret
1517}
1518
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001519function _trigger_build()
1520(
1521 local -r bc="$1"; shift
1522 if T="$(gettop)"; then
1523 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1524 else
1525 echo "Couldn't locate the top of the tree. Try setting TOP."
1526 fi
1527)
1528
1529function m()
1530(
1531 _trigger_build "all-modules" "$@"
1532)
1533
1534function mm()
1535(
1536 _trigger_build "modules-in-a-dir-no-deps" "$@"
1537)
1538
1539function mmm()
1540(
1541 _trigger_build "modules-in-dirs-no-deps" "$@"
1542)
1543
1544function mma()
1545(
1546 _trigger_build "modules-in-a-dir" "$@"
1547)
1548
1549function mmma()
1550(
1551 _trigger_build "modules-in-dirs" "$@"
1552)
1553
Matt Alexanderd9c56562020-05-21 10:49:17 +00001554function make()
1555{
Dan Willemsene9842242017-07-28 13:00:13 -07001556 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001557}
1558
Matt Alexanderd9c56562020-05-21 10:49:17 +00001559function provision()
1560{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001561 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1562 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1563 return 1
1564 fi
1565 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1566 echo "There is no provisioning script for the device." >&2
1567 return 1
1568 fi
1569
1570 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001571 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001572 shift 1
1573 else
1574 echo "This action will reflash your device."
1575 echo ""
1576 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1577 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001578 echo -n "Are you sure you want to do this (yes/no)? "
1579 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001580 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001581 echo "Not taking any action. Exiting." >&2
1582 return 1
1583 fi
1584 fi
1585 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1586}
1587
Jim Tanga881a252018-06-19 16:34:41 +08001588# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001589function enable_zsh_completion() {
1590 # Don't override user's options if bash-style completion is already enabled.
1591 if ! declare -f complete >/dev/null; then
1592 autoload -U compinit && compinit
1593 autoload -U bashcompinit && bashcompinit
1594 fi
Jim Tanga881a252018-06-19 16:34:41 +08001595}
1596
1597function validate_current_shell() {
1598 local current_sh="$(ps -o command -p $$)"
1599 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001600 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001601 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001602 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001603 *zsh*)
1604 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001605 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001606 *)
Jim Tanga881a252018-06-19 16:34:41 +08001607 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 -07001608 ;;
1609 esac
Jim Tanga881a252018-06-19 16:34:41 +08001610}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001611
1612# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001613# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1614# load those.
1615#
1616# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001617function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001618 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001619 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001620 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001621 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 -08001622 if [ -n "$allowed" ]; then
1623 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1624 echo " $allowed"
1625 echo " $f"
1626 return
1627 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001628 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001629 done
1630
1631 allowed_files=
1632 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001633 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001634 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001635 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001636
1637 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001638 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001639 else
1640 echo "ignoring $f, not in $allowed"
1641 fi
Jim Tanga881a252018-06-19 16:34:41 +08001642 done
1643 done
1644}
Kenny Root52aa81c2011-07-15 11:07:06 -07001645
Dan Albertbab814f2020-08-26 15:34:53 -07001646function showcommands() {
1647 local T=$(gettop)
1648 if [[ -z "$TARGET_PRODUCT" ]]; then
1649 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1650 return
1651 fi
1652 case $(uname -s) in
1653 Darwin)
1654 PREBUILT_NAME=darwin-x86
1655 ;;
1656 Linux)
1657 PREBUILT_NAME=linux-x86
1658 ;;
1659 *)
1660 >&2 echo Unknown host $(uname -s)
1661 return
1662 ;;
1663 esac
1664 if [[ -z "$OUT_DIR" ]]; then
1665 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1666 OUT_DIR=out
1667 else
1668 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1669 fi
1670 fi
1671 if [[ "$1" == "--regenerate" ]]; then
1672 shift 1
1673 NINJA_ARGS="-t commands $@" m
1674 else
1675 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1676 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1677 -t commands "$@")
1678 fi
1679}
1680
Jim Tanga881a252018-06-19 16:34:41 +08001681validate_current_shell
1682source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001683addcompletions