blob: c03e2cb7d23bc6c62e87f5af2cb39b6f8fca6ec5 [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.
Cole Faust24c36db2021-01-23 02:39:37 +000036- outmod: Gets the location of a module's installed outputs with a certain extension.
37- installmod: Adb installs a module's built APK.
38- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000039- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070040
Roland Levillain39341922015-10-20 12:48:19 +010041Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070042- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080043- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070044
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070045Look at the source to view more functions. The complete list is:
46EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070047 local T=$(gettop)
48 local A=""
49 local i
Jacky Cao89483b82015-05-15 22:12:53 +080050 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 -070051 A="$A $i"
52 done
53 echo $A
54}
55
Ying Wang08800fd2016-03-03 20:57:21 -080056# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000057function build_build_var_cache()
58{
Christopher Ferris55257d22017-03-23 11:08:58 -070059 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080060 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080061 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' ' '`)
62 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 -080063 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080064 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080065 --vars="${cached_vars[*]}" \
66 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070067 --var-prefix=var_cache_ \
68 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080069 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000070 if [ $ret -ne 0 ]
71 then
Ying Wang08800fd2016-03-03 20:57:21 -080072 unset build_dicts_script
73 return $ret
74 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070075 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080076 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080077 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080078 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000079 if [ $ret -ne 0 ]
80 then
Ying Wang08800fd2016-03-03 20:57:21 -080081 return $ret
82 fi
83 BUILD_VAR_CACHE_READY="true"
84}
85
Ying Wangf0cb3972016-03-04 13:56:23 -080086# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080087# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000088function destroy_build_var_cache()
89{
Ying Wang08800fd2016-03-03 20:57:21 -080090 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070091 local v
Ying Wang08800fd2016-03-03 20:57:21 -080092 for v in $cached_vars; do
93 unset var_cache_$v
94 done
95 unset cached_vars
96 for v in $cached_abs_vars; do
97 unset abs_var_cache_$v
98 done
99 unset cached_abs_vars
100}
101
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700102# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000103function get_abs_build_var()
104{
105 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
106 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800107 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000108 return
Ying Wang08800fd2016-03-03 20:57:21 -0800109 fi
110
Christopher Ferris55257d22017-03-23 11:08:58 -0700111 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700112 if [ ! "$T" ]; then
113 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
114 return
115 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700116 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700117}
118
119# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000120function get_build_var()
121{
122 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
123 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800124 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100125 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800126 fi
127
Christopher Ferris55257d22017-03-23 11:08:58 -0700128 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700129 if [ ! "$T" ]; then
130 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100131 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700132 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700133 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800134}
135
136# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000137function check_product()
138{
Christopher Ferris55257d22017-03-23 11:08:58 -0700139 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140 if [ ! "$T" ]; then
141 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
142 return
143 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700144 TARGET_PRODUCT=$1 \
145 TARGET_BUILD_VARIANT= \
146 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700147 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800148 get_build_var TARGET_DEVICE > /dev/null
149 # hide successful answers, but allow the errors to show
150}
151
152VARIANT_CHOICES=(user userdebug eng)
153
154# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000155function check_variant()
156{
Christopher Ferris55257d22017-03-23 11:08:58 -0700157 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800158 for v in ${VARIANT_CHOICES[@]}
159 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000160 if [ "$v" = "$1" ]
161 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800162 return 0
163 fi
164 done
165 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700166}
167
Matt Alexanderd9c56562020-05-21 10:49:17 +0000168function setpaths()
169{
Christopher Ferris55257d22017-03-23 11:08:58 -0700170 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700171 if [ ! "$T" ]; then
172 echo "Couldn't locate the top of the tree. Try setting TOP."
173 return
174 fi
175
176 ##################################################################
177 # #
178 # Read me before you modify this code #
179 # #
180 # This function sets ANDROID_BUILD_PATHS to what it is adding #
181 # to PATH, and the next time it is run, it removes that from #
182 # PATH. This is required so lunch can be run more than once #
183 # and still have working paths. #
184 # #
185 ##################################################################
186
Raphael Mollc639c782011-06-20 17:25:01 -0700187 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
188 # due to "C:\Program Files" being in the path.
189
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700190 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000191 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700192 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
193 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000194 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700195 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800196 # strip leading ':', if any
197 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500198 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700199
200 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700201 local prebuiltdir=$(getprebuilt)
202 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700203
Ben Cheng8bc4c432012-11-16 13:29:13 -0800204 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700205 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
206 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800207 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800208
Raphael Mollc639c782011-06-20 17:25:01 -0700209 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800210 export ANDROID_TOOLCHAIN=
211 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200212 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700213 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200214 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000215 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700216 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000217 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400218 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000219 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
222 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700223 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200224 *)
225 echo "Can't find toolchain for unknown architecture: $ARCH"
226 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700227 ;;
228 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700229 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800230 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700231 fi
Raphael732936d2011-06-22 14:35:32 -0700232
Christopher Ferris55257d22017-03-23 11:08:58 -0700233 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800234 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
235 fi
236
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700237 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700238
239 # add kernel specific binaries
240 case $(uname -s) in
241 Linux)
242 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
243 ;;
244 *)
245 ;;
246 esac
247
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400248 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
249 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
250 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
251 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700252 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
253
254 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
255 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
256 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200257
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800258 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
259 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
260
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200261 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
262 # to ensure that the corresponding 'emulator' binaries are used.
263 case $(uname -s) in
264 Darwin)
265 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
266 ;;
267 Linux)
268 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
269 ;;
270 *)
271 ANDROID_EMULATOR_PREBUILTS=
272 ;;
273 esac
274 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700275 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200276 export ANDROID_EMULATOR_PREBUILTS
277 fi
278
Jim Tangb3fda302018-12-22 10:24:55 +0800279 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
280 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700281 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
282 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
283 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
284 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800285
Ryan Prichard8426a192019-07-15 18:05:29 -0700286 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800287
288 # out with the duplicate old
289 if [ -n $ANDROID_PYTHONPATH ]; then
290 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
291 fi
292 # and in with the new
293 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800294 if [ -n $VENDOR_PYTHONPATH ]; then
295 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
296 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800297 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800298
Colin Crosse97e6932017-06-30 16:01:45 -0700299 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
300 export JAVA_HOME=$ANDROID_JAVA_HOME
301 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
302 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
303 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500304
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800305 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700306 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
307 export OUT=$ANDROID_PRODUCT_OUT
308
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700309 unset ANDROID_HOST_OUT
310 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
311
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900312 unset ANDROID_SOONG_HOST_OUT
313 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
314
Simran Basidd050ed2017-02-13 13:46:48 -0800315 unset ANDROID_HOST_OUT_TESTCASES
316 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
317
318 unset ANDROID_TARGET_OUT_TESTCASES
319 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
320
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800321 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322 # TODO: fix the path
323 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
324}
325
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000326function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000327{
328 local T="$(gettop)"
329 if [ ! "$T" ]; then
330 echo "Couldn't locate the top of the tree. Try setting TOP."
331 return
332 fi
333
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000334 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500335 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
336 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000337 fi
338
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000339 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000340}
341
Matt Alexanderd9c56562020-05-21 10:49:17 +0000342function printconfig()
343{
Christopher Ferris55257d22017-03-23 11:08:58 -0700344 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800345 if [ ! "$T" ]; then
346 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
347 return
348 fi
349 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700350}
351
Matt Alexanderd9c56562020-05-21 10:49:17 +0000352function set_stuff_for_environment()
353{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800354 setpaths
355 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700356
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800357 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700358 # With this environment variable new GCC can apply colors to warnings/errors
359 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 -0700360}
361
Matt Alexanderd9c56562020-05-21 10:49:17 +0000362function set_sequence_number()
363{
Colin Cross88737132017-03-21 17:41:03 -0700364 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700365}
366
Makoto Onukida971062018-06-18 10:15:19 -0700367# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
368function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800369 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700370 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800371 *:"$cmd":*)
372 return 1
373 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700374 esac
375 return 0
376}
377
Matt Alexanderd9c56562020-05-21 10:49:17 +0000378function addcompletions()
379{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700380 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700381
Jim Tanga881a252018-06-19 16:34:41 +0800382 # Keep us from trying to run in something that's neither bash nor zsh.
383 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700384 return
385 fi
386
387 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800388 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700389 return
390 fi
391
Jim Tanga881a252018-06-19 16:34:41 +0800392 local completion_files=(
393 system/core/adb/adb.bash
394 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800395 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800396 )
Makoto Onukida971062018-06-18 10:15:19 -0700397 # Completion can be disabled selectively to allow users to use non-standard completion.
398 # e.g.
399 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
400 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800401 for f in ${completion_files[*]}; do
402 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700403 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700404 fi
405 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700406
Matt Alexanderd9c56562020-05-21 10:49:17 +0000407 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700408 complete -C "bit --tab" bit
409 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000410 if [ -z "$ZSH_VERSION" ]; then
411 # Doesn't work in zsh.
412 complete -o nospace -F _croot croot
413 fi
Jim Tanga881a252018-06-19 16:34:41 +0800414 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800415
Cole Faust24c36db2021-01-23 02:39:37 +0000416 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100417 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000418 complete -F _complete_android_module_names outmod
419 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100420 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700421}
422
Matt Alexanderd9c56562020-05-21 10:49:17 +0000423function choosetype()
424{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425 echo "Build type choices are:"
426 echo " 1. release"
427 echo " 2. debug"
428 echo
429
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800430 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700431 DEFAULT_NUM=1
432 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700433
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800434 export TARGET_BUILD_TYPE=
435 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436 while [ -z $TARGET_BUILD_TYPE ]
437 do
438 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000439 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800440 read ANSWER
441 else
442 echo $1
443 ANSWER=$1
444 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445 case $ANSWER in
446 "")
447 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
448 ;;
449 1)
450 export TARGET_BUILD_TYPE=release
451 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800452 release)
453 export TARGET_BUILD_TYPE=release
454 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455 2)
456 export TARGET_BUILD_TYPE=debug
457 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800458 debug)
459 export TARGET_BUILD_TYPE=debug
460 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700461 *)
462 echo
463 echo "I didn't understand your response. Please try again."
464 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 ;;
466 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000467 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468 break
469 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700470 done
471
Ying Wang08800fd2016-03-03 20:57:21 -0800472 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700473 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800474 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475}
476
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800477#
478# This function isn't really right: It chooses a TARGET_PRODUCT
479# based on the list of boards. Usually, that gets you something
480# that kinda works with a generic product, but really, you should
481# pick a product by name.
482#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000483function chooseproduct()
484{
Christopher Ferris55257d22017-03-23 11:08:58 -0700485 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000486 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700487 default_value=$TARGET_PRODUCT
488 else
Ying Wang0a76df52015-06-08 11:57:26 -0700489 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 fi
491
Ying Wang08800fd2016-03-03 20:57:21 -0800492 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800493 export TARGET_PRODUCT=
494 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 while [ -z "$TARGET_PRODUCT" ]
496 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700497 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000498 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499 read ANSWER
500 else
501 echo $1
502 ANSWER=$1
503 fi
504
Matt Alexanderd9c56562020-05-21 10:49:17 +0000505 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800507 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000508 if check_product $ANSWER
509 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800510 export TARGET_PRODUCT=$ANSWER
511 else
512 echo "** Not a valid product: $ANSWER"
513 fi
514 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000515 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800516 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700517 fi
518 done
519
Ying Wang08800fd2016-03-03 20:57:21 -0800520 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700521 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800522 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700523}
524
Matt Alexanderd9c56562020-05-21 10:49:17 +0000525function choosevariant()
526{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800527 echo "Variant choices are:"
528 local index=1
529 local v
530 for v in ${VARIANT_CHOICES[@]}
531 do
532 # The product name is the name of the directory containing
533 # the makefile we found, above.
534 echo " $index. $v"
535 index=$(($index+1))
536 done
537
538 local default_value=eng
539 local ANSWER
540
541 export TARGET_BUILD_VARIANT=
542 while [ -z "$TARGET_BUILD_VARIANT" ]
543 do
544 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000545 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800546 read ANSWER
547 else
548 echo $1
549 ANSWER=$1
550 fi
551
Matt Alexanderd9c56562020-05-21 10:49:17 +0000552 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800553 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000554 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
555 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200556 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557 fi
558 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000559 if check_variant $ANSWER
560 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561 export TARGET_BUILD_VARIANT=$ANSWER
562 else
563 echo "** Not a valid variant: $ANSWER"
564 fi
565 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000566 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800567 break
568 fi
569 done
570}
571
Matt Alexanderd9c56562020-05-21 10:49:17 +0000572function choosecombo()
573{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700574 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700575
576 echo
577 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700578 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579
580 echo
581 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700582 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800583
584 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800585 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700586 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800587 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800588 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589}
590
Matt Alexanderd9c56562020-05-21 10:49:17 +0000591function add_lunch_combo()
592{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800593 if [ -n "$ZSH_VERSION" ]; then
594 echo -n "${funcfiletrace[1]}: "
595 else
596 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
597 fi
598 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 -0800599}
600
Matt Alexanderd9c56562020-05-21 10:49:17 +0000601function print_lunch_menu()
602{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700603 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100604 local choices
605 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
606 local ret=$?
607
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 echo
609 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700610 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100611
Matt Alexanderd9c56562020-05-21 10:49:17 +0000612 if [ $ret -ne 0 ]
613 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100614 echo "Warning: Cannot display lunch menu."
615 echo
616 echo "Note: You can invoke lunch with an explicit target:"
617 echo
618 echo " usage: lunch [target]" >&2
619 echo
620 return
621 fi
622
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700623 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624
625 local i=1
626 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700627 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800628 do
629 echo " $i. $choice"
630 i=$(($i+1))
631 done
632
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700633 echo
634}
635
Matt Alexanderd9c56562020-05-21 10:49:17 +0000636function lunch()
637{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800638 local answer
639
Steven Moreland92793dc2020-02-25 18:30:18 -0800640 if [[ $# -gt 1 ]]; then
641 echo "usage: lunch [target]" >&2
642 return 1
643 fi
644
645 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700647 else
648 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700649 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800650 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651 fi
652
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800653 local selection=
654
Matt Alexanderd9c56562020-05-21 10:49:17 +0000655 if [ -z "$answer" ]
656 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700657 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000658 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
659 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800660 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000661 if [ $answer -le ${#choices[@]} ]
662 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800663 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000664 if [ -n "$ZSH_VERSION" ]
665 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800666 selection=${choices[$(($answer))]}
667 else
668 selection=${choices[$(($answer-1))]}
669 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800670 fi
Colin Cross88737132017-03-21 17:41:03 -0700671 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800672 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700673 fi
674
Joe Onoratoda12daf2010-06-09 18:18:31 -0700675 export TARGET_BUILD_APPS=
676
Colin Cross88737132017-03-21 17:41:03 -0700677 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700678 product=${selection%%-*} # Trim everything after first dash
679 variant_and_version=${selection#*-} # Trim everything up to first dash
680 if [ "$variant_and_version" != "$selection" ]; then
681 variant=${variant_and_version%%-*}
682 if [ "$variant" != "$variant_and_version" ]; then
683 version=${variant_and_version#*-}
684 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700685 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800686
Matt Alexanderd9c56562020-05-21 10:49:17 +0000687 if [ -z "$product" ]
688 then
Ying Wang08800fd2016-03-03 20:57:21 -0800689 echo
Colin Cross88737132017-03-21 17:41:03 -0700690 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700691 return 1
692 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800693
Colin Cross88737132017-03-21 17:41:03 -0700694 TARGET_PRODUCT=$product \
695 TARGET_BUILD_VARIANT=$variant \
696 TARGET_PLATFORM_VERSION=$version \
697 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000698 if [ $? -ne 0 ]
699 then
Colin Cross88737132017-03-21 17:41:03 -0700700 return 1
701 fi
Colin Cross88737132017-03-21 17:41:03 -0700702 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
703 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700704 if [ -n "$version" ]; then
705 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
706 else
707 unset TARGET_PLATFORM_VERSION
708 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700709 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700710
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700711 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800712
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700713 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700714 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800715 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716}
717
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700718unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700719# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000720function _lunch()
721{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700722 local cur prev opts
723 COMPREPLY=()
724 cur="${COMP_WORDS[COMP_CWORD]}"
725 prev="${COMP_WORDS[COMP_CWORD-1]}"
726
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700727 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800728 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700729 fi
730
731 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700732 return 0
733}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700734
Joe Onoratoda12daf2010-06-09 18:18:31 -0700735# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700736# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000737function tapas()
738{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700739 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800740 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700741 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700742 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 -0800743 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 -0700744
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700745 if [ "$showHelp" != "" ]; then
746 $(gettop)/build/make/tapasHelp.sh
747 return
748 fi
749
Ying Wang67f02922012-08-22 10:25:20 -0700750 if [ $(echo $arch | wc -w) -gt 1 ]; then
751 echo "tapas: Error: Multiple build archs supplied: $arch"
752 return
753 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700754 if [ $(echo $variant | wc -w) -gt 1 ]; then
755 echo "tapas: Error: Multiple build variants supplied: $variant"
756 return
757 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700758 if [ $(echo $density | wc -w) -gt 1 ]; then
759 echo "tapas: Error: Multiple densities supplied: $density"
760 return
761 fi
Ying Wang67f02922012-08-22 10:25:20 -0700762
Ying Wang0a76df52015-06-08 11:57:26 -0700763 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700764 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000765 x86) product=aosp_x86;;
766 arm64) product=aosp_arm64;;
767 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700768 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700769 if [ -z "$variant" ]; then
770 variant=eng
771 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700772 if [ -z "$apps" ]; then
773 apps=all
774 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600775 if [ -z "$density" ]; then
776 density=alldpi
777 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700778
Ying Wang67f02922012-08-22 10:25:20 -0700779 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700780 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700781 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700782 export TARGET_BUILD_TYPE=release
783 export TARGET_BUILD_APPS=$apps
784
Ying Wang08800fd2016-03-03 20:57:21 -0800785 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700786 set_stuff_for_environment
787 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800788 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700789}
790
Matt Alexanderd9c56562020-05-21 10:49:17 +0000791function gettop
792{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700793 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000794 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700795 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000796 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700797 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000798 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800799 # The following circumlocution (repeated below as well) ensures
800 # that we record the true directory name and not one that is
801 # faked up with symlink names.
802 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700803 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800804 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700805 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000806 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800807 \cd ..
synergyb112a402013-07-05 19:47:47 -0700808 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700809 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000810 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700811 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000812 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700813 fi
814 fi
815 fi
816}
817
Matt Alexanderd9c56562020-05-21 10:49:17 +0000818function croot()
819{
Christopher Ferris55257d22017-03-23 11:08:58 -0700820 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700821 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700822 if [ "$1" ]; then
823 \cd $(gettop)/$1
824 else
825 \cd $(gettop)
826 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700827 else
828 echo "Couldn't locate the top of the tree. Try setting TOP."
829 fi
830}
831
Matt Alexanderd9c56562020-05-21 10:49:17 +0000832function _croot()
833{
Anton Hanssonece9c482019-02-04 18:15:39 +0000834 local T=$(gettop)
835 if [ "$T" ]; then
836 local cur="${COMP_WORDS[COMP_CWORD]}"
837 k=0
838 for c in $(compgen -d ${T}/${cur}); do
839 COMPREPLY[k++]=${c#${T}/}/
840 done
841 fi
842}
843
Matt Alexanderd9c56562020-05-21 10:49:17 +0000844function cproj()
845{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700846 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700847 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700848 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700849 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
850 T=$PWD
851 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800852 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700853 return
854 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800855 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700856 done
Ying Wang9cd17642012-12-13 10:52:07 -0800857 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700858 echo "can't find Android.mk"
859}
860
Daniel Sandler47e0a882013-07-30 13:23:52 -0400861# simplified version of ps; output in the form
862# <pid> <procname>
863function qpid() {
864 local prepend=''
865 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000866 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400867 prepend=' '
868 append='$'
869 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000870 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800871 echo "usage: qpid [[--exact] <process name|pid>"
872 return 255
873 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400874
875 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000876 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800877 qpid | \grep "$prepend$EXE$append"
878 else
879 adb shell ps \
880 | tr -d '\r' \
881 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
882 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400883}
884
Steven Moreland74114f12020-09-10 01:23:32 +0000885# syswrite - disable verity, reboot if needed, and remount image
886#
887# Easy way to make system.img/etc writable
888function syswrite() {
889 adb wait-for-device && adb root || return 1
890 if [[ $(adb disable-verity | grep "reboot") ]]; then
891 echo "rebooting"
892 adb reboot && adb wait-for-device && adb root || return 1
893 fi
894 adb wait-for-device && adb remount || return 1
895}
896
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800897# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700898# that has the core-file-size limit set correctly
899#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800900# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700901# if its core-file-size limit is not set already.
902# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
903
Matt Alexanderd9c56562020-05-21 10:49:17 +0000904function coredump_setup()
905{
906 echo "Getting root...";
907 adb root;
908 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700909
Matt Alexanderd9c56562020-05-21 10:49:17 +0000910 echo "Remounting root partition read-write...";
911 adb shell mount -w -o remount -t rootfs rootfs;
912 sleep 1;
913 adb wait-for-device;
914 adb shell mkdir -p /cores;
915 adb shell mount -t tmpfs tmpfs /cores;
916 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700917
Matt Alexanderd9c56562020-05-21 10:49:17 +0000918 echo "Granting SELinux permission to dump in /cores...";
919 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700920
Matt Alexanderd9c56562020-05-21 10:49:17 +0000921 echo "Set core pattern.";
922 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923
Ying Wang08800fd2016-03-03 20:57:21 -0800924 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700925}
926
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800927# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000928# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700929#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800930# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700931# dump to actually be generated.
932
Matt Alexanderd9c56562020-05-21 10:49:17 +0000933function coredump_enable()
934{
935 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800936 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000937 printf "Expecting a PID!\n";
938 return;
939 fi;
940 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800941 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700942}
943
944# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000945# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700946#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800947# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700948# enabled globally.
949
Matt Alexanderd9c56562020-05-21 10:49:17 +0000950function core()
951{
952 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700953
Ying Wang08800fd2016-03-03 20:57:21 -0800954 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000955 printf "Expecting a PID!\n";
956 return;
957 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700958
Matt Alexanderd9c56562020-05-21 10:49:17 +0000959 local CORENAME=core.$PID;
960 local COREPATH=/cores/$CORENAME;
961 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700962
Matt Alexanderd9c56562020-05-21 10:49:17 +0000963 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700964
Matt Alexanderd9c56562020-05-21 10:49:17 +0000965 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800966 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000967 printf "\tSending SIG%s to %d...\n" $SIG $PID;
968 adb shell kill -$SIG $PID;
969 sleep 1;
970 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700971
Matt Alexanderd9c56562020-05-21 10:49:17 +0000972 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
973 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700974}
975
Christopher Tate744ee802009-11-12 15:33:08 -0800976# systemstack - dump the current stack trace of all threads in the system process
977# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000978function systemstack()
979{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800980 stacks system_server
981}
982
Michael Wrightaeed7212014-06-19 19:58:12 -0700983# Read the ELF header from /proc/$PID/exe to determine if the process is
984# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000985function is64bit()
986{
Ben Chengfba67bf2014-02-25 10:27:07 -0800987 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000988 if [ "$PID" ] ; then
989 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800990 echo "64"
991 else
992 echo ""
993 fi
994 else
995 echo ""
996 fi
997}
998
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700999case `uname -s` in
1000 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001001 function sgrep()
1002 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001003 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 -04001004 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001005 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001006
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001007 ;;
1008 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001009 function sgrep()
1010 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001011 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 -04001012 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001013 }
1014 ;;
1015esac
1016
Matt Alexanderd9c56562020-05-21 10:49:17 +00001017function gettargetarch
1018{
Raghu Gandham8da43102012-07-25 19:57:22 -07001019 get_build_var TARGET_ARCH
1020}
1021
Matt Alexanderd9c56562020-05-21 10:49:17 +00001022function ggrep()
1023{
Mike Frysinger5e479732015-09-22 18:13:48 -04001024 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1025 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001026}
1027
Matt Alexanderd9c56562020-05-21 10:49:17 +00001028function gogrep()
1029{
Orion Hodson831472d2019-10-25 11:35:15 +01001030 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1031 -exec grep --color -n "$@" {} +
1032}
1033
Matt Alexanderd9c56562020-05-21 10:49:17 +00001034function jgrep()
1035{
Mike Frysinger5e479732015-09-22 18:13:48 -04001036 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1037 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001038}
1039
Matt Alexanderd9c56562020-05-21 10:49:17 +00001040function cgrep()
1041{
Mike Frysinger5e479732015-09-22 18:13:48 -04001042 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' \) \
1043 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001044}
1045
Matt Alexanderd9c56562020-05-21 10:49:17 +00001046function resgrep()
1047{
Christopher Ferris55257d22017-03-23 11:08:58 -07001048 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001049 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1050 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1051 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001052}
1053
Matt Alexanderd9c56562020-05-21 10:49:17 +00001054function mangrep()
1055{
Mike Frysinger5e479732015-09-22 18:13:48 -04001056 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1057 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001058}
1059
Matt Alexanderd9c56562020-05-21 10:49:17 +00001060function owngrep()
1061{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001062 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1063 -exec grep --color -n "$@" {} +
1064}
1065
Matt Alexanderd9c56562020-05-21 10:49:17 +00001066function sepgrep()
1067{
Mike Frysinger5e479732015-09-22 18:13:48 -04001068 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1069 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001070}
1071
Matt Alexanderd9c56562020-05-21 10:49:17 +00001072function rcgrep()
1073{
Mike Frysinger5e479732015-09-22 18:13:48 -04001074 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1075 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001076}
1077
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001078case `uname -s` in
1079 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001080 function mgrep()
1081 {
Orion Hodson831472d2019-10-25 11:35:15 +01001082 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 -04001083 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001084 }
1085
Matt Alexanderd9c56562020-05-21 10:49:17 +00001086 function treegrep()
1087 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001088 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 -04001089 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001090 }
1091
1092 ;;
1093 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001094 function mgrep()
1095 {
Orion Hodson831472d2019-10-25 11:35:15 +01001096 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 -04001097 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001098 }
1099
Matt Alexanderd9c56562020-05-21 10:49:17 +00001100 function treegrep()
1101 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001102 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 -04001103 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001104 }
1105
1106 ;;
1107esac
1108
Matt Alexanderd9c56562020-05-21 10:49:17 +00001109function getprebuilt
1110{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001111 get_abs_build_var ANDROID_PREBUILTS
1112}
1113
Matt Alexanderd9c56562020-05-21 10:49:17 +00001114function tracedmdump()
1115{
Christopher Ferris55257d22017-03-23 11:08:58 -07001116 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001117 if [ ! "$T" ]; then
1118 echo "Couldn't locate the top of the tree. Try setting TOP."
1119 return
1120 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001121 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001122 local arch=$(gettargetarch)
1123 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001124
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001125 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001126 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001127 echo "usage: tracedmdump tracename"
1128 return
1129 fi
1130
Matt Alexanderd9c56562020-05-21 10:49:17 +00001131 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001132 echo "Error: cannot find kernel: '$KERNEL'"
1133 return
1134 fi
1135
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001136 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001137 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001138 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1139 fi
1140
1141 echo "post-processing traces..."
1142 rm -f $TRACE/qtrace.dexlist
1143 post_trace $TRACE
1144 if [ $? -ne 0 ]; then
1145 echo "***"
1146 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1147 echo "***"
1148 return
1149 fi
1150 echo "generating dexlist output..."
1151 /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
1152 echo "generating dmtrace data..."
1153 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1154 echo "generating html file..."
1155 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1156 echo "done, see $TRACE/dmtrace.html for details"
1157 echo "or run:"
1158 echo " traceview $TRACE/dmtrace"
1159}
1160
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001161# communicate with a running device or emulator, set up necessary state,
1162# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001163function runhat()
1164{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001165 # process standard adb options
1166 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001168 adbTarget=$1
1169 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001170 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001171 adbTarget="$1 $2"
1172 shift 2
1173 fi
1174 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001175 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001176
1177 # runhat options
1178 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001179
Matt Alexanderd9c56562020-05-21 10:49:17 +00001180 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001181 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001182 return
1183 fi
1184
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001185 # confirm hat is available
1186 if [ -z $(which hat) ]; then
1187 echo "hat is not available in this configuration."
1188 return
1189 fi
1190
Andy McFaddenb6289852010-07-12 08:00:19 -07001191 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001192 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001194 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001195 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001196 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197 echo -n "> "
1198 read
1199
The Android Open Source Project88b60792009-03-03 19:28:42 -08001200 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001201
The Android Open Source Project88b60792009-03-03 19:28:42 -08001202 echo "Retrieving file $devFile..."
1203 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001204
The Android Open Source Project88b60792009-03-03 19:28:42 -08001205 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206
The Android Open Source Project88b60792009-03-03 19:28:42 -08001207 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208 echo "View the output by pointing your browser at http://localhost:7000/"
1209 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001210 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001211}
1212
Matt Alexanderd9c56562020-05-21 10:49:17 +00001213function getbugreports()
1214{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001215 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216
1217 if [ ! "$reports" ]; then
1218 echo "Could not locate any bugreports."
1219 return
1220 fi
1221
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001222 local report
1223 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001224 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001225 echo "/sdcard/bugreports/${report}"
1226 adb pull /sdcard/bugreports/${report} ${report}
1227 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001228 done
1229}
1230
Matt Alexanderd9c56562020-05-21 10:49:17 +00001231function getsdcardpath()
1232{
Victoria Lease1b296b42012-08-21 15:44:06 -07001233 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1234}
1235
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236function getscreenshotpath()
1237{
Victoria Lease1b296b42012-08-21 15:44:06 -07001238 echo "$(getsdcardpath)/Pictures/Screenshots"
1239}
1240
Matt Alexanderd9c56562020-05-21 10:49:17 +00001241function getlastscreenshot()
1242{
Victoria Lease1b296b42012-08-21 15:44:06 -07001243 local screenshot_path=$(getscreenshotpath)
1244 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 +00001245 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001246 echo "No screenshots found."
1247 return
1248 fi
1249 echo "${screenshot}"
1250 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1251}
1252
Matt Alexanderd9c56562020-05-21 10:49:17 +00001253function startviewserver()
1254{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001255 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001256 if [ $# -gt 0 ]; then
1257 port=$1
1258 fi
1259 adb shell service call window 1 i32 $port
1260}
1261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262function stopviewserver()
1263{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264 adb shell service call window 2
1265}
1266
Matt Alexanderd9c56562020-05-21 10:49:17 +00001267function isviewserverstarted()
1268{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001269 adb shell service call window 3
1270}
1271
Matt Alexanderd9c56562020-05-21 10:49:17 +00001272function key_home()
1273{
Romain Guyb84049a2010-10-04 16:56:11 -07001274 adb shell input keyevent 3
1275}
1276
Matt Alexanderd9c56562020-05-21 10:49:17 +00001277function key_back()
1278{
Romain Guyb84049a2010-10-04 16:56:11 -07001279 adb shell input keyevent 4
1280}
1281
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282function key_menu()
1283{
Romain Guyb84049a2010-10-04 16:56:11 -07001284 adb shell input keyevent 82
1285}
1286
Matt Alexanderd9c56562020-05-21 10:49:17 +00001287function smoketest()
1288{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001289 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1290 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1291 return
1292 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001293 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001294 if [ ! "$T" ]; then
1295 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1296 return
1297 fi
1298
Ying Wang9cd17642012-12-13 10:52:07 -08001299 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001300 adb uninstall com.android.smoketest > /dev/null &&
1301 adb uninstall com.android.smoketest.tests > /dev/null &&
1302 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1303 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1304 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1305}
1306
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001307# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001308function runtest()
1309{
Christopher Ferris55257d22017-03-23 11:08:58 -07001310 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001311 if [ ! "$T" ]; then
1312 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1313 return
1314 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001315 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001316}
1317
The Android Open Source Project88b60792009-03-03 19:28:42 -08001318function godir () {
1319 if [[ -z "$1" ]]; then
1320 echo "Usage: godir <regex>"
1321 return
1322 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001323 local T=$(gettop)
1324 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001326 mkdir -p $OUT_DIR
1327 FILELIST=$OUT_DIR/filelist
1328 else
1329 FILELIST=$T/filelist
1330 fi
1331 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001332 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001333 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001334 echo " Done"
1335 echo ""
1336 fi
1337 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001338 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001339 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001340 echo "Not found"
1341 return
1342 fi
1343 local pathname
1344 local choice
1345 if [[ ${#lines[@]} > 1 ]]; then
1346 while [[ -z "$pathname" ]]; do
1347 local index=1
1348 local line
1349 for line in ${lines[@]}; do
1350 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001351 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001352 done
1353 echo
1354 echo -n "Select one: "
1355 unset choice
1356 read choice
1357 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1358 echo "Invalid choice"
1359 continue
1360 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001361 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001362 done
1363 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001364 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001365 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001366 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001367}
1368
Steven Moreland62054a42018-12-06 10:11:40 -08001369# Update module-info.json in out.
1370function refreshmod() {
1371 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1372 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1373 return 1
1374 fi
1375
1376 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1377
1378 # for the output of the next command
1379 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1380
1381 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001382 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001383 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1384}
1385
Cole Faust24c36db2021-01-23 02:39:37 +00001386# Verifies that module-info.txt exists, creating it if it doesn't.
1387function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001388 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1389 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1390 return 1
1391 fi
1392
1393 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1394 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1395 refreshmod || return 1
1396 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001397}
1398
1399# List all modules for the current device, as cached in module-info.json. If any build change is
1400# made and it should be reflected in the output, you should run 'refreshmod' first.
1401function allmod() {
1402 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001403
LuK1337b6a78192020-01-12 03:12:17 +01001404 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 -08001405}
1406
Rett Berg78d1c932019-01-24 14:34:23 -08001407# 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 -08001408# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001409function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001410 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001411 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001412 return 1
1413 fi
1414
Cole Faust24c36db2021-01-23 02:39:37 +00001415 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001416
1417 local relpath=$(python -c "import json, os
1418module = '$1'
1419module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1420if module not in module_info:
1421 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001422print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001423
1424 if [ -z "$relpath" ]; then
1425 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1426 return 1
1427 else
Rett Berg78d1c932019-01-24 14:34:23 -08001428 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001429 fi
1430}
1431
Rett Berg78d1c932019-01-24 14:34:23 -08001432# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1433# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1434function gomod() {
1435 if [[ $# -ne 1 ]]; then
1436 echo "usage: gomod <module>" >&2
1437 return 1
1438 fi
1439
1440 local path="$(pathmod $@)"
1441 if [ -z "$path" ]; then
1442 return 1
1443 fi
1444 cd $path
1445}
1446
Cole Faust24c36db2021-01-23 02:39:37 +00001447# Gets the list of a module's installed outputs, as cached in module-info.json.
1448# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1449function outmod() {
1450 if [[ $# -ne 1 ]]; then
1451 echo "usage: outmod <module>" >&2
1452 return 1
1453 fi
1454
1455 verifymodinfo || return 1
1456
1457 local relpath
1458 relpath=$(python -c "import json, os
1459module = '$1'
1460module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1461if module not in module_info:
1462 exit(1)
1463for output in module_info[module]['installed']:
1464 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1465
1466 if [ $? -ne 0 ]; then
1467 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1468 return 1
1469 elif [ ! -z "$relpath" ]; then
1470 echo "$relpath"
1471 fi
1472}
1473
1474# adb install a module's apk, as cached in module-info.json. If any build change
1475# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1476# Usage: installmod [adb install arguments] <module>
1477# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1478function installmod() {
1479 if [[ $# -eq 0 ]]; then
1480 echo "usage: installmod [adb install arguments] <module>" >&2
1481 return 1
1482 fi
1483
1484 local _path
1485 _path=$(outmod ${@:$#:1})
1486 if [ $? -ne 0 ]; then
1487 return 1
1488 fi
1489
1490 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1491 if [ -z "$_path" ]; then
1492 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1493 return 1
1494 fi
1495 local length=$(( $# - 1 ))
1496 echo adb install ${@:1:$length} $_path
1497 adb install ${@:1:$length} $_path
1498}
1499
dimitry73b84812018-12-11 18:06:00 +01001500function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001501 local word=${COMP_WORDS[COMP_CWORD]}
1502 COMPREPLY=( $(allmod | grep -E "^$word") )
1503}
1504
Alex Rayf0d08eb2013-03-08 15:15:06 -08001505# Print colored exit condition
1506function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001507 "$@"
1508 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001509 if [ $retval -ne 0 ]
1510 then
Jacky Cao89483b82015-05-15 22:12:53 +08001511 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001512 else
Jacky Cao89483b82015-05-15 22:12:53 +08001513 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001514 fi
1515 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001516}
1517
Matt Alexanderd9c56562020-05-21 10:49:17 +00001518function get_make_command()
1519{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001520 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1521 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001522 # Always use the real make if -C is passed in
1523 for arg in "$@"; do
1524 if [[ $arg == -C* ]]; then
1525 echo command make
1526 return
1527 fi
1528 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001529 echo build/soong/soong_ui.bash --make-mode
1530 else
1531 echo command make
1532 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001533}
1534
Matt Alexanderd9c56562020-05-21 10:49:17 +00001535function _wrap_build()
1536{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001537 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1538 "$@"
1539 return $?
1540 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001541 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001542 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001543 local ret=$?
1544 local end_time=$(date +"%s")
1545 local tdiff=$(($end_time-$start_time))
1546 local hours=$(($tdiff / 3600 ))
1547 local mins=$((($tdiff % 3600) / 60))
1548 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001549 local ncolors=$(tput colors 2>/dev/null)
1550 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001551 color_failed=$'\E'"[0;31m"
1552 color_success=$'\E'"[0;32m"
1553 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001554 else
1555 color_failed=""
1556 color_success=""
1557 color_reset=""
1558 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001559 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001560 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001561 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001562 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001563 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001564 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001565 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001566 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001567 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001568 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001569 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001570 printf "(%s seconds)" $secs
1571 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001572 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001573 echo
1574 return $ret
1575}
1576
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001577function _trigger_build()
1578(
1579 local -r bc="$1"; shift
1580 if T="$(gettop)"; then
1581 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1582 else
1583 echo "Couldn't locate the top of the tree. Try setting TOP."
1584 fi
1585)
1586
1587function m()
1588(
1589 _trigger_build "all-modules" "$@"
1590)
1591
1592function mm()
1593(
1594 _trigger_build "modules-in-a-dir-no-deps" "$@"
1595)
1596
1597function mmm()
1598(
1599 _trigger_build "modules-in-dirs-no-deps" "$@"
1600)
1601
1602function mma()
1603(
1604 _trigger_build "modules-in-a-dir" "$@"
1605)
1606
1607function mmma()
1608(
1609 _trigger_build "modules-in-dirs" "$@"
1610)
1611
Matt Alexanderd9c56562020-05-21 10:49:17 +00001612function make()
1613{
Dan Willemsene9842242017-07-28 13:00:13 -07001614 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001615}
1616
Matt Alexanderd9c56562020-05-21 10:49:17 +00001617function provision()
1618{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001619 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1620 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1621 return 1
1622 fi
1623 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1624 echo "There is no provisioning script for the device." >&2
1625 return 1
1626 fi
1627
1628 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001629 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001630 shift 1
1631 else
1632 echo "This action will reflash your device."
1633 echo ""
1634 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1635 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001636 echo -n "Are you sure you want to do this (yes/no)? "
1637 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001638 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001639 echo "Not taking any action. Exiting." >&2
1640 return 1
1641 fi
1642 fi
1643 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1644}
1645
Jim Tanga881a252018-06-19 16:34:41 +08001646# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001647function enable_zsh_completion() {
1648 # Don't override user's options if bash-style completion is already enabled.
1649 if ! declare -f complete >/dev/null; then
1650 autoload -U compinit && compinit
1651 autoload -U bashcompinit && bashcompinit
1652 fi
Jim Tanga881a252018-06-19 16:34:41 +08001653}
1654
1655function validate_current_shell() {
1656 local current_sh="$(ps -o command -p $$)"
1657 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001658 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001659 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001660 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001661 *zsh*)
1662 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001663 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001664 *)
Jim Tanga881a252018-06-19 16:34:41 +08001665 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 -07001666 ;;
1667 esac
Jim Tanga881a252018-06-19 16:34:41 +08001668}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001669
1670# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001671# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1672# load those.
1673#
1674# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001675function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001676 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001677 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001678 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001679 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 -08001680 if [ -n "$allowed" ]; then
1681 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1682 echo " $allowed"
1683 echo " $f"
1684 return
1685 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001686 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001687 done
1688
1689 allowed_files=
1690 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001691 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001692 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001693 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001694
1695 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001696 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001697 else
1698 echo "ignoring $f, not in $allowed"
1699 fi
Jim Tanga881a252018-06-19 16:34:41 +08001700 done
1701 done
1702}
Kenny Root52aa81c2011-07-15 11:07:06 -07001703
Dan Albertbab814f2020-08-26 15:34:53 -07001704function showcommands() {
1705 local T=$(gettop)
1706 if [[ -z "$TARGET_PRODUCT" ]]; then
1707 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1708 return
1709 fi
1710 case $(uname -s) in
1711 Darwin)
1712 PREBUILT_NAME=darwin-x86
1713 ;;
1714 Linux)
1715 PREBUILT_NAME=linux-x86
1716 ;;
1717 *)
1718 >&2 echo Unknown host $(uname -s)
1719 return
1720 ;;
1721 esac
1722 if [[ -z "$OUT_DIR" ]]; then
1723 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1724 OUT_DIR=out
1725 else
1726 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1727 fi
1728 fi
1729 if [[ "$1" == "--regenerate" ]]; then
1730 shift 1
1731 NINJA_ARGS="-t commands $@" m
1732 else
1733 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1734 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1735 -t commands "$@")
1736 fi
1737}
1738
Jim Tanga881a252018-06-19 16:34:41 +08001739validate_current_shell
1740source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001741addcompletions