blob: 9e7e6f5d81e0ce9fcc6de0f613978c487d7988af [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).
Anton Hansson90ac61c2022-09-06 14:36:00 +000013- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64|arm64_only|x86_64only] \
14 [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010015 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000016- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080017- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070018- mm: Builds and installs all of the modules in the current directory, and their
19 dependencies.
20- mmm: Builds and installs all of the modules in the supplied directories, and their
21 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080022 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070023- mma: Same as 'mm'
24- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080025- provision: Flash device with all required partitions. Options will be passed on to fastboot.
26- cgrep: Greps on all local C/C++ files.
27- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010028- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080029- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090030- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080031- resgrep: Greps on all local res/*.xml files.
32- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070033- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060034- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080035- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080036- sepgrep: Greps on all local sepolicy files.
37- sgrep: Greps on all local source files.
38- godir: Go to the directory containing a file.
39- allmod: List all modules.
40- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000041- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080042- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000043- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080044- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000045- installmod: Adb installs a module's built APK.
46- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000047- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070048
Roland Levillain39341922015-10-20 12:48:19 +010049Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070050- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080051- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070052
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070053Look at the source to view more functions. The complete list is:
54EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070055 local T=$(gettop)
56 local A=""
57 local i
Jacky Cao89483b82015-05-15 22:12:53 +080058 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 -070059 A="$A $i"
60 done
61 echo $A
62}
63
Ying Wang08800fd2016-03-03 20:57:21 -080064# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000065function build_build_var_cache()
66{
Christopher Ferris55257d22017-03-23 11:08:58 -070067 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080068 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080069 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' ' '`)
70 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 -080071 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080072 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080073 --vars="${cached_vars[*]}" \
74 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070075 --var-prefix=var_cache_ \
76 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080077 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000078 if [ $ret -ne 0 ]
79 then
Ying Wang08800fd2016-03-03 20:57:21 -080080 unset build_dicts_script
81 return $ret
82 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070083 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080084 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080085 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080086 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000087 if [ $ret -ne 0 ]
88 then
Ying Wang08800fd2016-03-03 20:57:21 -080089 return $ret
90 fi
91 BUILD_VAR_CACHE_READY="true"
92}
93
Ying Wangf0cb3972016-03-04 13:56:23 -080094# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080095# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000096function destroy_build_var_cache()
97{
Ying Wang08800fd2016-03-03 20:57:21 -080098 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070099 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800100 for v in $cached_vars; do
101 unset var_cache_$v
102 done
103 unset cached_vars
104 for v in $cached_abs_vars; do
105 unset abs_var_cache_$v
106 done
107 unset cached_abs_vars
108}
109
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700110# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000111function get_abs_build_var()
112{
113 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
114 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800115 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200116 return
Ying Wang08800fd2016-03-03 20:57:21 -0800117 fi
118
Christopher Ferris55257d22017-03-23 11:08:58 -0700119 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700120 if [ ! "$T" ]; then
121 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
122 return
123 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700124 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700125}
126
127# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000128function get_build_var()
129{
130 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
131 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800132 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100133 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800134 fi
135
Christopher Ferris55257d22017-03-23 11:08:58 -0700136 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700137 if [ ! "$T" ]; then
138 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100139 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700140 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700141 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142}
143
144# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000145function check_product()
146{
Christopher Ferris55257d22017-03-23 11:08:58 -0700147 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800148 if [ ! "$T" ]; then
149 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
150 return
151 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700152 TARGET_PRODUCT=$1 \
153 TARGET_BUILD_VARIANT= \
154 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700155 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800156 get_build_var TARGET_DEVICE > /dev/null
157 # hide successful answers, but allow the errors to show
158}
159
160VARIANT_CHOICES=(user userdebug eng)
161
162# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000163function check_variant()
164{
Christopher Ferris55257d22017-03-23 11:08:58 -0700165 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800166 for v in ${VARIANT_CHOICES[@]}
167 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000168 if [ "$v" = "$1" ]
169 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800170 return 0
171 fi
172 done
173 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700174}
175
Matt Alexanderd9c56562020-05-21 10:49:17 +0000176function setpaths()
177{
Christopher Ferris55257d22017-03-23 11:08:58 -0700178 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700179 if [ ! "$T" ]; then
180 echo "Couldn't locate the top of the tree. Try setting TOP."
181 return
182 fi
183
184 ##################################################################
185 # #
186 # Read me before you modify this code #
187 # #
188 # This function sets ANDROID_BUILD_PATHS to what it is adding #
189 # to PATH, and the next time it is run, it removes that from #
190 # PATH. This is required so lunch can be run more than once #
191 # and still have working paths. #
192 # #
193 ##################################################################
194
Raphael Mollc639c782011-06-20 17:25:01 -0700195 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
196 # due to "C:\Program Files" being in the path.
197
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000199 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700200 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
201 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000202 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700203 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800204 # strip leading ':', if any
205 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500206 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700207
208 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700209 local prebuiltdir=$(getprebuilt)
210 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700211
Ben Cheng8bc4c432012-11-16 13:29:13 -0800212 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700213 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
214 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800215 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800216
Raphael Mollc639c782011-06-20 17:25:01 -0700217 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800218 export ANDROID_TOOLCHAIN=
219 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700221 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200222 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700224 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000225 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400226 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000227 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200228 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000229 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
230 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700231 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200232 *)
233 echo "Can't find toolchain for unknown architecture: $ARCH"
234 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700235 ;;
236 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700237 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800238 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700239 fi
Raphael732936d2011-06-22 14:35:32 -0700240
Christopher Ferris55257d22017-03-23 11:08:58 -0700241 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800242 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
243 fi
244
Dan Willemsen838dcec2021-09-30 22:40:34 +0000245 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700246
247 # add kernel specific binaries
248 case $(uname -s) in
249 Linux)
250 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
251 ;;
252 *)
253 ;;
254 esac
255
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400256 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
Martin Stjernholm6a012262021-11-24 14:56:15 +0000257 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
Yi Kongdfd00b12019-05-21 16:00:04 -0700258 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
259
260 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
261 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
262 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200263
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800264 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
265 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
266
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200267 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
268 # to ensure that the corresponding 'emulator' binaries are used.
269 case $(uname -s) in
270 Darwin)
271 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
272 ;;
273 Linux)
274 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
275 ;;
276 *)
277 ANDROID_EMULATOR_PREBUILTS=
278 ;;
279 esac
280 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700281 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200282 export ANDROID_EMULATOR_PREBUILTS
283 fi
284
Jim Tangb3fda302018-12-22 10:24:55 +0800285 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
286 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700287 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
288 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
289 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000290 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800291
Martin Stjernholm6a012262021-11-24 14:56:15 +0000292 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700293 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800294
295 # out with the duplicate old
296 if [ -n $ANDROID_PYTHONPATH ]; then
297 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
298 fi
299 # and in with the new
300 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800301 if [ -n $VENDOR_PYTHONPATH ]; then
302 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
303 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800304 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800305
Colin Crosse97e6932017-06-30 16:01:45 -0700306 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
307 export JAVA_HOME=$ANDROID_JAVA_HOME
308 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
309 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
310 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500311
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800312 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700313 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
314 export OUT=$ANDROID_PRODUCT_OUT
315
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700316 unset ANDROID_HOST_OUT
317 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
318
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900319 unset ANDROID_SOONG_HOST_OUT
320 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
321
Simran Basidd050ed2017-02-13 13:46:48 -0800322 unset ANDROID_HOST_OUT_TESTCASES
323 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
324
325 unset ANDROID_TARGET_OUT_TESTCASES
326 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
327
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800328 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700329 # TODO: fix the path
330 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
331}
332
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000333function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000334{
Jingwen Chen17d56992021-06-10 08:17:54 +0000335 if which bazel &>/dev/null; then
336 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
337 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000338 fi
339
Jingwen Chen17d56992021-06-10 08:17:54 +0000340 local T="$(gettop)"
341 if [ ! "$T" ]; then
342 >&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."
343 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000344 fi
345
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000346 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000347}
348
Matt Alexanderd9c56562020-05-21 10:49:17 +0000349function printconfig()
350{
Christopher Ferris55257d22017-03-23 11:08:58 -0700351 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800352 if [ ! "$T" ]; then
353 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
354 return
355 fi
356 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700357}
358
Matt Alexanderd9c56562020-05-21 10:49:17 +0000359function set_stuff_for_environment()
360{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800361 setpaths
362 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700363
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800364 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700365 # With this environment variable new GCC can apply colors to warnings/errors
366 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 -0700367}
368
Matt Alexanderd9c56562020-05-21 10:49:17 +0000369function set_sequence_number()
370{
Colin Cross88737132017-03-21 17:41:03 -0700371 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700372}
373
Makoto Onukida971062018-06-18 10:15:19 -0700374# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
375function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800376 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700377 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800378 *:"$cmd":*)
379 return 1
380 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700381 esac
382 return 0
383}
384
Matt Alexanderd9c56562020-05-21 10:49:17 +0000385function addcompletions()
386{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700387 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700388
Jim Tanga881a252018-06-19 16:34:41 +0800389 # Keep us from trying to run in something that's neither bash nor zsh.
390 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700391 return
392 fi
393
394 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800395 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700396 return
397 fi
398
Jim Tanga881a252018-06-19 16:34:41 +0800399 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000400 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800401 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800402 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400403 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800404 )
Makoto Onukida971062018-06-18 10:15:19 -0700405 # Completion can be disabled selectively to allow users to use non-standard completion.
406 # e.g.
407 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
408 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400409 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800410 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400411 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000412 if [ ! -f "$f" ]; then
413 echo "Warning: completion file $f not found"
414 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700415 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700416 fi
417 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700418
Matt Alexanderd9c56562020-05-21 10:49:17 +0000419 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700420 complete -C "bit --tab" bit
421 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000422 if [ -z "$ZSH_VERSION" ]; then
423 # Doesn't work in zsh.
424 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400425 # TODO(b/244559459): Support b autocompletion for zsh
426 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000427 fi
Jim Tanga881a252018-06-19 16:34:41 +0800428 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800429
Cole Faust24c36db2021-01-23 02:39:37 +0000430 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100431 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000432 complete -F _complete_android_module_names outmod
433 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000434 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100435 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700436}
437
Joe Onorato824608c2022-04-08 11:06:16 -0700438function multitree_lunch_help()
439{
440 echo "usage: lunch PRODUCT-VARIANT" 1>&2
441 echo " Set up android build environment based on a product short name and variant" 1>&2
442 echo 1>&2
443 echo "lunch COMBO_FILE VARIANT" 1>&2
444 echo " Set up android build environment based on a specific lunch combo file" 1>&2
445 echo " and variant." 1>&2
446 echo 1>&2
447 echo "lunch --print [CONFIG]" 1>&2
448 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
449 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
450 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
451 echo 1>&2
452 echo "lunch --list" 1>&2
453 echo " List all possible combo files available in the current tree" 1>&2
454 echo 1>&2
455 echo "lunch --help" 1>&2
456 echo "lunch -h" 1>&2
457 echo " Prints this message." 1>&2
458}
459
460function multitree_lunch()
461{
462 local code
463 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000464 # Lunch must be run in the topdir, but this way we get a clear error
465 # message, instead of FileNotFound.
466 local T=$(multitree_gettop)
467 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +0000468 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
LaMont Jonesc39e5022022-06-23 19:09:06 +0000469 else
470 _multitree_lunch_error
471 return 1
472 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700473 if $(echo "$1" | grep -q '^-') ; then
474 # Calls starting with a -- argument are passed directly and the function
475 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000476 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700477 code=$?
478 if [[ $code -eq 2 ]] ; then
479 echo 1>&2
480 multitree_lunch_help
481 return $code
482 elif [[ $code -ne 0 ]] ; then
483 return $code
484 fi
485 else
486 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000487 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700488 code=$?
489 if [[ $code -eq 2 ]] ; then
490 echo 1>&2
491 multitree_lunch_help
492 return $code
493 elif [[ $code -ne 0 ]] ; then
494 return $code
495 fi
496
497 export TARGET_BUILD_COMBO=${results[0]}
498 export TARGET_BUILD_VARIANT=${results[1]}
499 fi
500}
501
Matt Alexanderd9c56562020-05-21 10:49:17 +0000502function choosetype()
503{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700504 echo "Build type choices are:"
505 echo " 1. release"
506 echo " 2. debug"
507 echo
508
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800509 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700510 DEFAULT_NUM=1
511 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800513 export TARGET_BUILD_TYPE=
514 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700515 while [ -z $TARGET_BUILD_TYPE ]
516 do
517 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000518 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800519 read ANSWER
520 else
521 echo $1
522 ANSWER=$1
523 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700524 case $ANSWER in
525 "")
526 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
527 ;;
528 1)
529 export TARGET_BUILD_TYPE=release
530 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800531 release)
532 export TARGET_BUILD_TYPE=release
533 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534 2)
535 export TARGET_BUILD_TYPE=debug
536 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800537 debug)
538 export TARGET_BUILD_TYPE=debug
539 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700540 *)
541 echo
542 echo "I didn't understand your response. Please try again."
543 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544 ;;
545 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000546 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800547 break
548 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549 done
550
Ying Wang08800fd2016-03-03 20:57:21 -0800551 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800553 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700554}
555
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556#
557# This function isn't really right: It chooses a TARGET_PRODUCT
558# based on the list of boards. Usually, that gets you something
559# that kinda works with a generic product, but really, you should
560# pick a product by name.
561#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000562function chooseproduct()
563{
Christopher Ferris55257d22017-03-23 11:08:58 -0700564 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000565 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 default_value=$TARGET_PRODUCT
567 else
Ying Wang0a76df52015-06-08 11:57:26 -0700568 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700569 fi
570
Ying Wang08800fd2016-03-03 20:57:21 -0800571 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800572 export TARGET_PRODUCT=
573 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 while [ -z "$TARGET_PRODUCT" ]
575 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700576 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000577 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578 read ANSWER
579 else
580 echo $1
581 ANSWER=$1
582 fi
583
Matt Alexanderd9c56562020-05-21 10:49:17 +0000584 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800586 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000587 if check_product $ANSWER
588 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 export TARGET_PRODUCT=$ANSWER
590 else
591 echo "** Not a valid product: $ANSWER"
592 fi
593 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000594 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 fi
597 done
598
Ying Wang08800fd2016-03-03 20:57:21 -0800599 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800601 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602}
603
Matt Alexanderd9c56562020-05-21 10:49:17 +0000604function choosevariant()
605{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800606 echo "Variant choices are:"
607 local index=1
608 local v
609 for v in ${VARIANT_CHOICES[@]}
610 do
611 # The product name is the name of the directory containing
612 # the makefile we found, above.
613 echo " $index. $v"
614 index=$(($index+1))
615 done
616
617 local default_value=eng
618 local ANSWER
619
620 export TARGET_BUILD_VARIANT=
621 while [ -z "$TARGET_BUILD_VARIANT" ]
622 do
623 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000624 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800625 read ANSWER
626 else
627 echo $1
628 ANSWER=$1
629 fi
630
Matt Alexanderd9c56562020-05-21 10:49:17 +0000631 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800632 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000633 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
634 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200635 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636 fi
637 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000638 if check_variant $ANSWER
639 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640 export TARGET_BUILD_VARIANT=$ANSWER
641 else
642 echo "** Not a valid variant: $ANSWER"
643 fi
644 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000645 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646 break
647 fi
648 done
649}
650
Matt Alexanderd9c56562020-05-21 10:49:17 +0000651function choosecombo()
652{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700653 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700654
655 echo
656 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700657 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700658
659 echo
660 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700661 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662
663 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800664 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700665 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800666 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800667 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700668}
669
Matt Alexanderd9c56562020-05-21 10:49:17 +0000670function add_lunch_combo()
671{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800672 if [ -n "$ZSH_VERSION" ]; then
673 echo -n "${funcfiletrace[1]}: "
674 else
675 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
676 fi
677 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 -0800678}
679
Matt Alexanderd9c56562020-05-21 10:49:17 +0000680function print_lunch_menu()
681{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700682 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100683 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700684 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 +0100685 local ret=$?
686
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700687 echo
688 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700689 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100690
Matt Alexanderd9c56562020-05-21 10:49:17 +0000691 if [ $ret -ne 0 ]
692 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100693 echo "Warning: Cannot display lunch menu."
694 echo
695 echo "Note: You can invoke lunch with an explicit target:"
696 echo
697 echo " usage: lunch [target]" >&2
698 echo
699 return
700 fi
701
Will Burr40401202022-02-07 12:12:01 +0000702 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800703
704 local i=1
705 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700706 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800707 do
708 echo " $i. $choice"
709 i=$(($i+1))
710 done
711
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700712 echo
713}
714
Matt Alexanderd9c56562020-05-21 10:49:17 +0000715function lunch()
716{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800717 local answer
718
Steven Moreland92793dc2020-02-25 18:30:18 -0800719 if [[ $# -gt 1 ]]; then
720 echo "usage: lunch [target]" >&2
721 return 1
722 fi
723
Will Burr40401202022-02-07 12:12:01 +0000724 local used_lunch_menu=0
725
Steven Moreland92793dc2020-02-25 18:30:18 -0800726 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800727 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700728 else
729 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000730 echo "Which would you like? [aosp_arm-eng]"
731 echo -n "Pick from common choices above (e.g. 13) or specify your own (e.g. aosp_barbet-eng): "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800732 read answer
Will Burr40401202022-02-07 12:12:01 +0000733 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700734 fi
735
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800736 local selection=
737
Matt Alexanderd9c56562020-05-21 10:49:17 +0000738 if [ -z "$answer" ]
739 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700740 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000741 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
742 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800743 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000744 if [ $answer -le ${#choices[@]} ]
745 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800746 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000747 if [ -n "$ZSH_VERSION" ]
748 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800749 selection=${choices[$(($answer))]}
750 else
751 selection=${choices[$(($answer-1))]}
752 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800753 fi
Colin Cross88737132017-03-21 17:41:03 -0700754 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800755 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700756 fi
757
Joe Onoratoda12daf2010-06-09 18:18:31 -0700758 export TARGET_BUILD_APPS=
759
Colin Cross88737132017-03-21 17:41:03 -0700760 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700761 product=${selection%%-*} # Trim everything after first dash
762 variant_and_version=${selection#*-} # Trim everything up to first dash
763 if [ "$variant_and_version" != "$selection" ]; then
764 variant=${variant_and_version%%-*}
765 if [ "$variant" != "$variant_and_version" ]; then
766 version=${variant_and_version#*-}
767 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700768 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800769
Matt Alexanderd9c56562020-05-21 10:49:17 +0000770 if [ -z "$product" ]
771 then
Ying Wang08800fd2016-03-03 20:57:21 -0800772 echo
Colin Cross88737132017-03-21 17:41:03 -0700773 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700774 return 1
775 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800776
Colin Cross88737132017-03-21 17:41:03 -0700777 TARGET_PRODUCT=$product \
778 TARGET_BUILD_VARIANT=$variant \
779 TARGET_PLATFORM_VERSION=$version \
780 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000781 if [ $? -ne 0 ]
782 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100783 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
784 then
785 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
786 fi
Colin Cross88737132017-03-21 17:41:03 -0700787 return 1
788 fi
Colin Cross88737132017-03-21 17:41:03 -0700789 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
790 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700791 if [ -n "$version" ]; then
792 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
793 else
794 unset TARGET_PLATFORM_VERSION
795 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700796 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700797
Will Burr40401202022-02-07 12:12:01 +0000798 if [ $used_lunch_menu -eq 1 ]; then
799 echo
800 echo "Hint: next time you can simply run 'lunch $selection'"
801 fi
802
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700803 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800804
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700805 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700806 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800807 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400808
809 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
810 check_mu_config
811 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700812}
813
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700814unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700815# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000816function _lunch()
817{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700818 local cur prev opts
819 COMPREPLY=()
820 cur="${COMP_WORDS[COMP_CWORD]}"
821 prev="${COMP_WORDS[COMP_CWORD-1]}"
822
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700823 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800824 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700825 fi
826
827 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700828 return 0
829}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700830
Joe Onoratoda12daf2010-06-09 18:18:31 -0700831# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700832# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000833function tapas()
834{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700835 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800836 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700837 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700838 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Colin Cross7f49a672022-01-27 18:15:53 -0800839 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
840 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|devkeys)$' | xargs)"
841
Joe Onoratoda12daf2010-06-09 18:18:31 -0700842
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700843 if [ "$showHelp" != "" ]; then
844 $(gettop)/build/make/tapasHelp.sh
845 return
846 fi
847
Ying Wang67f02922012-08-22 10:25:20 -0700848 if [ $(echo $arch | wc -w) -gt 1 ]; then
849 echo "tapas: Error: Multiple build archs supplied: $arch"
850 return
851 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700852 if [ $(echo $variant | wc -w) -gt 1 ]; then
853 echo "tapas: Error: Multiple build variants supplied: $variant"
854 return
855 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700856 if [ $(echo $density | wc -w) -gt 1 ]; then
857 echo "tapas: Error: Multiple densities supplied: $density"
858 return
859 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800860 if [ $(echo $keys | wc -w) -gt 1 ]; then
861 echo "tapas: Error: Multiple keys supplied: $keys"
862 return
863 fi
Ying Wang67f02922012-08-22 10:25:20 -0700864
Ying Wang0a76df52015-06-08 11:57:26 -0700865 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700866 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000867 x86) product=aosp_x86;;
868 arm64) product=aosp_arm64;;
869 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700870 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800871 if [ -n "$keys" ]; then
872 product=${product/aosp_/aosp_${keys}_}
873 fi;
874
Joe Onoratoda12daf2010-06-09 18:18:31 -0700875 if [ -z "$variant" ]; then
876 variant=eng
877 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700878 if [ -z "$apps" ]; then
879 apps=all
880 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600881 if [ -z "$density" ]; then
882 density=alldpi
883 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700884
Ying Wang67f02922012-08-22 10:25:20 -0700885 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700886 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700887 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700888 export TARGET_BUILD_TYPE=release
889 export TARGET_BUILD_APPS=$apps
890
Ying Wang08800fd2016-03-03 20:57:21 -0800891 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700892 set_stuff_for_environment
893 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800894 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700895}
896
Martin Stjernholmf692c752021-04-12 00:01:10 +0100897# Configures the build to build unbundled Android modules (APEXes).
898# Run banchan with one or more module names (from apex{} modules).
899function banchan()
900{
901 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000902 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64|arm64only|x86_64only)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100903 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100904 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 +0100905
906 if [ "$showHelp" != "" ]; then
907 $(gettop)/build/make/banchanHelp.sh
908 return
909 fi
910
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100911 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000912 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100913 elif [ $(echo $product | wc -w) -gt 1 ]; then
914 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100915 return
916 fi
917 if [ $(echo $variant | wc -w) -gt 1 ]; then
918 echo "banchan: Error: Multiple build variants supplied: $variant"
919 return
920 fi
921 if [ -z "$apps" ]; then
922 echo "banchan: Error: No modules supplied"
923 return
924 fi
925
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100926 case $product in
927 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100928 x86) product=module_x86;;
929 arm64) product=module_arm64;;
930 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000931 arm64only) product=module_arm64only;;
932 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100933 esac
934 if [ -z "$variant" ]; then
935 variant=eng
936 fi
937
938 export TARGET_PRODUCT=$product
939 export TARGET_BUILD_VARIANT=$variant
940 export TARGET_BUILD_DENSITY=alldpi
941 export TARGET_BUILD_TYPE=release
942
943 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
944 # case is different and it may diverge in the future.
945 export TARGET_BUILD_APPS=$apps
946
947 build_build_var_cache
948 set_stuff_for_environment
949 printconfig
950 destroy_build_var_cache
951}
952
Matt Alexanderd9c56562020-05-21 10:49:17 +0000953function gettop
954{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700955 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000956 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700957 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000958 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700959 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000960 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800961 # The following circumlocution (repeated below as well) ensures
962 # that we record the true directory name and not one that is
963 # faked up with symlink names.
964 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700965 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800966 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700967 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000968 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800969 \cd ..
synergyb112a402013-07-05 19:47:47 -0700970 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700971 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000972 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700973 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000974 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700975 fi
976 fi
977 fi
978}
979
Joe Onorato7cf6f972022-05-11 21:39:57 -0700980# TODO: Merge into gettop as part of launching multitree
981function multitree_gettop
982{
Spandan Dasca762052022-09-21 00:08:34 +0000983 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700984 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
985 # The following circumlocution ensures we remove symlinks from TOP.
986 (cd "$TOP"; PWD= /bin/pwd)
987 else
988 if [ -f $TOPFILE ] ; then
989 # The following circumlocution (repeated below as well) ensures
990 # that we record the true directory name and not one that is
991 # faked up with symlink names.
992 PWD= /bin/pwd
993 else
994 local HERE=$PWD
995 local T=
996 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
997 \cd ..
998 T=`PWD= /bin/pwd -P`
999 done
1000 \cd "$HERE"
1001 if [ -f "$T/$TOPFILE" ]; then
1002 echo "$T"
1003 fi
1004 fi
1005 fi
1006}
1007
Matt Alexanderd9c56562020-05-21 10:49:17 +00001008function croot()
1009{
Christopher Ferris55257d22017-03-23 11:08:58 -07001010 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001011 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001012 if [ "$1" ]; then
1013 \cd $(gettop)/$1
1014 else
1015 \cd $(gettop)
1016 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001017 else
1018 echo "Couldn't locate the top of the tree. Try setting TOP."
1019 fi
1020}
1021
Matt Alexanderd9c56562020-05-21 10:49:17 +00001022function _croot()
1023{
Anton Hanssonece9c482019-02-04 18:15:39 +00001024 local T=$(gettop)
1025 if [ "$T" ]; then
1026 local cur="${COMP_WORDS[COMP_CWORD]}"
1027 k=0
1028 for c in $(compgen -d ${T}/${cur}); do
1029 COMPREPLY[k++]=${c#${T}/}/
1030 done
1031 fi
1032}
1033
Matt Alexanderd9c56562020-05-21 10:49:17 +00001034function cproj()
1035{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001036 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001037 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001038 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001039 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1040 T=$PWD
1041 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001042 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001043 return
1044 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001045 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001046 done
Ying Wang9cd17642012-12-13 10:52:07 -08001047 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001048 echo "can't find Android.mk"
1049}
1050
Daniel Sandler47e0a882013-07-30 13:23:52 -04001051# simplified version of ps; output in the form
1052# <pid> <procname>
1053function qpid() {
1054 local prepend=''
1055 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001056 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001057 prepend=' '
1058 append='$'
1059 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001060 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001061 echo "usage: qpid [[--exact] <process name|pid>"
1062 return 255
1063 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001064
1065 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001066 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001067 qpid | \grep "$prepend$EXE$append"
1068 else
1069 adb shell ps \
1070 | tr -d '\r' \
1071 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1072 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001073}
1074
Steven Moreland74114f12020-09-10 01:23:32 +00001075# syswrite - disable verity, reboot if needed, and remount image
1076#
1077# Easy way to make system.img/etc writable
1078function syswrite() {
1079 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001080 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001081 echo "rebooting"
1082 adb reboot && adb wait-for-device && adb root || return 1
1083 fi
1084 adb wait-for-device && adb remount || return 1
1085}
1086
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001087# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001088# that has the core-file-size limit set correctly
1089#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001090# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001091# if its core-file-size limit is not set already.
1092# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1093
Matt Alexanderd9c56562020-05-21 10:49:17 +00001094function coredump_setup()
1095{
1096 echo "Getting root...";
1097 adb root;
1098 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001099
Matt Alexanderd9c56562020-05-21 10:49:17 +00001100 echo "Remounting root partition read-write...";
1101 adb shell mount -w -o remount -t rootfs rootfs;
1102 sleep 1;
1103 adb wait-for-device;
1104 adb shell mkdir -p /cores;
1105 adb shell mount -t tmpfs tmpfs /cores;
1106 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001107
Matt Alexanderd9c56562020-05-21 10:49:17 +00001108 echo "Granting SELinux permission to dump in /cores...";
1109 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001110
Matt Alexanderd9c56562020-05-21 10:49:17 +00001111 echo "Set core pattern.";
1112 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001113
Ying Wang08800fd2016-03-03 20:57:21 -08001114 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001115}
1116
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001117# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001118# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001119#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001120# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001121# dump to actually be generated.
1122
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123function coredump_enable()
1124{
1125 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001126 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001127 printf "Expecting a PID!\n";
1128 return;
1129 fi;
1130 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001131 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001132}
1133
1134# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001136#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001137# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001138# enabled globally.
1139
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140function core()
1141{
1142 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001143
Ying Wang08800fd2016-03-03 20:57:21 -08001144 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145 printf "Expecting a PID!\n";
1146 return;
1147 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001148
Matt Alexanderd9c56562020-05-21 10:49:17 +00001149 local CORENAME=core.$PID;
1150 local COREPATH=/cores/$CORENAME;
1151 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001152
Matt Alexanderd9c56562020-05-21 10:49:17 +00001153 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001154
Matt Alexanderd9c56562020-05-21 10:49:17 +00001155 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001156 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001157 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1158 adb shell kill -$SIG $PID;
1159 sleep 1;
1160 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001161
Matt Alexanderd9c56562020-05-21 10:49:17 +00001162 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1163 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001164}
1165
Christopher Tate744ee802009-11-12 15:33:08 -08001166# systemstack - dump the current stack trace of all threads in the system process
1167# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001168function systemstack()
1169{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001170 stacks system_server
1171}
1172
Michael Wrightaeed7212014-06-19 19:58:12 -07001173# Read the ELF header from /proc/$PID/exe to determine if the process is
1174# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001175function is64bit()
1176{
Ben Chengfba67bf2014-02-25 10:27:07 -08001177 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178 if [ "$PID" ] ; then
1179 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001180 echo "64"
1181 else
1182 echo ""
1183 fi
1184 else
1185 echo ""
1186 fi
1187}
1188
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001189case `uname -s` in
1190 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001191 function sgrep()
1192 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001193 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 -04001194 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001195 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197 ;;
1198 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001199 function sgrep()
1200 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001201 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 -04001202 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001203 }
1204 ;;
1205esac
1206
Matt Alexanderd9c56562020-05-21 10:49:17 +00001207function gettargetarch
1208{
Raghu Gandham8da43102012-07-25 19:57:22 -07001209 get_build_var TARGET_ARCH
1210}
1211
Matt Alexanderd9c56562020-05-21 10:49:17 +00001212function ggrep()
1213{
Mike Frysinger5e479732015-09-22 18:13:48 -04001214 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1215 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001216}
1217
Matt Alexanderd9c56562020-05-21 10:49:17 +00001218function gogrep()
1219{
Orion Hodson831472d2019-10-25 11:35:15 +01001220 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1221 -exec grep --color -n "$@" {} +
1222}
1223
Matt Alexanderd9c56562020-05-21 10:49:17 +00001224function jgrep()
1225{
Mike Frysinger5e479732015-09-22 18:13:48 -04001226 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1227 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001228}
1229
Alistair Delva176e5342021-02-22 13:31:26 -08001230function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001231{
1232 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1233 -exec grep --color -n "$@" {} +
1234}
1235
Taesu Leeea0cecd2020-10-28 11:05:18 +09001236function ktgrep()
1237{
1238 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1239 -exec grep --color -n "$@" {} +
1240}
1241
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242function cgrep()
1243{
Mike Frysinger5e479732015-09-22 18:13:48 -04001244 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' \) \
1245 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001246}
1247
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248function resgrep()
1249{
Christopher Ferris55257d22017-03-23 11:08:58 -07001250 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001251 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1252 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1253 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001254}
1255
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256function mangrep()
1257{
Mike Frysinger5e479732015-09-22 18:13:48 -04001258 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1259 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001260}
1261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262function owngrep()
1263{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001264 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1265 -exec grep --color -n "$@" {} +
1266}
1267
Matt Alexanderd9c56562020-05-21 10:49:17 +00001268function sepgrep()
1269{
Mike Frysinger5e479732015-09-22 18:13:48 -04001270 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1271 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001272}
1273
Matt Alexanderd9c56562020-05-21 10:49:17 +00001274function rcgrep()
1275{
Mike Frysinger5e479732015-09-22 18:13:48 -04001276 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1277 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001278}
1279
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001280case `uname -s` in
1281 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001282 function mgrep()
1283 {
Orion Hodson831472d2019-10-25 11:35:15 +01001284 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 -04001285 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286 }
1287
Matt Alexanderd9c56562020-05-21 10:49:17 +00001288 function treegrep()
1289 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001290 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 -04001291 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001292 }
1293
1294 ;;
1295 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001296 function mgrep()
1297 {
Orion Hodson831472d2019-10-25 11:35:15 +01001298 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 -04001299 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001300 }
1301
Matt Alexanderd9c56562020-05-21 10:49:17 +00001302 function treegrep()
1303 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001304 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 -04001305 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001306 }
1307
1308 ;;
1309esac
1310
Matt Alexanderd9c56562020-05-21 10:49:17 +00001311function getprebuilt
1312{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001313 get_abs_build_var ANDROID_PREBUILTS
1314}
1315
Matt Alexanderd9c56562020-05-21 10:49:17 +00001316function tracedmdump()
1317{
Christopher Ferris55257d22017-03-23 11:08:58 -07001318 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001319 if [ ! "$T" ]; then
1320 echo "Couldn't locate the top of the tree. Try setting TOP."
1321 return
1322 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001323 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001324 local arch=$(gettargetarch)
1325 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001326
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001327 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001328 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001329 echo "usage: tracedmdump tracename"
1330 return
1331 fi
1332
Matt Alexanderd9c56562020-05-21 10:49:17 +00001333 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001334 echo "Error: cannot find kernel: '$KERNEL'"
1335 return
1336 fi
1337
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001338 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001339 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001340 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1341 fi
1342
1343 echo "post-processing traces..."
1344 rm -f $TRACE/qtrace.dexlist
1345 post_trace $TRACE
1346 if [ $? -ne 0 ]; then
1347 echo "***"
1348 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1349 echo "***"
1350 return
1351 fi
1352 echo "generating dexlist output..."
1353 /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
1354 echo "generating dmtrace data..."
1355 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1356 echo "generating html file..."
1357 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1358 echo "done, see $TRACE/dmtrace.html for details"
1359 echo "or run:"
1360 echo " traceview $TRACE/dmtrace"
1361}
1362
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001363# communicate with a running device or emulator, set up necessary state,
1364# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001365function runhat()
1366{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001367 # process standard adb options
1368 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001369 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001370 adbTarget=$1
1371 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001372 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001373 adbTarget="$1 $2"
1374 shift 2
1375 fi
1376 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001377 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001378
1379 # runhat options
1380 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001381
Matt Alexanderd9c56562020-05-21 10:49:17 +00001382 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001383 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001384 return
1385 fi
1386
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001387 # confirm hat is available
1388 if [ -z $(which hat) ]; then
1389 echo "hat is not available in this configuration."
1390 return
1391 fi
1392
Andy McFaddenb6289852010-07-12 08:00:19 -07001393 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001394 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001395 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001396 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001397 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001398 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001399 echo -n "> "
1400 read
1401
The Android Open Source Project88b60792009-03-03 19:28:42 -08001402 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001403
The Android Open Source Project88b60792009-03-03 19:28:42 -08001404 echo "Retrieving file $devFile..."
1405 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001406
The Android Open Source Project88b60792009-03-03 19:28:42 -08001407 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001408
The Android Open Source Project88b60792009-03-03 19:28:42 -08001409 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001410 echo "View the output by pointing your browser at http://localhost:7000/"
1411 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001412 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001413}
1414
Matt Alexanderd9c56562020-05-21 10:49:17 +00001415function getbugreports()
1416{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001417 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001418
1419 if [ ! "$reports" ]; then
1420 echo "Could not locate any bugreports."
1421 return
1422 fi
1423
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001424 local report
1425 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001426 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001427 echo "/sdcard/bugreports/${report}"
1428 adb pull /sdcard/bugreports/${report} ${report}
1429 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001430 done
1431}
1432
Matt Alexanderd9c56562020-05-21 10:49:17 +00001433function getsdcardpath()
1434{
Victoria Lease1b296b42012-08-21 15:44:06 -07001435 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1436}
1437
Matt Alexanderd9c56562020-05-21 10:49:17 +00001438function getscreenshotpath()
1439{
Victoria Lease1b296b42012-08-21 15:44:06 -07001440 echo "$(getsdcardpath)/Pictures/Screenshots"
1441}
1442
Matt Alexanderd9c56562020-05-21 10:49:17 +00001443function getlastscreenshot()
1444{
Victoria Lease1b296b42012-08-21 15:44:06 -07001445 local screenshot_path=$(getscreenshotpath)
1446 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 +00001447 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001448 echo "No screenshots found."
1449 return
1450 fi
1451 echo "${screenshot}"
1452 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1453}
1454
Matt Alexanderd9c56562020-05-21 10:49:17 +00001455function startviewserver()
1456{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001457 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001458 if [ $# -gt 0 ]; then
1459 port=$1
1460 fi
1461 adb shell service call window 1 i32 $port
1462}
1463
Matt Alexanderd9c56562020-05-21 10:49:17 +00001464function stopviewserver()
1465{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001466 adb shell service call window 2
1467}
1468
Matt Alexanderd9c56562020-05-21 10:49:17 +00001469function isviewserverstarted()
1470{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001471 adb shell service call window 3
1472}
1473
Matt Alexanderd9c56562020-05-21 10:49:17 +00001474function key_home()
1475{
Romain Guyb84049a2010-10-04 16:56:11 -07001476 adb shell input keyevent 3
1477}
1478
Matt Alexanderd9c56562020-05-21 10:49:17 +00001479function key_back()
1480{
Romain Guyb84049a2010-10-04 16:56:11 -07001481 adb shell input keyevent 4
1482}
1483
Matt Alexanderd9c56562020-05-21 10:49:17 +00001484function key_menu()
1485{
Romain Guyb84049a2010-10-04 16:56:11 -07001486 adb shell input keyevent 82
1487}
1488
Matt Alexanderd9c56562020-05-21 10:49:17 +00001489function smoketest()
1490{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001491 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1492 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1493 return
1494 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001495 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001496 if [ ! "$T" ]; then
1497 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1498 return
1499 fi
1500
Ying Wang9cd17642012-12-13 10:52:07 -08001501 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001502 adb uninstall com.android.smoketest > /dev/null &&
1503 adb uninstall com.android.smoketest.tests > /dev/null &&
1504 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1505 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1506 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1507}
1508
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001509# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001510function runtest()
1511{
Christopher Ferris55257d22017-03-23 11:08:58 -07001512 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001513 if [ ! "$T" ]; then
1514 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1515 return
1516 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001517 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001518}
1519
The Android Open Source Project88b60792009-03-03 19:28:42 -08001520function godir () {
1521 if [[ -z "$1" ]]; then
1522 echo "Usage: godir <regex>"
1523 return
1524 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001525 local T=$(gettop)
1526 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001527 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001528 mkdir -p $OUT_DIR
1529 FILELIST=$OUT_DIR/filelist
1530 else
1531 FILELIST=$T/filelist
1532 fi
1533 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001534 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001535 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001536 echo " Done"
1537 echo ""
1538 fi
1539 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001540 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001541 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001542 echo "Not found"
1543 return
1544 fi
1545 local pathname
1546 local choice
1547 if [[ ${#lines[@]} > 1 ]]; then
1548 while [[ -z "$pathname" ]]; do
1549 local index=1
1550 local line
1551 for line in ${lines[@]}; do
1552 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001553 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001554 done
1555 echo
1556 echo -n "Select one: "
1557 unset choice
1558 read choice
1559 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1560 echo "Invalid choice"
1561 continue
1562 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001563 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001564 done
1565 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001566 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001567 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001568 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001569}
1570
Steven Moreland62054a42018-12-06 10:11:40 -08001571# Update module-info.json in out.
1572function refreshmod() {
1573 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1574 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1575 return 1
1576 fi
1577
1578 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1579
1580 # for the output of the next command
1581 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1582
1583 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001584 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001585 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1586}
1587
Cole Faust3e192382021-10-25 13:29:15 -07001588# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001589function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001590 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001591 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1592 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1593 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001594 return 1
1595 fi
1596
1597 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001598 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001599 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001600 fi
1601 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001602 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001603}
1604
1605# List all modules for the current device, as cached in module-info.json. If any build change is
1606# made and it should be reflected in the output, you should run 'refreshmod' first.
1607function allmod() {
1608 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001609
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001610 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 -08001611}
1612
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001613# Return the Bazel label of a Soong module if it is converted with bp2build.
1614function bmod()
1615(
1616 if [ $# -ne 1 ]; then
1617 echo "usage: bmod <module>" >&2
1618 return 1
1619 fi
1620
1621 # We could run bp2build here, but it might trigger bp2build invalidation
1622 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1623 # time overhead.
1624 #
1625 # For a snappy result, use the latest generated version in soong_injection,
1626 # and ask users to run m bp2build if it doesn't exist.
1627 converted_json="out/soong/soong_injection/metrics/converted_modules_path_map.json"
1628
1629 if [ ! -f $(gettop)/${converted_json} ]; then
1630 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1631 return 1
1632 fi
1633
1634 local target_label=$(python3 -c "import json
1635module = '$1'
1636converted_json='$converted_json'
1637bp2build_converted_map = json.load(open(converted_json))
1638if module not in bp2build_converted_map:
1639 exit(1)
1640print(bp2build_converted_map[module] + ':' + module)")
1641
1642 if [ -z "${target_label}" ]; then
1643 echo "$1 is not converted to Bazel." >&2
1644 return 1
1645 else
1646 echo "${target_label}"
1647 fi
1648)
1649
Joe Onorato2c1aa472021-02-25 16:42:39 -08001650# Get the path of a specific module in the android tree, as cached in module-info.json.
1651# If any build change is made, and it should be reflected in the output, you should run
1652# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001653function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001654 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001655 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001656 return 1
1657 fi
1658
Cole Faust24c36db2021-01-23 02:39:37 +00001659 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001660
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001661 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001662module = '$1'
1663module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1664if module not in module_info:
1665 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001666print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001667
1668 if [ -z "$relpath" ]; then
1669 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1670 return 1
1671 else
Rett Berg78d1c932019-01-24 14:34:23 -08001672 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001673 fi
1674}
1675
Joe Onorato2c1aa472021-02-25 16:42:39 -08001676# Get the path of a specific module in the android tree, as cached in module-info.json.
1677# If any build change is made, and it should be reflected in the output, you should run
1678# 'refreshmod' first. Note: This is the inverse of pathmod.
1679function dirmods() {
1680 if [[ $# -ne 1 ]]; then
1681 echo "usage: dirmods <path>" >&2
1682 return 1
1683 fi
1684
1685 verifymodinfo || return 1
1686
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001687 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001688dir = '$1'
1689while dir.endswith('/'):
1690 dir = dir[:-1]
1691prefix = dir + '/'
1692module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1693results = set()
1694for m in module_info.values():
1695 for path in m.get(u'path', []):
1696 if path == dir or path.startswith(prefix):
1697 name = m.get(u'module_name')
1698 if name:
1699 results.add(name)
1700for name in sorted(results):
1701 print(name)
1702"
1703}
1704
1705
Rett Berg78d1c932019-01-24 14:34:23 -08001706# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1707# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1708function gomod() {
1709 if [[ $# -ne 1 ]]; then
1710 echo "usage: gomod <module>" >&2
1711 return 1
1712 fi
1713
1714 local path="$(pathmod $@)"
1715 if [ -z "$path" ]; then
1716 return 1
1717 fi
1718 cd $path
1719}
1720
Cole Faust24c36db2021-01-23 02:39:37 +00001721# Gets the list of a module's installed outputs, as cached in module-info.json.
1722# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1723function outmod() {
1724 if [[ $# -ne 1 ]]; then
1725 echo "usage: outmod <module>" >&2
1726 return 1
1727 fi
1728
1729 verifymodinfo || return 1
1730
1731 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001732 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001733module = '$1'
1734module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1735if module not in module_info:
1736 exit(1)
1737for output in module_info[module]['installed']:
1738 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1739
1740 if [ $? -ne 0 ]; then
1741 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1742 return 1
1743 elif [ ! -z "$relpath" ]; then
1744 echo "$relpath"
1745 fi
1746}
1747
1748# adb install a module's apk, as cached in module-info.json. If any build change
1749# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1750# Usage: installmod [adb install arguments] <module>
1751# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1752function installmod() {
1753 if [[ $# -eq 0 ]]; then
1754 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001755 echo "" >&2
1756 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1757 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1758 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001759 return 1
1760 fi
1761
1762 local _path
1763 _path=$(outmod ${@:$#:1})
1764 if [ $? -ne 0 ]; then
1765 return 1
1766 fi
1767
1768 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1769 if [ -z "$_path" ]; then
1770 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1771 return 1
1772 fi
Cole Faust3e192382021-10-25 13:29:15 -07001773 local serial_device=""
1774 if [[ "$1" == "-s" ]]; then
1775 if [[ $# -le 2 ]]; then
1776 echo "-s requires an argument" >&2
1777 return 1
1778 fi
1779 serial_device="-s $2"
1780 shift 2
1781 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001782 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001783 echo adb $serial_device install ${@:1:$length} $_path
1784 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001785}
1786
dimitry73b84812018-12-11 18:06:00 +01001787function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001788 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001789 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001790}
1791
Alex Rayf0d08eb2013-03-08 15:15:06 -08001792# Print colored exit condition
1793function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001794 "$@"
1795 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001796 if [ $retval -ne 0 ]
1797 then
Jacky Cao89483b82015-05-15 22:12:53 +08001798 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001799 else
Jacky Cao89483b82015-05-15 22:12:53 +08001800 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001801 fi
1802 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001803}
1804
Matt Alexanderd9c56562020-05-21 10:49:17 +00001805function get_make_command()
1806{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001807 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1808 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001809 # Always use the real make if -C is passed in
1810 for arg in "$@"; do
1811 if [[ $arg == -C* ]]; then
1812 echo command make
1813 return
1814 fi
1815 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001816 echo build/soong/soong_ui.bash --make-mode
1817 else
1818 echo command make
1819 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001820}
1821
Matt Alexanderd9c56562020-05-21 10:49:17 +00001822function _wrap_build()
1823{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001824 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1825 "$@"
1826 return $?
1827 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001828 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001829 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001830 local ret=$?
1831 local end_time=$(date +"%s")
1832 local tdiff=$(($end_time-$start_time))
1833 local hours=$(($tdiff / 3600 ))
1834 local mins=$((($tdiff % 3600) / 60))
1835 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001836 local ncolors=$(tput colors 2>/dev/null)
1837 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001838 color_failed=$'\E'"[0;31m"
1839 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001840 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001841 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001842 else
1843 color_failed=""
1844 color_success=""
1845 color_reset=""
1846 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001847
1848 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1849 echo
1850 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1851 fi
1852
Ed Heylcc6be0a2014-06-18 14:55:58 -07001853 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001854 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001855 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001856 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001857 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001858 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001859 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001860 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001861 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001862 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001863 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001864 printf "(%s seconds)" $secs
1865 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001866 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001867 echo
1868 return $ret
1869}
1870
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001871function _trigger_build()
1872(
1873 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001874 local T=$(gettop)
1875 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001876 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1877 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001878 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1879 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001880 fi
1881)
1882
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001883# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001884function b()
1885(
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001886 # zsh breaks posix by not doing string-splitting on unquoted args by default.
1887 # See https://zsh.sourceforge.io/Guide/zshguide05.html section 5.4.4.
1888 # Tell it to emulate Bourne shell for this function.
1889 if [ -n "$ZSH_VERSION" ]; then emulate -L sh; fi
1890
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001891 # Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
1892 local bazel_args=""
1893 local skip_tests="--skip-soong-tests"
1894 for i in $@; do
1895 if [[ $i != "--run-soong-tests" ]]; then
1896 bazel_args+="$i "
1897 else
1898 skip_tests=""
1899 fi
1900 done
Jingwen Chend728ee12021-05-18 06:02:53 +00001901 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Jingwen Chen28a22982022-08-31 09:47:33 +00001902 _trigger_build "all-modules" bp2build $skip_tests USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001903 # Then, run Bazel using the synthetic workspace as the --package_path.
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001904 if [[ -z "$bazel_args" ]]; then
Jingwen Chen6f4963b2022-09-09 07:08:25 +00001905 # If there are no args, show help and exit.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001906 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001907 else
1908 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Cole Fausta68f5162022-08-18 17:33:51 -07001909 # Add the --config=bp2build after the first argument that doesn't start with a dash. That should be the bazel
1910 # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work with commands like:
1911 # b run //foo -- --args-for-foo
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001912 local config_set=0
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001913
1914 # Represent the args as an array, not a string.
1915 local bazel_args_with_config=()
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001916 for arg in $bazel_args; do
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001917 if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
Cole Fausta68f5162022-08-18 17:33:51 -07001918 then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001919 bazel_args_with_config+=("--config=bp2build -- ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001920 config_set=1
1921 else
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001922 bazel_args_with_config+=("$arg ")
Cole Fausta68f5162022-08-18 17:33:51 -07001923 fi
1924 done
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001925 if [[ $config_set -ne 1 ]]; then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001926 bazel_args_with_config+=("--config=bp2build ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001927 fi
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001928
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001929 # Call Bazel.
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001930 bazel ${bazel_args_with_config[@]}
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001931 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001932)
1933
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001934function m()
1935(
1936 _trigger_build "all-modules" "$@"
1937)
1938
1939function mm()
1940(
1941 _trigger_build "modules-in-a-dir-no-deps" "$@"
1942)
1943
1944function mmm()
1945(
1946 _trigger_build "modules-in-dirs-no-deps" "$@"
1947)
1948
1949function mma()
1950(
1951 _trigger_build "modules-in-a-dir" "$@"
1952)
1953
1954function mmma()
1955(
1956 _trigger_build "modules-in-dirs" "$@"
1957)
1958
Matt Alexanderd9c56562020-05-21 10:49:17 +00001959function make()
1960{
Dan Willemsene9842242017-07-28 13:00:13 -07001961 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001962}
1963
Joe Onorato7cf6f972022-05-11 21:39:57 -07001964function _multitree_lunch_error()
1965{
1966 >&2 echo "Couldn't locate the top of the tree. Please run \'source build/envsetup.sh\' and multitree_lunch from the root of your workspace."
1967}
1968
1969function multitree_build()
1970{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001971 local T=$(multitree_gettop)
1972 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001973 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001974 else
1975 _multitree_lunch_error
1976 return 1
1977 fi
1978}
1979
Matt Alexanderd9c56562020-05-21 10:49:17 +00001980function provision()
1981{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001982 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1983 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1984 return 1
1985 fi
1986 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1987 echo "There is no provisioning script for the device." >&2
1988 return 1
1989 fi
1990
1991 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001992 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001993 shift 1
1994 else
1995 echo "This action will reflash your device."
1996 echo ""
1997 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1998 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001999 echo -n "Are you sure you want to do this (yes/no)? "
2000 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00002001 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04002002 echo "Not taking any action. Exiting." >&2
2003 return 1
2004 fi
2005 fi
2006 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
2007}
2008
Jim Tanga881a252018-06-19 16:34:41 +08002009# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02002010function enable_zsh_completion() {
2011 # Don't override user's options if bash-style completion is already enabled.
2012 if ! declare -f complete >/dev/null; then
2013 autoload -U compinit && compinit
2014 autoload -U bashcompinit && bashcompinit
2015 fi
Jim Tanga881a252018-06-19 16:34:41 +08002016}
2017
2018function validate_current_shell() {
2019 local current_sh="$(ps -o command -p $$)"
2020 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07002021 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08002022 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07002023 ;;
Jim Tanga881a252018-06-19 16:34:41 +08002024 *zsh*)
2025 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00002026 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07002027 *)
Jim Tanga881a252018-06-19 16:34:41 +08002028 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 -07002029 ;;
2030 esac
Jim Tanga881a252018-06-19 16:34:41 +08002031}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08002032
2033# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08002034# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
2035# load those.
2036#
2037# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002038function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002039 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002040 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002041 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002042 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 -08002043 if [ -n "$allowed" ]; then
2044 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2045 echo " $allowed"
2046 echo " $f"
2047 return
2048 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002049 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002050 done
2051
2052 allowed_files=
2053 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002054 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002055 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002056 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002057
2058 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002059 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002060 else
2061 echo "ignoring $f, not in $allowed"
2062 fi
Jim Tanga881a252018-06-19 16:34:41 +08002063 done
2064 done
2065}
Kenny Root52aa81c2011-07-15 11:07:06 -07002066
Dan Albertbab814f2020-08-26 15:34:53 -07002067function showcommands() {
2068 local T=$(gettop)
2069 if [[ -z "$TARGET_PRODUCT" ]]; then
2070 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2071 return
2072 fi
2073 case $(uname -s) in
2074 Darwin)
2075 PREBUILT_NAME=darwin-x86
2076 ;;
2077 Linux)
2078 PREBUILT_NAME=linux-x86
2079 ;;
2080 *)
2081 >&2 echo Unknown host $(uname -s)
2082 return
2083 ;;
2084 esac
2085 if [[ -z "$OUT_DIR" ]]; then
2086 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
2087 OUT_DIR=out
2088 else
2089 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
2090 fi
2091 fi
2092 if [[ "$1" == "--regenerate" ]]; then
2093 shift 1
2094 NINJA_ARGS="-t commands $@" m
2095 else
2096 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2097 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2098 -t commands "$@")
2099 fi
2100}
2101
Cole Faust45844ab2022-08-30 13:59:07 -07002102function avbtool() {
2103 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2104 m avbtool
2105 fi
2106 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2107}
2108
Jim Tanga881a252018-06-19 16:34:41 +08002109validate_current_shell
2110source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002111addcompletions