blob: f4cf0f6cf3065a0abc2609fbd3eb2f4baec91159 [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.
Taesu Leeea0cecd2020-10-28 11:05:18 +090026- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080027- resgrep: Greps on all local res/*.xml files.
28- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070029- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060030- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080031- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080032- sepgrep: Greps on all local sepolicy files.
33- sgrep: Greps on all local source files.
34- godir: Go to the directory containing a file.
35- allmod: List all modules.
36- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080037- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000038- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080039- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000040- installmod: Adb installs a module's built APK.
41- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000042- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070043
Roland Levillain39341922015-10-20 12:48:19 +010044Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070045- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080046- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070047
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070048Look at the source to view more functions. The complete list is:
49EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070050 local T=$(gettop)
51 local A=""
52 local i
Jacky Cao89483b82015-05-15 22:12:53 +080053 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 -070054 A="$A $i"
55 done
56 echo $A
57}
58
Ying Wang08800fd2016-03-03 20:57:21 -080059# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000060function build_build_var_cache()
61{
Christopher Ferris55257d22017-03-23 11:08:58 -070062 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080063 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080064 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' ' '`)
65 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 -080066 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080067 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080068 --vars="${cached_vars[*]}" \
69 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070070 --var-prefix=var_cache_ \
71 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080072 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000073 if [ $ret -ne 0 ]
74 then
Ying Wang08800fd2016-03-03 20:57:21 -080075 unset build_dicts_script
76 return $ret
77 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070078 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080079 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080080 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080081 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000082 if [ $ret -ne 0 ]
83 then
Ying Wang08800fd2016-03-03 20:57:21 -080084 return $ret
85 fi
86 BUILD_VAR_CACHE_READY="true"
87}
88
Ying Wangf0cb3972016-03-04 13:56:23 -080089# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080090# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000091function destroy_build_var_cache()
92{
Ying Wang08800fd2016-03-03 20:57:21 -080093 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070094 local v
Ying Wang08800fd2016-03-03 20:57:21 -080095 for v in $cached_vars; do
96 unset var_cache_$v
97 done
98 unset cached_vars
99 for v in $cached_abs_vars; do
100 unset abs_var_cache_$v
101 done
102 unset cached_abs_vars
103}
104
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700105# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000106function get_abs_build_var()
107{
108 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
109 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800110 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200111 return
Ying Wang08800fd2016-03-03 20:57:21 -0800112 fi
113
Christopher Ferris55257d22017-03-23 11:08:58 -0700114 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700115 if [ ! "$T" ]; then
116 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
117 return
118 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700119 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700120}
121
122# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000123function get_build_var()
124{
125 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
126 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800127 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100128 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800129 fi
130
Christopher Ferris55257d22017-03-23 11:08:58 -0700131 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700132 if [ ! "$T" ]; then
133 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100134 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700135 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700136 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800137}
138
139# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000140function check_product()
141{
Christopher Ferris55257d22017-03-23 11:08:58 -0700142 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800143 if [ ! "$T" ]; then
144 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
145 return
146 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700147 TARGET_PRODUCT=$1 \
148 TARGET_BUILD_VARIANT= \
149 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700150 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800151 get_build_var TARGET_DEVICE > /dev/null
152 # hide successful answers, but allow the errors to show
153}
154
155VARIANT_CHOICES=(user userdebug eng)
156
157# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000158function check_variant()
159{
Christopher Ferris55257d22017-03-23 11:08:58 -0700160 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800161 for v in ${VARIANT_CHOICES[@]}
162 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000163 if [ "$v" = "$1" ]
164 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800165 return 0
166 fi
167 done
168 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700169}
170
Matt Alexanderd9c56562020-05-21 10:49:17 +0000171function setpaths()
172{
Christopher Ferris55257d22017-03-23 11:08:58 -0700173 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700174 if [ ! "$T" ]; then
175 echo "Couldn't locate the top of the tree. Try setting TOP."
176 return
177 fi
178
179 ##################################################################
180 # #
181 # Read me before you modify this code #
182 # #
183 # This function sets ANDROID_BUILD_PATHS to what it is adding #
184 # to PATH, and the next time it is run, it removes that from #
185 # PATH. This is required so lunch can be run more than once #
186 # and still have working paths. #
187 # #
188 ##################################################################
189
Raphael Mollc639c782011-06-20 17:25:01 -0700190 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
191 # due to "C:\Program Files" being in the path.
192
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700193 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000194 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700195 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
196 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000197 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700198 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800199 # strip leading ':', if any
200 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500201 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700202
203 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700204 local prebuiltdir=$(getprebuilt)
205 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700206
Ben Cheng8bc4c432012-11-16 13:29:13 -0800207 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700208 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
209 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800210 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800211
Raphael Mollc639c782011-06-20 17:25:01 -0700212 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800213 export ANDROID_TOOLCHAIN=
214 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200215 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700216 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200217 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000218 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700219 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000220 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400221 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000222 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200223 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000224 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
225 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700226 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200227 *)
228 echo "Can't find toolchain for unknown architecture: $ARCH"
229 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700230 ;;
231 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700232 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800233 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700234 fi
Raphael732936d2011-06-22 14:35:32 -0700235
Christopher Ferris55257d22017-03-23 11:08:58 -0700236 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800237 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
238 fi
239
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700240 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700241
242 # add kernel specific binaries
243 case $(uname -s) in
244 Linux)
245 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
246 ;;
247 *)
248 ;;
249 esac
250
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400251 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
252 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
254 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700255 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
256
257 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
258 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
259 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200260
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800261 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
262 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
263
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200264 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
265 # to ensure that the corresponding 'emulator' binaries are used.
266 case $(uname -s) in
267 Darwin)
268 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
269 ;;
270 Linux)
271 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
272 ;;
273 *)
274 ANDROID_EMULATOR_PREBUILTS=
275 ;;
276 esac
277 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700278 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200279 export ANDROID_EMULATOR_PREBUILTS
280 fi
281
Jim Tangb3fda302018-12-22 10:24:55 +0800282 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
283 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700284 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
285 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
286 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
287 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800288
Ryan Prichard8426a192019-07-15 18:05:29 -0700289 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800290
291 # out with the duplicate old
292 if [ -n $ANDROID_PYTHONPATH ]; then
293 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
294 fi
295 # and in with the new
296 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800297 if [ -n $VENDOR_PYTHONPATH ]; then
298 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
299 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800300 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800301
Colin Crosse97e6932017-06-30 16:01:45 -0700302 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
303 export JAVA_HOME=$ANDROID_JAVA_HOME
304 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
305 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
306 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500307
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800308 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700309 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
310 export OUT=$ANDROID_PRODUCT_OUT
311
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700312 unset ANDROID_HOST_OUT
313 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
314
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900315 unset ANDROID_SOONG_HOST_OUT
316 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
317
Simran Basidd050ed2017-02-13 13:46:48 -0800318 unset ANDROID_HOST_OUT_TESTCASES
319 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
320
321 unset ANDROID_TARGET_OUT_TESTCASES
322 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
323
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800324 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700325 # TODO: fix the path
326 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
327}
328
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000329function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000330{
331 local T="$(gettop)"
332 if [ ! "$T" ]; then
333 echo "Couldn't locate the top of the tree. Try setting TOP."
334 return
335 fi
336
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000337 if which bazel &>/dev/null; then
Jingwen Chenc8b5e192020-12-14 10:28:33 -0500338 >&2 echo "NOTE: bazel() function sourced from envsetup.sh is being used instead of $(which bazel)"
339 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000340 fi
341
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000342 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000343}
344
Matt Alexanderd9c56562020-05-21 10:49:17 +0000345function printconfig()
346{
Christopher Ferris55257d22017-03-23 11:08:58 -0700347 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800348 if [ ! "$T" ]; then
349 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
350 return
351 fi
352 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700353}
354
Matt Alexanderd9c56562020-05-21 10:49:17 +0000355function set_stuff_for_environment()
356{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800357 setpaths
358 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700359
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800360 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700361 # With this environment variable new GCC can apply colors to warnings/errors
362 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 -0700363}
364
Matt Alexanderd9c56562020-05-21 10:49:17 +0000365function set_sequence_number()
366{
Colin Cross88737132017-03-21 17:41:03 -0700367 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700368}
369
Makoto Onukida971062018-06-18 10:15:19 -0700370# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
371function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800372 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700373 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800374 *:"$cmd":*)
375 return 1
376 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700377 esac
378 return 0
379}
380
Matt Alexanderd9c56562020-05-21 10:49:17 +0000381function addcompletions()
382{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700383 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700384
Jim Tanga881a252018-06-19 16:34:41 +0800385 # Keep us from trying to run in something that's neither bash nor zsh.
386 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700387 return
388 fi
389
390 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800391 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700392 return
393 fi
394
Jim Tanga881a252018-06-19 16:34:41 +0800395 local completion_files=(
396 system/core/adb/adb.bash
397 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800398 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800399 )
Makoto Onukida971062018-06-18 10:15:19 -0700400 # Completion can be disabled selectively to allow users to use non-standard completion.
401 # e.g.
402 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
403 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800404 for f in ${completion_files[*]}; do
405 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700406 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700407 fi
408 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700409
Matt Alexanderd9c56562020-05-21 10:49:17 +0000410 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700411 complete -C "bit --tab" bit
412 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000413 if [ -z "$ZSH_VERSION" ]; then
414 # Doesn't work in zsh.
415 complete -o nospace -F _croot croot
416 fi
Jim Tanga881a252018-06-19 16:34:41 +0800417 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800418
Cole Faust24c36db2021-01-23 02:39:37 +0000419 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100420 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000421 complete -F _complete_android_module_names outmod
422 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100423 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700424}
425
Matt Alexanderd9c56562020-05-21 10:49:17 +0000426function choosetype()
427{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428 echo "Build type choices are:"
429 echo " 1. release"
430 echo " 2. debug"
431 echo
432
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800433 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700434 DEFAULT_NUM=1
435 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437 export TARGET_BUILD_TYPE=
438 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700439 while [ -z $TARGET_BUILD_TYPE ]
440 do
441 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000442 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800443 read ANSWER
444 else
445 echo $1
446 ANSWER=$1
447 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700448 case $ANSWER in
449 "")
450 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
451 ;;
452 1)
453 export TARGET_BUILD_TYPE=release
454 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800455 release)
456 export TARGET_BUILD_TYPE=release
457 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700458 2)
459 export TARGET_BUILD_TYPE=debug
460 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800461 debug)
462 export TARGET_BUILD_TYPE=debug
463 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700464 *)
465 echo
466 echo "I didn't understand your response. Please try again."
467 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700468 ;;
469 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000470 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800471 break
472 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700473 done
474
Ying Wang08800fd2016-03-03 20:57:21 -0800475 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700476 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800477 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478}
479
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480#
481# This function isn't really right: It chooses a TARGET_PRODUCT
482# based on the list of boards. Usually, that gets you something
483# that kinda works with a generic product, but really, you should
484# pick a product by name.
485#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000486function chooseproduct()
487{
Christopher Ferris55257d22017-03-23 11:08:58 -0700488 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000489 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 default_value=$TARGET_PRODUCT
491 else
Ying Wang0a76df52015-06-08 11:57:26 -0700492 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493 fi
494
Ying Wang08800fd2016-03-03 20:57:21 -0800495 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800496 export TARGET_PRODUCT=
497 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498 while [ -z "$TARGET_PRODUCT" ]
499 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700500 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000501 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502 read ANSWER
503 else
504 echo $1
505 ANSWER=$1
506 fi
507
Matt Alexanderd9c56562020-05-21 10:49:17 +0000508 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800510 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000511 if check_product $ANSWER
512 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800513 export TARGET_PRODUCT=$ANSWER
514 else
515 echo "** Not a valid product: $ANSWER"
516 fi
517 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000518 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800519 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700520 fi
521 done
522
Ying Wang08800fd2016-03-03 20:57:21 -0800523 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700524 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800525 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700526}
527
Matt Alexanderd9c56562020-05-21 10:49:17 +0000528function choosevariant()
529{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800530 echo "Variant choices are:"
531 local index=1
532 local v
533 for v in ${VARIANT_CHOICES[@]}
534 do
535 # The product name is the name of the directory containing
536 # the makefile we found, above.
537 echo " $index. $v"
538 index=$(($index+1))
539 done
540
541 local default_value=eng
542 local ANSWER
543
544 export TARGET_BUILD_VARIANT=
545 while [ -z "$TARGET_BUILD_VARIANT" ]
546 do
547 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000548 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800549 read ANSWER
550 else
551 echo $1
552 ANSWER=$1
553 fi
554
Matt Alexanderd9c56562020-05-21 10:49:17 +0000555 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000557 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
558 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200559 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800560 fi
561 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000562 if check_variant $ANSWER
563 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800564 export TARGET_BUILD_VARIANT=$ANSWER
565 else
566 echo "** Not a valid variant: $ANSWER"
567 fi
568 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000569 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800570 break
571 fi
572 done
573}
574
Matt Alexanderd9c56562020-05-21 10:49:17 +0000575function choosecombo()
576{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700577 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578
579 echo
580 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700581 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700582
583 echo
584 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700585 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586
587 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800588 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700589 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800590 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800591 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700592}
593
Matt Alexanderd9c56562020-05-21 10:49:17 +0000594function add_lunch_combo()
595{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800596 if [ -n "$ZSH_VERSION" ]; then
597 echo -n "${funcfiletrace[1]}: "
598 else
599 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
600 fi
601 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 -0800602}
603
Matt Alexanderd9c56562020-05-21 10:49:17 +0000604function print_lunch_menu()
605{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700606 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100607 local choices
608 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
609 local ret=$?
610
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700611 echo
612 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700613 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100614
Matt Alexanderd9c56562020-05-21 10:49:17 +0000615 if [ $ret -ne 0 ]
616 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100617 echo "Warning: Cannot display lunch menu."
618 echo
619 echo "Note: You can invoke lunch with an explicit target:"
620 echo
621 echo " usage: lunch [target]" >&2
622 echo
623 return
624 fi
625
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700626 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800627
628 local i=1
629 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700630 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800631 do
632 echo " $i. $choice"
633 i=$(($i+1))
634 done
635
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700636 echo
637}
638
Matt Alexanderd9c56562020-05-21 10:49:17 +0000639function lunch()
640{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800641 local answer
642
Steven Moreland92793dc2020-02-25 18:30:18 -0800643 if [[ $# -gt 1 ]]; then
644 echo "usage: lunch [target]" >&2
645 return 1
646 fi
647
648 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800649 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700650 else
651 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700652 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800653 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700654 fi
655
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800656 local selection=
657
Matt Alexanderd9c56562020-05-21 10:49:17 +0000658 if [ -z "$answer" ]
659 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700660 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000661 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
662 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800663 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000664 if [ $answer -le ${#choices[@]} ]
665 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800666 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000667 if [ -n "$ZSH_VERSION" ]
668 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800669 selection=${choices[$(($answer))]}
670 else
671 selection=${choices[$(($answer-1))]}
672 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800673 fi
Colin Cross88737132017-03-21 17:41:03 -0700674 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800675 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700676 fi
677
Joe Onoratoda12daf2010-06-09 18:18:31 -0700678 export TARGET_BUILD_APPS=
679
Colin Cross88737132017-03-21 17:41:03 -0700680 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700681 product=${selection%%-*} # Trim everything after first dash
682 variant_and_version=${selection#*-} # Trim everything up to first dash
683 if [ "$variant_and_version" != "$selection" ]; then
684 variant=${variant_and_version%%-*}
685 if [ "$variant" != "$variant_and_version" ]; then
686 version=${variant_and_version#*-}
687 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700688 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800689
Matt Alexanderd9c56562020-05-21 10:49:17 +0000690 if [ -z "$product" ]
691 then
Ying Wang08800fd2016-03-03 20:57:21 -0800692 echo
Colin Cross88737132017-03-21 17:41:03 -0700693 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700694 return 1
695 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800696
Colin Cross88737132017-03-21 17:41:03 -0700697 TARGET_PRODUCT=$product \
698 TARGET_BUILD_VARIANT=$variant \
699 TARGET_PLATFORM_VERSION=$version \
700 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000701 if [ $? -ne 0 ]
702 then
Colin Cross88737132017-03-21 17:41:03 -0700703 return 1
704 fi
Colin Cross88737132017-03-21 17:41:03 -0700705 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
706 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700707 if [ -n "$version" ]; then
708 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
709 else
710 unset TARGET_PLATFORM_VERSION
711 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700712 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700713
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700714 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800715
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700717 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800718 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700719}
720
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700721unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700722# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000723function _lunch()
724{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700725 local cur prev opts
726 COMPREPLY=()
727 cur="${COMP_WORDS[COMP_CWORD]}"
728 prev="${COMP_WORDS[COMP_CWORD-1]}"
729
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700730 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800731 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700732 fi
733
734 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700735 return 0
736}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700737
Joe Onoratoda12daf2010-06-09 18:18:31 -0700738# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700739# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000740function tapas()
741{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700742 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800743 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700744 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700745 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 -0800746 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 -0700747
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700748 if [ "$showHelp" != "" ]; then
749 $(gettop)/build/make/tapasHelp.sh
750 return
751 fi
752
Ying Wang67f02922012-08-22 10:25:20 -0700753 if [ $(echo $arch | wc -w) -gt 1 ]; then
754 echo "tapas: Error: Multiple build archs supplied: $arch"
755 return
756 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700757 if [ $(echo $variant | wc -w) -gt 1 ]; then
758 echo "tapas: Error: Multiple build variants supplied: $variant"
759 return
760 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700761 if [ $(echo $density | wc -w) -gt 1 ]; then
762 echo "tapas: Error: Multiple densities supplied: $density"
763 return
764 fi
Ying Wang67f02922012-08-22 10:25:20 -0700765
Ying Wang0a76df52015-06-08 11:57:26 -0700766 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700767 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000768 x86) product=aosp_x86;;
769 arm64) product=aosp_arm64;;
770 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700771 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700772 if [ -z "$variant" ]; then
773 variant=eng
774 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700775 if [ -z "$apps" ]; then
776 apps=all
777 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600778 if [ -z "$density" ]; then
779 density=alldpi
780 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700781
Ying Wang67f02922012-08-22 10:25:20 -0700782 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700783 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700784 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700785 export TARGET_BUILD_TYPE=release
786 export TARGET_BUILD_APPS=$apps
787
Ying Wang08800fd2016-03-03 20:57:21 -0800788 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700789 set_stuff_for_environment
790 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800791 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700792}
793
Matt Alexanderd9c56562020-05-21 10:49:17 +0000794function gettop
795{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700796 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000797 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700798 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000799 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700800 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000801 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800802 # The following circumlocution (repeated below as well) ensures
803 # that we record the true directory name and not one that is
804 # faked up with symlink names.
805 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700806 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800807 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700808 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000809 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800810 \cd ..
synergyb112a402013-07-05 19:47:47 -0700811 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700812 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000813 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700814 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000815 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700816 fi
817 fi
818 fi
819}
820
Matt Alexanderd9c56562020-05-21 10:49:17 +0000821function croot()
822{
Christopher Ferris55257d22017-03-23 11:08:58 -0700823 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700824 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700825 if [ "$1" ]; then
826 \cd $(gettop)/$1
827 else
828 \cd $(gettop)
829 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700830 else
831 echo "Couldn't locate the top of the tree. Try setting TOP."
832 fi
833}
834
Matt Alexanderd9c56562020-05-21 10:49:17 +0000835function _croot()
836{
Anton Hanssonece9c482019-02-04 18:15:39 +0000837 local T=$(gettop)
838 if [ "$T" ]; then
839 local cur="${COMP_WORDS[COMP_CWORD]}"
840 k=0
841 for c in $(compgen -d ${T}/${cur}); do
842 COMPREPLY[k++]=${c#${T}/}/
843 done
844 fi
845}
846
Matt Alexanderd9c56562020-05-21 10:49:17 +0000847function cproj()
848{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700849 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700850 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700851 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700852 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
853 T=$PWD
854 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800855 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700856 return
857 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800858 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700859 done
Ying Wang9cd17642012-12-13 10:52:07 -0800860 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700861 echo "can't find Android.mk"
862}
863
Daniel Sandler47e0a882013-07-30 13:23:52 -0400864# simplified version of ps; output in the form
865# <pid> <procname>
866function qpid() {
867 local prepend=''
868 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000869 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400870 prepend=' '
871 append='$'
872 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000873 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800874 echo "usage: qpid [[--exact] <process name|pid>"
875 return 255
876 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400877
878 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000879 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800880 qpid | \grep "$prepend$EXE$append"
881 else
882 adb shell ps \
883 | tr -d '\r' \
884 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
885 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400886}
887
Steven Moreland74114f12020-09-10 01:23:32 +0000888# syswrite - disable verity, reboot if needed, and remount image
889#
890# Easy way to make system.img/etc writable
891function syswrite() {
892 adb wait-for-device && adb root || return 1
893 if [[ $(adb disable-verity | grep "reboot") ]]; then
894 echo "rebooting"
895 adb reboot && adb wait-for-device && adb root || return 1
896 fi
897 adb wait-for-device && adb remount || return 1
898}
899
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800900# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700901# that has the core-file-size limit set correctly
902#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800903# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700904# if its core-file-size limit is not set already.
905# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
906
Matt Alexanderd9c56562020-05-21 10:49:17 +0000907function coredump_setup()
908{
909 echo "Getting root...";
910 adb root;
911 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700912
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913 echo "Remounting root partition read-write...";
914 adb shell mount -w -o remount -t rootfs rootfs;
915 sleep 1;
916 adb wait-for-device;
917 adb shell mkdir -p /cores;
918 adb shell mount -t tmpfs tmpfs /cores;
919 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700920
Matt Alexanderd9c56562020-05-21 10:49:17 +0000921 echo "Granting SELinux permission to dump in /cores...";
922 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923
Matt Alexanderd9c56562020-05-21 10:49:17 +0000924 echo "Set core pattern.";
925 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700926
Ying Wang08800fd2016-03-03 20:57:21 -0800927 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700928}
929
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800930# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000931# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700932#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800933# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700934# dump to actually be generated.
935
Matt Alexanderd9c56562020-05-21 10:49:17 +0000936function coredump_enable()
937{
938 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800939 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000940 printf "Expecting a PID!\n";
941 return;
942 fi;
943 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800944 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700945}
946
947# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000948# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700949#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800950# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700951# enabled globally.
952
Matt Alexanderd9c56562020-05-21 10:49:17 +0000953function core()
954{
955 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700956
Ying Wang08800fd2016-03-03 20:57:21 -0800957 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000958 printf "Expecting a PID!\n";
959 return;
960 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700961
Matt Alexanderd9c56562020-05-21 10:49:17 +0000962 local CORENAME=core.$PID;
963 local COREPATH=/cores/$CORENAME;
964 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700965
Matt Alexanderd9c56562020-05-21 10:49:17 +0000966 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700967
Matt Alexanderd9c56562020-05-21 10:49:17 +0000968 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800969 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000970 printf "\tSending SIG%s to %d...\n" $SIG $PID;
971 adb shell kill -$SIG $PID;
972 sleep 1;
973 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700974
Matt Alexanderd9c56562020-05-21 10:49:17 +0000975 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
976 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700977}
978
Christopher Tate744ee802009-11-12 15:33:08 -0800979# systemstack - dump the current stack trace of all threads in the system process
980# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000981function systemstack()
982{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800983 stacks system_server
984}
985
Michael Wrightaeed7212014-06-19 19:58:12 -0700986# Read the ELF header from /proc/$PID/exe to determine if the process is
987# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000988function is64bit()
989{
Ben Chengfba67bf2014-02-25 10:27:07 -0800990 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991 if [ "$PID" ] ; then
992 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800993 echo "64"
994 else
995 echo ""
996 fi
997 else
998 echo ""
999 fi
1000}
1001
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001002case `uname -s` in
1003 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001004 function sgrep()
1005 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001006 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|kt|xml|sh|mk|aidl|vts|proto)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001007 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001008 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001009
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001010 ;;
1011 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001012 function sgrep()
1013 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001014 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|kt\|xml\|sh\|mk\|aidl\|vts\|proto\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001015 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001016 }
1017 ;;
1018esac
1019
Matt Alexanderd9c56562020-05-21 10:49:17 +00001020function gettargetarch
1021{
Raghu Gandham8da43102012-07-25 19:57:22 -07001022 get_build_var TARGET_ARCH
1023}
1024
Matt Alexanderd9c56562020-05-21 10:49:17 +00001025function ggrep()
1026{
Mike Frysinger5e479732015-09-22 18:13:48 -04001027 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1028 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001029}
1030
Matt Alexanderd9c56562020-05-21 10:49:17 +00001031function gogrep()
1032{
Orion Hodson831472d2019-10-25 11:35:15 +01001033 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1034 -exec grep --color -n "$@" {} +
1035}
1036
Matt Alexanderd9c56562020-05-21 10:49:17 +00001037function jgrep()
1038{
Mike Frysinger5e479732015-09-22 18:13:48 -04001039 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1040 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001041}
1042
Alistair Delva176e5342021-02-22 13:31:26 -08001043function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001044{
1045 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1046 -exec grep --color -n "$@" {} +
1047}
1048
Taesu Leeea0cecd2020-10-28 11:05:18 +09001049function ktgrep()
1050{
1051 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1052 -exec grep --color -n "$@" {} +
1053}
1054
Matt Alexanderd9c56562020-05-21 10:49:17 +00001055function cgrep()
1056{
Mike Frysinger5e479732015-09-22 18:13:48 -04001057 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' \) \
1058 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001059}
1060
Matt Alexanderd9c56562020-05-21 10:49:17 +00001061function resgrep()
1062{
Christopher Ferris55257d22017-03-23 11:08:58 -07001063 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001064 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1065 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1066 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001067}
1068
Matt Alexanderd9c56562020-05-21 10:49:17 +00001069function mangrep()
1070{
Mike Frysinger5e479732015-09-22 18:13:48 -04001071 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1072 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001073}
1074
Matt Alexanderd9c56562020-05-21 10:49:17 +00001075function owngrep()
1076{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001077 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1078 -exec grep --color -n "$@" {} +
1079}
1080
Matt Alexanderd9c56562020-05-21 10:49:17 +00001081function sepgrep()
1082{
Mike Frysinger5e479732015-09-22 18:13:48 -04001083 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1084 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001085}
1086
Matt Alexanderd9c56562020-05-21 10:49:17 +00001087function rcgrep()
1088{
Mike Frysinger5e479732015-09-22 18:13:48 -04001089 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1090 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001091}
1092
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001093case `uname -s` in
1094 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001095 function mgrep()
1096 {
Orion Hodson831472d2019-10-25 11:35:15 +01001097 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 -04001098 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001099 }
1100
Matt Alexanderd9c56562020-05-21 10:49:17 +00001101 function treegrep()
1102 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001103 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001104 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001105 }
1106
1107 ;;
1108 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001109 function mgrep()
1110 {
Orion Hodson831472d2019-10-25 11:35:15 +01001111 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 -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 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001117 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|kt|xml)' -type f \
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 ;;
1122esac
1123
Matt Alexanderd9c56562020-05-21 10:49:17 +00001124function getprebuilt
1125{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001126 get_abs_build_var ANDROID_PREBUILTS
1127}
1128
Matt Alexanderd9c56562020-05-21 10:49:17 +00001129function tracedmdump()
1130{
Christopher Ferris55257d22017-03-23 11:08:58 -07001131 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001132 if [ ! "$T" ]; then
1133 echo "Couldn't locate the top of the tree. Try setting TOP."
1134 return
1135 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001136 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001137 local arch=$(gettargetarch)
1138 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001139
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001140 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001141 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001142 echo "usage: tracedmdump tracename"
1143 return
1144 fi
1145
Matt Alexanderd9c56562020-05-21 10:49:17 +00001146 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001147 echo "Error: cannot find kernel: '$KERNEL'"
1148 return
1149 fi
1150
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001151 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001152 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1154 fi
1155
1156 echo "post-processing traces..."
1157 rm -f $TRACE/qtrace.dexlist
1158 post_trace $TRACE
1159 if [ $? -ne 0 ]; then
1160 echo "***"
1161 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1162 echo "***"
1163 return
1164 fi
1165 echo "generating dexlist output..."
1166 /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
1167 echo "generating dmtrace data..."
1168 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1169 echo "generating html file..."
1170 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1171 echo "done, see $TRACE/dmtrace.html for details"
1172 echo "or run:"
1173 echo " traceview $TRACE/dmtrace"
1174}
1175
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001176# communicate with a running device or emulator, set up necessary state,
1177# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178function runhat()
1179{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001180 # process standard adb options
1181 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001182 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001183 adbTarget=$1
1184 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001185 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001186 adbTarget="$1 $2"
1187 shift 2
1188 fi
1189 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001190 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001191
1192 # runhat options
1193 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001194
Matt Alexanderd9c56562020-05-21 10:49:17 +00001195 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001196 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197 return
1198 fi
1199
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001200 # confirm hat is available
1201 if [ -z $(which hat) ]; then
1202 echo "hat is not available in this configuration."
1203 return
1204 fi
1205
Andy McFaddenb6289852010-07-12 08:00:19 -07001206 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001207 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001209 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001210 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001211 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001212 echo -n "> "
1213 read
1214
The Android Open Source Project88b60792009-03-03 19:28:42 -08001215 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216
The Android Open Source Project88b60792009-03-03 19:28:42 -08001217 echo "Retrieving file $devFile..."
1218 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219
The Android Open Source Project88b60792009-03-03 19:28:42 -08001220 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001221
The Android Open Source Project88b60792009-03-03 19:28:42 -08001222 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223 echo "View the output by pointing your browser at http://localhost:7000/"
1224 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001225 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001226}
1227
Matt Alexanderd9c56562020-05-21 10:49:17 +00001228function getbugreports()
1229{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001230 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231
1232 if [ ! "$reports" ]; then
1233 echo "Could not locate any bugreports."
1234 return
1235 fi
1236
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001237 local report
1238 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001239 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001240 echo "/sdcard/bugreports/${report}"
1241 adb pull /sdcard/bugreports/${report} ${report}
1242 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001243 done
1244}
1245
Matt Alexanderd9c56562020-05-21 10:49:17 +00001246function getsdcardpath()
1247{
Victoria Lease1b296b42012-08-21 15:44:06 -07001248 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1249}
1250
Matt Alexanderd9c56562020-05-21 10:49:17 +00001251function getscreenshotpath()
1252{
Victoria Lease1b296b42012-08-21 15:44:06 -07001253 echo "$(getsdcardpath)/Pictures/Screenshots"
1254}
1255
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256function getlastscreenshot()
1257{
Victoria Lease1b296b42012-08-21 15:44:06 -07001258 local screenshot_path=$(getscreenshotpath)
1259 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 +00001260 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001261 echo "No screenshots found."
1262 return
1263 fi
1264 echo "${screenshot}"
1265 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1266}
1267
Matt Alexanderd9c56562020-05-21 10:49:17 +00001268function startviewserver()
1269{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001270 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001271 if [ $# -gt 0 ]; then
1272 port=$1
1273 fi
1274 adb shell service call window 1 i32 $port
1275}
1276
Matt Alexanderd9c56562020-05-21 10:49:17 +00001277function stopviewserver()
1278{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001279 adb shell service call window 2
1280}
1281
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282function isviewserverstarted()
1283{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001284 adb shell service call window 3
1285}
1286
Matt Alexanderd9c56562020-05-21 10:49:17 +00001287function key_home()
1288{
Romain Guyb84049a2010-10-04 16:56:11 -07001289 adb shell input keyevent 3
1290}
1291
Matt Alexanderd9c56562020-05-21 10:49:17 +00001292function key_back()
1293{
Romain Guyb84049a2010-10-04 16:56:11 -07001294 adb shell input keyevent 4
1295}
1296
Matt Alexanderd9c56562020-05-21 10:49:17 +00001297function key_menu()
1298{
Romain Guyb84049a2010-10-04 16:56:11 -07001299 adb shell input keyevent 82
1300}
1301
Matt Alexanderd9c56562020-05-21 10:49:17 +00001302function smoketest()
1303{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001304 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1305 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1306 return
1307 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001308 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001309 if [ ! "$T" ]; then
1310 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1311 return
1312 fi
1313
Ying Wang9cd17642012-12-13 10:52:07 -08001314 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001315 adb uninstall com.android.smoketest > /dev/null &&
1316 adb uninstall com.android.smoketest.tests > /dev/null &&
1317 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1318 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1319 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1320}
1321
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001322# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001323function runtest()
1324{
Christopher Ferris55257d22017-03-23 11:08:58 -07001325 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001326 if [ ! "$T" ]; then
1327 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1328 return
1329 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001330 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001331}
1332
The Android Open Source Project88b60792009-03-03 19:28:42 -08001333function godir () {
1334 if [[ -z "$1" ]]; then
1335 echo "Usage: godir <regex>"
1336 return
1337 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001338 local T=$(gettop)
1339 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001340 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001341 mkdir -p $OUT_DIR
1342 FILELIST=$OUT_DIR/filelist
1343 else
1344 FILELIST=$T/filelist
1345 fi
1346 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001347 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001348 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001349 echo " Done"
1350 echo ""
1351 fi
1352 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001353 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001354 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001355 echo "Not found"
1356 return
1357 fi
1358 local pathname
1359 local choice
1360 if [[ ${#lines[@]} > 1 ]]; then
1361 while [[ -z "$pathname" ]]; do
1362 local index=1
1363 local line
1364 for line in ${lines[@]}; do
1365 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001366 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001367 done
1368 echo
1369 echo -n "Select one: "
1370 unset choice
1371 read choice
1372 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1373 echo "Invalid choice"
1374 continue
1375 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001376 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001377 done
1378 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001379 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001380 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001381 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001382}
1383
Steven Moreland62054a42018-12-06 10:11:40 -08001384# Update module-info.json in out.
1385function refreshmod() {
1386 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1387 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1388 return 1
1389 fi
1390
1391 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1392
1393 # for the output of the next command
1394 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1395
1396 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001397 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001398 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1399}
1400
Cole Faust24c36db2021-01-23 02:39:37 +00001401# Verifies that module-info.txt exists, creating it if it doesn't.
1402function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001403 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1404 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1405 return 1
1406 fi
1407
1408 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1409 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1410 refreshmod || return 1
1411 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001412}
1413
1414# List all modules for the current device, as cached in module-info.json. If any build change is
1415# made and it should be reflected in the output, you should run 'refreshmod' first.
1416function allmod() {
1417 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001418
LuK1337b6a78192020-01-12 03:12:17 +01001419 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 -08001420}
1421
Joe Onorato2c1aa472021-02-25 16:42:39 -08001422# Get the path of a specific module in the android tree, as cached in module-info.json.
1423# If any build change is made, and it should be reflected in the output, you should run
1424# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001425function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001426 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001427 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001428 return 1
1429 fi
1430
Cole Faust24c36db2021-01-23 02:39:37 +00001431 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001432
1433 local relpath=$(python -c "import json, os
1434module = '$1'
1435module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1436if module not in module_info:
1437 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001438print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001439
1440 if [ -z "$relpath" ]; then
1441 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1442 return 1
1443 else
Rett Berg78d1c932019-01-24 14:34:23 -08001444 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001445 fi
1446}
1447
Joe Onorato2c1aa472021-02-25 16:42:39 -08001448# Get the path of a specific module in the android tree, as cached in module-info.json.
1449# If any build change is made, and it should be reflected in the output, you should run
1450# 'refreshmod' first. Note: This is the inverse of pathmod.
1451function dirmods() {
1452 if [[ $# -ne 1 ]]; then
1453 echo "usage: dirmods <path>" >&2
1454 return 1
1455 fi
1456
1457 verifymodinfo || return 1
1458
1459 python -c "import json, os
1460dir = '$1'
1461while dir.endswith('/'):
1462 dir = dir[:-1]
1463prefix = dir + '/'
1464module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1465results = set()
1466for m in module_info.values():
1467 for path in m.get(u'path', []):
1468 if path == dir or path.startswith(prefix):
1469 name = m.get(u'module_name')
1470 if name:
1471 results.add(name)
1472for name in sorted(results):
1473 print(name)
1474"
1475}
1476
1477
Rett Berg78d1c932019-01-24 14:34:23 -08001478# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1479# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1480function gomod() {
1481 if [[ $# -ne 1 ]]; then
1482 echo "usage: gomod <module>" >&2
1483 return 1
1484 fi
1485
1486 local path="$(pathmod $@)"
1487 if [ -z "$path" ]; then
1488 return 1
1489 fi
1490 cd $path
1491}
1492
Cole Faust24c36db2021-01-23 02:39:37 +00001493# Gets the list of a module's installed outputs, as cached in module-info.json.
1494# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1495function outmod() {
1496 if [[ $# -ne 1 ]]; then
1497 echo "usage: outmod <module>" >&2
1498 return 1
1499 fi
1500
1501 verifymodinfo || return 1
1502
1503 local relpath
1504 relpath=$(python -c "import json, os
1505module = '$1'
1506module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1507if module not in module_info:
1508 exit(1)
1509for output in module_info[module]['installed']:
1510 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1511
1512 if [ $? -ne 0 ]; then
1513 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1514 return 1
1515 elif [ ! -z "$relpath" ]; then
1516 echo "$relpath"
1517 fi
1518}
1519
1520# adb install a module's apk, as cached in module-info.json. If any build change
1521# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1522# Usage: installmod [adb install arguments] <module>
1523# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1524function installmod() {
1525 if [[ $# -eq 0 ]]; then
1526 echo "usage: installmod [adb install arguments] <module>" >&2
1527 return 1
1528 fi
1529
1530 local _path
1531 _path=$(outmod ${@:$#:1})
1532 if [ $? -ne 0 ]; then
1533 return 1
1534 fi
1535
1536 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1537 if [ -z "$_path" ]; then
1538 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1539 return 1
1540 fi
1541 local length=$(( $# - 1 ))
1542 echo adb install ${@:1:$length} $_path
1543 adb install ${@:1:$length} $_path
1544}
1545
dimitry73b84812018-12-11 18:06:00 +01001546function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001547 local word=${COMP_WORDS[COMP_CWORD]}
1548 COMPREPLY=( $(allmod | grep -E "^$word") )
1549}
1550
Alex Rayf0d08eb2013-03-08 15:15:06 -08001551# Print colored exit condition
1552function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001553 "$@"
1554 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001555 if [ $retval -ne 0 ]
1556 then
Jacky Cao89483b82015-05-15 22:12:53 +08001557 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001558 else
Jacky Cao89483b82015-05-15 22:12:53 +08001559 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001560 fi
1561 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001562}
1563
Matt Alexanderd9c56562020-05-21 10:49:17 +00001564function get_make_command()
1565{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001566 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1567 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001568 # Always use the real make if -C is passed in
1569 for arg in "$@"; do
1570 if [[ $arg == -C* ]]; then
1571 echo command make
1572 return
1573 fi
1574 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001575 echo build/soong/soong_ui.bash --make-mode
1576 else
1577 echo command make
1578 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001579}
1580
Matt Alexanderd9c56562020-05-21 10:49:17 +00001581function _wrap_build()
1582{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001583 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1584 "$@"
1585 return $?
1586 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001587 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001588 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001589 local ret=$?
1590 local end_time=$(date +"%s")
1591 local tdiff=$(($end_time-$start_time))
1592 local hours=$(($tdiff / 3600 ))
1593 local mins=$((($tdiff % 3600) / 60))
1594 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001595 local ncolors=$(tput colors 2>/dev/null)
1596 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001597 color_failed=$'\E'"[0;31m"
1598 color_success=$'\E'"[0;32m"
1599 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001600 else
1601 color_failed=""
1602 color_success=""
1603 color_reset=""
1604 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001605 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001606 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001607 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001608 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001609 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001610 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001611 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001612 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001613 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001614 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001615 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001616 printf "(%s seconds)" $secs
1617 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001618 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001619 echo
1620 return $ret
1621}
1622
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001623function _trigger_build()
1624(
1625 local -r bc="$1"; shift
1626 if T="$(gettop)"; then
1627 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1628 else
1629 echo "Couldn't locate the top of the tree. Try setting TOP."
1630 fi
1631)
1632
1633function m()
1634(
1635 _trigger_build "all-modules" "$@"
1636)
1637
1638function mm()
1639(
1640 _trigger_build "modules-in-a-dir-no-deps" "$@"
1641)
1642
1643function mmm()
1644(
1645 _trigger_build "modules-in-dirs-no-deps" "$@"
1646)
1647
1648function mma()
1649(
1650 _trigger_build "modules-in-a-dir" "$@"
1651)
1652
1653function mmma()
1654(
1655 _trigger_build "modules-in-dirs" "$@"
1656)
1657
Matt Alexanderd9c56562020-05-21 10:49:17 +00001658function make()
1659{
Dan Willemsene9842242017-07-28 13:00:13 -07001660 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001661}
1662
Matt Alexanderd9c56562020-05-21 10:49:17 +00001663function provision()
1664{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001665 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1666 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1667 return 1
1668 fi
1669 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1670 echo "There is no provisioning script for the device." >&2
1671 return 1
1672 fi
1673
1674 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001675 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001676 shift 1
1677 else
1678 echo "This action will reflash your device."
1679 echo ""
1680 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1681 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001682 echo -n "Are you sure you want to do this (yes/no)? "
1683 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001684 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001685 echo "Not taking any action. Exiting." >&2
1686 return 1
1687 fi
1688 fi
1689 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1690}
1691
Jim Tanga881a252018-06-19 16:34:41 +08001692# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001693function enable_zsh_completion() {
1694 # Don't override user's options if bash-style completion is already enabled.
1695 if ! declare -f complete >/dev/null; then
1696 autoload -U compinit && compinit
1697 autoload -U bashcompinit && bashcompinit
1698 fi
Jim Tanga881a252018-06-19 16:34:41 +08001699}
1700
1701function validate_current_shell() {
1702 local current_sh="$(ps -o command -p $$)"
1703 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001704 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001705 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001706 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001707 *zsh*)
1708 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001709 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001710 *)
Jim Tanga881a252018-06-19 16:34:41 +08001711 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 -07001712 ;;
1713 esac
Jim Tanga881a252018-06-19 16:34:41 +08001714}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001715
1716# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001717# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1718# load those.
1719#
1720# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001721function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001722 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001723 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001724 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001725 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 -08001726 if [ -n "$allowed" ]; then
1727 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1728 echo " $allowed"
1729 echo " $f"
1730 return
1731 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001732 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001733 done
1734
1735 allowed_files=
1736 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001737 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001738 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001739 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001740
1741 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001742 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001743 else
1744 echo "ignoring $f, not in $allowed"
1745 fi
Jim Tanga881a252018-06-19 16:34:41 +08001746 done
1747 done
1748}
Kenny Root52aa81c2011-07-15 11:07:06 -07001749
Dan Albertbab814f2020-08-26 15:34:53 -07001750function showcommands() {
1751 local T=$(gettop)
1752 if [[ -z "$TARGET_PRODUCT" ]]; then
1753 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1754 return
1755 fi
1756 case $(uname -s) in
1757 Darwin)
1758 PREBUILT_NAME=darwin-x86
1759 ;;
1760 Linux)
1761 PREBUILT_NAME=linux-x86
1762 ;;
1763 *)
1764 >&2 echo Unknown host $(uname -s)
1765 return
1766 ;;
1767 esac
1768 if [[ -z "$OUT_DIR" ]]; then
1769 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1770 OUT_DIR=out
1771 else
1772 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1773 fi
1774 fi
1775 if [[ "$1" == "--regenerate" ]]; then
1776 shift 1
1777 NINJA_ARGS="-t commands $@" m
1778 else
1779 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1780 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1781 -t commands "$@")
1782 fi
1783}
1784
Jim Tanga881a252018-06-19 16:34:41 +08001785validate_current_shell
1786source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001787addcompletions