blob: 8fa608becbf02ff8f61b9c5e3b12bb73d4813cd1 [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
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900310 unset ANDROID_SOONG_HOST_OUT
311 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
312
Simran Basidd050ed2017-02-13 13:46:48 -0800313 unset ANDROID_HOST_OUT_TESTCASES
314 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
315
316 unset ANDROID_TARGET_OUT_TESTCASES
317 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
318
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800319 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700320 # TODO: fix the path
321 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
322}
323
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000324function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000325{
326 local T="$(gettop)"
327 if [ ! "$T" ]; then
328 echo "Couldn't locate the top of the tree. Try setting TOP."
329 return
330 fi
331
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000332 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500333 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
334 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000335 fi
336
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000337 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000338}
339
Matt Alexanderd9c56562020-05-21 10:49:17 +0000340function printconfig()
341{
Christopher Ferris55257d22017-03-23 11:08:58 -0700342 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800343 if [ ! "$T" ]; then
344 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
345 return
346 fi
347 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700348}
349
Matt Alexanderd9c56562020-05-21 10:49:17 +0000350function set_stuff_for_environment()
351{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800352 setpaths
353 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700354
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800355 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700356 # With this environment variable new GCC can apply colors to warnings/errors
357 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 -0700358}
359
Matt Alexanderd9c56562020-05-21 10:49:17 +0000360function set_sequence_number()
361{
Colin Cross88737132017-03-21 17:41:03 -0700362 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700363}
364
Makoto Onukida971062018-06-18 10:15:19 -0700365# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
366function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800367 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700368 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800369 *:"$cmd":*)
370 return 1
371 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700372 esac
373 return 0
374}
375
Matt Alexanderd9c56562020-05-21 10:49:17 +0000376function addcompletions()
377{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700378 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700379
Jim Tanga881a252018-06-19 16:34:41 +0800380 # Keep us from trying to run in something that's neither bash nor zsh.
381 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700382 return
383 fi
384
385 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800386 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700387 return
388 fi
389
Jim Tanga881a252018-06-19 16:34:41 +0800390 local completion_files=(
391 system/core/adb/adb.bash
392 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800393 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800394 )
Makoto Onukida971062018-06-18 10:15:19 -0700395 # Completion can be disabled selectively to allow users to use non-standard completion.
396 # e.g.
397 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
398 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800399 for f in ${completion_files[*]}; do
400 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700401 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700402 fi
403 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700404
Matt Alexanderd9c56562020-05-21 10:49:17 +0000405 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700406 complete -C "bit --tab" bit
407 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000408 if [ -z "$ZSH_VERSION" ]; then
409 # Doesn't work in zsh.
410 complete -o nospace -F _croot croot
411 fi
Jim Tanga881a252018-06-19 16:34:41 +0800412 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800413
dimitry73b84812018-12-11 18:06:00 +0100414 complete -F _complete_android_module_names gomod
415 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700416}
417
Matt Alexanderd9c56562020-05-21 10:49:17 +0000418function choosetype()
419{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700420 echo "Build type choices are:"
421 echo " 1. release"
422 echo " 2. debug"
423 echo
424
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800425 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700426 DEFAULT_NUM=1
427 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800429 export TARGET_BUILD_TYPE=
430 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700431 while [ -z $TARGET_BUILD_TYPE ]
432 do
433 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000434 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800435 read ANSWER
436 else
437 echo $1
438 ANSWER=$1
439 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700440 case $ANSWER in
441 "")
442 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
443 ;;
444 1)
445 export TARGET_BUILD_TYPE=release
446 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800447 release)
448 export TARGET_BUILD_TYPE=release
449 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450 2)
451 export TARGET_BUILD_TYPE=debug
452 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800453 debug)
454 export TARGET_BUILD_TYPE=debug
455 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456 *)
457 echo
458 echo "I didn't understand your response. Please try again."
459 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 ;;
461 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000462 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463 break
464 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 done
466
Ying Wang08800fd2016-03-03 20:57:21 -0800467 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700468 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800469 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700470}
471
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800472#
473# This function isn't really right: It chooses a TARGET_PRODUCT
474# based on the list of boards. Usually, that gets you something
475# that kinda works with a generic product, but really, you should
476# pick a product by name.
477#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000478function chooseproduct()
479{
Christopher Ferris55257d22017-03-23 11:08:58 -0700480 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000481 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 default_value=$TARGET_PRODUCT
483 else
Ying Wang0a76df52015-06-08 11:57:26 -0700484 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700485 fi
486
Ying Wang08800fd2016-03-03 20:57:21 -0800487 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800488 export TARGET_PRODUCT=
489 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 while [ -z "$TARGET_PRODUCT" ]
491 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700492 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000493 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800494 read ANSWER
495 else
496 echo $1
497 ANSWER=$1
498 fi
499
Matt Alexanderd9c56562020-05-21 10:49:17 +0000500 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700501 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000503 if check_product $ANSWER
504 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800505 export TARGET_PRODUCT=$ANSWER
506 else
507 echo "** Not a valid product: $ANSWER"
508 fi
509 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000510 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800511 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512 fi
513 done
514
Ying Wang08800fd2016-03-03 20:57:21 -0800515 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700516 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800517 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700518}
519
Matt Alexanderd9c56562020-05-21 10:49:17 +0000520function choosevariant()
521{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800522 echo "Variant choices are:"
523 local index=1
524 local v
525 for v in ${VARIANT_CHOICES[@]}
526 do
527 # The product name is the name of the directory containing
528 # the makefile we found, above.
529 echo " $index. $v"
530 index=$(($index+1))
531 done
532
533 local default_value=eng
534 local ANSWER
535
536 export TARGET_BUILD_VARIANT=
537 while [ -z "$TARGET_BUILD_VARIANT" ]
538 do
539 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000540 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800541 read ANSWER
542 else
543 echo $1
544 ANSWER=$1
545 fi
546
Matt Alexanderd9c56562020-05-21 10:49:17 +0000547 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000549 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
550 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200551 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800552 fi
553 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000554 if check_variant $ANSWER
555 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556 export TARGET_BUILD_VARIANT=$ANSWER
557 else
558 echo "** Not a valid variant: $ANSWER"
559 fi
560 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000561 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562 break
563 fi
564 done
565}
566
Matt Alexanderd9c56562020-05-21 10:49:17 +0000567function choosecombo()
568{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700569 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700570
571 echo
572 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700573 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574
575 echo
576 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700577 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578
579 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800580 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800583 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584}
585
Matt Alexanderd9c56562020-05-21 10:49:17 +0000586function add_lunch_combo()
587{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800588 if [ -n "$ZSH_VERSION" ]; then
589 echo -n "${funcfiletrace[1]}: "
590 else
591 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
592 fi
593 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 -0800594}
595
Matt Alexanderd9c56562020-05-21 10:49:17 +0000596function print_lunch_menu()
597{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100599 local choices
600 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
601 local ret=$?
602
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700603 echo
604 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100606
Matt Alexanderd9c56562020-05-21 10:49:17 +0000607 if [ $ret -ne 0 ]
608 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100609 echo "Warning: Cannot display lunch menu."
610 echo
611 echo "Note: You can invoke lunch with an explicit target:"
612 echo
613 echo " usage: lunch [target]" >&2
614 echo
615 return
616 fi
617
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700618 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619
620 local i=1
621 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700622 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800623 do
624 echo " $i. $choice"
625 i=$(($i+1))
626 done
627
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700628 echo
629}
630
Matt Alexanderd9c56562020-05-21 10:49:17 +0000631function lunch()
632{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633 local answer
634
Steven Moreland92793dc2020-02-25 18:30:18 -0800635 if [[ $# -gt 1 ]]; then
636 echo "usage: lunch [target]" >&2
637 return 1
638 fi
639
640 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800641 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700642 else
643 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700644 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800645 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700646 fi
647
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648 local selection=
649
Matt Alexanderd9c56562020-05-21 10:49:17 +0000650 if [ -z "$answer" ]
651 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700652 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000653 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
654 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800655 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000656 if [ $answer -le ${#choices[@]} ]
657 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800658 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000659 if [ -n "$ZSH_VERSION" ]
660 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800661 selection=${choices[$(($answer))]}
662 else
663 selection=${choices[$(($answer-1))]}
664 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800665 fi
Colin Cross88737132017-03-21 17:41:03 -0700666 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800667 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700668 fi
669
Joe Onoratoda12daf2010-06-09 18:18:31 -0700670 export TARGET_BUILD_APPS=
671
Colin Cross88737132017-03-21 17:41:03 -0700672 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700673 product=${selection%%-*} # Trim everything after first dash
674 variant_and_version=${selection#*-} # Trim everything up to first dash
675 if [ "$variant_and_version" != "$selection" ]; then
676 variant=${variant_and_version%%-*}
677 if [ "$variant" != "$variant_and_version" ]; then
678 version=${variant_and_version#*-}
679 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700680 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800681
Matt Alexanderd9c56562020-05-21 10:49:17 +0000682 if [ -z "$product" ]
683 then
Ying Wang08800fd2016-03-03 20:57:21 -0800684 echo
Colin Cross88737132017-03-21 17:41:03 -0700685 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700686 return 1
687 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800688
Colin Cross88737132017-03-21 17:41:03 -0700689 TARGET_PRODUCT=$product \
690 TARGET_BUILD_VARIANT=$variant \
691 TARGET_PLATFORM_VERSION=$version \
692 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693 if [ $? -ne 0 ]
694 then
Colin Cross88737132017-03-21 17:41:03 -0700695 return 1
696 fi
Colin Cross88737132017-03-21 17:41:03 -0700697 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
698 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700699 if [ -n "$version" ]; then
700 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
701 else
702 unset TARGET_PLATFORM_VERSION
703 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700704 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700705
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700706 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800707
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700708 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700709 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800710 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700711}
712
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700713unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700714# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000715function _lunch()
716{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700717 local cur prev opts
718 COMPREPLY=()
719 cur="${COMP_WORDS[COMP_CWORD]}"
720 prev="${COMP_WORDS[COMP_CWORD-1]}"
721
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700722 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800723 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700724 fi
725
726 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700727 return 0
728}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700729
Joe Onoratoda12daf2010-06-09 18:18:31 -0700730# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700731# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000732function tapas()
733{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700734 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800735 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700736 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700737 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 -0800738 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 -0700739
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700740 if [ "$showHelp" != "" ]; then
741 $(gettop)/build/make/tapasHelp.sh
742 return
743 fi
744
Ying Wang67f02922012-08-22 10:25:20 -0700745 if [ $(echo $arch | wc -w) -gt 1 ]; then
746 echo "tapas: Error: Multiple build archs supplied: $arch"
747 return
748 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700749 if [ $(echo $variant | wc -w) -gt 1 ]; then
750 echo "tapas: Error: Multiple build variants supplied: $variant"
751 return
752 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700753 if [ $(echo $density | wc -w) -gt 1 ]; then
754 echo "tapas: Error: Multiple densities supplied: $density"
755 return
756 fi
Ying Wang67f02922012-08-22 10:25:20 -0700757
Ying Wang0a76df52015-06-08 11:57:26 -0700758 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700759 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000760 x86) product=aosp_x86;;
761 arm64) product=aosp_arm64;;
762 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700763 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700764 if [ -z "$variant" ]; then
765 variant=eng
766 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700767 if [ -z "$apps" ]; then
768 apps=all
769 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600770 if [ -z "$density" ]; then
771 density=alldpi
772 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700773
Ying Wang67f02922012-08-22 10:25:20 -0700774 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700775 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700776 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700777 export TARGET_BUILD_TYPE=release
778 export TARGET_BUILD_APPS=$apps
779
Ying Wang08800fd2016-03-03 20:57:21 -0800780 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700781 set_stuff_for_environment
782 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800783 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700784}
785
Matt Alexanderd9c56562020-05-21 10:49:17 +0000786function gettop
787{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700788 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000789 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700790 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000791 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700792 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000793 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800794 # The following circumlocution (repeated below as well) ensures
795 # that we record the true directory name and not one that is
796 # faked up with symlink names.
797 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800799 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700800 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000801 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800802 \cd ..
synergyb112a402013-07-05 19:47:47 -0700803 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700804 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000805 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700806 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000807 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700808 fi
809 fi
810 fi
811}
812
Matt Alexanderd9c56562020-05-21 10:49:17 +0000813function croot()
814{
Christopher Ferris55257d22017-03-23 11:08:58 -0700815 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700816 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700817 if [ "$1" ]; then
818 \cd $(gettop)/$1
819 else
820 \cd $(gettop)
821 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700822 else
823 echo "Couldn't locate the top of the tree. Try setting TOP."
824 fi
825}
826
Matt Alexanderd9c56562020-05-21 10:49:17 +0000827function _croot()
828{
Anton Hanssonece9c482019-02-04 18:15:39 +0000829 local T=$(gettop)
830 if [ "$T" ]; then
831 local cur="${COMP_WORDS[COMP_CWORD]}"
832 k=0
833 for c in $(compgen -d ${T}/${cur}); do
834 COMPREPLY[k++]=${c#${T}/}/
835 done
836 fi
837}
838
Matt Alexanderd9c56562020-05-21 10:49:17 +0000839function cproj()
840{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700841 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700842 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700843 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700844 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
845 T=$PWD
846 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800847 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700848 return
849 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800850 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700851 done
Ying Wang9cd17642012-12-13 10:52:07 -0800852 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700853 echo "can't find Android.mk"
854}
855
Daniel Sandler47e0a882013-07-30 13:23:52 -0400856# simplified version of ps; output in the form
857# <pid> <procname>
858function qpid() {
859 local prepend=''
860 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000861 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400862 prepend=' '
863 append='$'
864 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000865 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800866 echo "usage: qpid [[--exact] <process name|pid>"
867 return 255
868 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400869
870 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000871 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800872 qpid | \grep "$prepend$EXE$append"
873 else
874 adb shell ps \
875 | tr -d '\r' \
876 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
877 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400878}
879
Steven Moreland74114f12020-09-10 01:23:32 +0000880# syswrite - disable verity, reboot if needed, and remount image
881#
882# Easy way to make system.img/etc writable
883function syswrite() {
884 adb wait-for-device && adb root || return 1
885 if [[ $(adb disable-verity | grep "reboot") ]]; then
886 echo "rebooting"
887 adb reboot && adb wait-for-device && adb root || return 1
888 fi
889 adb wait-for-device && adb remount || return 1
890}
891
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800892# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700893# that has the core-file-size limit set correctly
894#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800895# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700896# if its core-file-size limit is not set already.
897# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
898
Matt Alexanderd9c56562020-05-21 10:49:17 +0000899function coredump_setup()
900{
901 echo "Getting root...";
902 adb root;
903 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700904
Matt Alexanderd9c56562020-05-21 10:49:17 +0000905 echo "Remounting root partition read-write...";
906 adb shell mount -w -o remount -t rootfs rootfs;
907 sleep 1;
908 adb wait-for-device;
909 adb shell mkdir -p /cores;
910 adb shell mount -t tmpfs tmpfs /cores;
911 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700912
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913 echo "Granting SELinux permission to dump in /cores...";
914 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Matt Alexanderd9c56562020-05-21 10:49:17 +0000916 echo "Set core pattern.";
917 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700918
Ying Wang08800fd2016-03-03 20:57:21 -0800919 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700920}
921
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800922# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000923# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700924#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800925# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700926# dump to actually be generated.
927
Matt Alexanderd9c56562020-05-21 10:49:17 +0000928function coredump_enable()
929{
930 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800931 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000932 printf "Expecting a PID!\n";
933 return;
934 fi;
935 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800936 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700937}
938
939# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000940# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700941#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800942# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700943# enabled globally.
944
Matt Alexanderd9c56562020-05-21 10:49:17 +0000945function core()
946{
947 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700948
Ying Wang08800fd2016-03-03 20:57:21 -0800949 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000950 printf "Expecting a PID!\n";
951 return;
952 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700953
Matt Alexanderd9c56562020-05-21 10:49:17 +0000954 local CORENAME=core.$PID;
955 local COREPATH=/cores/$CORENAME;
956 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700957
Matt Alexanderd9c56562020-05-21 10:49:17 +0000958 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700959
Matt Alexanderd9c56562020-05-21 10:49:17 +0000960 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800961 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000962 printf "\tSending SIG%s to %d...\n" $SIG $PID;
963 adb shell kill -$SIG $PID;
964 sleep 1;
965 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700966
Matt Alexanderd9c56562020-05-21 10:49:17 +0000967 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
968 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700969}
970
Christopher Tate744ee802009-11-12 15:33:08 -0800971# systemstack - dump the current stack trace of all threads in the system process
972# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000973function systemstack()
974{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800975 stacks system_server
976}
977
Michael Wrightaeed7212014-06-19 19:58:12 -0700978# Read the ELF header from /proc/$PID/exe to determine if the process is
979# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000980function is64bit()
981{
Ben Chengfba67bf2014-02-25 10:27:07 -0800982 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000983 if [ "$PID" ] ; then
984 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800985 echo "64"
986 else
987 echo ""
988 fi
989 else
990 echo ""
991 fi
992}
993
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700994case `uname -s` in
995 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996 function sgrep()
997 {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800998 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 -0400999 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001000 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001001
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001002 ;;
1003 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001004 function sgrep()
1005 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001006 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 -04001007 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001008 }
1009 ;;
1010esac
1011
Matt Alexanderd9c56562020-05-21 10:49:17 +00001012function gettargetarch
1013{
Raghu Gandham8da43102012-07-25 19:57:22 -07001014 get_build_var TARGET_ARCH
1015}
1016
Matt Alexanderd9c56562020-05-21 10:49:17 +00001017function ggrep()
1018{
Mike Frysinger5e479732015-09-22 18:13:48 -04001019 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1020 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001021}
1022
Matt Alexanderd9c56562020-05-21 10:49:17 +00001023function gogrep()
1024{
Orion Hodson831472d2019-10-25 11:35:15 +01001025 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1026 -exec grep --color -n "$@" {} +
1027}
1028
Matt Alexanderd9c56562020-05-21 10:49:17 +00001029function jgrep()
1030{
Mike Frysinger5e479732015-09-22 18:13:48 -04001031 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1032 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001033}
1034
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035function cgrep()
1036{
Mike Frysinger5e479732015-09-22 18:13:48 -04001037 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' \) \
1038 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001039}
1040
Matt Alexanderd9c56562020-05-21 10:49:17 +00001041function resgrep()
1042{
Christopher Ferris55257d22017-03-23 11:08:58 -07001043 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001044 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1045 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1046 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001047}
1048
Matt Alexanderd9c56562020-05-21 10:49:17 +00001049function mangrep()
1050{
Mike Frysinger5e479732015-09-22 18:13:48 -04001051 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1052 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001053}
1054
Matt Alexanderd9c56562020-05-21 10:49:17 +00001055function owngrep()
1056{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001057 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1058 -exec grep --color -n "$@" {} +
1059}
1060
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061function sepgrep()
1062{
Mike Frysinger5e479732015-09-22 18:13:48 -04001063 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1064 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001065}
1066
Matt Alexanderd9c56562020-05-21 10:49:17 +00001067function rcgrep()
1068{
Mike Frysinger5e479732015-09-22 18:13:48 -04001069 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1070 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001071}
1072
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001073case `uname -s` in
1074 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001075 function mgrep()
1076 {
Orion Hodson831472d2019-10-25 11:35:15 +01001077 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 -04001078 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001079 }
1080
Matt Alexanderd9c56562020-05-21 10:49:17 +00001081 function treegrep()
1082 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001083 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 -04001084 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001085 }
1086
1087 ;;
1088 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089 function mgrep()
1090 {
Orion Hodson831472d2019-10-25 11:35:15 +01001091 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 -04001092 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001093 }
1094
Matt Alexanderd9c56562020-05-21 10:49:17 +00001095 function treegrep()
1096 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001097 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 -04001098 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001099 }
1100
1101 ;;
1102esac
1103
Matt Alexanderd9c56562020-05-21 10:49:17 +00001104function getprebuilt
1105{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001106 get_abs_build_var ANDROID_PREBUILTS
1107}
1108
Matt Alexanderd9c56562020-05-21 10:49:17 +00001109function tracedmdump()
1110{
Christopher Ferris55257d22017-03-23 11:08:58 -07001111 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001112 if [ ! "$T" ]; then
1113 echo "Couldn't locate the top of the tree. Try setting TOP."
1114 return
1115 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001116 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001117 local arch=$(gettargetarch)
1118 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001120 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001121 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001122 echo "usage: tracedmdump tracename"
1123 return
1124 fi
1125
Matt Alexanderd9c56562020-05-21 10:49:17 +00001126 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001127 echo "Error: cannot find kernel: '$KERNEL'"
1128 return
1129 fi
1130
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001131 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001132 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001133 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1134 fi
1135
1136 echo "post-processing traces..."
1137 rm -f $TRACE/qtrace.dexlist
1138 post_trace $TRACE
1139 if [ $? -ne 0 ]; then
1140 echo "***"
1141 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1142 echo "***"
1143 return
1144 fi
1145 echo "generating dexlist output..."
1146 /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
1147 echo "generating dmtrace data..."
1148 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1149 echo "generating html file..."
1150 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1151 echo "done, see $TRACE/dmtrace.html for details"
1152 echo "or run:"
1153 echo " traceview $TRACE/dmtrace"
1154}
1155
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001156# communicate with a running device or emulator, set up necessary state,
1157# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001158function runhat()
1159{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001160 # process standard adb options
1161 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001163 adbTarget=$1
1164 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001165 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001166 adbTarget="$1 $2"
1167 shift 2
1168 fi
1169 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001170 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001171
1172 # runhat options
1173 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001174
Matt Alexanderd9c56562020-05-21 10:49:17 +00001175 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001176 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001177 return
1178 fi
1179
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001180 # confirm hat is available
1181 if [ -z $(which hat) ]; then
1182 echo "hat is not available in this configuration."
1183 return
1184 fi
1185
Andy McFaddenb6289852010-07-12 08:00:19 -07001186 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001187 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001188 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001189 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001190 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001191 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001192 echo -n "> "
1193 read
1194
The Android Open Source Project88b60792009-03-03 19:28:42 -08001195 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001196
The Android Open Source Project88b60792009-03-03 19:28:42 -08001197 echo "Retrieving file $devFile..."
1198 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001199
The Android Open Source Project88b60792009-03-03 19:28:42 -08001200 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001201
The Android Open Source Project88b60792009-03-03 19:28:42 -08001202 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001203 echo "View the output by pointing your browser at http://localhost:7000/"
1204 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001205 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206}
1207
Matt Alexanderd9c56562020-05-21 10:49:17 +00001208function getbugreports()
1209{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001210 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001211
1212 if [ ! "$reports" ]; then
1213 echo "Could not locate any bugreports."
1214 return
1215 fi
1216
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001217 local report
1218 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001220 echo "/sdcard/bugreports/${report}"
1221 adb pull /sdcard/bugreports/${report} ${report}
1222 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223 done
1224}
1225
Matt Alexanderd9c56562020-05-21 10:49:17 +00001226function getsdcardpath()
1227{
Victoria Lease1b296b42012-08-21 15:44:06 -07001228 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1229}
1230
Matt Alexanderd9c56562020-05-21 10:49:17 +00001231function getscreenshotpath()
1232{
Victoria Lease1b296b42012-08-21 15:44:06 -07001233 echo "$(getsdcardpath)/Pictures/Screenshots"
1234}
1235
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236function getlastscreenshot()
1237{
Victoria Lease1b296b42012-08-21 15:44:06 -07001238 local screenshot_path=$(getscreenshotpath)
1239 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 +00001240 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001241 echo "No screenshots found."
1242 return
1243 fi
1244 echo "${screenshot}"
1245 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1246}
1247
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248function startviewserver()
1249{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001250 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001251 if [ $# -gt 0 ]; then
1252 port=$1
1253 fi
1254 adb shell service call window 1 i32 $port
1255}
1256
Matt Alexanderd9c56562020-05-21 10:49:17 +00001257function stopviewserver()
1258{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001259 adb shell service call window 2
1260}
1261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262function isviewserverstarted()
1263{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264 adb shell service call window 3
1265}
1266
Matt Alexanderd9c56562020-05-21 10:49:17 +00001267function key_home()
1268{
Romain Guyb84049a2010-10-04 16:56:11 -07001269 adb shell input keyevent 3
1270}
1271
Matt Alexanderd9c56562020-05-21 10:49:17 +00001272function key_back()
1273{
Romain Guyb84049a2010-10-04 16:56:11 -07001274 adb shell input keyevent 4
1275}
1276
Matt Alexanderd9c56562020-05-21 10:49:17 +00001277function key_menu()
1278{
Romain Guyb84049a2010-10-04 16:56:11 -07001279 adb shell input keyevent 82
1280}
1281
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282function smoketest()
1283{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001284 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1285 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1286 return
1287 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001288 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001289 if [ ! "$T" ]; then
1290 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1291 return
1292 fi
1293
Ying Wang9cd17642012-12-13 10:52:07 -08001294 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001295 adb uninstall com.android.smoketest > /dev/null &&
1296 adb uninstall com.android.smoketest.tests > /dev/null &&
1297 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1298 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1299 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1300}
1301
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001302# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001303function runtest()
1304{
Christopher Ferris55257d22017-03-23 11:08:58 -07001305 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001306 if [ ! "$T" ]; then
1307 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1308 return
1309 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001310 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001311}
1312
The Android Open Source Project88b60792009-03-03 19:28:42 -08001313function godir () {
1314 if [[ -z "$1" ]]; then
1315 echo "Usage: godir <regex>"
1316 return
1317 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001318 local T=$(gettop)
1319 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001320 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001321 mkdir -p $OUT_DIR
1322 FILELIST=$OUT_DIR/filelist
1323 else
1324 FILELIST=$T/filelist
1325 fi
1326 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001327 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001328 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001329 echo " Done"
1330 echo ""
1331 fi
1332 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001333 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001334 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001335 echo "Not found"
1336 return
1337 fi
1338 local pathname
1339 local choice
1340 if [[ ${#lines[@]} > 1 ]]; then
1341 while [[ -z "$pathname" ]]; do
1342 local index=1
1343 local line
1344 for line in ${lines[@]}; do
1345 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001346 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001347 done
1348 echo
1349 echo -n "Select one: "
1350 unset choice
1351 read choice
1352 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1353 echo "Invalid choice"
1354 continue
1355 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001356 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001357 done
1358 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001359 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001360 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001361 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001362}
1363
Steven Moreland62054a42018-12-06 10:11:40 -08001364# Update module-info.json in out.
1365function refreshmod() {
1366 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1367 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1368 return 1
1369 fi
1370
1371 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1372
1373 # for the output of the next command
1374 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1375
1376 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001377 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001378 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1379}
1380
1381# List all modules for the current device, as cached in module-info.json. If any build change is
1382# made and it should be reflected in the output, you should run 'refreshmod' first.
1383function allmod() {
1384 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1385 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1386 return 1
1387 fi
1388
1389 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1390 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1391 refreshmod || return 1
1392 fi
1393
LuK1337b6a78192020-01-12 03:12:17 +01001394 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 -08001395}
1396
Rett Berg78d1c932019-01-24 14:34:23 -08001397# 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 -08001398# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001399function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001400 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1401 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1402 return 1
1403 fi
1404
1405 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001406 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001407 return 1
1408 fi
1409
1410 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1411 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1412 refreshmod || return 1
1413 fi
1414
1415 local relpath=$(python -c "import json, os
1416module = '$1'
1417module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1418if module not in module_info:
1419 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001420print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001421
1422 if [ -z "$relpath" ]; then
1423 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1424 return 1
1425 else
Rett Berg78d1c932019-01-24 14:34:23 -08001426 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001427 fi
1428}
1429
Rett Berg78d1c932019-01-24 14:34:23 -08001430# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1431# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1432function gomod() {
1433 if [[ $# -ne 1 ]]; then
1434 echo "usage: gomod <module>" >&2
1435 return 1
1436 fi
1437
1438 local path="$(pathmod $@)"
1439 if [ -z "$path" ]; then
1440 return 1
1441 fi
1442 cd $path
1443}
1444
dimitry73b84812018-12-11 18:06:00 +01001445function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001446 local word=${COMP_WORDS[COMP_CWORD]}
1447 COMPREPLY=( $(allmod | grep -E "^$word") )
1448}
1449
Alex Rayf0d08eb2013-03-08 15:15:06 -08001450# Print colored exit condition
1451function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001452 "$@"
1453 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001454 if [ $retval -ne 0 ]
1455 then
Jacky Cao89483b82015-05-15 22:12:53 +08001456 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001457 else
Jacky Cao89483b82015-05-15 22:12:53 +08001458 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001459 fi
1460 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001461}
1462
Matt Alexanderd9c56562020-05-21 10:49:17 +00001463function get_make_command()
1464{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001465 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1466 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001467 # Always use the real make if -C is passed in
1468 for arg in "$@"; do
1469 if [[ $arg == -C* ]]; then
1470 echo command make
1471 return
1472 fi
1473 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001474 echo build/soong/soong_ui.bash --make-mode
1475 else
1476 echo command make
1477 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001478}
1479
Matt Alexanderd9c56562020-05-21 10:49:17 +00001480function _wrap_build()
1481{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001482 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1483 "$@"
1484 return $?
1485 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001486 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001487 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001488 local ret=$?
1489 local end_time=$(date +"%s")
1490 local tdiff=$(($end_time-$start_time))
1491 local hours=$(($tdiff / 3600 ))
1492 local mins=$((($tdiff % 3600) / 60))
1493 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001494 local ncolors=$(tput colors 2>/dev/null)
1495 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001496 color_failed=$'\E'"[0;31m"
1497 color_success=$'\E'"[0;32m"
1498 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001499 else
1500 color_failed=""
1501 color_success=""
1502 color_reset=""
1503 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001504 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001505 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001506 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001507 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001508 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001509 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001510 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001511 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001512 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001513 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001514 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001515 printf "(%s seconds)" $secs
1516 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001517 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001518 echo
1519 return $ret
1520}
1521
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001522function _trigger_build()
1523(
1524 local -r bc="$1"; shift
1525 if T="$(gettop)"; then
1526 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1527 else
1528 echo "Couldn't locate the top of the tree. Try setting TOP."
1529 fi
1530)
1531
1532function m()
1533(
1534 _trigger_build "all-modules" "$@"
1535)
1536
1537function mm()
1538(
1539 _trigger_build "modules-in-a-dir-no-deps" "$@"
1540)
1541
1542function mmm()
1543(
1544 _trigger_build "modules-in-dirs-no-deps" "$@"
1545)
1546
1547function mma()
1548(
1549 _trigger_build "modules-in-a-dir" "$@"
1550)
1551
1552function mmma()
1553(
1554 _trigger_build "modules-in-dirs" "$@"
1555)
1556
Matt Alexanderd9c56562020-05-21 10:49:17 +00001557function make()
1558{
Dan Willemsene9842242017-07-28 13:00:13 -07001559 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001560}
1561
Matt Alexanderd9c56562020-05-21 10:49:17 +00001562function provision()
1563{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001564 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1565 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1566 return 1
1567 fi
1568 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1569 echo "There is no provisioning script for the device." >&2
1570 return 1
1571 fi
1572
1573 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001574 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001575 shift 1
1576 else
1577 echo "This action will reflash your device."
1578 echo ""
1579 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1580 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001581 echo -n "Are you sure you want to do this (yes/no)? "
1582 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001583 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001584 echo "Not taking any action. Exiting." >&2
1585 return 1
1586 fi
1587 fi
1588 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1589}
1590
Jim Tanga881a252018-06-19 16:34:41 +08001591# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001592function enable_zsh_completion() {
1593 # Don't override user's options if bash-style completion is already enabled.
1594 if ! declare -f complete >/dev/null; then
1595 autoload -U compinit && compinit
1596 autoload -U bashcompinit && bashcompinit
1597 fi
Jim Tanga881a252018-06-19 16:34:41 +08001598}
1599
1600function validate_current_shell() {
1601 local current_sh="$(ps -o command -p $$)"
1602 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001603 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001604 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001605 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001606 *zsh*)
1607 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001608 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001609 *)
Jim Tanga881a252018-06-19 16:34:41 +08001610 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 -07001611 ;;
1612 esac
Jim Tanga881a252018-06-19 16:34:41 +08001613}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001614
1615# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001616# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1617# load those.
1618#
1619# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001620function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001621 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001622 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001623 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001624 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 -08001625 if [ -n "$allowed" ]; then
1626 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1627 echo " $allowed"
1628 echo " $f"
1629 return
1630 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001631 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001632 done
1633
1634 allowed_files=
1635 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001636 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001637 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001638 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001639
1640 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001641 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001642 else
1643 echo "ignoring $f, not in $allowed"
1644 fi
Jim Tanga881a252018-06-19 16:34:41 +08001645 done
1646 done
1647}
Kenny Root52aa81c2011-07-15 11:07:06 -07001648
Dan Albertbab814f2020-08-26 15:34:53 -07001649function showcommands() {
1650 local T=$(gettop)
1651 if [[ -z "$TARGET_PRODUCT" ]]; then
1652 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1653 return
1654 fi
1655 case $(uname -s) in
1656 Darwin)
1657 PREBUILT_NAME=darwin-x86
1658 ;;
1659 Linux)
1660 PREBUILT_NAME=linux-x86
1661 ;;
1662 *)
1663 >&2 echo Unknown host $(uname -s)
1664 return
1665 ;;
1666 esac
1667 if [[ -z "$OUT_DIR" ]]; then
1668 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1669 OUT_DIR=out
1670 else
1671 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1672 fi
1673 fi
1674 if [[ "$1" == "--regenerate" ]]; then
1675 shift 1
1676 NINJA_ARGS="-t commands $@" m
1677 else
1678 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1679 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1680 -t commands "$@")
1681 fi
1682}
1683
Jim Tanga881a252018-06-19 16:34:41 +08001684validate_current_shell
1685source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001686addcompletions