blob: bc206c432461311243b1dbdd076b8f5afe50e57d [file] [log] [blame]
Scott Anderson1a5fc952012-03-07 17:15:06 -08001function hmm() {
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07002cat <<EOF
Jeff Gastonc6dfc4e2017-05-30 17:12:37 -07003
4Run "m help" for help with the build system itself.
5
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08006Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
Steven Moreland62054a42018-12-06 10:11:40 -08007- lunch: lunch <product_name>-<build_variant>
8 Selects <product_name> as the product to build, and <build_variant> as the variant to
9 build, and stores those selections in the environment to be read by subsequent
10 invocations of 'm' etc.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010012 Sets up the build environment for building unbundled apps (APKs).
Anton Hansson90ac61c2022-09-06 14:36:00 +000013- banchan: banchan <module1> [<module2> ...] [arm|x86|arm64|x86_64|arm64_only|x86_64only] \
14 [eng|userdebug|user]
Martin Stjernholmf692c752021-04-12 00:01:10 +010015 Sets up the build environment for building unbundled modules (APEXes).
Anton Hanssonece9c482019-02-04 18:15:39 +000016- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080017- m: Makes from the top of the tree.
Dan Willemsen67074fe2019-10-30 12:35:34 -070018- mm: Builds and installs all of the modules in the current directory, and their
19 dependencies.
20- mmm: Builds and installs all of the modules in the supplied directories, and their
21 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080022 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070023- mma: Same as 'mm'
24- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080025- provision: Flash device with all required partitions. Options will be passed on to fastboot.
26- cgrep: Greps on all local C/C++ files.
27- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010028- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080029- jgrep: Greps on all local Java files.
Taesu Leeea0cecd2020-10-28 11:05:18 +090030- ktgrep: Greps on all local Kotlin files.
Steven Moreland62054a42018-12-06 10:11:40 -080031- resgrep: Greps on all local res/*.xml files.
32- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070033- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060034- owngrep: Greps on all local OWNERS files.
Alistair Delva176e5342021-02-22 13:31:26 -080035- rsgrep: Greps on all local Rust files.
Steven Moreland62054a42018-12-06 10:11:40 -080036- sepgrep: Greps on all local sepolicy files.
37- sgrep: Greps on all local source files.
38- godir: Go to the directory containing a file.
39- allmod: List all modules.
40- gomod: Go to the directory containing a module.
Jingwen Chen83eeebb2022-10-05 02:27:07 +000041- bmod: Get the Bazel label of a Soong module if it is converted with bp2build.
Rett Berg78d1c932019-01-24 14:34:23 -080042- pathmod: Get the directory containing a module.
Cole Faust24c36db2021-01-23 02:39:37 +000043- outmod: Gets the location of a module's installed outputs with a certain extension.
Joe Onorato2c1aa472021-02-25 16:42:39 -080044- dirmods: Gets the modules defined in a given directory.
Cole Faust24c36db2021-01-23 02:39:37 +000045- installmod: Adb installs a module's built APK.
46- refreshmod: Refresh list of modules for allmod/gomod/pathmod/outmod/installmod.
Steven Moreland74114f12020-09-10 01:23:32 +000047- syswrite: Remount partitions (e.g. system.img) as writable, rebooting if necessary.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070048
Roland Levillain39341922015-10-20 12:48:19 +010049Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070050- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080051- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070052
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070053Look at the source to view more functions. The complete list is:
54EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070055 local T=$(gettop)
56 local A=""
57 local i
Jacky Cao89483b82015-05-15 22:12:53 +080058 for i in `cat $T/build/envsetup.sh | sed -n "/^[[:blank:]]*function /s/function \([a-z_]*\).*/\1/p" | sort | uniq`; do
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070059 A="$A $i"
60 done
61 echo $A
62}
63
Ying Wang08800fd2016-03-03 20:57:21 -080064# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000065function build_build_var_cache()
66{
Christopher Ferris55257d22017-03-23 11:08:58 -070067 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080068 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080069 cached_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
70 cached_abs_vars=(`cat $T/build/envsetup.sh | tr '()' ' ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`)
Ying Wang08800fd2016-03-03 20:57:21 -080071 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080072 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080073 --vars="${cached_vars[*]}" \
74 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070075 --var-prefix=var_cache_ \
76 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080077 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000078 if [ $ret -ne 0 ]
79 then
Ying Wang08800fd2016-03-03 20:57:21 -080080 unset build_dicts_script
81 return $ret
82 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070083 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080084 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080085 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080086 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000087 if [ $ret -ne 0 ]
88 then
Ying Wang08800fd2016-03-03 20:57:21 -080089 return $ret
90 fi
91 BUILD_VAR_CACHE_READY="true"
92}
93
Ying Wangf0cb3972016-03-04 13:56:23 -080094# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080095# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000096function destroy_build_var_cache()
97{
Ying Wang08800fd2016-03-03 20:57:21 -080098 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070099 local v
Ying Wang08800fd2016-03-03 20:57:21 -0800100 for v in $cached_vars; do
101 unset var_cache_$v
102 done
103 unset cached_vars
104 for v in $cached_abs_vars; do
105 unset abs_var_cache_$v
106 done
107 unset cached_abs_vars
108}
109
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700110# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000111function get_abs_build_var()
112{
113 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
114 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800115 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200116 return
Ying Wang08800fd2016-03-03 20:57:21 -0800117 fi
118
Christopher Ferris55257d22017-03-23 11:08:58 -0700119 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700120 if [ ! "$T" ]; then
121 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
122 return
123 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700124 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700125}
126
127# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000128function get_build_var()
129{
130 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
131 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800132 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100133 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800134 fi
135
Christopher Ferris55257d22017-03-23 11:08:58 -0700136 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700137 if [ ! "$T" ]; then
138 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100139 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700140 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700141 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142}
143
144# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000145function check_product()
146{
Christopher Ferris55257d22017-03-23 11:08:58 -0700147 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800148 if [ ! "$T" ]; then
149 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
150 return
151 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700152 TARGET_PRODUCT=$1 \
153 TARGET_BUILD_VARIANT= \
154 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700155 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800156 get_build_var TARGET_DEVICE > /dev/null
157 # hide successful answers, but allow the errors to show
158}
159
160VARIANT_CHOICES=(user userdebug eng)
161
162# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000163function check_variant()
164{
Christopher Ferris55257d22017-03-23 11:08:58 -0700165 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800166 for v in ${VARIANT_CHOICES[@]}
167 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000168 if [ "$v" = "$1" ]
169 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800170 return 0
171 fi
172 done
173 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700174}
175
Matt Alexanderd9c56562020-05-21 10:49:17 +0000176function setpaths()
177{
Christopher Ferris55257d22017-03-23 11:08:58 -0700178 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700179 if [ ! "$T" ]; then
180 echo "Couldn't locate the top of the tree. Try setting TOP."
181 return
182 fi
183
184 ##################################################################
185 # #
186 # Read me before you modify this code #
187 # #
188 # This function sets ANDROID_BUILD_PATHS to what it is adding #
189 # to PATH, and the next time it is run, it removes that from #
190 # PATH. This is required so lunch can be run more than once #
191 # and still have working paths. #
192 # #
193 ##################################################################
194
Raphael Mollc639c782011-06-20 17:25:01 -0700195 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
196 # due to "C:\Program Files" being in the path.
197
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700198 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000199 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700200 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
201 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000202 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700203 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800204 # strip leading ':', if any
205 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500206 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700207
208 # and in with the new
Ben Chengfba67bf2014-02-25 10:27:07 -0800209
Dan Willemsen838dcec2021-09-30 22:40:34 +0000210 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700211
212 # add kernel specific binaries
213 case $(uname -s) in
214 Linux)
215 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
216 ;;
217 *)
218 ;;
219 esac
220
Colin Crossc0b01042022-10-05 12:03:03 -0700221 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS)
Yi Kongdfd00b12019-05-21 16:00:04 -0700222 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
223
224 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
225 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
226 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200227
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800228 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
229 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
230
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200231 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
232 # to ensure that the corresponding 'emulator' binaries are used.
233 case $(uname -s) in
234 Darwin)
235 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
236 ;;
237 Linux)
238 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
239 ;;
240 *)
241 ANDROID_EMULATOR_PREBUILTS=
242 ;;
243 esac
244 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700245 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200246 export ANDROID_EMULATOR_PREBUILTS
247 fi
248
Jim Tangb3fda302018-12-22 10:24:55 +0800249 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
250 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700251 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
252 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
253 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
Martin Stjernholm6a012262021-11-24 14:56:15 +0000254 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800255
Martin Stjernholm6a012262021-11-24 14:56:15 +0000256 export ANDROID_BUILD_PATHS=$(tr -s : <<<"${ANDROID_BUILD_PATHS}:")
Ryan Prichard8426a192019-07-15 18:05:29 -0700257 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800258
259 # out with the duplicate old
260 if [ -n $ANDROID_PYTHONPATH ]; then
261 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
262 fi
263 # and in with the new
264 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800265 if [ -n $VENDOR_PYTHONPATH ]; then
266 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
267 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800268 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800269
Colin Crosse97e6932017-06-30 16:01:45 -0700270 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
271 export JAVA_HOME=$ANDROID_JAVA_HOME
272 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
273 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
274 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500275
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800276 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700277 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
278 export OUT=$ANDROID_PRODUCT_OUT
279
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700280 unset ANDROID_HOST_OUT
281 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
282
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900283 unset ANDROID_SOONG_HOST_OUT
284 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
285
Simran Basidd050ed2017-02-13 13:46:48 -0800286 unset ANDROID_HOST_OUT_TESTCASES
287 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
288
289 unset ANDROID_TARGET_OUT_TESTCASES
290 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
291
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800292 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700293 # TODO: fix the path
294 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
295}
296
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000297function bazel()
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000298{
Jingwen Chen17d56992021-06-10 08:17:54 +0000299 if which bazel &>/dev/null; then
300 >&2 echo "NOTE: bazel() function sourced from Android's envsetup.sh is being used instead of $(which bazel)"
301 >&2 echo
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000302 fi
303
Jingwen Chen17d56992021-06-10 08:17:54 +0000304 local T="$(gettop)"
305 if [ ! "$T" ]; then
306 >&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."
307 return
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000308 fi
309
Rupert Shuttleworth5d60d022020-10-25 05:20:03 +0000310 "$T/tools/bazel" "$@"
Rupert Shuttleworth131fa7d2020-10-12 13:41:12 +0000311}
312
Matt Alexanderd9c56562020-05-21 10:49:17 +0000313function printconfig()
314{
Christopher Ferris55257d22017-03-23 11:08:58 -0700315 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 if [ ! "$T" ]; then
317 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
318 return
319 fi
320 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700321}
322
Matt Alexanderd9c56562020-05-21 10:49:17 +0000323function set_stuff_for_environment()
324{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800325 setpaths
326 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800328 export ANDROID_BUILD_TOP=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700329}
330
Matt Alexanderd9c56562020-05-21 10:49:17 +0000331function set_sequence_number()
332{
Colin Cross88737132017-03-21 17:41:03 -0700333 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700334}
335
Makoto Onukida971062018-06-18 10:15:19 -0700336# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
337function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800338 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700339 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800340 *:"$cmd":*)
341 return 1
342 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700343 esac
344 return 0
345}
346
Matt Alexanderd9c56562020-05-21 10:49:17 +0000347function addcompletions()
348{
Ben Taitelbaum8c2c9cf2020-09-22 16:45:05 -0700349 local f=
Kenny Root52aa81c2011-07-15 11:07:06 -0700350
Jim Tanga881a252018-06-19 16:34:41 +0800351 # Keep us from trying to run in something that's neither bash nor zsh.
352 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700353 return
354 fi
355
356 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800357 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700358 return
359 fi
360
Jim Tanga881a252018-06-19 16:34:41 +0800361 local completion_files=(
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000362 packages/modules/adb/adb.bash
Jim Tanga881a252018-06-19 16:34:41 +0800363 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800364 tools/asuite/asuite.sh
Chris Parsonsa2972972022-08-31 15:04:38 -0400365 prebuilts/bazel/common/bazel-complete.bash
Jim Tanga881a252018-06-19 16:34:41 +0800366 )
Makoto Onukida971062018-06-18 10:15:19 -0700367 # Completion can be disabled selectively to allow users to use non-standard completion.
368 # e.g.
369 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
370 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Usta Shrestha1433fb32022-05-13 14:49:40 -0400371 local T=$(gettop)
Jim Tanga881a252018-06-19 16:34:41 +0800372 for f in ${completion_files[*]}; do
Usta Shrestha1433fb32022-05-13 14:49:40 -0400373 f="$T/$f"
MÃ¥rten Kongstadcb5c73f2022-05-04 14:08:12 +0000374 if [ ! -f "$f" ]; then
375 echo "Warning: completion file $f not found"
376 elif should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700377 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700378 fi
379 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700380
Matt Alexanderd9c56562020-05-21 10:49:17 +0000381 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700382 complete -C "bit --tab" bit
383 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000384 if [ -z "$ZSH_VERSION" ]; then
385 # Doesn't work in zsh.
386 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400387 # TODO(b/244559459): Support b autocompletion for zsh
388 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000389 fi
Jim Tanga881a252018-06-19 16:34:41 +0800390 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800391
Cole Faust24c36db2021-01-23 02:39:37 +0000392 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100393 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000394 complete -F _complete_android_module_names outmod
395 complete -F _complete_android_module_names installmod
Jingwen Chen83eeebb2022-10-05 02:27:07 +0000396 complete -F _complete_android_module_names bmod
dimitry73b84812018-12-11 18:06:00 +0100397 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700398}
399
Joe Onorato824608c2022-04-08 11:06:16 -0700400function multitree_lunch_help()
401{
402 echo "usage: lunch PRODUCT-VARIANT" 1>&2
403 echo " Set up android build environment based on a product short name and variant" 1>&2
404 echo 1>&2
405 echo "lunch COMBO_FILE VARIANT" 1>&2
406 echo " Set up android build environment based on a specific lunch combo file" 1>&2
407 echo " and variant." 1>&2
408 echo 1>&2
409 echo "lunch --print [CONFIG]" 1>&2
410 echo " Print the contents of a configuration. If CONFIG is supplied, that config" 1>&2
411 echo " will be flattened and printed. If CONFIG is not supplied, the currently" 1>&2
412 echo " selected config will be printed. Returns 0 on success or nonzero on error." 1>&2
413 echo 1>&2
414 echo "lunch --list" 1>&2
415 echo " List all possible combo files available in the current tree" 1>&2
416 echo 1>&2
417 echo "lunch --help" 1>&2
418 echo "lunch -h" 1>&2
419 echo " Prints this message." 1>&2
420}
421
422function multitree_lunch()
423{
424 local code
425 local results
LaMont Jonesc39e5022022-06-23 19:09:06 +0000426 # Lunch must be run in the topdir, but this way we get a clear error
427 # message, instead of FileNotFound.
428 local T=$(multitree_gettop)
429 if [ -n "$T" ]; then
LaMont Jones5151ddc2022-10-13 23:05:15 +0000430 "$T/orchestrator/build/orchestrator/core/lunch.py" "$@"
LaMont Jonesc39e5022022-06-23 19:09:06 +0000431 else
432 _multitree_lunch_error
433 return 1
434 fi
Joe Onorato824608c2022-04-08 11:06:16 -0700435 if $(echo "$1" | grep -q '^-') ; then
436 # Calls starting with a -- argument are passed directly and the function
437 # returns with the lunch.py exit code.
Spandan Dasca762052022-09-21 00:08:34 +0000438 "${T}/orchestrator/build/orchestrator/core/lunch.py" "$@"
Joe Onorato824608c2022-04-08 11:06:16 -0700439 code=$?
440 if [[ $code -eq 2 ]] ; then
441 echo 1>&2
442 multitree_lunch_help
443 return $code
444 elif [[ $code -ne 0 ]] ; then
445 return $code
446 fi
447 else
448 # All other calls go through the --lunch variant of lunch.py
Spandan Dasca762052022-09-21 00:08:34 +0000449 results=($(${T}/orchestrator/build/orchestrator/core/lunch.py --lunch "$@"))
Joe Onorato824608c2022-04-08 11:06:16 -0700450 code=$?
451 if [[ $code -eq 2 ]] ; then
452 echo 1>&2
453 multitree_lunch_help
454 return $code
455 elif [[ $code -ne 0 ]] ; then
456 return $code
457 fi
458
459 export TARGET_BUILD_COMBO=${results[0]}
460 export TARGET_BUILD_VARIANT=${results[1]}
461 fi
462}
463
Matt Alexanderd9c56562020-05-21 10:49:17 +0000464function choosetype()
465{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700466 echo "Build type choices are:"
467 echo " 1. release"
468 echo " 2. debug"
469 echo
470
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800471 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700472 DEFAULT_NUM=1
473 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700474
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800475 export TARGET_BUILD_TYPE=
476 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 while [ -z $TARGET_BUILD_TYPE ]
478 do
479 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000480 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800481 read ANSWER
482 else
483 echo $1
484 ANSWER=$1
485 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700486 case $ANSWER in
487 "")
488 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
489 ;;
490 1)
491 export TARGET_BUILD_TYPE=release
492 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800493 release)
494 export TARGET_BUILD_TYPE=release
495 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700496 2)
497 export TARGET_BUILD_TYPE=debug
498 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800499 debug)
500 export TARGET_BUILD_TYPE=debug
501 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700502 *)
503 echo
504 echo "I didn't understand your response. Please try again."
505 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700506 ;;
507 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000508 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800509 break
510 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700511 done
512
Ying Wang08800fd2016-03-03 20:57:21 -0800513 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700514 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800515 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700516}
517
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800518#
519# This function isn't really right: It chooses a TARGET_PRODUCT
520# based on the list of boards. Usually, that gets you something
521# that kinda works with a generic product, but really, you should
522# pick a product by name.
523#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000524function chooseproduct()
525{
Christopher Ferris55257d22017-03-23 11:08:58 -0700526 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000527 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700528 default_value=$TARGET_PRODUCT
529 else
Ying Wang0a76df52015-06-08 11:57:26 -0700530 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700531 fi
532
Ying Wang08800fd2016-03-03 20:57:21 -0800533 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800534 export TARGET_PRODUCT=
535 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700536 while [ -z "$TARGET_PRODUCT" ]
537 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700538 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000539 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800540 read ANSWER
541 else
542 echo $1
543 ANSWER=$1
544 fi
545
Matt Alexanderd9c56562020-05-21 10:49:17 +0000546 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700547 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800548 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000549 if check_product $ANSWER
550 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800551 export TARGET_PRODUCT=$ANSWER
552 else
553 echo "** Not a valid product: $ANSWER"
554 fi
555 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000556 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700558 fi
559 done
560
Ying Wang08800fd2016-03-03 20:57:21 -0800561 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700562 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800563 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700564}
565
Matt Alexanderd9c56562020-05-21 10:49:17 +0000566function choosevariant()
567{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800568 echo "Variant choices are:"
569 local index=1
570 local v
571 for v in ${VARIANT_CHOICES[@]}
572 do
573 # The product name is the name of the directory containing
574 # the makefile we found, above.
575 echo " $index. $v"
576 index=$(($index+1))
577 done
578
579 local default_value=eng
580 local ANSWER
581
582 export TARGET_BUILD_VARIANT=
583 while [ -z "$TARGET_BUILD_VARIANT" ]
584 do
585 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000586 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800587 read ANSWER
588 else
589 echo $1
590 ANSWER=$1
591 fi
592
Matt Alexanderd9c56562020-05-21 10:49:17 +0000593 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800594 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000595 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
596 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200597 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800598 fi
599 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000600 if check_variant $ANSWER
601 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 export TARGET_BUILD_VARIANT=$ANSWER
603 else
604 echo "** Not a valid variant: $ANSWER"
605 fi
606 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000607 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800608 break
609 fi
610 done
611}
612
Matt Alexanderd9c56562020-05-21 10:49:17 +0000613function choosecombo()
614{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700615 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700616
617 echo
618 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700619 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700620
621 echo
622 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700623 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624
625 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800626 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700627 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800628 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800629 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700630}
631
Matt Alexanderd9c56562020-05-21 10:49:17 +0000632function add_lunch_combo()
633{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800634 if [ -n "$ZSH_VERSION" ]; then
635 echo -n "${funcfiletrace[1]}: "
636 else
637 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
638 fi
639 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 -0800640}
641
Matt Alexanderd9c56562020-05-21 10:49:17 +0000642function print_lunch_menu()
643{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700644 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100645 local choices
Colin Crossfa50d402021-04-22 13:05:41 -0700646 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 +0100647 local ret=$?
648
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700649 echo
650 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100652
Matt Alexanderd9c56562020-05-21 10:49:17 +0000653 if [ $ret -ne 0 ]
654 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100655 echo "Warning: Cannot display lunch menu."
656 echo
657 echo "Note: You can invoke lunch with an explicit target:"
658 echo
659 echo " usage: lunch [target]" >&2
660 echo
661 return
662 fi
663
Will Burr40401202022-02-07 12:12:01 +0000664 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800665
666 local i=1
667 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700668 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800669 do
670 echo " $i. $choice"
671 i=$(($i+1))
672 done
673
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700674 echo
675}
676
Matt Alexanderd9c56562020-05-21 10:49:17 +0000677function lunch()
678{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800679 local answer
680
Steven Moreland92793dc2020-02-25 18:30:18 -0800681 if [[ $# -gt 1 ]]; then
682 echo "usage: lunch [target]" >&2
683 return 1
684 fi
685
Will Burr40401202022-02-07 12:12:01 +0000686 local used_lunch_menu=0
687
Steven Moreland92793dc2020-02-25 18:30:18 -0800688 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800689 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700690 else
691 print_lunch_menu
Will Burr40401202022-02-07 12:12:01 +0000692 echo "Which would you like? [aosp_arm-eng]"
693 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 -0800694 read answer
Will Burr40401202022-02-07 12:12:01 +0000695 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700696 fi
697
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800698 local selection=
699
Matt Alexanderd9c56562020-05-21 10:49:17 +0000700 if [ -z "$answer" ]
701 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700702 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000703 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
704 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800705 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000706 if [ $answer -le ${#choices[@]} ]
707 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800708 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000709 if [ -n "$ZSH_VERSION" ]
710 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800711 selection=${choices[$(($answer))]}
712 else
713 selection=${choices[$(($answer-1))]}
714 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800715 fi
Colin Cross88737132017-03-21 17:41:03 -0700716 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800717 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700718 fi
719
Joe Onoratoda12daf2010-06-09 18:18:31 -0700720 export TARGET_BUILD_APPS=
721
Colin Cross88737132017-03-21 17:41:03 -0700722 local product variant_and_version variant version
Colin Cross88737132017-03-21 17:41:03 -0700723 product=${selection%%-*} # Trim everything after first dash
724 variant_and_version=${selection#*-} # Trim everything up to first dash
725 if [ "$variant_and_version" != "$selection" ]; then
726 variant=${variant_and_version%%-*}
727 if [ "$variant" != "$variant_and_version" ]; then
728 version=${variant_and_version#*-}
729 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700730 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800731
Matt Alexanderd9c56562020-05-21 10:49:17 +0000732 if [ -z "$product" ]
733 then
Ying Wang08800fd2016-03-03 20:57:21 -0800734 echo
Colin Cross88737132017-03-21 17:41:03 -0700735 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700736 return 1
737 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800738
Colin Cross88737132017-03-21 17:41:03 -0700739 TARGET_PRODUCT=$product \
740 TARGET_BUILD_VARIANT=$variant \
741 TARGET_PLATFORM_VERSION=$version \
742 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000743 if [ $? -ne 0 ]
744 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100745 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
746 then
747 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
748 fi
Colin Cross88737132017-03-21 17:41:03 -0700749 return 1
750 fi
Colin Cross88737132017-03-21 17:41:03 -0700751 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
752 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700753 if [ -n "$version" ]; then
754 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
755 else
756 unset TARGET_PLATFORM_VERSION
757 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700758 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759
Will Burr40401202022-02-07 12:12:01 +0000760 if [ $used_lunch_menu -eq 1 ]; then
761 echo
762 echo "Hint: next time you can simply run 'lunch $selection'"
763 fi
764
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700765 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800766
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 set_stuff_for_environment
Sasha Smundak90d07bc2020-06-04 10:48:15 -0700768 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800769 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400770
771 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
772 check_mu_config
773 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774}
775
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700776unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700777# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000778function _lunch()
779{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700780 local cur prev opts
781 COMPREPLY=()
782 cur="${COMP_WORDS[COMP_CWORD]}"
783 prev="${COMP_WORDS[COMP_CWORD-1]}"
784
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700785 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800786 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700787 fi
788
789 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700790 return 0
791}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700792
Joe Onoratoda12daf2010-06-09 18:18:31 -0700793# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700794# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000795function tapas()
796{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700797 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800798 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700799 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700800 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 -0800801 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
802 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)"
803
Joe Onoratoda12daf2010-06-09 18:18:31 -0700804
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700805 if [ "$showHelp" != "" ]; then
806 $(gettop)/build/make/tapasHelp.sh
807 return
808 fi
809
Ying Wang67f02922012-08-22 10:25:20 -0700810 if [ $(echo $arch | wc -w) -gt 1 ]; then
811 echo "tapas: Error: Multiple build archs supplied: $arch"
812 return
813 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700814 if [ $(echo $variant | wc -w) -gt 1 ]; then
815 echo "tapas: Error: Multiple build variants supplied: $variant"
816 return
817 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700818 if [ $(echo $density | wc -w) -gt 1 ]; then
819 echo "tapas: Error: Multiple densities supplied: $density"
820 return
821 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800822 if [ $(echo $keys | wc -w) -gt 1 ]; then
823 echo "tapas: Error: Multiple keys supplied: $keys"
824 return
825 fi
Ying Wang67f02922012-08-22 10:25:20 -0700826
Ying Wang0a76df52015-06-08 11:57:26 -0700827 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700828 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000829 x86) product=aosp_x86;;
830 arm64) product=aosp_arm64;;
831 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700832 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800833 if [ -n "$keys" ]; then
834 product=${product/aosp_/aosp_${keys}_}
835 fi;
836
Joe Onoratoda12daf2010-06-09 18:18:31 -0700837 if [ -z "$variant" ]; then
838 variant=eng
839 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700840 if [ -z "$apps" ]; then
841 apps=all
842 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600843 if [ -z "$density" ]; then
844 density=alldpi
845 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700846
Ying Wang67f02922012-08-22 10:25:20 -0700847 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700848 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700849 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700850 export TARGET_BUILD_TYPE=release
851 export TARGET_BUILD_APPS=$apps
852
Ying Wang08800fd2016-03-03 20:57:21 -0800853 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700854 set_stuff_for_environment
855 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800856 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700857}
858
Martin Stjernholmf692c752021-04-12 00:01:10 +0100859# Configures the build to build unbundled Android modules (APEXes).
860# Run banchan with one or more module names (from apex{} modules).
861function banchan()
862{
863 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Anton Hansson90ac61c2022-09-06 14:36:00 +0000864 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 +0100865 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100866 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 +0100867
868 if [ "$showHelp" != "" ]; then
869 $(gettop)/build/make/banchanHelp.sh
870 return
871 fi
872
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100873 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000874 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100875 elif [ $(echo $product | wc -w) -gt 1 ]; then
876 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100877 return
878 fi
879 if [ $(echo $variant | wc -w) -gt 1 ]; then
880 echo "banchan: Error: Multiple build variants supplied: $variant"
881 return
882 fi
883 if [ -z "$apps" ]; then
884 echo "banchan: Error: No modules supplied"
885 return
886 fi
887
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100888 case $product in
889 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100890 x86) product=module_x86;;
891 arm64) product=module_arm64;;
892 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000893 arm64only) product=module_arm64only;;
894 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100895 esac
896 if [ -z "$variant" ]; then
897 variant=eng
898 fi
899
900 export TARGET_PRODUCT=$product
901 export TARGET_BUILD_VARIANT=$variant
902 export TARGET_BUILD_DENSITY=alldpi
903 export TARGET_BUILD_TYPE=release
904
905 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
906 # case is different and it may diverge in the future.
907 export TARGET_BUILD_APPS=$apps
908
909 build_build_var_cache
910 set_stuff_for_environment
911 printconfig
912 destroy_build_var_cache
913}
914
Matt Alexanderd9c56562020-05-21 10:49:17 +0000915function gettop
916{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700917 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000918 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700919 # The following circumlocution ensures we remove symlinks from TOP.
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000920 (cd "$TOP"; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700921 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000922 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800923 # The following circumlocution (repeated below as well) ensures
924 # that we record the true directory name and not one that is
925 # faked up with symlink names.
926 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700927 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800928 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700929 local T=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000930 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800931 \cd ..
synergyb112a402013-07-05 19:47:47 -0700932 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700933 done
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000934 \cd "$HERE"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700935 if [ -f "$T/$TOPFILE" ]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +0000936 echo "$T"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700937 fi
938 fi
939 fi
940}
941
Joe Onorato7cf6f972022-05-11 21:39:57 -0700942# TODO: Merge into gettop as part of launching multitree
943function multitree_gettop
944{
Spandan Dasca762052022-09-21 00:08:34 +0000945 local TOPFILE=orchestrator/build/make/core/envsetup.mk
Joe Onorato7cf6f972022-05-11 21:39:57 -0700946 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
947 # The following circumlocution ensures we remove symlinks from TOP.
948 (cd "$TOP"; PWD= /bin/pwd)
949 else
950 if [ -f $TOPFILE ] ; then
951 # The following circumlocution (repeated below as well) ensures
952 # that we record the true directory name and not one that is
953 # faked up with symlink names.
954 PWD= /bin/pwd
955 else
956 local HERE=$PWD
957 local T=
958 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
959 \cd ..
960 T=`PWD= /bin/pwd -P`
961 done
962 \cd "$HERE"
963 if [ -f "$T/$TOPFILE" ]; then
964 echo "$T"
965 fi
966 fi
967 fi
968}
969
Matt Alexanderd9c56562020-05-21 10:49:17 +0000970function croot()
971{
Christopher Ferris55257d22017-03-23 11:08:58 -0700972 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700973 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700974 if [ "$1" ]; then
975 \cd $(gettop)/$1
976 else
977 \cd $(gettop)
978 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700979 else
980 echo "Couldn't locate the top of the tree. Try setting TOP."
981 fi
982}
983
Matt Alexanderd9c56562020-05-21 10:49:17 +0000984function _croot()
985{
Anton Hanssonece9c482019-02-04 18:15:39 +0000986 local T=$(gettop)
987 if [ "$T" ]; then
988 local cur="${COMP_WORDS[COMP_CWORD]}"
989 k=0
990 for c in $(compgen -d ${T}/${cur}); do
991 COMPREPLY[k++]=${c#${T}/}/
992 done
993 fi
994}
995
Matt Alexanderd9c56562020-05-21 10:49:17 +0000996function cproj()
997{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700998 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700999 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001000 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001001 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1002 T=$PWD
1003 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001004 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001005 return
1006 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001007 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001008 done
Ying Wang9cd17642012-12-13 10:52:07 -08001009 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001010 echo "can't find Android.mk"
1011}
1012
Daniel Sandler47e0a882013-07-30 13:23:52 -04001013# simplified version of ps; output in the form
1014# <pid> <procname>
1015function qpid() {
1016 local prepend=''
1017 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +00001018 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -04001019 prepend=' '
1020 append='$'
1021 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +00001022 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001023 echo "usage: qpid [[--exact] <process name|pid>"
1024 return 255
1025 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001026
1027 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001028 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001029 qpid | \grep "$prepend$EXE$append"
1030 else
1031 adb shell ps \
1032 | tr -d '\r' \
1033 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1034 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001035}
1036
Steven Moreland74114f12020-09-10 01:23:32 +00001037# syswrite - disable verity, reboot if needed, and remount image
1038#
1039# Easy way to make system.img/etc writable
1040function syswrite() {
1041 adb wait-for-device && adb root || return 1
Frederick Maylee59a8792022-08-22 22:40:23 +00001042 if [[ $(adb disable-verity | grep -i "reboot") ]]; then
Steven Moreland74114f12020-09-10 01:23:32 +00001043 echo "rebooting"
1044 adb reboot && adb wait-for-device && adb root || return 1
1045 fi
1046 adb wait-for-device && adb remount || return 1
1047}
1048
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001049# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001050# that has the core-file-size limit set correctly
1051#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001052# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001053# if its core-file-size limit is not set already.
1054# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1055
Matt Alexanderd9c56562020-05-21 10:49:17 +00001056function coredump_setup()
1057{
1058 echo "Getting root...";
1059 adb root;
1060 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001061
Matt Alexanderd9c56562020-05-21 10:49:17 +00001062 echo "Remounting root partition read-write...";
1063 adb shell mount -w -o remount -t rootfs rootfs;
1064 sleep 1;
1065 adb wait-for-device;
1066 adb shell mkdir -p /cores;
1067 adb shell mount -t tmpfs tmpfs /cores;
1068 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001069
Matt Alexanderd9c56562020-05-21 10:49:17 +00001070 echo "Granting SELinux permission to dump in /cores...";
1071 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001072
Matt Alexanderd9c56562020-05-21 10:49:17 +00001073 echo "Set core pattern.";
1074 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001075
Ying Wang08800fd2016-03-03 20:57:21 -08001076 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001077}
1078
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001079# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001080# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001081#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001082# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001083# dump to actually be generated.
1084
Matt Alexanderd9c56562020-05-21 10:49:17 +00001085function coredump_enable()
1086{
1087 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -08001088 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001089 printf "Expecting a PID!\n";
1090 return;
1091 fi;
1092 echo "Setting core limit for $PID to infinite...";
xi yuaddf4832022-05-26 12:41:21 +00001093 adb shell /system/bin/ulimit -P $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001094}
1095
1096# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +00001097# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001098#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001099# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001100# enabled globally.
1101
Matt Alexanderd9c56562020-05-21 10:49:17 +00001102function core()
1103{
1104 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001105
Ying Wang08800fd2016-03-03 20:57:21 -08001106 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +00001107 printf "Expecting a PID!\n";
1108 return;
1109 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001110
Matt Alexanderd9c56562020-05-21 10:49:17 +00001111 local CORENAME=core.$PID;
1112 local COREPATH=/cores/$CORENAME;
1113 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114
Matt Alexanderd9c56562020-05-21 10:49:17 +00001115 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001116
Matt Alexanderd9c56562020-05-21 10:49:17 +00001117 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -08001118 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +00001119 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1120 adb shell kill -$SIG $PID;
1121 sleep 1;
1122 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001123
Matt Alexanderd9c56562020-05-21 10:49:17 +00001124 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1125 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001126}
1127
Christopher Tate744ee802009-11-12 15:33:08 -08001128# systemstack - dump the current stack trace of all threads in the system process
1129# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +00001130function systemstack()
1131{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001132 stacks system_server
1133}
1134
Michael Wrightaeed7212014-06-19 19:58:12 -07001135# Read the ELF header from /proc/$PID/exe to determine if the process is
1136# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001137function is64bit()
1138{
Ben Chengfba67bf2014-02-25 10:27:07 -08001139 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140 if [ "$PID" ] ; then
1141 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001142 echo "64"
1143 else
1144 echo ""
1145 fi
1146 else
1147 echo ""
1148 fi
1149}
1150
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001151case `uname -s` in
1152 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001153 function sgrep()
1154 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001155 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 -04001156 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001157 }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001158
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001159 ;;
1160 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001161 function sgrep()
1162 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001163 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 -04001164 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001165 }
1166 ;;
1167esac
1168
Matt Alexanderd9c56562020-05-21 10:49:17 +00001169function gettargetarch
1170{
Raghu Gandham8da43102012-07-25 19:57:22 -07001171 get_build_var TARGET_ARCH
1172}
1173
Matt Alexanderd9c56562020-05-21 10:49:17 +00001174function ggrep()
1175{
Mike Frysinger5e479732015-09-22 18:13:48 -04001176 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1177 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001178}
1179
Matt Alexanderd9c56562020-05-21 10:49:17 +00001180function gogrep()
1181{
Orion Hodson831472d2019-10-25 11:35:15 +01001182 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
1183 -exec grep --color -n "$@" {} +
1184}
1185
Matt Alexanderd9c56562020-05-21 10:49:17 +00001186function jgrep()
1187{
Mike Frysinger5e479732015-09-22 18:13:48 -04001188 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1189 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001190}
1191
Alistair Delva176e5342021-02-22 13:31:26 -08001192function rsgrep()
Jeff Vander Stoep1431ab82021-02-02 19:18:26 +01001193{
1194 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rs" \
1195 -exec grep --color -n "$@" {} +
1196}
1197
Taesu Leeea0cecd2020-10-28 11:05:18 +09001198function ktgrep()
1199{
1200 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.kt" \
1201 -exec grep --color -n "$@" {} +
1202}
1203
Matt Alexanderd9c56562020-05-21 10:49:17 +00001204function cgrep()
1205{
Mike Frysinger5e479732015-09-22 18:13:48 -04001206 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' \) \
1207 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001208}
1209
Matt Alexanderd9c56562020-05-21 10:49:17 +00001210function resgrep()
1211{
Christopher Ferris55257d22017-03-23 11:08:58 -07001212 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001213 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1214 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1215 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216}
1217
Matt Alexanderd9c56562020-05-21 10:49:17 +00001218function mangrep()
1219{
Mike Frysinger5e479732015-09-22 18:13:48 -04001220 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1221 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001222}
1223
Matt Alexanderd9c56562020-05-21 10:49:17 +00001224function owngrep()
1225{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001226 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1227 -exec grep --color -n "$@" {} +
1228}
1229
Matt Alexanderd9c56562020-05-21 10:49:17 +00001230function sepgrep()
1231{
Mike Frysinger5e479732015-09-22 18:13:48 -04001232 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1233 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001234}
1235
Matt Alexanderd9c56562020-05-21 10:49:17 +00001236function rcgrep()
1237{
Mike Frysinger5e479732015-09-22 18:13:48 -04001238 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1239 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001240}
1241
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001242case `uname -s` in
1243 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001244 function mgrep()
1245 {
Orion Hodson831472d2019-10-25 11:35:15 +01001246 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 -04001247 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001248 }
1249
Matt Alexanderd9c56562020-05-21 10:49:17 +00001250 function treegrep()
1251 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001252 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 -04001253 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001254 }
1255
1256 ;;
1257 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001258 function mgrep()
1259 {
Orion Hodson831472d2019-10-25 11:35:15 +01001260 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 -04001261 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001262 }
1263
Matt Alexanderd9c56562020-05-21 10:49:17 +00001264 function treegrep()
1265 {
Taesu Leeea0cecd2020-10-28 11:05:18 +09001266 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 -04001267 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001268 }
1269
1270 ;;
1271esac
1272
Matt Alexanderd9c56562020-05-21 10:49:17 +00001273function getprebuilt
1274{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001275 get_abs_build_var ANDROID_PREBUILTS
1276}
1277
Matt Alexanderd9c56562020-05-21 10:49:17 +00001278function tracedmdump()
1279{
Christopher Ferris55257d22017-03-23 11:08:58 -07001280 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001281 if [ ! "$T" ]; then
1282 echo "Couldn't locate the top of the tree. Try setting TOP."
1283 return
1284 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001285 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001286 local arch=$(gettargetarch)
1287 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001288
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001289 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001290 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001291 echo "usage: tracedmdump tracename"
1292 return
1293 fi
1294
Matt Alexanderd9c56562020-05-21 10:49:17 +00001295 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001296 echo "Error: cannot find kernel: '$KERNEL'"
1297 return
1298 fi
1299
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001300 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001301 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001302 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1303 fi
1304
1305 echo "post-processing traces..."
1306 rm -f $TRACE/qtrace.dexlist
1307 post_trace $TRACE
1308 if [ $? -ne 0 ]; then
1309 echo "***"
1310 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1311 echo "***"
1312 return
1313 fi
1314 echo "generating dexlist output..."
1315 /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
1316 echo "generating dmtrace data..."
1317 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1318 echo "generating html file..."
1319 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1320 echo "done, see $TRACE/dmtrace.html for details"
1321 echo "or run:"
1322 echo " traceview $TRACE/dmtrace"
1323}
1324
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001325# communicate with a running device or emulator, set up necessary state,
1326# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001327function runhat()
1328{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001329 # process standard adb options
1330 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001331 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001332 adbTarget=$1
1333 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001334 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001335 adbTarget="$1 $2"
1336 shift 2
1337 fi
1338 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001339 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001340
1341 # runhat options
1342 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001343
Matt Alexanderd9c56562020-05-21 10:49:17 +00001344 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001345 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001346 return
1347 fi
1348
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001349 # confirm hat is available
1350 if [ -z $(which hat) ]; then
1351 echo "hat is not available in this configuration."
1352 return
1353 fi
1354
Andy McFaddenb6289852010-07-12 08:00:19 -07001355 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001356 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001357 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001358 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001359 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001360 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001361 echo -n "> "
1362 read
1363
The Android Open Source Project88b60792009-03-03 19:28:42 -08001364 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001365
The Android Open Source Project88b60792009-03-03 19:28:42 -08001366 echo "Retrieving file $devFile..."
1367 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001368
The Android Open Source Project88b60792009-03-03 19:28:42 -08001369 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001370
The Android Open Source Project88b60792009-03-03 19:28:42 -08001371 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001372 echo "View the output by pointing your browser at http://localhost:7000/"
1373 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001374 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001375}
1376
Matt Alexanderd9c56562020-05-21 10:49:17 +00001377function getbugreports()
1378{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001379 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001380
1381 if [ ! "$reports" ]; then
1382 echo "Could not locate any bugreports."
1383 return
1384 fi
1385
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001386 local report
1387 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001388 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001389 echo "/sdcard/bugreports/${report}"
1390 adb pull /sdcard/bugreports/${report} ${report}
1391 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001392 done
1393}
1394
Matt Alexanderd9c56562020-05-21 10:49:17 +00001395function getsdcardpath()
1396{
Victoria Lease1b296b42012-08-21 15:44:06 -07001397 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1398}
1399
Matt Alexanderd9c56562020-05-21 10:49:17 +00001400function getscreenshotpath()
1401{
Victoria Lease1b296b42012-08-21 15:44:06 -07001402 echo "$(getsdcardpath)/Pictures/Screenshots"
1403}
1404
Matt Alexanderd9c56562020-05-21 10:49:17 +00001405function getlastscreenshot()
1406{
Victoria Lease1b296b42012-08-21 15:44:06 -07001407 local screenshot_path=$(getscreenshotpath)
1408 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 +00001409 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001410 echo "No screenshots found."
1411 return
1412 fi
1413 echo "${screenshot}"
1414 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1415}
1416
Matt Alexanderd9c56562020-05-21 10:49:17 +00001417function startviewserver()
1418{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001419 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001420 if [ $# -gt 0 ]; then
1421 port=$1
1422 fi
1423 adb shell service call window 1 i32 $port
1424}
1425
Matt Alexanderd9c56562020-05-21 10:49:17 +00001426function stopviewserver()
1427{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001428 adb shell service call window 2
1429}
1430
Matt Alexanderd9c56562020-05-21 10:49:17 +00001431function isviewserverstarted()
1432{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001433 adb shell service call window 3
1434}
1435
Matt Alexanderd9c56562020-05-21 10:49:17 +00001436function key_home()
1437{
Romain Guyb84049a2010-10-04 16:56:11 -07001438 adb shell input keyevent 3
1439}
1440
Matt Alexanderd9c56562020-05-21 10:49:17 +00001441function key_back()
1442{
Romain Guyb84049a2010-10-04 16:56:11 -07001443 adb shell input keyevent 4
1444}
1445
Matt Alexanderd9c56562020-05-21 10:49:17 +00001446function key_menu()
1447{
Romain Guyb84049a2010-10-04 16:56:11 -07001448 adb shell input keyevent 82
1449}
1450
Matt Alexanderd9c56562020-05-21 10:49:17 +00001451function smoketest()
1452{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001453 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1454 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1455 return
1456 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001457 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001458 if [ ! "$T" ]; then
1459 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1460 return
1461 fi
1462
Ying Wang9cd17642012-12-13 10:52:07 -08001463 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001464 adb uninstall com.android.smoketest > /dev/null &&
1465 adb uninstall com.android.smoketest.tests > /dev/null &&
1466 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1467 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1468 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1469}
1470
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001471# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001472function runtest()
1473{
Christopher Ferris55257d22017-03-23 11:08:58 -07001474 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001475 if [ ! "$T" ]; then
1476 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1477 return
1478 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001479 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001480}
1481
The Android Open Source Project88b60792009-03-03 19:28:42 -08001482function godir () {
1483 if [[ -z "$1" ]]; then
1484 echo "Usage: godir <regex>"
1485 return
1486 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001487 local T=$(gettop)
1488 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001489 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001490 mkdir -p $OUT_DIR
1491 FILELIST=$OUT_DIR/filelist
1492 else
1493 FILELIST=$T/filelist
1494 fi
1495 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001496 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001497 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001498 echo " Done"
1499 echo ""
1500 fi
1501 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001502 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001503 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001504 echo "Not found"
1505 return
1506 fi
1507 local pathname
1508 local choice
1509 if [[ ${#lines[@]} > 1 ]]; then
1510 while [[ -z "$pathname" ]]; do
1511 local index=1
1512 local line
1513 for line in ${lines[@]}; do
1514 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001515 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001516 done
1517 echo
1518 echo -n "Select one: "
1519 unset choice
1520 read choice
1521 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1522 echo "Invalid choice"
1523 continue
1524 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001525 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001526 done
1527 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001528 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001529 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001530 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001531}
1532
Steven Moreland62054a42018-12-06 10:11:40 -08001533# Update module-info.json in out.
1534function refreshmod() {
1535 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1536 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1537 return 1
1538 fi
1539
1540 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1541
1542 # for the output of the next command
1543 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1544
1545 # Note, can't use absolute path because of the way make works.
Alessandro Astonec8771be2020-05-10 11:27:57 +02001546 m $(get_build_var PRODUCT_OUT)/module-info.json \
Steven Moreland62054a42018-12-06 10:11:40 -08001547 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1548}
1549
Cole Faust3e192382021-10-25 13:29:15 -07001550# Verifies that module-info.txt exists, returning nonzero if it doesn't.
Cole Faust24c36db2021-01-23 02:39:37 +00001551function verifymodinfo() {
Steven Moreland62054a42018-12-06 10:11:40 -08001552 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001553 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
1554 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1555 fi
Steven Moreland62054a42018-12-06 10:11:40 -08001556 return 1
1557 fi
1558
1559 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001560 if [ "$QUIET_VERIFYMODINFO" != "true" ] ; then
Cole Faust3e192382021-10-25 13:29:15 -07001561 echo "Could not find module-info.json. Please run 'refreshmod' first." >&2
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001562 fi
1563 return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001564 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001565}
1566
1567# List all modules for the current device, as cached in module-info.json. If any build change is
1568# made and it should be reflected in the output, you should run 'refreshmod' first.
1569function allmod() {
1570 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001571
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001572 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 -08001573}
1574
Jingwen Chen83eeebb2022-10-05 02:27:07 +00001575# Return the Bazel label of a Soong module if it is converted with bp2build.
1576function bmod()
1577(
1578 if [ $# -ne 1 ]; then
1579 echo "usage: bmod <module>" >&2
1580 return 1
1581 fi
1582
1583 # We could run bp2build here, but it might trigger bp2build invalidation
1584 # when used with `b` (e.g. --run_soong_tests) and/or add unnecessary waiting
1585 # time overhead.
1586 #
1587 # For a snappy result, use the latest generated version in soong_injection,
1588 # and ask users to run m bp2build if it doesn't exist.
1589 converted_json="out/soong/soong_injection/metrics/converted_modules_path_map.json"
1590
1591 if [ ! -f $(gettop)/${converted_json} ]; then
1592 echo "bp2build files not found. Have you ran 'm bp2build'?" >&2
1593 return 1
1594 fi
1595
1596 local target_label=$(python3 -c "import json
1597module = '$1'
1598converted_json='$converted_json'
1599bp2build_converted_map = json.load(open(converted_json))
1600if module not in bp2build_converted_map:
1601 exit(1)
1602print(bp2build_converted_map[module] + ':' + module)")
1603
1604 if [ -z "${target_label}" ]; then
1605 echo "$1 is not converted to Bazel." >&2
1606 return 1
1607 else
1608 echo "${target_label}"
1609 fi
1610)
1611
Joe Onorato2c1aa472021-02-25 16:42:39 -08001612# Get the path of a specific module in the android tree, as cached in module-info.json.
1613# If any build change is made, and it should be reflected in the output, you should run
1614# 'refreshmod' first. Note: This is the inverse of dirmods.
Rett Berg78d1c932019-01-24 14:34:23 -08001615function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001616 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001617 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001618 return 1
1619 fi
1620
Cole Faust24c36db2021-01-23 02:39:37 +00001621 verifymodinfo || return 1
Steven Moreland62054a42018-12-06 10:11:40 -08001622
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001623 local relpath=$(python3 -c "import json, os
Steven Moreland62054a42018-12-06 10:11:40 -08001624module = '$1'
1625module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1626if module not in module_info:
1627 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001628print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001629
1630 if [ -z "$relpath" ]; then
1631 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1632 return 1
1633 else
Rett Berg78d1c932019-01-24 14:34:23 -08001634 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001635 fi
1636}
1637
Joe Onorato2c1aa472021-02-25 16:42:39 -08001638# Get the path of a specific module in the android tree, as cached in module-info.json.
1639# If any build change is made, and it should be reflected in the output, you should run
1640# 'refreshmod' first. Note: This is the inverse of pathmod.
1641function dirmods() {
1642 if [[ $# -ne 1 ]]; then
1643 echo "usage: dirmods <path>" >&2
1644 return 1
1645 fi
1646
1647 verifymodinfo || return 1
1648
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001649 python3 -c "import json, os
Joe Onorato2c1aa472021-02-25 16:42:39 -08001650dir = '$1'
1651while dir.endswith('/'):
1652 dir = dir[:-1]
1653prefix = dir + '/'
1654module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1655results = set()
1656for m in module_info.values():
1657 for path in m.get(u'path', []):
1658 if path == dir or path.startswith(prefix):
1659 name = m.get(u'module_name')
1660 if name:
1661 results.add(name)
1662for name in sorted(results):
1663 print(name)
1664"
1665}
1666
1667
Rett Berg78d1c932019-01-24 14:34:23 -08001668# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1669# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1670function gomod() {
1671 if [[ $# -ne 1 ]]; then
1672 echo "usage: gomod <module>" >&2
1673 return 1
1674 fi
1675
1676 local path="$(pathmod $@)"
1677 if [ -z "$path" ]; then
1678 return 1
1679 fi
1680 cd $path
1681}
1682
Cole Faust24c36db2021-01-23 02:39:37 +00001683# Gets the list of a module's installed outputs, as cached in module-info.json.
1684# If any build change is made, and it should be reflected in the output, you should run 'refreshmod' first.
1685function outmod() {
1686 if [[ $# -ne 1 ]]; then
1687 echo "usage: outmod <module>" >&2
1688 return 1
1689 fi
1690
1691 verifymodinfo || return 1
1692
1693 local relpath
Joe Onorato4acbe3b2021-04-29 15:31:42 -07001694 relpath=$(python3 -c "import json, os
Cole Faust24c36db2021-01-23 02:39:37 +00001695module = '$1'
1696module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1697if module not in module_info:
1698 exit(1)
1699for output in module_info[module]['installed']:
1700 print(os.path.join('$ANDROID_BUILD_TOP', output))" 2>/dev/null)
1701
1702 if [ $? -ne 0 ]; then
1703 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)" >&2
1704 return 1
1705 elif [ ! -z "$relpath" ]; then
1706 echo "$relpath"
1707 fi
1708}
1709
1710# adb install a module's apk, as cached in module-info.json. If any build change
1711# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1712# Usage: installmod [adb install arguments] <module>
1713# For example: installmod -r Dialer -> adb install -r /path/to/Dialer.apk
1714function installmod() {
1715 if [[ $# -eq 0 ]]; then
1716 echo "usage: installmod [adb install arguments] <module>" >&2
Cole Faust3e192382021-10-25 13:29:15 -07001717 echo "" >&2
1718 echo "Only flags to be passed after the \"install\" in adb install are supported," >&2
1719 echo "with the exception of -s. If -s is passed it will be placed before the \"install\"." >&2
1720 echo "-s must be the first flag passed if it exists." >&2
Cole Faust24c36db2021-01-23 02:39:37 +00001721 return 1
1722 fi
1723
1724 local _path
1725 _path=$(outmod ${@:$#:1})
1726 if [ $? -ne 0 ]; then
1727 return 1
1728 fi
1729
1730 _path=$(echo "$_path" | grep -E \\.apk$ | head -n 1)
1731 if [ -z "$_path" ]; then
1732 echo "Module '$1' does not produce a file ending with .apk (try 'refreshmod' if there have been build changes?)" >&2
1733 return 1
1734 fi
Cole Faust3e192382021-10-25 13:29:15 -07001735 local serial_device=""
1736 if [[ "$1" == "-s" ]]; then
1737 if [[ $# -le 2 ]]; then
1738 echo "-s requires an argument" >&2
1739 return 1
1740 fi
1741 serial_device="-s $2"
1742 shift 2
1743 fi
Cole Faust24c36db2021-01-23 02:39:37 +00001744 local length=$(( $# - 1 ))
Cole Faust3e192382021-10-25 13:29:15 -07001745 echo adb $serial_device install ${@:1:$length} $_path
1746 adb $serial_device install ${@:1:$length} $_path
Cole Faust24c36db2021-01-23 02:39:37 +00001747}
1748
dimitry73b84812018-12-11 18:06:00 +01001749function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001750 local word=${COMP_WORDS[COMP_CWORD]}
Joe Onorato0bac4fe2021-04-07 08:51:28 -07001751 COMPREPLY=( $(QUIET_VERIFYMODINFO=true allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -08001752}
1753
Alex Rayf0d08eb2013-03-08 15:15:06 -08001754# Print colored exit condition
1755function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001756 "$@"
1757 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001758 if [ $retval -ne 0 ]
1759 then
Jacky Cao89483b82015-05-15 22:12:53 +08001760 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001761 else
Jacky Cao89483b82015-05-15 22:12:53 +08001762 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001763 fi
1764 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001765}
1766
Matt Alexanderd9c56562020-05-21 10:49:17 +00001767function get_make_command()
1768{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001769 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1770 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001771 # Always use the real make if -C is passed in
1772 for arg in "$@"; do
1773 if [[ $arg == -C* ]]; then
1774 echo command make
1775 return
1776 fi
1777 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001778 echo build/soong/soong_ui.bash --make-mode
1779 else
1780 echo command make
1781 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001782}
1783
Matt Alexanderd9c56562020-05-21 10:49:17 +00001784function _wrap_build()
1785{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001786 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1787 "$@"
1788 return $?
1789 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001790 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001791 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001792 local ret=$?
1793 local end_time=$(date +"%s")
1794 local tdiff=$(($end_time-$start_time))
1795 local hours=$(($tdiff / 3600 ))
1796 local mins=$((($tdiff % 3600) / 60))
1797 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001798 local ncolors=$(tput colors 2>/dev/null)
1799 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001800 color_failed=$'\E'"[0;31m"
1801 color_success=$'\E'"[0;32m"
Kousik Kumar09af2542021-08-19 16:13:32 -04001802 color_warning=$'\E'"[0;33m"
Jacky Cao89483b82015-05-15 22:12:53 +08001803 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001804 else
1805 color_failed=""
1806 color_success=""
1807 color_reset=""
1808 fi
Kousik Kumar09af2542021-08-19 16:13:32 -04001809
1810 if [[ "x${USE_RBE}" == "x" && $mins -gt 15 && "${ANDROID_BUILD_ENVIRONMENT_CONFIG}" == "googler" ]]; then
1811 echo
1812 echo "${color_warning}Start using RBE (http://go/build-fast) to get faster builds!${color_reset}"
1813 fi
1814
Ed Heylcc6be0a2014-06-18 14:55:58 -07001815 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001816 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001817 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001818 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001819 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001820 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001821 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001822 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001823 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001824 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001825 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001826 printf "(%s seconds)" $secs
1827 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001828 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001829 echo
1830 return $ret
1831}
1832
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001833function _trigger_build()
1834(
1835 local -r bc="$1"; shift
LaMont Jonesc39e5022022-06-23 19:09:06 +00001836 local T=$(gettop)
1837 if [ -n "$T" ]; then
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001838 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1839 else
Jingwen Chend728ee12021-05-18 06:02:53 +00001840 >&2 echo "Couldn't locate the top of the tree. Try setting TOP."
1841 return 1
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001842 fi
1843)
1844
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001845# Convenience entry point (like m) to use Bazel in AOSP.
Jingwen Chend728ee12021-05-18 06:02:53 +00001846function b()
1847(
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001848 # zsh breaks posix by not doing string-splitting on unquoted args by default.
1849 # See https://zsh.sourceforge.io/Guide/zshguide05.html section 5.4.4.
1850 # Tell it to emulate Bourne shell for this function.
1851 if [ -n "$ZSH_VERSION" ]; then emulate -L sh; fi
1852
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001853 # Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
1854 local bazel_args=""
1855 local skip_tests="--skip-soong-tests"
1856 for i in $@; do
1857 if [[ $i != "--run-soong-tests" ]]; then
1858 bazel_args+="$i "
1859 else
1860 skip_tests=""
1861 fi
1862 done
Jingwen Chen141d63d2022-10-06 15:26:58 +00001863
Jingwen Chend728ee12021-05-18 06:02:53 +00001864 # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
Jingwen Chen141d63d2022-10-06 15:26:58 +00001865 # RBE is disabled because it's not used with b builds and adds overhead: b/251441524
1866 USE_RBE=false _trigger_build "all-modules" bp2build $skip_tests USE_BAZEL_ANALYSIS= || return 1
Jingwen Chend728ee12021-05-18 06:02:53 +00001867 # Then, run Bazel using the synthetic workspace as the --package_path.
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001868 if [[ -z "$bazel_args" ]]; then
Jingwen Chen6f4963b2022-09-09 07:08:25 +00001869 # If there are no args, show help and exit.
Jingwen Chenbfa114d2021-06-17 05:42:43 +00001870 bazel help
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001871 else
1872 # Else, always run with the bp2build configuration, which sets Bazel's package path to the synthetic workspace.
Cole Fausta68f5162022-08-18 17:33:51 -07001873 # Add the --config=bp2build after the first argument that doesn't start with a dash. That should be the bazel
1874 # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work with commands like:
1875 # b run //foo -- --args-for-foo
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001876 local config_set=0
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001877
1878 # Represent the args as an array, not a string.
1879 local bazel_args_with_config=()
Sam Delmericoe4c74c52022-08-30 10:17:38 -04001880 for arg in $bazel_args; do
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001881 if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here
Cole Fausta68f5162022-08-18 17:33:51 -07001882 then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001883 bazel_args_with_config+=("--config=bp2build -- ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001884 config_set=1
1885 else
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001886 bazel_args_with_config+=("$arg ")
Cole Fausta68f5162022-08-18 17:33:51 -07001887 fi
1888 done
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001889 if [[ $config_set -ne 1 ]]; then
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001890 bazel_args_with_config+=("--config=bp2build ")
Sam Delmericoe31d7de2022-08-29 17:39:44 -04001891 fi
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001892
Jingwen Chen4ed082f2022-09-09 07:08:25 +00001893 # Call Bazel.
Sam Delmericod0c8cf02022-09-22 14:10:40 -04001894 bazel ${bazel_args_with_config[@]}
Jingwen Chen69f9ed12021-06-16 09:44:26 +00001895 fi
Jingwen Chend728ee12021-05-18 06:02:53 +00001896)
1897
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001898function m()
1899(
1900 _trigger_build "all-modules" "$@"
1901)
1902
1903function mm()
1904(
1905 _trigger_build "modules-in-a-dir-no-deps" "$@"
1906)
1907
1908function mmm()
1909(
1910 _trigger_build "modules-in-dirs-no-deps" "$@"
1911)
1912
1913function mma()
1914(
1915 _trigger_build "modules-in-a-dir" "$@"
1916)
1917
1918function mmma()
1919(
1920 _trigger_build "modules-in-dirs" "$@"
1921)
1922
Matt Alexanderd9c56562020-05-21 10:49:17 +00001923function make()
1924{
Dan Willemsene9842242017-07-28 13:00:13 -07001925 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001926}
1927
Joe Onorato7cf6f972022-05-11 21:39:57 -07001928function _multitree_lunch_error()
1929{
1930 >&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."
1931}
1932
1933function multitree_build()
1934{
LaMont Jonesc39e5022022-06-23 19:09:06 +00001935 local T=$(multitree_gettop)
1936 if [ -n "$T" ]; then
Spandan Dasca762052022-09-21 00:08:34 +00001937 "$T/orchestrator/build/orchestrator/core/orchestrator.py" "$@"
Joe Onorato7cf6f972022-05-11 21:39:57 -07001938 else
1939 _multitree_lunch_error
1940 return 1
1941 fi
1942}
1943
Matt Alexanderd9c56562020-05-21 10:49:17 +00001944function provision()
1945{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001946 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1947 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1948 return 1
1949 fi
1950 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1951 echo "There is no provisioning script for the device." >&2
1952 return 1
1953 fi
1954
1955 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001956 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001957 shift 1
1958 else
1959 echo "This action will reflash your device."
1960 echo ""
1961 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1962 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001963 echo -n "Are you sure you want to do this (yes/no)? "
1964 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001965 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001966 echo "Not taking any action. Exiting." >&2
1967 return 1
1968 fi
1969 fi
1970 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1971}
1972
Jim Tanga881a252018-06-19 16:34:41 +08001973# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001974function enable_zsh_completion() {
1975 # Don't override user's options if bash-style completion is already enabled.
1976 if ! declare -f complete >/dev/null; then
1977 autoload -U compinit && compinit
1978 autoload -U bashcompinit && bashcompinit
1979 fi
Jim Tanga881a252018-06-19 16:34:41 +08001980}
1981
1982function validate_current_shell() {
1983 local current_sh="$(ps -o command -p $$)"
1984 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001985 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001986 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001987 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001988 *zsh*)
1989 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001990 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001991 *)
Jim Tanga881a252018-06-19 16:34:41 +08001992 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 -07001993 ;;
1994 esac
Jim Tanga881a252018-06-19 16:34:41 +08001995}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001996
1997# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001998# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1999# load those.
2000#
2001# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08002002function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08002003 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002004 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08002005 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002006 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 -08002007 if [ -n "$allowed" ]; then
2008 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
2009 echo " $allowed"
2010 echo " $f"
2011 return
2012 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002013 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002014 done
2015
2016 allowed_files=
2017 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08002018 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002019 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08002020 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08002021
2022 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00002023 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08002024 else
2025 echo "ignoring $f, not in $allowed"
2026 fi
Jim Tanga881a252018-06-19 16:34:41 +08002027 done
2028 done
2029}
Kenny Root52aa81c2011-07-15 11:07:06 -07002030
Dan Albertbab814f2020-08-26 15:34:53 -07002031function showcommands() {
2032 local T=$(gettop)
2033 if [[ -z "$TARGET_PRODUCT" ]]; then
2034 >&2 echo "TARGET_PRODUCT not set. Run lunch."
2035 return
2036 fi
2037 case $(uname -s) in
2038 Darwin)
2039 PREBUILT_NAME=darwin-x86
2040 ;;
2041 Linux)
2042 PREBUILT_NAME=linux-x86
2043 ;;
2044 *)
2045 >&2 echo Unknown host $(uname -s)
2046 return
2047 ;;
2048 esac
2049 if [[ -z "$OUT_DIR" ]]; then
2050 if [[ -z "$OUT_DIR_COMMON_BASE" ]]; then
2051 OUT_DIR=out
2052 else
2053 OUT_DIR=${OUT_DIR_COMMON_BASE}/${PWD##*/}
2054 fi
2055 fi
2056 if [[ "$1" == "--regenerate" ]]; then
2057 shift 1
2058 NINJA_ARGS="-t commands $@" m
2059 else
2060 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
2061 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
2062 -t commands "$@")
2063 fi
2064}
2065
Cole Faust45844ab2022-08-30 13:59:07 -07002066function avbtool() {
2067 if [[ ! -f "$ANDROID_SOONG_HOST_OUT"/bin/avbtool ]]; then
2068 m avbtool
2069 fi
2070 "$ANDROID_SOONG_HOST_OUT"/bin/avbtool $@
2071}
2072
Jim Tanga881a252018-06-19 16:34:41 +08002073validate_current_shell
2074source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07002075addcompletions