blob: 82c4565620004cb9850186bdb2b04d21e2e6af28 [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Anton Hanssonece9c482019-02-04 18:15:39 +000012- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080013- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070014- mm: Builds and installs all of the modules in the current directory, and their
15 dependencies.
16- mmm: Builds and installs all of the modules in the supplied directories, and their
17 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080018 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070019- mma: Same as 'mm'
20- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080021- provision: Flash device with all required partitions. Options will be passed on to fastboot.
22- cgrep: Greps on all local C/C++ files.
23- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010024- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080025- jgrep: Greps on all local Java files.
26- resgrep: Greps on all local res/*.xml files.
27- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070028- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060029- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- sepgrep: Greps on all local sepolicy files.
31- sgrep: Greps on all local source files.
32- godir: Go to the directory containing a file.
33- allmod: List all modules.
34- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080035- pathmod: Get the directory containing a module.
Steven Morelandab25c7a2020-04-09 12:05:34 -070036- refreshmod: Refresh list of modules for allmod/gomod/pathmod.
Steven Moreland74114f12020-09-10 01:23:32 +000037- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070038
Roland Levillain39341922015-10-20 12:48:19 +010039Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070040- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080041- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070042
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070043Look at the source to view more functions. The complete list is:
44EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070045 local T=$(gettop)
46 local A=""
47 local i
Jacky Cao89483b82015-05-15 22:12:53 +080048 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070049 A="$A $i"
50 done
51 echo $A
52}
53
Ying Wang08800fd2016-03-03 20:57:21 -080054# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000055function build_build_var_cache()
56{
Christopher Ferris55257d22017-03-23 11:08:58 -070057 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080058 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080059 cached_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
60 cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
Ying Wang08800fd2016-03-03 20:57:21 -080061 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080062 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080063 --vars="${cached_vars[*]}" \
64 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070065 --var-prefix=var_cache_ \
66 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080067 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000068 if [ $ret -ne 0 ]
69 then
Ying Wang08800fd2016-03-03 20:57:21 -080070 unset build_dicts_script
71 return $ret
72 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070073 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080074 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080075 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080076 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000077 if [ $ret -ne 0 ]
78 then
Ying Wang08800fd2016-03-03 20:57:21 -080079 return $ret
80 fi
81 BUILD_VAR_CACHE_READY="true"
82}
83
Ying Wangf0cb3972016-03-04 13:56:23 -080084# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080085# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000086function destroy_build_var_cache()
87{
Ying Wang08800fd2016-03-03 20:57:21 -080088 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070089 local v
Ying Wang08800fd2016-03-03 20:57:21 -080090 for v in $cached_vars; do
91 unset var_cache_$v
92 done
93 unset cached_vars
94 for v in $cached_abs_vars; do
95 unset abs_var_cache_$v
96 done
97 unset cached_abs_vars
98}
99
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700100# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000101function get_abs_build_var()
102{
103 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
104 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800105 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000106 return
Ying Wang08800fd2016-03-03 20:57:21 -0800107 fi
108
Christopher Ferris55257d22017-03-23 11:08:58 -0700109 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700110 if [ ! "$T" ]; then
111 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
112 return
113 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700114 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115}
116
117# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000118function get_build_var()
119{
120 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
121 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800122 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100123 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800124 fi
125
Christopher Ferris55257d22017-03-23 11:08:58 -0700126 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700127 if [ ! "$T" ]; then
128 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100129 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700130 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700131 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800132}
133
134# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000135function check_product()
136{
Christopher Ferris55257d22017-03-23 11:08:58 -0700137 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800138 if [ ! "$T" ]; then
139 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
140 return
141 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700142 TARGET_PRODUCT=$1 \
143 TARGET_BUILD_VARIANT= \
144 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700145 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 get_build_var TARGET_DEVICE > /dev/null
147 # hide successful answers, but allow the errors to show
148}
149
150VARIANT_CHOICES=(user userdebug eng)
151
152# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000153function check_variant()
154{
Christopher Ferris55257d22017-03-23 11:08:58 -0700155 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800156 for v in ${VARIANT_CHOICES[@]}
157 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000158 if [ "$v" = "$1" ]
159 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800160 return 0
161 fi
162 done
163 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700164}
165
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166function setpaths()
167{
Christopher Ferris55257d22017-03-23 11:08:58 -0700168 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700169 if [ ! "$T" ]; then
170 echo "Couldn't locate the top of the tree. Try setting TOP."
171 return
172 fi
173
174 ##################################################################
175 # #
176 # Read me before you modify this code #
177 # #
178 # This function sets ANDROID_BUILD_PATHS to what it is adding #
179 # to PATH, and the next time it is run, it removes that from #
180 # PATH. This is required so lunch can be run more than once #
181 # and still have working paths. #
182 # #
183 ##################################################################
184
Raphael Mollc639c782011-06-20 17:25:01 -0700185 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
186 # due to "C:\Program Files" being in the path.
187
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700188 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000189 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700190 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
191 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000192 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700193 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800194 # strip leading ':', if any
195 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500196 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700197
198 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700199 local prebuiltdir=$(getprebuilt)
200 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700201
Ben Cheng8bc4c432012-11-16 13:29:13 -0800202 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700203 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
204 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800205 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800206
Raphael Mollc639c782011-06-20 17:25:01 -0700207 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800208 export ANDROID_TOOLCHAIN=
209 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200210 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700211 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200212 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000213 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700214 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000215 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400216 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000217 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200218 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000219 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
220 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700221 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200222 *)
223 echo "Can't find toolchain for unknown architecture: $ARCH"
224 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700225 ;;
226 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700227 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800228 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700229 fi
Raphael732936d2011-06-22 14:35:32 -0700230
Christopher Ferris55257d22017-03-23 11:08:58 -0700231 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800232 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
233 fi
234
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700235 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700236
237 # add kernel specific binaries
238 case $(uname -s) in
239 Linux)
240 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
241 ;;
242 *)
243 ;;
244 esac
245
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400246 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
247 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
248 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
249 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700250 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
251
252 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
253 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
254 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200255
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800256 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
257 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
258
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200259 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
260 # to ensure that the corresponding 'emulator' binaries are used.
261 case $(uname -s) in
262 Darwin)
263 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
264 ;;
265 Linux)
266 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
267 ;;
268 *)
269 ANDROID_EMULATOR_PREBUILTS=
270 ;;
271 esac
272 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700273 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200274 export ANDROID_EMULATOR_PREBUILTS
275 fi
276
Jim Tangb3fda302018-12-22 10:24:55 +0800277 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
278 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700279 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
280 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
281 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
282 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800283
Ryan Prichard8426a192019-07-15 18:05:29 -0700284 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800285
286 # out with the duplicate old
287 if [ -n $ANDROID_PYTHONPATH ]; then
288 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
289 fi
290 # and in with the new
291 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800292 if [ -n $VENDOR_PYTHONPATH ]; then
293 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
294 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800295 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800296
Colin Crosse97e6932017-06-30 16:01:45 -0700297 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
298 export JAVA_HOME=$ANDROID_JAVA_HOME
299 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
300 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
301 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500302
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800303 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700304 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
305 export OUT=$ANDROID_PRODUCT_OUT
306
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700307 unset ANDROID_HOST_OUT
308 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
309
Simran Basidd050ed2017-02-13 13:46:48 -0800310 unset ANDROID_HOST_OUT_TESTCASES
311 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
312
313 unset ANDROID_TARGET_OUT_TESTCASES
314 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
315
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700317 # TODO: fix the path
318 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
319}
320
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000321function abazel()
322{
323 local T="$(gettop)"
324 if [ ! "$T" ]; then
325 echo "Couldn't locate the top of the tree. Try setting TOP."
326 return
327 fi
328
329 case $(uname -s) in
330 Darwin)
331 ANDROID_BAZEL_PATH="${T}/prebuilts/bazel/darwin-x86_64/bazel"
332 ANDROID_BAZELRC_PATH="${T}/build/bazel/darwin.bazelrc"
333 ANDROID_BAZEL_JDK_PATH="${T}/prebuilts/jdk/jdk11/darwin-x86"
334 ;;
335 Linux)
336 ANDROID_BAZEL_PATH="${T}/prebuilts/bazel/linux-x86_64/bazel"
337 ANDROID_BAZELRC_PATH="${T}/build/bazel/linux.bazelrc"
338 ANDROID_BAZEL_JDK_PATH="${T}/prebuilts/jdk/jdk11/linux-x86"
339 ;;
340 *)
341 ANDROID_BAZEL_PATH=
342 ANDROID_BAZELRC_PATH=
343 ANDROID_BAZEL_JDK_PATH=
344 ;;
345 esac
346
347 if [ -n "$ANDROID_BAZEL_PATH" -a -f "$ANDROID_BAZEL_PATH" ]; then
348 export ANDROID_BAZEL_PATH
349 else
350 echo "Couldn't locate Bazel binary"
351 return
352 fi
353
354 if [ -n "$ANDROID_BAZELRC_PATH" -a -f "$ANDROID_BAZELRC_PATH" ]; then
355 export ANDROID_BAZELRC_PATH
356 else
357 echo "Couldn't locate bazelrc file for Bazel"
358 return
359 fi
360
361 if [ -n "$ANDROID_BAZEL_JDK_PATH" -a -d "$ANDROID_BAZEL_JDK_PATH" ]; then
362 export ANDROID_BAZEL_JDK_PATH
363 else
364 echo "Couldn't locate JDK to use for Bazel"
365 return
366 fi
367
368 echo "WARNING: Bazel support for the Android Platform is experimental and is undergoing development."
369 echo "WARNING: Currently, build stability is not guaranteed. Thank you."
370 echo
371 "${ANDROID_BAZEL_PATH}" --server_javabase="${ANDROID_BAZEL_JDK_PATH}" --bazelrc="${ANDROID_BAZELRC_PATH}" "$@"
372}
373
Matt Alexanderd9c56562020-05-21 10:49:17 +0000374function printconfig()
375{
Christopher Ferris55257d22017-03-23 11:08:58 -0700376 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800377 if [ ! "$T" ]; then
378 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
379 return
380 fi
381 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700382}
383
Matt Alexanderd9c56562020-05-21 10:49:17 +0000384function set_stuff_for_environment()
385{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800386 setpaths
387 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700388
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700390 # With this environment variable new GCC can apply colors to warnings/errors
391 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 -0700392}
393
Matt Alexanderd9c56562020-05-21 10:49:17 +0000394function set_sequence_number()
395{
Colin Cross88737132017-03-21 17:41:03 -0700396 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700397}
398
Makoto Onukida971062018-06-18 10:15:19 -0700399# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
400function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800401 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700402 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800403 *:"$cmd":*)
404 return 1
405 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700406 esac
407 return 0
408}
409
Matt Alexanderd9c56562020-05-21 10:49:17 +0000410function addcompletions()
411{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700412 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700413
Jim Tanga881a252018-06-19 16:34:41 +0800414 # Keep us from trying to run in something that's neither bash nor zsh.
415 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700416 return
417 fi
418
419 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800420 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700421 return
422 fi
423
Jim Tanga881a252018-06-19 16:34:41 +0800424 local completion_files=(
425 system/core/adb/adb.bash
426 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800427 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800428 )
Makoto Onukida971062018-06-18 10:15:19 -0700429 # Completion can be disabled selectively to allow users to use non-standard completion.
430 # e.g.
431 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
432 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800433 for f in ${completion_files[*]}; do
434 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700435 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700436 fi
437 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700438
Matt Alexanderd9c56562020-05-21 10:49:17 +0000439 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700440 complete -C "bit --tab" bit
441 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000442 if [ -z "$ZSH_VERSION" ]; then
443 # Doesn't work in zsh.
444 complete -o nospace -F _croot croot
445 fi
Jim Tanga881a252018-06-19 16:34:41 +0800446 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800447
dimitry73b84812018-12-11 18:06:00 +0100448 complete -F _complete_android_module_names gomod
449 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700450}
451
Matt Alexanderd9c56562020-05-21 10:49:17 +0000452function choosetype()
453{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700454 echo "Build type choices are:"
455 echo " 1. release"
456 echo " 2. debug"
457 echo
458
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800459 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700460 DEFAULT_NUM=1
461 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463 export TARGET_BUILD_TYPE=
464 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 while [ -z $TARGET_BUILD_TYPE ]
466 do
467 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000468 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469 read ANSWER
470 else
471 echo $1
472 ANSWER=$1
473 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700474 case $ANSWER in
475 "")
476 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
477 ;;
478 1)
479 export TARGET_BUILD_TYPE=release
480 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800481 release)
482 export TARGET_BUILD_TYPE=release
483 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 2)
485 export TARGET_BUILD_TYPE=debug
486 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800487 debug)
488 export TARGET_BUILD_TYPE=debug
489 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 *)
491 echo
492 echo "I didn't understand your response. Please try again."
493 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494 ;;
495 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000496 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800497 break
498 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700499 done
500
Ying Wang08800fd2016-03-03 20:57:21 -0800501 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700502 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800503 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700504}
505
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800506#
507# This function isn't really right: It chooses a TARGET_PRODUCT
508# based on the list of boards. Usually, that gets you something
509# that kinda works with a generic product, but really, you should
510# pick a product by name.
511#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000512function chooseproduct()
513{
Christopher Ferris55257d22017-03-23 11:08:58 -0700514 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000515 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700516 default_value=$TARGET_PRODUCT
517 else
Ying Wang0a76df52015-06-08 11:57:26 -0700518 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700519 fi
520
Ying Wang08800fd2016-03-03 20:57:21 -0800521 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800522 export TARGET_PRODUCT=
523 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700524 while [ -z "$TARGET_PRODUCT" ]
525 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700526 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000527 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528 read ANSWER
529 else
530 echo $1
531 ANSWER=$1
532 fi
533
Matt Alexanderd9c56562020-05-21 10:49:17 +0000534 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700535 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800536 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000537 if check_product $ANSWER
538 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800539 export TARGET_PRODUCT=$ANSWER
540 else
541 echo "** Not a valid product: $ANSWER"
542 fi
543 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000544 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800545 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546 fi
547 done
548
Ying Wang08800fd2016-03-03 20:57:21 -0800549 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700550 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800551 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552}
553
Matt Alexanderd9c56562020-05-21 10:49:17 +0000554function choosevariant()
555{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556 echo "Variant choices are:"
557 local index=1
558 local v
559 for v in ${VARIANT_CHOICES[@]}
560 do
561 # The product name is the name of the directory containing
562 # the makefile we found, above.
563 echo " $index. $v"
564 index=$(($index+1))
565 done
566
567 local default_value=eng
568 local ANSWER
569
570 export TARGET_BUILD_VARIANT=
571 while [ -z "$TARGET_BUILD_VARIANT" ]
572 do
573 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000574 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575 read ANSWER
576 else
577 echo $1
578 ANSWER=$1
579 fi
580
Matt Alexanderd9c56562020-05-21 10:49:17 +0000581 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000583 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
584 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200585 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 fi
587 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000588 if check_variant $ANSWER
589 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800590 export TARGET_BUILD_VARIANT=$ANSWER
591 else
592 echo "** Not a valid variant: $ANSWER"
593 fi
594 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000595 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 break
597 fi
598 done
599}
600
Matt Alexanderd9c56562020-05-21 10:49:17 +0000601function choosecombo()
602{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700603 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700604
605 echo
606 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700607 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608
609 echo
610 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700611 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800612
613 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800614 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700615 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800617 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700618}
619
Matt Alexanderd9c56562020-05-21 10:49:17 +0000620function add_lunch_combo()
621{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800622 if [ -n "$ZSH_VERSION" ]; then
623 echo -n "${funcfiletrace[1]}: "
624 else
625 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
626 fi
627 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 -0800628}
629
Matt Alexanderd9c56562020-05-21 10:49:17 +0000630function print_lunch_menu()
631{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700632 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100633 local choices
634 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
635 local ret=$?
636
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700637 echo
638 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700639 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100640
Matt Alexanderd9c56562020-05-21 10:49:17 +0000641 if [ $ret -ne 0 ]
642 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100643 echo "Warning: Cannot display lunch menu."
644 echo
645 echo "Note: You can invoke lunch with an explicit target:"
646 echo
647 echo " usage: lunch [target]" >&2
648 echo
649 return
650 fi
651
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700652 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800653
654 local i=1
655 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700656 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800657 do
658 echo " $i. $choice"
659 i=$(($i+1))
660 done
661
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662 echo
663}
664
Matt Alexanderd9c56562020-05-21 10:49:17 +0000665function lunch()
666{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800667 local answer
668
Steven Moreland92793dc2020-02-25 18:30:18 -0800669 if [[ $# -gt 1 ]]; then
670 echo "usage: lunch [target]" >&2
671 return 1
672 fi
673
674 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800675 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700676 else
677 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700678 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800679 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700680 fi
681
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800682 local selection=
683
Matt Alexanderd9c56562020-05-21 10:49:17 +0000684 if [ -z "$answer" ]
685 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700686 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000687 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
688 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800689 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000690 if [ $answer -le ${#choices[@]} ]
691 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800692 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693 if [ -n "$ZSH_VERSION" ]
694 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800695 selection=${choices[$(($answer))]}
696 else
697 selection=${choices[$(($answer-1))]}
698 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800699 fi
Colin Cross88737132017-03-21 17:41:03 -0700700 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800701 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700702 fi
703
Joe Onoratoda12daf2010-06-09 18:18:31 -0700704 export TARGET_BUILD_APPS=
705
Colin Cross88737132017-03-21 17:41:03 -0700706 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700707 product=${selection%%-*} # Trim everything after first dash
708 variant_and_version=${selection#*-} # Trim everything up to first dash
709 if [ "$variant_and_version" != "$selection" ]; then
710 variant=${variant_and_version%%-*}
711 if [ "$variant" != "$variant_and_version" ]; then
712 version=${variant_and_version#*-}
713 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700714 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800715
Matt Alexanderd9c56562020-05-21 10:49:17 +0000716 if [ -z "$product" ]
717 then
Ying Wang08800fd2016-03-03 20:57:21 -0800718 echo
Colin Cross88737132017-03-21 17:41:03 -0700719 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700720 return 1
721 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800722
Colin Cross88737132017-03-21 17:41:03 -0700723 TARGET_PRODUCT=$product \
724 TARGET_BUILD_VARIANT=$variant \
725 TARGET_PLATFORM_VERSION=$version \
726 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000727 if [ $? -ne 0 ]
728 then
Colin Cross88737132017-03-21 17:41:03 -0700729 return 1
730 fi
Colin Cross88737132017-03-21 17:41:03 -0700731 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
732 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700733 if [ -n "$version" ]; then
734 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
735 else
736 unset TARGET_PLATFORM_VERSION
737 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700738 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700739
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700740 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800741
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700742 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700743 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800744 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700745}
746
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700747unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700748# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000749function _lunch()
750{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700751 local cur prev opts
752 COMPREPLY=()
753 cur="${COMP_WORDS[COMP_CWORD]}"
754 prev="${COMP_WORDS[COMP_CWORD-1]}"
755
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700756 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800757 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700758 fi
759
760 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700761 return 0
762}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700763
Joe Onoratoda12daf2010-06-09 18:18:31 -0700764# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700765# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000766function tapas()
767{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700768 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800769 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700770 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700771 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 -0800772 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 -0700773
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700774 if [ "$showHelp" != "" ]; then
775 $(gettop)/build/make/tapasHelp.sh
776 return
777 fi
778
Ying Wang67f02922012-08-22 10:25:20 -0700779 if [ $(echo $arch | wc -w) -gt 1 ]; then
780 echo "tapas: Error: Multiple build archs supplied: $arch"
781 return
782 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700783 if [ $(echo $variant | wc -w) -gt 1 ]; then
784 echo "tapas: Error: Multiple build variants supplied: $variant"
785 return
786 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700787 if [ $(echo $density | wc -w) -gt 1 ]; then
788 echo "tapas: Error: Multiple densities supplied: $density"
789 return
790 fi
Ying Wang67f02922012-08-22 10:25:20 -0700791
Ying Wang0a76df52015-06-08 11:57:26 -0700792 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700793 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000794 x86) product=aosp_x86;;
795 arm64) product=aosp_arm64;;
796 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700797 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700798 if [ -z "$variant" ]; then
799 variant=eng
800 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700801 if [ -z "$apps" ]; then
802 apps=all
803 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600804 if [ -z "$density" ]; then
805 density=alldpi
806 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700807
Ying Wang67f02922012-08-22 10:25:20 -0700808 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700809 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700810 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700811 export TARGET_BUILD_TYPE=release
812 export TARGET_BUILD_APPS=$apps
813
Ying Wang08800fd2016-03-03 20:57:21 -0800814 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700815 set_stuff_for_environment
816 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800817 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700818}
819
Matt Alexanderd9c56562020-05-21 10:49:17 +0000820function gettop
821{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700822 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000823 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700824 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000825 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700826 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000827 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800828 # The following circumlocution (repeated below as well) ensures
829 # that we record the true directory name and not one that is
830 # faked up with symlink names.
831 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700832 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800833 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700834 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000835 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800836 \cd ..
synergyb112a402013-07-05 19:47:47 -0700837 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700838 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000839 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700840 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000841 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700842 fi
843 fi
844 fi
845}
846
Matt Alexanderd9c56562020-05-21 10:49:17 +0000847function croot()
848{
Christopher Ferris55257d22017-03-23 11:08:58 -0700849 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700850 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700851 if [ "$1" ]; then
852 \cd $(gettop)/$1
853 else
854 \cd $(gettop)
855 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700856 else
857 echo "Couldn't locate the top of the tree. Try setting TOP."
858 fi
859}
860
Matt Alexanderd9c56562020-05-21 10:49:17 +0000861function _croot()
862{
Anton Hanssonece9c482019-02-04 18:15:39 +0000863 local T=$(gettop)
864 if [ "$T" ]; then
865 local cur="${COMP_WORDS[COMP_CWORD]}"
866 k=0
867 for c in $(compgen -d ${T}/${cur}); do
868 COMPREPLY[k++]=${c#${T}/}/
869 done
870 fi
871}
872
Matt Alexanderd9c56562020-05-21 10:49:17 +0000873function cproj()
874{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700875 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700876 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700877 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700878 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
879 T=$PWD
880 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800881 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700882 return
883 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800884 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700885 done
Ying Wang9cd17642012-12-13 10:52:07 -0800886 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700887 echo "can't find Android.mk"
888}
889
Daniel Sandler47e0a882013-07-30 13:23:52 -0400890# simplified version of ps; output in the form
891# <pid> <procname>
892function qpid() {
893 local prepend=''
894 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000895 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400896 prepend=' '
897 append='$'
898 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000899 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800900 echo "usage: qpid [[--exact] <process name|pid>"
901 return 255
902 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400903
904 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000905 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800906 qpid | \grep "$prepend$EXE$append"
907 else
908 adb shell ps \
909 | tr -d '\r' \
910 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
911 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400912}
913
Steven Moreland74114f12020-09-10 01:23:32 +0000914# syswrite - disable verity, reboot if needed, and remount image
915#
916# Easy way to make system.img/etc writable
917function syswrite() {
918 adb wait-for-device && adb root || return 1
919 if [[ $(adb disable-verity | grep "reboot") ]]; then
920 echo "rebooting"
921 adb reboot && adb wait-for-device && adb root || return 1
922 fi
923 adb wait-for-device && adb remount || return 1
924}
925
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800926# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700927# that has the core-file-size limit set correctly
928#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800929# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700930# if its core-file-size limit is not set already.
931# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
932
Matt Alexanderd9c56562020-05-21 10:49:17 +0000933function coredump_setup()
934{
935 echo "Getting root...";
936 adb root;
937 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700938
Matt Alexanderd9c56562020-05-21 10:49:17 +0000939 echo "Remounting root partition read-write...";
940 adb shell mount -w -o remount -t rootfs rootfs;
941 sleep 1;
942 adb wait-for-device;
943 adb shell mkdir -p /cores;
944 adb shell mount -t tmpfs tmpfs /cores;
945 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700946
Matt Alexanderd9c56562020-05-21 10:49:17 +0000947 echo "Granting SELinux permission to dump in /cores...";
948 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700949
Matt Alexanderd9c56562020-05-21 10:49:17 +0000950 echo "Set core pattern.";
951 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700952
Ying Wang08800fd2016-03-03 20:57:21 -0800953 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700954}
955
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800956# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000957# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700958#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800959# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700960# dump to actually be generated.
961
Matt Alexanderd9c56562020-05-21 10:49:17 +0000962function coredump_enable()
963{
964 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800965 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000966 printf "Expecting a PID!\n";
967 return;
968 fi;
969 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800970 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700971}
972
973# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000974# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700975#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800976# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700977# enabled globally.
978
Matt Alexanderd9c56562020-05-21 10:49:17 +0000979function core()
980{
981 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700982
Ying Wang08800fd2016-03-03 20:57:21 -0800983 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000984 printf "Expecting a PID!\n";
985 return;
986 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700987
Matt Alexanderd9c56562020-05-21 10:49:17 +0000988 local CORENAME=core.$PID;
989 local COREPATH=/cores/$CORENAME;
990 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700991
Matt Alexanderd9c56562020-05-21 10:49:17 +0000992 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700993
Matt Alexanderd9c56562020-05-21 10:49:17 +0000994 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800995 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996 printf "\tSending SIG%s to %d...\n" $SIG $PID;
997 adb shell kill -$SIG $PID;
998 sleep 1;
999 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001000
Matt Alexanderd9c56562020-05-21 10:49:17 +00001001 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1002 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001003}
1004
Christopher Tate744ee802009-11-12 15:33:08 -08001005# systemstack - dump the current stack trace of all threads in the system process
1006# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001007function systemstack()
1008{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001009 stacks system_server
1010}
1011
Michael Wrightaeed7212014-06-19 19:58:12 -07001012# Read the ELF header from /proc/$PID/exe to determine if the process is
1013# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001014function is64bit()
1015{
Ben Chengfba67bf2014-02-25 10:27:07 -08001016 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001017 if [ "$PID" ] ; then
1018 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001019 echo "64"
1020 else
1021 echo ""
1022 fi
1023 else
1024 echo ""
1025 fi
1026}
1027
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001028case `uname -s` in
1029 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001030 function sgrep()
1031 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001032 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 -04001033 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001034 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001036 ;;
1037 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001038 function sgrep()
1039 {
Christopher Tatee2fe9592020-03-05 11:34:16 -08001040 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 -04001041 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001042 }
1043 ;;
1044esac
1045
Matt Alexanderd9c56562020-05-21 10:49:17 +00001046function gettargetarch
1047{
Raghu Gandham8da43102012-07-25 19:57:22 -07001048 get_build_var TARGET_ARCH
1049}
1050
Matt Alexanderd9c56562020-05-21 10:49:17 +00001051function ggrep()
1052{
Mike Frysinger5e479732015-09-22 18:13:48 -04001053 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1054 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001055}
1056
Matt Alexanderd9c56562020-05-21 10:49:17 +00001057function gogrep()
1058{
Orion Hodson831472d2019-10-25 11:35:15 +01001059 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1060 -exec grep --color -n "$@" {} +
1061}
1062
Matt Alexanderd9c56562020-05-21 10:49:17 +00001063function jgrep()
1064{
Mike Frysinger5e479732015-09-22 18:13:48 -04001065 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1066 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001067}
1068
Matt Alexanderd9c56562020-05-21 10:49:17 +00001069function cgrep()
1070{
Mike Frysinger5e479732015-09-22 18:13:48 -04001071 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' \) \
1072 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001073}
1074
Matt Alexanderd9c56562020-05-21 10:49:17 +00001075function resgrep()
1076{
Christopher Ferris55257d22017-03-23 11:08:58 -07001077 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001078 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1079 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1080 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001081}
1082
Matt Alexanderd9c56562020-05-21 10:49:17 +00001083function mangrep()
1084{
Mike Frysinger5e479732015-09-22 18:13:48 -04001085 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1086 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001087}
1088
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089function owngrep()
1090{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001091 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1092 -exec grep --color -n "$@" {} +
1093}
1094
Matt Alexanderd9c56562020-05-21 10:49:17 +00001095function sepgrep()
1096{
Mike Frysinger5e479732015-09-22 18:13:48 -04001097 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1098 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001099}
1100
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101function rcgrep()
1102{
Mike Frysinger5e479732015-09-22 18:13:48 -04001103 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1104 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001105}
1106
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001107case `uname -s` in
1108 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001109 function mgrep()
1110 {
Orion Hodson831472d2019-10-25 11:35:15 +01001111 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 -04001112 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001113 }
1114
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115 function treegrep()
1116 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001117 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 -04001118 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119 }
1120
1121 ;;
1122 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123 function mgrep()
1124 {
Orion Hodson831472d2019-10-25 11:35:15 +01001125 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 -04001126 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001127 }
1128
Matt Alexanderd9c56562020-05-21 10:49:17 +00001129 function treegrep()
1130 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001131 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 -04001132 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001133 }
1134
1135 ;;
1136esac
1137
Matt Alexanderd9c56562020-05-21 10:49:17 +00001138function getprebuilt
1139{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001140 get_abs_build_var ANDROID_PREBUILTS
1141}
1142
Matt Alexanderd9c56562020-05-21 10:49:17 +00001143function tracedmdump()
1144{
Christopher Ferris55257d22017-03-23 11:08:58 -07001145 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001146 if [ ! "$T" ]; then
1147 echo "Couldn't locate the top of the tree. Try setting TOP."
1148 return
1149 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001150 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001151 local arch=$(gettargetarch)
1152 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001154 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001155 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001156 echo "usage: tracedmdump tracename"
1157 return
1158 fi
1159
Matt Alexanderd9c56562020-05-21 10:49:17 +00001160 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001161 echo "Error: cannot find kernel: '$KERNEL'"
1162 return
1163 fi
1164
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001165 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001166 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001167 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1168 fi
1169
1170 echo "post-processing traces..."
1171 rm -f $TRACE/qtrace.dexlist
1172 post_trace $TRACE
1173 if [ $? -ne 0 ]; then
1174 echo "***"
1175 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1176 echo "***"
1177 return
1178 fi
1179 echo "generating dexlist output..."
1180 /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
1181 echo "generating dmtrace data..."
1182 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1183 echo "generating html file..."
1184 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1185 echo "done, see $TRACE/dmtrace.html for details"
1186 echo "or run:"
1187 echo " traceview $TRACE/dmtrace"
1188}
1189
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001190# communicate with a running device or emulator, set up necessary state,
1191# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001192function runhat()
1193{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001194 # process standard adb options
1195 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001197 adbTarget=$1
1198 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001199 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001200 adbTarget="$1 $2"
1201 shift 2
1202 fi
1203 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001204 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001205
1206 # runhat options
1207 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208
Matt Alexanderd9c56562020-05-21 10:49:17 +00001209 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001210 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001211 return
1212 fi
1213
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001214 # confirm hat is available
1215 if [ -z $(which hat) ]; then
1216 echo "hat is not available in this configuration."
1217 return
1218 fi
1219
Andy McFaddenb6289852010-07-12 08:00:19 -07001220 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001221 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001222 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001223 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001224 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001225 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001226 echo -n "> "
1227 read
1228
The Android Open Source Project88b60792009-03-03 19:28:42 -08001229 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001230
The Android Open Source Project88b60792009-03-03 19:28:42 -08001231 echo "Retrieving file $devFile..."
1232 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001233
The Android Open Source Project88b60792009-03-03 19:28:42 -08001234 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001235
The Android Open Source Project88b60792009-03-03 19:28:42 -08001236 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001237 echo "View the output by pointing your browser at http://localhost:7000/"
1238 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001239 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001240}
1241
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242function getbugreports()
1243{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001244 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245
1246 if [ ! "$reports" ]; then
1247 echo "Could not locate any bugreports."
1248 return
1249 fi
1250
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001251 local report
1252 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001253 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001254 echo "/sdcard/bugreports/${report}"
1255 adb pull /sdcard/bugreports/${report} ${report}
1256 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001257 done
1258}
1259
Matt Alexanderd9c56562020-05-21 10:49:17 +00001260function getsdcardpath()
1261{
Victoria Lease1b296b42012-08-21 15:44:06 -07001262 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1263}
1264
Matt Alexanderd9c56562020-05-21 10:49:17 +00001265function getscreenshotpath()
1266{
Victoria Lease1b296b42012-08-21 15:44:06 -07001267 echo "$(getsdcardpath)/Pictures/Screenshots"
1268}
1269
Matt Alexanderd9c56562020-05-21 10:49:17 +00001270function getlastscreenshot()
1271{
Victoria Lease1b296b42012-08-21 15:44:06 -07001272 local screenshot_path=$(getscreenshotpath)
1273 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 +00001274 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001275 echo "No screenshots found."
1276 return
1277 fi
1278 echo "${screenshot}"
1279 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1280}
1281
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282function startviewserver()
1283{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001284 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001285 if [ $# -gt 0 ]; then
1286 port=$1
1287 fi
1288 adb shell service call window 1 i32 $port
1289}
1290
Matt Alexanderd9c56562020-05-21 10:49:17 +00001291function stopviewserver()
1292{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001293 adb shell service call window 2
1294}
1295
Matt Alexanderd9c56562020-05-21 10:49:17 +00001296function isviewserverstarted()
1297{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001298 adb shell service call window 3
1299}
1300
Matt Alexanderd9c56562020-05-21 10:49:17 +00001301function key_home()
1302{
Romain Guyb84049a2010-10-04 16:56:11 -07001303 adb shell input keyevent 3
1304}
1305
Matt Alexanderd9c56562020-05-21 10:49:17 +00001306function key_back()
1307{
Romain Guyb84049a2010-10-04 16:56:11 -07001308 adb shell input keyevent 4
1309}
1310
Matt Alexanderd9c56562020-05-21 10:49:17 +00001311function key_menu()
1312{
Romain Guyb84049a2010-10-04 16:56:11 -07001313 adb shell input keyevent 82
1314}
1315
Matt Alexanderd9c56562020-05-21 10:49:17 +00001316function smoketest()
1317{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001318 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1319 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1320 return
1321 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001322 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001323 if [ ! "$T" ]; then
1324 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1325 return
1326 fi
1327
Ying Wang9cd17642012-12-13 10:52:07 -08001328 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001329 adb uninstall com.android.smoketest > /dev/null &&
1330 adb uninstall com.android.smoketest.tests > /dev/null &&
1331 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1332 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1333 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1334}
1335
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001336# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001337function runtest()
1338{
Christopher Ferris55257d22017-03-23 11:08:58 -07001339 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001340 if [ ! "$T" ]; then
1341 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1342 return
1343 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001344 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001345}
1346
The Android Open Source Project88b60792009-03-03 19:28:42 -08001347function godir () {
1348 if [[ -z "$1" ]]; then
1349 echo "Usage: godir <regex>"
1350 return
1351 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001352 local T=$(gettop)
1353 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001354 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001355 mkdir -p $OUT_DIR
1356 FILELIST=$OUT_DIR/filelist
1357 else
1358 FILELIST=$T/filelist
1359 fi
1360 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001361 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001362 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001363 echo " Done"
1364 echo ""
1365 fi
1366 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001367 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001368 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001369 echo "Not found"
1370 return
1371 fi
1372 local pathname
1373 local choice
1374 if [[ ${#lines[@]} > 1 ]]; then
1375 while [[ -z "$pathname" ]]; do
1376 local index=1
1377 local line
1378 for line in ${lines[@]}; do
1379 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001380 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001381 done
1382 echo
1383 echo -n "Select one: "
1384 unset choice
1385 read choice
1386 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1387 echo "Invalid choice"
1388 continue
1389 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001390 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001391 done
1392 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001393 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001394 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001395 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001396}
1397
Steven Moreland62054a42018-12-06 10:11:40 -08001398# Update module-info.json in out.
1399function refreshmod() {
1400 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1401 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1402 return 1
1403 fi
1404
1405 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1406
1407 # for the output of the next command
1408 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1409
1410 # Note, can't use absolute path because of the way make works.
1411 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1412 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1413}
1414
1415# List all modules for the current device, as cached in module-info.json. If any build change is
1416# made and it should be reflected in the output, you should run 'refreshmod' first.
1417function allmod() {
1418 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1419 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1420 return 1
1421 fi
1422
1423 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1424 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1425 refreshmod || return 1
1426 fi
1427
LuK1337b6a78192020-01-12 03:12:17 +01001428 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 -08001429}
1430
Rett Berg78d1c932019-01-24 14:34:23 -08001431# 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 -08001432# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001433function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001434 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1435 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1436 return 1
1437 fi
1438
1439 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001440 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001441 return 1
1442 fi
1443
1444 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1445 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1446 refreshmod || return 1
1447 fi
1448
1449 local relpath=$(python -c "import json, os
1450module = '$1'
1451module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1452if module not in module_info:
1453 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001454print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001455
1456 if [ -z "$relpath" ]; then
1457 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1458 return 1
1459 else
Rett Berg78d1c932019-01-24 14:34:23 -08001460 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001461 fi
1462}
1463
Rett Berg78d1c932019-01-24 14:34:23 -08001464# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1465# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1466function gomod() {
1467 if [[ $# -ne 1 ]]; then
1468 echo "usage: gomod <module>" >&2
1469 return 1
1470 fi
1471
1472 local path="$(pathmod $@)"
1473 if [ -z "$path" ]; then
1474 return 1
1475 fi
1476 cd $path
1477}
1478
dimitry73b84812018-12-11 18:06:00 +01001479function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001480 local word=${COMP_WORDS[COMP_CWORD]}
1481 COMPREPLY=( $(allmod | grep -E "^$word") )
1482}
1483
Alex Rayf0d08eb2013-03-08 15:15:06 -08001484# Print colored exit condition
1485function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001486 "$@"
1487 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001488 if [ $retval -ne 0 ]
1489 then
Jacky Cao89483b82015-05-15 22:12:53 +08001490 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001491 else
Jacky Cao89483b82015-05-15 22:12:53 +08001492 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001493 fi
1494 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001495}
1496
Matt Alexanderd9c56562020-05-21 10:49:17 +00001497function get_make_command()
1498{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001499 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1500 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001501 # Always use the real make if -C is passed in
1502 for arg in "$@"; do
1503 if [[ $arg == -C* ]]; then
1504 echo command make
1505 return
1506 fi
1507 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001508 echo build/soong/soong_ui.bash --make-mode
1509 else
1510 echo command make
1511 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001512}
1513
Matt Alexanderd9c56562020-05-21 10:49:17 +00001514function _wrap_build()
1515{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001516 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1517 "$@"
1518 return $?
1519 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001520 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001521 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001522 local ret=$?
1523 local end_time=$(date +"%s")
1524 local tdiff=$(($end_time-$start_time))
1525 local hours=$(($tdiff / 3600 ))
1526 local mins=$((($tdiff % 3600) / 60))
1527 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001528 local ncolors=$(tput colors 2>/dev/null)
1529 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001530 color_failed=$'\E'"[0;31m"
1531 color_success=$'\E'"[0;32m"
1532 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001533 else
1534 color_failed=""
1535 color_success=""
1536 color_reset=""
1537 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001538 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001539 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001540 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001541 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001542 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001543 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001544 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001545 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001546 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001547 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001548 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001549 printf "(%s seconds)" $secs
1550 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001551 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001552 echo
1553 return $ret
1554}
1555
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001556function _trigger_build()
1557(
1558 local -r bc="$1"; shift
1559 if T="$(gettop)"; then
1560 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1561 else
1562 echo "Couldn't locate the top of the tree. Try setting TOP."
1563 fi
1564)
1565
1566function m()
1567(
1568 _trigger_build "all-modules" "$@"
1569)
1570
1571function mm()
1572(
1573 _trigger_build "modules-in-a-dir-no-deps" "$@"
1574)
1575
1576function mmm()
1577(
1578 _trigger_build "modules-in-dirs-no-deps" "$@"
1579)
1580
1581function mma()
1582(
1583 _trigger_build "modules-in-a-dir" "$@"
1584)
1585
1586function mmma()
1587(
1588 _trigger_build "modules-in-dirs" "$@"
1589)
1590
Matt Alexanderd9c56562020-05-21 10:49:17 +00001591function make()
1592{
Dan Willemsene9842242017-07-28 13:00:13 -07001593 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001594}
1595
Matt Alexanderd9c56562020-05-21 10:49:17 +00001596function provision()
1597{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001598 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1599 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1600 return 1
1601 fi
1602 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1603 echo "There is no provisioning script for the device." >&2
1604 return 1
1605 fi
1606
1607 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001608 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001609 shift 1
1610 else
1611 echo "This action will reflash your device."
1612 echo ""
1613 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1614 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001615 echo -n "Are you sure you want to do this (yes/no)? "
1616 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001617 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001618 echo "Not taking any action. Exiting." >&2
1619 return 1
1620 fi
1621 fi
1622 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1623}
1624
Jim Tanga881a252018-06-19 16:34:41 +08001625# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001626function enable_zsh_completion() {
1627 # Don't override user's options if bash-style completion is already enabled.
1628 if ! declare -f complete >/dev/null; then
1629 autoload -U compinit && compinit
1630 autoload -U bashcompinit && bashcompinit
1631 fi
Jim Tanga881a252018-06-19 16:34:41 +08001632}
1633
1634function validate_current_shell() {
1635 local current_sh="$(ps -o command -p $$)"
1636 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001637 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001638 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001639 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001640 *zsh*)
1641 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001642 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001643 *)
Jim Tanga881a252018-06-19 16:34:41 +08001644 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 -07001645 ;;
1646 esac
Jim Tanga881a252018-06-19 16:34:41 +08001647}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001648
1649# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001650# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1651# load those.
1652#
1653# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001654function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001655 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001656 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001657 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001658 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 -08001659 if [ -n "$allowed" ]; then
1660 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1661 echo " $allowed"
1662 echo " $f"
1663 return
1664 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001665 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001666 done
1667
1668 allowed_files=
1669 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001670 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001671 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001672 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001673
1674 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001675 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001676 else
1677 echo "ignoring $f, not in $allowed"
1678 fi
Jim Tanga881a252018-06-19 16:34:41 +08001679 done
1680 done
1681}
Kenny Root52aa81c2011-07-15 11:07:06 -07001682
Dan Albertbab814f2020-08-26 15:34:53 -07001683function showcommands() {
1684 local T=$(gettop)
1685 if [[ -z "$TARGET_PRODUCT" ]]; then
1686 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1687 return
1688 fi
1689 case $(uname -s) in
1690 Darwin)
1691 PREBUILT_NAME=darwin-x86
1692 ;;
1693 Linux)
1694 PREBUILT_NAME=linux-x86
1695 ;;
1696 *)
1697 >&2 echo Unknown host $(uname -s)
1698 return
1699 ;;
1700 esac
1701 if [[ -z "$OUT_DIR" ]]; then
1702 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1703 OUT_DIR=out
1704 else
1705 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1706 fi
1707 fi
1708 if [[ "$1" == "--regenerate" ]]; then
1709 shift 1
1710 NINJA_ARGS="-t commands $@" m
1711 else
1712 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1713 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1714 -t commands "$@")
1715 fi
1716}
1717
Jim Tanga881a252018-06-19 16:34:41 +08001718validate_current_shell
1719source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001720addcompletions