blob: 0392e86b539ee284e1e47635bc5524817289b2ef [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.
11- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user]
Anton Hanssonece9c482019-02-04 18:15:39 +000012- croot: Changes directory to the top of the tree, or a subdirectory thereof.
Steven Moreland62054a42018-12-06 10:11:40 -080013- m: Makes from the top of the tree.
14- mm: Builds all of the modules in the current directory, but not their dependencies.
15- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
16 To limit the modules being built use the syntax: mmm dir/:target1,target2.
17- mma: Builds all of the modules in the current directory, and their dependencies.
18- mmma: Builds all of the modules in the supplied directories, and their dependencies.
19- provision: Flash device with all required partitions. Options will be passed on to fastboot.
20- cgrep: Greps on all local C/C++ files.
21- ggrep: Greps on all local Gradle files.
22- jgrep: Greps on all local Java files.
23- resgrep: Greps on all local res/*.xml files.
24- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070025- mgrep: Greps on all local Makefiles and *.bp files.
Steven Moreland62054a42018-12-06 10:11:40 -080026- sepgrep: Greps on all local sepolicy files.
27- sgrep: Greps on all local source files.
28- godir: Go to the directory containing a file.
29- allmod: List all modules.
30- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080031- pathmod: Get the directory containing a module.
Steven Moreland62054a42018-12-06 10:11:40 -080032- refreshmod: Refresh list of modules for allmod/gomod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070033
Roland Levillain39341922015-10-20 12:48:19 +010034Environment options:
Dan Albert4ae5d4b2014-10-31 16:23:08 -070035- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
36 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
37 build is leak-check clean.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080038- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070039
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070040Look at the source to view more functions. The complete list is:
41EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070042 local T=$(gettop)
43 local A=""
44 local i
Jacky Cao89483b82015-05-15 22:12:53 +080045 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 -070046 A="$A $i"
47 done
48 echo $A
49}
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.
52function 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=$?
65 if [ $ret -ne 0 ]
66 then
67 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
Ying Wang08800fd2016-03-03 20:57:21 -080074 if [ $ret -ne 0 ]
75 then
76 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.
83function destroy_build_var_cache()
84{
85 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.
98function get_abs_build_var()
99{
Ying Wang08800fd2016-03-03 20:57:21 -0800100 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
101 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800102 eval "echo \"\${abs_var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800103 return
104 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.
115function get_build_var()
116{
Ying Wang08800fd2016-03-03 20:57:21 -0800117 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
118 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800119 eval "echo \"\${var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800120 return
121 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
126 return
127 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
131# check to see if the supplied product is one we can build
132function check_product()
133{
Christopher Ferris55257d22017-03-23 11:08:58 -0700134 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800135 if [ ! "$T" ]; then
136 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
137 return
138 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700139 TARGET_PRODUCT=$1 \
140 TARGET_BUILD_VARIANT= \
141 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700142 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800143 get_build_var TARGET_DEVICE > /dev/null
144 # hide successful answers, but allow the errors to show
145}
146
147VARIANT_CHOICES=(user userdebug eng)
148
149# check to see if the supplied variant is valid
150function check_variant()
151{
Christopher Ferris55257d22017-03-23 11:08:58 -0700152 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800153 for v in ${VARIANT_CHOICES[@]}
154 do
155 if [ "$v" = "$1" ]
156 then
157 return 0
158 fi
159 done
160 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700161}
162
163function setpaths()
164{
Christopher Ferris55257d22017-03-23 11:08:58 -0700165 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700166 if [ ! "$T" ]; then
167 echo "Couldn't locate the top of the tree. Try setting TOP."
168 return
169 fi
170
171 ##################################################################
172 # #
173 # Read me before you modify this code #
174 # #
175 # This function sets ANDROID_BUILD_PATHS to what it is adding #
176 # to PATH, and the next time it is run, it removes that from #
177 # PATH. This is required so lunch can be run more than once #
178 # and still have working paths. #
179 # #
180 ##################################################################
181
Raphael Mollc639c782011-06-20 17:25:01 -0700182 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
183 # due to "C:\Program Files" being in the path.
184
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700185 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700186 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700187 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
188 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700189 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700190 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800191 # strip leading ':', if any
192 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500193 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700194
195 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700196 local prebuiltdir=$(getprebuilt)
197 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700198
Ben Cheng8bc4c432012-11-16 13:29:13 -0800199 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700200 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
201 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800202 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800203
Raphael Mollc639c782011-06-20 17:25:01 -0700204 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800205 export ANDROID_TOOLCHAIN=
206 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200207 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700208 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200209 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400210 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700211 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400212 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
213 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800214 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200215 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800216 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700217 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700218 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700219 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000220 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200221 *)
222 echo "Can't find toolchain for unknown architecture: $ARCH"
223 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700224 ;;
225 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700226 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800227 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700228 fi
Raphael732936d2011-06-22 14:35:32 -0700229
Christopher Ferris55257d22017-03-23 11:08:58 -0700230 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800231 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
232 fi
233
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700234 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700235
236 # add kernel specific binaries
237 case $(uname -s) in
238 Linux)
239 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
240 ;;
241 *)
242 ;;
243 esac
244
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400245 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
246 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
247 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
248 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700249 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
250
251 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
252 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200254
255 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
256 # to ensure that the corresponding 'emulator' binaries are used.
257 case $(uname -s) in
258 Darwin)
259 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
260 ;;
261 Linux)
262 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
263 ;;
264 *)
265 ANDROID_EMULATOR_PREBUILTS=
266 ;;
267 esac
268 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700269 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200270 export ANDROID_EMULATOR_PREBUILTS
271 fi
272
Jim Tangb3fda302018-12-22 10:24:55 +0800273 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
274 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700275 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
276 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
277 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
278 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800279
Ryan Prichard8426a192019-07-15 18:05:29 -0700280 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800281
282 # out with the duplicate old
283 if [ -n $ANDROID_PYTHONPATH ]; then
284 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
285 fi
286 # and in with the new
287 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800288 if [ -n $VENDOR_PYTHONPATH ]; then
289 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
290 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800291 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800292
Colin Crosse97e6932017-06-30 16:01:45 -0700293 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
294 export JAVA_HOME=$ANDROID_JAVA_HOME
295 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
296 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
297 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500298
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800299 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700300 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
301 export OUT=$ANDROID_PRODUCT_OUT
302
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700303 unset ANDROID_HOST_OUT
304 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
305
Simran Basidd050ed2017-02-13 13:46:48 -0800306 unset ANDROID_HOST_OUT_TESTCASES
307 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
308
309 unset ANDROID_TARGET_OUT_TESTCASES
310 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
311
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800312 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700313 # TODO: fix the path
314 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
315}
316
317function printconfig()
318{
Christopher Ferris55257d22017-03-23 11:08:58 -0700319 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800320 if [ ! "$T" ]; then
321 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
322 return
323 fi
324 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700325}
326
327function set_stuff_for_environment()
328{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 setpaths
330 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700331
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800332 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700333 # With this environment variable new GCC can apply colors to warnings/errors
334 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700335 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700336}
337
338function set_sequence_number()
339{
Colin Cross88737132017-03-21 17:41:03 -0700340 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700341}
342
Makoto Onukida971062018-06-18 10:15:19 -0700343# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
344function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800345 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700346 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800347 *:"$cmd":*)
348 return 1
349 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700350 esac
351 return 0
352}
353
Kenny Root52aa81c2011-07-15 11:07:06 -0700354function addcompletions()
355{
356 local T dir f
357
Jim Tanga881a252018-06-19 16:34:41 +0800358 # Keep us from trying to run in something that's neither bash nor zsh.
359 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700360 return
361 fi
362
363 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800364 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700365 return
366 fi
367
Jim Tanga881a252018-06-19 16:34:41 +0800368 local completion_files=(
369 system/core/adb/adb.bash
370 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800371 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800372 )
Makoto Onukida971062018-06-18 10:15:19 -0700373 # Completion can be disabled selectively to allow users to use non-standard completion.
374 # e.g.
375 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
376 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800377 for f in ${completion_files[*]}; do
378 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700379 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700380 fi
381 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700382
Makoto Onukida971062018-06-18 10:15:19 -0700383 if should_add_completion bit ; then
384 complete -C "bit --tab" bit
385 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000386 if [ -z "$ZSH_VERSION" ]; then
387 # Doesn't work in zsh.
388 complete -o nospace -F _croot croot
389 fi
Jim Tanga881a252018-06-19 16:34:41 +0800390 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800391
dimitry73b84812018-12-11 18:06:00 +0100392 complete -F _complete_android_module_names gomod
393 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700394}
395
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700396function choosetype()
397{
398 echo "Build type choices are:"
399 echo " 1. release"
400 echo " 2. debug"
401 echo
402
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800403 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700404 DEFAULT_NUM=1
405 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700406
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800407 export TARGET_BUILD_TYPE=
408 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700409 while [ -z $TARGET_BUILD_TYPE ]
410 do
411 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800412 if [ -z "$1" ] ; then
413 read ANSWER
414 else
415 echo $1
416 ANSWER=$1
417 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700418 case $ANSWER in
419 "")
420 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
421 ;;
422 1)
423 export TARGET_BUILD_TYPE=release
424 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800425 release)
426 export TARGET_BUILD_TYPE=release
427 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428 2)
429 export TARGET_BUILD_TYPE=debug
430 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800431 debug)
432 export TARGET_BUILD_TYPE=debug
433 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700434 *)
435 echo
436 echo "I didn't understand your response. Please try again."
437 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438 ;;
439 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800440 if [ -n "$1" ] ; then
441 break
442 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700443 done
444
Ying Wang08800fd2016-03-03 20:57:21 -0800445 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700446 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800447 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700448}
449
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800450#
451# This function isn't really right: It chooses a TARGET_PRODUCT
452# based on the list of boards. Usually, that gets you something
453# that kinda works with a generic product, but really, you should
454# pick a product by name.
455#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456function chooseproduct()
457{
Christopher Ferris55257d22017-03-23 11:08:58 -0700458 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700459 if [ "x$TARGET_PRODUCT" != x ] ; then
460 default_value=$TARGET_PRODUCT
461 else
Ying Wang0a76df52015-06-08 11:57:26 -0700462 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700463 fi
464
Ying Wang08800fd2016-03-03 20:57:21 -0800465 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800466 export TARGET_PRODUCT=
467 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700468 while [ -z "$TARGET_PRODUCT" ]
469 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700470 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800471 if [ -z "$1" ] ; then
472 read ANSWER
473 else
474 echo $1
475 ANSWER=$1
476 fi
477
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478 if [ -z "$ANSWER" ] ; then
479 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480 else
481 if check_product $ANSWER
482 then
483 export TARGET_PRODUCT=$ANSWER
484 else
485 echo "** Not a valid product: $ANSWER"
486 fi
487 fi
488 if [ -n "$1" ] ; then
489 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490 fi
491 done
492
Ying Wang08800fd2016-03-03 20:57:21 -0800493 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700494 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800495 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700496}
497
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800498function choosevariant()
499{
500 echo "Variant choices are:"
501 local index=1
502 local v
503 for v in ${VARIANT_CHOICES[@]}
504 do
505 # The product name is the name of the directory containing
506 # the makefile we found, above.
507 echo " $index. $v"
508 index=$(($index+1))
509 done
510
511 local default_value=eng
512 local ANSWER
513
514 export TARGET_BUILD_VARIANT=
515 while [ -z "$TARGET_BUILD_VARIANT" ]
516 do
517 echo -n "Which would you like? [$default_value] "
518 if [ -z "$1" ] ; then
519 read ANSWER
520 else
521 echo $1
522 ANSWER=$1
523 fi
524
525 if [ -z "$ANSWER" ] ; then
526 export TARGET_BUILD_VARIANT=$default_value
527 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
528 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800529 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800530 fi
531 else
532 if check_variant $ANSWER
533 then
534 export TARGET_BUILD_VARIANT=$ANSWER
535 else
536 echo "** Not a valid variant: $ANSWER"
537 fi
538 fi
539 if [ -n "$1" ] ; then
540 break
541 fi
542 done
543}
544
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700545function choosecombo()
546{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700547 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700548
549 echo
550 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700551 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700552
553 echo
554 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700555 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800556
557 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800558 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700559 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800560 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800561 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700562}
563
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800564function add_lunch_combo()
565{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800566 if [ -n "$ZSH_VERSION" ]; then
567 echo -n "${funcfiletrace[1]}: "
568 else
569 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
570 fi
571 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 -0800572}
573
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574function print_lunch_menu()
575{
576 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 echo
578 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700579 echo
580 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800581
582 local i=1
583 local choice
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800584 for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 do
586 echo " $i. $choice"
587 i=$(($i+1))
588 done
589
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 echo
591}
592
593function lunch()
594{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 local answer
596
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700597 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800598 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700599 else
600 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700601 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700603 fi
604
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800605 local selection=
606
607 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700609 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800610 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
611 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800612 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700613 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800614 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800615 # array in zsh starts from 1 instead of 0.
616 if [ -n "$ZSH_VERSION" ]
617 then
618 selection=${choices[$(($answer))]}
619 else
620 selection=${choices[$(($answer-1))]}
621 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800622 fi
Colin Cross88737132017-03-21 17:41:03 -0700623 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800624 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700625 fi
626
Joe Onoratoda12daf2010-06-09 18:18:31 -0700627 export TARGET_BUILD_APPS=
628
Colin Cross88737132017-03-21 17:41:03 -0700629 local product variant_and_version variant version
630
631 product=${selection%%-*} # Trim everything after first dash
632 variant_and_version=${selection#*-} # Trim everything up to first dash
633 if [ "$variant_and_version" != "$selection" ]; then
634 variant=${variant_and_version%%-*}
635 if [ "$variant" != "$variant_and_version" ]; then
636 version=${variant_and_version#*-}
637 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700638 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800639
Colin Cross88737132017-03-21 17:41:03 -0700640 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800641 then
642 echo
Colin Cross88737132017-03-21 17:41:03 -0700643 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700644 return 1
645 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800646
Colin Cross88737132017-03-21 17:41:03 -0700647 TARGET_PRODUCT=$product \
648 TARGET_BUILD_VARIANT=$variant \
649 TARGET_PLATFORM_VERSION=$version \
650 build_build_var_cache
651 if [ $? -ne 0 ]
652 then
653 return 1
654 fi
655
656 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
657 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700658 if [ -n "$version" ]; then
659 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
660 else
661 unset TARGET_PLATFORM_VERSION
662 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700663 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664
665 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800666
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800668 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800669 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700670}
671
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700672unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700673# Tab completion for lunch.
674function _lunch()
675{
676 local cur prev opts
677 COMPREPLY=()
678 cur="${COMP_WORDS[COMP_CWORD]}"
679 prev="${COMP_WORDS[COMP_CWORD-1]}"
680
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700681 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800682 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700683 fi
684
685 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700686 return 0
687}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700688
Joe Onoratoda12daf2010-06-09 18:18:31 -0700689# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700690# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700691function tapas()
692{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700693 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800694 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700695 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700696 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800697 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700698
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700699 if [ "$showHelp" != "" ]; then
700 $(gettop)/build/make/tapasHelp.sh
701 return
702 fi
703
Ying Wang67f02922012-08-22 10:25:20 -0700704 if [ $(echo $arch | wc -w) -gt 1 ]; then
705 echo "tapas: Error: Multiple build archs supplied: $arch"
706 return
707 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700708 if [ $(echo $variant | wc -w) -gt 1 ]; then
709 echo "tapas: Error: Multiple build variants supplied: $variant"
710 return
711 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700712 if [ $(echo $density | wc -w) -gt 1 ]; then
713 echo "tapas: Error: Multiple densities supplied: $density"
714 return
715 fi
Ying Wang67f02922012-08-22 10:25:20 -0700716
Ying Wang0a76df52015-06-08 11:57:26 -0700717 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700718 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700719 x86) product=aosp_x86;;
720 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700721 arm64) product=aosp_arm64;;
722 x86_64) product=aosp_x86_64;;
723 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700724 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700725 if [ -z "$variant" ]; then
726 variant=eng
727 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700728 if [ -z "$apps" ]; then
729 apps=all
730 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600731 if [ -z "$density" ]; then
732 density=alldpi
733 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700734
Ying Wang67f02922012-08-22 10:25:20 -0700735 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700737 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700738 export TARGET_BUILD_TYPE=release
739 export TARGET_BUILD_APPS=$apps
740
Ying Wang08800fd2016-03-03 20:57:21 -0800741 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700742 set_stuff_for_environment
743 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800744 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700745}
746
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700747function gettop
748{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700749 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700750 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700751 # The following circumlocution ensures we remove symlinks from TOP.
752 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700753 else
754 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800755 # The following circumlocution (repeated below as well) ensures
756 # that we record the true directory name and not one that is
757 # faked up with symlink names.
758 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800760 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700761 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700762 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800763 \cd ..
synergyb112a402013-07-05 19:47:47 -0700764 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700765 done
Ying Wang9cd17642012-12-13 10:52:07 -0800766 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700767 if [ -f "$T/$TOPFILE" ]; then
768 echo $T
769 fi
770 fi
771 fi
772}
773
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774function croot()
775{
Christopher Ferris55257d22017-03-23 11:08:58 -0700776 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700777 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700778 if [ "$1" ]; then
779 \cd $(gettop)/$1
780 else
781 \cd $(gettop)
782 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 else
784 echo "Couldn't locate the top of the tree. Try setting TOP."
785 fi
786}
787
Anton Hanssonece9c482019-02-04 18:15:39 +0000788function _croot()
789{
790 local T=$(gettop)
791 if [ "$T" ]; then
792 local cur="${COMP_WORDS[COMP_CWORD]}"
793 k=0
794 for c in $(compgen -d ${T}/${cur}); do
795 COMPREPLY[k++]=${c#${T}/}/
796 done
797 fi
798}
799
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700800function cproj()
801{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700802 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700803 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700804 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700805 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
806 T=$PWD
807 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800808 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700809 return
810 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800811 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700812 done
Ying Wang9cd17642012-12-13 10:52:07 -0800813 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700814 echo "can't find Android.mk"
815}
816
Daniel Sandler47e0a882013-07-30 13:23:52 -0400817# simplified version of ps; output in the form
818# <pid> <procname>
819function qpid() {
820 local prepend=''
821 local append=''
822 if [ "$1" = "--exact" ]; then
823 prepend=' '
824 append='$'
825 shift
826 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800827 echo "usage: qpid [[--exact] <process name|pid>"
828 return 255
829 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400830
831 local EXE="$1"
832 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800833 qpid | \grep "$prepend$EXE$append"
834 else
835 adb shell ps \
836 | tr -d '\r' \
837 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
838 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400839}
840
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800841# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700842# that has the core-file-size limit set correctly
843#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800844# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700845# if its core-file-size limit is not set already.
846# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
847
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800848function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700849{
Ying Wang08800fd2016-03-03 20:57:21 -0800850 echo "Getting root...";
851 adb root;
852 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700853
Ying Wang08800fd2016-03-03 20:57:21 -0800854 echo "Remounting root partition read-write...";
855 adb shell mount -w -o remount -t rootfs rootfs;
856 sleep 1;
857 adb wait-for-device;
858 adb shell mkdir -p /cores;
859 adb shell mount -t tmpfs tmpfs /cores;
860 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700861
Ying Wang08800fd2016-03-03 20:57:21 -0800862 echo "Granting SELinux permission to dump in /cores...";
863 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700864
Ying Wang08800fd2016-03-03 20:57:21 -0800865 echo "Set core pattern.";
866 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700867
Ying Wang08800fd2016-03-03 20:57:21 -0800868 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700869}
870
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800871# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700872# $1 = PID of process (e.g., $(pid mediaserver))
873#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800874# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700875# dump to actually be generated.
876
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800877function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700878{
Ying Wang08800fd2016-03-03 20:57:21 -0800879 local PID=$1;
880 if [ -z "$PID" ]; then
881 printf "Expecting a PID!\n";
882 return;
883 fi;
884 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800885 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700886}
887
888# core - send SIGV and pull the core for process
889# $1 = PID of process (e.g., $(pid mediaserver))
890#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800891# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700892# enabled globally.
893
894function core()
895{
Ying Wang08800fd2016-03-03 20:57:21 -0800896 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700897
Ying Wang08800fd2016-03-03 20:57:21 -0800898 if [ -z "$PID" ]; then
899 printf "Expecting a PID!\n";
900 return;
901 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700902
Ying Wang08800fd2016-03-03 20:57:21 -0800903 local CORENAME=core.$PID;
904 local COREPATH=/cores/$CORENAME;
905 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700906
Ying Wang08800fd2016-03-03 20:57:21 -0800907 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700908
Ying Wang08800fd2016-03-03 20:57:21 -0800909 local done=0;
910 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
911 printf "\tSending SIG%s to %d...\n" $SIG $PID;
912 adb shell kill -$SIG $PID;
913 sleep 1;
914 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700915
Ying Wang08800fd2016-03-03 20:57:21 -0800916 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
917 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700918}
919
Christopher Tate744ee802009-11-12 15:33:08 -0800920# systemstack - dump the current stack trace of all threads in the system process
921# to the usual ANR traces file
922function systemstack()
923{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800924 stacks system_server
925}
926
Michael Wrightaeed7212014-06-19 19:58:12 -0700927# Read the ELF header from /proc/$PID/exe to determine if the process is
928# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -0800929function is64bit()
930{
931 local PID="$1"
932 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -0800933 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800934 echo "64"
935 else
936 echo ""
937 fi
938 else
939 echo ""
940 fi
941}
942
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700943case `uname -s` in
944 Darwin)
945 function sgrep()
946 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100947 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cc|cpp|hpp|S|java|xml|sh|mk|aidl|vts)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400948 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700949 }
950
951 ;;
952 *)
953 function sgrep()
954 {
Aurelio Jargas67edd152018-11-06 17:25:11 +0100955 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400956 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700957 }
958 ;;
959esac
960
Raghu Gandham8da43102012-07-25 19:57:22 -0700961function gettargetarch
962{
963 get_build_var TARGET_ARCH
964}
965
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700966function ggrep()
967{
Mike Frysinger5e479732015-09-22 18:13:48 -0400968 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
969 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700970}
971
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700972function jgrep()
973{
Mike Frysinger5e479732015-09-22 18:13:48 -0400974 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
975 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700976}
977
978function cgrep()
979{
Mike Frysinger5e479732015-09-22 18:13:48 -0400980 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' \) \
981 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700982}
983
984function resgrep()
985{
Christopher Ferris55257d22017-03-23 11:08:58 -0700986 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -0400987 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
988 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
989 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700990}
991
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800992function mangrep()
993{
Mike Frysinger5e479732015-09-22 18:13:48 -0400994 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
995 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -0800996}
997
Alex Klyubinba5fc8e2013-05-06 14:11:48 -0700998function sepgrep()
999{
Mike Frysinger5e479732015-09-22 18:13:48 -04001000 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1001 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001002}
1003
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001004function rcgrep()
1005{
Mike Frysinger5e479732015-09-22 18:13:48 -04001006 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1007 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001008}
1009
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001010case `uname -s` in
1011 Darwin)
1012 function mgrep()
1013 {
David Grossd1d6fc52017-05-10 10:49:44 -07001014 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?soong/[^/]*.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001015 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001016 }
1017
1018 function treegrep()
1019 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001020 find -E . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' \
Mike Frysinger5e479732015-09-22 18:13:48 -04001021 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001022 }
1023
1024 ;;
1025 *)
1026 function mgrep()
1027 {
David Grossd1d6fc52017-05-10 10:49:44 -07001028 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 '(.*/)?soong/[^/]*.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001029 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001030 }
1031
1032 function treegrep()
1033 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001034 find . -name .repo -prune -o -name .git -prune -o -regextype posix-egrep -iregex '.*\.(c|h|cpp|hpp|S|java|xml)' -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001035 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001036 }
1037
1038 ;;
1039esac
1040
1041function getprebuilt
1042{
1043 get_abs_build_var ANDROID_PREBUILTS
1044}
1045
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001046function tracedmdump()
1047{
Christopher Ferris55257d22017-03-23 11:08:58 -07001048 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001049 if [ ! "$T" ]; then
1050 echo "Couldn't locate the top of the tree. Try setting TOP."
1051 return
1052 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001053 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001054 local arch=$(gettargetarch)
1055 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001056
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001057 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001058 if [ ! "$TRACE" ] ; then
1059 echo "usage: tracedmdump tracename"
1060 return
1061 fi
1062
Jack Veenstra60116fc2009-04-09 18:12:34 -07001063 if [ ! -r "$KERNEL" ] ; then
1064 echo "Error: cannot find kernel: '$KERNEL'"
1065 return
1066 fi
1067
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001068 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001069 if [ "$BASETRACE" = "$TRACE" ] ; then
1070 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1071 fi
1072
1073 echo "post-processing traces..."
1074 rm -f $TRACE/qtrace.dexlist
1075 post_trace $TRACE
1076 if [ $? -ne 0 ]; then
1077 echo "***"
1078 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1079 echo "***"
1080 return
1081 fi
1082 echo "generating dexlist output..."
1083 /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
1084 echo "generating dmtrace data..."
1085 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1086 echo "generating html file..."
1087 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1088 echo "done, see $TRACE/dmtrace.html for details"
1089 echo "or run:"
1090 echo " traceview $TRACE/dmtrace"
1091}
1092
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001093# communicate with a running device or emulator, set up necessary state,
1094# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001095function runhat()
1096{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001097 # process standard adb options
1098 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001099 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001100 adbTarget=$1
1101 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001102 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001103 adbTarget="$1 $2"
1104 shift 2
1105 fi
1106 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001107 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001108
1109 # runhat options
1110 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001111
1112 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001113 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001114 return
1115 fi
1116
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001117 # confirm hat is available
1118 if [ -z $(which hat) ]; then
1119 echo "hat is not available in this configuration."
1120 return
1121 fi
1122
Andy McFaddenb6289852010-07-12 08:00:19 -07001123 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001124 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001125 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001126 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001127 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001128 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001129 echo -n "> "
1130 read
1131
The Android Open Source Project88b60792009-03-03 19:28:42 -08001132 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001133
The Android Open Source Project88b60792009-03-03 19:28:42 -08001134 echo "Retrieving file $devFile..."
1135 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001136
The Android Open Source Project88b60792009-03-03 19:28:42 -08001137 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001138
The Android Open Source Project88b60792009-03-03 19:28:42 -08001139 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001140 echo "View the output by pointing your browser at http://localhost:7000/"
1141 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001142 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001143}
1144
1145function getbugreports()
1146{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001147 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001148
1149 if [ ! "$reports" ]; then
1150 echo "Could not locate any bugreports."
1151 return
1152 fi
1153
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001154 local report
1155 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001156 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001157 echo "/sdcard/bugreports/${report}"
1158 adb pull /sdcard/bugreports/${report} ${report}
1159 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001160 done
1161}
1162
Victoria Lease1b296b42012-08-21 15:44:06 -07001163function getsdcardpath()
1164{
1165 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1166}
1167
1168function getscreenshotpath()
1169{
1170 echo "$(getsdcardpath)/Pictures/Screenshots"
1171}
1172
1173function getlastscreenshot()
1174{
1175 local screenshot_path=$(getscreenshotpath)
1176 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1177 if [ "$screenshot" = "" ]; then
1178 echo "No screenshots found."
1179 return
1180 fi
1181 echo "${screenshot}"
1182 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1183}
1184
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001185function startviewserver()
1186{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001187 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001188 if [ $# -gt 0 ]; then
1189 port=$1
1190 fi
1191 adb shell service call window 1 i32 $port
1192}
1193
1194function stopviewserver()
1195{
1196 adb shell service call window 2
1197}
1198
1199function isviewserverstarted()
1200{
1201 adb shell service call window 3
1202}
1203
Romain Guyb84049a2010-10-04 16:56:11 -07001204function key_home()
1205{
1206 adb shell input keyevent 3
1207}
1208
1209function key_back()
1210{
1211 adb shell input keyevent 4
1212}
1213
1214function key_menu()
1215{
1216 adb shell input keyevent 82
1217}
1218
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219function smoketest()
1220{
1221 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1222 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1223 return
1224 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001225 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001226 if [ ! "$T" ]; then
1227 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1228 return
1229 fi
1230
Ying Wang9cd17642012-12-13 10:52:07 -08001231 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001232 adb uninstall com.android.smoketest > /dev/null &&
1233 adb uninstall com.android.smoketest.tests > /dev/null &&
1234 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1235 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1236 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1237}
1238
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001239# simple shortcut to the runtest command
1240function runtest()
1241{
Christopher Ferris55257d22017-03-23 11:08:58 -07001242 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001243 if [ ! "$T" ]; then
1244 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1245 return
1246 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001247 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001248}
1249
The Android Open Source Project88b60792009-03-03 19:28:42 -08001250function godir () {
1251 if [[ -z "$1" ]]; then
1252 echo "Usage: godir <regex>"
1253 return
1254 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001255 local T=$(gettop)
1256 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001257 if [ ! "$OUT_DIR" = "" ]; then
1258 mkdir -p $OUT_DIR
1259 FILELIST=$OUT_DIR/filelist
1260 else
1261 FILELIST=$T/filelist
1262 fi
1263 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001264 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001265 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001266 echo " Done"
1267 echo ""
1268 fi
1269 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001270 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001271 if [[ ${#lines[@]} = 0 ]]; then
1272 echo "Not found"
1273 return
1274 fi
1275 local pathname
1276 local choice
1277 if [[ ${#lines[@]} > 1 ]]; then
1278 while [[ -z "$pathname" ]]; do
1279 local index=1
1280 local line
1281 for line in ${lines[@]}; do
1282 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001283 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001284 done
1285 echo
1286 echo -n "Select one: "
1287 unset choice
1288 read choice
1289 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1290 echo "Invalid choice"
1291 continue
1292 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001293 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001294 done
1295 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001296 pathname=${lines[0]}
1297 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001298 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001299}
1300
Steven Moreland62054a42018-12-06 10:11:40 -08001301# Update module-info.json in out.
1302function refreshmod() {
1303 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1304 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1305 return 1
1306 fi
1307
1308 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1309
1310 # for the output of the next command
1311 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1312
1313 # Note, can't use absolute path because of the way make works.
1314 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1315 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1316}
1317
1318# List all modules for the current device, as cached in module-info.json. If any build change is
1319# made and it should be reflected in the output, you should run 'refreshmod' first.
1320function allmod() {
1321 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1322 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1323 return 1
1324 fi
1325
1326 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1327 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1328 refreshmod || return 1
1329 fi
1330
1331 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1332}
1333
Rett Berg78d1c932019-01-24 14:34:23 -08001334# Get the path of a specific module in the android tree, as cached in module-info.json. If any build change
Steven Moreland62054a42018-12-06 10:11:40 -08001335# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001336function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001337 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1338 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1339 return 1
1340 fi
1341
1342 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001343 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001344 return 1
1345 fi
1346
1347 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1348 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1349 refreshmod || return 1
1350 fi
1351
1352 local relpath=$(python -c "import json, os
1353module = '$1'
1354module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1355if module not in module_info:
1356 exit(1)
1357print module_info[module]['path'][0]" 2>/dev/null)
1358
1359 if [ -z "$relpath" ]; then
1360 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1361 return 1
1362 else
Rett Berg78d1c932019-01-24 14:34:23 -08001363 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001364 fi
1365}
1366
Rett Berg78d1c932019-01-24 14:34:23 -08001367# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1368# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1369function gomod() {
1370 if [[ $# -ne 1 ]]; then
1371 echo "usage: gomod <module>" >&2
1372 return 1
1373 fi
1374
1375 local path="$(pathmod $@)"
1376 if [ -z "$path" ]; then
1377 return 1
1378 fi
1379 cd $path
1380}
1381
dimitry73b84812018-12-11 18:06:00 +01001382function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001383 local word=${COMP_WORDS[COMP_CWORD]}
1384 COMPREPLY=( $(allmod | grep -E "^$word") )
1385}
1386
Alex Rayf0d08eb2013-03-08 15:15:06 -08001387# Print colored exit condition
1388function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001389 "$@"
1390 local retval=$?
1391 if [ $retval -ne 0 ]
1392 then
Jacky Cao89483b82015-05-15 22:12:53 +08001393 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001394 else
Jacky Cao89483b82015-05-15 22:12:53 +08001395 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001396 fi
1397 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001398}
1399
Ying Wanged21d4c2014-08-24 22:14:19 -07001400function get_make_command()
1401{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001402 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1403 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001404 # Always use the real make if -C is passed in
1405 for arg in "$@"; do
1406 if [[ $arg == -C* ]]; then
1407 echo command make
1408 return
1409 fi
1410 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001411 echo build/soong/soong_ui.bash --make-mode
1412 else
1413 echo command make
1414 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001415}
1416
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001417function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001418{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001419 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1420 "$@"
1421 return $?
1422 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001423 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001424 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001425 local ret=$?
1426 local end_time=$(date +"%s")
1427 local tdiff=$(($end_time-$start_time))
1428 local hours=$(($tdiff / 3600 ))
1429 local mins=$((($tdiff % 3600) / 60))
1430 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001431 local ncolors=$(tput colors 2>/dev/null)
1432 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001433 color_failed=$'\E'"[0;31m"
1434 color_success=$'\E'"[0;32m"
1435 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001436 else
1437 color_failed=""
1438 color_success=""
1439 color_reset=""
1440 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001441 echo
1442 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001443 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001444 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001445 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001446 fi
1447 if [ $hours -gt 0 ] ; then
1448 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1449 elif [ $mins -gt 0 ] ; then
1450 printf "(%02g:%02g (mm:ss))" $mins $secs
1451 elif [ $secs -gt 0 ] ; then
1452 printf "(%s seconds)" $secs
1453 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001454 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001455 echo
1456 return $ret
1457}
1458
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001459function _trigger_build()
1460(
1461 local -r bc="$1"; shift
1462 if T="$(gettop)"; then
1463 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1464 else
1465 echo "Couldn't locate the top of the tree. Try setting TOP."
1466 fi
1467)
1468
1469function m()
1470(
1471 _trigger_build "all-modules" "$@"
1472)
1473
1474function mm()
1475(
1476 _trigger_build "modules-in-a-dir-no-deps" "$@"
1477)
1478
1479function mmm()
1480(
1481 _trigger_build "modules-in-dirs-no-deps" "$@"
1482)
1483
1484function mma()
1485(
1486 _trigger_build "modules-in-a-dir" "$@"
1487)
1488
1489function mmma()
1490(
1491 _trigger_build "modules-in-dirs" "$@"
1492)
1493
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001494function make()
1495{
Dan Willemsene9842242017-07-28 13:00:13 -07001496 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001497}
1498
David Zeuthen1b126ff2015-09-30 17:10:48 -04001499function provision()
1500{
1501 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1502 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1503 return 1
1504 fi
1505 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1506 echo "There is no provisioning script for the device." >&2
1507 return 1
1508 fi
1509
1510 # Check if user really wants to do this.
1511 if [ "$1" = "--no-confirmation" ]; then
1512 shift 1
1513 else
1514 echo "This action will reflash your device."
1515 echo ""
1516 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1517 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001518 echo -n "Are you sure you want to do this (yes/no)? "
1519 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001520 if [[ "${REPLY}" != "yes" ]] ; then
1521 echo "Not taking any action. Exiting." >&2
1522 return 1
1523 fi
1524 fi
1525 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1526}
1527
Jim Tanga881a252018-06-19 16:34:41 +08001528# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001529function enable_zsh_completion() {
1530 # Don't override user's options if bash-style completion is already enabled.
1531 if ! declare -f complete >/dev/null; then
1532 autoload -U compinit && compinit
1533 autoload -U bashcompinit && bashcompinit
1534 fi
Jim Tanga881a252018-06-19 16:34:41 +08001535}
1536
1537function validate_current_shell() {
1538 local current_sh="$(ps -o command -p $$)"
1539 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001540 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001541 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001542 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001543 *zsh*)
1544 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001545 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001546 *)
Jim Tanga881a252018-06-19 16:34:41 +08001547 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 -07001548 ;;
1549 esac
Jim Tanga881a252018-06-19 16:34:41 +08001550}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001551
1552# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001553# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1554# load those.
1555#
1556# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001557function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001558 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001559 allowed=
1560 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1561 if [ -n "$allowed" ]; then
1562 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1563 echo " $allowed"
1564 echo " $f"
1565 return
1566 fi
1567 allowed="$f"
1568 done
1569
1570 allowed_files=
1571 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001572 for dir in device vendor product; do
1573 for f in $(test -d $dir && \
1574 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001575
1576 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1577 echo "including $f"; . "$f"
1578 else
1579 echo "ignoring $f, not in $allowed"
1580 fi
Jim Tanga881a252018-06-19 16:34:41 +08001581 done
1582 done
1583}
Kenny Root52aa81c2011-07-15 11:07:06 -07001584
Jim Tanga881a252018-06-19 16:34:41 +08001585validate_current_shell
1586source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001587addcompletions