blob: a23bbad9c0ea2101146b07812e3161809612016b [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]
Martin Stjernholmf692c752021-04-12 00:01:10 +010012 Sets up the build environment for building unbundled apps (APKs).
13- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
14 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000015- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080016- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070017- mm: Builds and installs all of the modules in the current directory, and their
18 dependencies.
19- mmm: Builds and installs all of the modules in the supplied directories, and their
20 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080021 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070022- mma: Same as 'mm'
23- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080024- provision: Flash device with all required partitions. Options will be passed on to fastboot.
25- cgrep: Greps on all local C/C++ files.
26- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010027- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080028- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090029- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- resgrep: Greps on all local res/*.xml files.
31- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070032- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060033- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080034- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080035- sepgrep: Greps on all local sepolicy files.
36- sgrep: Greps on all local source files.
37- godir: Go to the directory containing a file.
38- allmod: List all modules.
39- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080040- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000041- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080042- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000043- installmod: Adb installs a module's built APK.
44- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000045- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046
Roland Levillain39341922015-10-20 12:48:19 +010047Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070048- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080049- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070050
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070051Look at the source to view more functions. The complete list is:
52EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070053 local T=$(gettop)
54 local A=""
55 local i
Jacky Cao89483b82015-05-15 22:12:53 +080056 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 -070057 A="$A $i"
58 done
59 echo $A
60}
61
Ying Wang08800fd2016-03-03 20:57:21 -080062# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000063function build_build_var_cache()
64{
Christopher Ferris55257d22017-03-23 11:08:58 -070065 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080066 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080067 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' ' '`)
68 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 -080069 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080070 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080071 --vars="${cached_vars[*]}" \
72 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070073 --var-prefix=var_cache_ \
74 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080075 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000076 if [ $ret -ne 0 ]
77 then
Ying Wang08800fd2016-03-03 20:57:21 -080078 unset build_dicts_script
79 return $ret
80 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070081 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080082 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080083 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080084 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000085 if [ $ret -ne 0 ]
86 then
Ying Wang08800fd2016-03-03 20:57:21 -080087 return $ret
88 fi
89 BUILD_VAR_CACHE_READY="true"
90}
91
Ying Wangf0cb3972016-03-04 13:56:23 -080092# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080093# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000094function destroy_build_var_cache()
95{
Ying Wang08800fd2016-03-03 20:57:21 -080096 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070097 local v
Ying Wang08800fd2016-03-03 20:57:21 -080098 for v in $cached_vars; do
99 unset var_cache_$v
100 done
101 unset cached_vars
102 for v in $cached_abs_vars; do
103 unset abs_var_cache_$v
104 done
105 unset cached_abs_vars
106}
107
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000109function get_abs_build_var()
110{
111 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
112 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800113 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200114 return
Ying Wang08800fd2016-03-03 20:57:21 -0800115 fi
116
Christopher Ferris55257d22017-03-23 11:08:58 -0700117 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700118 if [ ! "$T" ]; then
119 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
120 return
121 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700122 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700123}
124
125# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000126function get_build_var()
127{
128 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
129 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800130 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100131 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800132 fi
133
Christopher Ferris55257d22017-03-23 11:08:58 -0700134 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700135 if [ ! "$T" ]; then
136 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100137 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700139 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140}
141
142# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000143function check_product()
144{
Christopher Ferris55257d22017-03-23 11:08:58 -0700145 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 if [ ! "$T" ]; then
147 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
148 return
149 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700150 TARGET_PRODUCT=$1 \
151 TARGET_BUILD_VARIANT= \
152 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700153 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800154 get_build_var TARGET_DEVICE > /dev/null
155 # hide successful answers, but allow the errors to show
156}
157
158VARIANT_CHOICES=(user userdebug eng)
159
160# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000161function check_variant()
162{
Christopher Ferris55257d22017-03-23 11:08:58 -0700163 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800164 for v in ${VARIANT_CHOICES[@]}
165 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166 if [ "$v" = "$1" ]
167 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800168 return 0
169 fi
170 done
171 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700172}
173
Matt Alexanderd9c56562020-05-21 10:49:17 +0000174function setpaths()
175{
Christopher Ferris55257d22017-03-23 11:08:58 -0700176 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700177 if [ ! "$T" ]; then
178 echo "Couldn't locate the top of the tree. Try setting TOP."
179 return
180 fi
181
182 ##################################################################
183 # #
184 # Read me before you modify this code #
185 # #
186 # This function sets ANDROID_BUILD_PATHS to what it is adding #
187 # to PATH, and the next time it is run, it removes that from #
188 # PATH. This is required so lunch can be run more than once #
189 # and still have working paths. #
190 # #
191 ##################################################################
192
Raphael Mollc639c782011-06-20 17:25:01 -0700193 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
194 # due to "C:\Program Files" being in the path.
195
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000197 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
199 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000200 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700201 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800202 # strip leading ':', if any
203 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500204 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700205
206 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local prebuiltdir=$(getprebuilt)
208 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700209
Ben Cheng8bc4c432012-11-16 13:29:13 -0800210 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700211 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
212 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800213 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800214
Raphael Mollc639c782011-06-20 17:25:01 -0700215 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800216 export ANDROID_TOOLCHAIN=
217 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200218 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700219 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700222 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400224 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000225 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200226 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000227 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
228 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700229 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200230 *)
231 echo "Can't find toolchain for unknown architecture: $ARCH"
232 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700233 ;;
234 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700235 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800236 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700237 fi
Raphael732936d2011-06-22 14:35:32 -0700238
Christopher Ferris55257d22017-03-23 11:08:58 -0700239 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800240 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
241 fi
242
Dan Willemsen838dcec2021-09-30 22:40:34 +0000243 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700244
245 # add kernel specific binaries
246 case $(uname -s) in
247 Linux)
248 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
249 ;;
250 *)
251 ;;
252 esac
253
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400254 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
Martin Stjernholm6a012262021-11-24 14:56:15 +0000255 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
Yi Kongdfd00b12019-05-21 16:00:04 -0700256 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
257
258 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
259 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
260 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200261
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800262 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
263 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
264
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200265 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
266 # to ensure that the corresponding 'emulator' binaries are used.
267 case $(uname -s) in
268 Darwin)
269 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
270 ;;
271 Linux)
272 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
273 ;;
274 *)
275 ANDROID_EMULATOR_PREBUILTS=
276 ;;
277 esac
278 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700279 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200280 export ANDROID_EMULATOR_PREBUILTS
281 fi
282
Jim Tangb3fda302018-12-22 10:24:55 +0800283 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
284 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700285 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
286 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
287 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000288 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800289
Martin Stjernholm6a012262021-11-24 14:56:15 +0000290 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700291 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800292
293 # out with the duplicate old
294 if [ -n $ANDROID_PYTHONPATH ]; then
295 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
296 fi
297 # and in with the new
298 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800299 if [ -n $VENDOR_PYTHONPATH ]; then
300 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
301 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800302 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800303
Colin Crosse97e6932017-06-30 16:01:45 -0700304 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
305 export JAVA_HOME=$ANDROID_JAVA_HOME
306 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
307 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
308 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500309
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800310 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
312 export OUT=$ANDROID_PRODUCT_OUT
313
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700314 unset ANDROID_HOST_OUT
315 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
316
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900317 unset ANDROID_SOONG_HOST_OUT
318 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
319
Simran Basidd050ed2017-02-13 13:46:48 -0800320 unset ANDROID_HOST_OUT_TESTCASES
321 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
322
323 unset ANDROID_TARGET_OUT_TESTCASES
324 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327 # TODO: fix the path
328 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
329}
330
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000331function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000332{
Jingwen Chen17d56992021-06-10 08:17:54 +0000333 if which bazel &>/dev/null; then
334 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
335 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000336 fi
337
Jingwen Chen17d56992021-06-10 08:17:54 +0000338 local T="$(gettop)"
339 if [ ! "$T" ]; then
340 >&2 echo "Couldn't locate the top of the Android tree. Try setting TOP. This bazel() function cannot be used outside of the AOSP directory."
341 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000342 fi
343
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000344 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000345}
346
Matt Alexanderd9c56562020-05-21 10:49:17 +0000347function printconfig()
348{
Christopher Ferris55257d22017-03-23 11:08:58 -0700349 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800350 if [ ! "$T" ]; then
351 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
352 return
353 fi
354 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700355}
356
Matt Alexanderd9c56562020-05-21 10:49:17 +0000357function set_stuff_for_environment()
358{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800359 setpaths
360 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700361
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800362 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700363 # With this environment variable new GCC can apply colors to warnings/errors
364 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 -0700365}
366
Matt Alexanderd9c56562020-05-21 10:49:17 +0000367function set_sequence_number()
368{
Colin Cross88737132017-03-21 17:41:03 -0700369 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700370}
371
Makoto Onukida971062018-06-18 10:15:19 -0700372# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
373function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800374 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700375 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800376 *:"$cmd":*)
377 return 1
378 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700379 esac
380 return 0
381}
382
Matt Alexanderd9c56562020-05-21 10:49:17 +0000383function addcompletions()
384{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700385 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700386
Jim Tanga881a252018-06-19 16:34:41 +0800387 # Keep us from trying to run in something that's neither bash nor zsh.
388 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700389 return
390 fi
391
392 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800393 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700394 return
395 fi
396
Jim Tanga881a252018-06-19 16:34:41 +0800397 local completion_files=(
398 system/core/adb/adb.bash
399 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800400 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800401 )
Makoto Onukida971062018-06-18 10:15:19 -0700402 # Completion can be disabled selectively to allow users to use non-standard completion.
403 # e.g.
404 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
405 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800406 for f in ${completion_files[*]}; do
407 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700408 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700409 fi
410 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700411
Matt Alexanderd9c56562020-05-21 10:49:17 +0000412 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700413 complete -C "bit --tab" bit
414 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000415 if [ -z "$ZSH_VERSION" ]; then
416 # Doesn't work in zsh.
417 complete -o nospace -F _croot croot
418 fi
Jim Tanga881a252018-06-19 16:34:41 +0800419 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800420
Cole Faust24c36db2021-01-23 02:39:37 +0000421 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100422 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000423 complete -F _complete_android_module_names outmod
424 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100425 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700426}
427
Matt Alexanderd9c56562020-05-21 10:49:17 +0000428function choosetype()
429{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700430 echo "Build type choices are:"
431 echo " 1. release"
432 echo " 2. debug"
433 echo
434
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800435 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700436 DEFAULT_NUM=1
437 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800439 export TARGET_BUILD_TYPE=
440 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700441 while [ -z $TARGET_BUILD_TYPE ]
442 do
443 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000444 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800445 read ANSWER
446 else
447 echo $1
448 ANSWER=$1
449 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450 case $ANSWER in
451 "")
452 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
453 ;;
454 1)
455 export TARGET_BUILD_TYPE=release
456 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800457 release)
458 export TARGET_BUILD_TYPE=release
459 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 2)
461 export TARGET_BUILD_TYPE=debug
462 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463 debug)
464 export TARGET_BUILD_TYPE=debug
465 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466 *)
467 echo
468 echo "I didn't understand your response. Please try again."
469 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700470 ;;
471 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000472 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800473 break
474 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 done
476
Ying Wang08800fd2016-03-03 20:57:21 -0800477 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800479 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700480}
481
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800482#
483# This function isn't really right: It chooses a TARGET_PRODUCT
484# based on the list of boards. Usually, that gets you something
485# that kinda works with a generic product, but really, you should
486# pick a product by name.
487#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000488function chooseproduct()
489{
Christopher Ferris55257d22017-03-23 11:08:58 -0700490 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000491 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492 default_value=$TARGET_PRODUCT
493 else
Ying Wang0a76df52015-06-08 11:57:26 -0700494 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700495 fi
496
Ying Wang08800fd2016-03-03 20:57:21 -0800497 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800498 export TARGET_PRODUCT=
499 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700500 while [ -z "$TARGET_PRODUCT" ]
501 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700502 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000503 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800504 read ANSWER
505 else
506 echo $1
507 ANSWER=$1
508 fi
509
Matt Alexanderd9c56562020-05-21 10:49:17 +0000510 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700511 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000513 if check_product $ANSWER
514 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800515 export TARGET_PRODUCT=$ANSWER
516 else
517 echo "** Not a valid product: $ANSWER"
518 fi
519 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000520 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800521 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700522 fi
523 done
524
Ying Wang08800fd2016-03-03 20:57:21 -0800525 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700526 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800527 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700528}
529
Matt Alexanderd9c56562020-05-21 10:49:17 +0000530function choosevariant()
531{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800532 echo "Variant choices are:"
533 local index=1
534 local v
535 for v in ${VARIANT_CHOICES[@]}
536 do
537 # The product name is the name of the directory containing
538 # the makefile we found, above.
539 echo " $index. $v"
540 index=$(($index+1))
541 done
542
543 local default_value=eng
544 local ANSWER
545
546 export TARGET_BUILD_VARIANT=
547 while [ -z "$TARGET_BUILD_VARIANT" ]
548 do
549 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000550 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800551 read ANSWER
552 else
553 echo $1
554 ANSWER=$1
555 fi
556
Matt Alexanderd9c56562020-05-21 10:49:17 +0000557 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000559 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
560 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200561 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562 fi
563 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000564 if check_variant $ANSWER
565 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 export TARGET_BUILD_VARIANT=$ANSWER
567 else
568 echo "** Not a valid variant: $ANSWER"
569 fi
570 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000571 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800572 break
573 fi
574 done
575}
576
Matt Alexanderd9c56562020-05-21 10:49:17 +0000577function choosecombo()
578{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700579 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700580
581 echo
582 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700583 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584
585 echo
586 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700587 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800588
589 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800590 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700591 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800593 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594}
595
Matt Alexanderd9c56562020-05-21 10:49:17 +0000596function add_lunch_combo()
597{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800598 if [ -n "$ZSH_VERSION" ]; then
599 echo -n "${funcfiletrace[1]}: "
600 else
601 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
602 fi
603 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 -0800604}
605
Matt Alexanderd9c56562020-05-21 10:49:17 +0000606function print_lunch_menu()
607{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100609 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700610 choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100611 local ret=$?
612
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700613 echo
614 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700615 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100616
Matt Alexanderd9c56562020-05-21 10:49:17 +0000617 if [ $ret -ne 0 ]
618 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100619 echo "Warning: Cannot display lunch menu."
620 echo
621 echo "Note: You can invoke lunch with an explicit target:"
622 echo
623 echo " usage: lunch [target]" >&2
624 echo
625 return
626 fi
627
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700628 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800629
630 local i=1
631 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700632 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633 do
634 echo " $i. $choice"
635 i=$(($i+1))
636 done
637
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700638 echo
639}
640
Matt Alexanderd9c56562020-05-21 10:49:17 +0000641function lunch()
642{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800643 local answer
644
Steven Moreland92793dc2020-02-25 18:30:18 -0800645 if [[ $# -gt 1 ]]; then
646 echo "usage: lunch [target]" >&2
647 return 1
648 fi
649
650 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800651 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700652 else
653 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700654 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800655 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700656 fi
657
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800658 local selection=
659
Matt Alexanderd9c56562020-05-21 10:49:17 +0000660 if [ -z "$answer" ]
661 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700662 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000663 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
664 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800665 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000666 if [ $answer -le ${#choices[@]} ]
667 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800668 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000669 if [ -n "$ZSH_VERSION" ]
670 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800671 selection=${choices[$(($answer))]}
672 else
673 selection=${choices[$(($answer-1))]}
674 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800675 fi
Colin Cross88737132017-03-21 17:41:03 -0700676 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800677 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700678 fi
679
Joe Onoratoda12daf2010-06-09 18:18:31 -0700680 export TARGET_BUILD_APPS=
681
Colin Cross88737132017-03-21 17:41:03 -0700682 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700683 product=${selection%%-*} # Trim everything after first dash
684 variant_and_version=${selection#*-} # Trim everything up to first dash
685 if [ "$variant_and_version" != "$selection" ]; then
686 variant=${variant_and_version%%-*}
687 if [ "$variant" != "$variant_and_version" ]; then
688 version=${variant_and_version#*-}
689 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700690 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800691
Matt Alexanderd9c56562020-05-21 10:49:17 +0000692 if [ -z "$product" ]
693 then
Ying Wang08800fd2016-03-03 20:57:21 -0800694 echo
Colin Cross88737132017-03-21 17:41:03 -0700695 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700696 return 1
697 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800698
Colin Cross88737132017-03-21 17:41:03 -0700699 TARGET_PRODUCT=$product \
700 TARGET_BUILD_VARIANT=$variant \
701 TARGET_PLATFORM_VERSION=$version \
702 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000703 if [ $? -ne 0 ]
704 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100705 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
706 then
707 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
708 fi
Colin Cross88737132017-03-21 17:41:03 -0700709 return 1
710 fi
Colin Cross88737132017-03-21 17:41:03 -0700711 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
712 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700713 if [ -n "$version" ]; then
714 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
715 else
716 unset TARGET_PLATFORM_VERSION
717 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700718 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700719
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700720 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800721
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700722 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700723 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800724 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700725}
726
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700727unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700728# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000729function _lunch()
730{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700731 local cur prev opts
732 COMPREPLY=()
733 cur="${COMP_WORDS[COMP_CWORD]}"
734 prev="${COMP_WORDS[COMP_CWORD-1]}"
735
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700736 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800737 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700738 fi
739
740 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700741 return 0
742}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700743
Joe Onoratoda12daf2010-06-09 18:18:31 -0700744# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700745# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000746function tapas()
747{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700748 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800749 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700750 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700751 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 -0800752 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 -0700753
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700754 if [ "$showHelp" != "" ]; then
755 $(gettop)/build/make/tapasHelp.sh
756 return
757 fi
758
Ying Wang67f02922012-08-22 10:25:20 -0700759 if [ $(echo $arch | wc -w) -gt 1 ]; then
760 echo "tapas: Error: Multiple build archs supplied: $arch"
761 return
762 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700763 if [ $(echo $variant | wc -w) -gt 1 ]; then
764 echo "tapas: Error: Multiple build variants supplied: $variant"
765 return
766 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700767 if [ $(echo $density | wc -w) -gt 1 ]; then
768 echo "tapas: Error: Multiple densities supplied: $density"
769 return
770 fi
Ying Wang67f02922012-08-22 10:25:20 -0700771
Ying Wang0a76df52015-06-08 11:57:26 -0700772 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700773 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000774 x86) product=aosp_x86;;
775 arm64) product=aosp_arm64;;
776 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700777 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700778 if [ -z "$variant" ]; then
779 variant=eng
780 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700781 if [ -z "$apps" ]; then
782 apps=all
783 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600784 if [ -z "$density" ]; then
785 density=alldpi
786 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700787
Ying Wang67f02922012-08-22 10:25:20 -0700788 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700789 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700790 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700791 export TARGET_BUILD_TYPE=release
792 export TARGET_BUILD_APPS=$apps
793
Ying Wang08800fd2016-03-03 20:57:21 -0800794 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700795 set_stuff_for_environment
796 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800797 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700798}
799
Martin Stjernholmf692c752021-04-12 00:01:10 +0100800# Configures the build to build unbundled Android modules (APEXes).
801# Run banchan with one or more module names (from apex{} modules).
802function banchan()
803{
804 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100805 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100806 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100807 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|(.*_)?(arm|x86|arm64|x86_64))$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100808
809 if [ "$showHelp" != "" ]; then
810 $(gettop)/build/make/banchanHelp.sh
811 return
812 fi
813
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100814 if [ -z "$product" ]; then
815 product=arm
816 elif [ $(echo $product | wc -w) -gt 1 ]; then
817 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100818 return
819 fi
820 if [ $(echo $variant | wc -w) -gt 1 ]; then
821 echo "banchan: Error: Multiple build variants supplied: $variant"
822 return
823 fi
824 if [ -z "$apps" ]; then
825 echo "banchan: Error: No modules supplied"
826 return
827 fi
828
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100829 case $product in
830 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100831 x86) product=module_x86;;
832 arm64) product=module_arm64;;
833 x86_64) product=module_x86_64;;
834 esac
835 if [ -z "$variant" ]; then
836 variant=eng
837 fi
838
839 export TARGET_PRODUCT=$product
840 export TARGET_BUILD_VARIANT=$variant
841 export TARGET_BUILD_DENSITY=alldpi
842 export TARGET_BUILD_TYPE=release
843
844 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
845 # case is different and it may diverge in the future.
846 export TARGET_BUILD_APPS=$apps
847
848 build_build_var_cache
849 set_stuff_for_environment
850 printconfig
851 destroy_build_var_cache
852}
853
Matt Alexanderd9c56562020-05-21 10:49:17 +0000854function gettop
855{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700856 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000857 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700858 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000859 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700860 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000861 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800862 # The following circumlocution (repeated below as well) ensures
863 # that we record the true directory name and not one that is
864 # faked up with symlink names.
865 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700866 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800867 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700868 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000869 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800870 \cd ..
synergyb112a402013-07-05 19:47:47 -0700871 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700872 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000873 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700874 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000875 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700876 fi
877 fi
878 fi
879}
880
Matt Alexanderd9c56562020-05-21 10:49:17 +0000881function croot()
882{
Christopher Ferris55257d22017-03-23 11:08:58 -0700883 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700884 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700885 if [ "$1" ]; then
886 \cd $(gettop)/$1
887 else
888 \cd $(gettop)
889 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700890 else
891 echo "Couldn't locate the top of the tree. Try setting TOP."
892 fi
893}
894
Matt Alexanderd9c56562020-05-21 10:49:17 +0000895function _croot()
896{
Anton Hanssonece9c482019-02-04 18:15:39 +0000897 local T=$(gettop)
898 if [ "$T" ]; then
899 local cur="${COMP_WORDS[COMP_CWORD]}"
900 k=0
901 for c in $(compgen -d ${T}/${cur}); do
902 COMPREPLY[k++]=${c#${T}/}/
903 done
904 fi
905}
906
Matt Alexanderd9c56562020-05-21 10:49:17 +0000907function cproj()
908{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700909 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700910 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700911 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700912 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
913 T=$PWD
914 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800915 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700916 return
917 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800918 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700919 done
Ying Wang9cd17642012-12-13 10:52:07 -0800920 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700921 echo "can't find Android.mk"
922}
923
Daniel Sandler47e0a882013-07-30 13:23:52 -0400924# simplified version of ps; output in the form
925# <pid> <procname>
926function qpid() {
927 local prepend=''
928 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000929 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400930 prepend=' '
931 append='$'
932 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000933 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800934 echo "usage: qpid [[--exact] <process name|pid>"
935 return 255
936 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400937
938 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000939 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800940 qpid | \grep "$prepend$EXE$append"
941 else
942 adb shell ps \
943 | tr -d '\r' \
944 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
945 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400946}
947
Steven Moreland74114f12020-09-10 01:23:32 +0000948# syswrite - disable verity, reboot if needed, and remount image
949#
950# Easy way to make system.img/etc writable
951function syswrite() {
952 adb wait-for-device && adb root || return 1
953 if [[ $(adb disable-verity | grep "reboot") ]]; then
954 echo "rebooting"
955 adb reboot && adb wait-for-device && adb root || return 1
956 fi
957 adb wait-for-device && adb remount || return 1
958}
959
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800960# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700961# that has the core-file-size limit set correctly
962#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800963# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700964# if its core-file-size limit is not set already.
965# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
966
Matt Alexanderd9c56562020-05-21 10:49:17 +0000967function coredump_setup()
968{
969 echo "Getting root...";
970 adb root;
971 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700972
Matt Alexanderd9c56562020-05-21 10:49:17 +0000973 echo "Remounting root partition read-write...";
974 adb shell mount -w -o remount -t rootfs rootfs;
975 sleep 1;
976 adb wait-for-device;
977 adb shell mkdir -p /cores;
978 adb shell mount -t tmpfs tmpfs /cores;
979 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700980
Matt Alexanderd9c56562020-05-21 10:49:17 +0000981 echo "Granting SELinux permission to dump in /cores...";
982 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700983
Matt Alexanderd9c56562020-05-21 10:49:17 +0000984 echo "Set core pattern.";
985 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700986
Ying Wang08800fd2016-03-03 20:57:21 -0800987 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700988}
989
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800990# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000991# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700992#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800993# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700994# dump to actually be generated.
995
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996function coredump_enable()
997{
998 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800999 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001000 printf "Expecting a PID!\n";
1001 return;
1002 fi;
1003 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001004 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001005}
1006
1007# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001008# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001009#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001010# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001011# enabled globally.
1012
Matt Alexanderd9c56562020-05-21 10:49:17 +00001013function core()
1014{
1015 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001016
Ying Wang08800fd2016-03-03 20:57:21 -08001017 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001018 printf "Expecting a PID!\n";
1019 return;
1020 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001021
Matt Alexanderd9c56562020-05-21 10:49:17 +00001022 local CORENAME=core.$PID;
1023 local COREPATH=/cores/$CORENAME;
1024 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001025
Matt Alexanderd9c56562020-05-21 10:49:17 +00001026 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001027
Matt Alexanderd9c56562020-05-21 10:49:17 +00001028 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001029 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001030 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1031 adb shell kill -$SIG $PID;
1032 sleep 1;
1033 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001034
Matt Alexanderd9c56562020-05-21 10:49:17 +00001035 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1036 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001037}
1038
Christopher Tate744ee802009-11-12 15:33:08 -08001039# systemstack - dump the current stack trace of all threads in the system process
1040# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001041function systemstack()
1042{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001043 stacks system_server
1044}
1045
Michael Wrightaeed7212014-06-19 19:58:12 -07001046# Read the ELF header from /proc/$PID/exe to determine if the process is
1047# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001048function is64bit()
1049{
Ben Chengfba67bf2014-02-25 10:27:07 -08001050 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001051 if [ "$PID" ] ; then
1052 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001053 echo "64"
1054 else
1055 echo ""
1056 fi
1057 else
1058 echo ""
1059 fi
1060}
1061
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001062case `uname -s` in
1063 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001064 function sgrep()
1065 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001066 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 -04001067 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001068 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001069
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001070 ;;
1071 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001072 function sgrep()
1073 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001074 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 -04001075 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001076 }
1077 ;;
1078esac
1079
Matt Alexanderd9c56562020-05-21 10:49:17 +00001080function gettargetarch
1081{
Raghu Gandham8da43102012-07-25 19:57:22 -07001082 get_build_var TARGET_ARCH
1083}
1084
Matt Alexanderd9c56562020-05-21 10:49:17 +00001085function ggrep()
1086{
Mike Frysinger5e479732015-09-22 18:13:48 -04001087 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1088 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001089}
1090
Matt Alexanderd9c56562020-05-21 10:49:17 +00001091function gogrep()
1092{
Orion Hodson831472d2019-10-25 11:35:15 +01001093 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1094 -exec grep --color -n "$@" {} +
1095}
1096
Matt Alexanderd9c56562020-05-21 10:49:17 +00001097function jgrep()
1098{
Mike Frysinger5e479732015-09-22 18:13:48 -04001099 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1100 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001101}
1102
Alistair Delva176e5342021-02-22 13:31:26 -08001103function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001104{
1105 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1106 -exec grep --color -n "$@" {} +
1107}
1108
Taesu Leeea0cecd2020-10-28 11:05:18 +09001109function ktgrep()
1110{
1111 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1112 -exec grep --color -n "$@" {} +
1113}
1114
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115function cgrep()
1116{
Mike Frysinger5e479732015-09-22 18:13:48 -04001117 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' \) \
1118 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001119}
1120
Matt Alexanderd9c56562020-05-21 10:49:17 +00001121function resgrep()
1122{
Christopher Ferris55257d22017-03-23 11:08:58 -07001123 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001124 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1125 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1126 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001127}
1128
Matt Alexanderd9c56562020-05-21 10:49:17 +00001129function mangrep()
1130{
Mike Frysinger5e479732015-09-22 18:13:48 -04001131 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1132 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001133}
1134
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135function owngrep()
1136{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001137 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1138 -exec grep --color -n "$@" {} +
1139}
1140
Matt Alexanderd9c56562020-05-21 10:49:17 +00001141function sepgrep()
1142{
Mike Frysinger5e479732015-09-22 18:13:48 -04001143 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1144 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001145}
1146
Matt Alexanderd9c56562020-05-21 10:49:17 +00001147function rcgrep()
1148{
Mike Frysinger5e479732015-09-22 18:13:48 -04001149 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1150 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001151}
1152
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001153case `uname -s` in
1154 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001155 function mgrep()
1156 {
Orion Hodson831472d2019-10-25 11:35:15 +01001157 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 -04001158 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001159 }
1160
Matt Alexanderd9c56562020-05-21 10:49:17 +00001161 function treegrep()
1162 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001163 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 -04001164 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001165 }
1166
1167 ;;
1168 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001169 function mgrep()
1170 {
Orion Hodson831472d2019-10-25 11:35:15 +01001171 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 -04001172 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001173 }
1174
Matt Alexanderd9c56562020-05-21 10:49:17 +00001175 function treegrep()
1176 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001177 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 -04001178 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001179 }
1180
1181 ;;
1182esac
1183
Matt Alexanderd9c56562020-05-21 10:49:17 +00001184function getprebuilt
1185{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001186 get_abs_build_var ANDROID_PREBUILTS
1187}
1188
Matt Alexanderd9c56562020-05-21 10:49:17 +00001189function tracedmdump()
1190{
Christopher Ferris55257d22017-03-23 11:08:58 -07001191 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001192 if [ ! "$T" ]; then
1193 echo "Couldn't locate the top of the tree. Try setting TOP."
1194 return
1195 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001196 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001197 local arch=$(gettargetarch)
1198 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001199
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001200 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001201 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001202 echo "usage: tracedmdump tracename"
1203 return
1204 fi
1205
Matt Alexanderd9c56562020-05-21 10:49:17 +00001206 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001207 echo "Error: cannot find kernel: '$KERNEL'"
1208 return
1209 fi
1210
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001211 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001212 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001213 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1214 fi
1215
1216 echo "post-processing traces..."
1217 rm -f $TRACE/qtrace.dexlist
1218 post_trace $TRACE
1219 if [ $? -ne 0 ]; then
1220 echo "***"
1221 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1222 echo "***"
1223 return
1224 fi
1225 echo "generating dexlist output..."
1226 /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
1227 echo "generating dmtrace data..."
1228 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1229 echo "generating html file..."
1230 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1231 echo "done, see $TRACE/dmtrace.html for details"
1232 echo "or run:"
1233 echo " traceview $TRACE/dmtrace"
1234}
1235
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001236# communicate with a running device or emulator, set up necessary state,
1237# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001238function runhat()
1239{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001240 # process standard adb options
1241 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001243 adbTarget=$1
1244 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001245 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001246 adbTarget="$1 $2"
1247 shift 2
1248 fi
1249 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001250 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001251
1252 # runhat options
1253 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001254
Matt Alexanderd9c56562020-05-21 10:49:17 +00001255 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001256 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001257 return
1258 fi
1259
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001260 # confirm hat is available
1261 if [ -z $(which hat) ]; then
1262 echo "hat is not available in this configuration."
1263 return
1264 fi
1265
Andy McFaddenb6289852010-07-12 08:00:19 -07001266 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001267 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001268 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001269 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001270 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001271 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001272 echo -n "> "
1273 read
1274
The Android Open Source Project88b60792009-03-03 19:28:42 -08001275 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001276
The Android Open Source Project88b60792009-03-03 19:28:42 -08001277 echo "Retrieving file $devFile..."
1278 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001279
The Android Open Source Project88b60792009-03-03 19:28:42 -08001280 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001281
The Android Open Source Project88b60792009-03-03 19:28:42 -08001282 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001283 echo "View the output by pointing your browser at http://localhost:7000/"
1284 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001285 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286}
1287
Matt Alexanderd9c56562020-05-21 10:49:17 +00001288function getbugreports()
1289{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001290 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001291
1292 if [ ! "$reports" ]; then
1293 echo "Could not locate any bugreports."
1294 return
1295 fi
1296
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001297 local report
1298 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001299 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001300 echo "/sdcard/bugreports/${report}"
1301 adb pull /sdcard/bugreports/${report} ${report}
1302 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001303 done
1304}
1305
Matt Alexanderd9c56562020-05-21 10:49:17 +00001306function getsdcardpath()
1307{
Victoria Lease1b296b42012-08-21 15:44:06 -07001308 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1309}
1310
Matt Alexanderd9c56562020-05-21 10:49:17 +00001311function getscreenshotpath()
1312{
Victoria Lease1b296b42012-08-21 15:44:06 -07001313 echo "$(getsdcardpath)/Pictures/Screenshots"
1314}
1315
Matt Alexanderd9c56562020-05-21 10:49:17 +00001316function getlastscreenshot()
1317{
Victoria Lease1b296b42012-08-21 15:44:06 -07001318 local screenshot_path=$(getscreenshotpath)
1319 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 +00001320 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001321 echo "No screenshots found."
1322 return
1323 fi
1324 echo "${screenshot}"
1325 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1326}
1327
Matt Alexanderd9c56562020-05-21 10:49:17 +00001328function startviewserver()
1329{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001330 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001331 if [ $# -gt 0 ]; then
1332 port=$1
1333 fi
1334 adb shell service call window 1 i32 $port
1335}
1336
Matt Alexanderd9c56562020-05-21 10:49:17 +00001337function stopviewserver()
1338{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001339 adb shell service call window 2
1340}
1341
Matt Alexanderd9c56562020-05-21 10:49:17 +00001342function isviewserverstarted()
1343{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001344 adb shell service call window 3
1345}
1346
Matt Alexanderd9c56562020-05-21 10:49:17 +00001347function key_home()
1348{
Romain Guyb84049a2010-10-04 16:56:11 -07001349 adb shell input keyevent 3
1350}
1351
Matt Alexanderd9c56562020-05-21 10:49:17 +00001352function key_back()
1353{
Romain Guyb84049a2010-10-04 16:56:11 -07001354 adb shell input keyevent 4
1355}
1356
Matt Alexanderd9c56562020-05-21 10:49:17 +00001357function key_menu()
1358{
Romain Guyb84049a2010-10-04 16:56:11 -07001359 adb shell input keyevent 82
1360}
1361
Matt Alexanderd9c56562020-05-21 10:49:17 +00001362function smoketest()
1363{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001364 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1365 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1366 return
1367 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001368 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001369 if [ ! "$T" ]; then
1370 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1371 return
1372 fi
1373
Ying Wang9cd17642012-12-13 10:52:07 -08001374 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001375 adb uninstall com.android.smoketest > /dev/null &&
1376 adb uninstall com.android.smoketest.tests > /dev/null &&
1377 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1378 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1379 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1380}
1381
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001382# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001383function runtest()
1384{
Christopher Ferris55257d22017-03-23 11:08:58 -07001385 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001386 if [ ! "$T" ]; then
1387 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1388 return
1389 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001390 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001391}
1392
The Android Open Source Project88b60792009-03-03 19:28:42 -08001393function godir () {
1394 if [[ -z "$1" ]]; then
1395 echo "Usage: godir <regex>"
1396 return
1397 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001398 local T=$(gettop)
1399 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001400 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001401 mkdir -p $OUT_DIR
1402 FILELIST=$OUT_DIR/filelist
1403 else
1404 FILELIST=$T/filelist
1405 fi
1406 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001407 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001408 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001409 echo " Done"
1410 echo ""
1411 fi
1412 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001413 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001414 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001415 echo "Not found"
1416 return
1417 fi
1418 local pathname
1419 local choice
1420 if [[ ${#lines[@]} > 1 ]]; then
1421 while [[ -z "$pathname" ]]; do
1422 local index=1
1423 local line
1424 for line in ${lines[@]}; do
1425 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001426 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001427 done
1428 echo
1429 echo -n "Select one: "
1430 unset choice
1431 read choice
1432 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1433 echo "Invalid choice"
1434 continue
1435 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001436 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001437 done
1438 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001439 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001440 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001441 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001442}
1443
Steven Moreland62054a42018-12-06 10:11:40 -08001444# Update module-info.json in out.
1445function refreshmod() {
1446 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1447 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1448 return 1
1449 fi
1450
1451 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1452
1453 # for the output of the next command
1454 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1455
1456 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001457 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001458 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1459}
1460
Cole Faust3e192382021-10-25 13:29:15 -07001461# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001462function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001463 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001464 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1465 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1466 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001467 return 1
1468 fi
1469
1470 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001471 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001472 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001473 fi
1474 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001475 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001476}
1477
1478# List all modules for the current device, as cached in module-info.json. If any build change is
1479# made and it should be reflected in the output, you should run 'refreshmod' first.
1480function allmod() {
1481 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001482
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001483 python3 -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
Steven Moreland62054a42018-12-06 10:11:40 -08001484}
1485
Joe Onorato2c1aa472021-02-25 16:42:39 -08001486# Get the path of a specific module in the android tree, as cached in module-info.json.
1487# If any build change is made, and it should be reflected in the output, you should run
1488# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001489function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001490 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001491 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001492 return 1
1493 fi
1494
Cole Faust24c36db2021-01-23 02:39:37 +00001495 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001496
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001497 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001498module = '$1'
1499module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1500if module not in module_info:
1501 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001502print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001503
1504 if [ -z "$relpath" ]; then
1505 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1506 return 1
1507 else
Rett Berg78d1c932019-01-24 14:34:23 -08001508 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001509 fi
1510}
1511
Joe Onorato2c1aa472021-02-25 16:42:39 -08001512# Get the path of a specific module in the android tree, as cached in module-info.json.
1513# If any build change is made, and it should be reflected in the output, you should run
1514# 'refreshmod' first. Note: This is the inverse of pathmod.
1515function dirmods() {
1516 if [[ $# -ne 1 ]]; then
1517 echo "usage: dirmods <path>" >&2
1518 return 1
1519 fi
1520
1521 verifymodinfo || return 1
1522
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001523 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001524dir = '$1'
1525while dir.endswith('/'):
1526 dir = dir[:-1]
1527prefix = dir + '/'
1528module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1529results = set()
1530for m in module_info.values():
1531 for path in m.get(u'path', []):
1532 if path == dir or path.startswith(prefix):
1533 name = m.get(u'module_name')
1534 if name:
1535 results.add(name)
1536for name in sorted(results):
1537 print(name)
1538"
1539}
1540
1541
Rett Berg78d1c932019-01-24 14:34:23 -08001542# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1543# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1544function gomod() {
1545 if [[ $# -ne 1 ]]; then
1546 echo "usage: gomod <module>" >&2
1547 return 1
1548 fi
1549
1550 local path="$(pathmod $@)"
1551 if [ -z "$path" ]; then
1552 return 1
1553 fi
1554 cd $path
1555}
1556
Cole Faust24c36db2021-01-23 02:39:37 +00001557# Gets the list of a module's installed outputs, as cached in module-info.json.
1558# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1559function outmod() {
1560 if [[ $# -ne 1 ]]; then
1561 echo "usage: outmod <module>" >&2
1562 return 1
1563 fi
1564
1565 verifymodinfo || return 1
1566
1567 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001568 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001569module = '$1'
1570module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1571if module not in module_info:
1572 exit(1)
1573for output in module_info[module]['installed']:
1574 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1575
1576 if [ $? -ne 0 ]; then
1577 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1578 return 1
1579 elif [ ! -z "$relpath" ]; then
1580 echo "$relpath"
1581 fi
1582}
1583
1584# adb install a module's apk, as cached in module-info.json. If any build change
1585# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1586# Usage: installmod [adb install arguments] <module>
1587# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1588function installmod() {
1589 if [[ $# -eq 0 ]]; then
1590 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001591 echo "" >&2
1592 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1593 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1594 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001595 return 1
1596 fi
1597
1598 local _path
1599 _path=$(outmod ${@:$#:1})
1600 if [ $? -ne 0 ]; then
1601 return 1
1602 fi
1603
1604 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1605 if [ -z "$_path" ]; then
1606 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1607 return 1
1608 fi
Cole Faust3e192382021-10-25 13:29:15 -07001609 local serial_device=""
1610 if [[ "$1" == "-s" ]]; then
1611 if [[ $# -le 2 ]]; then
1612 echo "-s requires an argument" >&2
1613 return 1
1614 fi
1615 serial_device="-s $2"
1616 shift 2
1617 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001618 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001619 echo adb $serial_device install ${@:1:$length} $_path
1620 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001621}
1622
dimitry73b84812018-12-11 18:06:00 +01001623function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001624 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001625 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001626}
1627
Alex Rayf0d08eb2013-03-08 15:15:06 -08001628# Print colored exit condition
1629function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001630 "$@"
1631 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001632 if [ $retval -ne 0 ]
1633 then
Jacky Cao89483b82015-05-15 22:12:53 +08001634 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001635 else
Jacky Cao89483b82015-05-15 22:12:53 +08001636 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001637 fi
1638 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001639}
1640
Matt Alexanderd9c56562020-05-21 10:49:17 +00001641function get_make_command()
1642{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001643 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1644 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001645 # Always use the real make if -C is passed in
1646 for arg in "$@"; do
1647 if [[ $arg == -C* ]]; then
1648 echo command make
1649 return
1650 fi
1651 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001652 echo build/soong/soong_ui.bash --make-mode
1653 else
1654 echo command make
1655 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001656}
1657
Matt Alexanderd9c56562020-05-21 10:49:17 +00001658function _wrap_build()
1659{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001660 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1661 "$@"
1662 return $?
1663 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001664 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001665 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001666 local ret=$?
1667 local end_time=$(date +"%s")
1668 local tdiff=$(($end_time-$start_time))
1669 local hours=$(($tdiff / 3600 ))
1670 local mins=$((($tdiff % 3600) / 60))
1671 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001672 local ncolors=$(tput colors 2>/dev/null)
1673 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001674 color_failed=$'\E'"[0;31m"
1675 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001676 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001677 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001678 else
1679 color_failed=""
1680 color_success=""
1681 color_reset=""
1682 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001683
1684 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1685 echo
1686 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1687 fi
1688
Ed Heylcc6be0a2014-06-18 14:55:58 -07001689 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001690 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001691 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001692 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001693 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001694 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001695 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001696 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001697 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001698 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001699 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001700 printf "(%s seconds)" $secs
1701 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001702 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001703 echo
1704 return $ret
1705}
1706
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001707function _trigger_build()
1708(
1709 local -r bc="$1"; shift
1710 if T="$(gettop)"; then
1711 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1712 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001713 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1714 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001715 fi
1716)
1717
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001718# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001719function b()
1720(
1721 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Lukacs T. Berki3fef6602021-09-07 08:18:35 +02001722 _trigger_build "all-modules" bp2build USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001723 # Then, run Bazel using the synthetic workspace as the --package_path.
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001724 if [[ -z "$@" ]]; then
1725 # If there are no args, show help.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001726 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001727 else
1728 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001729 bazel "$@" --config=bp2build
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001730 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001731)
1732
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001733function m()
1734(
1735 _trigger_build "all-modules" "$@"
1736)
1737
1738function mm()
1739(
1740 _trigger_build "modules-in-a-dir-no-deps" "$@"
1741)
1742
1743function mmm()
1744(
1745 _trigger_build "modules-in-dirs-no-deps" "$@"
1746)
1747
1748function mma()
1749(
1750 _trigger_build "modules-in-a-dir" "$@"
1751)
1752
1753function mmma()
1754(
1755 _trigger_build "modules-in-dirs" "$@"
1756)
1757
Matt Alexanderd9c56562020-05-21 10:49:17 +00001758function make()
1759{
Dan Willemsene9842242017-07-28 13:00:13 -07001760 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001761}
1762
Matt Alexanderd9c56562020-05-21 10:49:17 +00001763function provision()
1764{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001765 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1766 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1767 return 1
1768 fi
1769 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1770 echo "There is no provisioning script for the device." >&2
1771 return 1
1772 fi
1773
1774 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001775 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001776 shift 1
1777 else
1778 echo "This action will reflash your device."
1779 echo ""
1780 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1781 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001782 echo -n "Are you sure you want to do this (yes/no)? "
1783 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001784 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001785 echo "Not taking any action. Exiting." >&2
1786 return 1
1787 fi
1788 fi
1789 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1790}
1791
Jim Tanga881a252018-06-19 16:34:41 +08001792# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001793function enable_zsh_completion() {
1794 # Don't override user's options if bash-style completion is already enabled.
1795 if ! declare -f complete >/dev/null; then
1796 autoload -U compinit && compinit
1797 autoload -U bashcompinit && bashcompinit
1798 fi
Jim Tanga881a252018-06-19 16:34:41 +08001799}
1800
1801function validate_current_shell() {
1802 local current_sh="$(ps -o command -p $$)"
1803 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001804 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001805 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001806 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001807 *zsh*)
1808 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001809 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001810 *)
Jim Tanga881a252018-06-19 16:34:41 +08001811 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 -07001812 ;;
1813 esac
Jim Tanga881a252018-06-19 16:34:41 +08001814}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001815
1816# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001817# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1818# load those.
1819#
1820# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001821function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001822 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001823 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001824 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001825 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 -08001826 if [ -n "$allowed" ]; then
1827 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1828 echo " $allowed"
1829 echo " $f"
1830 return
1831 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001832 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001833 done
1834
1835 allowed_files=
1836 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001837 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001838 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001839 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001840
1841 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001842 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001843 else
1844 echo "ignoring $f, not in $allowed"
1845 fi
Jim Tanga881a252018-06-19 16:34:41 +08001846 done
1847 done
1848}
Kenny Root52aa81c2011-07-15 11:07:06 -07001849
Dan Albertbab814f2020-08-26 15:34:53 -07001850function showcommands() {
1851 local T=$(gettop)
1852 if [[ -z "$TARGET_PRODUCT" ]]; then
1853 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1854 return
1855 fi
1856 case $(uname -s) in
1857 Darwin)
1858 PREBUILT_NAME=darwin-x86
1859 ;;
1860 Linux)
1861 PREBUILT_NAME=linux-x86
1862 ;;
1863 *)
1864 >&2 echo Unknown host $(uname -s)
1865 return
1866 ;;
1867 esac
1868 if [[ -z "$OUT_DIR" ]]; then
1869 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
1870 OUT_DIR=out
1871 else
1872 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
1873 fi
1874 fi
1875 if [[ "$1" == "--regenerate" ]]; then
1876 shift 1
1877 NINJA_ARGS="-t commands $@" m
1878 else
1879 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1880 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1881 -t commands "$@")
1882 fi
1883}
1884
Jim Tanga881a252018-06-19 16:34:41 +08001885validate_current_shell
1886source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001887addcompletions