blob: a5f6b6df3644d683556a4133c6499419868bd104 [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.
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +010030- rgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080031- sepgrep: Greps on all local sepolicy files.
32- sgrep: Greps on all local source files.
33- godir: Go to the directory containing a file.
34- allmod: List all modules.
35- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080036- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000037- outmod: Gets the location of a module's installed outputs with a certain extension.
38- installmod: Adb installs a module's built APK.
39- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000040- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070041
Roland Levillain39341922015-10-20 12:48:19 +010042Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070043- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080044- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070045
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046Look at the source to view more functions. The complete list is:
47EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070048 local T=$(gettop)
49 local A=""
50 local i
Jacky Cao89483b82015-05-15 22:12:53 +080051 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 -070052 A="$A $i"
53 done
54 echo $A
55}
56
Ying Wang08800fd2016-03-03 20:57:21 -080057# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000058function build_build_var_cache()
59{
Christopher Ferris55257d22017-03-23 11:08:58 -070060 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080061 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080062 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' ' '`)
63 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 -080064 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080065 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080066 --vars="${cached_vars[*]}" \
67 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070068 --var-prefix=var_cache_ \
69 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080070 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000071 if [ $ret -ne 0 ]
72 then
Ying Wang08800fd2016-03-03 20:57:21 -080073 unset build_dicts_script
74 return $ret
75 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070076 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080077 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080078 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080079 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000080 if [ $ret -ne 0 ]
81 then
Ying Wang08800fd2016-03-03 20:57:21 -080082 return $ret
83 fi
84 BUILD_VAR_CACHE_READY="true"
85}
86
Ying Wangf0cb3972016-03-04 13:56:23 -080087# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080088# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000089function destroy_build_var_cache()
90{
Ying Wang08800fd2016-03-03 20:57:21 -080091 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070092 local v
Ying Wang08800fd2016-03-03 20:57:21 -080093 for v in $cached_vars; do
94 unset var_cache_$v
95 done
96 unset cached_vars
97 for v in $cached_abs_vars; do
98 unset abs_var_cache_$v
99 done
100 unset cached_abs_vars
101}
102
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700103# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000104function get_abs_build_var()
105{
106 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
107 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800108 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000109 return
Ying Wang08800fd2016-03-03 20:57:21 -0800110 fi
111
Christopher Ferris55257d22017-03-23 11:08:58 -0700112 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700113 if [ ! "$T" ]; then
114 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
115 return
116 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700117 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700118}
119
120# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000121function get_build_var()
122{
123 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
124 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800125 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100126 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800127 fi
128
Christopher Ferris55257d22017-03-23 11:08:58 -0700129 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700130 if [ ! "$T" ]; then
131 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100132 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700133 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700134 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800135}
136
137# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000138function check_product()
139{
Christopher Ferris55257d22017-03-23 11:08:58 -0700140 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800141 if [ ! "$T" ]; then
142 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
143 return
144 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700145 TARGET_PRODUCT=$1 \
146 TARGET_BUILD_VARIANT= \
147 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700148 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800149 get_build_var TARGET_DEVICE > /dev/null
150 # hide successful answers, but allow the errors to show
151}
152
153VARIANT_CHOICES=(user userdebug eng)
154
155# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000156function check_variant()
157{
Christopher Ferris55257d22017-03-23 11:08:58 -0700158 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800159 for v in ${VARIANT_CHOICES[@]}
160 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000161 if [ "$v" = "$1" ]
162 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800163 return 0
164 fi
165 done
166 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700167}
168
Matt Alexanderd9c56562020-05-21 10:49:17 +0000169function setpaths()
170{
Christopher Ferris55257d22017-03-23 11:08:58 -0700171 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700172 if [ ! "$T" ]; then
173 echo "Couldn't locate the top of the tree. Try setting TOP."
174 return
175 fi
176
177 ##################################################################
178 # #
179 # Read me before you modify this code #
180 # #
181 # This function sets ANDROID_BUILD_PATHS to what it is adding #
182 # to PATH, and the next time it is run, it removes that from #
183 # PATH. This is required so lunch can be run more than once #
184 # and still have working paths. #
185 # #
186 ##################################################################
187
Raphael Mollc639c782011-06-20 17:25:01 -0700188 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
189 # due to "C:\Program Files" being in the path.
190
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700191 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000192 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700193 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
194 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000195 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700196 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800197 # strip leading ':', if any
198 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500199 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700200
201 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700202 local prebuiltdir=$(getprebuilt)
203 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700204
Ben Cheng8bc4c432012-11-16 13:29:13 -0800205 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700206 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
207 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800208 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800209
Raphael Mollc639c782011-06-20 17:25:01 -0700210 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800211 export ANDROID_TOOLCHAIN=
212 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200213 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700214 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200215 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000216 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700217 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000218 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400219 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000220 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200221 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000222 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
223 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700224 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200225 *)
226 echo "Can't find toolchain for unknown architecture: $ARCH"
227 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700228 ;;
229 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700230 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800231 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700232 fi
Raphael732936d2011-06-22 14:35:32 -0700233
Christopher Ferris55257d22017-03-23 11:08:58 -0700234 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800235 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
236 fi
237
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700238 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700239
240 # add kernel specific binaries
241 case $(uname -s) in
242 Linux)
243 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
244 ;;
245 *)
246 ;;
247 esac
248
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400249 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
250 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
251 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
252 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
254
255 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
256 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
257 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200258
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800259 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
260 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
261
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200262 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
263 # to ensure that the corresponding 'emulator' binaries are used.
264 case $(uname -s) in
265 Darwin)
266 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
267 ;;
268 Linux)
269 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
270 ;;
271 *)
272 ANDROID_EMULATOR_PREBUILTS=
273 ;;
274 esac
275 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700276 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200277 export ANDROID_EMULATOR_PREBUILTS
278 fi
279
Jim Tangb3fda302018-12-22 10:24:55 +0800280 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
281 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700282 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
283 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
284 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
285 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800286
Ryan Prichard8426a192019-07-15 18:05:29 -0700287 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800288
289 # out with the duplicate old
290 if [ -n $ANDROID_PYTHONPATH ]; then
291 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
292 fi
293 # and in with the new
294 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800295 if [ -n $VENDOR_PYTHONPATH ]; then
296 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
297 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800298 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800299
Colin Crosse97e6932017-06-30 16:01:45 -0700300 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
301 export JAVA_HOME=$ANDROID_JAVA_HOME
302 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
303 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
304 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500305
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800306 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700307 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
308 export OUT=$ANDROID_PRODUCT_OUT
309
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700310 unset ANDROID_HOST_OUT
311 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
312
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900313 unset ANDROID_SOONG_HOST_OUT
314 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
315
Simran Basidd050ed2017-02-13 13:46:48 -0800316 unset ANDROID_HOST_OUT_TESTCASES
317 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
318
319 unset ANDROID_TARGET_OUT_TESTCASES
320 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800322 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700323 # TODO: fix the path
324 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
325}
326
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000327function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000328{
329 local T="$(gettop)"
330 if [ ! "$T" ]; then
331 echo "Couldn't locate the top of the tree. Try setting TOP."
332 return
333 fi
334
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000335 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500336 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
337 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000338 fi
339
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000340 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000341}
342
Matt Alexanderd9c56562020-05-21 10:49:17 +0000343function printconfig()
344{
Christopher Ferris55257d22017-03-23 11:08:58 -0700345 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800346 if [ ! "$T" ]; then
347 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
348 return
349 fi
350 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700351}
352
Matt Alexanderd9c56562020-05-21 10:49:17 +0000353function set_stuff_for_environment()
354{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800355 setpaths
356 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700357
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800358 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700359 # With this environment variable new GCC can apply colors to warnings/errors
360 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 -0700361}
362
Matt Alexanderd9c56562020-05-21 10:49:17 +0000363function set_sequence_number()
364{
Colin Cross88737132017-03-21 17:41:03 -0700365 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700366}
367
Makoto Onukida971062018-06-18 10:15:19 -0700368# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
369function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800370 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700371 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800372 *:"$cmd":*)
373 return 1
374 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700375 esac
376 return 0
377}
378
Matt Alexanderd9c56562020-05-21 10:49:17 +0000379function addcompletions()
380{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700381 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700382
Jim Tanga881a252018-06-19 16:34:41 +0800383 # Keep us from trying to run in something that's neither bash nor zsh.
384 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700385 return
386 fi
387
388 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800389 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700390 return
391 fi
392
Jim Tanga881a252018-06-19 16:34:41 +0800393 local completion_files=(
394 system/core/adb/adb.bash
395 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800396 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800397 )
Makoto Onukida971062018-06-18 10:15:19 -0700398 # Completion can be disabled selectively to allow users to use non-standard completion.
399 # e.g.
400 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
401 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800402 for f in ${completion_files[*]}; do
403 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700404 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700405 fi
406 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700407
Matt Alexanderd9c56562020-05-21 10:49:17 +0000408 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700409 complete -C "bit --tab" bit
410 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000411 if [ -z "$ZSH_VERSION" ]; then
412 # Doesn't work in zsh.
413 complete -o nospace -F _croot croot
414 fi
Jim Tanga881a252018-06-19 16:34:41 +0800415 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800416
Cole Faust24c36db2021-01-23 02:39:37 +0000417 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100418 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000419 complete -F _complete_android_module_names outmod
420 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100421 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700422}
423
Matt Alexanderd9c56562020-05-21 10:49:17 +0000424function choosetype()
425{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700426 echo "Build type choices are:"
427 echo " 1. release"
428 echo " 2. debug"
429 echo
430
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800431 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700432 DEFAULT_NUM=1
433 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700434
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800435 export TARGET_BUILD_TYPE=
436 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700437 while [ -z $TARGET_BUILD_TYPE ]
438 do
439 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000440 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800441 read ANSWER
442 else
443 echo $1
444 ANSWER=$1
445 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700446 case $ANSWER in
447 "")
448 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
449 ;;
450 1)
451 export TARGET_BUILD_TYPE=release
452 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800453 release)
454 export TARGET_BUILD_TYPE=release
455 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456 2)
457 export TARGET_BUILD_TYPE=debug
458 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800459 debug)
460 export TARGET_BUILD_TYPE=debug
461 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462 *)
463 echo
464 echo "I didn't understand your response. Please try again."
465 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466 ;;
467 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000468 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469 break
470 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700471 done
472
Ying Wang08800fd2016-03-03 20:57:21 -0800473 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700474 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800475 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700476}
477
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800478#
479# This function isn't really right: It chooses a TARGET_PRODUCT
480# based on the list of boards. Usually, that gets you something
481# that kinda works with a generic product, but really, you should
482# pick a product by name.
483#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000484function chooseproduct()
485{
Christopher Ferris55257d22017-03-23 11:08:58 -0700486 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000487 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700488 default_value=$TARGET_PRODUCT
489 else
Ying Wang0a76df52015-06-08 11:57:26 -0700490 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700491 fi
492
Ying Wang08800fd2016-03-03 20:57:21 -0800493 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800494 export TARGET_PRODUCT=
495 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700496 while [ -z "$TARGET_PRODUCT" ]
497 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700498 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000499 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800500 read ANSWER
501 else
502 echo $1
503 ANSWER=$1
504 fi
505
Matt Alexanderd9c56562020-05-21 10:49:17 +0000506 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700507 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800508 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000509 if check_product $ANSWER
510 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800511 export TARGET_PRODUCT=$ANSWER
512 else
513 echo "** Not a valid product: $ANSWER"
514 fi
515 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000516 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800517 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700518 fi
519 done
520
Ying Wang08800fd2016-03-03 20:57:21 -0800521 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700522 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800523 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700524}
525
Matt Alexanderd9c56562020-05-21 10:49:17 +0000526function choosevariant()
527{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528 echo "Variant choices are:"
529 local index=1
530 local v
531 for v in ${VARIANT_CHOICES[@]}
532 do
533 # The product name is the name of the directory containing
534 # the makefile we found, above.
535 echo " $index. $v"
536 index=$(($index+1))
537 done
538
539 local default_value=eng
540 local ANSWER
541
542 export TARGET_BUILD_VARIANT=
543 while [ -z "$TARGET_BUILD_VARIANT" ]
544 do
545 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000546 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800547 read ANSWER
548 else
549 echo $1
550 ANSWER=$1
551 fi
552
Matt Alexanderd9c56562020-05-21 10:49:17 +0000553 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800554 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000555 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
556 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200557 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558 fi
559 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000560 if check_variant $ANSWER
561 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562 export TARGET_BUILD_VARIANT=$ANSWER
563 else
564 echo "** Not a valid variant: $ANSWER"
565 fi
566 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000567 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800568 break
569 fi
570 done
571}
572
Matt Alexanderd9c56562020-05-21 10:49:17 +0000573function choosecombo()
574{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700575 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576
577 echo
578 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700579 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700580
581 echo
582 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700583 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800584
585 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800586 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800588 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800589 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590}
591
Matt Alexanderd9c56562020-05-21 10:49:17 +0000592function add_lunch_combo()
593{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800594 if [ -n "$ZSH_VERSION" ]; then
595 echo -n "${funcfiletrace[1]}: "
596 else
597 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
598 fi
599 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 -0800600}
601
Matt Alexanderd9c56562020-05-21 10:49:17 +0000602function print_lunch_menu()
603{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100605 local choices
606 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
607 local ret=$?
608
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700609 echo
610 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700611 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100612
Matt Alexanderd9c56562020-05-21 10:49:17 +0000613 if [ $ret -ne 0 ]
614 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100615 echo "Warning: Cannot display lunch menu."
616 echo
617 echo "Note: You can invoke lunch with an explicit target:"
618 echo
619 echo " usage: lunch [target]" >&2
620 echo
621 return
622 fi
623
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700624 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800625
626 local i=1
627 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700628 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800629 do
630 echo " $i. $choice"
631 i=$(($i+1))
632 done
633
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700634 echo
635}
636
Matt Alexanderd9c56562020-05-21 10:49:17 +0000637function lunch()
638{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800639 local answer
640
Steven Moreland92793dc2020-02-25 18:30:18 -0800641 if [[ $# -gt 1 ]]; then
642 echo "usage: lunch [target]" >&2
643 return 1
644 fi
645
646 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800647 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648 else
649 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700650 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800651 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700652 fi
653
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800654 local selection=
655
Matt Alexanderd9c56562020-05-21 10:49:17 +0000656 if [ -z "$answer" ]
657 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700658 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000659 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
660 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800661 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000662 if [ $answer -le ${#choices[@]} ]
663 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800664 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000665 if [ -n "$ZSH_VERSION" ]
666 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800667 selection=${choices[$(($answer))]}
668 else
669 selection=${choices[$(($answer-1))]}
670 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800671 fi
Colin Cross88737132017-03-21 17:41:03 -0700672 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800673 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700674 fi
675
Joe Onoratoda12daf2010-06-09 18:18:31 -0700676 export TARGET_BUILD_APPS=
677
Colin Cross88737132017-03-21 17:41:03 -0700678 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700679 product=${selection%%-*} # Trim everything after first dash
680 variant_and_version=${selection#*-} # Trim everything up to first dash
681 if [ "$variant_and_version" != "$selection" ]; then
682 variant=${variant_and_version%%-*}
683 if [ "$variant" != "$variant_and_version" ]; then
684 version=${variant_and_version#*-}
685 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700686 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800687
Matt Alexanderd9c56562020-05-21 10:49:17 +0000688 if [ -z "$product" ]
689 then
Ying Wang08800fd2016-03-03 20:57:21 -0800690 echo
Colin Cross88737132017-03-21 17:41:03 -0700691 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700692 return 1
693 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800694
Colin Cross88737132017-03-21 17:41:03 -0700695 TARGET_PRODUCT=$product \
696 TARGET_BUILD_VARIANT=$variant \
697 TARGET_PLATFORM_VERSION=$version \
698 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000699 if [ $? -ne 0 ]
700 then
Colin Cross88737132017-03-21 17:41:03 -0700701 return 1
702 fi
Colin Cross88737132017-03-21 17:41:03 -0700703 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
704 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700705 if [ -n "$version" ]; then
706 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
707 else
708 unset TARGET_PLATFORM_VERSION
709 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700710 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700711
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700712 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800713
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700714 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700715 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800716 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700717}
718
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700719unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700720# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000721function _lunch()
722{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700723 local cur prev opts
724 COMPREPLY=()
725 cur="${COMP_WORDS[COMP_CWORD]}"
726 prev="${COMP_WORDS[COMP_CWORD-1]}"
727
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700728 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800729 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700730 fi
731
732 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700733 return 0
734}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700735
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700737# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000738function tapas()
739{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700740 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800741 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700742 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700743 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 -0800744 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 -0700745
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700746 if [ "$showHelp" != "" ]; then
747 $(gettop)/build/make/tapasHelp.sh
748 return
749 fi
750
Ying Wang67f02922012-08-22 10:25:20 -0700751 if [ $(echo $arch | wc -w) -gt 1 ]; then
752 echo "tapas: Error: Multiple build archs supplied: $arch"
753 return
754 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700755 if [ $(echo $variant | wc -w) -gt 1 ]; then
756 echo "tapas: Error: Multiple build variants supplied: $variant"
757 return
758 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700759 if [ $(echo $density | wc -w) -gt 1 ]; then
760 echo "tapas: Error: Multiple densities supplied: $density"
761 return
762 fi
Ying Wang67f02922012-08-22 10:25:20 -0700763
Ying Wang0a76df52015-06-08 11:57:26 -0700764 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700765 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000766 x86) product=aosp_x86;;
767 arm64) product=aosp_arm64;;
768 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700769 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700770 if [ -z "$variant" ]; then
771 variant=eng
772 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700773 if [ -z "$apps" ]; then
774 apps=all
775 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600776 if [ -z "$density" ]; then
777 density=alldpi
778 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700779
Ying Wang67f02922012-08-22 10:25:20 -0700780 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700781 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700782 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700783 export TARGET_BUILD_TYPE=release
784 export TARGET_BUILD_APPS=$apps
785
Ying Wang08800fd2016-03-03 20:57:21 -0800786 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700787 set_stuff_for_environment
788 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800789 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700790}
791
Matt Alexanderd9c56562020-05-21 10:49:17 +0000792function gettop
793{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700794 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000795 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700796 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000797 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000799 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800800 # The following circumlocution (repeated below as well) ensures
801 # that we record the true directory name and not one that is
802 # faked up with symlink names.
803 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700804 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800805 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700806 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000807 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800808 \cd ..
synergyb112a402013-07-05 19:47:47 -0700809 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700810 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000811 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700812 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000813 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700814 fi
815 fi
816 fi
817}
818
Matt Alexanderd9c56562020-05-21 10:49:17 +0000819function croot()
820{
Christopher Ferris55257d22017-03-23 11:08:58 -0700821 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700822 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700823 if [ "$1" ]; then
824 \cd $(gettop)/$1
825 else
826 \cd $(gettop)
827 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700828 else
829 echo "Couldn't locate the top of the tree. Try setting TOP."
830 fi
831}
832
Matt Alexanderd9c56562020-05-21 10:49:17 +0000833function _croot()
834{
Anton Hanssonece9c482019-02-04 18:15:39 +0000835 local T=$(gettop)
836 if [ "$T" ]; then
837 local cur="${COMP_WORDS[COMP_CWORD]}"
838 k=0
839 for c in $(compgen -d ${T}/${cur}); do
840 COMPREPLY[k++]=${c#${T}/}/
841 done
842 fi
843}
844
Matt Alexanderd9c56562020-05-21 10:49:17 +0000845function cproj()
846{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700847 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700848 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700849 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700850 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
851 T=$PWD
852 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800853 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700854 return
855 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800856 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700857 done
Ying Wang9cd17642012-12-13 10:52:07 -0800858 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700859 echo "can't find Android.mk"
860}
861
Daniel Sandler47e0a882013-07-30 13:23:52 -0400862# simplified version of ps; output in the form
863# <pid> <procname>
864function qpid() {
865 local prepend=''
866 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000867 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400868 prepend=' '
869 append='$'
870 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000871 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800872 echo "usage: qpid [[--exact] <process name|pid>"
873 return 255
874 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400875
876 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000877 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800878 qpid | \grep "$prepend$EXE$append"
879 else
880 adb shell ps \
881 | tr -d '\r' \
882 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
883 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400884}
885
Steven Moreland74114f12020-09-10 01:23:32 +0000886# syswrite - disable verity, reboot if needed, and remount image
887#
888# Easy way to make system.img/etc writable
889function syswrite() {
890 adb wait-for-device && adb root || return 1
891 if [[ $(adb disable-verity | grep "reboot") ]]; then
892 echo "rebooting"
893 adb reboot && adb wait-for-device && adb root || return 1
894 fi
895 adb wait-for-device && adb remount || return 1
896}
897
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800898# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700899# that has the core-file-size limit set correctly
900#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800901# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700902# if its core-file-size limit is not set already.
903# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
904
Matt Alexanderd9c56562020-05-21 10:49:17 +0000905function coredump_setup()
906{
907 echo "Getting root...";
908 adb root;
909 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700910
Matt Alexanderd9c56562020-05-21 10:49:17 +0000911 echo "Remounting root partition read-write...";
912 adb shell mount -w -o remount -t rootfs rootfs;
913 sleep 1;
914 adb wait-for-device;
915 adb shell mkdir -p /cores;
916 adb shell mount -t tmpfs tmpfs /cores;
917 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700918
Matt Alexanderd9c56562020-05-21 10:49:17 +0000919 echo "Granting SELinux permission to dump in /cores...";
920 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700921
Matt Alexanderd9c56562020-05-21 10:49:17 +0000922 echo "Set core pattern.";
923 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700924
Ying Wang08800fd2016-03-03 20:57:21 -0800925 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700926}
927
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800928# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000929# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700930#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800931# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700932# dump to actually be generated.
933
Matt Alexanderd9c56562020-05-21 10:49:17 +0000934function coredump_enable()
935{
936 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800937 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000938 printf "Expecting a PID!\n";
939 return;
940 fi;
941 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800942 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700943}
944
945# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000946# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700947#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800948# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700949# enabled globally.
950
Matt Alexanderd9c56562020-05-21 10:49:17 +0000951function core()
952{
953 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700954
Ying Wang08800fd2016-03-03 20:57:21 -0800955 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000956 printf "Expecting a PID!\n";
957 return;
958 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700959
Matt Alexanderd9c56562020-05-21 10:49:17 +0000960 local CORENAME=core.$PID;
961 local COREPATH=/cores/$CORENAME;
962 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700963
Matt Alexanderd9c56562020-05-21 10:49:17 +0000964 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700965
Matt Alexanderd9c56562020-05-21 10:49:17 +0000966 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800967 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000968 printf "\tSending SIG%s to %d...\n" $SIG $PID;
969 adb shell kill -$SIG $PID;
970 sleep 1;
971 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700972
Matt Alexanderd9c56562020-05-21 10:49:17 +0000973 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
974 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700975}
976
Christopher Tate744ee802009-11-12 15:33:08 -0800977# systemstack - dump the current stack trace of all threads in the system process
978# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000979function systemstack()
980{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800981 stacks system_server
982}
983
Michael Wrightaeed7212014-06-19 19:58:12 -0700984# Read the ELF header from /proc/$PID/exe to determine if the process is
985# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000986function is64bit()
987{
Ben Chengfba67bf2014-02-25 10:27:07 -0800988 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000989 if [ "$PID" ] ; then
990 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800991 echo "64"
992 else
993 echo ""
994 fi
995 else
996 echo ""
997 fi
998}
999
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001000case `uname -s` in
1001 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001002 function sgrep()
1003 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001004 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 -04001005 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001006 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001007
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001008 ;;
1009 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001010 function sgrep()
1011 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001012 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 -04001013 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001014 }
1015 ;;
1016esac
1017
Matt Alexanderd9c56562020-05-21 10:49:17 +00001018function gettargetarch
1019{
Raghu Gandham8da43102012-07-25 19:57:22 -07001020 get_build_var TARGET_ARCH
1021}
1022
Matt Alexanderd9c56562020-05-21 10:49:17 +00001023function ggrep()
1024{
Mike Frysinger5e479732015-09-22 18:13:48 -04001025 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1026 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001027}
1028
Matt Alexanderd9c56562020-05-21 10:49:17 +00001029function gogrep()
1030{
Orion Hodson831472d2019-10-25 11:35:15 +01001031 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1032 -exec grep --color -n "$@" {} +
1033}
1034
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035function jgrep()
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 "*\.java" \
1038 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001039}
1040
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001041function rgrep()
1042{
1043 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1044 -exec grep --color -n "$@" {} +
1045}
1046
Matt Alexanderd9c56562020-05-21 10:49:17 +00001047function cgrep()
1048{
Mike Frysinger5e479732015-09-22 18:13:48 -04001049 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' \) \
1050 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001051}
1052
Matt Alexanderd9c56562020-05-21 10:49:17 +00001053function resgrep()
1054{
Christopher Ferris55257d22017-03-23 11:08:58 -07001055 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001056 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1057 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1058 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001059}
1060
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061function mangrep()
1062{
Mike Frysinger5e479732015-09-22 18:13:48 -04001063 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1064 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001065}
1066
Matt Alexanderd9c56562020-05-21 10:49:17 +00001067function owngrep()
1068{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001069 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1070 -exec grep --color -n "$@" {} +
1071}
1072
Matt Alexanderd9c56562020-05-21 10:49:17 +00001073function sepgrep()
1074{
Mike Frysinger5e479732015-09-22 18:13:48 -04001075 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1076 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001077}
1078
Matt Alexanderd9c56562020-05-21 10:49:17 +00001079function rcgrep()
1080{
Mike Frysinger5e479732015-09-22 18:13:48 -04001081 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1082 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001083}
1084
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001085case `uname -s` in
1086 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001087 function mgrep()
1088 {
Orion Hodson831472d2019-10-25 11:35:15 +01001089 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 -04001090 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001091 }
1092
Matt Alexanderd9c56562020-05-21 10:49:17 +00001093 function treegrep()
1094 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001095 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 -04001096 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001097 }
1098
1099 ;;
1100 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101 function mgrep()
1102 {
Orion Hodson831472d2019-10-25 11:35:15 +01001103 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 -04001104 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001105 }
1106
Matt Alexanderd9c56562020-05-21 10:49:17 +00001107 function treegrep()
1108 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001109 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 -04001110 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001111 }
1112
1113 ;;
1114esac
1115
Matt Alexanderd9c56562020-05-21 10:49:17 +00001116function getprebuilt
1117{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001118 get_abs_build_var ANDROID_PREBUILTS
1119}
1120
Matt Alexanderd9c56562020-05-21 10:49:17 +00001121function tracedmdump()
1122{
Christopher Ferris55257d22017-03-23 11:08:58 -07001123 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001124 if [ ! "$T" ]; then
1125 echo "Couldn't locate the top of the tree. Try setting TOP."
1126 return
1127 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001128 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001129 local arch=$(gettargetarch)
1130 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001131
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001132 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001133 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001134 echo "usage: tracedmdump tracename"
1135 return
1136 fi
1137
Matt Alexanderd9c56562020-05-21 10:49:17 +00001138 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001139 echo "Error: cannot find kernel: '$KERNEL'"
1140 return
1141 fi
1142
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001143 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001144 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001145 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1146 fi
1147
1148 echo "post-processing traces..."
1149 rm -f $TRACE/qtrace.dexlist
1150 post_trace $TRACE
1151 if [ $? -ne 0 ]; then
1152 echo "***"
1153 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1154 echo "***"
1155 return
1156 fi
1157 echo "generating dexlist output..."
1158 /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
1159 echo "generating dmtrace data..."
1160 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1161 echo "generating html file..."
1162 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1163 echo "done, see $TRACE/dmtrace.html for details"
1164 echo "or run:"
1165 echo " traceview $TRACE/dmtrace"
1166}
1167
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001168# communicate with a running device or emulator, set up necessary state,
1169# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001170function runhat()
1171{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001172 # process standard adb options
1173 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001174 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001175 adbTarget=$1
1176 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001177 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001178 adbTarget="$1 $2"
1179 shift 2
1180 fi
1181 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001182 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001183
1184 # runhat options
1185 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001186
Matt Alexanderd9c56562020-05-21 10:49:17 +00001187 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001188 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001189 return
1190 fi
1191
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001192 # confirm hat is available
1193 if [ -z $(which hat) ]; then
1194 echo "hat is not available in this configuration."
1195 return
1196 fi
1197
Andy McFaddenb6289852010-07-12 08:00:19 -07001198 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001199 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001200 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001201 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001202 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001203 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001204 echo -n "> "
1205 read
1206
The Android Open Source Project88b60792009-03-03 19:28:42 -08001207 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208
The Android Open Source Project88b60792009-03-03 19:28:42 -08001209 echo "Retrieving file $devFile..."
1210 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001211
The Android Open Source Project88b60792009-03-03 19:28:42 -08001212 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001213
The Android Open Source Project88b60792009-03-03 19:28:42 -08001214 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001215 echo "View the output by pointing your browser at http://localhost:7000/"
1216 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001217 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001218}
1219
Matt Alexanderd9c56562020-05-21 10:49:17 +00001220function getbugreports()
1221{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001222 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223
1224 if [ ! "$reports" ]; then
1225 echo "Could not locate any bugreports."
1226 return
1227 fi
1228
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001229 local report
1230 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001232 echo "/sdcard/bugreports/${report}"
1233 adb pull /sdcard/bugreports/${report} ${report}
1234 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001235 done
1236}
1237
Matt Alexanderd9c56562020-05-21 10:49:17 +00001238function getsdcardpath()
1239{
Victoria Lease1b296b42012-08-21 15:44:06 -07001240 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1241}
1242
Matt Alexanderd9c56562020-05-21 10:49:17 +00001243function getscreenshotpath()
1244{
Victoria Lease1b296b42012-08-21 15:44:06 -07001245 echo "$(getsdcardpath)/Pictures/Screenshots"
1246}
1247
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248function getlastscreenshot()
1249{
Victoria Lease1b296b42012-08-21 15:44:06 -07001250 local screenshot_path=$(getscreenshotpath)
1251 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 +00001252 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001253 echo "No screenshots found."
1254 return
1255 fi
1256 echo "${screenshot}"
1257 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1258}
1259
Matt Alexanderd9c56562020-05-21 10:49:17 +00001260function startviewserver()
1261{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001262 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001263 if [ $# -gt 0 ]; then
1264 port=$1
1265 fi
1266 adb shell service call window 1 i32 $port
1267}
1268
Matt Alexanderd9c56562020-05-21 10:49:17 +00001269function stopviewserver()
1270{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001271 adb shell service call window 2
1272}
1273
Matt Alexanderd9c56562020-05-21 10:49:17 +00001274function isviewserverstarted()
1275{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001276 adb shell service call window 3
1277}
1278
Matt Alexanderd9c56562020-05-21 10:49:17 +00001279function key_home()
1280{
Romain Guyb84049a2010-10-04 16:56:11 -07001281 adb shell input keyevent 3
1282}
1283
Matt Alexanderd9c56562020-05-21 10:49:17 +00001284function key_back()
1285{
Romain Guyb84049a2010-10-04 16:56:11 -07001286 adb shell input keyevent 4
1287}
1288
Matt Alexanderd9c56562020-05-21 10:49:17 +00001289function key_menu()
1290{
Romain Guyb84049a2010-10-04 16:56:11 -07001291 adb shell input keyevent 82
1292}
1293
Matt Alexanderd9c56562020-05-21 10:49:17 +00001294function smoketest()
1295{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001296 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1297 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1298 return
1299 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001300 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301 if [ ! "$T" ]; then
1302 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1303 return
1304 fi
1305
Ying Wang9cd17642012-12-13 10:52:07 -08001306 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001307 adb uninstall com.android.smoketest > /dev/null &&
1308 adb uninstall com.android.smoketest.tests > /dev/null &&
1309 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1310 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1311 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1312}
1313
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001314# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001315function runtest()
1316{
Christopher Ferris55257d22017-03-23 11:08:58 -07001317 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001318 if [ ! "$T" ]; then
1319 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1320 return
1321 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001322 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001323}
1324
The Android Open Source Project88b60792009-03-03 19:28:42 -08001325function godir () {
1326 if [[ -z "$1" ]]; then
1327 echo "Usage: godir <regex>"
1328 return
1329 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001330 local T=$(gettop)
1331 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001332 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001333 mkdir -p $OUT_DIR
1334 FILELIST=$OUT_DIR/filelist
1335 else
1336 FILELIST=$T/filelist
1337 fi
1338 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001339 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001340 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001341 echo " Done"
1342 echo ""
1343 fi
1344 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001345 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001346 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001347 echo "Not found"
1348 return
1349 fi
1350 local pathname
1351 local choice
1352 if [[ ${#lines[@]} > 1 ]]; then
1353 while [[ -z "$pathname" ]]; do
1354 local index=1
1355 local line
1356 for line in ${lines[@]}; do
1357 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001358 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001359 done
1360 echo
1361 echo -n "Select one: "
1362 unset choice
1363 read choice
1364 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1365 echo "Invalid choice"
1366 continue
1367 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001368 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001369 done
1370 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001371 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001372 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001373 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001374}
1375
Steven Moreland62054a42018-12-06 10:11:40 -08001376# Update module-info.json in out.
1377function refreshmod() {
1378 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1379 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1380 return 1
1381 fi
1382
1383 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1384
1385 # for the output of the next command
1386 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1387
1388 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001389 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001390 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1391}
1392
Cole Faust24c36db2021-01-23 02:39:37 +00001393# Verifies that module-info.txt exists, creating it if it doesn't.
1394function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001395 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1396 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1397 return 1
1398 fi
1399
1400 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1401 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1402 refreshmod || return 1
1403 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001404}
1405
1406# List all modules for the current device, as cached in module-info.json. If any build change is
1407# made and it should be reflected in the output, you should run 'refreshmod' first.
1408function allmod() {
1409 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001410
LuK1337b6a78192020-01-12 03:12:17 +01001411 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 -08001412}
1413
Rett Berg78d1c932019-01-24 14:34:23 -08001414# 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 -08001415# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001416function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001417 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001418 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001419 return 1
1420 fi
1421
Cole Faust24c36db2021-01-23 02:39:37 +00001422 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001423
1424 local relpath=$(python -c "import json, os
1425module = '$1'
1426module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1427if module not in module_info:
1428 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001429print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001430
1431 if [ -z "$relpath" ]; then
1432 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1433 return 1
1434 else
Rett Berg78d1c932019-01-24 14:34:23 -08001435 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001436 fi
1437}
1438
Rett Berg78d1c932019-01-24 14:34:23 -08001439# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1440# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1441function gomod() {
1442 if [[ $# -ne 1 ]]; then
1443 echo "usage: gomod <module>" >&2
1444 return 1
1445 fi
1446
1447 local path="$(pathmod $@)"
1448 if [ -z "$path" ]; then
1449 return 1
1450 fi
1451 cd $path
1452}
1453
Cole Faust24c36db2021-01-23 02:39:37 +00001454# Gets the list of a module's installed outputs, as cached in module-info.json.
1455# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1456function outmod() {
1457 if [[ $# -ne 1 ]]; then
1458 echo "usage: outmod <module>" >&2
1459 return 1
1460 fi
1461
1462 verifymodinfo || return 1
1463
1464 local relpath
1465 relpath=$(python -c "import json, os
1466module = '$1'
1467module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1468if module not in module_info:
1469 exit(1)
1470for output in module_info[module]['installed']:
1471 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1472
1473 if [ $? -ne 0 ]; then
1474 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1475 return 1
1476 elif [ ! -z "$relpath" ]; then
1477 echo "$relpath"
1478 fi
1479}
1480
1481# adb install a module's apk, as cached in module-info.json. If any build change
1482# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1483# Usage: installmod [adb install arguments] <module>
1484# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1485function installmod() {
1486 if [[ $# -eq 0 ]]; then
1487 echo "usage: installmod [adb install arguments] <module>" >&2
1488 return 1
1489 fi
1490
1491 local _path
1492 _path=$(outmod ${@:$#:1})
1493 if [ $? -ne 0 ]; then
1494 return 1
1495 fi
1496
1497 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1498 if [ -z "$_path" ]; then
1499 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1500 return 1
1501 fi
1502 local length=$(( $# - 1 ))
1503 echo adb install ${@:1:$length} $_path
1504 adb install ${@:1:$length} $_path
1505}
1506
dimitry73b84812018-12-11 18:06:00 +01001507function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001508 local word=${COMP_WORDS[COMP_CWORD]}
1509 COMPREPLY=( $(allmod | grep -E "^$word") )
1510}
1511
Alex Rayf0d08eb2013-03-08 15:15:06 -08001512# Print colored exit condition
1513function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001514 "$@"
1515 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001516 if [ $retval -ne 0 ]
1517 then
Jacky Cao89483b82015-05-15 22:12:53 +08001518 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001519 else
Jacky Cao89483b82015-05-15 22:12:53 +08001520 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001521 fi
1522 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001523}
1524
Matt Alexanderd9c56562020-05-21 10:49:17 +00001525function get_make_command()
1526{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001527 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1528 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001529 # Always use the real make if -C is passed in
1530 for arg in "$@"; do
1531 if [[ $arg == -C* ]]; then
1532 echo command make
1533 return
1534 fi
1535 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001536 echo build/soong/soong_ui.bash --make-mode
1537 else
1538 echo command make
1539 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001540}
1541
Matt Alexanderd9c56562020-05-21 10:49:17 +00001542function _wrap_build()
1543{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001544 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1545 "$@"
1546 return $?
1547 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001548 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001549 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001550 local ret=$?
1551 local end_time=$(date +"%s")
1552 local tdiff=$(($end_time-$start_time))
1553 local hours=$(($tdiff / 3600 ))
1554 local mins=$((($tdiff % 3600) / 60))
1555 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001556 local ncolors=$(tput colors 2>/dev/null)
1557 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001558 color_failed=$'\E'"[0;31m"
1559 color_success=$'\E'"[0;32m"
1560 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001561 else
1562 color_failed=""
1563 color_success=""
1564 color_reset=""
1565 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001566 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001567 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001568 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001569 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001570 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001571 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001572 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001573 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001574 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001575 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001576 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001577 printf "(%s seconds)" $secs
1578 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001579 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001580 echo
1581 return $ret
1582}
1583
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001584function _trigger_build()
1585(
1586 local -r bc="$1"; shift
1587 if T="$(gettop)"; then
1588 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1589 else
1590 echo "Couldn't locate the top of the tree. Try setting TOP."
1591 fi
1592)
1593
1594function m()
1595(
1596 _trigger_build "all-modules" "$@"
1597)
1598
1599function mm()
1600(
1601 _trigger_build "modules-in-a-dir-no-deps" "$@"
1602)
1603
1604function mmm()
1605(
1606 _trigger_build "modules-in-dirs-no-deps" "$@"
1607)
1608
1609function mma()
1610(
1611 _trigger_build "modules-in-a-dir" "$@"
1612)
1613
1614function mmma()
1615(
1616 _trigger_build "modules-in-dirs" "$@"
1617)
1618
Matt Alexanderd9c56562020-05-21 10:49:17 +00001619function make()
1620{
Dan Willemsene9842242017-07-28 13:00:13 -07001621 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001622}
1623
Matt Alexanderd9c56562020-05-21 10:49:17 +00001624function provision()
1625{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001626 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1627 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1628 return 1
1629 fi
1630 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1631 echo "There is no provisioning script for the device." >&2
1632 return 1
1633 fi
1634
1635 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001636 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001637 shift 1
1638 else
1639 echo "This action will reflash your device."
1640 echo ""
1641 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1642 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001643 echo -n "Are you sure you want to do this (yes/no)? "
1644 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001645 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001646 echo "Not taking any action. Exiting." >&2
1647 return 1
1648 fi
1649 fi
1650 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1651}
1652
Jim Tanga881a252018-06-19 16:34:41 +08001653# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001654function enable_zsh_completion() {
1655 # Don't override user's options if bash-style completion is already enabled.
1656 if ! declare -f complete >/dev/null; then
1657 autoload -U compinit && compinit
1658 autoload -U bashcompinit && bashcompinit
1659 fi
Jim Tanga881a252018-06-19 16:34:41 +08001660}
1661
1662function validate_current_shell() {
1663 local current_sh="$(ps -o command -p $$)"
1664 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001665 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001666 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001667 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001668 *zsh*)
1669 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001670 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001671 *)
Jim Tanga881a252018-06-19 16:34:41 +08001672 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 -07001673 ;;
1674 esac
Jim Tanga881a252018-06-19 16:34:41 +08001675}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001676
1677# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001678# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1679# load those.
1680#
1681# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001682function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001683 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001684 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001685 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001686 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 -08001687 if [ -n "$allowed" ]; then
1688 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1689 echo " $allowed"
1690 echo " $f"
1691 return
1692 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001693 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001694 done
1695
1696 allowed_files=
1697 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001698 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001699 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001700 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001701
1702 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001703 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001704 else
1705 echo "ignoring $f, not in $allowed"
1706 fi
Jim Tanga881a252018-06-19 16:34:41 +08001707 done
1708 done
1709}
Kenny Root52aa81c2011-07-15 11:07:06 -07001710
Dan Albertbab814f2020-08-26 15:34:53 -07001711function showcommands() {
1712 local T=$(gettop)
1713 if [[ -z "$TARGET_PRODUCT" ]]; then
1714 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1715 return
1716 fi
1717 case $(uname -s) in
1718 Darwin)
1719 PREBUILT_NAME=darwin-x86
1720 ;;
1721 Linux)
1722 PREBUILT_NAME=linux-x86
1723 ;;
1724 *)
1725 >&2 echo Unknown host $(uname -s)
1726 return
1727 ;;
1728 esac
1729 if [[ -z "$OUT_DIR" ]]; then
1730 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1731 OUT_DIR=out
1732 else
1733 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1734 fi
1735 fi
1736 if [[ "$1" == "--regenerate" ]]; then
1737 shift 1
1738 NINJA_ARGS="-t commands $@" m
1739 else
1740 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1741 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1742 -t commands "$@")
1743 fi
1744}
1745
Jim Tanga881a252018-06-19 16:34:41 +08001746validate_current_shell
1747source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001748addcompletions