blob: 58fcd3be6637850d9bd19f98350fbe6510a53358 [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.
Alistair Delva176e5342021-02-22 13:31:26 -080030- rsgrep: 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.
Joe Onorato2c1aa472021-02-25 16:42:39 -080038- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000039- installmod: Adb installs a module's built APK.
40- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000041- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070042
Roland Levillain39341922015-10-20 12:48:19 +010043Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070044- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080045- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070046
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070047Look at the source to view more functions. The complete list is:
48EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070049 local T=$(gettop)
50 local A=""
51 local i
Jacky Cao89483b82015-05-15 22:12:53 +080052 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 -070053 A="$A $i"
54 done
55 echo $A
56}
57
Ying Wang08800fd2016-03-03 20:57:21 -080058# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000059function build_build_var_cache()
60{
Christopher Ferris55257d22017-03-23 11:08:58 -070061 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080062 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080063 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' ' '`)
64 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 -080065 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080066 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080067 --vars="${cached_vars[*]}" \
68 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070069 --var-prefix=var_cache_ \
70 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080071 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000072 if [ $ret -ne 0 ]
73 then
Ying Wang08800fd2016-03-03 20:57:21 -080074 unset build_dicts_script
75 return $ret
76 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070077 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080078 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080079 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080080 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000081 if [ $ret -ne 0 ]
82 then
Ying Wang08800fd2016-03-03 20:57:21 -080083 return $ret
84 fi
85 BUILD_VAR_CACHE_READY="true"
86}
87
Ying Wangf0cb3972016-03-04 13:56:23 -080088# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080089# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000090function destroy_build_var_cache()
91{
Ying Wang08800fd2016-03-03 20:57:21 -080092 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070093 local v
Ying Wang08800fd2016-03-03 20:57:21 -080094 for v in $cached_vars; do
95 unset var_cache_$v
96 done
97 unset cached_vars
98 for v in $cached_abs_vars; do
99 unset abs_var_cache_$v
100 done
101 unset cached_abs_vars
102}
103
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700104# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000105function get_abs_build_var()
106{
107 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
108 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800109 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000110 return
Ying Wang08800fd2016-03-03 20:57:21 -0800111 fi
112
Christopher Ferris55257d22017-03-23 11:08:58 -0700113 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700114 if [ ! "$T" ]; then
115 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
116 return
117 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700118 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700119}
120
121# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000122function get_build_var()
123{
124 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
125 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800126 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100127 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800128 fi
129
Christopher Ferris55257d22017-03-23 11:08:58 -0700130 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700131 if [ ! "$T" ]; then
132 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100133 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700134 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700135 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800136}
137
138# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000139function check_product()
140{
Christopher Ferris55257d22017-03-23 11:08:58 -0700141 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142 if [ ! "$T" ]; then
143 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
144 return
145 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700146 TARGET_PRODUCT=$1 \
147 TARGET_BUILD_VARIANT= \
148 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700149 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800150 get_build_var TARGET_DEVICE > /dev/null
151 # hide successful answers, but allow the errors to show
152}
153
154VARIANT_CHOICES=(user userdebug eng)
155
156# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000157function check_variant()
158{
Christopher Ferris55257d22017-03-23 11:08:58 -0700159 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800160 for v in ${VARIANT_CHOICES[@]}
161 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000162 if [ "$v" = "$1" ]
163 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800164 return 0
165 fi
166 done
167 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700168}
169
Matt Alexanderd9c56562020-05-21 10:49:17 +0000170function setpaths()
171{
Christopher Ferris55257d22017-03-23 11:08:58 -0700172 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700173 if [ ! "$T" ]; then
174 echo "Couldn't locate the top of the tree. Try setting TOP."
175 return
176 fi
177
178 ##################################################################
179 # #
180 # Read me before you modify this code #
181 # #
182 # This function sets ANDROID_BUILD_PATHS to what it is adding #
183 # to PATH, and the next time it is run, it removes that from #
184 # PATH. This is required so lunch can be run more than once #
185 # and still have working paths. #
186 # #
187 ##################################################################
188
Raphael Mollc639c782011-06-20 17:25:01 -0700189 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
190 # due to "C:\Program Files" being in the path.
191
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700192 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000193 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700194 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
195 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000196 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700197 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800198 # strip leading ':', if any
199 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500200 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700201
202 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700203 local prebuiltdir=$(getprebuilt)
204 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700205
Ben Cheng8bc4c432012-11-16 13:29:13 -0800206 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
208 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800209 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800210
Raphael Mollc639c782011-06-20 17:25:01 -0700211 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800212 export ANDROID_TOOLCHAIN=
213 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200214 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700215 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200216 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000217 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700218 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000219 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400220 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200222 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
224 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700225 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200226 *)
227 echo "Can't find toolchain for unknown architecture: $ARCH"
228 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700229 ;;
230 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700231 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800232 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700233 fi
Raphael732936d2011-06-22 14:35:32 -0700234
Christopher Ferris55257d22017-03-23 11:08:58 -0700235 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800236 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
237 fi
238
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700239 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700240
241 # add kernel specific binaries
242 case $(uname -s) in
243 Linux)
244 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
245 ;;
246 *)
247 ;;
248 esac
249
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400250 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
251 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
252 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
253 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700254 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
255
256 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
257 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
258 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200259
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800260 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
261 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
262
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200263 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
264 # to ensure that the corresponding 'emulator' binaries are used.
265 case $(uname -s) in
266 Darwin)
267 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
268 ;;
269 Linux)
270 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
271 ;;
272 *)
273 ANDROID_EMULATOR_PREBUILTS=
274 ;;
275 esac
276 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700277 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200278 export ANDROID_EMULATOR_PREBUILTS
279 fi
280
Jim Tangb3fda302018-12-22 10:24:55 +0800281 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
282 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700283 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
284 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
285 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
286 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800287
Ryan Prichard8426a192019-07-15 18:05:29 -0700288 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800289
290 # out with the duplicate old
291 if [ -n $ANDROID_PYTHONPATH ]; then
292 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
293 fi
294 # and in with the new
295 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800296 if [ -n $VENDOR_PYTHONPATH ]; then
297 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
298 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800299 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800300
Colin Crosse97e6932017-06-30 16:01:45 -0700301 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
302 export JAVA_HOME=$ANDROID_JAVA_HOME
303 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
304 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
305 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500306
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800307 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700308 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
309 export OUT=$ANDROID_PRODUCT_OUT
310
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700311 unset ANDROID_HOST_OUT
312 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
313
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900314 unset ANDROID_SOONG_HOST_OUT
315 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
316
Simran Basidd050ed2017-02-13 13:46:48 -0800317 unset ANDROID_HOST_OUT_TESTCASES
318 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
319
320 unset ANDROID_TARGET_OUT_TESTCASES
321 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
322
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800323 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700324 # TODO: fix the path
325 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
326}
327
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000328function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000329{
330 local T="$(gettop)"
331 if [ ! "$T" ]; then
332 echo "Couldn't locate the top of the tree. Try setting TOP."
333 return
334 fi
335
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000336 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500337 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
338 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000339 fi
340
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000341 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000342}
343
Matt Alexanderd9c56562020-05-21 10:49:17 +0000344function printconfig()
345{
Christopher Ferris55257d22017-03-23 11:08:58 -0700346 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800347 if [ ! "$T" ]; then
348 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
349 return
350 fi
351 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700352}
353
Matt Alexanderd9c56562020-05-21 10:49:17 +0000354function set_stuff_for_environment()
355{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800356 setpaths
357 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700358
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800359 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700360 # With this environment variable new GCC can apply colors to warnings/errors
361 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 -0700362}
363
Matt Alexanderd9c56562020-05-21 10:49:17 +0000364function set_sequence_number()
365{
Colin Cross88737132017-03-21 17:41:03 -0700366 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700367}
368
Makoto Onukida971062018-06-18 10:15:19 -0700369# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
370function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800371 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700372 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800373 *:"$cmd":*)
374 return 1
375 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700376 esac
377 return 0
378}
379
Matt Alexanderd9c56562020-05-21 10:49:17 +0000380function addcompletions()
381{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700382 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700383
Jim Tanga881a252018-06-19 16:34:41 +0800384 # Keep us from trying to run in something that's neither bash nor zsh.
385 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700386 return
387 fi
388
389 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800390 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700391 return
392 fi
393
Jim Tanga881a252018-06-19 16:34:41 +0800394 local completion_files=(
395 system/core/adb/adb.bash
396 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800397 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800398 )
Makoto Onukida971062018-06-18 10:15:19 -0700399 # Completion can be disabled selectively to allow users to use non-standard completion.
400 # e.g.
401 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
402 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800403 for f in ${completion_files[*]}; do
404 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700405 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700406 fi
407 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700408
Matt Alexanderd9c56562020-05-21 10:49:17 +0000409 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700410 complete -C "bit --tab" bit
411 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000412 if [ -z "$ZSH_VERSION" ]; then
413 # Doesn't work in zsh.
414 complete -o nospace -F _croot croot
415 fi
Jim Tanga881a252018-06-19 16:34:41 +0800416 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800417
Cole Faust24c36db2021-01-23 02:39:37 +0000418 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100419 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000420 complete -F _complete_android_module_names outmod
421 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100422 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700423}
424
Matt Alexanderd9c56562020-05-21 10:49:17 +0000425function choosetype()
426{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700427 echo "Build type choices are:"
428 echo " 1. release"
429 echo " 2. debug"
430 echo
431
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800432 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700433 DEFAULT_NUM=1
434 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800436 export TARGET_BUILD_TYPE=
437 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438 while [ -z $TARGET_BUILD_TYPE ]
439 do
440 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000441 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800442 read ANSWER
443 else
444 echo $1
445 ANSWER=$1
446 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700447 case $ANSWER in
448 "")
449 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
450 ;;
451 1)
452 export TARGET_BUILD_TYPE=release
453 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800454 release)
455 export TARGET_BUILD_TYPE=release
456 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700457 2)
458 export TARGET_BUILD_TYPE=debug
459 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800460 debug)
461 export TARGET_BUILD_TYPE=debug
462 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700463 *)
464 echo
465 echo "I didn't understand your response. Please try again."
466 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700467 ;;
468 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000469 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800470 break
471 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472 done
473
Ying Wang08800fd2016-03-03 20:57:21 -0800474 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800476 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477}
478
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800479#
480# This function isn't really right: It chooses a TARGET_PRODUCT
481# based on the list of boards. Usually, that gets you something
482# that kinda works with a generic product, but really, you should
483# pick a product by name.
484#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000485function chooseproduct()
486{
Christopher Ferris55257d22017-03-23 11:08:58 -0700487 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000488 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700489 default_value=$TARGET_PRODUCT
490 else
Ying Wang0a76df52015-06-08 11:57:26 -0700491 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492 fi
493
Ying Wang08800fd2016-03-03 20:57:21 -0800494 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800495 export TARGET_PRODUCT=
496 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700497 while [ -z "$TARGET_PRODUCT" ]
498 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700499 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000500 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800501 read ANSWER
502 else
503 echo $1
504 ANSWER=$1
505 fi
506
Matt Alexanderd9c56562020-05-21 10:49:17 +0000507 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700508 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800509 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000510 if check_product $ANSWER
511 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 export TARGET_PRODUCT=$ANSWER
513 else
514 echo "** Not a valid product: $ANSWER"
515 fi
516 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000517 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800518 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700519 fi
520 done
521
Ying Wang08800fd2016-03-03 20:57:21 -0800522 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700523 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800524 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700525}
526
Matt Alexanderd9c56562020-05-21 10:49:17 +0000527function choosevariant()
528{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800529 echo "Variant choices are:"
530 local index=1
531 local v
532 for v in ${VARIANT_CHOICES[@]}
533 do
534 # The product name is the name of the directory containing
535 # the makefile we found, above.
536 echo " $index. $v"
537 index=$(($index+1))
538 done
539
540 local default_value=eng
541 local ANSWER
542
543 export TARGET_BUILD_VARIANT=
544 while [ -z "$TARGET_BUILD_VARIANT" ]
545 do
546 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000547 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 read ANSWER
549 else
550 echo $1
551 ANSWER=$1
552 fi
553
Matt Alexanderd9c56562020-05-21 10:49:17 +0000554 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800555 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000556 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
557 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200558 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800559 fi
560 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000561 if check_variant $ANSWER
562 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800563 export TARGET_BUILD_VARIANT=$ANSWER
564 else
565 echo "** Not a valid variant: $ANSWER"
566 fi
567 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000568 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800569 break
570 fi
571 done
572}
573
Matt Alexanderd9c56562020-05-21 10:49:17 +0000574function choosecombo()
575{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700576 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577
578 echo
579 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700580 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700581
582 echo
583 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700584 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585
586 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800587 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700588 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800590 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700591}
592
Matt Alexanderd9c56562020-05-21 10:49:17 +0000593function add_lunch_combo()
594{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800595 if [ -n "$ZSH_VERSION" ]; then
596 echo -n "${funcfiletrace[1]}: "
597 else
598 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
599 fi
600 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 -0800601}
602
Matt Alexanderd9c56562020-05-21 10:49:17 +0000603function print_lunch_menu()
604{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100606 local choices
607 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
608 local ret=$?
609
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700610 echo
611 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700612 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100613
Matt Alexanderd9c56562020-05-21 10:49:17 +0000614 if [ $ret -ne 0 ]
615 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100616 echo "Warning: Cannot display lunch menu."
617 echo
618 echo "Note: You can invoke lunch with an explicit target:"
619 echo
620 echo " usage: lunch [target]" >&2
621 echo
622 return
623 fi
624
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700625 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626
627 local i=1
628 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700629 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800630 do
631 echo " $i. $choice"
632 i=$(($i+1))
633 done
634
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700635 echo
636}
637
Matt Alexanderd9c56562020-05-21 10:49:17 +0000638function lunch()
639{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640 local answer
641
Steven Moreland92793dc2020-02-25 18:30:18 -0800642 if [[ $# -gt 1 ]]; then
643 echo "usage: lunch [target]" >&2
644 return 1
645 fi
646
647 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800648 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700649 else
650 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700651 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800652 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700653 fi
654
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800655 local selection=
656
Matt Alexanderd9c56562020-05-21 10:49:17 +0000657 if [ -z "$answer" ]
658 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700659 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000660 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
661 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800662 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000663 if [ $answer -le ${#choices[@]} ]
664 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800665 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000666 if [ -n "$ZSH_VERSION" ]
667 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800668 selection=${choices[$(($answer))]}
669 else
670 selection=${choices[$(($answer-1))]}
671 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800672 fi
Colin Cross88737132017-03-21 17:41:03 -0700673 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800674 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700675 fi
676
Joe Onoratoda12daf2010-06-09 18:18:31 -0700677 export TARGET_BUILD_APPS=
678
Colin Cross88737132017-03-21 17:41:03 -0700679 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700680 product=${selection%%-*} # Trim everything after first dash
681 variant_and_version=${selection#*-} # Trim everything up to first dash
682 if [ "$variant_and_version" != "$selection" ]; then
683 variant=${variant_and_version%%-*}
684 if [ "$variant" != "$variant_and_version" ]; then
685 version=${variant_and_version#*-}
686 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700687 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800688
Matt Alexanderd9c56562020-05-21 10:49:17 +0000689 if [ -z "$product" ]
690 then
Ying Wang08800fd2016-03-03 20:57:21 -0800691 echo
Colin Cross88737132017-03-21 17:41:03 -0700692 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700693 return 1
694 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800695
Colin Cross88737132017-03-21 17:41:03 -0700696 TARGET_PRODUCT=$product \
697 TARGET_BUILD_VARIANT=$variant \
698 TARGET_PLATFORM_VERSION=$version \
699 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000700 if [ $? -ne 0 ]
701 then
Colin Cross88737132017-03-21 17:41:03 -0700702 return 1
703 fi
Colin Cross88737132017-03-21 17:41:03 -0700704 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
705 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700706 if [ -n "$version" ]; then
707 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
708 else
709 unset TARGET_PLATFORM_VERSION
710 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700711 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700712
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700713 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800714
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700715 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700716 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800717 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700718}
719
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700720unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700721# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000722function _lunch()
723{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700724 local cur prev opts
725 COMPREPLY=()
726 cur="${COMP_WORDS[COMP_CWORD]}"
727 prev="${COMP_WORDS[COMP_CWORD-1]}"
728
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700729 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800730 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700731 fi
732
733 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700734 return 0
735}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700736
Joe Onoratoda12daf2010-06-09 18:18:31 -0700737# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700738# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000739function tapas()
740{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700741 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800742 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700743 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700744 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 -0800745 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 -0700746
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700747 if [ "$showHelp" != "" ]; then
748 $(gettop)/build/make/tapasHelp.sh
749 return
750 fi
751
Ying Wang67f02922012-08-22 10:25:20 -0700752 if [ $(echo $arch | wc -w) -gt 1 ]; then
753 echo "tapas: Error: Multiple build archs supplied: $arch"
754 return
755 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700756 if [ $(echo $variant | wc -w) -gt 1 ]; then
757 echo "tapas: Error: Multiple build variants supplied: $variant"
758 return
759 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700760 if [ $(echo $density | wc -w) -gt 1 ]; then
761 echo "tapas: Error: Multiple densities supplied: $density"
762 return
763 fi
Ying Wang67f02922012-08-22 10:25:20 -0700764
Ying Wang0a76df52015-06-08 11:57:26 -0700765 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700766 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000767 x86) product=aosp_x86;;
768 arm64) product=aosp_arm64;;
769 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700770 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700771 if [ -z "$variant" ]; then
772 variant=eng
773 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700774 if [ -z "$apps" ]; then
775 apps=all
776 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600777 if [ -z "$density" ]; then
778 density=alldpi
779 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700780
Ying Wang67f02922012-08-22 10:25:20 -0700781 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700782 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700783 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700784 export TARGET_BUILD_TYPE=release
785 export TARGET_BUILD_APPS=$apps
786
Ying Wang08800fd2016-03-03 20:57:21 -0800787 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700788 set_stuff_for_environment
789 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800790 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700791}
792
Matt Alexanderd9c56562020-05-21 10:49:17 +0000793function gettop
794{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700795 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000796 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700797 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000798 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700799 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000800 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800801 # The following circumlocution (repeated below as well) ensures
802 # that we record the true directory name and not one that is
803 # faked up with symlink names.
804 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700805 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800806 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700807 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000808 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800809 \cd ..
synergyb112a402013-07-05 19:47:47 -0700810 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700811 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000812 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700813 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000814 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700815 fi
816 fi
817 fi
818}
819
Matt Alexanderd9c56562020-05-21 10:49:17 +0000820function croot()
821{
Christopher Ferris55257d22017-03-23 11:08:58 -0700822 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700823 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700824 if [ "$1" ]; then
825 \cd $(gettop)/$1
826 else
827 \cd $(gettop)
828 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700829 else
830 echo "Couldn't locate the top of the tree. Try setting TOP."
831 fi
832}
833
Matt Alexanderd9c56562020-05-21 10:49:17 +0000834function _croot()
835{
Anton Hanssonece9c482019-02-04 18:15:39 +0000836 local T=$(gettop)
837 if [ "$T" ]; then
838 local cur="${COMP_WORDS[COMP_CWORD]}"
839 k=0
840 for c in $(compgen -d ${T}/${cur}); do
841 COMPREPLY[k++]=${c#${T}/}/
842 done
843 fi
844}
845
Matt Alexanderd9c56562020-05-21 10:49:17 +0000846function cproj()
847{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700848 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700849 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700850 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700851 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
852 T=$PWD
853 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800854 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700855 return
856 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800857 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700858 done
Ying Wang9cd17642012-12-13 10:52:07 -0800859 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700860 echo "can't find Android.mk"
861}
862
Daniel Sandler47e0a882013-07-30 13:23:52 -0400863# simplified version of ps; output in the form
864# <pid> <procname>
865function qpid() {
866 local prepend=''
867 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000868 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400869 prepend=' '
870 append='$'
871 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000872 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800873 echo "usage: qpid [[--exact] <process name|pid>"
874 return 255
875 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400876
877 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000878 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800879 qpid | \grep "$prepend$EXE$append"
880 else
881 adb shell ps \
882 | tr -d '\r' \
883 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
884 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400885}
886
Steven Moreland74114f12020-09-10 01:23:32 +0000887# syswrite - disable verity, reboot if needed, and remount image
888#
889# Easy way to make system.img/etc writable
890function syswrite() {
891 adb wait-for-device && adb root || return 1
892 if [[ $(adb disable-verity | grep "reboot") ]]; then
893 echo "rebooting"
894 adb reboot && adb wait-for-device && adb root || return 1
895 fi
896 adb wait-for-device && adb remount || return 1
897}
898
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800899# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700900# that has the core-file-size limit set correctly
901#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800902# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700903# if its core-file-size limit is not set already.
904# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
905
Matt Alexanderd9c56562020-05-21 10:49:17 +0000906function coredump_setup()
907{
908 echo "Getting root...";
909 adb root;
910 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700911
Matt Alexanderd9c56562020-05-21 10:49:17 +0000912 echo "Remounting root partition read-write...";
913 adb shell mount -w -o remount -t rootfs rootfs;
914 sleep 1;
915 adb wait-for-device;
916 adb shell mkdir -p /cores;
917 adb shell mount -t tmpfs tmpfs /cores;
918 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700919
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920 echo "Granting SELinux permission to dump in /cores...";
921 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700922
Matt Alexanderd9c56562020-05-21 10:49:17 +0000923 echo "Set core pattern.";
924 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700925
Ying Wang08800fd2016-03-03 20:57:21 -0800926 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700927}
928
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800929# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000930# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700931#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800932# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700933# dump to actually be generated.
934
Matt Alexanderd9c56562020-05-21 10:49:17 +0000935function coredump_enable()
936{
937 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800938 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000939 printf "Expecting a PID!\n";
940 return;
941 fi;
942 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800943 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700944}
945
946# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000947# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700948#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800949# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700950# enabled globally.
951
Matt Alexanderd9c56562020-05-21 10:49:17 +0000952function core()
953{
954 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700955
Ying Wang08800fd2016-03-03 20:57:21 -0800956 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000957 printf "Expecting a PID!\n";
958 return;
959 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700960
Matt Alexanderd9c56562020-05-21 10:49:17 +0000961 local CORENAME=core.$PID;
962 local COREPATH=/cores/$CORENAME;
963 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700964
Matt Alexanderd9c56562020-05-21 10:49:17 +0000965 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700966
Matt Alexanderd9c56562020-05-21 10:49:17 +0000967 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800968 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000969 printf "\tSending SIG%s to %d...\n" $SIG $PID;
970 adb shell kill -$SIG $PID;
971 sleep 1;
972 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700973
Matt Alexanderd9c56562020-05-21 10:49:17 +0000974 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
975 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700976}
977
Christopher Tate744ee802009-11-12 15:33:08 -0800978# systemstack - dump the current stack trace of all threads in the system process
979# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000980function systemstack()
981{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800982 stacks system_server
983}
984
Michael Wrightaeed7212014-06-19 19:58:12 -0700985# Read the ELF header from /proc/$PID/exe to determine if the process is
986# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000987function is64bit()
988{
Ben Chengfba67bf2014-02-25 10:27:07 -0800989 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000990 if [ "$PID" ] ; then
991 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800992 echo "64"
993 else
994 echo ""
995 fi
996 else
997 echo ""
998 fi
999}
1000
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001001case `uname -s` in
1002 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001003 function sgrep()
1004 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001005 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 -04001006 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001007 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001008
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001009 ;;
1010 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001011 function sgrep()
1012 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001013 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 -04001014 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001015 }
1016 ;;
1017esac
1018
Matt Alexanderd9c56562020-05-21 10:49:17 +00001019function gettargetarch
1020{
Raghu Gandham8da43102012-07-25 19:57:22 -07001021 get_build_var TARGET_ARCH
1022}
1023
Matt Alexanderd9c56562020-05-21 10:49:17 +00001024function ggrep()
1025{
Mike Frysinger5e479732015-09-22 18:13:48 -04001026 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1027 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001028}
1029
Matt Alexanderd9c56562020-05-21 10:49:17 +00001030function gogrep()
1031{
Orion Hodson831472d2019-10-25 11:35:15 +01001032 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1033 -exec grep --color -n "$@" {} +
1034}
1035
Matt Alexanderd9c56562020-05-21 10:49:17 +00001036function jgrep()
1037{
Mike Frysinger5e479732015-09-22 18:13:48 -04001038 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1039 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001040}
1041
Alistair Delva176e5342021-02-22 13:31:26 -08001042function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001043{
1044 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1045 -exec grep --color -n "$@" {} +
1046}
1047
Matt Alexanderd9c56562020-05-21 10:49:17 +00001048function cgrep()
1049{
Mike Frysinger5e479732015-09-22 18:13:48 -04001050 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' \) \
1051 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001052}
1053
Matt Alexanderd9c56562020-05-21 10:49:17 +00001054function resgrep()
1055{
Christopher Ferris55257d22017-03-23 11:08:58 -07001056 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001057 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1058 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1059 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001060}
1061
Matt Alexanderd9c56562020-05-21 10:49:17 +00001062function mangrep()
1063{
Mike Frysinger5e479732015-09-22 18:13:48 -04001064 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1065 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001066}
1067
Matt Alexanderd9c56562020-05-21 10:49:17 +00001068function owngrep()
1069{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001070 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1071 -exec grep --color -n "$@" {} +
1072}
1073
Matt Alexanderd9c56562020-05-21 10:49:17 +00001074function sepgrep()
1075{
Mike Frysinger5e479732015-09-22 18:13:48 -04001076 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1077 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001078}
1079
Matt Alexanderd9c56562020-05-21 10:49:17 +00001080function rcgrep()
1081{
Mike Frysinger5e479732015-09-22 18:13:48 -04001082 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1083 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001084}
1085
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001086case `uname -s` in
1087 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001088 function mgrep()
1089 {
Orion Hodson831472d2019-10-25 11:35:15 +01001090 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 -04001091 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001092 }
1093
Matt Alexanderd9c56562020-05-21 10:49:17 +00001094 function treegrep()
1095 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001096 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 -04001097 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001098 }
1099
1100 ;;
1101 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001102 function mgrep()
1103 {
Orion Hodson831472d2019-10-25 11:35:15 +01001104 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 -04001105 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001106 }
1107
Matt Alexanderd9c56562020-05-21 10:49:17 +00001108 function treegrep()
1109 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001110 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 -04001111 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001112 }
1113
1114 ;;
1115esac
1116
Matt Alexanderd9c56562020-05-21 10:49:17 +00001117function getprebuilt
1118{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119 get_abs_build_var ANDROID_PREBUILTS
1120}
1121
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122function tracedmdump()
1123{
Christopher Ferris55257d22017-03-23 11:08:58 -07001124 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001125 if [ ! "$T" ]; then
1126 echo "Couldn't locate the top of the tree. Try setting TOP."
1127 return
1128 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001129 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001130 local arch=$(gettargetarch)
1131 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001132
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001133 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001134 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001135 echo "usage: tracedmdump tracename"
1136 return
1137 fi
1138
Matt Alexanderd9c56562020-05-21 10:49:17 +00001139 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001140 echo "Error: cannot find kernel: '$KERNEL'"
1141 return
1142 fi
1143
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001144 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001146 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1147 fi
1148
1149 echo "post-processing traces..."
1150 rm -f $TRACE/qtrace.dexlist
1151 post_trace $TRACE
1152 if [ $? -ne 0 ]; then
1153 echo "***"
1154 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1155 echo "***"
1156 return
1157 fi
1158 echo "generating dexlist output..."
1159 /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
1160 echo "generating dmtrace data..."
1161 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1162 echo "generating html file..."
1163 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1164 echo "done, see $TRACE/dmtrace.html for details"
1165 echo "or run:"
1166 echo " traceview $TRACE/dmtrace"
1167}
1168
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001169# communicate with a running device or emulator, set up necessary state,
1170# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001171function runhat()
1172{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001173 # process standard adb options
1174 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001175 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001176 adbTarget=$1
1177 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001179 adbTarget="$1 $2"
1180 shift 2
1181 fi
1182 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001183 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001184
1185 # runhat options
1186 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001187
Matt Alexanderd9c56562020-05-21 10:49:17 +00001188 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001189 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190 return
1191 fi
1192
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001193 # confirm hat is available
1194 if [ -z $(which hat) ]; then
1195 echo "hat is not available in this configuration."
1196 return
1197 fi
1198
Andy McFaddenb6289852010-07-12 08:00:19 -07001199 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001200 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001201 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001202 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001203 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001204 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001205 echo -n "> "
1206 read
1207
The Android Open Source Project88b60792009-03-03 19:28:42 -08001208 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001209
The Android Open Source Project88b60792009-03-03 19:28:42 -08001210 echo "Retrieving file $devFile..."
1211 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001212
The Android Open Source Project88b60792009-03-03 19:28:42 -08001213 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001214
The Android Open Source Project88b60792009-03-03 19:28:42 -08001215 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216 echo "View the output by pointing your browser at http://localhost:7000/"
1217 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001218 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219}
1220
Matt Alexanderd9c56562020-05-21 10:49:17 +00001221function getbugreports()
1222{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001223 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001224
1225 if [ ! "$reports" ]; then
1226 echo "Could not locate any bugreports."
1227 return
1228 fi
1229
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001230 local report
1231 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001232 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001233 echo "/sdcard/bugreports/${report}"
1234 adb pull /sdcard/bugreports/${report} ${report}
1235 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001236 done
1237}
1238
Matt Alexanderd9c56562020-05-21 10:49:17 +00001239function getsdcardpath()
1240{
Victoria Lease1b296b42012-08-21 15:44:06 -07001241 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1242}
1243
Matt Alexanderd9c56562020-05-21 10:49:17 +00001244function getscreenshotpath()
1245{
Victoria Lease1b296b42012-08-21 15:44:06 -07001246 echo "$(getsdcardpath)/Pictures/Screenshots"
1247}
1248
Matt Alexanderd9c56562020-05-21 10:49:17 +00001249function getlastscreenshot()
1250{
Victoria Lease1b296b42012-08-21 15:44:06 -07001251 local screenshot_path=$(getscreenshotpath)
1252 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 +00001253 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001254 echo "No screenshots found."
1255 return
1256 fi
1257 echo "${screenshot}"
1258 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1259}
1260
Matt Alexanderd9c56562020-05-21 10:49:17 +00001261function startviewserver()
1262{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001263 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264 if [ $# -gt 0 ]; then
1265 port=$1
1266 fi
1267 adb shell service call window 1 i32 $port
1268}
1269
Matt Alexanderd9c56562020-05-21 10:49:17 +00001270function stopviewserver()
1271{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001272 adb shell service call window 2
1273}
1274
Matt Alexanderd9c56562020-05-21 10:49:17 +00001275function isviewserverstarted()
1276{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001277 adb shell service call window 3
1278}
1279
Matt Alexanderd9c56562020-05-21 10:49:17 +00001280function key_home()
1281{
Romain Guyb84049a2010-10-04 16:56:11 -07001282 adb shell input keyevent 3
1283}
1284
Matt Alexanderd9c56562020-05-21 10:49:17 +00001285function key_back()
1286{
Romain Guyb84049a2010-10-04 16:56:11 -07001287 adb shell input keyevent 4
1288}
1289
Matt Alexanderd9c56562020-05-21 10:49:17 +00001290function key_menu()
1291{
Romain Guyb84049a2010-10-04 16:56:11 -07001292 adb shell input keyevent 82
1293}
1294
Matt Alexanderd9c56562020-05-21 10:49:17 +00001295function smoketest()
1296{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001297 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1298 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1299 return
1300 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001301 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001302 if [ ! "$T" ]; then
1303 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1304 return
1305 fi
1306
Ying Wang9cd17642012-12-13 10:52:07 -08001307 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001308 adb uninstall com.android.smoketest > /dev/null &&
1309 adb uninstall com.android.smoketest.tests > /dev/null &&
1310 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1311 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1312 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1313}
1314
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001315# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001316function runtest()
1317{
Christopher Ferris55257d22017-03-23 11:08:58 -07001318 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001319 if [ ! "$T" ]; then
1320 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1321 return
1322 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001323 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001324}
1325
The Android Open Source Project88b60792009-03-03 19:28:42 -08001326function godir () {
1327 if [[ -z "$1" ]]; then
1328 echo "Usage: godir <regex>"
1329 return
1330 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001331 local T=$(gettop)
1332 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001333 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001334 mkdir -p $OUT_DIR
1335 FILELIST=$OUT_DIR/filelist
1336 else
1337 FILELIST=$T/filelist
1338 fi
1339 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001340 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001341 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001342 echo " Done"
1343 echo ""
1344 fi
1345 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001346 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001347 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001348 echo "Not found"
1349 return
1350 fi
1351 local pathname
1352 local choice
1353 if [[ ${#lines[@]} > 1 ]]; then
1354 while [[ -z "$pathname" ]]; do
1355 local index=1
1356 local line
1357 for line in ${lines[@]}; do
1358 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001359 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001360 done
1361 echo
1362 echo -n "Select one: "
1363 unset choice
1364 read choice
1365 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1366 echo "Invalid choice"
1367 continue
1368 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001369 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001370 done
1371 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001372 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001373 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001374 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001375}
1376
Steven Moreland62054a42018-12-06 10:11:40 -08001377# Update module-info.json in out.
1378function refreshmod() {
1379 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1380 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1381 return 1
1382 fi
1383
1384 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1385
1386 # for the output of the next command
1387 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1388
1389 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001390 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001391 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1392}
1393
Cole Faust24c36db2021-01-23 02:39:37 +00001394# Verifies that module-info.txt exists, creating it if it doesn't.
1395function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001396 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1397 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1398 return 1
1399 fi
1400
1401 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1402 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1403 refreshmod || return 1
1404 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001405}
1406
1407# List all modules for the current device, as cached in module-info.json. If any build change is
1408# made and it should be reflected in the output, you should run 'refreshmod' first.
1409function allmod() {
1410 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001411
LuK1337b6a78192020-01-12 03:12:17 +01001412 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 -08001413}
1414
Joe Onorato2c1aa472021-02-25 16:42:39 -08001415# Get the path of a specific module in the android tree, as cached in module-info.json.
1416# If any build change is made, and it should be reflected in the output, you should run
1417# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001418function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001419 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001420 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001421 return 1
1422 fi
1423
Cole Faust24c36db2021-01-23 02:39:37 +00001424 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001425
1426 local relpath=$(python -c "import json, os
1427module = '$1'
1428module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1429if module not in module_info:
1430 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001431print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001432
1433 if [ -z "$relpath" ]; then
1434 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1435 return 1
1436 else
Rett Berg78d1c932019-01-24 14:34:23 -08001437 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001438 fi
1439}
1440
Joe Onorato2c1aa472021-02-25 16:42:39 -08001441# Get the path of a specific module in the android tree, as cached in module-info.json.
1442# If any build change is made, and it should be reflected in the output, you should run
1443# 'refreshmod' first. Note: This is the inverse of pathmod.
1444function dirmods() {
1445 if [[ $# -ne 1 ]]; then
1446 echo "usage: dirmods <path>" >&2
1447 return 1
1448 fi
1449
1450 verifymodinfo || return 1
1451
1452 python -c "import json, os
1453dir = '$1'
1454while dir.endswith('/'):
1455 dir = dir[:-1]
1456prefix = dir + '/'
1457module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1458results = set()
1459for m in module_info.values():
1460 for path in m.get(u'path', []):
1461 if path == dir or path.startswith(prefix):
1462 name = m.get(u'module_name')
1463 if name:
1464 results.add(name)
1465for name in sorted(results):
1466 print(name)
1467"
1468}
1469
1470
Rett Berg78d1c932019-01-24 14:34:23 -08001471# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1472# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1473function gomod() {
1474 if [[ $# -ne 1 ]]; then
1475 echo "usage: gomod <module>" >&2
1476 return 1
1477 fi
1478
1479 local path="$(pathmod $@)"
1480 if [ -z "$path" ]; then
1481 return 1
1482 fi
1483 cd $path
1484}
1485
Cole Faust24c36db2021-01-23 02:39:37 +00001486# Gets the list of a module's installed outputs, as cached in module-info.json.
1487# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1488function outmod() {
1489 if [[ $# -ne 1 ]]; then
1490 echo "usage: outmod <module>" >&2
1491 return 1
1492 fi
1493
1494 verifymodinfo || return 1
1495
1496 local relpath
1497 relpath=$(python -c "import json, os
1498module = '$1'
1499module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1500if module not in module_info:
1501 exit(1)
1502for output in module_info[module]['installed']:
1503 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1504
1505 if [ $? -ne 0 ]; then
1506 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1507 return 1
1508 elif [ ! -z "$relpath" ]; then
1509 echo "$relpath"
1510 fi
1511}
1512
1513# adb install a module's apk, as cached in module-info.json. If any build change
1514# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1515# Usage: installmod [adb install arguments] <module>
1516# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1517function installmod() {
1518 if [[ $# -eq 0 ]]; then
1519 echo "usage: installmod [adb install arguments] <module>" >&2
1520 return 1
1521 fi
1522
1523 local _path
1524 _path=$(outmod ${@:$#:1})
1525 if [ $? -ne 0 ]; then
1526 return 1
1527 fi
1528
1529 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1530 if [ -z "$_path" ]; then
1531 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1532 return 1
1533 fi
1534 local length=$(( $# - 1 ))
1535 echo adb install ${@:1:$length} $_path
1536 adb install ${@:1:$length} $_path
1537}
1538
dimitry73b84812018-12-11 18:06:00 +01001539function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001540 local word=${COMP_WORDS[COMP_CWORD]}
1541 COMPREPLY=( $(allmod | grep -E "^$word") )
1542}
1543
Alex Rayf0d08eb2013-03-08 15:15:06 -08001544# Print colored exit condition
1545function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001546 "$@"
1547 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001548 if [ $retval -ne 0 ]
1549 then
Jacky Cao89483b82015-05-15 22:12:53 +08001550 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001551 else
Jacky Cao89483b82015-05-15 22:12:53 +08001552 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001553 fi
1554 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001555}
1556
Matt Alexanderd9c56562020-05-21 10:49:17 +00001557function get_make_command()
1558{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001559 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1560 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001561 # Always use the real make if -C is passed in
1562 for arg in "$@"; do
1563 if [[ $arg == -C* ]]; then
1564 echo command make
1565 return
1566 fi
1567 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001568 echo build/soong/soong_ui.bash --make-mode
1569 else
1570 echo command make
1571 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001572}
1573
Matt Alexanderd9c56562020-05-21 10:49:17 +00001574function _wrap_build()
1575{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001576 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1577 "$@"
1578 return $?
1579 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001580 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001581 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001582 local ret=$?
1583 local end_time=$(date +"%s")
1584 local tdiff=$(($end_time-$start_time))
1585 local hours=$(($tdiff / 3600 ))
1586 local mins=$((($tdiff % 3600) / 60))
1587 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001588 local ncolors=$(tput colors 2>/dev/null)
1589 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001590 color_failed=$'\E'"[0;31m"
1591 color_success=$'\E'"[0;32m"
1592 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001593 else
1594 color_failed=""
1595 color_success=""
1596 color_reset=""
1597 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001598 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001599 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001600 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001601 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001602 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001603 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001604 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001605 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001606 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001607 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001608 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001609 printf "(%s seconds)" $secs
1610 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001611 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001612 echo
1613 return $ret
1614}
1615
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001616function _trigger_build()
1617(
1618 local -r bc="$1"; shift
1619 if T="$(gettop)"; then
1620 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1621 else
1622 echo "Couldn't locate the top of the tree. Try setting TOP."
1623 fi
1624)
1625
1626function m()
1627(
1628 _trigger_build "all-modules" "$@"
1629)
1630
1631function mm()
1632(
1633 _trigger_build "modules-in-a-dir-no-deps" "$@"
1634)
1635
1636function mmm()
1637(
1638 _trigger_build "modules-in-dirs-no-deps" "$@"
1639)
1640
1641function mma()
1642(
1643 _trigger_build "modules-in-a-dir" "$@"
1644)
1645
1646function mmma()
1647(
1648 _trigger_build "modules-in-dirs" "$@"
1649)
1650
Matt Alexanderd9c56562020-05-21 10:49:17 +00001651function make()
1652{
Dan Willemsene9842242017-07-28 13:00:13 -07001653 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001654}
1655
Matt Alexanderd9c56562020-05-21 10:49:17 +00001656function provision()
1657{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001658 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1659 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1660 return 1
1661 fi
1662 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1663 echo "There is no provisioning script for the device." >&2
1664 return 1
1665 fi
1666
1667 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001668 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001669 shift 1
1670 else
1671 echo "This action will reflash your device."
1672 echo ""
1673 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1674 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001675 echo -n "Are you sure you want to do this (yes/no)? "
1676 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001677 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001678 echo "Not taking any action. Exiting." >&2
1679 return 1
1680 fi
1681 fi
1682 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1683}
1684
Jim Tanga881a252018-06-19 16:34:41 +08001685# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001686function enable_zsh_completion() {
1687 # Don't override user's options if bash-style completion is already enabled.
1688 if ! declare -f complete >/dev/null; then
1689 autoload -U compinit && compinit
1690 autoload -U bashcompinit && bashcompinit
1691 fi
Jim Tanga881a252018-06-19 16:34:41 +08001692}
1693
1694function validate_current_shell() {
1695 local current_sh="$(ps -o command -p $$)"
1696 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001697 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001698 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001699 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001700 *zsh*)
1701 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001702 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001703 *)
Jim Tanga881a252018-06-19 16:34:41 +08001704 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 -07001705 ;;
1706 esac
Jim Tanga881a252018-06-19 16:34:41 +08001707}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001708
1709# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001710# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1711# load those.
1712#
1713# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001714function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001715 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001716 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001717 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001718 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 -08001719 if [ -n "$allowed" ]; then
1720 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1721 echo " $allowed"
1722 echo " $f"
1723 return
1724 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001725 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001726 done
1727
1728 allowed_files=
1729 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001730 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001731 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001732 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001733
1734 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001735 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001736 else
1737 echo "ignoring $f, not in $allowed"
1738 fi
Jim Tanga881a252018-06-19 16:34:41 +08001739 done
1740 done
1741}
Kenny Root52aa81c2011-07-15 11:07:06 -07001742
Dan Albertbab814f2020-08-26 15:34:53 -07001743function showcommands() {
1744 local T=$(gettop)
1745 if [[ -z "$TARGET_PRODUCT" ]]; then
1746 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1747 return
1748 fi
1749 case $(uname -s) in
1750 Darwin)
1751 PREBUILT_NAME=darwin-x86
1752 ;;
1753 Linux)
1754 PREBUILT_NAME=linux-x86
1755 ;;
1756 *)
1757 >&2 echo Unknown host $(uname -s)
1758 return
1759 ;;
1760 esac
1761 if [[ -z "$OUT_DIR" ]]; then
1762 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1763 OUT_DIR=out
1764 else
1765 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1766 fi
1767 fi
1768 if [[ "$1" == "--regenerate" ]]; then
1769 shift 1
1770 NINJA_ARGS="-t commands $@" m
1771 else
1772 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1773 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1774 -t commands "$@")
1775 fi
1776}
1777
Jim Tanga881a252018-06-19 16:34:41 +08001778validate_current_shell
1779source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001780addcompletions