blob: 01bc7002e85593f34cf3251f0c8513c7ce54bb0d [file] [log] [blame]
Joe Onorato344e4042022-12-05 15:15:36 -08001# Copyright (C) 2022 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# gettop is duplicated here and in shell_utils.mk, because it's difficult
16# to find shell_utils.make without it for all the novel ways this file can be
17# sourced. Other common functions should only be in one place or the other.
18function _gettop_once
19{
20 local TOPFILE=build/make/core/envsetup.mk
21 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
22 # The following circumlocution ensures we remove symlinks from TOP.
23 (cd "$TOP"; PWD= /bin/pwd)
24 else
25 if [ -f $TOPFILE ] ; then
26 # The following circumlocution (repeated below as well) ensures
27 # that we record the true directory name and not one that is
28 # faked up with symlink names.
29 PWD= /bin/pwd
30 else
31 local HERE=$PWD
32 local T=
33 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
34 \cd ..
35 T=`PWD= /bin/pwd -P`
36 done
37 \cd "$HERE"
38 if [ -f "$T/$TOPFILE" ]; then
39 echo "$T"
40 fi
41 fi
42 fi
43}
44T=$(_gettop_once)
45if [ ! "$T" ]; then
46 echo "Couldn't locate the top of the tree. Always source build/envsetup.sh from the root of the tree." >&2
47 return 1
48fi
49IMPORTING_ENVSETUP=true source $T/build/make/shell_utils.sh
50
Ying Wang08800fd2016-03-03 20:57:21 -080051# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000052function build_build_var_cache()
53{
Christopher Ferris55257d22017-03-23 11:08:58 -070054 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080055 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080056 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' ' '`)
57 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 -080058 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080059 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080060 --vars="${cached_vars[*]}" \
61 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070062 --var-prefix=var_cache_ \
63 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080064 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000065 if [ $ret -ne 0 ]
66 then
Ying Wang08800fd2016-03-03 20:57:21 -080067 unset build_dicts_script
68 return $ret
69 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070070 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080071 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080072 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080073 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000074 if [ $ret -ne 0 ]
75 then
Ying Wang08800fd2016-03-03 20:57:21 -080076 return $ret
77 fi
78 BUILD_VAR_CACHE_READY="true"
79}
80
Ying Wangf0cb3972016-03-04 13:56:23 -080081# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080082# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000083function destroy_build_var_cache()
84{
Ying Wang08800fd2016-03-03 20:57:21 -080085 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070086 local v
Ying Wang08800fd2016-03-03 20:57:21 -080087 for v in $cached_vars; do
88 unset var_cache_$v
89 done
90 unset cached_vars
91 for v in $cached_abs_vars; do
92 unset abs_var_cache_$v
93 done
94 unset cached_abs_vars
95}
96
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070097# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +000098function get_abs_build_var()
99{
100 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
101 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800102 eval "echo \"\${abs_var_cache_$1}\""
Timi0469c3f2021-04-15 16:41:18 +0200103 return
Ying Wang08800fd2016-03-03 20:57:21 -0800104 fi
105
Christopher Ferris55257d22017-03-23 11:08:58 -0700106 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700107 if [ ! "$T" ]; then
108 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
109 return
110 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700111 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700112}
113
114# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000115function get_build_var()
116{
117 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
118 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800119 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100120 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800121 fi
122
Christopher Ferris55257d22017-03-23 11:08:58 -0700123 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700124 if [ ! "$T" ]; then
125 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100126 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700127 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700128 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800129}
130
Joe Onorato32b2aa32024-05-14 13:54:13 -0700131# This logic matches envsetup.mk
132function get_host_prebuilt_prefix
133{
134 local un=$(uname)
135 if [[ $un == "Linux" ]] ; then
136 echo linux-x86
137 elif [[ $un == "Darwin" ]] ; then
138 echo darwin-x86
139 else
140 echo "Error: Invalid host operating system: $un" 1>&2
141 fi
142}
143
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800144# Add directories to PATH that are dependent on the lunch target.
145# For directories that are not lunch-specific, add them in set_global_paths
146function set_lunch_paths()
Matt Alexanderd9c56562020-05-21 10:49:17 +0000147{
Christopher Ferris55257d22017-03-23 11:08:58 -0700148 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700149 if [ ! "$T" ]; then
150 echo "Couldn't locate the top of the tree. Try setting TOP."
151 return
152 fi
153
154 ##################################################################
155 # #
156 # Read me before you modify this code #
157 # #
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800158 # This function sets ANDROID_LUNCH_BUILD_PATHS to what it is #
159 # adding to PATH, and the next time it is run, it removes that #
160 # from PATH. This is required so lunch can be run more than #
161 # once and still have working paths. #
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700162 # #
163 ##################################################################
164
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800165 # Note: on windows/cygwin, ANDROID_LUNCH_BUILD_PATHS will contain spaces
Raphael Mollc639c782011-06-20 17:25:01 -0700166 # due to "C:\Program Files" being in the path.
167
Kevin Dagostino185109b2024-01-11 17:39:02 +0000168 # Handle compat with the old ANDROID_BUILD_PATHS variable.
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800169 # TODO: Remove this after we think everyone has lunched again.
170 if [ -z "$ANDROID_LUNCH_BUILD_PATHS" -a -n "$ANDROID_BUILD_PATHS" ] ; then
171 ANDROID_LUNCH_BUILD_PATHS="$ANDROID_BUILD_PATHS"
172 ANDROID_BUILD_PATHS=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700173 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000174 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700175 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800176 # strip leading ':', if any
177 export PATH=${PATH/:%/}
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800178 ANDROID_PRE_BUILD_PATHS=
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500179 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700180
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800181 # Out with the old...
182 if [ -n "$ANDROID_LUNCH_BUILD_PATHS" ] ; then
183 export PATH=${PATH/$ANDROID_LUNCH_BUILD_PATHS/}
184 fi
Ben Chengfba67bf2014-02-25 10:27:07 -0800185
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800186 # And in with the new...
187 ANDROID_LUNCH_BUILD_PATHS=$(get_abs_build_var SOONG_HOST_OUT_EXECUTABLES)
188 ANDROID_LUNCH_BUILD_PATHS+=:$(get_abs_build_var HOST_OUT_EXECUTABLES)
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700189
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800190 # Append llvm binutils prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Yi Kongdfd00b12019-05-21 16:00:04 -0700191 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800192 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200193
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800194 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
195 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
196
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800197 # Append asuite prebuilts path to ANDROID_LUNCH_BUILD_PATHS.
Jim Tangb3fda302018-12-22 10:24:55 +0800198 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800199 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/acloud/$os_arch
200 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/aidegen/$os_arch
201 ANDROID_LUNCH_BUILD_PATHS+=:$T/prebuilts/asuite/atest/$os_arch
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800202
Colin Crosse97e6932017-06-30 16:01:45 -0700203 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
204 export JAVA_HOME=$ANDROID_JAVA_HOME
205 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800206 ANDROID_LUNCH_BUILD_PATHS+=:$ANDROID_JAVA_TOOLCHAIN
207
208 # Fix up PYTHONPATH
209 if [ -n $ANDROID_PYTHONPATH ]; then
210 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
211 fi
Dan Albert0d6d3592023-01-26 00:07:03 +0000212 # //development/python-packages contains both a pseudo-PYTHONPATH which
213 # mimics an already assembled venv, but also contains real Python packages
214 # that are not in that layout until they are installed. We can fake it for
215 # the latter type by adding the package source directories to the PYTHONPATH
216 # directly. For the former group, we only need to add the python-packages
217 # directory itself.
218 #
219 # This could be cleaned up by converting the remaining packages that are in
220 # the first category into a typical python source layout (that is, another
221 # layer of directory nesting) and automatically adding all subdirectories of
222 # python-packages to the PYTHONPATH instead of manually curating this. We
223 # can't convert the packages like adb to the other style because doing so
224 # would prevent exporting type info from those packages.
225 #
226 # http://b/266688086
Dan Albert426ac692023-05-16 22:59:55 +0000227 export ANDROID_PYTHONPATH=$T/development/python-packages/adb:$T/development/python-packages/gdbrunner:$T/development/python-packages:
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800228 if [ -n $VENDOR_PYTHONPATH ]; then
229 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
230 fi
231 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500232
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800233 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700234 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
235 export OUT=$ANDROID_PRODUCT_OUT
236
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700237 unset ANDROID_HOST_OUT
238 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
239
Jiyong Parkc02b1c42020-11-03 11:06:39 +0900240 unset ANDROID_SOONG_HOST_OUT
241 export ANDROID_SOONG_HOST_OUT=$(get_abs_build_var SOONG_HOST_OUT)
242
Simran Basidd050ed2017-02-13 13:46:48 -0800243 unset ANDROID_HOST_OUT_TESTCASES
244 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
245
246 unset ANDROID_TARGET_OUT_TESTCASES
247 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
248
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800249 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800250 export PATH=$ANDROID_LUNCH_BUILD_PATHS:$PATH
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800251}
252
253# Add directories to PATH that are NOT dependent on the lunch target.
254# For directories that are lunch-specific, add them in set_lunch_paths
255function set_global_paths()
256{
257 local T=$(gettop)
258 if [ ! "$T" ]; then
259 echo "Couldn't locate the top of the tree. Try setting TOP."
260 return
261 fi
262
263 ##################################################################
264 # #
265 # Read me before you modify this code #
266 # #
267 # This function sets ANDROID_GLOBAL_BUILD_PATHS to what it is #
268 # adding to PATH, and the next time it is run, it removes that #
269 # from PATH. This is required so envsetup.sh can be sourced #
270 # more than once and still have working paths. #
271 # #
272 ##################################################################
273
274 # Out with the old...
275 if [ -n "$ANDROID_GLOBAL_BUILD_PATHS" ] ; then
276 export PATH=${PATH/$ANDROID_GLOBAL_BUILD_PATHS/}
277 fi
278
279 # And in with the new...
Joe Onorato84e61d02024-02-02 22:53:39 -0800280 ANDROID_GLOBAL_BUILD_PATHS=$T/build/soong/bin
LaMont Jonese8a3be22024-02-12 09:55:41 -0800281 ANDROID_GLOBAL_BUILD_PATHS+=:$T/build/bazel/bin
Joe Onorato1cb9e152022-12-05 16:56:15 -0800282 ANDROID_GLOBAL_BUILD_PATHS+=:$T/development/scripts
283 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/devtools/tools
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800284
285 # add kernel specific binaries
286 if [ $(uname -s) = Linux ] ; then
287 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/dtc
288 ANDROID_GLOBAL_BUILD_PATHS+=:$T/prebuilts/misc/linux-x86/libufdt
289 fi
290
291 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
292 # to ensure that the corresponding 'emulator' binaries are used.
293 case $(uname -s) in
294 Darwin)
295 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
296 ;;
297 Linux)
298 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
299 ;;
300 *)
301 ANDROID_EMULATOR_PREBUILTS=
302 ;;
303 esac
304 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
305 ANDROID_GLOBAL_BUILD_PATHS+=:$ANDROID_EMULATOR_PREBUILTS
306 export ANDROID_EMULATOR_PREBUILTS
307 fi
308
309 # Finally, set PATH
Joe Onorato1cb9e152022-12-05 16:56:15 -0800310 export PATH=$ANDROID_GLOBAL_BUILD_PATHS:$PATH
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311}
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{
Joe Onorato7c3a77f2022-12-05 13:05:14 -0800325 set_lunch_paths
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 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
Anton Hanssonece9c482019-02-04 18:15:39 +0000381 if [ -z "$ZSH_VERSION" ]; then
382 # Doesn't work in zsh.
383 complete -o nospace -F _croot croot
Chris Parsonsa2972972022-08-31 15:04:38 -0400384 # TODO(b/244559459): Support b autocompletion for zsh
385 complete -F _bazel__complete -o nospace b
Anton Hanssonece9c482019-02-04 18:15:39 +0000386 fi
Jim Tanga881a252018-06-19 16:34:41 +0800387 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800388
Cole Faust24c36db2021-01-23 02:39:37 +0000389 complete -F _complete_android_module_names pathmod
dimitry73b84812018-12-11 18:06:00 +0100390 complete -F _complete_android_module_names gomod
Cole Faust24c36db2021-01-23 02:39:37 +0000391 complete -F _complete_android_module_names outmod
392 complete -F _complete_android_module_names installmod
dimitry73b84812018-12-11 18:06:00 +0100393 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700394}
395
Matt Alexanderd9c56562020-05-21 10:49:17 +0000396function add_lunch_combo()
397{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800398 if [ -n "$ZSH_VERSION" ]; then
399 echo -n "${funcfiletrace[1]}: "
400 else
401 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
402 fi
403 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 -0800404}
405
Matt Alexanderd9c56562020-05-21 10:49:17 +0000406function print_lunch_menu()
407{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700408 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100409 local choices
Greg Kaiserb6a0d392023-10-27 15:56:32 -0600410 choices=$(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_RELEASE= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100411 local ret=$?
412
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700413 echo
414 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700415 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100416
Matt Alexanderd9c56562020-05-21 10:49:17 +0000417 if [ $ret -ne 0 ]
418 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100419 echo "Warning: Cannot display lunch menu."
420 echo
421 echo "Note: You can invoke lunch with an explicit target:"
422 echo
423 echo " usage: lunch [target]" >&2
424 echo
425 return
426 fi
427
Will Burr40401202022-02-07 12:12:01 +0000428 echo "Lunch menu .. Here are the common combinations:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800429
430 local i=1
431 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700432 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800433 do
434 echo " $i. $choice"
435 i=$(($i+1))
436 done
437
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438 echo
439}
440
Matt Alexanderd9c56562020-05-21 10:49:17 +0000441function lunch()
442{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800443 local answer
444
Steven Moreland92793dc2020-02-25 18:30:18 -0800445 if [[ $# -gt 1 ]]; then
446 echo "usage: lunch [target]" >&2
447 return 1
448 fi
449
Will Burr40401202022-02-07 12:12:01 +0000450 local used_lunch_menu=0
451
Steven Moreland92793dc2020-02-25 18:30:18 -0800452 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800453 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700454 else
455 print_lunch_menu
Greg Kaiserbae4c572024-01-04 15:57:54 -0700456 echo "Which would you like? [aosp_cf_x86_64_phone-trunk_staging-eng]"
Greg Kaiser9a5a5262023-11-02 16:54:27 +0000457 echo -n "Pick from common choices above (e.g. 13) or specify your own (e.g. aosp_barbet-trunk_staging-eng): "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800458 read answer
Will Burr40401202022-02-07 12:12:01 +0000459 used_lunch_menu=1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 fi
461
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800462 local selection=
463
Matt Alexanderd9c56562020-05-21 10:49:17 +0000464 if [ -z "$answer" ]
465 then
Greg Kaiserbae4c572024-01-04 15:57:54 -0700466 selection=aosp_cf_x86_64_phone-trunk_staging-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000467 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
468 then
Fabián Cañas03dd0282024-01-05 16:57:39 -0500469 local choices=($(TARGET_BUILD_APPS= TARGET_PRODUCT= TARGET_RELEASE= TARGET_BUILD_VARIANT= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000470 if [ $answer -le ${#choices[@]} ]
471 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800472 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000473 if [ -n "$ZSH_VERSION" ]
474 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800475 selection=${choices[$(($answer))]}
476 else
477 selection=${choices[$(($answer-1))]}
478 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800479 fi
Colin Cross88737132017-03-21 17:41:03 -0700480 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800481 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700482 fi
483
Joe Onoratoda12daf2010-06-09 18:18:31 -0700484 export TARGET_BUILD_APPS=
485
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600486 # This must be <product>-<release>-<variant>
487 local product release variant
488 # Split string on the '-' character.
489 IFS="-" read -r product release variant <<< "$selection"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800490
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600491 if [[ -z "$product" ]] || [[ -z "$release" ]] || [[ -z "$variant" ]]
Matt Alexanderd9c56562020-05-21 10:49:17 +0000492 then
Ying Wang08800fd2016-03-03 20:57:21 -0800493 echo
Colin Cross88737132017-03-21 17:41:03 -0700494 echo "Invalid lunch combo: $selection"
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600495 echo "Valid combos must be of the form <product>-<release>-<variant>"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700496 return 1
497 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800498
Colin Cross88737132017-03-21 17:41:03 -0700499 TARGET_PRODUCT=$product \
500 TARGET_BUILD_VARIANT=$variant \
Jeff Hamiltona02c7472023-05-08 03:13:29 +0000501 TARGET_RELEASE=$release \
Colin Cross88737132017-03-21 17:41:03 -0700502 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000503 if [ $? -ne 0 ]
504 then
Anton Hansson32fa7ee2021-06-14 17:09:58 +0100505 if [[ "$product" =~ .*_(eng|user|userdebug) ]]
506 then
507 echo "Did you mean -${product/*_/}? (dash instead of underscore)"
508 fi
Colin Cross88737132017-03-21 17:41:03 -0700509 return 1
510 fi
Colin Cross88737132017-03-21 17:41:03 -0700511 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
512 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Greg Kaiser5e2d3392023-10-27 14:15:48 -0600513 export TARGET_RELEASE=$release
514 # Note this is the string "release", not the value of the variable.
Jeff Browne33ba4c2011-07-11 22:11:46 -0700515 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700516
Mark White2c807512023-10-25 17:27:07 +0000517 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || echo
518
519 set_stuff_for_environment
520 [[ -n "${ANDROID_QUIET_BUILD:-}" ]] || printconfig
521
522 if [ "${TARGET_BUILD_VARIANT}" = "userdebug" ] && [[ -z "${ANDROID_QUIET_BUILD}" ]]; then
523 echo
524 echo "Want FASTER LOCAL BUILDS? Use -eng instead of -userdebug (however for" \
525 "performance benchmarking continue to use userdebug)"
526 fi
Will Burr40401202022-02-07 12:12:01 +0000527 if [ $used_lunch_menu -eq 1 ]; then
528 echo
529 echo "Hint: next time you can simply run 'lunch $selection'"
530 fi
531
Ying Wang08800fd2016-03-03 20:57:21 -0800532 destroy_build_var_cache
Kousik Kumar41dacd12021-05-11 18:38:38 -0400533
534 if [[ -n "${CHECK_MU_CONFIG:-}" ]]; then
535 check_mu_config
536 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700537}
538
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700539unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700540# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000541function _lunch()
542{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700543 local cur prev opts
544 COMPREPLY=()
545 cur="${COMP_WORDS[COMP_CWORD]}"
546 prev="${COMP_WORDS[COMP_CWORD-1]}"
547
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700548 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800549 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700550 fi
551
552 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700553 return 0
554}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700555
Joe Onoratoda12daf2010-06-09 18:18:31 -0700556# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700557# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000558function tapas()
559{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700560 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800561 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Greg Kaiser83ed1592023-10-26 18:37:40 +0000562 # TODO(b/307975293): Expand tapas to take release arguments (and update hmm() usage).
563 local release="trunk_staging"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700564 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700565 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 -0800566 local keys="$(echo $* | xargs -n 1 echo | \grep -E '^(devkeys)$' | xargs)"
567 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)"
568
Joe Onoratoda12daf2010-06-09 18:18:31 -0700569
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700570 if [ "$showHelp" != "" ]; then
571 $(gettop)/build/make/tapasHelp.sh
572 return
573 fi
574
Ying Wang67f02922012-08-22 10:25:20 -0700575 if [ $(echo $arch | wc -w) -gt 1 ]; then
576 echo "tapas: Error: Multiple build archs supplied: $arch"
577 return
578 fi
Greg Kaiser83ed1592023-10-26 18:37:40 +0000579 if [ $(echo $release | wc -w) -gt 1 ]; then
580 echo "tapas: Error: Multiple build releases supplied: $release"
581 return
582 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700583 if [ $(echo $variant | wc -w) -gt 1 ]; then
584 echo "tapas: Error: Multiple build variants supplied: $variant"
585 return
586 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700587 if [ $(echo $density | wc -w) -gt 1 ]; then
588 echo "tapas: Error: Multiple densities supplied: $density"
589 return
590 fi
Colin Cross7f49a672022-01-27 18:15:53 -0800591 if [ $(echo $keys | wc -w) -gt 1 ]; then
592 echo "tapas: Error: Multiple keys supplied: $keys"
593 return
594 fi
Ying Wang67f02922012-08-22 10:25:20 -0700595
Ying Wang0a76df52015-06-08 11:57:26 -0700596 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700597 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000598 x86) product=aosp_x86;;
599 arm64) product=aosp_arm64;;
600 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700601 esac
Colin Cross7f49a672022-01-27 18:15:53 -0800602 if [ -n "$keys" ]; then
603 product=${product/aosp_/aosp_${keys}_}
604 fi;
605
Joe Onoratoda12daf2010-06-09 18:18:31 -0700606 if [ -z "$variant" ]; then
607 variant=eng
608 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700609 if [ -z "$apps" ]; then
610 apps=all
611 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600612 if [ -z "$density" ]; then
613 density=alldpi
614 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700615
Ying Wang67f02922012-08-22 10:25:20 -0700616 export TARGET_PRODUCT=$product
Greg Kaiser83ed1592023-10-26 18:37:40 +0000617 export TARGET_RELEASE=$release
Joe Onoratoda12daf2010-06-09 18:18:31 -0700618 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700619 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700620 export TARGET_BUILD_TYPE=release
621 export TARGET_BUILD_APPS=$apps
622
Ying Wang08800fd2016-03-03 20:57:21 -0800623 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700624 set_stuff_for_environment
625 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800626 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700627}
628
Martin Stjernholmf692c752021-04-12 00:01:10 +0100629# Configures the build to build unbundled Android modules (APEXes).
630# Run banchan with one or more module names (from apex{} modules).
631function banchan()
632{
633 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100634 local product="$(echo $* | xargs -n 1 echo | \grep -E '^(.*_)?(arm|x86|arm64|riscv64|x86_64|arm64only|x86_64only)$' | xargs)"
Greg Kaiserd35095e2023-10-27 16:04:30 -0600635 # TODO: Expand banchan to take release arguments (and update hmm() usage).
636 local release="trunk_staging"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100637 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100638 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|(.*_)?(arm|x86|arm64|riscv64|x86_64))$' | xargs)"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100639
640 if [ "$showHelp" != "" ]; then
641 $(gettop)/build/make/banchanHelp.sh
642 return
643 fi
644
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100645 if [ -z "$product" ]; then
Anton Hansson0328e322022-05-24 15:47:40 +0000646 product=arm64
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100647 elif [ $(echo $product | wc -w) -gt 1 ]; then
648 echo "banchan: Error: Multiple build archs or products supplied: $products"
Martin Stjernholmf692c752021-04-12 00:01:10 +0100649 return
650 fi
Greg Kaiserd35095e2023-10-27 16:04:30 -0600651 if [ $(echo $release | wc -w) -gt 1 ]; then
652 echo "banchan: Error: Multiple build releases supplied: $release"
653 return
654 fi
Martin Stjernholmf692c752021-04-12 00:01:10 +0100655 if [ $(echo $variant | wc -w) -gt 1 ]; then
656 echo "banchan: Error: Multiple build variants supplied: $variant"
657 return
658 fi
659 if [ -z "$apps" ]; then
660 echo "banchan: Error: No modules supplied"
661 return
662 fi
663
Martin Stjernholm2b8d9232021-04-16 20:45:03 +0100664 case $product in
665 arm) product=module_arm;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100666 x86) product=module_x86;;
667 arm64) product=module_arm64;;
Ulya Trafimovich08c381b2023-06-12 15:33:09 +0100668 riscv64) product=module_riscv64;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100669 x86_64) product=module_x86_64;;
Anton Hansson90ac61c2022-09-06 14:36:00 +0000670 arm64only) product=module_arm64only;;
671 x86_64only) product=module_x86_64only;;
Martin Stjernholmf692c752021-04-12 00:01:10 +0100672 esac
673 if [ -z "$variant" ]; then
674 variant=eng
675 fi
676
677 export TARGET_PRODUCT=$product
Greg Kaiserd35095e2023-10-27 16:04:30 -0600678 export TARGET_RELEASE=$release
Martin Stjernholmf692c752021-04-12 00:01:10 +0100679 export TARGET_BUILD_VARIANT=$variant
680 export TARGET_BUILD_DENSITY=alldpi
681 export TARGET_BUILD_TYPE=release
682
683 # This setup currently uses TARGET_BUILD_APPS just like tapas, but the use
684 # case is different and it may diverge in the future.
685 export TARGET_BUILD_APPS=$apps
686
687 build_build_var_cache
688 set_stuff_for_environment
689 printconfig
690 destroy_build_var_cache
691}
692
Matt Alexanderd9c56562020-05-21 10:49:17 +0000693function croot()
694{
Christopher Ferris55257d22017-03-23 11:08:58 -0700695 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700696 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700697 if [ "$1" ]; then
698 \cd $(gettop)/$1
699 else
700 \cd $(gettop)
701 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700702 else
703 echo "Couldn't locate the top of the tree. Try setting TOP."
704 fi
705}
706
Matt Alexanderd9c56562020-05-21 10:49:17 +0000707function _croot()
708{
Anton Hanssonece9c482019-02-04 18:15:39 +0000709 local T=$(gettop)
710 if [ "$T" ]; then
711 local cur="${COMP_WORDS[COMP_CWORD]}"
712 k=0
713 for c in $(compgen -d ${T}/${cur}); do
714 COMPREPLY[k++]=${c#${T}/}/
715 done
716 fi
717}
718
Matt Alexanderd9c56562020-05-21 10:49:17 +0000719function cproj()
720{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700721 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700722 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700723 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700724 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
725 T=$PWD
726 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800727 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700728 return
729 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800730 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700731 done
Ying Wang9cd17642012-12-13 10:52:07 -0800732 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700733 echo "can't find Android.mk"
734}
735
Elliott Hughes86e99172024-03-21 16:55:08 +0000736# Ensure that we're always using the adb in the tree. This works around the fact
737# that bash caches $PATH lookups, so if you use adb before lunching/building the
738# one in your tree, you'll continue to get /usr/bin/adb or whatever even after
739# you have the one from your current tree on your path. Historically this would
740# cause confusion because glinux had adb in /usr/bin/ by default, though that
741# doesn't appear to be the case on my rodete hosts; it is however still the case
742# that my Mac has /usr/local/bin/adb installed by default and on the default
743# path.
Shaju Mathew21439002023-07-10 00:53:22 +0000744function adb() {
Elliott Hughes57c47b72024-03-22 15:46:59 +0000745 # We need `command which` because zsh has a built-in `which` that's more
746 # like `type`.
747 local ADB=$(command which adb)
Elliott Hughes7e7ff752024-03-20 17:23:40 -0700748 if [ -z "$ADB" ]; then
749 echo "Command adb not found; try lunch (and building) first?"
750 return 1
751 fi
Zhuoyao Zhang60dd9dd2024-04-19 00:07:59 +0000752 run_tool_with_logging "ADB" $ADB "${@}"
Shaju Mathew21439002023-07-10 00:53:22 +0000753}
754
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000755function run_tool_with_logging() {
756 # Run commands in a subshell for us to handle forced terminations with a trap
757 # handler.
758 (
759 local tool_tag="$1"
760 shift
761 local tool_binary="$1"
762 shift
763
Zhuoyao Zhangef1c03f2024-05-07 22:18:37 +0000764 # If the logger is not configured, run the original command and return.
765 if [[ -z "${ANDROID_TOOL_LOGGER}" ]]; then
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000766 "${tool_binary}" "${@}"
767 return $?
768 fi
769
770 # Otherwise, run the original command and call the logger when done.
771 local start_time
772 start_time=$(date +%s.%N)
773 local logger=${ANDROID_TOOL_LOGGER}
774
775 # Install a trap to call the logger even when the process terminates abnormally.
776 # The logger is run in the background and its output suppressed to avoid
777 # interference with the user flow.
778 trap '
779 exit_code=$?;
780 # Remove the trap to prevent duplicate log.
781 trap - EXIT;
782 "${logger}" \
Zhuoyao Zhangdfdf19f2024-05-20 18:31:12 +0000783 --tool_tag="${tool_tag}" \
784 --start_timestamp="${start_time}" \
785 --end_timestamp="$(date +%s.%N)" \
786 --tool_args="$*" \
787 --exit_code="${exit_code}" \
Zhuoyao Zhangdb666bc2024-04-30 00:28:34 +0000788 ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \
Zhuoyao Zhangcc44d2e2024-03-26 22:50:12 +0000789 > /dev/null 2>&1 &
790 exit ${exit_code}
791 ' SIGINT SIGTERM SIGQUIT EXIT
792
793 # Run the original command.
794 "${tool_binary}" "${@}"
795 )
796}
797
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800798# communicate with a running device or emulator, set up necessary state,
799# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000800function runhat()
801{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800802 # process standard adb options
803 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000804 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800805 adbTarget=$1
806 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +0000807 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800808 adbTarget="$1 $2"
809 shift 2
810 fi
811 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +0000812 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800813
814 # runhat options
815 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700816
Matt Alexanderd9c56562020-05-21 10:49:17 +0000817 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -0700818 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700819 return
820 fi
821
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800822 # confirm hat is available
823 if [ -z $(which hat) ]; then
824 echo "hat is not available in this configuration."
825 return
826 fi
827
Andy McFaddenb6289852010-07-12 08:00:19 -0700828 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -0700829 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700830 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -0700831 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -0700832 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -0800833 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700834 echo -n "> "
835 read
836
The Android Open Source Project88b60792009-03-03 19:28:42 -0800837 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700838
The Android Open Source Project88b60792009-03-03 19:28:42 -0800839 echo "Retrieving file $devFile..."
840 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700841
The Android Open Source Project88b60792009-03-03 19:28:42 -0800842 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700843
The Android Open Source Project88b60792009-03-03 19:28:42 -0800844 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700845 echo "View the output by pointing your browser at http://localhost:7000/"
846 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -0800847 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700848}
849
Matt Alexanderd9c56562020-05-21 10:49:17 +0000850function getbugreports()
851{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800852 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700853
854 if [ ! "$reports" ]; then
855 echo "Could not locate any bugreports."
856 return
857 fi
858
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800859 local report
860 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700861 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800862 echo "/sdcard/bugreports/${report}"
863 adb pull /sdcard/bugreports/${report} ${report}
864 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700865 done
866}
867
Matt Alexanderd9c56562020-05-21 10:49:17 +0000868function smoketest()
869{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700870 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
871 echo "Couldn't locate output files. Try running 'lunch' first." >&2
872 return
873 fi
Christopher Ferris55257d22017-03-23 11:08:58 -0700874 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700875 if [ ! "$T" ]; then
876 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
877 return
878 fi
879
Ying Wang9cd17642012-12-13 10:52:07 -0800880 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700881 adb uninstall com.android.smoketest > /dev/null &&
882 adb uninstall com.android.smoketest.tests > /dev/null &&
883 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
884 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
885 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
886}
887
The Android Open Source Project88b60792009-03-03 19:28:42 -0800888function godir () {
889 if [[ -z "$1" ]]; then
890 echo "Usage: godir <regex>"
891 return
892 fi
Christopher Ferris55257d22017-03-23 11:08:58 -0700893 local T=$(gettop)
894 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +0000895 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -0700896 mkdir -p $OUT_DIR
897 FILELIST=$OUT_DIR/filelist
898 else
899 FILELIST=$T/filelist
900 fi
901 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -0800902 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -0700903 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -0800904 echo " Done"
905 echo ""
906 fi
907 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -0700908 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000909 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -0800910 echo "Not found"
911 return
912 fi
913 local pathname
914 local choice
915 if [[ ${#lines[@]} > 1 ]]; then
916 while [[ -z "$pathname" ]]; do
917 local index=1
918 local line
919 for line in ${lines[@]}; do
920 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -0700921 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -0800922 done
923 echo
924 echo -n "Select one: "
925 unset choice
926 read choice
927 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
928 echo "Invalid choice"
929 continue
930 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200931 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -0800932 done
933 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200934 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -0800935 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800936 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -0800937}
938
Rett Berg78d1c932019-01-24 14:34:23 -0800939# Go to a specific module in the android tree, as cached in module-info.json. If any build change
940# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Joe Onorato6b543832024-05-23 12:26:50 -0700941# Note: This function is in envsetup because changing the directory needs to happen in the current
942# shell. All other functions that use module-info.json should be in build/soong/bin.
Rett Berg78d1c932019-01-24 14:34:23 -0800943function gomod() {
944 if [[ $# -ne 1 ]]; then
945 echo "usage: gomod <module>" >&2
946 return 1
947 fi
948
949 local path="$(pathmod $@)"
950 if [ -z "$path" ]; then
951 return 1
952 fi
953 cd $path
954}
955
dimitry73b84812018-12-11 18:06:00 +0100956function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -0800957 local word=${COMP_WORDS[COMP_CWORD]}
Cole Faust5d825b72022-10-26 18:16:44 -0700958 COMPREPLY=( $(allmod | grep -E "^$word") )
Steven Moreland62054a42018-12-06 10:11:40 -0800959}
960
Matt Alexanderd9c56562020-05-21 10:49:17 +0000961function get_make_command()
962{
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700963 # If we're in the top of an Android tree, use soong_ui.bash instead of make
964 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -0700965 # Always use the real make if -C is passed in
966 for arg in "$@"; do
967 if [[ $arg == -C* ]]; then
968 echo command make
969 return
970 fi
971 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700972 echo build/soong/soong_ui.bash --make-mode
973 else
974 echo command make
975 fi
Ying Wanged21d4c2014-08-24 22:14:19 -0700976}
977
Matt Alexanderd9c56562020-05-21 10:49:17 +0000978function make()
979{
Dan Willemsene9842242017-07-28 13:00:13 -0700980 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700981}
982
Jim Tanga881a252018-06-19 16:34:41 +0800983# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +0200984function enable_zsh_completion() {
985 # Don't override user's options if bash-style completion is already enabled.
986 if ! declare -f complete >/dev/null; then
987 autoload -U compinit && compinit
988 autoload -U bashcompinit && bashcompinit
989 fi
Jim Tanga881a252018-06-19 16:34:41 +0800990}
991
992function validate_current_shell() {
993 local current_sh="$(ps -o command -p $$)"
994 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -0700995 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +0800996 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -0700997 ;;
Jim Tanga881a252018-06-19 16:34:41 +0800998 *zsh*)
999 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001000 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001001 *)
Jim Tanga881a252018-06-19 16:34:41 +08001002 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 -07001003 ;;
1004 esac
Jim Tanga881a252018-06-19 16:34:41 +08001005}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001006
1007# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001008# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1009# load those.
1010#
1011# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001012function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001013 unset VENDOR_PYTHONPATH
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001014 local T="$(gettop)"
Dan Willemsend855a722019-02-12 15:52:36 -08001015 allowed=
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001016 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 -08001017 if [ -n "$allowed" ]; then
1018 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1019 echo " $allowed"
1020 echo " $f"
1021 return
1022 fi
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001023 allowed="$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001024 done
1025
1026 allowed_files=
1027 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001028 for dir in device vendor product; do
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001029 for f in $(cd "$T" && test -d $dir && \
Jim Tanga881a252018-06-19 16:34:41 +08001030 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001031
1032 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
Patrice Arrudaaa4b8242020-10-12 21:29:14 +00001033 echo "including $f"; . "$T/$f"
Dan Willemsend855a722019-02-12 15:52:36 -08001034 else
1035 echo "ignoring $f, not in $allowed"
1036 fi
Jim Tanga881a252018-06-19 16:34:41 +08001037 done
1038 done
Kousik Kumarec5416c2023-09-14 17:11:45 +00001039
1040 if [[ "${PWD}" == /google/cog/* ]]; then
1041 f="build/make/cogsetup.sh"
1042 echo "including $f"; . "$T/$f"
1043 fi
Jim Tanga881a252018-06-19 16:34:41 +08001044}
Kenny Root52aa81c2011-07-15 11:07:06 -07001045
Dan Albertbab814f2020-08-26 15:34:53 -07001046function showcommands() {
1047 local T=$(gettop)
1048 if [[ -z "$TARGET_PRODUCT" ]]; then
1049 >&2 echo "TARGET_PRODUCT not set. Run lunch."
1050 return
1051 fi
1052 case $(uname -s) in
1053 Darwin)
1054 PREBUILT_NAME=darwin-x86
1055 ;;
1056 Linux)
1057 PREBUILT_NAME=linux-x86
1058 ;;
1059 *)
1060 >&2 echo Unknown host $(uname -s)
1061 return
1062 ;;
1063 esac
Jingwen Chenacdcaa02022-10-13 07:24:24 +00001064 OUT_DIR="$(get_abs_build_var OUT_DIR)"
Dan Albertbab814f2020-08-26 15:34:53 -07001065 if [[ "$1" == "--regenerate" ]]; then
1066 shift 1
1067 NINJA_ARGS="-t commands $@" m
1068 else
1069 (cd $T && prebuilts/build-tools/$PREBUILT_NAME/bin/ninja \
1070 -f $OUT_DIR/combined-${TARGET_PRODUCT}.ninja \
1071 -t commands "$@")
1072 fi
1073}
1074
Joe Onorato1b9ab292024-05-17 12:16:43 -07001075# These functions used to be here but are now standalone scripts
1076# in build/soong/bin. Unset these for the time being so the real
1077# script is picked up.
Joe Onorato23124752024-05-14 15:06:48 -07001078# TODO: Remove this some time after a suitable delay (maybe 2025?)
Joe Onorato6b543832024-05-23 12:26:50 -07001079unset allmod
Joe Onorato23124752024-05-14 15:06:48 -07001080unset aninja
Joe Onorato1b9ab292024-05-17 12:16:43 -07001081unset cgrep
Joe Onorato3acb3082024-05-24 14:14:46 -07001082unset core
1083unset coredump_enable
1084unset coredump_setup
Joe Onorato6b543832024-05-23 12:26:50 -07001085unset dirmods
Joe Onorato3acb3082024-05-24 14:14:46 -07001086unset getlastscreenshot
1087unset getprebuilt
1088unset getscreenshotpath
1089unset getsdcardpath
1090unset gettargetarch
Joe Onorato1b9ab292024-05-17 12:16:43 -07001091unset ggrep
1092unset gogrep
Joe Onorato3acb3082024-05-24 14:14:46 -07001093unset hmm
Joe Onorato6b543832024-05-23 12:26:50 -07001094unset installmod
Joe Onorato3acb3082024-05-24 14:14:46 -07001095unset is64bit
1096unset isviewserverstarted
Joe Onorato1b9ab292024-05-17 12:16:43 -07001097unset jgrep
1098unset jsongrep
Joe Onorato3acb3082024-05-24 14:14:46 -07001099unset key_back
1100unset key_home
1101unset key_menu
Joe Onorato1b9ab292024-05-17 12:16:43 -07001102unset ktgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001103unset m
Joe Onorato1b9ab292024-05-17 12:16:43 -07001104unset mangrep
1105unset mgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001106unset mm
1107unset mma
1108unset mmm
1109unset mmma
1110unset outmod
1111unset overrideflags
Joe Onorato1b9ab292024-05-17 12:16:43 -07001112unset owngrep
Joe Onorato6b543832024-05-23 12:26:50 -07001113unset pathmod
Joe Onoratoff277c52024-05-23 12:52:07 -07001114unset pez
Joe Onorato1b9ab292024-05-17 12:16:43 -07001115unset pygrep
Joe Onorato6b543832024-05-23 12:26:50 -07001116unset qpid
Joe Onorato1b9ab292024-05-17 12:16:43 -07001117unset rcgrep
Joe Onorato6b543832024-05-23 12:26:50 -07001118unset refreshmod
Joe Onorato1b9ab292024-05-17 12:16:43 -07001119unset resgrep
1120unset rsgrep
1121unset sepgrep
1122unset sgrep
Joe Onorato3acb3082024-05-24 14:14:46 -07001123unset startviewserver
1124unset stopviewserver
1125unset systemstack
Joe Onorato6b543832024-05-23 12:26:50 -07001126unset syswrite
Joe Onorato1b9ab292024-05-17 12:16:43 -07001127unset tomlgrep
1128unset treegrep
Cole Faust45844ab2022-08-30 13:59:07 -07001129
Colin Cross71319722023-10-24 10:58:46 -07001130
Jim Tanga881a252018-06-19 16:34:41 +08001131validate_current_shell
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001132set_global_paths
Jim Tanga881a252018-06-19 16:34:41 +08001133source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001134addcompletions
Joe Onorato7c3a77f2022-12-05 13:05:14 -08001135
Joe Onorato23124752024-05-14 15:06:48 -07001136