blob: 8b0865dc8c498c0ded4e2ef1f74bbad6efcd708d [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010012 Sets up the build environment for building unbundled apps (APKs).
Anton Hansson90ac61c2022-09-06 14:36:00 +000013- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64|arm64_only|x86_64only] \
14 [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010015 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000016- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080017- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070018- mm: Builds and installs all of the modules in the current directory, and their
19 dependencies.
20- mmm: Builds and installs all of the modules in the supplied directories, and their
21 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080022 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070023- mma: Same as 'mm'
24- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080025- provision: Flash device with all required partitions. Options will be passed on to fastboot.
26- cgrep: Greps on all local C/C++ files.
27- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010028- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080029- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090030- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080031- resgrep: Greps on all local res/*.xml files.
32- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070033- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060034- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080035- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080036- sepgrep: Greps on all local sepolicy files.
37- sgrep: Greps on all local source files.
38- godir: Go to the directory containing a file.
39- allmod: List all modules.
40- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080041- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000042- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080043- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000044- installmod: Adb installs a module's built APK.
45- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000046- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070047
Roland Levillain39341922015-10-20 12:48:19 +010048Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070049- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080050- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070051
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070052Look at the source to view more functions. The complete list is:
53EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070054 local T=$(gettop)
55 local A=""
56 local i
Jacky Cao89483b82015-05-15 22:12:53 +080057 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 -070058 A="$A $i"
59 done
60 echo $A
61}
62
Ying Wang08800fd2016-03-03 20:57:21 -080063# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000064function build_build_var_cache()
65{
Christopher Ferris55257d22017-03-23 11:08:58 -070066 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080067 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080068 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' ' '`)
69 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 -080070 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080071 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080072 --vars="${cached_vars[*]}" \
73 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070074 --var-prefix=var_cache_ \
75 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080076 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000077 if [ $ret -ne 0 ]
78 then
Ying Wang08800fd2016-03-03 20:57:21 -080079 unset build_dicts_script
80 return $ret
81 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070082 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080083 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080084 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080085 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000086 if [ $ret -ne 0 ]
87 then
Ying Wang08800fd2016-03-03 20:57:21 -080088 return $ret
89 fi
90 BUILD_VAR_CACHE_READY="true"
91}
92
Ying Wangf0cb3972016-03-04 13:56:23 -080093# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080094# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000095function destroy_build_var_cache()
96{
Ying Wang08800fd2016-03-03 20:57:21 -080097 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070098 local v
Ying Wang08800fd2016-03-03 20:57:21 -080099 for v in $cached_vars; do
100 unset var_cache_$v
101 done
102 unset cached_vars
103 for v in $cached_abs_vars; do
104 unset abs_var_cache_$v
105 done
106 unset cached_abs_vars
107}
108
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700109# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000110function get_abs_build_var()
111{
112 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
113 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800114 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200115 return
Ying Wang08800fd2016-03-03 20:57:21 -0800116 fi
117
Christopher Ferris55257d22017-03-23 11:08:58 -0700118 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700119 if [ ! "$T" ]; then
120 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
121 return
122 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700123 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700124}
125
126# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000127function get_build_var()
128{
129 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
130 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800131 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100132 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800133 fi
134
Christopher Ferris55257d22017-03-23 11:08:58 -0700135 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700136 if [ ! "$T" ]; then
137 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100138 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700139 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700140 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800141}
142
143# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000144function check_product()
145{
Christopher Ferris55257d22017-03-23 11:08:58 -0700146 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800147 if [ ! "$T" ]; then
148 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
149 return
150 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700151 TARGET_PRODUCT=$1 \
152 TARGET_BUILD_VARIANT= \
153 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700154 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800155 get_build_var TARGET_DEVICE > /dev/null
156 # hide successful answers, but allow the errors to show
157}
158
159VARIANT_CHOICES=(user userdebug eng)
160
161# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000162function check_variant()
163{
Christopher Ferris55257d22017-03-23 11:08:58 -0700164 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800165 for v in ${VARIANT_CHOICES[@]}
166 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000167 if [ "$v" = "$1" ]
168 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800169 return 0
170 fi
171 done
172 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700173}
174
Matt Alexanderd9c56562020-05-21 10:49:17 +0000175function setpaths()
176{
Christopher Ferris55257d22017-03-23 11:08:58 -0700177 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700178 if [ ! "$T" ]; then
179 echo "Couldn't locate the top of the tree. Try setting TOP."
180 return
181 fi
182
183 ##################################################################
184 # #
185 # Read me before you modify this code #
186 # #
187 # This function sets ANDROID_BUILD_PATHS to what it is adding #
188 # to PATH, and the next time it is run, it removes that from #
189 # PATH. This is required so lunch can be run more than once #
190 # and still have working paths. #
191 # #
192 ##################################################################
193
Raphael Mollc639c782011-06-20 17:25:01 -0700194 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
195 # due to "C:\Program Files" being in the path.
196
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700197 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000198 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700199 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
200 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000201 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700202 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800203 # strip leading ':', if any
204 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500205 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700206
207 # and in with the new
Ben Chengfba67bf2014-02-25 10:27:07 -0800208
Dan Willemsen838dcec2021-09-30 22:40:34 +0000209 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700210
211 # add kernel specific binaries
212 case $(uname -s) in
213 Linux)
214 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
215 ;;
216 *)
217 ;;
218 esac
219
Colin Crossc0b01042022-10-05 12:03:03 -0700220 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS)
Yi Kongdfd00b12019-05-21 16:00:04 -0700221 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
222
223 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
224 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
225 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200226
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800227 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
228 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
229
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200230 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
231 # to ensure that the corresponding 'emulator' binaries are used.
232 case $(uname -s) in
233 Darwin)
234 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
235 ;;
236 Linux)
237 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
238 ;;
239 *)
240 ANDROID_EMULATOR_PREBUILTS=
241 ;;
242 esac
243 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700244 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200245 export ANDROID_EMULATOR_PREBUILTS
246 fi
247
Jim Tangb3fda302018-12-22 10:24:55 +0800248 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
249 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700250 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
251 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
252 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800254
Martin Stjernholm6a012262021-11-24 14:56:15 +0000255 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700256 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800257
258 # out with the duplicate old
259 if [ -n $ANDROID_PYTHONPATH ]; then
260 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
261 fi
262 # and in with the new
263 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800264 if [ -n $VENDOR_PYTHONPATH ]; then
265 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
266 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800267 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800268
Colin Crosse97e6932017-06-30 16:01:45 -0700269 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
270 export JAVA_HOME=$ANDROID_JAVA_HOME
271 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
272 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
273 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500274
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800275 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700276 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
277 export OUT=$ANDROID_PRODUCT_OUT
278
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700279 unset ANDROID_HOST_OUT
280 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
281
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900282 unset ANDROID_SOONG_HOST_OUT
283 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
284
Simran Basidd050ed2017-02-13 13:46:48 -0800285 unset ANDROID_HOST_OUT_TESTCASES
286 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
287
288 unset ANDROID_TARGET_OUT_TESTCASES
289 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
290
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800291 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700292 # TODO: fix the path
293 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
294}
295
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000296function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000297{
Jingwen Chen17d56992021-06-10 08:17:54 +0000298 if which bazel &>/dev/null; then
299 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
300 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000301 fi
302
Jingwen Chen17d56992021-06-10 08:17:54 +0000303 local T="$(gettop)"
304 if [ ! "$T" ]; then
305 >&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."
306 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000307 fi
308
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000309 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000310}
311
Matt Alexanderd9c56562020-05-21 10:49:17 +0000312function printconfig()
313{
Christopher Ferris55257d22017-03-23 11:08:58 -0700314 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800315 if [ ! "$T" ]; then
316 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
317 return
318 fi
319 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700320}
321
Matt Alexanderd9c56562020-05-21 10:49:17 +0000322function set_stuff_for_environment()
323{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800324 setpaths
325 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700326
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800327 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328}
329
Matt Alexanderd9c56562020-05-21 10:49:17 +0000330function set_sequence_number()
331{
Colin Cross88737132017-03-21 17:41:03 -0700332 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333}
334
Makoto Onukida971062018-06-18 10:15:19 -0700335# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
336function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800337 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700338 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800339 *:"$cmd":*)
340 return 1
341 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700342 esac
343 return 0
344}
345
Matt Alexanderd9c56562020-05-21 10:49:17 +0000346function addcompletions()
347{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700348 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700349
Jim Tanga881a252018-06-19 16:34:41 +0800350 # Keep us from trying to run in something that's neither bash nor zsh.
351 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700352 return
353 fi
354
355 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800356 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700357 return
358 fi
359
Jim Tanga881a252018-06-19 16:34:41 +0800360 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000361 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800362 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800363 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400364 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800365 )
Makoto Onukida971062018-06-18 10:15:19 -0700366 # Completion can be disabled selectively to allow users to use non-standard completion.
367 # e.g.
368 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
369 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400370 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800371 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400372 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000373 if [ ! -f "$f" ]; then
374 echo "Warning: completion file $f not found"
375 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700376 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700377 fi
378 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700379
Matt Alexanderd9c56562020-05-21 10:49:17 +0000380 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700381 complete -C "bit --tab" bit
382 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000383 if [ -z "$ZSH_VERSION" ]; then
384 # Doesn't work in zsh.
385 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400386 # TODO(b/244559459): Support b autocompletion for zsh
387 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000388 fi
Jim Tanga881a252018-06-19 16:34:41 +0800389 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800390
Cole Faust24c36db2021-01-23 02:39:37 +0000391 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100392 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000393 complete -F _complete_android_module_names outmod
394 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100395 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700396}
397
Joe Onorato824608c2022-04-08 11:06:16 -0700398function multitree_lunch_help()
399{
400 echo "usage: lunch PRODUCT-VARIANT" 1>&2
401 echo " Set up android build environment based on a product short name and variant" 1>&2
402 echo 1>&2
403 echo "lunch COMBO_FILE VARIANT" 1>&2
404 echo " Set up android build environment based on a specific lunch combo file" 1>&2
405 echo " and variant." 1>&2
406 echo 1>&2
407 echo "lunch --print [CONFIG]" 1>&2
408 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
409 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
410 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
411 echo 1>&2
412 echo "lunch --list" 1>&2
413 echo " List all possible combo files available in the current tree" 1>&2
414 echo 1>&2
415 echo "lunch --help" 1>&2
416 echo "lunch -h" 1>&2
417 echo " Prints this message." 1>&2
418}
419
420function multitree_lunch()
421{
422 local code
423 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000424 # Lunch must be run in the topdir, but this way we get a clear error
425 # message, instead of FileNotFound.
426 local T=$(multitree_gettop)
427 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +0000428 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
LaMont Jonesc39e5022022-06-23 19:09:06 +0000429 else
430 _multitree_lunch_error
431 return 1
432 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700433 if $(echo "$1" | grep -q '^-') ; then
434 # Calls starting with a -- argument are passed directly and the function
435 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000436 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700437 code=$?
438 if [[ $code -eq 2 ]] ; then
439 echo 1>&2
440 multitree_lunch_help
441 return $code
442 elif [[ $code -ne 0 ]] ; then
443 return $code
444 fi
445 else
446 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000447 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700448 code=$?
449 if [[ $code -eq 2 ]] ; then
450 echo 1>&2
451 multitree_lunch_help
452 return $code
453 elif [[ $code -ne 0 ]] ; then
454 return $code
455 fi
456
457 export TARGET_BUILD_COMBO=${results[0]}
458 export TARGET_BUILD_VARIANT=${results[1]}
459 fi
460}
461
Matt Alexanderd9c56562020-05-21 10:49:17 +0000462function choosetype()
463{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700464 echo "Build type choices are:"
465 echo " 1. release"
466 echo " 2. debug"
467 echo
468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800469 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700470 DEFAULT_NUM=1
471 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800473 export TARGET_BUILD_TYPE=
474 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 while [ -z $TARGET_BUILD_TYPE ]
476 do
477 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000478 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800479 read ANSWER
480 else
481 echo $1
482 ANSWER=$1
483 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 case $ANSWER in
485 "")
486 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
487 ;;
488 1)
489 export TARGET_BUILD_TYPE=release
490 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491 release)
492 export TARGET_BUILD_TYPE=release
493 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494 2)
495 export TARGET_BUILD_TYPE=debug
496 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800497 debug)
498 export TARGET_BUILD_TYPE=debug
499 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700500 *)
501 echo
502 echo "I didn't understand your response. Please try again."
503 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700504 ;;
505 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000506 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800507 break
508 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700509 done
510
Ying Wang08800fd2016-03-03 20:57:21 -0800511 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700512 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800513 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700514}
515
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800516#
517# This function isn't really right: It chooses a TARGET_PRODUCT
518# based on the list of boards. Usually, that gets you something
519# that kinda works with a generic product, but really, you should
520# pick a product by name.
521#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000522function chooseproduct()
523{
Christopher Ferris55257d22017-03-23 11:08:58 -0700524 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000525 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700526 default_value=$TARGET_PRODUCT
527 else
Ying Wang0a76df52015-06-08 11:57:26 -0700528 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700529 fi
530
Ying Wang08800fd2016-03-03 20:57:21 -0800531 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800532 export TARGET_PRODUCT=
533 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534 while [ -z "$TARGET_PRODUCT" ]
535 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700536 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000537 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800538 read ANSWER
539 else
540 echo $1
541 ANSWER=$1
542 fi
543
Matt Alexanderd9c56562020-05-21 10:49:17 +0000544 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700545 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800546 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000547 if check_product $ANSWER
548 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800549 export TARGET_PRODUCT=$ANSWER
550 else
551 echo "** Not a valid product: $ANSWER"
552 fi
553 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000554 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800555 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556 fi
557 done
558
Ying Wang08800fd2016-03-03 20:57:21 -0800559 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700560 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800561 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700562}
563
Matt Alexanderd9c56562020-05-21 10:49:17 +0000564function choosevariant()
565{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800566 echo "Variant choices are:"
567 local index=1
568 local v
569 for v in ${VARIANT_CHOICES[@]}
570 do
571 # The product name is the name of the directory containing
572 # the makefile we found, above.
573 echo " $index. $v"
574 index=$(($index+1))
575 done
576
577 local default_value=eng
578 local ANSWER
579
580 export TARGET_BUILD_VARIANT=
581 while [ -z "$TARGET_BUILD_VARIANT" ]
582 do
583 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000584 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 read ANSWER
586 else
587 echo $1
588 ANSWER=$1
589 fi
590
Matt Alexanderd9c56562020-05-21 10:49:17 +0000591 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000593 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
594 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200595 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 fi
597 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000598 if check_variant $ANSWER
599 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800600 export TARGET_BUILD_VARIANT=$ANSWER
601 else
602 echo "** Not a valid variant: $ANSWER"
603 fi
604 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000605 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800606 break
607 fi
608 done
609}
610
Matt Alexanderd9c56562020-05-21 10:49:17 +0000611function choosecombo()
612{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700613 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700614
615 echo
616 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700617 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700618
619 echo
620 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700621 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800622
623 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800624 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700625 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800627 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700628}
629
Matt Alexanderd9c56562020-05-21 10:49:17 +0000630function add_lunch_combo()
631{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800632 if [ -n "$ZSH_VERSION" ]; then
633 echo -n "${funcfiletrace[1]}: "
634 else
635 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
636 fi
637 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 -0800638}
639
Matt Alexanderd9c56562020-05-21 10:49:17 +0000640function print_lunch_menu()
641{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700642 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100643 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700644 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 +0100645 local ret=$?
646
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700647 echo
648 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700649 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100650
Matt Alexanderd9c56562020-05-21 10:49:17 +0000651 if [ $ret -ne 0 ]
652 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100653 echo "Warning: Cannot display lunch menu."
654 echo
655 echo "Note: You can invoke lunch with an explicit target:"
656 echo
657 echo " usage: lunch [target]" >&2
658 echo
659 return
660 fi
661
Will Burr40401202022-02-07 12:12:01 +0000662 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800663
664 local i=1
665 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700666 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800667 do
668 echo " $i. $choice"
669 i=$(($i+1))
670 done
671
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700672 echo
673}
674
Matt Alexanderd9c56562020-05-21 10:49:17 +0000675function lunch()
676{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800677 local answer
678
Steven Moreland92793dc2020-02-25 18:30:18 -0800679 if [[ $# -gt 1 ]]; then
680 echo "usage: lunch [target]" >&2
681 return 1
682 fi
683
Will Burr40401202022-02-07 12:12:01 +0000684 local used_lunch_menu=0
685
Steven Moreland92793dc2020-02-25 18:30:18 -0800686 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800687 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700688 else
689 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000690 echo "Which would you like? [aosp_arm-eng]"
691 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 -0800692 read answer
Will Burr40401202022-02-07 12:12:01 +0000693 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700694 fi
695
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800696 local selection=
697
Matt Alexanderd9c56562020-05-21 10:49:17 +0000698 if [ -z "$answer" ]
699 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700700 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000701 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
702 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800703 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000704 if [ $answer -le ${#choices[@]} ]
705 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800706 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000707 if [ -n "$ZSH_VERSION" ]
708 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800709 selection=${choices[$(($answer))]}
710 else
711 selection=${choices[$(($answer-1))]}
712 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800713 fi
Colin Cross88737132017-03-21 17:41:03 -0700714 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800715 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700716 fi
717
Joe Onoratoda12daf2010-06-09 18:18:31 -0700718 export TARGET_BUILD_APPS=
719
Colin Cross88737132017-03-21 17:41:03 -0700720 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700721 product=${selection%%-*} # Trim everything after first dash
722 variant_and_version=${selection#*-} # Trim everything up to first dash
723 if [ "$variant_and_version" != "$selection" ]; then
724 variant=${variant_and_version%%-*}
725 if [ "$variant" != "$variant_and_version" ]; then
726 version=${variant_and_version#*-}
727 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700728 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800729
Matt Alexanderd9c56562020-05-21 10:49:17 +0000730 if [ -z "$product" ]
731 then
Ying Wang08800fd2016-03-03 20:57:21 -0800732 echo
Colin Cross88737132017-03-21 17:41:03 -0700733 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700734 return 1
735 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800736
Colin Cross88737132017-03-21 17:41:03 -0700737 TARGET_PRODUCT=$product \
738 TARGET_BUILD_VARIANT=$variant \
739 TARGET_PLATFORM_VERSION=$version \
740 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000741 if [ $? -ne 0 ]
742 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100743 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
744 then
745 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
746 fi
Colin Cross88737132017-03-21 17:41:03 -0700747 return 1
748 fi
Colin Cross88737132017-03-21 17:41:03 -0700749 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
750 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700751 if [ -n "$version" ]; then
752 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
753 else
754 unset TARGET_PLATFORM_VERSION
755 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700756 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700757
Will Burr40401202022-02-07 12:12:01 +0000758 if [ $used_lunch_menu -eq 1 ]; then
759 echo
760 echo "Hint: next time you can simply run 'lunch $selection'"
761 fi
762
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700763 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800764
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700765 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700766 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800767 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400768
769 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
770 check_mu_config
771 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700772}
773
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700774unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700775# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000776function _lunch()
777{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700778 local cur prev opts
779 COMPREPLY=()
780 cur="${COMP_WORDS[COMP_CWORD]}"
781 prev="${COMP_WORDS[COMP_CWORD-1]}"
782
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700783 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800784 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700785 fi
786
787 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700788 return 0
789}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700790
Joe Onoratoda12daf2010-06-09 18:18:31 -0700791# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700792# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000793function tapas()
794{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700795 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800796 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700797 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700798 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Colin Cross7f49a672022-01-27 18:15:53 -0800799 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
800 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)"
801
Joe Onoratoda12daf2010-06-09 18:18:31 -0700802
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700803 if [ "$showHelp" != "" ]; then
804 $(gettop)/build/make/tapasHelp.sh
805 return
806 fi
807
Ying Wang67f02922012-08-22 10:25:20 -0700808 if [ $(echo $arch | wc -w) -gt 1 ]; then
809 echo "tapas: Error: Multiple build archs supplied: $arch"
810 return
811 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700812 if [ $(echo $variant | wc -w) -gt 1 ]; then
813 echo "tapas: Error: Multiple build variants supplied: $variant"
814 return
815 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700816 if [ $(echo $density | wc -w) -gt 1 ]; then
817 echo "tapas: Error: Multiple densities supplied: $density"
818 return
819 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800820 if [ $(echo $keys | wc -w) -gt 1 ]; then
821 echo "tapas: Error: Multiple keys supplied: $keys"
822 return
823 fi
Ying Wang67f02922012-08-22 10:25:20 -0700824
Ying Wang0a76df52015-06-08 11:57:26 -0700825 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700826 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000827 x86) product=aosp_x86;;
828 arm64) product=aosp_arm64;;
829 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700830 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800831 if [ -n "$keys" ]; then
832 product=${product/aosp_/aosp_${keys}_}
833 fi;
834
Joe Onoratoda12daf2010-06-09 18:18:31 -0700835 if [ -z "$variant" ]; then
836 variant=eng
837 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700838 if [ -z "$apps" ]; then
839 apps=all
840 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600841 if [ -z "$density" ]; then
842 density=alldpi
843 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700844
Ying Wang67f02922012-08-22 10:25:20 -0700845 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700846 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700847 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700848 export TARGET_BUILD_TYPE=release
849 export TARGET_BUILD_APPS=$apps
850
Ying Wang08800fd2016-03-03 20:57:21 -0800851 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700852 set_stuff_for_environment
853 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800854 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700855}
856
Martin Stjernholmf692c752021-04-12 00:01:10 +0100857# Configures the build to build unbundled Android modules (APEXes).
858# Run banchan with one or more module names (from apex{} modules).
859function banchan()
860{
861 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000862 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|x86_64|arm64only|x86_64only)$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100863 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100864 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 +0100865
866 if [ "$showHelp" != "" ]; then
867 $(gettop)/build/make/banchanHelp.sh
868 return
869 fi
870
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100871 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000872 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100873 elif [ $(echo $product | wc -w) -gt 1 ]; then
874 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100875 return
876 fi
877 if [ $(echo $variant | wc -w) -gt 1 ]; then
878 echo "banchan: Error: Multiple build variants supplied: $variant"
879 return
880 fi
881 if [ -z "$apps" ]; then
882 echo "banchan: Error: No modules supplied"
883 return
884 fi
885
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100886 case $product in
887 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100888 x86) product=module_x86;;
889 arm64) product=module_arm64;;
890 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000891 arm64only) product=module_arm64only;;
892 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100893 esac
894 if [ -z "$variant" ]; then
895 variant=eng
896 fi
897
898 export TARGET_PRODUCT=$product
899 export TARGET_BUILD_VARIANT=$variant
900 export TARGET_BUILD_DENSITY=alldpi
901 export TARGET_BUILD_TYPE=release
902
903 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
904 # case is different and it may diverge in the future.
905 export TARGET_BUILD_APPS=$apps
906
907 build_build_var_cache
908 set_stuff_for_environment
909 printconfig
910 destroy_build_var_cache
911}
912
Matt Alexanderd9c56562020-05-21 10:49:17 +0000913function gettop
914{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700915 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000916 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700917 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000918 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700919 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800921 # The following circumlocution (repeated below as well) ensures
922 # that we record the true directory name and not one that is
923 # faked up with symlink names.
924 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700925 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800926 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700927 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000928 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800929 \cd ..
synergyb112a402013-07-05 19:47:47 -0700930 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700931 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000932 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700933 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000934 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700935 fi
936 fi
937 fi
938}
939
Joe Onorato7cf6f972022-05-11 21:39:57 -0700940# TODO: Merge into gettop as part of launching multitree
941function multitree_gettop
942{
Spandan Dasca762052022-09-21 00:08:34 +0000943 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700944 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
945 # The following circumlocution ensures we remove symlinks from TOP.
946 (cd "$TOP"; PWD= /bin/pwd)
947 else
948 if [ -f $TOPFILE ] ; then
949 # 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
953 else
954 local HERE=$PWD
955 local T=
956 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
957 \cd ..
958 T=`PWD= /bin/pwd -P`
959 done
960 \cd "$HERE"
961 if [ -f "$T/$TOPFILE" ]; then
962 echo "$T"
963 fi
964 fi
965 fi
966}
967
Matt Alexanderd9c56562020-05-21 10:49:17 +0000968function croot()
969{
Christopher Ferris55257d22017-03-23 11:08:58 -0700970 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700971 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700972 if [ "$1" ]; then
973 \cd $(gettop)/$1
974 else
975 \cd $(gettop)
976 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700977 else
978 echo "Couldn't locate the top of the tree. Try setting TOP."
979 fi
980}
981
Matt Alexanderd9c56562020-05-21 10:49:17 +0000982function _croot()
983{
Anton Hanssonece9c482019-02-04 18:15:39 +0000984 local T=$(gettop)
985 if [ "$T" ]; then
986 local cur="${COMP_WORDS[COMP_CWORD]}"
987 k=0
988 for c in $(compgen -d ${T}/${cur}); do
989 COMPREPLY[k++]=${c#${T}/}/
990 done
991 fi
992}
993
Matt Alexanderd9c56562020-05-21 10:49:17 +0000994function cproj()
995{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700996 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700997 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700998 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700999 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1000 T=$PWD
1001 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001002 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001003 return
1004 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001005 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001006 done
Ying Wang9cd17642012-12-13 10:52:07 -08001007 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001008 echo "can't find Android.mk"
1009}
1010
Daniel Sandler47e0a882013-07-30 13:23:52 -04001011# simplified version of ps; output in the form
1012# <pid> <procname>
1013function qpid() {
1014 local prepend=''
1015 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001016 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001017 prepend=' '
1018 append='$'
1019 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001020 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001021 echo "usage: qpid [[--exact] <process name|pid>"
1022 return 255
1023 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001024
1025 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001026 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001027 qpid | \grep "$prepend$EXE$append"
1028 else
1029 adb shell ps \
1030 | tr -d '\r' \
1031 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1032 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001033}
1034
Steven Moreland74114f12020-09-10 01:23:32 +00001035# syswrite - disable verity, reboot if needed, and remount image
1036#
1037# Easy way to make system.img/etc writable
1038function syswrite() {
1039 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001040 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001041 echo "rebooting"
1042 adb reboot && adb wait-for-device && adb root || return 1
1043 fi
1044 adb wait-for-device && adb remount || return 1
1045}
1046
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001047# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001048# that has the core-file-size limit set correctly
1049#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001050# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001051# if its core-file-size limit is not set already.
1052# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1053
Matt Alexanderd9c56562020-05-21 10:49:17 +00001054function coredump_setup()
1055{
1056 echo "Getting root...";
1057 adb root;
1058 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001059
Matt Alexanderd9c56562020-05-21 10:49:17 +00001060 echo "Remounting root partition read-write...";
1061 adb shell mount -w -o remount -t rootfs rootfs;
1062 sleep 1;
1063 adb wait-for-device;
1064 adb shell mkdir -p /cores;
1065 adb shell mount -t tmpfs tmpfs /cores;
1066 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001067
Matt Alexanderd9c56562020-05-21 10:49:17 +00001068 echo "Granting SELinux permission to dump in /cores...";
1069 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001070
Matt Alexanderd9c56562020-05-21 10:49:17 +00001071 echo "Set core pattern.";
1072 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001073
Ying Wang08800fd2016-03-03 20:57:21 -08001074 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001075}
1076
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001077# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001078# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001079#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001080# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001081# dump to actually be generated.
1082
Matt Alexanderd9c56562020-05-21 10:49:17 +00001083function coredump_enable()
1084{
1085 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001086 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001087 printf "Expecting a PID!\n";
1088 return;
1089 fi;
1090 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001091 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001092}
1093
1094# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001095# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001096#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001097# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001098# enabled globally.
1099
Matt Alexanderd9c56562020-05-21 10:49:17 +00001100function core()
1101{
1102 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001103
Ying Wang08800fd2016-03-03 20:57:21 -08001104 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001105 printf "Expecting a PID!\n";
1106 return;
1107 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001108
Matt Alexanderd9c56562020-05-21 10:49:17 +00001109 local CORENAME=core.$PID;
1110 local COREPATH=/cores/$CORENAME;
1111 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001112
Matt Alexanderd9c56562020-05-21 10:49:17 +00001113 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001116 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001117 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1118 adb shell kill -$SIG $PID;
1119 sleep 1;
1120 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001121
Matt Alexanderd9c56562020-05-21 10:49:17 +00001122 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1123 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124}
1125
Christopher Tate744ee802009-11-12 15:33:08 -08001126# systemstack - dump the current stack trace of all threads in the system process
1127# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001128function systemstack()
1129{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001130 stacks system_server
1131}
1132
Michael Wrightaeed7212014-06-19 19:58:12 -07001133# Read the ELF header from /proc/$PID/exe to determine if the process is
1134# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135function is64bit()
1136{
Ben Chengfba67bf2014-02-25 10:27:07 -08001137 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001138 if [ "$PID" ] ; then
1139 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001140 echo "64"
1141 else
1142 echo ""
1143 fi
1144 else
1145 echo ""
1146 fi
1147}
1148
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001149case `uname -s` in
1150 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001151 function sgrep()
1152 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001153 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 -04001154 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001155 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001156
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001157 ;;
1158 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001159 function sgrep()
1160 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001161 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 -04001162 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001163 }
1164 ;;
1165esac
1166
Matt Alexanderd9c56562020-05-21 10:49:17 +00001167function gettargetarch
1168{
Raghu Gandham8da43102012-07-25 19:57:22 -07001169 get_build_var TARGET_ARCH
1170}
1171
Matt Alexanderd9c56562020-05-21 10:49:17 +00001172function ggrep()
1173{
Mike Frysinger5e479732015-09-22 18:13:48 -04001174 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1175 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001176}
1177
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178function gogrep()
1179{
Orion Hodson831472d2019-10-25 11:35:15 +01001180 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1181 -exec grep --color -n "$@" {} +
1182}
1183
Matt Alexanderd9c56562020-05-21 10:49:17 +00001184function jgrep()
1185{
Mike Frysinger5e479732015-09-22 18:13:48 -04001186 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1187 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001188}
1189
Alistair Delva176e5342021-02-22 13:31:26 -08001190function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001191{
1192 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1193 -exec grep --color -n "$@" {} +
1194}
1195
Taesu Leeea0cecd2020-10-28 11:05:18 +09001196function ktgrep()
1197{
1198 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1199 -exec grep --color -n "$@" {} +
1200}
1201
Matt Alexanderd9c56562020-05-21 10:49:17 +00001202function cgrep()
1203{
Mike Frysinger5e479732015-09-22 18:13:48 -04001204 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' \) \
1205 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001206}
1207
Matt Alexanderd9c56562020-05-21 10:49:17 +00001208function resgrep()
1209{
Christopher Ferris55257d22017-03-23 11:08:58 -07001210 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001211 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1212 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1213 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001214}
1215
Matt Alexanderd9c56562020-05-21 10:49:17 +00001216function mangrep()
1217{
Mike Frysinger5e479732015-09-22 18:13:48 -04001218 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1219 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001220}
1221
Matt Alexanderd9c56562020-05-21 10:49:17 +00001222function owngrep()
1223{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001224 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1225 -exec grep --color -n "$@" {} +
1226}
1227
Matt Alexanderd9c56562020-05-21 10:49:17 +00001228function sepgrep()
1229{
Mike Frysinger5e479732015-09-22 18:13:48 -04001230 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1231 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001232}
1233
Matt Alexanderd9c56562020-05-21 10:49:17 +00001234function rcgrep()
1235{
Mike Frysinger5e479732015-09-22 18:13:48 -04001236 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1237 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001238}
1239
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001240case `uname -s` in
1241 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242 function mgrep()
1243 {
Orion Hodson831472d2019-10-25 11:35:15 +01001244 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 -04001245 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001246 }
1247
Matt Alexanderd9c56562020-05-21 10:49:17 +00001248 function treegrep()
1249 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001250 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 -04001251 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001252 }
1253
1254 ;;
1255 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001256 function mgrep()
1257 {
Orion Hodson831472d2019-10-25 11:35:15 +01001258 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 -04001259 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001260 }
1261
Matt Alexanderd9c56562020-05-21 10:49:17 +00001262 function treegrep()
1263 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001264 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 -04001265 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001266 }
1267
1268 ;;
1269esac
1270
Matt Alexanderd9c56562020-05-21 10:49:17 +00001271function getprebuilt
1272{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001273 get_abs_build_var ANDROID_PREBUILTS
1274}
1275
Matt Alexanderd9c56562020-05-21 10:49:17 +00001276function tracedmdump()
1277{
Christopher Ferris55257d22017-03-23 11:08:58 -07001278 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001279 if [ ! "$T" ]; then
1280 echo "Couldn't locate the top of the tree. Try setting TOP."
1281 return
1282 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001283 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001284 local arch=$(gettargetarch)
1285 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001286
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001287 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001288 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001289 echo "usage: tracedmdump tracename"
1290 return
1291 fi
1292
Matt Alexanderd9c56562020-05-21 10:49:17 +00001293 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001294 echo "Error: cannot find kernel: '$KERNEL'"
1295 return
1296 fi
1297
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001298 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001299 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001300 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1301 fi
1302
1303 echo "post-processing traces..."
1304 rm -f $TRACE/qtrace.dexlist
1305 post_trace $TRACE
1306 if [ $? -ne 0 ]; then
1307 echo "***"
1308 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1309 echo "***"
1310 return
1311 fi
1312 echo "generating dexlist output..."
1313 /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
1314 echo "generating dmtrace data..."
1315 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1316 echo "generating html file..."
1317 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1318 echo "done, see $TRACE/dmtrace.html for details"
1319 echo "or run:"
1320 echo " traceview $TRACE/dmtrace"
1321}
1322
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001323# communicate with a running device or emulator, set up necessary state,
1324# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001325function runhat()
1326{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001327 # process standard adb options
1328 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001329 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001330 adbTarget=$1
1331 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001332 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001333 adbTarget="$1 $2"
1334 shift 2
1335 fi
1336 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001337 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001338
1339 # runhat options
1340 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001341
Matt Alexanderd9c56562020-05-21 10:49:17 +00001342 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001343 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001344 return
1345 fi
1346
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001347 # confirm hat is available
1348 if [ -z $(which hat) ]; then
1349 echo "hat is not available in this configuration."
1350 return
1351 fi
1352
Andy McFaddenb6289852010-07-12 08:00:19 -07001353 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001354 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001355 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001356 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001357 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001358 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001359 echo -n "> "
1360 read
1361
The Android Open Source Project88b60792009-03-03 19:28:42 -08001362 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001363
The Android Open Source Project88b60792009-03-03 19:28:42 -08001364 echo "Retrieving file $devFile..."
1365 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001366
The Android Open Source Project88b60792009-03-03 19:28:42 -08001367 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001368
The Android Open Source Project88b60792009-03-03 19:28:42 -08001369 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001370 echo "View the output by pointing your browser at http://localhost:7000/"
1371 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001372 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001373}
1374
Matt Alexanderd9c56562020-05-21 10:49:17 +00001375function getbugreports()
1376{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001377 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001378
1379 if [ ! "$reports" ]; then
1380 echo "Could not locate any bugreports."
1381 return
1382 fi
1383
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001384 local report
1385 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001386 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001387 echo "/sdcard/bugreports/${report}"
1388 adb pull /sdcard/bugreports/${report} ${report}
1389 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001390 done
1391}
1392
Matt Alexanderd9c56562020-05-21 10:49:17 +00001393function getsdcardpath()
1394{
Victoria Lease1b296b42012-08-21 15:44:06 -07001395 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1396}
1397
Matt Alexanderd9c56562020-05-21 10:49:17 +00001398function getscreenshotpath()
1399{
Victoria Lease1b296b42012-08-21 15:44:06 -07001400 echo "$(getsdcardpath)/Pictures/Screenshots"
1401}
1402
Matt Alexanderd9c56562020-05-21 10:49:17 +00001403function getlastscreenshot()
1404{
Victoria Lease1b296b42012-08-21 15:44:06 -07001405 local screenshot_path=$(getscreenshotpath)
1406 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 +00001407 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001408 echo "No screenshots found."
1409 return
1410 fi
1411 echo "${screenshot}"
1412 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1413}
1414
Matt Alexanderd9c56562020-05-21 10:49:17 +00001415function startviewserver()
1416{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001417 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001418 if [ $# -gt 0 ]; then
1419 port=$1
1420 fi
1421 adb shell service call window 1 i32 $port
1422}
1423
Matt Alexanderd9c56562020-05-21 10:49:17 +00001424function stopviewserver()
1425{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001426 adb shell service call window 2
1427}
1428
Matt Alexanderd9c56562020-05-21 10:49:17 +00001429function isviewserverstarted()
1430{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001431 adb shell service call window 3
1432}
1433
Matt Alexanderd9c56562020-05-21 10:49:17 +00001434function key_home()
1435{
Romain Guyb84049a2010-10-04 16:56:11 -07001436 adb shell input keyevent 3
1437}
1438
Matt Alexanderd9c56562020-05-21 10:49:17 +00001439function key_back()
1440{
Romain Guyb84049a2010-10-04 16:56:11 -07001441 adb shell input keyevent 4
1442}
1443
Matt Alexanderd9c56562020-05-21 10:49:17 +00001444function key_menu()
1445{
Romain Guyb84049a2010-10-04 16:56:11 -07001446 adb shell input keyevent 82
1447}
1448
Matt Alexanderd9c56562020-05-21 10:49:17 +00001449function smoketest()
1450{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001451 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1452 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1453 return
1454 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001455 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001456 if [ ! "$T" ]; then
1457 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1458 return
1459 fi
1460
Ying Wang9cd17642012-12-13 10:52:07 -08001461 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001462 adb uninstall com.android.smoketest > /dev/null &&
1463 adb uninstall com.android.smoketest.tests > /dev/null &&
1464 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1465 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1466 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1467}
1468
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001469# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001470function runtest()
1471{
Christopher Ferris55257d22017-03-23 11:08:58 -07001472 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001473 if [ ! "$T" ]; then
1474 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1475 return
1476 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001477 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001478}
1479
The Android Open Source Project88b60792009-03-03 19:28:42 -08001480function godir () {
1481 if [[ -z "$1" ]]; then
1482 echo "Usage: godir <regex>"
1483 return
1484 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001485 local T=$(gettop)
1486 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001487 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001488 mkdir -p $OUT_DIR
1489 FILELIST=$OUT_DIR/filelist
1490 else
1491 FILELIST=$T/filelist
1492 fi
1493 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001494 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001495 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001496 echo " Done"
1497 echo ""
1498 fi
1499 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001500 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001501 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001502 echo "Not found"
1503 return
1504 fi
1505 local pathname
1506 local choice
1507 if [[ ${#lines[@]} > 1 ]]; then
1508 while [[ -z "$pathname" ]]; do
1509 local index=1
1510 local line
1511 for line in ${lines[@]}; do
1512 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001513 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001514 done
1515 echo
1516 echo -n "Select one: "
1517 unset choice
1518 read choice
1519 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1520 echo "Invalid choice"
1521 continue
1522 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001523 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001524 done
1525 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001526 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001527 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001528 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001529}
1530
Steven Moreland62054a42018-12-06 10:11:40 -08001531# Update module-info.json in out.
1532function refreshmod() {
1533 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1534 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1535 return 1
1536 fi
1537
1538 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1539
1540 # for the output of the next command
1541 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1542
1543 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001544 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001545 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1546}
1547
Cole Faust3e192382021-10-25 13:29:15 -07001548# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001549function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001550 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001551 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1552 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1553 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001554 return 1
1555 fi
1556
1557 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001558 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001559 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001560 fi
1561 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001562 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001563}
1564
1565# List all modules for the current device, as cached in module-info.json. If any build change is
1566# made and it should be reflected in the output, you should run 'refreshmod' first.
1567function allmod() {
1568 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001569
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001570 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 -08001571}
1572
Joe Onorato2c1aa472021-02-25 16:42:39 -08001573# Get the path of a specific module in the android tree, as cached in module-info.json.
1574# If any build change is made, and it should be reflected in the output, you should run
1575# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001576function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001577 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001578 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001579 return 1
1580 fi
1581
Cole Faust24c36db2021-01-23 02:39:37 +00001582 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001583
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001584 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001585module = '$1'
1586module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1587if module not in module_info:
1588 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001589print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001590
1591 if [ -z "$relpath" ]; then
1592 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1593 return 1
1594 else
Rett Berg78d1c932019-01-24 14:34:23 -08001595 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001596 fi
1597}
1598
Joe Onorato2c1aa472021-02-25 16:42:39 -08001599# Get the path of a specific module in the android tree, as cached in module-info.json.
1600# If any build change is made, and it should be reflected in the output, you should run
1601# 'refreshmod' first. Note: This is the inverse of pathmod.
1602function dirmods() {
1603 if [[ $# -ne 1 ]]; then
1604 echo "usage: dirmods <path>" >&2
1605 return 1
1606 fi
1607
1608 verifymodinfo || return 1
1609
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001610 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001611dir = '$1'
1612while dir.endswith('/'):
1613 dir = dir[:-1]
1614prefix = dir + '/'
1615module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1616results = set()
1617for m in module_info.values():
1618 for path in m.get(u'path', []):
1619 if path == dir or path.startswith(prefix):
1620 name = m.get(u'module_name')
1621 if name:
1622 results.add(name)
1623for name in sorted(results):
1624 print(name)
1625"
1626}
1627
1628
Rett Berg78d1c932019-01-24 14:34:23 -08001629# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1630# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1631function gomod() {
1632 if [[ $# -ne 1 ]]; then
1633 echo "usage: gomod <module>" >&2
1634 return 1
1635 fi
1636
1637 local path="$(pathmod $@)"
1638 if [ -z "$path" ]; then
1639 return 1
1640 fi
1641 cd $path
1642}
1643
Cole Faust24c36db2021-01-23 02:39:37 +00001644# Gets the list of a module's installed outputs, as cached in module-info.json.
1645# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1646function outmod() {
1647 if [[ $# -ne 1 ]]; then
1648 echo "usage: outmod <module>" >&2
1649 return 1
1650 fi
1651
1652 verifymodinfo || return 1
1653
1654 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001655 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001656module = '$1'
1657module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1658if module not in module_info:
1659 exit(1)
1660for output in module_info[module]['installed']:
1661 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1662
1663 if [ $? -ne 0 ]; then
1664 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1665 return 1
1666 elif [ ! -z "$relpath" ]; then
1667 echo "$relpath"
1668 fi
1669}
1670
1671# adb install a module's apk, as cached in module-info.json. If any build change
1672# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1673# Usage: installmod [adb install arguments] <module>
1674# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1675function installmod() {
1676 if [[ $# -eq 0 ]]; then
1677 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001678 echo "" >&2
1679 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1680 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1681 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001682 return 1
1683 fi
1684
1685 local _path
1686 _path=$(outmod ${@:$#:1})
1687 if [ $? -ne 0 ]; then
1688 return 1
1689 fi
1690
1691 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1692 if [ -z "$_path" ]; then
1693 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1694 return 1
1695 fi
Cole Faust3e192382021-10-25 13:29:15 -07001696 local serial_device=""
1697 if [[ "$1" == "-s" ]]; then
1698 if [[ $# -le 2 ]]; then
1699 echo "-s requires an argument" >&2
1700 return 1
1701 fi
1702 serial_device="-s $2"
1703 shift 2
1704 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001705 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001706 echo adb $serial_device install ${@:1:$length} $_path
1707 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001708}
1709
dimitry73b84812018-12-11 18:06:00 +01001710function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001711 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001712 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001713}
1714
Alex Rayf0d08eb2013-03-08 15:15:06 -08001715# Print colored exit condition
1716function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001717 "$@"
1718 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001719 if [ $retval -ne 0 ]
1720 then
Jacky Cao89483b82015-05-15 22:12:53 +08001721 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001722 else
Jacky Cao89483b82015-05-15 22:12:53 +08001723 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001724 fi
1725 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001726}
1727
Matt Alexanderd9c56562020-05-21 10:49:17 +00001728function get_make_command()
1729{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001730 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1731 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001732 # Always use the real make if -C is passed in
1733 for arg in "$@"; do
1734 if [[ $arg == -C* ]]; then
1735 echo command make
1736 return
1737 fi
1738 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001739 echo build/soong/soong_ui.bash --make-mode
1740 else
1741 echo command make
1742 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001743}
1744
Matt Alexanderd9c56562020-05-21 10:49:17 +00001745function _wrap_build()
1746{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001747 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1748 "$@"
1749 return $?
1750 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001751 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001752 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001753 local ret=$?
1754 local end_time=$(date +"%s")
1755 local tdiff=$(($end_time-$start_time))
1756 local hours=$(($tdiff / 3600 ))
1757 local mins=$((($tdiff % 3600) / 60))
1758 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001759 local ncolors=$(tput colors 2>/dev/null)
1760 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001761 color_failed=$'\E'"[0;31m"
1762 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001763 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001764 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001765 else
1766 color_failed=""
1767 color_success=""
1768 color_reset=""
1769 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001770
1771 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1772 echo
1773 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1774 fi
1775
Ed Heylcc6be0a2014-06-18 14:55:58 -07001776 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001777 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001778 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001779 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001780 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001781 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001782 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001783 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001784 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001785 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001786 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001787 printf "(%s seconds)" $secs
1788 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001789 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001790 echo
1791 return $ret
1792}
1793
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001794function _trigger_build()
1795(
1796 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001797 local T=$(gettop)
1798 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001799 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1800 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001801 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1802 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001803 fi
1804)
1805
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001806# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001807function b()
1808(
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001809 # zsh breaks posix by not doing string-splitting on unquoted args by default.
1810 # See https://zsh.sourceforge.io/Guide/zshguide05.html section 5.4.4.
1811 # Tell it to emulate Bourne shell for this function.
1812 if [ -n "$ZSH_VERSION" ]; then emulate -L sh; fi
1813
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001814 # Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
1815 local bazel_args=""
1816 local skip_tests="--skip-soong-tests"
1817 for i in $@; do
1818 if [[ $i != "--run-soong-tests" ]]; then
1819 bazel_args+="$i "
1820 else
1821 skip_tests=""
1822 fi
1823 done
Jingwen Chend728ee12021-05-18 06:02:53 +00001824 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Jingwen Chen28a22982022-08-31 09:47:33 +00001825 _trigger_build "all-modules" bp2build $skip_tests USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001826 # Then, run Bazel using the synthetic workspace as the --package_path.
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001827 if [[ -z "$bazel_args" ]]; then
Jingwen Chen6f4963b2022-09-09 07:08:25 +00001828 # If there are no args, show help and exit.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001829 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001830 else
1831 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Cole Fausta68f5162022-08-18 17:33:51 -07001832 # Add the --config=bp2build after the first argument that doesn't start with a dash. That should be the bazel
1833 # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work with commands like:
1834 # b run //foo -- --args-for-foo
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001835 local config_set=0
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001836
1837 # Represent the args as an array, not a string.
1838 local bazel_args_with_config=()
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001839 for arg in $bazel_args; do
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001840 if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
Cole Fausta68f5162022-08-18 17:33:51 -07001841 then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001842 bazel_args_with_config+=("--config=bp2build -- ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001843 config_set=1
1844 else
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001845 bazel_args_with_config+=("$arg ")
Cole Fausta68f5162022-08-18 17:33:51 -07001846 fi
1847 done
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001848 if [[ $config_set -ne 1 ]]; then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001849 bazel_args_with_config+=("--config=bp2build ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001850 fi
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001851
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001852 # Call Bazel.
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001853 bazel ${bazel_args_with_config[@]}
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001854 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001855)
1856
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001857function m()
1858(
1859 _trigger_build "all-modules" "$@"
1860)
1861
1862function mm()
1863(
1864 _trigger_build "modules-in-a-dir-no-deps" "$@"
1865)
1866
1867function mmm()
1868(
1869 _trigger_build "modules-in-dirs-no-deps" "$@"
1870)
1871
1872function mma()
1873(
1874 _trigger_build "modules-in-a-dir" "$@"
1875)
1876
1877function mmma()
1878(
1879 _trigger_build "modules-in-dirs" "$@"
1880)
1881
Matt Alexanderd9c56562020-05-21 10:49:17 +00001882function make()
1883{
Dan Willemsene9842242017-07-28 13:00:13 -07001884 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001885}
1886
Joe Onorato7cf6f972022-05-11 21:39:57 -07001887function _multitree_lunch_error()
1888{
1889 >&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."
1890}
1891
1892function multitree_build()
1893{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001894 local T=$(multitree_gettop)
1895 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001896 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001897 else
1898 _multitree_lunch_error
1899 return 1
1900 fi
1901}
1902
Matt Alexanderd9c56562020-05-21 10:49:17 +00001903function provision()
1904{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001905 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1906 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1907 return 1
1908 fi
1909 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1910 echo "There is no provisioning script for the device." >&2
1911 return 1
1912 fi
1913
1914 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001915 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001916 shift 1
1917 else
1918 echo "This action will reflash your device."
1919 echo ""
1920 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1921 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001922 echo -n "Are you sure you want to do this (yes/no)? "
1923 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001924 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001925 echo "Not taking any action. Exiting." >&2
1926 return 1
1927 fi
1928 fi
1929 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1930}
1931
Jim Tanga881a252018-06-19 16:34:41 +08001932# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001933function enable_zsh_completion() {
1934 # Don't override user's options if bash-style completion is already enabled.
1935 if ! declare -f complete >/dev/null; then
1936 autoload -U compinit && compinit
1937 autoload -U bashcompinit && bashcompinit
1938 fi
Jim Tanga881a252018-06-19 16:34:41 +08001939}
1940
1941function validate_current_shell() {
1942 local current_sh="$(ps -o command -p $$)"
1943 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001944 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001945 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001946 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001947 *zsh*)
1948 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001949 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001950 *)
Jim Tanga881a252018-06-19 16:34:41 +08001951 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 -07001952 ;;
1953 esac
Jim Tanga881a252018-06-19 16:34:41 +08001954}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001955
1956# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001957# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1958# load those.
1959#
1960# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001961function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001962 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001963 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001964 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001965 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 -08001966 if [ -n "$allowed" ]; then
1967 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1968 echo " $allowed"
1969 echo " $f"
1970 return
1971 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001972 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001973 done
1974
1975 allowed_files=
1976 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001977 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001978 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001979 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001980
1981 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001982 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001983 else
1984 echo "ignoring $f, not in $allowed"
1985 fi
Jim Tanga881a252018-06-19 16:34:41 +08001986 done
1987 done
1988}
Kenny Root52aa81c2011-07-15 11:07:06 -07001989
Dan Albertbab814f2020-08-26 15:34:53 -07001990function showcommands() {
1991 local T=$(gettop)
1992 if [[ -z "$TARGET_PRODUCT" ]]; then
1993 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1994 return
1995 fi
1996 case $(uname -s) in
1997 Darwin)
1998 PREBUILT_NAME=darwin-x86
1999 ;;
2000 Linux)
2001 PREBUILT_NAME=linux-x86
2002 ;;
2003 *)
2004 >&2 echo Unknown host $(uname -s)
2005 return
2006 ;;
2007 esac
2008 if [[ -z "$OUT_DIR" ]]; then
2009 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
2010 OUT_DIR=out
2011 else
2012 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
2013 fi
2014 fi
2015 if [[ "$1" == "--regenerate" ]]; then
2016 shift 1
2017 NINJA_ARGS="-t commands $@" m
2018 else
2019 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2020 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2021 -t commands "$@")
2022 fi
2023}
2024
Cole Faust45844ab2022-08-30 13:59:07 -07002025function avbtool() {
2026 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2027 m avbtool
2028 fi
2029 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2030}
2031
Jim Tanga881a252018-06-19 16:34:41 +08002032validate_current_shell
2033source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002034addcompletions