blob: ea28c2ea42351dcf697ea2c984430f95cfeecc95 [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010012 Sets up the build environment for building unbundled apps (APKs).
13- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
14 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000015- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080016- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070017- mm: Builds and installs all of the modules in the current directory, and their
18 dependencies.
19- mmm: Builds and installs all of the modules in the supplied directories, and their
20 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080021 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070022- mma: Same as 'mm'
23- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080024- provision: Flash device with all required partitions. Options will be passed on to fastboot.
25- cgrep: Greps on all local C/C++ files.
26- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010027- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080028- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090029- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- resgrep: Greps on all local res/*.xml files.
31- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070032- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060033- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080034- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080035- sepgrep: Greps on all local sepolicy files.
36- sgrep: Greps on all local source files.
37- godir: Go to the directory containing a file.
38- allmod: List all modules.
39- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080040- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000041- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080042- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000043- installmod: Adb installs a module's built APK.
44- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000045- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070046
Roland Levillain39341922015-10-20 12:48:19 +010047Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070048- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080049- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070050
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070051Look at the source to view more functions. The complete list is:
52EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070053 local T=$(gettop)
54 local A=""
55 local i
Jacky Cao89483b82015-05-15 22:12:53 +080056 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070057 A="$A $i"
58 done
59 echo $A
60}
61
Ying Wang08800fd2016-03-03 20:57:21 -080062# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000063function build_build_var_cache()
64{
Christopher Ferris55257d22017-03-23 11:08:58 -070065 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080066 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080067 cached_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
68 cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
Ying Wang08800fd2016-03-03 20:57:21 -080069 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080070 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080071 --vars="${cached_vars[*]}" \
72 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070073 --var-prefix=var_cache_ \
74 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080075 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000076 if [ $ret -ne 0 ]
77 then
Ying Wang08800fd2016-03-03 20:57:21 -080078 unset build_dicts_script
79 return $ret
80 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070081 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080082 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080083 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080084 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000085 if [ $ret -ne 0 ]
86 then
Ying Wang08800fd2016-03-03 20:57:21 -080087 return $ret
88 fi
89 BUILD_VAR_CACHE_READY="true"
90}
91
Ying Wangf0cb3972016-03-04 13:56:23 -080092# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080093# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000094function destroy_build_var_cache()
95{
Ying Wang08800fd2016-03-03 20:57:21 -080096 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070097 local v
Ying Wang08800fd2016-03-03 20:57:21 -080098 for v in $cached_vars; do
99 unset var_cache_$v
100 done
101 unset cached_vars
102 for v in $cached_abs_vars; do
103 unset abs_var_cache_$v
104 done
105 unset cached_abs_vars
106}
107
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700108# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000109function get_abs_build_var()
110{
111 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
112 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800113 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200114 return
Ying Wang08800fd2016-03-03 20:57:21 -0800115 fi
116
Christopher Ferris55257d22017-03-23 11:08:58 -0700117 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700118 if [ ! "$T" ]; then
119 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
120 return
121 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700122 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700123}
124
125# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000126function get_build_var()
127{
128 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
129 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800130 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100131 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800132 fi
133
Christopher Ferris55257d22017-03-23 11:08:58 -0700134 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700135 if [ ! "$T" ]; then
136 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100137 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700138 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700139 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800140}
141
142# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000143function check_product()
144{
Christopher Ferris55257d22017-03-23 11:08:58 -0700145 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800146 if [ ! "$T" ]; then
147 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
148 return
149 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700150 TARGET_PRODUCT=$1 \
151 TARGET_BUILD_VARIANT= \
152 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700153 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800154 get_build_var TARGET_DEVICE > /dev/null
155 # hide successful answers, but allow the errors to show
156}
157
158VARIANT_CHOICES=(user userdebug eng)
159
160# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000161function check_variant()
162{
Christopher Ferris55257d22017-03-23 11:08:58 -0700163 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800164 for v in ${VARIANT_CHOICES[@]}
165 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000166 if [ "$v" = "$1" ]
167 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800168 return 0
169 fi
170 done
171 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700172}
173
Matt Alexanderd9c56562020-05-21 10:49:17 +0000174function setpaths()
175{
Christopher Ferris55257d22017-03-23 11:08:58 -0700176 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700177 if [ ! "$T" ]; then
178 echo "Couldn't locate the top of the tree. Try setting TOP."
179 return
180 fi
181
182 ##################################################################
183 # #
184 # Read me before you modify this code #
185 # #
186 # This function sets ANDROID_BUILD_PATHS to what it is adding #
187 # to PATH, and the next time it is run, it removes that from #
188 # PATH. This is required so lunch can be run more than once #
189 # and still have working paths. #
190 # #
191 ##################################################################
192
Raphael Mollc639c782011-06-20 17:25:01 -0700193 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
194 # due to "C:\Program Files" being in the path.
195
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000197 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
199 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000200 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700201 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800202 # strip leading ':', if any
203 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500204 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700205
206 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local prebuiltdir=$(getprebuilt)
208 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700209
Ben Cheng8bc4c432012-11-16 13:29:13 -0800210 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700211 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
212 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800213 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800214
Raphael Mollc639c782011-06-20 17:25:01 -0700215 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800216 export ANDROID_TOOLCHAIN=
217 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200218 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700219 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000221 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700222 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000223 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400224 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000225 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200226 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000227 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
228 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700229 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200230 *)
231 echo "Can't find toolchain for unknown architecture: $ARCH"
232 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700233 ;;
234 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700235 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800236 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700237 fi
Raphael732936d2011-06-22 14:35:32 -0700238
Christopher Ferris55257d22017-03-23 11:08:58 -0700239 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800240 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
241 fi
242
Dan Willemsen838dcec2021-09-30 22:40:34 +0000243 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700244
245 # add kernel specific binaries
246 case $(uname -s) in
247 Linux)
248 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
249 ;;
250 *)
251 ;;
252 esac
253
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400254 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
Martin Stjernholm6a012262021-11-24 14:56:15 +0000255 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
Yi Kongdfd00b12019-05-21 16:00:04 -0700256 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
257
258 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
259 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
260 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200261
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800262 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
263 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
264
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200265 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
266 # to ensure that the corresponding 'emulator' binaries are used.
267 case $(uname -s) in
268 Darwin)
269 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
270 ;;
271 Linux)
272 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
273 ;;
274 *)
275 ANDROID_EMULATOR_PREBUILTS=
276 ;;
277 esac
278 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700279 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200280 export ANDROID_EMULATOR_PREBUILTS
281 fi
282
Jim Tangb3fda302018-12-22 10:24:55 +0800283 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
284 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700285 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
286 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
287 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000288 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800289
Martin Stjernholm6a012262021-11-24 14:56:15 +0000290 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700291 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800292
293 # out with the duplicate old
294 if [ -n $ANDROID_PYTHONPATH ]; then
295 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
296 fi
297 # and in with the new
298 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800299 if [ -n $VENDOR_PYTHONPATH ]; then
300 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
301 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800302 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800303
Colin Crosse97e6932017-06-30 16:01:45 -0700304 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
305 export JAVA_HOME=$ANDROID_JAVA_HOME
306 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
307 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
308 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500309
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800310 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
312 export OUT=$ANDROID_PRODUCT_OUT
313
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700314 unset ANDROID_HOST_OUT
315 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
316
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900317 unset ANDROID_SOONG_HOST_OUT
318 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
319
Simran Basidd050ed2017-02-13 13:46:48 -0800320 unset ANDROID_HOST_OUT_TESTCASES
321 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
322
323 unset ANDROID_TARGET_OUT_TESTCASES
324 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327 # TODO: fix the path
328 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
329}
330
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000331function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000332{
Jingwen Chen17d56992021-06-10 08:17:54 +0000333 if which bazel &>/dev/null; then
334 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
335 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000336 fi
337
Jingwen Chen17d56992021-06-10 08:17:54 +0000338 local T="$(gettop)"
339 if [ ! "$T" ]; then
340 >&2 echo "Couldn't locate the top of the Android tree. Try setting TOP. This bazel() function cannot be used outside of the AOSP directory."
341 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000342 fi
343
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000344 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000345}
346
Matt Alexanderd9c56562020-05-21 10:49:17 +0000347function printconfig()
348{
Christopher Ferris55257d22017-03-23 11:08:58 -0700349 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800350 if [ ! "$T" ]; then
351 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
352 return
353 fi
354 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700355}
356
Matt Alexanderd9c56562020-05-21 10:49:17 +0000357function set_stuff_for_environment()
358{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800359 setpaths
360 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700361
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800362 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700363 # With this environment variable new GCC can apply colors to warnings/errors
364 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700365}
366
Matt Alexanderd9c56562020-05-21 10:49:17 +0000367function set_sequence_number()
368{
Colin Cross88737132017-03-21 17:41:03 -0700369 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700370}
371
Makoto Onukida971062018-06-18 10:15:19 -0700372# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
373function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800374 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700375 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800376 *:"$cmd":*)
377 return 1
378 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700379 esac
380 return 0
381}
382
Matt Alexanderd9c56562020-05-21 10:49:17 +0000383function addcompletions()
384{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700385 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700386
Jim Tanga881a252018-06-19 16:34:41 +0800387 # Keep us from trying to run in something that's neither bash nor zsh.
388 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700389 return
390 fi
391
392 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800393 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700394 return
395 fi
396
Jim Tanga881a252018-06-19 16:34:41 +0800397 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000398 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800399 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800400 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800401 )
Makoto Onukida971062018-06-18 10:15:19 -0700402 # Completion can be disabled selectively to allow users to use non-standard completion.
403 # e.g.
404 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
405 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400406 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800407 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400408 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000409 if [ ! -f "$f" ]; then
410 echo "Warning: completion file $f not found"
411 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700412 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700413 fi
414 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700415
Matt Alexanderd9c56562020-05-21 10:49:17 +0000416 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700417 complete -C "bit --tab" bit
418 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000419 if [ -z "$ZSH_VERSION" ]; then
420 # Doesn't work in zsh.
421 complete -o nospace -F _croot croot
422 fi
Jim Tanga881a252018-06-19 16:34:41 +0800423 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800424
Cole Faust24c36db2021-01-23 02:39:37 +0000425 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100426 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000427 complete -F _complete_android_module_names outmod
428 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100429 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700430}
431
Joe Onorato824608c2022-04-08 11:06:16 -0700432function multitree_lunch_help()
433{
434 echo "usage: lunch PRODUCT-VARIANT" 1>&2
435 echo " Set up android build environment based on a product short name and variant" 1>&2
436 echo 1>&2
437 echo "lunch COMBO_FILE VARIANT" 1>&2
438 echo " Set up android build environment based on a specific lunch combo file" 1>&2
439 echo " and variant." 1>&2
440 echo 1>&2
441 echo "lunch --print [CONFIG]" 1>&2
442 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
443 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
444 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
445 echo 1>&2
446 echo "lunch --list" 1>&2
447 echo " List all possible combo files available in the current tree" 1>&2
448 echo 1>&2
449 echo "lunch --help" 1>&2
450 echo "lunch -h" 1>&2
451 echo " Prints this message." 1>&2
452}
453
454function multitree_lunch()
455{
456 local code
457 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000458 # Lunch must be run in the topdir, but this way we get a clear error
459 # message, instead of FileNotFound.
460 local T=$(multitree_gettop)
461 if [ -n "$T" ]; then
462 "$T/build/build/make/orchestrator/core/orchestrator.py" "$@"
463 else
464 _multitree_lunch_error
465 return 1
466 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700467 if $(echo "$1" | grep -q '^-') ; then
468 # Calls starting with a -- argument are passed directly and the function
469 # returns with the lunch.py exit code.
LaMont Jonesc39e5022022-06-23 19:09:06 +0000470 "${T}/build/build/make/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700471 code=$?
472 if [[ $code -eq 2 ]] ; then
473 echo 1>&2
474 multitree_lunch_help
475 return $code
476 elif [[ $code -ne 0 ]] ; then
477 return $code
478 fi
479 else
480 # All other calls go through the --lunch variant of lunch.py
LaMont Jonesc39e5022022-06-23 19:09:06 +0000481 results=($(${T}/build/build/make/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700482 code=$?
483 if [[ $code -eq 2 ]] ; then
484 echo 1>&2
485 multitree_lunch_help
486 return $code
487 elif [[ $code -ne 0 ]] ; then
488 return $code
489 fi
490
491 export TARGET_BUILD_COMBO=${results[0]}
492 export TARGET_BUILD_VARIANT=${results[1]}
493 fi
494}
495
Matt Alexanderd9c56562020-05-21 10:49:17 +0000496function choosetype()
497{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498 echo "Build type choices are:"
499 echo " 1. release"
500 echo " 2. debug"
501 echo
502
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800503 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700504 DEFAULT_NUM=1
505 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800507 export TARGET_BUILD_TYPE=
508 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 while [ -z $TARGET_BUILD_TYPE ]
510 do
511 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000512 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800513 read ANSWER
514 else
515 echo $1
516 ANSWER=$1
517 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700518 case $ANSWER in
519 "")
520 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
521 ;;
522 1)
523 export TARGET_BUILD_TYPE=release
524 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800525 release)
526 export TARGET_BUILD_TYPE=release
527 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700528 2)
529 export TARGET_BUILD_TYPE=debug
530 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800531 debug)
532 export TARGET_BUILD_TYPE=debug
533 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534 *)
535 echo
536 echo "I didn't understand your response. Please try again."
537 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700538 ;;
539 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000540 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800541 break
542 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700543 done
544
Ying Wang08800fd2016-03-03 20:57:21 -0800545 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800547 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700548}
549
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800550#
551# This function isn't really right: It chooses a TARGET_PRODUCT
552# based on the list of boards. Usually, that gets you something
553# that kinda works with a generic product, but really, you should
554# pick a product by name.
555#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000556function chooseproduct()
557{
Christopher Ferris55257d22017-03-23 11:08:58 -0700558 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000559 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560 default_value=$TARGET_PRODUCT
561 else
Ying Wang0a76df52015-06-08 11:57:26 -0700562 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700563 fi
564
Ying Wang08800fd2016-03-03 20:57:21 -0800565 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 export TARGET_PRODUCT=
567 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700568 while [ -z "$TARGET_PRODUCT" ]
569 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700570 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000571 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800572 read ANSWER
573 else
574 echo $1
575 ANSWER=$1
576 fi
577
Matt Alexanderd9c56562020-05-21 10:49:17 +0000578 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800580 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000581 if check_product $ANSWER
582 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800583 export TARGET_PRODUCT=$ANSWER
584 else
585 echo "** Not a valid product: $ANSWER"
586 fi
587 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000588 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 fi
591 done
592
Ying Wang08800fd2016-03-03 20:57:21 -0800593 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800595 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596}
597
Matt Alexanderd9c56562020-05-21 10:49:17 +0000598function choosevariant()
599{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800600 echo "Variant choices are:"
601 local index=1
602 local v
603 for v in ${VARIANT_CHOICES[@]}
604 do
605 # The product name is the name of the directory containing
606 # the makefile we found, above.
607 echo " $index. $v"
608 index=$(($index+1))
609 done
610
611 local default_value=eng
612 local ANSWER
613
614 export TARGET_BUILD_VARIANT=
615 while [ -z "$TARGET_BUILD_VARIANT" ]
616 do
617 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000618 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 read ANSWER
620 else
621 echo $1
622 ANSWER=$1
623 fi
624
Matt Alexanderd9c56562020-05-21 10:49:17 +0000625 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000627 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
628 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200629 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800630 fi
631 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000632 if check_variant $ANSWER
633 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800634 export TARGET_BUILD_VARIANT=$ANSWER
635 else
636 echo "** Not a valid variant: $ANSWER"
637 fi
638 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000639 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640 break
641 fi
642 done
643}
644
Matt Alexanderd9c56562020-05-21 10:49:17 +0000645function choosecombo()
646{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700647 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648
649 echo
650 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700651 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700652
653 echo
654 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700655 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800656
657 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800658 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700659 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800660 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800661 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700662}
663
Matt Alexanderd9c56562020-05-21 10:49:17 +0000664function add_lunch_combo()
665{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800666 if [ -n "$ZSH_VERSION" ]; then
667 echo -n "${funcfiletrace[1]}: "
668 else
669 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
670 fi
671 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 -0800672}
673
Matt Alexanderd9c56562020-05-21 10:49:17 +0000674function print_lunch_menu()
675{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700676 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100677 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700678 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 +0100679 local ret=$?
680
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700681 echo
682 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700683 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100684
Matt Alexanderd9c56562020-05-21 10:49:17 +0000685 if [ $ret -ne 0 ]
686 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100687 echo "Warning: Cannot display lunch menu."
688 echo
689 echo "Note: You can invoke lunch with an explicit target:"
690 echo
691 echo " usage: lunch [target]" >&2
692 echo
693 return
694 fi
695
Will Burr40401202022-02-07 12:12:01 +0000696 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800697
698 local i=1
699 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700700 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800701 do
702 echo " $i. $choice"
703 i=$(($i+1))
704 done
705
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700706 echo
707}
708
Matt Alexanderd9c56562020-05-21 10:49:17 +0000709function lunch()
710{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800711 local answer
712
Steven Moreland92793dc2020-02-25 18:30:18 -0800713 if [[ $# -gt 1 ]]; then
714 echo "usage: lunch [target]" >&2
715 return 1
716 fi
717
Will Burr40401202022-02-07 12:12:01 +0000718 local used_lunch_menu=0
719
Steven Moreland92793dc2020-02-25 18:30:18 -0800720 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800721 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700722 else
723 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000724 echo "Which would you like? [aosp_arm-eng]"
725 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 -0800726 read answer
Will Burr40401202022-02-07 12:12:01 +0000727 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700728 fi
729
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800730 local selection=
731
Matt Alexanderd9c56562020-05-21 10:49:17 +0000732 if [ -z "$answer" ]
733 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700734 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000735 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
736 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800737 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000738 if [ $answer -le ${#choices[@]} ]
739 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800740 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000741 if [ -n "$ZSH_VERSION" ]
742 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800743 selection=${choices[$(($answer))]}
744 else
745 selection=${choices[$(($answer-1))]}
746 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800747 fi
Colin Cross88737132017-03-21 17:41:03 -0700748 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800749 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700750 fi
751
Joe Onoratoda12daf2010-06-09 18:18:31 -0700752 export TARGET_BUILD_APPS=
753
Colin Cross88737132017-03-21 17:41:03 -0700754 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700755 product=${selection%%-*} # Trim everything after first dash
756 variant_and_version=${selection#*-} # Trim everything up to first dash
757 if [ "$variant_and_version" != "$selection" ]; then
758 variant=${variant_and_version%%-*}
759 if [ "$variant" != "$variant_and_version" ]; then
760 version=${variant_and_version#*-}
761 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700762 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800763
Matt Alexanderd9c56562020-05-21 10:49:17 +0000764 if [ -z "$product" ]
765 then
Ying Wang08800fd2016-03-03 20:57:21 -0800766 echo
Colin Cross88737132017-03-21 17:41:03 -0700767 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700768 return 1
769 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800770
Colin Cross88737132017-03-21 17:41:03 -0700771 TARGET_PRODUCT=$product \
772 TARGET_BUILD_VARIANT=$variant \
773 TARGET_PLATFORM_VERSION=$version \
774 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000775 if [ $? -ne 0 ]
776 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100777 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
778 then
779 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
780 fi
Colin Cross88737132017-03-21 17:41:03 -0700781 return 1
782 fi
Colin Cross88737132017-03-21 17:41:03 -0700783 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
784 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700785 if [ -n "$version" ]; then
786 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
787 else
788 unset TARGET_PLATFORM_VERSION
789 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700790 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700791
Will Burr40401202022-02-07 12:12:01 +0000792 if [ $used_lunch_menu -eq 1 ]; then
793 echo
794 echo "Hint: next time you can simply run 'lunch $selection'"
795 fi
796
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700797 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800798
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700799 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700800 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800801 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700802}
803
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700804unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700805# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000806function _lunch()
807{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700808 local cur prev opts
809 COMPREPLY=()
810 cur="${COMP_WORDS[COMP_CWORD]}"
811 prev="${COMP_WORDS[COMP_CWORD-1]}"
812
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700813 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800814 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700815 fi
816
817 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700818 return 0
819}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700820
Joe Onoratoda12daf2010-06-09 18:18:31 -0700821# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700822# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000823function tapas()
824{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700825 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800826 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700827 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700828 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Colin Cross5ff43802022-01-27 18:15:53 -0800829 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
830 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)"
831
Joe Onoratoda12daf2010-06-09 18:18:31 -0700832
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700833 if [ "$showHelp" != "" ]; then
834 $(gettop)/build/make/tapasHelp.sh
835 return
836 fi
837
Ying Wang67f02922012-08-22 10:25:20 -0700838 if [ $(echo $arch | wc -w) -gt 1 ]; then
839 echo "tapas: Error: Multiple build archs supplied: $arch"
840 return
841 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700842 if [ $(echo $variant | wc -w) -gt 1 ]; then
843 echo "tapas: Error: Multiple build variants supplied: $variant"
844 return
845 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700846 if [ $(echo $density | wc -w) -gt 1 ]; then
847 echo "tapas: Error: Multiple densities supplied: $density"
848 return
849 fi
Colin Cross5ff43802022-01-27 18:15:53 -0800850 if [ $(echo $keys | wc -w) -gt 1 ]; then
851 echo "tapas: Error: Multiple keys supplied: $keys"
852 return
853 fi
Ying Wang67f02922012-08-22 10:25:20 -0700854
Ying Wang0a76df52015-06-08 11:57:26 -0700855 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700856 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000857 x86) product=aosp_x86;;
858 arm64) product=aosp_arm64;;
859 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700860 esac
Colin Cross5ff43802022-01-27 18:15:53 -0800861 if [ -n "$keys" ]; then
862 product=${product/aosp_/aosp_${keys}_}
863 fi;
864
Joe Onoratoda12daf2010-06-09 18:18:31 -0700865 if [ -z "$variant" ]; then
866 variant=eng
867 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700868 if [ -z "$apps" ]; then
869 apps=all
870 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600871 if [ -z "$density" ]; then
872 density=alldpi
873 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700874
Ying Wang67f02922012-08-22 10:25:20 -0700875 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700876 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700877 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700878 export TARGET_BUILD_TYPE=release
879 export TARGET_BUILD_APPS=$apps
880
Ying Wang08800fd2016-03-03 20:57:21 -0800881 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700882 set_stuff_for_environment
883 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800884 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700885}
886
Martin Stjernholmf692c752021-04-12 00:01:10 +0100887# Configures the build to build unbundled Android modules (APEXes).
888# Run banchan with one or more module names (from apex{} modules).
889function banchan()
890{
891 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100892 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100893 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100894 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 +0100895
896 if [ "$showHelp" != "" ]; then
897 $(gettop)/build/make/banchanHelp.sh
898 return
899 fi
900
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100901 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000902 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100903 elif [ $(echo $product | wc -w) -gt 1 ]; then
904 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100905 return
906 fi
907 if [ $(echo $variant | wc -w) -gt 1 ]; then
908 echo "banchan: Error: Multiple build variants supplied: $variant"
909 return
910 fi
911 if [ -z "$apps" ]; then
912 echo "banchan: Error: No modules supplied"
913 return
914 fi
915
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100916 case $product in
917 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100918 x86) product=module_x86;;
919 arm64) product=module_arm64;;
920 x86_64) product=module_x86_64;;
921 esac
922 if [ -z "$variant" ]; then
923 variant=eng
924 fi
925
926 export TARGET_PRODUCT=$product
927 export TARGET_BUILD_VARIANT=$variant
928 export TARGET_BUILD_DENSITY=alldpi
929 export TARGET_BUILD_TYPE=release
930
931 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
932 # case is different and it may diverge in the future.
933 export TARGET_BUILD_APPS=$apps
934
935 build_build_var_cache
936 set_stuff_for_environment
937 printconfig
938 destroy_build_var_cache
939}
940
Matt Alexanderd9c56562020-05-21 10:49:17 +0000941function gettop
942{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700943 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000944 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700945 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000946 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700947 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000948 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800949 # The following circumlocution (repeated below as well) ensures
950 # that we record the true directory name and not one that is
951 # faked up with symlink names.
952 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700953 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800954 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700955 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000956 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800957 \cd ..
synergyb112a402013-07-05 19:47:47 -0700958 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700959 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000960 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700961 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000962 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700963 fi
964 fi
965 fi
966}
967
Joe Onorato7cf6f972022-05-11 21:39:57 -0700968# TODO: Merge into gettop as part of launching multitree
969function multitree_gettop
970{
971 local TOPFILE=build/build/make/core/envsetup.mk
972 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
973 # The following circumlocution ensures we remove symlinks from TOP.
974 (cd "$TOP"; PWD= /bin/pwd)
975 else
976 if [ -f $TOPFILE ] ; then
977 # The following circumlocution (repeated below as well) ensures
978 # that we record the true directory name and not one that is
979 # faked up with symlink names.
980 PWD= /bin/pwd
981 else
982 local HERE=$PWD
983 local T=
984 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
985 \cd ..
986 T=`PWD= /bin/pwd -P`
987 done
988 \cd "$HERE"
989 if [ -f "$T/$TOPFILE" ]; then
990 echo "$T"
991 fi
992 fi
993 fi
994}
995
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996function croot()
997{
Christopher Ferris55257d22017-03-23 11:08:58 -0700998 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700999 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -07001000 if [ "$1" ]; then
1001 \cd $(gettop)/$1
1002 else
1003 \cd $(gettop)
1004 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001005 else
1006 echo "Couldn't locate the top of the tree. Try setting TOP."
1007 fi
1008}
1009
Matt Alexanderd9c56562020-05-21 10:49:17 +00001010function _croot()
1011{
Anton Hanssonece9c482019-02-04 18:15:39 +00001012 local T=$(gettop)
1013 if [ "$T" ]; then
1014 local cur="${COMP_WORDS[COMP_CWORD]}"
1015 k=0
1016 for c in $(compgen -d ${T}/${cur}); do
1017 COMPREPLY[k++]=${c#${T}/}/
1018 done
1019 fi
1020}
1021
Matt Alexanderd9c56562020-05-21 10:49:17 +00001022function cproj()
1023{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001024 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001025 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001026 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001027 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1028 T=$PWD
1029 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001030 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001031 return
1032 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001033 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001034 done
Ying Wang9cd17642012-12-13 10:52:07 -08001035 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001036 echo "can't find Android.mk"
1037}
1038
Daniel Sandler47e0a882013-07-30 13:23:52 -04001039# simplified version of ps; output in the form
1040# <pid> <procname>
1041function qpid() {
1042 local prepend=''
1043 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001044 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001045 prepend=' '
1046 append='$'
1047 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001048 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001049 echo "usage: qpid [[--exact] <process name|pid>"
1050 return 255
1051 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001052
1053 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001054 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001055 qpid | \grep "$prepend$EXE$append"
1056 else
1057 adb shell ps \
1058 | tr -d '\r' \
1059 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1060 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001061}
1062
Steven Moreland74114f12020-09-10 01:23:32 +00001063# syswrite - disable verity, reboot if needed, and remount image
1064#
1065# Easy way to make system.img/etc writable
1066function syswrite() {
1067 adb wait-for-device && adb root || return 1
1068 if [[ $(adb disable-verity | grep "reboot") ]]; then
1069 echo "rebooting"
1070 adb reboot && adb wait-for-device && adb root || return 1
1071 fi
1072 adb wait-for-device && adb remount || return 1
1073}
1074
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001075# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001076# that has the core-file-size limit set correctly
1077#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001078# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001079# if its core-file-size limit is not set already.
1080# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1081
Matt Alexanderd9c56562020-05-21 10:49:17 +00001082function coredump_setup()
1083{
1084 echo "Getting root...";
1085 adb root;
1086 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001087
Matt Alexanderd9c56562020-05-21 10:49:17 +00001088 echo "Remounting root partition read-write...";
1089 adb shell mount -w -o remount -t rootfs rootfs;
1090 sleep 1;
1091 adb wait-for-device;
1092 adb shell mkdir -p /cores;
1093 adb shell mount -t tmpfs tmpfs /cores;
1094 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001095
Matt Alexanderd9c56562020-05-21 10:49:17 +00001096 echo "Granting SELinux permission to dump in /cores...";
1097 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001098
Matt Alexanderd9c56562020-05-21 10:49:17 +00001099 echo "Set core pattern.";
1100 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001101
Ying Wang08800fd2016-03-03 20:57:21 -08001102 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001103}
1104
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001105# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001106# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001107#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001108# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001109# dump to actually be generated.
1110
Matt Alexanderd9c56562020-05-21 10:49:17 +00001111function coredump_enable()
1112{
1113 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001114 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115 printf "Expecting a PID!\n";
1116 return;
1117 fi;
1118 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001119 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001120}
1121
1122# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001123# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001125# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126# enabled globally.
1127
Matt Alexanderd9c56562020-05-21 10:49:17 +00001128function core()
1129{
1130 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001131
Ying Wang08800fd2016-03-03 20:57:21 -08001132 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001133 printf "Expecting a PID!\n";
1134 return;
1135 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001136
Matt Alexanderd9c56562020-05-21 10:49:17 +00001137 local CORENAME=core.$PID;
1138 local COREPATH=/cores/$CORENAME;
1139 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001140
Matt Alexanderd9c56562020-05-21 10:49:17 +00001141 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001142
Matt Alexanderd9c56562020-05-21 10:49:17 +00001143 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001144 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1146 adb shell kill -$SIG $PID;
1147 sleep 1;
1148 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001149
Matt Alexanderd9c56562020-05-21 10:49:17 +00001150 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1151 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001152}
1153
Christopher Tate744ee802009-11-12 15:33:08 -08001154# systemstack - dump the current stack trace of all threads in the system process
1155# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156function systemstack()
1157{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001158 stacks system_server
1159}
1160
Michael Wrightaeed7212014-06-19 19:58:12 -07001161# Read the ELF header from /proc/$PID/exe to determine if the process is
1162# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001163function is64bit()
1164{
Ben Chengfba67bf2014-02-25 10:27:07 -08001165 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001166 if [ "$PID" ] ; then
1167 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001168 echo "64"
1169 else
1170 echo ""
1171 fi
1172 else
1173 echo ""
1174 fi
1175}
1176
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001177case `uname -s` in
1178 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001179 function sgrep()
1180 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001181 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 -04001182 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001183 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001184
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001185 ;;
1186 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001187 function sgrep()
1188 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001189 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 -04001190 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001191 }
1192 ;;
1193esac
1194
Matt Alexanderd9c56562020-05-21 10:49:17 +00001195function gettargetarch
1196{
Raghu Gandham8da43102012-07-25 19:57:22 -07001197 get_build_var TARGET_ARCH
1198}
1199
Matt Alexanderd9c56562020-05-21 10:49:17 +00001200function ggrep()
1201{
Mike Frysinger5e479732015-09-22 18:13:48 -04001202 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1203 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001204}
1205
Matt Alexanderd9c56562020-05-21 10:49:17 +00001206function gogrep()
1207{
Orion Hodson831472d2019-10-25 11:35:15 +01001208 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1209 -exec grep --color -n "$@" {} +
1210}
1211
Matt Alexanderd9c56562020-05-21 10:49:17 +00001212function jgrep()
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 "*\.java" \
1215 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216}
1217
Alistair Delva176e5342021-02-22 13:31:26 -08001218function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001219{
1220 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1221 -exec grep --color -n "$@" {} +
1222}
1223
Taesu Leeea0cecd2020-10-28 11:05:18 +09001224function ktgrep()
1225{
1226 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1227 -exec grep --color -n "$@" {} +
1228}
1229
Matt Alexanderd9c56562020-05-21 10:49:17 +00001230function cgrep()
1231{
Mike Frysinger5e479732015-09-22 18:13:48 -04001232 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' \) \
1233 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001234}
1235
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236function resgrep()
1237{
Christopher Ferris55257d22017-03-23 11:08:58 -07001238 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001239 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1240 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1241 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001242}
1243
Matt Alexanderd9c56562020-05-21 10:49:17 +00001244function mangrep()
1245{
Mike Frysinger5e479732015-09-22 18:13:48 -04001246 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1247 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001248}
1249
Matt Alexanderd9c56562020-05-21 10:49:17 +00001250function owngrep()
1251{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001252 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1253 -exec grep --color -n "$@" {} +
1254}
1255
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256function sepgrep()
1257{
Mike Frysinger5e479732015-09-22 18:13:48 -04001258 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1259 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001260}
1261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262function rcgrep()
1263{
Mike Frysinger5e479732015-09-22 18:13:48 -04001264 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1265 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001266}
1267
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001268case `uname -s` in
1269 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001270 function mgrep()
1271 {
Orion Hodson831472d2019-10-25 11:35:15 +01001272 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 -04001273 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001274 }
1275
Matt Alexanderd9c56562020-05-21 10:49:17 +00001276 function treegrep()
1277 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001278 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 -04001279 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001280 }
1281
1282 ;;
1283 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001284 function mgrep()
1285 {
Orion Hodson831472d2019-10-25 11:35:15 +01001286 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 -04001287 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001288 }
1289
Matt Alexanderd9c56562020-05-21 10:49:17 +00001290 function treegrep()
1291 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001292 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 -04001293 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001294 }
1295
1296 ;;
1297esac
1298
Matt Alexanderd9c56562020-05-21 10:49:17 +00001299function getprebuilt
1300{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301 get_abs_build_var ANDROID_PREBUILTS
1302}
1303
Matt Alexanderd9c56562020-05-21 10:49:17 +00001304function tracedmdump()
1305{
Christopher Ferris55257d22017-03-23 11:08:58 -07001306 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001307 if [ ! "$T" ]; then
1308 echo "Couldn't locate the top of the tree. Try setting TOP."
1309 return
1310 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001311 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001312 local arch=$(gettargetarch)
1313 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001314
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001315 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001316 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001317 echo "usage: tracedmdump tracename"
1318 return
1319 fi
1320
Matt Alexanderd9c56562020-05-21 10:49:17 +00001321 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001322 echo "Error: cannot find kernel: '$KERNEL'"
1323 return
1324 fi
1325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001326 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001327 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001328 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1329 fi
1330
1331 echo "post-processing traces..."
1332 rm -f $TRACE/qtrace.dexlist
1333 post_trace $TRACE
1334 if [ $? -ne 0 ]; then
1335 echo "***"
1336 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1337 echo "***"
1338 return
1339 fi
1340 echo "generating dexlist output..."
1341 /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
1342 echo "generating dmtrace data..."
1343 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1344 echo "generating html file..."
1345 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1346 echo "done, see $TRACE/dmtrace.html for details"
1347 echo "or run:"
1348 echo " traceview $TRACE/dmtrace"
1349}
1350
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001351# communicate with a running device or emulator, set up necessary state,
1352# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001353function runhat()
1354{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001355 # process standard adb options
1356 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001357 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001358 adbTarget=$1
1359 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001360 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001361 adbTarget="$1 $2"
1362 shift 2
1363 fi
1364 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001365 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001366
1367 # runhat options
1368 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001369
Matt Alexanderd9c56562020-05-21 10:49:17 +00001370 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001371 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001372 return
1373 fi
1374
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001375 # confirm hat is available
1376 if [ -z $(which hat) ]; then
1377 echo "hat is not available in this configuration."
1378 return
1379 fi
1380
Andy McFaddenb6289852010-07-12 08:00:19 -07001381 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001382 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001383 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001384 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001385 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001386 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001387 echo -n "> "
1388 read
1389
The Android Open Source Project88b60792009-03-03 19:28:42 -08001390 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001391
The Android Open Source Project88b60792009-03-03 19:28:42 -08001392 echo "Retrieving file $devFile..."
1393 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394
The Android Open Source Project88b60792009-03-03 19:28:42 -08001395 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001396
The Android Open Source Project88b60792009-03-03 19:28:42 -08001397 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001398 echo "View the output by pointing your browser at http://localhost:7000/"
1399 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001400 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001401}
1402
Matt Alexanderd9c56562020-05-21 10:49:17 +00001403function getbugreports()
1404{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001405 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001406
1407 if [ ! "$reports" ]; then
1408 echo "Could not locate any bugreports."
1409 return
1410 fi
1411
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001412 local report
1413 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001414 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001415 echo "/sdcard/bugreports/${report}"
1416 adb pull /sdcard/bugreports/${report} ${report}
1417 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001418 done
1419}
1420
Matt Alexanderd9c56562020-05-21 10:49:17 +00001421function getsdcardpath()
1422{
Victoria Lease1b296b42012-08-21 15:44:06 -07001423 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1424}
1425
Matt Alexanderd9c56562020-05-21 10:49:17 +00001426function getscreenshotpath()
1427{
Victoria Lease1b296b42012-08-21 15:44:06 -07001428 echo "$(getsdcardpath)/Pictures/Screenshots"
1429}
1430
Matt Alexanderd9c56562020-05-21 10:49:17 +00001431function getlastscreenshot()
1432{
Victoria Lease1b296b42012-08-21 15:44:06 -07001433 local screenshot_path=$(getscreenshotpath)
1434 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 +00001435 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001436 echo "No screenshots found."
1437 return
1438 fi
1439 echo "${screenshot}"
1440 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1441}
1442
Matt Alexanderd9c56562020-05-21 10:49:17 +00001443function startviewserver()
1444{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001445 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001446 if [ $# -gt 0 ]; then
1447 port=$1
1448 fi
1449 adb shell service call window 1 i32 $port
1450}
1451
Matt Alexanderd9c56562020-05-21 10:49:17 +00001452function stopviewserver()
1453{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001454 adb shell service call window 2
1455}
1456
Matt Alexanderd9c56562020-05-21 10:49:17 +00001457function isviewserverstarted()
1458{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001459 adb shell service call window 3
1460}
1461
Matt Alexanderd9c56562020-05-21 10:49:17 +00001462function key_home()
1463{
Romain Guyb84049a2010-10-04 16:56:11 -07001464 adb shell input keyevent 3
1465}
1466
Matt Alexanderd9c56562020-05-21 10:49:17 +00001467function key_back()
1468{
Romain Guyb84049a2010-10-04 16:56:11 -07001469 adb shell input keyevent 4
1470}
1471
Matt Alexanderd9c56562020-05-21 10:49:17 +00001472function key_menu()
1473{
Romain Guyb84049a2010-10-04 16:56:11 -07001474 adb shell input keyevent 82
1475}
1476
Matt Alexanderd9c56562020-05-21 10:49:17 +00001477function smoketest()
1478{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001479 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1480 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1481 return
1482 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001483 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001484 if [ ! "$T" ]; then
1485 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1486 return
1487 fi
1488
Ying Wang9cd17642012-12-13 10:52:07 -08001489 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001490 adb uninstall com.android.smoketest > /dev/null &&
1491 adb uninstall com.android.smoketest.tests > /dev/null &&
1492 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1493 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1494 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1495}
1496
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001497# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001498function runtest()
1499{
Christopher Ferris55257d22017-03-23 11:08:58 -07001500 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001501 if [ ! "$T" ]; then
1502 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1503 return
1504 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001505 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001506}
1507
The Android Open Source Project88b60792009-03-03 19:28:42 -08001508function godir () {
1509 if [[ -z "$1" ]]; then
1510 echo "Usage: godir <regex>"
1511 return
1512 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001513 local T=$(gettop)
1514 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001515 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001516 mkdir -p $OUT_DIR
1517 FILELIST=$OUT_DIR/filelist
1518 else
1519 FILELIST=$T/filelist
1520 fi
1521 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001522 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001523 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001524 echo " Done"
1525 echo ""
1526 fi
1527 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001528 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001529 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001530 echo "Not found"
1531 return
1532 fi
1533 local pathname
1534 local choice
1535 if [[ ${#lines[@]} > 1 ]]; then
1536 while [[ -z "$pathname" ]]; do
1537 local index=1
1538 local line
1539 for line in ${lines[@]}; do
1540 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001541 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001542 done
1543 echo
1544 echo -n "Select one: "
1545 unset choice
1546 read choice
1547 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1548 echo "Invalid choice"
1549 continue
1550 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001551 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001552 done
1553 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001554 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001555 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001556 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001557}
1558
Steven Moreland62054a42018-12-06 10:11:40 -08001559# Update module-info.json in out.
1560function refreshmod() {
1561 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1562 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1563 return 1
1564 fi
1565
1566 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1567
1568 # for the output of the next command
1569 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1570
1571 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001572 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001573 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1574}
1575
Cole Faust3e192382021-10-25 13:29:15 -07001576# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001577function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001578 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001579 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1580 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1581 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001582 return 1
1583 fi
1584
1585 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001586 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001587 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001588 fi
1589 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001590 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001591}
1592
1593# List all modules for the current device, as cached in module-info.json. If any build change is
1594# made and it should be reflected in the output, you should run 'refreshmod' first.
1595function allmod() {
1596 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001597
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001598 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 -08001599}
1600
Joe Onorato2c1aa472021-02-25 16:42:39 -08001601# Get the path of a specific module in the android tree, as cached in module-info.json.
1602# If any build change is made, and it should be reflected in the output, you should run
1603# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001604function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001605 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001606 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001607 return 1
1608 fi
1609
Cole Faust24c36db2021-01-23 02:39:37 +00001610 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001611
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001612 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001613module = '$1'
1614module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1615if module not in module_info:
1616 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001617print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001618
1619 if [ -z "$relpath" ]; then
1620 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1621 return 1
1622 else
Rett Berg78d1c932019-01-24 14:34:23 -08001623 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001624 fi
1625}
1626
Joe Onorato2c1aa472021-02-25 16:42:39 -08001627# Get the path of a specific module in the android tree, as cached in module-info.json.
1628# If any build change is made, and it should be reflected in the output, you should run
1629# 'refreshmod' first. Note: This is the inverse of pathmod.
1630function dirmods() {
1631 if [[ $# -ne 1 ]]; then
1632 echo "usage: dirmods <path>" >&2
1633 return 1
1634 fi
1635
1636 verifymodinfo || return 1
1637
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001638 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001639dir = '$1'
1640while dir.endswith('/'):
1641 dir = dir[:-1]
1642prefix = dir + '/'
1643module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1644results = set()
1645for m in module_info.values():
1646 for path in m.get(u'path', []):
1647 if path == dir or path.startswith(prefix):
1648 name = m.get(u'module_name')
1649 if name:
1650 results.add(name)
1651for name in sorted(results):
1652 print(name)
1653"
1654}
1655
1656
Rett Berg78d1c932019-01-24 14:34:23 -08001657# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1658# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1659function gomod() {
1660 if [[ $# -ne 1 ]]; then
1661 echo "usage: gomod <module>" >&2
1662 return 1
1663 fi
1664
1665 local path="$(pathmod $@)"
1666 if [ -z "$path" ]; then
1667 return 1
1668 fi
1669 cd $path
1670}
1671
Cole Faust24c36db2021-01-23 02:39:37 +00001672# Gets the list of a module's installed outputs, as cached in module-info.json.
1673# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1674function outmod() {
1675 if [[ $# -ne 1 ]]; then
1676 echo "usage: outmod <module>" >&2
1677 return 1
1678 fi
1679
1680 verifymodinfo || return 1
1681
1682 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001683 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001684module = '$1'
1685module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1686if module not in module_info:
1687 exit(1)
1688for output in module_info[module]['installed']:
1689 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1690
1691 if [ $? -ne 0 ]; then
1692 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1693 return 1
1694 elif [ ! -z "$relpath" ]; then
1695 echo "$relpath"
1696 fi
1697}
1698
1699# adb install a module's apk, as cached in module-info.json. If any build change
1700# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1701# Usage: installmod [adb install arguments] <module>
1702# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1703function installmod() {
1704 if [[ $# -eq 0 ]]; then
1705 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001706 echo "" >&2
1707 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1708 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1709 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001710 return 1
1711 fi
1712
1713 local _path
1714 _path=$(outmod ${@:$#:1})
1715 if [ $? -ne 0 ]; then
1716 return 1
1717 fi
1718
1719 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1720 if [ -z "$_path" ]; then
1721 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1722 return 1
1723 fi
Cole Faust3e192382021-10-25 13:29:15 -07001724 local serial_device=""
1725 if [[ "$1" == "-s" ]]; then
1726 if [[ $# -le 2 ]]; then
1727 echo "-s requires an argument" >&2
1728 return 1
1729 fi
1730 serial_device="-s $2"
1731 shift 2
1732 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001733 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001734 echo adb $serial_device install ${@:1:$length} $_path
1735 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001736}
1737
dimitry73b84812018-12-11 18:06:00 +01001738function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001739 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001740 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001741}
1742
Alex Rayf0d08eb2013-03-08 15:15:06 -08001743# Print colored exit condition
1744function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001745 "$@"
1746 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001747 if [ $retval -ne 0 ]
1748 then
Jacky Cao89483b82015-05-15 22:12:53 +08001749 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001750 else
Jacky Cao89483b82015-05-15 22:12:53 +08001751 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001752 fi
1753 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001754}
1755
Matt Alexanderd9c56562020-05-21 10:49:17 +00001756function get_make_command()
1757{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001758 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1759 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001760 # Always use the real make if -C is passed in
1761 for arg in "$@"; do
1762 if [[ $arg == -C* ]]; then
1763 echo command make
1764 return
1765 fi
1766 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001767 echo build/soong/soong_ui.bash --make-mode
1768 else
1769 echo command make
1770 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001771}
1772
Matt Alexanderd9c56562020-05-21 10:49:17 +00001773function _wrap_build()
1774{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001775 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1776 "$@"
1777 return $?
1778 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001779 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001780 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001781 local ret=$?
1782 local end_time=$(date +"%s")
1783 local tdiff=$(($end_time-$start_time))
1784 local hours=$(($tdiff / 3600 ))
1785 local mins=$((($tdiff % 3600) / 60))
1786 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001787 local ncolors=$(tput colors 2>/dev/null)
1788 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001789 color_failed=$'\E'"[0;31m"
1790 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001791 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001792 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001793 else
1794 color_failed=""
1795 color_success=""
1796 color_reset=""
1797 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001798
1799 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1800 echo
1801 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1802 fi
1803
Ed Heylcc6be0a2014-06-18 14:55:58 -07001804 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001805 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001806 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001807 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001808 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001809 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001810 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001811 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001812 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001813 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001814 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001815 printf "(%s seconds)" $secs
1816 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001817 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001818 echo
1819 return $ret
1820}
1821
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001822function _trigger_build()
1823(
1824 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001825 local T=$(gettop)
1826 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001827 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1828 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001829 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1830 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001831 fi
1832)
1833
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001834# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001835function b()
1836(
1837 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Sam Delmerico9d67a902022-04-11 12:25:18 +00001838 _trigger_build "all-modules" bp2build USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001839 # Then, run Bazel using the synthetic workspace as the --package_path.
Sam Delmerico9d67a902022-04-11 12:25:18 +00001840 if [[ -z "$@" ]]; then
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001841 # If there are no args, show help.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001842 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001843 else
1844 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Sam Delmerico9d67a902022-04-11 12:25:18 +00001845 bazel "$@" --config=bp2build
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001846 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001847)
1848
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001849function m()
1850(
1851 _trigger_build "all-modules" "$@"
1852)
1853
1854function mm()
1855(
1856 _trigger_build "modules-in-a-dir-no-deps" "$@"
1857)
1858
1859function mmm()
1860(
1861 _trigger_build "modules-in-dirs-no-deps" "$@"
1862)
1863
1864function mma()
1865(
1866 _trigger_build "modules-in-a-dir" "$@"
1867)
1868
1869function mmma()
1870(
1871 _trigger_build "modules-in-dirs" "$@"
1872)
1873
Matt Alexanderd9c56562020-05-21 10:49:17 +00001874function make()
1875{
Dan Willemsene9842242017-07-28 13:00:13 -07001876 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001877}
1878
Joe Onorato7cf6f972022-05-11 21:39:57 -07001879function _multitree_lunch_error()
1880{
1881 >&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."
1882}
1883
1884function multitree_build()
1885{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001886 local T=$(multitree_gettop)
1887 if [ -n "$T" ]; then
1888 "$T/build/build/make/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001889 else
1890 _multitree_lunch_error
1891 return 1
1892 fi
1893}
1894
Matt Alexanderd9c56562020-05-21 10:49:17 +00001895function provision()
1896{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001897 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1898 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1899 return 1
1900 fi
1901 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1902 echo "There is no provisioning script for the device." >&2
1903 return 1
1904 fi
1905
1906 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001907 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001908 shift 1
1909 else
1910 echo "This action will reflash your device."
1911 echo ""
1912 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1913 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001914 echo -n "Are you sure you want to do this (yes/no)? "
1915 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001916 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001917 echo "Not taking any action. Exiting." >&2
1918 return 1
1919 fi
1920 fi
1921 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1922}
1923
Jim Tanga881a252018-06-19 16:34:41 +08001924# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001925function enable_zsh_completion() {
1926 # Don't override user's options if bash-style completion is already enabled.
1927 if ! declare -f complete >/dev/null; then
1928 autoload -U compinit && compinit
1929 autoload -U bashcompinit && bashcompinit
1930 fi
Jim Tanga881a252018-06-19 16:34:41 +08001931}
1932
1933function validate_current_shell() {
1934 local current_sh="$(ps -o command -p $$)"
1935 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001936 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001937 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001938 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001939 *zsh*)
1940 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001941 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001942 *)
Jim Tanga881a252018-06-19 16:34:41 +08001943 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 -07001944 ;;
1945 esac
Jim Tanga881a252018-06-19 16:34:41 +08001946}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001947
1948# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001949# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1950# load those.
1951#
1952# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001953function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001954 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001955 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001956 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001957 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 -08001958 if [ -n "$allowed" ]; then
1959 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1960 echo " $allowed"
1961 echo " $f"
1962 return
1963 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001964 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001965 done
1966
1967 allowed_files=
1968 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001969 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001970 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001971 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001972
1973 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001974 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001975 else
1976 echo "ignoring $f, not in $allowed"
1977 fi
Jim Tanga881a252018-06-19 16:34:41 +08001978 done
1979 done
1980}
Kenny Root52aa81c2011-07-15 11:07:06 -07001981
Dan Albertbab814f2020-08-26 15:34:53 -07001982function showcommands() {
1983 local T=$(gettop)
1984 if [[ -z "$TARGET_PRODUCT" ]]; then
1985 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1986 return
1987 fi
1988 case $(uname -s) in
1989 Darwin)
1990 PREBUILT_NAME=darwin-x86
1991 ;;
1992 Linux)
1993 PREBUILT_NAME=linux-x86
1994 ;;
1995 *)
1996 >&2 echo Unknown host $(uname -s)
1997 return
1998 ;;
1999 esac
2000 if [[ -z "$OUT_DIR" ]]; then
2001 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
2002 OUT_DIR=out
2003 else
2004 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
2005 fi
2006 fi
2007 if [[ "$1" == "--regenerate" ]]; then
2008 shift 1
2009 NINJA_ARGS="-t commands $@" m
2010 else
2011 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2012 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2013 -t commands "$@")
2014 fi
2015}
2016
Jim Tanga881a252018-06-19 16:34:41 +08002017validate_current_shell
2018source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002019addcompletions