blob: 9e381a23e3d7170a4e3571070f93246abf08ca5b [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.
25- mgrep: Greps on all local Makefiles files.
26- 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
249 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS:
250 export ANDROID_BUILD_PATHS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200251
252 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
253 # to ensure that the corresponding 'emulator' binaries are used.
254 case $(uname -s) in
255 Darwin)
256 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
257 ;;
258 Linux)
259 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
260 ;;
261 *)
262 ANDROID_EMULATOR_PREBUILTS=
263 ;;
264 esac
265 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700266 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200267 export ANDROID_EMULATOR_PREBUILTS
268 fi
269
Jim Tangb3fda302018-12-22 10:24:55 +0800270 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
271 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
272 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch:"
273 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch:"
274 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch:"
275 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ACLOUD_PATH$AIDEGEN_PATH$ATEST_PATH
276
Ying Wangaa1c9b52012-11-26 20:51:59 -0800277 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800278
279 # out with the duplicate old
280 if [ -n $ANDROID_PYTHONPATH ]; then
281 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
282 fi
283 # and in with the new
284 export ANDROID_PYTHONPATH=$T/development/python-packages:
285 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800286
Colin Crosse97e6932017-06-30 16:01:45 -0700287 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
288 export JAVA_HOME=$ANDROID_JAVA_HOME
289 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
290 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
291 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500292
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800293 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700294 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
295 export OUT=$ANDROID_PRODUCT_OUT
296
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700297 unset ANDROID_HOST_OUT
298 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
299
Simran Basidd050ed2017-02-13 13:46:48 -0800300 unset ANDROID_HOST_OUT_TESTCASES
301 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
302
303 unset ANDROID_TARGET_OUT_TESTCASES
304 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
305
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800306 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700307 # TODO: fix the path
308 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
309}
310
311function printconfig()
312{
Christopher Ferris55257d22017-03-23 11:08:58 -0700313 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800314 if [ ! "$T" ]; then
315 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
316 return
317 fi
318 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700319}
320
321function set_stuff_for_environment()
322{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800323 setpaths
324 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700325
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800326 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700327 # With this environment variable new GCC can apply colors to warnings/errors
328 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 -0700329 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700330}
331
332function set_sequence_number()
333{
Colin Cross88737132017-03-21 17:41:03 -0700334 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700335}
336
Makoto Onukida971062018-06-18 10:15:19 -0700337# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
338function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800339 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700340 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800341 *:"$cmd":*)
342 return 1
343 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700344 esac
345 return 0
346}
347
Kenny Root52aa81c2011-07-15 11:07:06 -0700348function addcompletions()
349{
350 local T dir f
351
Jim Tanga881a252018-06-19 16:34:41 +0800352 # Keep us from trying to run in something that's neither bash nor zsh.
353 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700354 return
355 fi
356
357 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800358 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700359 return
360 fi
361
Jim Tanga881a252018-06-19 16:34:41 +0800362 local completion_files=(
363 system/core/adb/adb.bash
364 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800365 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800366 )
Makoto Onukida971062018-06-18 10:15:19 -0700367 # Completion can be disabled selectively to allow users to use non-standard completion.
368 # e.g.
369 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
370 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800371 for f in ${completion_files[*]}; do
372 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700373 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700374 fi
375 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700376
Makoto Onukida971062018-06-18 10:15:19 -0700377 if should_add_completion bit ; then
378 complete -C "bit --tab" bit
379 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000380 if [ -z "$ZSH_VERSION" ]; then
381 # Doesn't work in zsh.
382 complete -o nospace -F _croot croot
383 fi
Jim Tanga881a252018-06-19 16:34:41 +0800384 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800385
dimitry73b84812018-12-11 18:06:00 +0100386 complete -F _complete_android_module_names gomod
387 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700388}
389
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700390function choosetype()
391{
392 echo "Build type choices are:"
393 echo " 1. release"
394 echo " 2. debug"
395 echo
396
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800397 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700398 DEFAULT_NUM=1
399 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800401 export TARGET_BUILD_TYPE=
402 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700403 while [ -z $TARGET_BUILD_TYPE ]
404 do
405 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800406 if [ -z "$1" ] ; then
407 read ANSWER
408 else
409 echo $1
410 ANSWER=$1
411 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700412 case $ANSWER in
413 "")
414 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
415 ;;
416 1)
417 export TARGET_BUILD_TYPE=release
418 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800419 release)
420 export TARGET_BUILD_TYPE=release
421 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700422 2)
423 export TARGET_BUILD_TYPE=debug
424 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800425 debug)
426 export TARGET_BUILD_TYPE=debug
427 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428 *)
429 echo
430 echo "I didn't understand your response. Please try again."
431 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700432 ;;
433 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800434 if [ -n "$1" ] ; then
435 break
436 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700437 done
438
Ying Wang08800fd2016-03-03 20:57:21 -0800439 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700440 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800441 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700442}
443
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800444#
445# This function isn't really right: It chooses a TARGET_PRODUCT
446# based on the list of boards. Usually, that gets you something
447# that kinda works with a generic product, but really, you should
448# pick a product by name.
449#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450function chooseproduct()
451{
Christopher Ferris55257d22017-03-23 11:08:58 -0700452 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700453 if [ "x$TARGET_PRODUCT" != x ] ; then
454 default_value=$TARGET_PRODUCT
455 else
Ying Wang0a76df52015-06-08 11:57:26 -0700456 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700457 fi
458
Ying Wang08800fd2016-03-03 20:57:21 -0800459 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800460 export TARGET_PRODUCT=
461 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462 while [ -z "$TARGET_PRODUCT" ]
463 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700464 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800465 if [ -z "$1" ] ; then
466 read ANSWER
467 else
468 echo $1
469 ANSWER=$1
470 fi
471
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472 if [ -z "$ANSWER" ] ; then
473 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800474 else
475 if check_product $ANSWER
476 then
477 export TARGET_PRODUCT=$ANSWER
478 else
479 echo "** Not a valid product: $ANSWER"
480 fi
481 fi
482 if [ -n "$1" ] ; then
483 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700484 fi
485 done
486
Ying Wang08800fd2016-03-03 20:57:21 -0800487 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700488 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800489 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700490}
491
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800492function choosevariant()
493{
494 echo "Variant choices are:"
495 local index=1
496 local v
497 for v in ${VARIANT_CHOICES[@]}
498 do
499 # The product name is the name of the directory containing
500 # the makefile we found, above.
501 echo " $index. $v"
502 index=$(($index+1))
503 done
504
505 local default_value=eng
506 local ANSWER
507
508 export TARGET_BUILD_VARIANT=
509 while [ -z "$TARGET_BUILD_VARIANT" ]
510 do
511 echo -n "Which would you like? [$default_value] "
512 if [ -z "$1" ] ; then
513 read ANSWER
514 else
515 echo $1
516 ANSWER=$1
517 fi
518
519 if [ -z "$ANSWER" ] ; then
520 export TARGET_BUILD_VARIANT=$default_value
521 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
522 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800523 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800524 fi
525 else
526 if check_variant $ANSWER
527 then
528 export TARGET_BUILD_VARIANT=$ANSWER
529 else
530 echo "** Not a valid variant: $ANSWER"
531 fi
532 fi
533 if [ -n "$1" ] ; then
534 break
535 fi
536 done
537}
538
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700539function choosecombo()
540{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700541 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700542
543 echo
544 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700545 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546
547 echo
548 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700549 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800550
551 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800552 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700553 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800554 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800555 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700556}
557
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558function add_lunch_combo()
559{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800560 if [ -n "$ZSH_VERSION" ]; then
561 echo -n "${funcfiletrace[1]}: "
562 else
563 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
564 fi
565 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 -0800566}
567
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700568function print_lunch_menu()
569{
570 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700571 echo
572 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700573 echo
574 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800575
576 local i=1
577 local choice
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800578 for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800579 do
580 echo " $i. $choice"
581 i=$(($i+1))
582 done
583
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 echo
585}
586
587function lunch()
588{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 local answer
590
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700591 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700593 else
594 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700595 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800596 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700597 fi
598
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599 local selection=
600
601 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700602 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700603 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800604 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
605 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800606 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700607 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800608 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800609 # array in zsh starts from 1 instead of 0.
610 if [ -n "$ZSH_VERSION" ]
611 then
612 selection=${choices[$(($answer))]}
613 else
614 selection=${choices[$(($answer-1))]}
615 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800616 fi
Colin Cross88737132017-03-21 17:41:03 -0700617 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800618 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700619 fi
620
Joe Onoratoda12daf2010-06-09 18:18:31 -0700621 export TARGET_BUILD_APPS=
622
Colin Cross88737132017-03-21 17:41:03 -0700623 local product variant_and_version variant version
624
625 product=${selection%%-*} # Trim everything after first dash
626 variant_and_version=${selection#*-} # Trim everything up to first dash
627 if [ "$variant_and_version" != "$selection" ]; then
628 variant=${variant_and_version%%-*}
629 if [ "$variant" != "$variant_and_version" ]; then
630 version=${variant_and_version#*-}
631 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700632 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633
Colin Cross88737132017-03-21 17:41:03 -0700634 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800635 then
636 echo
Colin Cross88737132017-03-21 17:41:03 -0700637 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700638 return 1
639 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800640
Colin Cross88737132017-03-21 17:41:03 -0700641 TARGET_PRODUCT=$product \
642 TARGET_BUILD_VARIANT=$variant \
643 TARGET_PLATFORM_VERSION=$version \
644 build_build_var_cache
645 if [ $? -ne 0 ]
646 then
647 return 1
648 fi
649
650 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
651 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700652 if [ -n "$version" ]; then
653 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
654 else
655 unset TARGET_PLATFORM_VERSION
656 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700657 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700658
659 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800660
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700661 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800663 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700664}
665
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700666unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700667# Tab completion for lunch.
668function _lunch()
669{
670 local cur prev opts
671 COMPREPLY=()
672 cur="${COMP_WORDS[COMP_CWORD]}"
673 prev="${COMP_WORDS[COMP_CWORD-1]}"
674
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700675 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800676 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700677 fi
678
679 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700680 return 0
681}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700682
Joe Onoratoda12daf2010-06-09 18:18:31 -0700683# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700684# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700685function tapas()
686{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700687 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800688 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700689 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700690 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 -0800691 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 -0700692
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700693 if [ "$showHelp" != "" ]; then
694 $(gettop)/build/make/tapasHelp.sh
695 return
696 fi
697
Ying Wang67f02922012-08-22 10:25:20 -0700698 if [ $(echo $arch | wc -w) -gt 1 ]; then
699 echo "tapas: Error: Multiple build archs supplied: $arch"
700 return
701 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700702 if [ $(echo $variant | wc -w) -gt 1 ]; then
703 echo "tapas: Error: Multiple build variants supplied: $variant"
704 return
705 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700706 if [ $(echo $density | wc -w) -gt 1 ]; then
707 echo "tapas: Error: Multiple densities supplied: $density"
708 return
709 fi
Ying Wang67f02922012-08-22 10:25:20 -0700710
Ying Wang0a76df52015-06-08 11:57:26 -0700711 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700712 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700713 x86) product=aosp_x86;;
714 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700715 arm64) product=aosp_arm64;;
716 x86_64) product=aosp_x86_64;;
717 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700718 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700719 if [ -z "$variant" ]; then
720 variant=eng
721 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700722 if [ -z "$apps" ]; then
723 apps=all
724 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600725 if [ -z "$density" ]; then
726 density=alldpi
727 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700728
Ying Wang67f02922012-08-22 10:25:20 -0700729 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700730 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700731 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700732 export TARGET_BUILD_TYPE=release
733 export TARGET_BUILD_APPS=$apps
734
Ying Wang08800fd2016-03-03 20:57:21 -0800735 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700736 set_stuff_for_environment
737 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800738 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700739}
740
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700741function gettop
742{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700743 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700744 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700745 # The following circumlocution ensures we remove symlinks from TOP.
746 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700747 else
748 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800749 # The following circumlocution (repeated below as well) ensures
750 # that we record the true directory name and not one that is
751 # faked up with symlink names.
752 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700753 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800754 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700755 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700756 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800757 \cd ..
synergyb112a402013-07-05 19:47:47 -0700758 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700759 done
Ying Wang9cd17642012-12-13 10:52:07 -0800760 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700761 if [ -f "$T/$TOPFILE" ]; then
762 echo $T
763 fi
764 fi
765 fi
766}
767
768function m()
769{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800770 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700771 if [ "$T" ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800772 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700773 else
774 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700775 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700776 fi
777}
778
779function findmakefile()
780{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700781 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800782 local HERE=$PWD
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800783 if [ "$1" ]; then
784 \cd $1
785 fi;
Christopher Ferris55257d22017-03-23 11:08:58 -0700786 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700787 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700788 T=`PWD= /bin/pwd`
Colin Cross86425252016-05-26 15:32:15 -0700789 if [ -f "$T/Android.mk" -o -f "$T/Android.bp" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700790 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800791 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700792 return
793 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800794 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700795 done
Ying Wang9cd17642012-12-13 10:52:07 -0800796 \cd $HERE
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700797 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798}
799
800function mm()
801{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800802 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700803 # If we're sitting in the root of the build tree, just do a
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700804 # normal build.
805 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800806 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700807 else
808 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800809 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700810 local MODULES=
811 local GET_INSTALL_PATH=
812 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700813 # Remove the path to top as the makefilepath needs to be relative
814 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700815 if [ ! "$T" ]; then
816 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700817 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700818 elif [ ! "$M" ]; then
819 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700820 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700821 else
Christopher Ferris55257d22017-03-23 11:08:58 -0700822 local ARG
Ying Wanga7deb082013-08-16 13:24:47 -0700823 for ARG in $@; do
824 case $ARG in
825 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
826 esac
827 done
828 if [ -n "$GET_INSTALL_PATH" ]; then
829 MODULES=
Dan Willemsen53e38992016-08-11 17:20:33 -0700830 ARGS=GET-INSTALL-PATH-IN-$(dirname ${M})
831 ARGS=${ARGS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700832 else
Colin Cross86425252016-05-26 15:32:15 -0700833 MODULES=MODULES-IN-$(dirname ${M})
834 # Convert "/" to "-".
835 MODULES=${MODULES//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700836 ARGS=$@
837 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700838 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
839 MODULES=tidy_only
840 fi
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800841 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 -0700842 fi
843 fi
844}
845
846function mmm()
847{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800848 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700849 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800850 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800851 local MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700852 local MODULES_IN_PATHS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800853 local ARGS=
854 local DIR TO_CHOP
Colin Cross86425252016-05-26 15:32:15 -0700855 local DIR_MODULES
Ying Wanga7deb082013-08-16 13:24:47 -0700856 local GET_INSTALL_PATH=
Dan Willemsen53e38992016-08-11 17:20:33 -0700857 local GET_INSTALL_PATHS=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800858 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
859 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
860 for DIR in $DIRS ; do
Colin Cross86425252016-05-26 15:32:15 -0700861 DIR_MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
Alon Albert68895a92011-11-30 12:40:19 -0800862 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
Colin Cross86425252016-05-26 15:32:15 -0700863 # Remove the leading ./ and trailing / if any exists.
864 DIR=${DIR#./}
865 DIR=${DIR%/}
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800866 local M
867 if [ "$DIR_MODULES" = "" ]; then
868 M=$(findmakefile $DIR)
869 else
870 # Only check the target directory if a module is specified.
871 if [ -f $DIR/Android.mk -o -f $DIR/Android.bp ]; then
872 local HERE=$PWD
873 cd $DIR
874 M=`PWD= /bin/pwd`
875 M=$M/Android.mk
876 cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700877 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800878 fi
879 if [ "$M" ]; then
880 # Remove the path to top as the makefilepath needs to be relative
881 local M=`echo $M|sed 's:'$T'/::'`
Colin Cross4bfae062016-08-31 17:22:36 -0700882 if [ "$DIR_MODULES" = "" ]; then
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800883 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$(dirname ${M})"
884 GET_INSTALL_PATHS="$GET_INSTALL_PATHS GET-INSTALL-PATH-IN-$(dirname ${M})"
Colin Cross4bfae062016-08-31 17:22:36 -0700885 else
886 MODULES="$MODULES $DIR_MODULES"
887 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800888 MAKEFILE="$MAKEFILE $M"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700889 else
Ying Wanga7deb082013-08-16 13:24:47 -0700890 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700891 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700892 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997a72a6e72015-10-18 20:25:48 +0200893 *) if [ -d $DIR ]; then
894 echo "No Android.mk in $DIR.";
895 else
896 echo "Couldn't locate the directory $DIR";
897 fi
898 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700899 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700900 fi
901 done
Ying Wanga7deb082013-08-16 13:24:47 -0700902 if [ -n "$GET_INSTALL_PATH" ]; then
Dan Willemsen53e38992016-08-11 17:20:33 -0700903 ARGS=${GET_INSTALL_PATHS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700904 MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700905 MODULES_IN_PATHS=
Ying Wanga7deb082013-08-16 13:24:47 -0700906 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700907 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
908 MODULES=tidy_only
Colin Cross86425252016-05-26 15:32:15 -0700909 MODULES_IN_PATHS=
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700910 fi
Colin Cross86425252016-05-26 15:32:15 -0700911 # Convert "/" to "-".
912 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800913 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 -0700914 else
915 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700916 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700917 fi
918}
919
Ying Wangb607f7b2013-02-08 18:01:04 -0800920function mma()
921{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800922 local T=$(gettop)
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700923 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800924 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800925 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800926 if [ ! "$T" ]; then
927 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700928 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800929 fi
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700930 local M=$(findmakefile || echo $(realpath $PWD)/Android.mk)
Colin Cross127fcea2016-09-01 15:30:18 -0700931 # Remove the path to top as the makefilepath needs to be relative
932 local M=`echo $M|sed 's:'$T'/::'`
933 local MODULES_IN_PATHS=MODULES-IN-$(dirname ${M})
Ying Wang61cd8842015-09-24 16:19:19 -0700934 # Convert "/" to "-".
935 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800936 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@ $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800937 fi
938}
939
940function mmma()
941{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800942 local T=$(gettop)
Ying Wangb607f7b2013-02-08 18:01:04 -0800943 if [ "$T" ]; then
944 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
945 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
946 local MY_PWD=`PWD= /bin/pwd`
947 if [ "$MY_PWD" = "$T" ]; then
948 MY_PWD=
949 else
950 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
951 fi
952 local DIR=
Ying Wangcaeaa082015-09-23 16:08:55 -0700953 local MODULES_IN_PATHS=
Ying Wangb607f7b2013-02-08 18:01:04 -0800954 local ARGS=
955 for DIR in $DIRS ; do
956 if [ -d $DIR ]; then
Ying Wangcaeaa082015-09-23 16:08:55 -0700957 # Remove the leading ./ and trailing / if any exists.
958 DIR=${DIR#./}
959 DIR=${DIR%/}
960 if [ "$MY_PWD" != "" ]; then
961 DIR=$MY_PWD/$DIR
Ying Wangb607f7b2013-02-08 18:01:04 -0800962 fi
Ying Wang61cd8842015-09-24 16:19:19 -0700963 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$DIR"
Ying Wangb607f7b2013-02-08 18:01:04 -0800964 else
965 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700966 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800967 *) echo "Couldn't find directory $DIR"; return 1;;
968 esac
969 fi
970 done
Ying Wang61cd8842015-09-24 16:19:19 -0700971 # Convert "/" to "-".
972 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800973 _wrap_build $T/build/soong/soong_ui.bash --make-mode $DASH_ARGS $ARGS $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800974 else
975 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700976 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800977 fi
978}
979
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700980function croot()
981{
Christopher Ferris55257d22017-03-23 11:08:58 -0700982 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700983 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700984 if [ "$1" ]; then
985 \cd $(gettop)/$1
986 else
987 \cd $(gettop)
988 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700989 else
990 echo "Couldn't locate the top of the tree. Try setting TOP."
991 fi
992}
993
Anton Hanssonece9c482019-02-04 18:15:39 +0000994function _croot()
995{
996 local T=$(gettop)
997 if [ "$T" ]; then
998 local cur="${COMP_WORDS[COMP_CWORD]}"
999 k=0
1000 for c in $(compgen -d ${T}/${cur}); do
1001 COMPREPLY[k++]=${c#${T}/}/
1002 done
1003 fi
1004}
1005
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001006function cproj()
1007{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001008 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001009 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001010 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001011 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1012 T=$PWD
1013 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001014 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001015 return
1016 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001017 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001018 done
Ying Wang9cd17642012-12-13 10:52:07 -08001019 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001020 echo "can't find Android.mk"
1021}
1022
Daniel Sandler47e0a882013-07-30 13:23:52 -04001023# simplified version of ps; output in the form
1024# <pid> <procname>
1025function qpid() {
1026 local prepend=''
1027 local append=''
1028 if [ "$1" = "--exact" ]; then
1029 prepend=' '
1030 append='$'
1031 shift
1032 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001033 echo "usage: qpid [[--exact] <process name|pid>"
1034 return 255
1035 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001036
1037 local EXE="$1"
1038 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001039 qpid | \grep "$prepend$EXE$append"
1040 else
1041 adb shell ps \
1042 | tr -d '\r' \
1043 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1044 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001045}
1046
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001047# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001048# that has the core-file-size limit set correctly
1049#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001050# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001051# if its core-file-size limit is not set already.
1052# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1053
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001054function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001055{
Ying Wang08800fd2016-03-03 20:57:21 -08001056 echo "Getting root...";
1057 adb root;
1058 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001059
Ying Wang08800fd2016-03-03 20:57:21 -08001060 echo "Remounting root partition read-write...";
1061 adb shell mount -w -o remount -t rootfs rootfs;
1062 sleep 1;
1063 adb wait-for-device;
1064 adb shell mkdir -p /cores;
1065 adb shell mount -t tmpfs tmpfs /cores;
1066 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001067
Ying Wang08800fd2016-03-03 20:57:21 -08001068 echo "Granting SELinux permission to dump in /cores...";
1069 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001070
Ying Wang08800fd2016-03-03 20:57:21 -08001071 echo "Set core pattern.";
1072 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001073
Ying Wang08800fd2016-03-03 20:57:21 -08001074 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001075}
1076
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001077# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001078# $1 = PID of process (e.g., $(pid mediaserver))
1079#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001080# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001081# dump to actually be generated.
1082
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001083function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001084{
Ying Wang08800fd2016-03-03 20:57:21 -08001085 local PID=$1;
1086 if [ -z "$PID" ]; then
1087 printf "Expecting a PID!\n";
1088 return;
1089 fi;
1090 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001091 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001092}
1093
1094# core - send SIGV and pull the core for process
1095# $1 = PID of process (e.g., $(pid mediaserver))
1096#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001097# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001098# enabled globally.
1099
1100function core()
1101{
Ying Wang08800fd2016-03-03 20:57:21 -08001102 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001103
Ying Wang08800fd2016-03-03 20:57:21 -08001104 if [ -z "$PID" ]; then
1105 printf "Expecting a PID!\n";
1106 return;
1107 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001108
Ying Wang08800fd2016-03-03 20:57:21 -08001109 local CORENAME=core.$PID;
1110 local COREPATH=/cores/$CORENAME;
1111 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001112
Ying Wang08800fd2016-03-03 20:57:21 -08001113 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114
Ying Wang08800fd2016-03-03 20:57:21 -08001115 local done=0;
1116 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1117 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1118 adb shell kill -$SIG $PID;
1119 sleep 1;
1120 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001121
Ying Wang08800fd2016-03-03 20:57:21 -08001122 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1123 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001124}
1125
Christopher Tate744ee802009-11-12 15:33:08 -08001126# systemstack - dump the current stack trace of all threads in the system process
1127# to the usual ANR traces file
1128function systemstack()
1129{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001130 stacks system_server
1131}
1132
Michael Wrightaeed7212014-06-19 19:58:12 -07001133# Read the ELF header from /proc/$PID/exe to determine if the process is
1134# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001135function is64bit()
1136{
1137 local PID="$1"
1138 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -08001139 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001140 echo "64"
1141 else
1142 echo ""
1143 fi
1144 else
1145 echo ""
1146 fi
1147}
1148
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001149case `uname -s` in
1150 Darwin)
1151 function sgrep()
1152 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001153 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 -04001154 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001155 }
1156
1157 ;;
1158 *)
1159 function sgrep()
1160 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001161 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 -04001162 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001163 }
1164 ;;
1165esac
1166
Raghu Gandham8da43102012-07-25 19:57:22 -07001167function gettargetarch
1168{
1169 get_build_var TARGET_ARCH
1170}
1171
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001172function ggrep()
1173{
Mike Frysinger5e479732015-09-22 18:13:48 -04001174 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1175 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001176}
1177
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001178function jgrep()
1179{
Mike Frysinger5e479732015-09-22 18:13:48 -04001180 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1181 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001182}
1183
1184function cgrep()
1185{
Mike Frysinger5e479732015-09-22 18:13:48 -04001186 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
1187 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001188}
1189
1190function resgrep()
1191{
Christopher Ferris55257d22017-03-23 11:08:58 -07001192 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001193 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1194 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1195 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001196}
1197
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001198function mangrep()
1199{
Mike Frysinger5e479732015-09-22 18:13:48 -04001200 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1201 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001202}
1203
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001204function sepgrep()
1205{
Mike Frysinger5e479732015-09-22 18:13:48 -04001206 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1207 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001208}
1209
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001210function rcgrep()
1211{
Mike Frysinger5e479732015-09-22 18:13:48 -04001212 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1213 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001214}
1215
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001216case `uname -s` in
1217 Darwin)
1218 function mgrep()
1219 {
David Grossd1d6fc52017-05-10 10:49:44 -07001220 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 -04001221 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001222 }
1223
1224 function treegrep()
1225 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001226 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 -04001227 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001228 }
1229
1230 ;;
1231 *)
1232 function mgrep()
1233 {
David Grossd1d6fc52017-05-10 10:49:44 -07001234 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 -04001235 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001236 }
1237
1238 function treegrep()
1239 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001240 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 -04001241 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001242 }
1243
1244 ;;
1245esac
1246
1247function getprebuilt
1248{
1249 get_abs_build_var ANDROID_PREBUILTS
1250}
1251
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001252function tracedmdump()
1253{
Christopher Ferris55257d22017-03-23 11:08:58 -07001254 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001255 if [ ! "$T" ]; then
1256 echo "Couldn't locate the top of the tree. Try setting TOP."
1257 return
1258 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001259 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001260 local arch=$(gettargetarch)
1261 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001262
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001263 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001264 if [ ! "$TRACE" ] ; then
1265 echo "usage: tracedmdump tracename"
1266 return
1267 fi
1268
Jack Veenstra60116fc2009-04-09 18:12:34 -07001269 if [ ! -r "$KERNEL" ] ; then
1270 echo "Error: cannot find kernel: '$KERNEL'"
1271 return
1272 fi
1273
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001274 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001275 if [ "$BASETRACE" = "$TRACE" ] ; then
1276 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1277 fi
1278
1279 echo "post-processing traces..."
1280 rm -f $TRACE/qtrace.dexlist
1281 post_trace $TRACE
1282 if [ $? -ne 0 ]; then
1283 echo "***"
1284 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1285 echo "***"
1286 return
1287 fi
1288 echo "generating dexlist output..."
1289 /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
1290 echo "generating dmtrace data..."
1291 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1292 echo "generating html file..."
1293 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1294 echo "done, see $TRACE/dmtrace.html for details"
1295 echo "or run:"
1296 echo " traceview $TRACE/dmtrace"
1297}
1298
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001299# communicate with a running device or emulator, set up necessary state,
1300# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301function runhat()
1302{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001303 # process standard adb options
1304 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001305 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001306 adbTarget=$1
1307 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001308 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001309 adbTarget="$1 $2"
1310 shift 2
1311 fi
1312 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001313 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001314
1315 # runhat options
1316 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001317
1318 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001319 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001320 return
1321 fi
1322
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001323 # confirm hat is available
1324 if [ -z $(which hat) ]; then
1325 echo "hat is not available in this configuration."
1326 return
1327 fi
1328
Andy McFaddenb6289852010-07-12 08:00:19 -07001329 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001330 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001331 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001332 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001333 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001334 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001335 echo -n "> "
1336 read
1337
The Android Open Source Project88b60792009-03-03 19:28:42 -08001338 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001339
The Android Open Source Project88b60792009-03-03 19:28:42 -08001340 echo "Retrieving file $devFile..."
1341 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001342
The Android Open Source Project88b60792009-03-03 19:28:42 -08001343 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001344
The Android Open Source Project88b60792009-03-03 19:28:42 -08001345 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001346 echo "View the output by pointing your browser at http://localhost:7000/"
1347 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001348 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001349}
1350
1351function getbugreports()
1352{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001353 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001354
1355 if [ ! "$reports" ]; then
1356 echo "Could not locate any bugreports."
1357 return
1358 fi
1359
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001360 local report
1361 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001362 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001363 echo "/sdcard/bugreports/${report}"
1364 adb pull /sdcard/bugreports/${report} ${report}
1365 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001366 done
1367}
1368
Victoria Lease1b296b42012-08-21 15:44:06 -07001369function getsdcardpath()
1370{
1371 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1372}
1373
1374function getscreenshotpath()
1375{
1376 echo "$(getsdcardpath)/Pictures/Screenshots"
1377}
1378
1379function getlastscreenshot()
1380{
1381 local screenshot_path=$(getscreenshotpath)
1382 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1383 if [ "$screenshot" = "" ]; then
1384 echo "No screenshots found."
1385 return
1386 fi
1387 echo "${screenshot}"
1388 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1389}
1390
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001391function startviewserver()
1392{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001393 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001394 if [ $# -gt 0 ]; then
1395 port=$1
1396 fi
1397 adb shell service call window 1 i32 $port
1398}
1399
1400function stopviewserver()
1401{
1402 adb shell service call window 2
1403}
1404
1405function isviewserverstarted()
1406{
1407 adb shell service call window 3
1408}
1409
Romain Guyb84049a2010-10-04 16:56:11 -07001410function key_home()
1411{
1412 adb shell input keyevent 3
1413}
1414
1415function key_back()
1416{
1417 adb shell input keyevent 4
1418}
1419
1420function key_menu()
1421{
1422 adb shell input keyevent 82
1423}
1424
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001425function smoketest()
1426{
1427 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1428 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1429 return
1430 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001431 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001432 if [ ! "$T" ]; then
1433 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1434 return
1435 fi
1436
Ying Wang9cd17642012-12-13 10:52:07 -08001437 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001438 adb uninstall com.android.smoketest > /dev/null &&
1439 adb uninstall com.android.smoketest.tests > /dev/null &&
1440 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1441 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1442 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1443}
1444
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001445# simple shortcut to the runtest command
1446function runtest()
1447{
Christopher Ferris55257d22017-03-23 11:08:58 -07001448 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001449 if [ ! "$T" ]; then
1450 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1451 return
1452 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001453 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001454}
1455
The Android Open Source Project88b60792009-03-03 19:28:42 -08001456function godir () {
1457 if [[ -z "$1" ]]; then
1458 echo "Usage: godir <regex>"
1459 return
1460 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001461 local T=$(gettop)
1462 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001463 if [ ! "$OUT_DIR" = "" ]; then
1464 mkdir -p $OUT_DIR
1465 FILELIST=$OUT_DIR/filelist
1466 else
1467 FILELIST=$T/filelist
1468 fi
1469 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001470 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001471 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001472 echo " Done"
1473 echo ""
1474 fi
1475 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001476 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001477 if [[ ${#lines[@]} = 0 ]]; then
1478 echo "Not found"
1479 return
1480 fi
1481 local pathname
1482 local choice
1483 if [[ ${#lines[@]} > 1 ]]; then
1484 while [[ -z "$pathname" ]]; do
1485 local index=1
1486 local line
1487 for line in ${lines[@]}; do
1488 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001489 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001490 done
1491 echo
1492 echo -n "Select one: "
1493 unset choice
1494 read choice
1495 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1496 echo "Invalid choice"
1497 continue
1498 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001499 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001500 done
1501 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001502 pathname=${lines[0]}
1503 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001504 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001505}
1506
Steven Moreland62054a42018-12-06 10:11:40 -08001507# Update module-info.json in out.
1508function refreshmod() {
1509 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1510 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1511 return 1
1512 fi
1513
1514 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1515
1516 # for the output of the next command
1517 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1518
1519 # Note, can't use absolute path because of the way make works.
1520 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1521 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1522}
1523
1524# List all modules for the current device, as cached in module-info.json. If any build change is
1525# made and it should be reflected in the output, you should run 'refreshmod' first.
1526function allmod() {
1527 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1528 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1529 return 1
1530 fi
1531
1532 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1533 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1534 refreshmod || return 1
1535 fi
1536
1537 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1538}
1539
Rett Berg78d1c932019-01-24 14:34:23 -08001540# 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 -08001541# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001542function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001543 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1544 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1545 return 1
1546 fi
1547
1548 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001549 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001550 return 1
1551 fi
1552
1553 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1554 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1555 refreshmod || return 1
1556 fi
1557
1558 local relpath=$(python -c "import json, os
1559module = '$1'
1560module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1561if module not in module_info:
1562 exit(1)
1563print module_info[module]['path'][0]" 2>/dev/null)
1564
1565 if [ -z "$relpath" ]; then
1566 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1567 return 1
1568 else
Rett Berg78d1c932019-01-24 14:34:23 -08001569 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001570 fi
1571}
1572
Rett Berg78d1c932019-01-24 14:34:23 -08001573# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1574# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1575function gomod() {
1576 if [[ $# -ne 1 ]]; then
1577 echo "usage: gomod <module>" >&2
1578 return 1
1579 fi
1580
1581 local path="$(pathmod $@)"
1582 if [ -z "$path" ]; then
1583 return 1
1584 fi
1585 cd $path
1586}
1587
dimitry73b84812018-12-11 18:06:00 +01001588function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001589 local word=${COMP_WORDS[COMP_CWORD]}
1590 COMPREPLY=( $(allmod | grep -E "^$word") )
1591}
1592
Alex Rayf0d08eb2013-03-08 15:15:06 -08001593# Print colored exit condition
1594function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001595 "$@"
1596 local retval=$?
1597 if [ $retval -ne 0 ]
1598 then
Jacky Cao89483b82015-05-15 22:12:53 +08001599 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001600 else
Jacky Cao89483b82015-05-15 22:12:53 +08001601 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001602 fi
1603 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001604}
1605
Ying Wanged21d4c2014-08-24 22:14:19 -07001606function get_make_command()
1607{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001608 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1609 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001610 # Always use the real make if -C is passed in
1611 for arg in "$@"; do
1612 if [[ $arg == -C* ]]; then
1613 echo command make
1614 return
1615 fi
1616 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001617 echo build/soong/soong_ui.bash --make-mode
1618 else
1619 echo command make
1620 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001621}
1622
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001623function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001624{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001625 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1626 "$@"
1627 return $?
1628 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001629 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001630 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001631 local ret=$?
1632 local end_time=$(date +"%s")
1633 local tdiff=$(($end_time-$start_time))
1634 local hours=$(($tdiff / 3600 ))
1635 local mins=$((($tdiff % 3600) / 60))
1636 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001637 local ncolors=$(tput colors 2>/dev/null)
1638 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001639 color_failed=$'\E'"[0;31m"
1640 color_success=$'\E'"[0;32m"
1641 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001642 else
1643 color_failed=""
1644 color_success=""
1645 color_reset=""
1646 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001647 echo
1648 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001649 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001650 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001651 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001652 fi
1653 if [ $hours -gt 0 ] ; then
1654 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1655 elif [ $mins -gt 0 ] ; then
1656 printf "(%02g:%02g (mm:ss))" $mins $secs
1657 elif [ $secs -gt 0 ] ; then
1658 printf "(%s seconds)" $secs
1659 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001660 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001661 echo
1662 return $ret
1663}
1664
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001665function make()
1666{
Dan Willemsene9842242017-07-28 13:00:13 -07001667 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001668}
1669
David Zeuthen1b126ff2015-09-30 17:10:48 -04001670function provision()
1671{
1672 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1673 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1674 return 1
1675 fi
1676 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1677 echo "There is no provisioning script for the device." >&2
1678 return 1
1679 fi
1680
1681 # Check if user really wants to do this.
1682 if [ "$1" = "--no-confirmation" ]; then
1683 shift 1
1684 else
1685 echo "This action will reflash your device."
1686 echo ""
1687 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1688 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001689 echo -n "Are you sure you want to do this (yes/no)? "
1690 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001691 if [[ "${REPLY}" != "yes" ]] ; then
1692 echo "Not taking any action. Exiting." >&2
1693 return 1
1694 fi
1695 fi
1696 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1697}
1698
Jim Tanga881a252018-06-19 16:34:41 +08001699# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001700function enable_zsh_completion() {
1701 # Don't override user's options if bash-style completion is already enabled.
1702 if ! declare -f complete >/dev/null; then
1703 autoload -U compinit && compinit
1704 autoload -U bashcompinit && bashcompinit
1705 fi
Jim Tanga881a252018-06-19 16:34:41 +08001706}
1707
1708function validate_current_shell() {
1709 local current_sh="$(ps -o command -p $$)"
1710 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001711 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001712 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001713 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001714 *zsh*)
1715 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001716 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001717 *)
Jim Tanga881a252018-06-19 16:34:41 +08001718 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 -07001719 ;;
1720 esac
Jim Tanga881a252018-06-19 16:34:41 +08001721}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001722
1723# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001724# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1725# load those.
1726#
1727# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001728function source_vendorsetup() {
Dan Willemsend855a722019-02-12 15:52:36 -08001729 allowed=
1730 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1731 if [ -n "$allowed" ]; then
1732 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1733 echo " $allowed"
1734 echo " $f"
1735 return
1736 fi
1737 allowed="$f"
1738 done
1739
1740 allowed_files=
1741 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001742 for dir in device vendor product; do
1743 for f in $(test -d $dir && \
1744 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001745
1746 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1747 echo "including $f"; . "$f"
1748 else
1749 echo "ignoring $f, not in $allowed"
1750 fi
Jim Tanga881a252018-06-19 16:34:41 +08001751 done
1752 done
1753}
Kenny Root52aa81c2011-07-15 11:07:06 -07001754
Jim Tanga881a252018-06-19 16:34:41 +08001755validate_current_shell
1756source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001757addcompletions