blob: 5d2e550dee5cfa00063d0384d79ed954edb9b284 [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
Ying Wangaa1c9b52012-11-26 20:51:59 -0800270 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800271
272 # out with the duplicate old
273 if [ -n $ANDROID_PYTHONPATH ]; then
274 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
275 fi
276 # and in with the new
277 export ANDROID_PYTHONPATH=$T/development/python-packages:
278 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800279
Colin Crosse97e6932017-06-30 16:01:45 -0700280 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
281 export JAVA_HOME=$ANDROID_JAVA_HOME
282 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
283 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
284 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500285
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800286 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700287 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
288 export OUT=$ANDROID_PRODUCT_OUT
289
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700290 unset ANDROID_HOST_OUT
291 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
292
Simran Basidd050ed2017-02-13 13:46:48 -0800293 unset ANDROID_HOST_OUT_TESTCASES
294 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
295
296 unset ANDROID_TARGET_OUT_TESTCASES
297 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
298
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800299 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700300 # TODO: fix the path
301 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
302}
303
304function printconfig()
305{
Christopher Ferris55257d22017-03-23 11:08:58 -0700306 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800307 if [ ! "$T" ]; then
308 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
309 return
310 fi
311 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700312}
313
314function set_stuff_for_environment()
315{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800316 setpaths
317 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700318
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800319 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700320 # With this environment variable new GCC can apply colors to warnings/errors
321 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 -0700322 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700323}
324
325function set_sequence_number()
326{
Colin Cross88737132017-03-21 17:41:03 -0700327 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328}
329
Makoto Onukida971062018-06-18 10:15:19 -0700330# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
331function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800332 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700333 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800334 *:"$cmd":*)
335 return 1
336 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700337 esac
338 return 0
339}
340
Kenny Root52aa81c2011-07-15 11:07:06 -0700341function addcompletions()
342{
343 local T dir f
344
Jim Tanga881a252018-06-19 16:34:41 +0800345 # Keep us from trying to run in something that's neither bash nor zsh.
346 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700347 return
348 fi
349
350 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800351 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700352 return
353 fi
354
Jim Tanga881a252018-06-19 16:34:41 +0800355 local completion_files=(
356 system/core/adb/adb.bash
357 system/core/fastboot/fastboot.bash
358 tools/tradefederation/core/atest/atest_completion.sh
359 )
Makoto Onukida971062018-06-18 10:15:19 -0700360 # Completion can be disabled selectively to allow users to use non-standard completion.
361 # e.g.
362 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
363 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800364 for f in ${completion_files[*]}; do
365 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700366 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700367 fi
368 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700369
Makoto Onukida971062018-06-18 10:15:19 -0700370 if should_add_completion bit ; then
371 complete -C "bit --tab" bit
372 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000373 if [ -z "$ZSH_VERSION" ]; then
374 # Doesn't work in zsh.
375 complete -o nospace -F _croot croot
376 fi
Jim Tanga881a252018-06-19 16:34:41 +0800377 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800378
dimitry73b84812018-12-11 18:06:00 +0100379 complete -F _complete_android_module_names gomod
380 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700381}
382
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700383function choosetype()
384{
385 echo "Build type choices are:"
386 echo " 1. release"
387 echo " 2. debug"
388 echo
389
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800390 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700391 DEFAULT_NUM=1
392 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700393
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800394 export TARGET_BUILD_TYPE=
395 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700396 while [ -z $TARGET_BUILD_TYPE ]
397 do
398 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800399 if [ -z "$1" ] ; then
400 read ANSWER
401 else
402 echo $1
403 ANSWER=$1
404 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700405 case $ANSWER in
406 "")
407 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
408 ;;
409 1)
410 export TARGET_BUILD_TYPE=release
411 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800412 release)
413 export TARGET_BUILD_TYPE=release
414 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700415 2)
416 export TARGET_BUILD_TYPE=debug
417 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800418 debug)
419 export TARGET_BUILD_TYPE=debug
420 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700421 *)
422 echo
423 echo "I didn't understand your response. Please try again."
424 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425 ;;
426 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800427 if [ -n "$1" ] ; then
428 break
429 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700430 done
431
Ying Wang08800fd2016-03-03 20:57:21 -0800432 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700433 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800434 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700435}
436
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800437#
438# This function isn't really right: It chooses a TARGET_PRODUCT
439# based on the list of boards. Usually, that gets you something
440# that kinda works with a generic product, but really, you should
441# pick a product by name.
442#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700443function chooseproduct()
444{
Christopher Ferris55257d22017-03-23 11:08:58 -0700445 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700446 if [ "x$TARGET_PRODUCT" != x ] ; then
447 default_value=$TARGET_PRODUCT
448 else
Ying Wang0a76df52015-06-08 11:57:26 -0700449 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450 fi
451
Ying Wang08800fd2016-03-03 20:57:21 -0800452 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800453 export TARGET_PRODUCT=
454 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700455 while [ -z "$TARGET_PRODUCT" ]
456 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700457 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800458 if [ -z "$1" ] ; then
459 read ANSWER
460 else
461 echo $1
462 ANSWER=$1
463 fi
464
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 if [ -z "$ANSWER" ] ; then
466 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800467 else
468 if check_product $ANSWER
469 then
470 export TARGET_PRODUCT=$ANSWER
471 else
472 echo "** Not a valid product: $ANSWER"
473 fi
474 fi
475 if [ -n "$1" ] ; then
476 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700477 fi
478 done
479
Ying Wang08800fd2016-03-03 20:57:21 -0800480 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800482 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700483}
484
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800485function choosevariant()
486{
487 echo "Variant choices are:"
488 local index=1
489 local v
490 for v in ${VARIANT_CHOICES[@]}
491 do
492 # The product name is the name of the directory containing
493 # the makefile we found, above.
494 echo " $index. $v"
495 index=$(($index+1))
496 done
497
498 local default_value=eng
499 local ANSWER
500
501 export TARGET_BUILD_VARIANT=
502 while [ -z "$TARGET_BUILD_VARIANT" ]
503 do
504 echo -n "Which would you like? [$default_value] "
505 if [ -z "$1" ] ; then
506 read ANSWER
507 else
508 echo $1
509 ANSWER=$1
510 fi
511
512 if [ -z "$ANSWER" ] ; then
513 export TARGET_BUILD_VARIANT=$default_value
514 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
515 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800516 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800517 fi
518 else
519 if check_variant $ANSWER
520 then
521 export TARGET_BUILD_VARIANT=$ANSWER
522 else
523 echo "** Not a valid variant: $ANSWER"
524 fi
525 fi
526 if [ -n "$1" ] ; then
527 break
528 fi
529 done
530}
531
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700532function choosecombo()
533{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700534 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700535
536 echo
537 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700538 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700539
540 echo
541 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700542 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800543
544 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800545 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700546 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800547 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800548 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700549}
550
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800551function add_lunch_combo()
552{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800553 if [ -n "$ZSH_VERSION" ]; then
554 echo -n "${funcfiletrace[1]}: "
555 else
556 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
557 fi
558 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 -0800559}
560
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700561function print_lunch_menu()
562{
563 local uname=$(uname)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700564 echo
565 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700566 echo
567 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800568
569 local i=1
570 local choice
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800571 for choice in $(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800572 do
573 echo " $i. $choice"
574 i=$(($i+1))
575 done
576
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700577 echo
578}
579
580function lunch()
581{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800582 local answer
583
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700584 if [ "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800585 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700586 else
587 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700588 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800589 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700590 fi
591
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800592 local selection=
593
594 if [ -z "$answer" ]
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700595 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700596 selection=aosp_arm-eng
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800597 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
598 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800599 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700600 if [ $answer -le ${#choices[@]} ]
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800601 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800602 # array in zsh starts from 1 instead of 0.
603 if [ -n "$ZSH_VERSION" ]
604 then
605 selection=${choices[$(($answer))]}
606 else
607 selection=${choices[$(($answer-1))]}
608 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800609 fi
Colin Cross88737132017-03-21 17:41:03 -0700610 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800611 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700612 fi
613
Joe Onoratoda12daf2010-06-09 18:18:31 -0700614 export TARGET_BUILD_APPS=
615
Colin Cross88737132017-03-21 17:41:03 -0700616 local product variant_and_version variant version
617
618 product=${selection%%-*} # Trim everything after first dash
619 variant_and_version=${selection#*-} # Trim everything up to first dash
620 if [ "$variant_and_version" != "$selection" ]; then
621 variant=${variant_and_version%%-*}
622 if [ "$variant" != "$variant_and_version" ]; then
623 version=${variant_and_version#*-}
624 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700625 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800626
Colin Cross88737132017-03-21 17:41:03 -0700627 if [ -z "$product" ]
Ying Wang08800fd2016-03-03 20:57:21 -0800628 then
629 echo
Colin Cross88737132017-03-21 17:41:03 -0700630 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700631 return 1
632 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800633
Colin Cross88737132017-03-21 17:41:03 -0700634 TARGET_PRODUCT=$product \
635 TARGET_BUILD_VARIANT=$variant \
636 TARGET_PLATFORM_VERSION=$version \
637 build_build_var_cache
638 if [ $? -ne 0 ]
639 then
640 return 1
641 fi
642
643 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
644 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700645 if [ -n "$version" ]; then
646 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
647 else
648 unset TARGET_PLATFORM_VERSION
649 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700650 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700651
652 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800653
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700654 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800655 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800656 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700657}
658
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700659unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700660# Tab completion for lunch.
661function _lunch()
662{
663 local cur prev opts
664 COMPREPLY=()
665 cur="${COMP_WORDS[COMP_CWORD]}"
666 prev="${COMP_WORDS[COMP_CWORD-1]}"
667
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700668 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800669 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700670 fi
671
672 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700673 return 0
674}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700675
Joe Onoratoda12daf2010-06-09 18:18:31 -0700676# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700677# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Joe Onoratoda12daf2010-06-09 18:18:31 -0700678function tapas()
679{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700680 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Dan Willemsendd3a2732018-01-08 15:26:16 -0800681 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|arm64|x86_64|mips64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700682 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700683 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 -0800684 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 -0700685
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700686 if [ "$showHelp" != "" ]; then
687 $(gettop)/build/make/tapasHelp.sh
688 return
689 fi
690
Ying Wang67f02922012-08-22 10:25:20 -0700691 if [ $(echo $arch | wc -w) -gt 1 ]; then
692 echo "tapas: Error: Multiple build archs supplied: $arch"
693 return
694 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700695 if [ $(echo $variant | wc -w) -gt 1 ]; then
696 echo "tapas: Error: Multiple build variants supplied: $variant"
697 return
698 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700699 if [ $(echo $density | wc -w) -gt 1 ]; then
700 echo "tapas: Error: Multiple densities supplied: $density"
701 return
702 fi
Ying Wang67f02922012-08-22 10:25:20 -0700703
Ying Wang0a76df52015-06-08 11:57:26 -0700704 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700705 case $arch in
Ying Wang0a76df52015-06-08 11:57:26 -0700706 x86) product=aosp_x86;;
707 mips) product=aosp_mips;;
Ying Wangb541ab62014-05-29 17:57:40 -0700708 arm64) product=aosp_arm64;;
709 x86_64) product=aosp_x86_64;;
710 mips64) product=aosp_mips64;;
Ying Wang67f02922012-08-22 10:25:20 -0700711 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700712 if [ -z "$variant" ]; then
713 variant=eng
714 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700715 if [ -z "$apps" ]; then
716 apps=all
717 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600718 if [ -z "$density" ]; then
719 density=alldpi
720 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700721
Ying Wang67f02922012-08-22 10:25:20 -0700722 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700723 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700724 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700725 export TARGET_BUILD_TYPE=release
726 export TARGET_BUILD_APPS=$apps
727
Ying Wang08800fd2016-03-03 20:57:21 -0800728 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700729 set_stuff_for_environment
730 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800731 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700732}
733
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700734function gettop
735{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700736 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700737 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700738 # The following circumlocution ensures we remove symlinks from TOP.
739 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700740 else
741 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800742 # The following circumlocution (repeated below as well) ensures
743 # that we record the true directory name and not one that is
744 # faked up with symlink names.
745 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700746 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800747 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700748 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700749 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800750 \cd ..
synergyb112a402013-07-05 19:47:47 -0700751 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700752 done
Ying Wang9cd17642012-12-13 10:52:07 -0800753 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700754 if [ -f "$T/$TOPFILE" ]; then
755 echo $T
756 fi
757 fi
758 fi
759}
760
761function m()
762{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800763 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700764 if [ "$T" ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800765 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700766 else
767 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700768 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700769 fi
770}
771
772function findmakefile()
773{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700774 local TOPFILE=build/make/core/envsetup.mk
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800775 local HERE=$PWD
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800776 if [ "$1" ]; then
777 \cd $1
778 fi;
Christopher Ferris55257d22017-03-23 11:08:58 -0700779 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700780 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang11b15b12012-10-11 15:05:07 -0700781 T=`PWD= /bin/pwd`
Colin Cross86425252016-05-26 15:32:15 -0700782 if [ -f "$T/Android.mk" -o -f "$T/Android.bp" ]; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 echo $T/Android.mk
Ying Wang9cd17642012-12-13 10:52:07 -0800784 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700785 return
786 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800787 \cd ..
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 done
Ying Wang9cd17642012-12-13 10:52:07 -0800789 \cd $HERE
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700790 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700791}
792
793function mm()
794{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800795 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700796 # If we're sitting in the root of the build tree, just do a
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700797 # normal build.
798 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800799 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700800 else
801 # Find the closest Android.mk file.
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800802 local M=$(findmakefile)
Ying Wanga7deb082013-08-16 13:24:47 -0700803 local MODULES=
804 local GET_INSTALL_PATH=
805 local ARGS=
Robert Greenwalt3c794d72009-07-15 15:07:44 -0700806 # Remove the path to top as the makefilepath needs to be relative
807 local M=`echo $M|sed 's:'$T'/::'`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700808 if [ ! "$T" ]; then
809 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700810 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700811 elif [ ! "$M" ]; then
812 echo "Couldn't locate a makefile from the current directory."
Ying Wang0c1374c2015-04-01 10:13:02 -0700813 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700814 else
Christopher Ferris55257d22017-03-23 11:08:58 -0700815 local ARG
Ying Wanga7deb082013-08-16 13:24:47 -0700816 for ARG in $@; do
817 case $ARG in
818 GET-INSTALL-PATH) GET_INSTALL_PATH=$ARG;;
819 esac
820 done
821 if [ -n "$GET_INSTALL_PATH" ]; then
822 MODULES=
Dan Willemsen53e38992016-08-11 17:20:33 -0700823 ARGS=GET-INSTALL-PATH-IN-$(dirname ${M})
824 ARGS=${ARGS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700825 else
Colin Cross86425252016-05-26 15:32:15 -0700826 MODULES=MODULES-IN-$(dirname ${M})
827 # Convert "/" to "-".
828 MODULES=${MODULES//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700829 ARGS=$@
830 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700831 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
832 MODULES=tidy_only
833 fi
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800834 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 -0700835 fi
836 fi
837}
838
839function mmm()
840{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800841 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700842 if [ "$T" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800843 local MAKEFILE=
Alon Albert68895a92011-11-30 12:40:19 -0800844 local MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700845 local MODULES_IN_PATHS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800846 local ARGS=
847 local DIR TO_CHOP
Colin Cross86425252016-05-26 15:32:15 -0700848 local DIR_MODULES
Ying Wanga7deb082013-08-16 13:24:47 -0700849 local GET_INSTALL_PATH=
Dan Willemsen53e38992016-08-11 17:20:33 -0700850 local GET_INSTALL_PATHS=
The Android Open Source Project88b60792009-03-03 19:28:42 -0800851 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
852 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
853 for DIR in $DIRS ; do
Colin Cross86425252016-05-26 15:32:15 -0700854 DIR_MODULES=`echo $DIR | sed -n -e 's/.*:\(.*$\)/\1/p' | sed 's/,/ /'`
Alon Albert68895a92011-11-30 12:40:19 -0800855 DIR=`echo $DIR | sed -e 's/:.*//' -e 's:/$::'`
Colin Cross86425252016-05-26 15:32:15 -0700856 # Remove the leading ./ and trailing / if any exists.
857 DIR=${DIR#./}
858 DIR=${DIR%/}
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800859 local M
860 if [ "$DIR_MODULES" = "" ]; then
861 M=$(findmakefile $DIR)
862 else
863 # Only check the target directory if a module is specified.
864 if [ -f $DIR/Android.mk -o -f $DIR/Android.bp ]; then
865 local HERE=$PWD
866 cd $DIR
867 M=`PWD= /bin/pwd`
868 M=$M/Android.mk
869 cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700870 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800871 fi
872 if [ "$M" ]; then
873 # Remove the path to top as the makefilepath needs to be relative
874 local M=`echo $M|sed 's:'$T'/::'`
Colin Cross4bfae062016-08-31 17:22:36 -0700875 if [ "$DIR_MODULES" = "" ]; then
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800876 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$(dirname ${M})"
877 GET_INSTALL_PATHS="$GET_INSTALL_PATHS GET-INSTALL-PATH-IN-$(dirname ${M})"
Colin Cross4bfae062016-08-31 17:22:36 -0700878 else
879 MODULES="$MODULES $DIR_MODULES"
880 fi
Jaewoong Jungb6112cd2019-01-02 13:36:48 -0800881 MAKEFILE="$MAKEFILE $M"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700882 else
Ying Wanga7deb082013-08-16 13:24:47 -0700883 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700884 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wanga7deb082013-08-16 13:24:47 -0700885 GET-INSTALL-PATH) GET_INSTALL_PATH=$DIR;;
Abhinav1997a72a6e72015-10-18 20:25:48 +0200886 *) if [ -d $DIR ]; then
887 echo "No Android.mk in $DIR.";
888 else
889 echo "Couldn't locate the directory $DIR";
890 fi
891 return 1;;
Ying Wanga7deb082013-08-16 13:24:47 -0700892 esac
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700893 fi
894 done
Ying Wanga7deb082013-08-16 13:24:47 -0700895 if [ -n "$GET_INSTALL_PATH" ]; then
Dan Willemsen53e38992016-08-11 17:20:33 -0700896 ARGS=${GET_INSTALL_PATHS//\//-}
Ying Wanga7deb082013-08-16 13:24:47 -0700897 MODULES=
Colin Cross86425252016-05-26 15:32:15 -0700898 MODULES_IN_PATHS=
Ying Wanga7deb082013-08-16 13:24:47 -0700899 fi
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700900 if [ "1" = "${WITH_TIDY_ONLY}" -o "true" = "${WITH_TIDY_ONLY}" ]; then
901 MODULES=tidy_only
Colin Cross86425252016-05-26 15:32:15 -0700902 MODULES_IN_PATHS=
Chih-Hung Hsieha9a55c72016-03-31 16:30:23 -0700903 fi
Colin Cross86425252016-05-26 15:32:15 -0700904 # Convert "/" to "-".
905 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800906 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 -0700907 else
908 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700909 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700910 fi
911}
912
Ying Wangb607f7b2013-02-08 18:01:04 -0800913function mma()
914{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800915 local T=$(gettop)
Dan Willemsend41ec5a2017-07-12 16:14:50 -0700916 if [ -f build/soong/soong_ui.bash ]; then
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800917 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@
Ying Wangb607f7b2013-02-08 18:01:04 -0800918 else
Ying Wangb607f7b2013-02-08 18:01:04 -0800919 if [ ! "$T" ]; then
920 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700921 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800922 fi
Steven Moreland3b99ecf2018-06-13 15:31:59 -0700923 local M=$(findmakefile || echo $(realpath $PWD)/Android.mk)
Colin Cross127fcea2016-09-01 15:30:18 -0700924 # Remove the path to top as the makefilepath needs to be relative
925 local M=`echo $M|sed 's:'$T'/::'`
926 local MODULES_IN_PATHS=MODULES-IN-$(dirname ${M})
Ying Wang61cd8842015-09-24 16:19:19 -0700927 # Convert "/" to "-".
928 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800929 _wrap_build $T/build/soong/soong_ui.bash --make-mode $@ $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800930 fi
931}
932
933function mmma()
934{
Andrew Hsieh906cb782013-09-10 17:37:14 +0800935 local T=$(gettop)
Ying Wangb607f7b2013-02-08 18:01:04 -0800936 if [ "$T" ]; then
937 local DASH_ARGS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^-.*$/')
938 local DIRS=$(echo "$@" | awk -v RS=" " -v ORS=" " '/^[^-].*$/')
939 local MY_PWD=`PWD= /bin/pwd`
940 if [ "$MY_PWD" = "$T" ]; then
941 MY_PWD=
942 else
943 MY_PWD=`echo $MY_PWD|sed 's:'$T'/::'`
944 fi
945 local DIR=
Ying Wangcaeaa082015-09-23 16:08:55 -0700946 local MODULES_IN_PATHS=
Ying Wangb607f7b2013-02-08 18:01:04 -0800947 local ARGS=
948 for DIR in $DIRS ; do
949 if [ -d $DIR ]; then
Ying Wangcaeaa082015-09-23 16:08:55 -0700950 # Remove the leading ./ and trailing / if any exists.
951 DIR=${DIR#./}
952 DIR=${DIR%/}
953 if [ "$MY_PWD" != "" ]; then
954 DIR=$MY_PWD/$DIR
Ying Wangb607f7b2013-02-08 18:01:04 -0800955 fi
Ying Wang61cd8842015-09-24 16:19:19 -0700956 MODULES_IN_PATHS="$MODULES_IN_PATHS MODULES-IN-$DIR"
Ying Wangb607f7b2013-02-08 18:01:04 -0800957 else
958 case $DIR in
Ying Wangcaeaa082015-09-23 16:08:55 -0700959 showcommands | snod | dist | *=*) ARGS="$ARGS $DIR";;
Ying Wangb607f7b2013-02-08 18:01:04 -0800960 *) echo "Couldn't find directory $DIR"; return 1;;
961 esac
962 fi
963 done
Ying Wang61cd8842015-09-24 16:19:19 -0700964 # Convert "/" to "-".
965 MODULES_IN_PATHS=${MODULES_IN_PATHS//\//-}
Chih-Hung Hsieh7ed0db82018-02-15 11:20:04 -0800966 _wrap_build $T/build/soong/soong_ui.bash --make-mode $DASH_ARGS $ARGS $MODULES_IN_PATHS
Ying Wangb607f7b2013-02-08 18:01:04 -0800967 else
968 echo "Couldn't locate the top of the tree. Try setting TOP."
Ying Wang0c1374c2015-04-01 10:13:02 -0700969 return 1
Ying Wangb607f7b2013-02-08 18:01:04 -0800970 fi
971}
972
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700973function croot()
974{
Christopher Ferris55257d22017-03-23 11:08:58 -0700975 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700976 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700977 if [ "$1" ]; then
978 \cd $(gettop)/$1
979 else
980 \cd $(gettop)
981 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700982 else
983 echo "Couldn't locate the top of the tree. Try setting TOP."
984 fi
985}
986
Anton Hanssonece9c482019-02-04 18:15:39 +0000987function _croot()
988{
989 local T=$(gettop)
990 if [ "$T" ]; then
991 local cur="${COMP_WORDS[COMP_CWORD]}"
992 k=0
993 for c in $(compgen -d ${T}/${cur}); do
994 COMPREPLY[k++]=${c#${T}/}/
995 done
996 fi
997}
998
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700999function cproj()
1000{
Colin Cross6cdc5d22017-10-20 11:37:33 -07001001 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001002 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -07001003 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001004 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
1005 T=$PWD
1006 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -08001007 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001008 return
1009 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001010 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001011 done
Ying Wang9cd17642012-12-13 10:52:07 -08001012 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001013 echo "can't find Android.mk"
1014}
1015
Daniel Sandler47e0a882013-07-30 13:23:52 -04001016# simplified version of ps; output in the form
1017# <pid> <procname>
1018function qpid() {
1019 local prepend=''
1020 local append=''
1021 if [ "$1" = "--exact" ]; then
1022 prepend=' '
1023 append='$'
1024 shift
1025 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001026 echo "usage: qpid [[--exact] <process name|pid>"
1027 return 255
1028 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001029
1030 local EXE="$1"
1031 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001032 qpid | \grep "$prepend$EXE$append"
1033 else
1034 adb shell ps \
1035 | tr -d '\r' \
1036 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1037 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001038}
1039
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001040# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001041# that has the core-file-size limit set correctly
1042#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001043# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001044# if its core-file-size limit is not set already.
1045# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1046
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001047function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001048{
Ying Wang08800fd2016-03-03 20:57:21 -08001049 echo "Getting root...";
1050 adb root;
1051 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001052
Ying Wang08800fd2016-03-03 20:57:21 -08001053 echo "Remounting root partition read-write...";
1054 adb shell mount -w -o remount -t rootfs rootfs;
1055 sleep 1;
1056 adb wait-for-device;
1057 adb shell mkdir -p /cores;
1058 adb shell mount -t tmpfs tmpfs /cores;
1059 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001060
Ying Wang08800fd2016-03-03 20:57:21 -08001061 echo "Granting SELinux permission to dump in /cores...";
1062 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001063
Ying Wang08800fd2016-03-03 20:57:21 -08001064 echo "Set core pattern.";
1065 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001066
Ying Wang08800fd2016-03-03 20:57:21 -08001067 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001068}
1069
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001070# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001071# $1 = PID of process (e.g., $(pid mediaserver))
1072#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001073# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001074# dump to actually be generated.
1075
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001076function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001077{
Ying Wang08800fd2016-03-03 20:57:21 -08001078 local PID=$1;
1079 if [ -z "$PID" ]; then
1080 printf "Expecting a PID!\n";
1081 return;
1082 fi;
1083 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001084 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001085}
1086
1087# core - send SIGV and pull the core for process
1088# $1 = PID of process (e.g., $(pid mediaserver))
1089#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001090# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001091# enabled globally.
1092
1093function core()
1094{
Ying Wang08800fd2016-03-03 20:57:21 -08001095 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001096
Ying Wang08800fd2016-03-03 20:57:21 -08001097 if [ -z "$PID" ]; then
1098 printf "Expecting a PID!\n";
1099 return;
1100 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001101
Ying Wang08800fd2016-03-03 20:57:21 -08001102 local CORENAME=core.$PID;
1103 local COREPATH=/cores/$CORENAME;
1104 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001105
Ying Wang08800fd2016-03-03 20:57:21 -08001106 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001107
Ying Wang08800fd2016-03-03 20:57:21 -08001108 local done=0;
1109 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1110 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1111 adb shell kill -$SIG $PID;
1112 sleep 1;
1113 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001114
Ying Wang08800fd2016-03-03 20:57:21 -08001115 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1116 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001117}
1118
Christopher Tate744ee802009-11-12 15:33:08 -08001119# systemstack - dump the current stack trace of all threads in the system process
1120# to the usual ANR traces file
1121function systemstack()
1122{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001123 stacks system_server
1124}
1125
Michael Wrightaeed7212014-06-19 19:58:12 -07001126# Read the ELF header from /proc/$PID/exe to determine if the process is
1127# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001128function is64bit()
1129{
1130 local PID="$1"
1131 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -08001132 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001133 echo "64"
1134 else
1135 echo ""
1136 fi
1137 else
1138 echo ""
1139 fi
1140}
1141
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001142case `uname -s` in
1143 Darwin)
1144 function sgrep()
1145 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001146 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 -04001147 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001148 }
1149
1150 ;;
1151 *)
1152 function sgrep()
1153 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001154 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 -04001155 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001156 }
1157 ;;
1158esac
1159
Raghu Gandham8da43102012-07-25 19:57:22 -07001160function gettargetarch
1161{
1162 get_build_var TARGET_ARCH
1163}
1164
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001165function ggrep()
1166{
Mike Frysinger5e479732015-09-22 18:13:48 -04001167 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1168 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001169}
1170
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001171function jgrep()
1172{
Mike Frysinger5e479732015-09-22 18:13:48 -04001173 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1174 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001175}
1176
1177function cgrep()
1178{
Mike Frysinger5e479732015-09-22 18:13:48 -04001179 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' \) \
1180 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001181}
1182
1183function resgrep()
1184{
Christopher Ferris55257d22017-03-23 11:08:58 -07001185 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001186 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1187 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1188 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001189}
1190
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001191function mangrep()
1192{
Mike Frysinger5e479732015-09-22 18:13:48 -04001193 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1194 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001195}
1196
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001197function sepgrep()
1198{
Mike Frysinger5e479732015-09-22 18:13:48 -04001199 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1200 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001201}
1202
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001203function rcgrep()
1204{
Mike Frysinger5e479732015-09-22 18:13:48 -04001205 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1206 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001207}
1208
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001209case `uname -s` in
1210 Darwin)
1211 function mgrep()
1212 {
David Grossd1d6fc52017-05-10 10:49:44 -07001213 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 -04001214 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001215 }
1216
1217 function treegrep()
1218 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001219 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 -04001220 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001221 }
1222
1223 ;;
1224 *)
1225 function mgrep()
1226 {
David Grossd1d6fc52017-05-10 10:49:44 -07001227 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 -04001228 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001229 }
1230
1231 function treegrep()
1232 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001233 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 -04001234 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001235 }
1236
1237 ;;
1238esac
1239
1240function getprebuilt
1241{
1242 get_abs_build_var ANDROID_PREBUILTS
1243}
1244
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245function tracedmdump()
1246{
Christopher Ferris55257d22017-03-23 11:08:58 -07001247 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001248 if [ ! "$T" ]; then
1249 echo "Couldn't locate the top of the tree. Try setting TOP."
1250 return
1251 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001252 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001253 local arch=$(gettargetarch)
1254 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001255
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001256 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001257 if [ ! "$TRACE" ] ; then
1258 echo "usage: tracedmdump tracename"
1259 return
1260 fi
1261
Jack Veenstra60116fc2009-04-09 18:12:34 -07001262 if [ ! -r "$KERNEL" ] ; then
1263 echo "Error: cannot find kernel: '$KERNEL'"
1264 return
1265 fi
1266
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001267 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001268 if [ "$BASETRACE" = "$TRACE" ] ; then
1269 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1270 fi
1271
1272 echo "post-processing traces..."
1273 rm -f $TRACE/qtrace.dexlist
1274 post_trace $TRACE
1275 if [ $? -ne 0 ]; then
1276 echo "***"
1277 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1278 echo "***"
1279 return
1280 fi
1281 echo "generating dexlist output..."
1282 /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
1283 echo "generating dmtrace data..."
1284 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1285 echo "generating html file..."
1286 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1287 echo "done, see $TRACE/dmtrace.html for details"
1288 echo "or run:"
1289 echo " traceview $TRACE/dmtrace"
1290}
1291
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001292# communicate with a running device or emulator, set up necessary state,
1293# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001294function runhat()
1295{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001296 # process standard adb options
1297 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001298 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001299 adbTarget=$1
1300 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001301 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001302 adbTarget="$1 $2"
1303 shift 2
1304 fi
1305 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001306 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001307
1308 # runhat options
1309 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001310
1311 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001312 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001313 return
1314 fi
1315
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001316 # confirm hat is available
1317 if [ -z $(which hat) ]; then
1318 echo "hat is not available in this configuration."
1319 return
1320 fi
1321
Andy McFaddenb6289852010-07-12 08:00:19 -07001322 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001323 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001324 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001325 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001326 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001327 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001328 echo -n "> "
1329 read
1330
The Android Open Source Project88b60792009-03-03 19:28:42 -08001331 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001332
The Android Open Source Project88b60792009-03-03 19:28:42 -08001333 echo "Retrieving file $devFile..."
1334 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001335
The Android Open Source Project88b60792009-03-03 19:28:42 -08001336 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001337
The Android Open Source Project88b60792009-03-03 19:28:42 -08001338 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001339 echo "View the output by pointing your browser at http://localhost:7000/"
1340 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001341 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001342}
1343
1344function getbugreports()
1345{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001346 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001347
1348 if [ ! "$reports" ]; then
1349 echo "Could not locate any bugreports."
1350 return
1351 fi
1352
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001353 local report
1354 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001355 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001356 echo "/sdcard/bugreports/${report}"
1357 adb pull /sdcard/bugreports/${report} ${report}
1358 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001359 done
1360}
1361
Victoria Lease1b296b42012-08-21 15:44:06 -07001362function getsdcardpath()
1363{
1364 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1365}
1366
1367function getscreenshotpath()
1368{
1369 echo "$(getsdcardpath)/Pictures/Screenshots"
1370}
1371
1372function getlastscreenshot()
1373{
1374 local screenshot_path=$(getscreenshotpath)
1375 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1376 if [ "$screenshot" = "" ]; then
1377 echo "No screenshots found."
1378 return
1379 fi
1380 echo "${screenshot}"
1381 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1382}
1383
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001384function startviewserver()
1385{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001386 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001387 if [ $# -gt 0 ]; then
1388 port=$1
1389 fi
1390 adb shell service call window 1 i32 $port
1391}
1392
1393function stopviewserver()
1394{
1395 adb shell service call window 2
1396}
1397
1398function isviewserverstarted()
1399{
1400 adb shell service call window 3
1401}
1402
Romain Guyb84049a2010-10-04 16:56:11 -07001403function key_home()
1404{
1405 adb shell input keyevent 3
1406}
1407
1408function key_back()
1409{
1410 adb shell input keyevent 4
1411}
1412
1413function key_menu()
1414{
1415 adb shell input keyevent 82
1416}
1417
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001418function smoketest()
1419{
1420 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1421 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1422 return
1423 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001424 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001425 if [ ! "$T" ]; then
1426 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1427 return
1428 fi
1429
Ying Wang9cd17642012-12-13 10:52:07 -08001430 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001431 adb uninstall com.android.smoketest > /dev/null &&
1432 adb uninstall com.android.smoketest.tests > /dev/null &&
1433 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1434 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1435 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1436}
1437
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001438# simple shortcut to the runtest command
1439function runtest()
1440{
Christopher Ferris55257d22017-03-23 11:08:58 -07001441 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001442 if [ ! "$T" ]; then
1443 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1444 return
1445 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001446 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001447}
1448
The Android Open Source Project88b60792009-03-03 19:28:42 -08001449function godir () {
1450 if [[ -z "$1" ]]; then
1451 echo "Usage: godir <regex>"
1452 return
1453 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001454 local T=$(gettop)
1455 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001456 if [ ! "$OUT_DIR" = "" ]; then
1457 mkdir -p $OUT_DIR
1458 FILELIST=$OUT_DIR/filelist
1459 else
1460 FILELIST=$T/filelist
1461 fi
1462 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001463 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001464 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001465 echo " Done"
1466 echo ""
1467 fi
1468 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001469 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001470 if [[ ${#lines[@]} = 0 ]]; then
1471 echo "Not found"
1472 return
1473 fi
1474 local pathname
1475 local choice
1476 if [[ ${#lines[@]} > 1 ]]; then
1477 while [[ -z "$pathname" ]]; do
1478 local index=1
1479 local line
1480 for line in ${lines[@]}; do
1481 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001482 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001483 done
1484 echo
1485 echo -n "Select one: "
1486 unset choice
1487 read choice
1488 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1489 echo "Invalid choice"
1490 continue
1491 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001492 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001493 done
1494 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001495 pathname=${lines[0]}
1496 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001497 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001498}
1499
Steven Moreland62054a42018-12-06 10:11:40 -08001500# Update module-info.json in out.
1501function refreshmod() {
1502 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1503 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1504 return 1
1505 fi
1506
1507 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1508
1509 # for the output of the next command
1510 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1511
1512 # Note, can't use absolute path because of the way make works.
1513 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1514 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1515}
1516
1517# List all modules for the current device, as cached in module-info.json. If any build change is
1518# made and it should be reflected in the output, you should run 'refreshmod' first.
1519function allmod() {
1520 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1521 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1522 return 1
1523 fi
1524
1525 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1526 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1527 refreshmod || return 1
1528 fi
1529
1530 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1531}
1532
Rett Berg78d1c932019-01-24 14:34:23 -08001533# 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 -08001534# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001535function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001536 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1537 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1538 return 1
1539 fi
1540
1541 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001542 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001543 return 1
1544 fi
1545
1546 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1547 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1548 refreshmod || return 1
1549 fi
1550
1551 local relpath=$(python -c "import json, os
1552module = '$1'
1553module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1554if module not in module_info:
1555 exit(1)
1556print module_info[module]['path'][0]" 2>/dev/null)
1557
1558 if [ -z "$relpath" ]; then
1559 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1560 return 1
1561 else
Rett Berg78d1c932019-01-24 14:34:23 -08001562 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001563 fi
1564}
1565
Rett Berg78d1c932019-01-24 14:34:23 -08001566# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1567# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1568function gomod() {
1569 if [[ $# -ne 1 ]]; then
1570 echo "usage: gomod <module>" >&2
1571 return 1
1572 fi
1573
1574 local path="$(pathmod $@)"
1575 if [ -z "$path" ]; then
1576 return 1
1577 fi
1578 cd $path
1579}
1580
dimitry73b84812018-12-11 18:06:00 +01001581function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001582 local word=${COMP_WORDS[COMP_CWORD]}
1583 COMPREPLY=( $(allmod | grep -E "^$word") )
1584}
1585
Alex Rayf0d08eb2013-03-08 15:15:06 -08001586# Print colored exit condition
1587function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001588 "$@"
1589 local retval=$?
1590 if [ $retval -ne 0 ]
1591 then
Jacky Cao89483b82015-05-15 22:12:53 +08001592 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001593 else
Jacky Cao89483b82015-05-15 22:12:53 +08001594 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001595 fi
1596 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001597}
1598
Ying Wanged21d4c2014-08-24 22:14:19 -07001599function get_make_command()
1600{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001601 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1602 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001603 # Always use the real make if -C is passed in
1604 for arg in "$@"; do
1605 if [[ $arg == -C* ]]; then
1606 echo command make
1607 return
1608 fi
1609 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001610 echo build/soong/soong_ui.bash --make-mode
1611 else
1612 echo command make
1613 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001614}
1615
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001616function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001617{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001618 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1619 "$@"
1620 return $?
1621 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001622 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001623 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001624 local ret=$?
1625 local end_time=$(date +"%s")
1626 local tdiff=$(($end_time-$start_time))
1627 local hours=$(($tdiff / 3600 ))
1628 local mins=$((($tdiff % 3600) / 60))
1629 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001630 local ncolors=$(tput colors 2>/dev/null)
1631 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001632 color_failed=$'\E'"[0;31m"
1633 color_success=$'\E'"[0;32m"
1634 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001635 else
1636 color_failed=""
1637 color_success=""
1638 color_reset=""
1639 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001640 echo
1641 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001642 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001643 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001644 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001645 fi
1646 if [ $hours -gt 0 ] ; then
1647 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1648 elif [ $mins -gt 0 ] ; then
1649 printf "(%02g:%02g (mm:ss))" $mins $secs
1650 elif [ $secs -gt 0 ] ; then
1651 printf "(%s seconds)" $secs
1652 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001653 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001654 echo
1655 return $ret
1656}
1657
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001658function make()
1659{
Dan Willemsene9842242017-07-28 13:00:13 -07001660 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001661}
1662
David Zeuthen1b126ff2015-09-30 17:10:48 -04001663function provision()
1664{
1665 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1666 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1667 return 1
1668 fi
1669 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1670 echo "There is no provisioning script for the device." >&2
1671 return 1
1672 fi
1673
1674 # Check if user really wants to do this.
1675 if [ "$1" = "--no-confirmation" ]; then
1676 shift 1
1677 else
1678 echo "This action will reflash your device."
1679 echo ""
1680 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1681 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001682 echo -n "Are you sure you want to do this (yes/no)? "
1683 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001684 if [[ "${REPLY}" != "yes" ]] ; then
1685 echo "Not taking any action. Exiting." >&2
1686 return 1
1687 fi
1688 fi
1689 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1690}
1691
Simran Basi424b8762017-08-23 12:03:04 -07001692function atest()
1693{
Jim Tangf258e7e2018-11-01 18:00:05 +08001694 # Let's use the built version over the prebuilt, then source code.
1695 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
1696 local built_atest=${ANDROID_HOST_OUT}/bin/atest
1697 local prebuilt_atest="$(gettop)"/prebuilts/asuite/atest/$os_arch/atest
1698 if [[ -x $built_atest ]]; then
1699 $built_atest "$@"
1700 elif [[ -x $prebuilt_atest ]]; then
1701 $prebuilt_atest "$@"
1702 else
1703 # TODO: once prebuilt atest released, remove the source code section
1704 # and change the location of atest_completion.sh in addcompletions().
1705 "$(gettop)"/tools/tradefederation/core/atest/atest.py "$@"
1706 fi
Simran Basi424b8762017-08-23 12:03:04 -07001707}
1708
Jim Tanga881a252018-06-19 16:34:41 +08001709# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001710function enable_zsh_completion() {
1711 # Don't override user's options if bash-style completion is already enabled.
1712 if ! declare -f complete >/dev/null; then
1713 autoload -U compinit && compinit
1714 autoload -U bashcompinit && bashcompinit
1715 fi
Jim Tanga881a252018-06-19 16:34:41 +08001716}
1717
1718function validate_current_shell() {
1719 local current_sh="$(ps -o command -p $$)"
1720 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001721 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001722 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001723 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001724 *zsh*)
1725 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001726 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001727 *)
Jim Tanga881a252018-06-19 16:34:41 +08001728 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 -07001729 ;;
1730 esac
Jim Tanga881a252018-06-19 16:34:41 +08001731}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001732
Kevin Chengbf89aff2018-05-22 14:07:50 -07001733function acloud()
1734{
1735 # Let's use the built version over the prebuilt.
1736 local built_acloud=${ANDROID_HOST_OUT}/bin/acloud
1737 if [ -f $built_acloud ]; then
1738 $built_acloud "$@"
1739 return $?
1740 fi
1741
1742 local host_os_arch=$(get_build_var HOST_PREBUILT_TAG)
1743 case $host_os_arch in
1744 linux-x86) "$(gettop)"/prebuilts/asuite/acloud/linux-x86/acloud "$@"
1745 ;;
Kevin Chengf0497e42018-12-03 01:01:27 -08001746 darwin-x86) "$(gettop)"/prebuilts/asuite/acloud/darwin-x86/acloud "$@"
1747 ;;
Kevin Chengbf89aff2018-05-22 14:07:50 -07001748 *)
1749 echo "acloud is not supported on your host arch: $host_os_arch"
1750 ;;
1751 esac
1752}
1753
patricktu345c9ae2018-11-29 15:25:40 +08001754function aidegen()
1755{
1756 # Always use the prebuilt version.
1757 local host_os_arch=$(get_build_var HOST_PREBUILT_TAG)
1758 case $host_os_arch in
1759 linux-x86) "$(gettop)"/prebuilts/asuite/aidegen/linux-x86/aidegen "$@"
1760 ;;
1761 *)
1762 echo "aidegen is not supported on your host arch: $host_os_arch"
1763 ;;
1764 esac
1765}
1766
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001767# Execute the contents of any vendorsetup.sh files we can find.
Jim Tanga881a252018-06-19 16:34:41 +08001768function source_vendorsetup() {
1769 for dir in device vendor product; do
1770 for f in $(test -d $dir && \
1771 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
1772 echo "including $f"; . $f
1773 done
1774 done
1775}
Kenny Root52aa81c2011-07-15 11:07:06 -07001776
Jim Tanga881a252018-06-19 16:34:41 +08001777validate_current_shell
1778source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001779addcompletions