blob: 2fa566075a05c6c56862599ebbe43a0737c7f43d [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)
275 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:"
Yi Kongdfd00b12019-05-21 16:00:04 -0700278 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH$AIDEGEN_PATH$ATEST_PATH
Jim Tangb3fda302018-12-22 10:24:55 +0800279
Yi Kongdfd00b12019-05-21 16:00:04 -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:
288 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800289
Colin Crosse97e6932017-06-30 16:01:45 -0700290 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
291 export JAVA_HOME=$ANDROID_JAVA_HOME
292 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
293 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
294 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500295
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800296 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700297 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
298 export OUT=$ANDROID_PRODUCT_OUT
299
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700300 unset ANDROID_HOST_OUT
301 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
302
Simran Basidd050ed2017-02-13 13:46:48 -0800303 unset ANDROID_HOST_OUT_TESTCASES
304 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
305
306 unset ANDROID_TARGET_OUT_TESTCASES
307 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
308
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800309 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700310 # TODO: fix the path
311 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
312}
313
314function printconfig()
315{
Christopher Ferris55257d22017-03-23 11:08:58 -0700316 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800317 if [ ! "$T" ]; then
318 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
319 return
320 fi
321 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322}
323
324function set_stuff_for_environment()
325{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 setpaths
327 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800329 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700330 # With this environment variable new GCC can apply colors to warnings/errors
331 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 -0700332 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700333}
334
335function set_sequence_number()
336{
Colin Cross88737132017-03-21 17:41:03 -0700337 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700338}
339
Makoto Onukida971062018-06-18 10:15:19 -0700340# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
341function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800342 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700343 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800344 *:"$cmd":*)
345 return 1
346 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700347 esac
348 return 0
349}
350
Kenny Root52aa81c2011-07-15 11:07:06 -0700351function addcompletions()
352{
353 local T dir f
354
Jim Tanga881a252018-06-19 16:34:41 +0800355 # Keep us from trying to run in something that's neither bash nor zsh.
356 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700357 return
358 fi
359
360 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800361 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700362 return
363 fi
364
Jim Tanga881a252018-06-19 16:34:41 +0800365 local completion_files=(
366 system/core/adb/adb.bash
367 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800368 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800369 )
Makoto Onukida971062018-06-18 10:15:19 -0700370 # Completion can be disabled selectively to allow users to use non-standard completion.
371 # e.g.
372 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
373 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800374 for f in ${completion_files[*]}; do
375 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700376 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700377 fi
378 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700379
Makoto Onukida971062018-06-18 10:15:19 -0700380 if should_add_completion bit ; then
381 complete -C "bit --tab" bit
382 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000383 if [ -z "$ZSH_VERSION" ]; then
384 # Doesn't work in zsh.
385 complete -o nospace -F _croot croot
386 fi
Jim Tanga881a252018-06-19 16:34:41 +0800387 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800388
dimitry73b84812018-12-11 18:06:00 +0100389 complete -F _complete_android_module_names gomod
390 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700391}
392
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700393function choosetype()
394{
395 echo "Build type choices are:"
396 echo " 1. release"
397 echo " 2. debug"
398 echo
399
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800400 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700401 DEFAULT_NUM=1
402 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700403
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800404 export TARGET_BUILD_TYPE=
405 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700406 while [ -z $TARGET_BUILD_TYPE ]
407 do
408 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800409 if [ -z "$1" ] ; then
410 read ANSWER
411 else
412 echo $1
413 ANSWER=$1
414 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700415 case $ANSWER in
416 "")
417 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
418 ;;
419 1)
420 export TARGET_BUILD_TYPE=release
421 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800422 release)
423 export TARGET_BUILD_TYPE=release
424 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425 2)
426 export TARGET_BUILD_TYPE=debug
427 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800428 debug)
429 export TARGET_BUILD_TYPE=debug
430 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700431 *)
432 echo
433 echo "I didn't understand your response. Please try again."
434 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435 ;;
436 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437 if [ -n "$1" ] ; then
438 break
439 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700440 done
441
Ying Wang08800fd2016-03-03 20:57:21 -0800442 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700443 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800444 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445}
446
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800447#
448# This function isn't really right: It chooses a TARGET_PRODUCT
449# based on the list of boards. Usually, that gets you something
450# that kinda works with a generic product, but really, you should
451# pick a product by name.
452#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700453function chooseproduct()
454{
Christopher Ferris55257d22017-03-23 11:08:58 -0700455 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700456 if [ "x$TARGET_PRODUCT" != x ] ; then
457 default_value=$TARGET_PRODUCT
458 else
Ying Wang0a76df52015-06-08 11:57:26 -0700459 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 fi
461
Ying Wang08800fd2016-03-03 20:57:21 -0800462 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800463 export TARGET_PRODUCT=
464 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 while [ -z "$TARGET_PRODUCT" ]
466 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700467 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468 if [ -z "$1" ] ; then
469 read ANSWER
470 else
471 echo $1
472 ANSWER=$1
473 fi
474
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700475 if [ -z "$ANSWER" ] ; then
476 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800477 else
478 if check_product $ANSWER
479 then
480 export TARGET_PRODUCT=$ANSWER
481 else
482 echo "** Not a valid product: $ANSWER"
483 fi
484 fi
485 if [ -n "$1" ] ; then
486 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700487 fi
488 done
489
Ying Wang08800fd2016-03-03 20:57:21 -0800490 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700491 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800492 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700493}
494
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800495function choosevariant()
496{
497 echo "Variant choices are:"
498 local index=1
499 local v
500 for v in ${VARIANT_CHOICES[@]}
501 do
502 # The product name is the name of the directory containing
503 # the makefile we found, above.
504 echo " $index. $v"
505 index=$(($index+1))
506 done
507
508 local default_value=eng
509 local ANSWER
510
511 export TARGET_BUILD_VARIANT=
512 while [ -z "$TARGET_BUILD_VARIANT" ]
513 do
514 echo -n "Which would you like? [$default_value] "
515 if [ -z "$1" ] ; then
516 read ANSWER
517 else
518 echo $1
519 ANSWER=$1
520 fi
521
522 if [ -z "$ANSWER" ] ; then
523 export TARGET_BUILD_VARIANT=$default_value
524 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
525 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800526 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800527 fi
528 else
529 if check_variant $ANSWER
530 then
531 export TARGET_BUILD_VARIANT=$ANSWER
532 else
533 echo "** Not a valid variant: $ANSWER"
534 fi
535 fi
536 if [ -n "$1" ] ; then
537 break
538 fi
539 done
540}
541
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700542function choosecombo()
543{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700544 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700545
546 echo
547 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700548 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549
550 echo
551 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700552 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800553
554 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800555 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800557 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800558 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700559}
560
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800561function add_lunch_combo()
562{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800563 if [ -n "$ZSH_VERSION" ]; then
564 echo -n "${funcfiletrace[1]}: "
565 else
566 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
567 fi
568 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 -0800569}
570
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571function print_lunch_menu()
572{
573 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700574 echo
575 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700576 echo
577 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800578
579 local i=1
580 local choice
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800581 for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 do
583 echo " $i. $choice"
584 i=$(($i+1))
585 done
586
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700587 echo
588}
589
590function lunch()
591{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 local answer
593
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700594 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800595 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700596 else
597 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700598 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700600 fi
601
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800602 local selection=
603
604 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700605 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700606 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800607 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
608 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800609 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700610 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800611 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800612 # array in zsh starts from 1 instead of 0.
613 if [ -n "$ZSH_VERSION" ]
614 then
615 selection=${choices[$(($answer))]}
616 else
617 selection=${choices[$(($answer-1))]}
618 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800619 fi
Colin Cross88737132017-03-21 17:41:03 -0700620 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700622 fi
623
Joe Onoratoda12daf2010-06-09 18:18:31 -0700624 export TARGET_BUILD_APPS=
625
Colin Cross88737132017-03-21 17:41:03 -0700626 local product variant_and_version variant version
627
628 product=${selection%%-*} # Trim everything after first dash
629 variant_and_version=${selection#*-} # Trim everything up to first dash
630 if [ "$variant_and_version" != "$selection" ]; then
631 variant=${variant_and_version%%-*}
632 if [ "$variant" != "$variant_and_version" ]; then
633 version=${variant_and_version#*-}
634 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700635 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800636
Colin Cross88737132017-03-21 17:41:03 -0700637 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800638 then
639 echo
Colin Cross88737132017-03-21 17:41:03 -0700640 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700641 return 1
642 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800643
Colin Cross88737132017-03-21 17:41:03 -0700644 TARGET_PRODUCT=$product \
645 TARGET_BUILD_VARIANT=$variant \
646 TARGET_PLATFORM_VERSION=$version \
647 build_build_var_cache
648 if [ $? -ne 0 ]
649 then
650 return 1
651 fi
652
653 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
654 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700655 if [ -n "$version" ]; then
656 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
657 else
658 unset TARGET_PLATFORM_VERSION
659 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700660 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700661
662 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800663
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800665 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800666 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700667}
668
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700669unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700670# Tab completion for lunch.
671function _lunch()
672{
673 local cur prev opts
674 COMPREPLY=()
675 cur="${COMP_WORDS[COMP_CWORD]}"
676 prev="${COMP_WORDS[COMP_CWORD-1]}"
677
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700678 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800679 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700680 fi
681
682 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700683 return 0
684}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700685
Joe Onoratoda12daf2010-06-09 18:18:31 -0700686# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700687# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700688function tapas()
689{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700690 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800691 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700692 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700693 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 -0800694 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 -0700695
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700696 if [ "$showHelp" != "" ]; then
697 $(gettop)/build/make/tapasHelp.sh
698 return
699 fi
700
Ying Wang67f02922012-08-22 10:25:20 -0700701 if [ $(echo $arch | wc -w) -gt 1 ]; then
702 echo "tapas: Error: Multiple build archs supplied: $arch"
703 return
704 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700705 if [ $(echo $variant | wc -w) -gt 1 ]; then
706 echo "tapas: Error: Multiple build variants supplied: $variant"
707 return
708 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700709 if [ $(echo $density | wc -w) -gt 1 ]; then
710 echo "tapas: Error: Multiple densities supplied: $density"
711 return
712 fi
Ying Wang67f02922012-08-22 10:25:20 -0700713
Ying Wang0a76df52015-06-08 11:57:26 -0700714 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700715 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700716 x86) product=aosp_x86;;
717 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700718 arm64) product=aosp_arm64;;
719 x86_64) product=aosp_x86_64;;
720 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700721 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700722 if [ -z "$variant" ]; then
723 variant=eng
724 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700725 if [ -z "$apps" ]; then
726 apps=all
727 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600728 if [ -z "$density" ]; then
729 density=alldpi
730 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700731
Ying Wang67f02922012-08-22 10:25:20 -0700732 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700733 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700734 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700735 export TARGET_BUILD_TYPE=release
736 export TARGET_BUILD_APPS=$apps
737
Ying Wang08800fd2016-03-03 20:57:21 -0800738 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700739 set_stuff_for_environment
740 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800741 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700742}
743
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700744function gettop
745{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700746 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700747 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700748 # The following circumlocution ensures we remove symlinks from TOP.
749 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700750 else
751 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800752 # The following circumlocution (repeated below as well) ensures
753 # that we record the true directory name and not one that is
754 # faked up with symlink names.
755 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700756 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800757 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700758 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800760 \cd ..
synergyb112a402013-07-05 19:47:47 -0700761 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700762 done
Ying Wang9cd17642012-12-13 10:52:07 -0800763 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 if [ -f "$T/$TOPFILE" ]; then
765 echo $T
766 fi
767 fi
768 fi
769}
770
771function m()
772{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800773 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774 if [ "$T" ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800775 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776 else
777 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700778 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700779 fi
780}
781
782function findmakefile()
783{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700784 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800785 local HERE=$PWD
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800786 if [ "$1" ]; then
787 \cd $1
788 fi;
Christopher Ferris55257d22017-03-23 11:08:58 -0700789 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700791 T=`PWD= /bin/pwd`
Colin Cross86425252016-05-26 15:32:15 -0700792 if [ -f "$T/Android.mk" -o -f "$T/Android.bp" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700793 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800794 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700795 return
796 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800797 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 done
Ying Wang9cd17642012-12-13 10:52:07 -0800799 \cd $HERE
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700800 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700801}
802
803function mm()
804{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800805 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700806 # If we're sitting in the root of the build tree, just do a
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700807 # normal build.
808 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800809 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700810 else
811 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800812 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700813 local MODULES=
814 local GET_INSTALL_PATH=
815 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700816 # Remove the path to top as the makefilepath needs to be relative
817 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700818 if [ ! "$T" ]; then
819 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700820 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700821 elif [ ! "$M" ]; then
822 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700823 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700824 else
Christopher Ferris55257d22017-03-23 11:08:58 -0700825 local ARG
Ying Wanga7deb082013-08-16 13:24:47 -0700826 for ARG in $@; do
827 case $ARG in
828 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
829 esac
830 done
831 if [ -n "$GET_INSTALL_PATH" ]; then
832 MODULES=
Dan Willemsen53e38992016-08-11 17:20:33 -0700833 ARGS=GET-INSTALL-PATH-IN-$(dirname ${M})
834 ARGS=${ARGS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700835 else
Colin Cross86425252016-05-26 15:32:15 -0700836 MODULES=MODULES-IN-$(dirname ${M})
837 # Convert "/" to "-".
838 MODULES=${MODULES//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700839 ARGS=$@
840 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700841 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
842 MODULES=tidy_only
843 fi
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800844 ONE_SHOT_MAKEFILE=$M _wrap_build $T/build/soong/soong_ui.bash --make-mode $MODULES $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700845 fi
846 fi
847}
848
849function mmm()
850{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800851 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700852 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800853 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800854 local MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700855 local MODULES_IN_PATHS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800856 local ARGS=
857 local DIR TO_CHOP
Colin Cross86425252016-05-26 15:32:15 -0700858 local DIR_MODULES
Ying Wanga7deb082013-08-16 13:24:47 -0700859 local GET_INSTALL_PATH=
Dan Willemsen53e38992016-08-11 17:20:33 -0700860 local GET_INSTALL_PATHS=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800861 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
862 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
863 for DIR in $DIRS ; do
Colin Cross86425252016-05-26 15:32:15 -0700864 DIR_MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
Alon Albert68895a92011-11-30 12:40:19 -0800865 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
Colin Cross86425252016-05-26 15:32:15 -0700866 # Remove the leading ./ and trailing / if any exists.
867 DIR=${DIR#./}
868 DIR=${DIR%/}
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800869 local M
870 if [ "$DIR_MODULES" = "" ]; then
871 M=$(findmakefile $DIR)
872 else
873 # Only check the target directory if a module is specified.
874 if [ -f $DIR/Android.mk -o -f $DIR/Android.bp ]; then
875 local HERE=$PWD
876 cd $DIR
877 M=`PWD= /bin/pwd`
878 M=$M/Android.mk
879 cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700880 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800881 fi
882 if [ "$M" ]; then
883 # Remove the path to top as the makefilepath needs to be relative
884 local M=`echo $M|sed 's:'$T'/::'`
Colin Cross4bfae062016-08-31 17:22:36 -0700885 if [ "$DIR_MODULES" = "" ]; then
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800886 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$(dirname ${M})"
887 GET_INSTALL_PATHS="$GET_INSTALL_PATHS GET-INSTALL-PATH-IN-$(dirname ${M})"
Colin Cross4bfae062016-08-31 17:22:36 -0700888 else
889 MODULES="$MODULES $DIR_MODULES"
890 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800891 MAKEFILE="$MAKEFILE $M"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700892 else
Ying Wanga7deb082013-08-16 13:24:47 -0700893 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700894 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700895 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997a72a6e72015-10-18 20:25:48 +0200896 *) if [ -d $DIR ]; then
897 echo "No Android.mk in $DIR.";
898 else
899 echo "Couldn't locate the directory $DIR";
900 fi
901 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700902 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700903 fi
904 done
Ying Wanga7deb082013-08-16 13:24:47 -0700905 if [ -n "$GET_INSTALL_PATH" ]; then
Dan Willemsen53e38992016-08-11 17:20:33 -0700906 ARGS=${GET_INSTALL_PATHS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700907 MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700908 MODULES_IN_PATHS=
Ying Wanga7deb082013-08-16 13:24:47 -0700909 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700910 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
911 MODULES=tidy_only
Colin Cross86425252016-05-26 15:32:15 -0700912 MODULES_IN_PATHS=
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700913 fi
Colin Cross86425252016-05-26 15:32:15 -0700914 # Convert "/" to "-".
915 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800916 ONE_SHOT_MAKEFILE="$MAKEFILE" _wrap_build $T/build/soong/soong_ui.bash --make-mode $DASH_ARGS $MODULES $MODULES_IN_PATHS $ARGS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700917 else
918 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700919 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700920 fi
921}
922
Ying Wangb607f7b2013-02-08 18:01:04 -0800923function mma()
924{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800925 local T=$(gettop)
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700926 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800927 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800928 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800929 if [ ! "$T" ]; then
930 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700931 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800932 fi
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700933 local M=$(findmakefile || echo $(realpath $PWD)/Android.mk)
Colin Cross127fcea2016-09-01 15:30:18 -0700934 # Remove the path to top as the makefilepath needs to be relative
935 local M=`echo $M|sed 's:'$T'/::'`
936 local MODULES_IN_PATHS=MODULES-IN-$(dirname ${M})
Ying Wang61cd8842015-09-24 16:19:19 -0700937 # Convert "/" to "-".
938 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800939 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@ $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800940 fi
941}
942
943function mmma()
944{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800945 local T=$(gettop)
Ying Wangb607f7b2013-02-08 18:01:04 -0800946 if [ "$T" ]; then
947 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
948 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
949 local MY_PWD=`PWD= /bin/pwd`
950 if [ "$MY_PWD" = "$T" ]; then
951 MY_PWD=
952 else
953 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
954 fi
955 local DIR=
Ying Wangcaeaa082015-09-23 16:08:55 -0700956 local MODULES_IN_PATHS=
Ying Wangb607f7b2013-02-08 18:01:04 -0800957 local ARGS=
958 for DIR in $DIRS ; do
959 if [ -d $DIR ]; then
Ying Wangcaeaa082015-09-23 16:08:55 -0700960 # Remove the leading ./ and trailing / if any exists.
961 DIR=${DIR#./}
962 DIR=${DIR%/}
963 if [ "$MY_PWD" != "" ]; then
964 DIR=$MY_PWD/$DIR
Ying Wangb607f7b2013-02-08 18:01:04 -0800965 fi
Ying Wang61cd8842015-09-24 16:19:19 -0700966 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$DIR"
Ying Wangb607f7b2013-02-08 18:01:04 -0800967 else
968 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700969 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800970 *) echo "Couldn't find directory $DIR"; return 1;;
971 esac
972 fi
973 done
Ying Wang61cd8842015-09-24 16:19:19 -0700974 # Convert "/" to "-".
975 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800976 _wrap_build $T/build/soong/soong_ui.bash --make-mode $DASH_ARGS $ARGS $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800977 else
978 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700979 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800980 fi
981}
982
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700983function croot()
984{
Christopher Ferris55257d22017-03-23 11:08:58 -0700985 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700986 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700987 if [ "$1" ]; then
988 \cd $(gettop)/$1
989 else
990 \cd $(gettop)
991 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700992 else
993 echo "Couldn't locate the top of the tree. Try setting TOP."
994 fi
995}
996
Anton Hanssonece9c482019-02-04 18:15:39 +0000997function _croot()
998{
999 local T=$(gettop)
1000 if [ "$T" ]; then
1001 local cur="${COMP_WORDS[COMP_CWORD]}"
1002 k=0
1003 for c in $(compgen -d ${T}/${cur}); do
1004 COMPREPLY[k++]=${c#${T}/}/
1005 done
1006 fi
1007}
1008
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001009function cproj()
1010{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001011 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001012 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001013 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001014 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1015 T=$PWD
1016 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001017 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001018 return
1019 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001020 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001021 done
Ying Wang9cd17642012-12-13 10:52:07 -08001022 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001023 echo "can't find Android.mk"
1024}
1025
Daniel Sandler47e0a882013-07-30 13:23:52 -04001026# simplified version of ps; output in the form
1027# <pid> <procname>
1028function qpid() {
1029 local prepend=''
1030 local append=''
1031 if [ "$1" = "--exact" ]; then
1032 prepend=' '
1033 append='$'
1034 shift
1035 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001036 echo "usage: qpid [[--exact] <process name|pid>"
1037 return 255
1038 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001039
1040 local EXE="$1"
1041 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001042 qpid | \grep "$prepend$EXE$append"
1043 else
1044 adb shell ps \
1045 | tr -d '\r' \
1046 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1047 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001048}
1049
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001050# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001051# that has the core-file-size limit set correctly
1052#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001053# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001054# if its core-file-size limit is not set already.
1055# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1056
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001057function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001058{
Ying Wang08800fd2016-03-03 20:57:21 -08001059 echo "Getting root...";
1060 adb root;
1061 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001062
Ying Wang08800fd2016-03-03 20:57:21 -08001063 echo "Remounting root partition read-write...";
1064 adb shell mount -w -o remount -t rootfs rootfs;
1065 sleep 1;
1066 adb wait-for-device;
1067 adb shell mkdir -p /cores;
1068 adb shell mount -t tmpfs tmpfs /cores;
1069 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001070
Ying Wang08800fd2016-03-03 20:57:21 -08001071 echo "Granting SELinux permission to dump in /cores...";
1072 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001073
Ying Wang08800fd2016-03-03 20:57:21 -08001074 echo "Set core pattern.";
1075 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001076
Ying Wang08800fd2016-03-03 20:57:21 -08001077 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001078}
1079
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001080# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001081# $1 = PID of process (e.g., $(pid mediaserver))
1082#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001083# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001084# dump to actually be generated.
1085
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001086function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001087{
Ying Wang08800fd2016-03-03 20:57:21 -08001088 local PID=$1;
1089 if [ -z "$PID" ]; then
1090 printf "Expecting a PID!\n";
1091 return;
1092 fi;
1093 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001094 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001095}
1096
1097# core - send SIGV and pull the core for process
1098# $1 = PID of process (e.g., $(pid mediaserver))
1099#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001100# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001101# enabled globally.
1102
1103function core()
1104{
Ying Wang08800fd2016-03-03 20:57:21 -08001105 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001106
Ying Wang08800fd2016-03-03 20:57:21 -08001107 if [ -z "$PID" ]; then
1108 printf "Expecting a PID!\n";
1109 return;
1110 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001111
Ying Wang08800fd2016-03-03 20:57:21 -08001112 local CORENAME=core.$PID;
1113 local COREPATH=/cores/$CORENAME;
1114 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001115
Ying Wang08800fd2016-03-03 20:57:21 -08001116 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001117
Ying Wang08800fd2016-03-03 20:57:21 -08001118 local done=0;
1119 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1120 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1121 adb shell kill -$SIG $PID;
1122 sleep 1;
1123 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124
Ying Wang08800fd2016-03-03 20:57:21 -08001125 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1126 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001127}
1128
Christopher Tate744ee802009-11-12 15:33:08 -08001129# systemstack - dump the current stack trace of all threads in the system process
1130# to the usual ANR traces file
1131function systemstack()
1132{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001133 stacks system_server
1134}
1135
Michael Wrightaeed7212014-06-19 19:58:12 -07001136# Read the ELF header from /proc/$PID/exe to determine if the process is
1137# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001138function is64bit()
1139{
1140 local PID="$1"
1141 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -08001142 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001143 echo "64"
1144 else
1145 echo ""
1146 fi
1147 else
1148 echo ""
1149 fi
1150}
1151
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001152case `uname -s` in
1153 Darwin)
1154 function sgrep()
1155 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001156 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 -04001157 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001158 }
1159
1160 ;;
1161 *)
1162 function sgrep()
1163 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001164 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 -04001165 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001166 }
1167 ;;
1168esac
1169
Raghu Gandham8da43102012-07-25 19:57:22 -07001170function gettargetarch
1171{
1172 get_build_var TARGET_ARCH
1173}
1174
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001175function ggrep()
1176{
Mike Frysinger5e479732015-09-22 18:13:48 -04001177 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1178 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001179}
1180
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001181function jgrep()
1182{
Mike Frysinger5e479732015-09-22 18:13:48 -04001183 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1184 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001185}
1186
1187function cgrep()
1188{
Mike Frysinger5e479732015-09-22 18:13:48 -04001189 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' \) \
1190 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001191}
1192
1193function resgrep()
1194{
Christopher Ferris55257d22017-03-23 11:08:58 -07001195 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001196 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1197 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1198 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001199}
1200
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001201function mangrep()
1202{
Mike Frysinger5e479732015-09-22 18:13:48 -04001203 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1204 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001205}
1206
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001207function sepgrep()
1208{
Mike Frysinger5e479732015-09-22 18:13:48 -04001209 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1210 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001211}
1212
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001213function rcgrep()
1214{
Mike Frysinger5e479732015-09-22 18:13:48 -04001215 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1216 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001217}
1218
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001219case `uname -s` in
1220 Darwin)
1221 function mgrep()
1222 {
David Grossd1d6fc52017-05-10 10:49:44 -07001223 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 -04001224 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001225 }
1226
1227 function treegrep()
1228 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001229 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 -04001230 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001231 }
1232
1233 ;;
1234 *)
1235 function mgrep()
1236 {
David Grossd1d6fc52017-05-10 10:49:44 -07001237 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 -04001238 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001239 }
1240
1241 function treegrep()
1242 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001243 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 -04001244 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245 }
1246
1247 ;;
1248esac
1249
1250function getprebuilt
1251{
1252 get_abs_build_var ANDROID_PREBUILTS
1253}
1254
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001255function tracedmdump()
1256{
Christopher Ferris55257d22017-03-23 11:08:58 -07001257 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001258 if [ ! "$T" ]; then
1259 echo "Couldn't locate the top of the tree. Try setting TOP."
1260 return
1261 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001262 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001263 local arch=$(gettargetarch)
1264 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001265
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001266 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001267 if [ ! "$TRACE" ] ; then
1268 echo "usage: tracedmdump tracename"
1269 return
1270 fi
1271
Jack Veenstra60116fc2009-04-09 18:12:34 -07001272 if [ ! -r "$KERNEL" ] ; then
1273 echo "Error: cannot find kernel: '$KERNEL'"
1274 return
1275 fi
1276
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001277 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001278 if [ "$BASETRACE" = "$TRACE" ] ; then
1279 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1280 fi
1281
1282 echo "post-processing traces..."
1283 rm -f $TRACE/qtrace.dexlist
1284 post_trace $TRACE
1285 if [ $? -ne 0 ]; then
1286 echo "***"
1287 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1288 echo "***"
1289 return
1290 fi
1291 echo "generating dexlist output..."
1292 /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
1293 echo "generating dmtrace data..."
1294 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1295 echo "generating html file..."
1296 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1297 echo "done, see $TRACE/dmtrace.html for details"
1298 echo "or run:"
1299 echo " traceview $TRACE/dmtrace"
1300}
1301
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001302# communicate with a running device or emulator, set up necessary state,
1303# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001304function runhat()
1305{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001306 # process standard adb options
1307 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001308 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001309 adbTarget=$1
1310 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001311 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001312 adbTarget="$1 $2"
1313 shift 2
1314 fi
1315 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001316 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001317
1318 # runhat options
1319 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001320
1321 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001322 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001323 return
1324 fi
1325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001326 # confirm hat is available
1327 if [ -z $(which hat) ]; then
1328 echo "hat is not available in this configuration."
1329 return
1330 fi
1331
Andy McFaddenb6289852010-07-12 08:00:19 -07001332 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001333 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001334 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001335 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001336 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001337 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001338 echo -n "> "
1339 read
1340
The Android Open Source Project88b60792009-03-03 19:28:42 -08001341 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001342
The Android Open Source Project88b60792009-03-03 19:28:42 -08001343 echo "Retrieving file $devFile..."
1344 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001345
The Android Open Source Project88b60792009-03-03 19:28:42 -08001346 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001347
The Android Open Source Project88b60792009-03-03 19:28:42 -08001348 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001349 echo "View the output by pointing your browser at http://localhost:7000/"
1350 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001351 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001352}
1353
1354function getbugreports()
1355{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001356 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001357
1358 if [ ! "$reports" ]; then
1359 echo "Could not locate any bugreports."
1360 return
1361 fi
1362
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001363 local report
1364 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001365 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001366 echo "/sdcard/bugreports/${report}"
1367 adb pull /sdcard/bugreports/${report} ${report}
1368 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001369 done
1370}
1371
Victoria Lease1b296b42012-08-21 15:44:06 -07001372function getsdcardpath()
1373{
1374 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1375}
1376
1377function getscreenshotpath()
1378{
1379 echo "$(getsdcardpath)/Pictures/Screenshots"
1380}
1381
1382function getlastscreenshot()
1383{
1384 local screenshot_path=$(getscreenshotpath)
1385 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1386 if [ "$screenshot" = "" ]; then
1387 echo "No screenshots found."
1388 return
1389 fi
1390 echo "${screenshot}"
1391 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1392}
1393
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394function startviewserver()
1395{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001396 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001397 if [ $# -gt 0 ]; then
1398 port=$1
1399 fi
1400 adb shell service call window 1 i32 $port
1401}
1402
1403function stopviewserver()
1404{
1405 adb shell service call window 2
1406}
1407
1408function isviewserverstarted()
1409{
1410 adb shell service call window 3
1411}
1412
Romain Guyb84049a2010-10-04 16:56:11 -07001413function key_home()
1414{
1415 adb shell input keyevent 3
1416}
1417
1418function key_back()
1419{
1420 adb shell input keyevent 4
1421}
1422
1423function key_menu()
1424{
1425 adb shell input keyevent 82
1426}
1427
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001428function smoketest()
1429{
1430 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1431 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1432 return
1433 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001434 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001435 if [ ! "$T" ]; then
1436 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1437 return
1438 fi
1439
Ying Wang9cd17642012-12-13 10:52:07 -08001440 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001441 adb uninstall com.android.smoketest > /dev/null &&
1442 adb uninstall com.android.smoketest.tests > /dev/null &&
1443 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1444 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1445 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1446}
1447
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001448# simple shortcut to the runtest command
1449function runtest()
1450{
Christopher Ferris55257d22017-03-23 11:08:58 -07001451 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001452 if [ ! "$T" ]; then
1453 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1454 return
1455 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001456 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001457}
1458
The Android Open Source Project88b60792009-03-03 19:28:42 -08001459function godir () {
1460 if [[ -z "$1" ]]; then
1461 echo "Usage: godir <regex>"
1462 return
1463 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001464 local T=$(gettop)
1465 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001466 if [ ! "$OUT_DIR" = "" ]; then
1467 mkdir -p $OUT_DIR
1468 FILELIST=$OUT_DIR/filelist
1469 else
1470 FILELIST=$T/filelist
1471 fi
1472 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001473 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001474 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001475 echo " Done"
1476 echo ""
1477 fi
1478 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001479 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001480 if [[ ${#lines[@]} = 0 ]]; then
1481 echo "Not found"
1482 return
1483 fi
1484 local pathname
1485 local choice
1486 if [[ ${#lines[@]} > 1 ]]; then
1487 while [[ -z "$pathname" ]]; do
1488 local index=1
1489 local line
1490 for line in ${lines[@]}; do
1491 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001492 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001493 done
1494 echo
1495 echo -n "Select one: "
1496 unset choice
1497 read choice
1498 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1499 echo "Invalid choice"
1500 continue
1501 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001502 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001503 done
1504 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001505 pathname=${lines[0]}
1506 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001507 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001508}
1509
Steven Moreland62054a42018-12-06 10:11:40 -08001510# Update module-info.json in out.
1511function refreshmod() {
1512 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1513 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1514 return 1
1515 fi
1516
1517 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1518
1519 # for the output of the next command
1520 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1521
1522 # Note, can't use absolute path because of the way make works.
1523 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1524 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1525}
1526
1527# List all modules for the current device, as cached in module-info.json. If any build change is
1528# made and it should be reflected in the output, you should run 'refreshmod' first.
1529function allmod() {
1530 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1531 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1532 return 1
1533 fi
1534
1535 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1536 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1537 refreshmod || return 1
1538 fi
1539
1540 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1541}
1542
Rett Berg78d1c932019-01-24 14:34:23 -08001543# 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 -08001544# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001545function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001546 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1547 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1548 return 1
1549 fi
1550
1551 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001552 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001553 return 1
1554 fi
1555
1556 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1557 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1558 refreshmod || return 1
1559 fi
1560
1561 local relpath=$(python -c "import json, os
1562module = '$1'
1563module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1564if module not in module_info:
1565 exit(1)
1566print module_info[module]['path'][0]" 2>/dev/null)
1567
1568 if [ -z "$relpath" ]; then
1569 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1570 return 1
1571 else
Rett Berg78d1c932019-01-24 14:34:23 -08001572 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001573 fi
1574}
1575
Rett Berg78d1c932019-01-24 14:34:23 -08001576# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1577# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1578function gomod() {
1579 if [[ $# -ne 1 ]]; then
1580 echo "usage: gomod <module>" >&2
1581 return 1
1582 fi
1583
1584 local path="$(pathmod $@)"
1585 if [ -z "$path" ]; then
1586 return 1
1587 fi
1588 cd $path
1589}
1590
dimitry73b84812018-12-11 18:06:00 +01001591function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001592 local word=${COMP_WORDS[COMP_CWORD]}
1593 COMPREPLY=( $(allmod | grep -E "^$word") )
1594}
1595
Alex Rayf0d08eb2013-03-08 15:15:06 -08001596# Print colored exit condition
1597function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001598 "$@"
1599 local retval=$?
1600 if [ $retval -ne 0 ]
1601 then
Jacky Cao89483b82015-05-15 22:12:53 +08001602 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001603 else
Jacky Cao89483b82015-05-15 22:12:53 +08001604 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001605 fi
1606 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001607}
1608
Ying Wanged21d4c2014-08-24 22:14:19 -07001609function get_make_command()
1610{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001611 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1612 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001613 # Always use the real make if -C is passed in
1614 for arg in "$@"; do
1615 if [[ $arg == -C* ]]; then
1616 echo command make
1617 return
1618 fi
1619 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001620 echo build/soong/soong_ui.bash --make-mode
1621 else
1622 echo command make
1623 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001624}
1625
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001626function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001627{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001628 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1629 "$@"
1630 return $?
1631 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001632 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001633 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001634 local ret=$?
1635 local end_time=$(date +"%s")
1636 local tdiff=$(($end_time-$start_time))
1637 local hours=$(($tdiff / 3600 ))
1638 local mins=$((($tdiff % 3600) / 60))
1639 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001640 local ncolors=$(tput colors 2>/dev/null)
1641 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001642 color_failed=$'\E'"[0;31m"
1643 color_success=$'\E'"[0;32m"
1644 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001645 else
1646 color_failed=""
1647 color_success=""
1648 color_reset=""
1649 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001650 echo
1651 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001652 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001653 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001654 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001655 fi
1656 if [ $hours -gt 0 ] ; then
1657 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1658 elif [ $mins -gt 0 ] ; then
1659 printf "(%02g:%02g (mm:ss))" $mins $secs
1660 elif [ $secs -gt 0 ] ; then
1661 printf "(%s seconds)" $secs
1662 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001663 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001664 echo
1665 return $ret
1666}
1667
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001668function make()
1669{
Dan Willemsene9842242017-07-28 13:00:13 -07001670 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001671}
1672
David Zeuthen1b126ff2015-09-30 17:10:48 -04001673function provision()
1674{
1675 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1676 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1677 return 1
1678 fi
1679 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1680 echo "There is no provisioning script for the device." >&2
1681 return 1
1682 fi
1683
1684 # Check if user really wants to do this.
1685 if [ "$1" = "--no-confirmation" ]; then
1686 shift 1
1687 else
1688 echo "This action will reflash your device."
1689 echo ""
1690 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1691 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001692 echo -n "Are you sure you want to do this (yes/no)? "
1693 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001694 if [[ "${REPLY}" != "yes" ]] ; then
1695 echo "Not taking any action. Exiting." >&2
1696 return 1
1697 fi
1698 fi
1699 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1700}
1701
Jim Tanga881a252018-06-19 16:34:41 +08001702# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001703function enable_zsh_completion() {
1704 # Don't override user's options if bash-style completion is already enabled.
1705 if ! declare -f complete >/dev/null; then
1706 autoload -U compinit && compinit
1707 autoload -U bashcompinit && bashcompinit
1708 fi
Jim Tanga881a252018-06-19 16:34:41 +08001709}
1710
1711function validate_current_shell() {
1712 local current_sh="$(ps -o command -p $$)"
1713 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001714 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001715 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001716 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001717 *zsh*)
1718 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001719 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001720 *)
Jim Tanga881a252018-06-19 16:34:41 +08001721 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 -07001722 ;;
1723 esac
Jim Tanga881a252018-06-19 16:34:41 +08001724}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001725
1726# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001727# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1728# load those.
1729#
1730# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001731function source_vendorsetup() {
Dan Willemsend855a722019-02-12 15:52:36 -08001732 allowed=
1733 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1734 if [ -n "$allowed" ]; then
1735 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1736 echo " $allowed"
1737 echo " $f"
1738 return
1739 fi
1740 allowed="$f"
1741 done
1742
1743 allowed_files=
1744 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001745 for dir in device vendor product; do
1746 for f in $(test -d $dir && \
1747 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001748
1749 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1750 echo "including $f"; . "$f"
1751 else
1752 echo "ignoring $f, not in $allowed"
1753 fi
Jim Tanga881a252018-06-19 16:34:41 +08001754 done
1755 done
1756}
Kenny Root52aa81c2011-07-15 11:07:06 -07001757
Jim Tanga881a252018-06-19 16:34:41 +08001758validate_current_shell
1759source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001760addcompletions