blob: 4947bf021993ea3d2e40b4fea4e6d555095d9586 [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]
12- croot: Changes directory to the top of the tree.
13- 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.
38
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070039Look at the source to view more functions. The complete list is:
40EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070041 local T=$(gettop)
42 local A=""
43 local i
Jacky Cao89483b82015-05-15 22:12:53 +080044 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 -070045 A="$A $i"
46 done
47 echo $A
48}
49
Ying Wang08800fd2016-03-03 20:57:21 -080050# Get all the build variables needed by this script in a single call to the build system.
51function build_build_var_cache()
52{
Christopher Ferris55257d22017-03-23 11:08:58 -070053 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080054 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080055 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' ' '`)
56 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 -080057 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080058 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080059 --vars="${cached_vars[*]}" \
60 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070061 --var-prefix=var_cache_ \
62 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080063 local ret=$?
64 if [ $ret -ne 0 ]
65 then
66 unset build_dicts_script
67 return $ret
68 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070069 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080070 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080071 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080072 unset build_dicts_script
Ying Wang08800fd2016-03-03 20:57:21 -080073 if [ $ret -ne 0 ]
74 then
75 return $ret
76 fi
77 BUILD_VAR_CACHE_READY="true"
78}
79
Ying Wangf0cb3972016-03-04 13:56:23 -080080# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080081# to get build variables not listed in this script.
82function destroy_build_var_cache()
83{
84 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070085 local v
Ying Wang08800fd2016-03-03 20:57:21 -080086 for v in $cached_vars; do
87 unset var_cache_$v
88 done
89 unset cached_vars
90 for v in $cached_abs_vars; do
91 unset abs_var_cache_$v
92 done
93 unset cached_abs_vars
94}
95
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070096# Get the value of a build variable as an absolute path.
97function get_abs_build_var()
98{
Ying Wang08800fd2016-03-03 20:57:21 -080099 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
100 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800101 eval "echo \"\${abs_var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800102 return
103 fi
104
Christopher Ferris55257d22017-03-23 11:08:58 -0700105 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700106 if [ ! "$T" ]; then
107 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
108 return
109 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700110 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700111}
112
113# Get the exact value of a build variable.
114function get_build_var()
115{
Ying Wang08800fd2016-03-03 20:57:21 -0800116 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
117 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800118 eval "echo \"\${var_cache_$1}\""
Ying Wang08800fd2016-03-03 20:57:21 -0800119 return
120 fi
121
Christopher Ferris55257d22017-03-23 11:08:58 -0700122 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700123 if [ ! "$T" ]; then
124 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
125 return
126 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700127 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800128}
129
130# check to see if the supplied product is one we can build
131function check_product()
132{
Christopher Ferris55257d22017-03-23 11:08:58 -0700133 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800134 if [ ! "$T" ]; then
135 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
136 return
137 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700138 TARGET_PRODUCT=$1 \
139 TARGET_BUILD_VARIANT= \
140 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700141 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800142 get_build_var TARGET_DEVICE > /dev/null
143 # hide successful answers, but allow the errors to show
144}
145
146VARIANT_CHOICES=(user userdebug eng)
147
148# check to see if the supplied variant is valid
149function check_variant()
150{
Christopher Ferris55257d22017-03-23 11:08:58 -0700151 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800152 for v in ${VARIANT_CHOICES[@]}
153 do
154 if [ "$v" = "$1" ]
155 then
156 return 0
157 fi
158 done
159 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700160}
161
162function setpaths()
163{
Christopher Ferris55257d22017-03-23 11:08:58 -0700164 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700165 if [ ! "$T" ]; then
166 echo "Couldn't locate the top of the tree. Try setting TOP."
167 return
168 fi
169
170 ##################################################################
171 # #
172 # Read me before you modify this code #
173 # #
174 # This function sets ANDROID_BUILD_PATHS to what it is adding #
175 # to PATH, and the next time it is run, it removes that from #
176 # PATH. This is required so lunch can be run more than once #
177 # and still have working paths. #
178 # #
179 ##################################################################
180
Raphael Mollc639c782011-06-20 17:25:01 -0700181 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
182 # due to "C:\Program Files" being in the path.
183
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700184 # out with the old
Raphael Mollc639c782011-06-20 17:25:01 -0700185 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700186 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
187 fi
Raphael Mollc639c782011-06-20 17:25:01 -0700188 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700189 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800190 # strip leading ':', if any
191 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500192 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700193
194 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700195 local prebuiltdir=$(getprebuilt)
196 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700197
Ben Cheng8bc4c432012-11-16 13:29:13 -0800198 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700199 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
200 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800201 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800202
Raphael Mollc639c782011-06-20 17:25:01 -0700203 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800204 export ANDROID_TOOLCHAIN=
205 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200206 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700207 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200208 case $ARCH in
Pavel Chupinc1a56642013-08-23 16:49:21 +0400209 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700210 ;;
Pavel Chupinfd82a492012-11-26 09:50:07 +0400211 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
212 ;;
Ben Cheng8bc4c432012-11-16 13:29:13 -0800213 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200214 ;;
Ben Chengfba67bf2014-02-25 10:27:07 -0800215 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
Colin Cross03b424a2014-05-22 11:57:43 -0700216 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700217 ;;
Duane Sand3c4fcd82014-07-22 14:34:00 -0700218 mips|mips64) toolchaindir=mips/mips64el-linux-android-$targetgccversion/bin
Serban Constantinescu9b68fb22014-01-14 10:33:53 +0000219 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200220 *)
221 echo "Can't find toolchain for unknown architecture: $ARCH"
222 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700223 ;;
224 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700225 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800226 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700227 fi
Raphael732936d2011-06-22 14:35:32 -0700228
Christopher Ferris55257d22017-03-23 11:08:58 -0700229 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800230 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
231 fi
232
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700233 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700234
235 # add kernel specific binaries
236 case $(uname -s) in
237 Linux)
238 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
239 ;;
240 *)
241 ;;
242 esac
243
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400244 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
245 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
246 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
247 fi
248 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS:
249 export ANDROID_BUILD_PATHS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200250
251 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
252 # to ensure that the corresponding 'emulator' binaries are used.
253 case $(uname -s) in
254 Darwin)
255 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
256 ;;
257 Linux)
258 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
259 ;;
260 *)
261 ANDROID_EMULATOR_PREBUILTS=
262 ;;
263 esac
264 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Christopher Ferris7110f242014-05-20 13:56:00 -0700265 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS$ANDROID_EMULATOR_PREBUILTS:
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200266 export ANDROID_EMULATOR_PREBUILTS
267 fi
268
Ying Wangaa1c9b52012-11-26 20:51:59 -0800269 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800270
271 # out with the duplicate old
272 if [ -n $ANDROID_PYTHONPATH ]; then
273 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
274 fi
275 # and in with the new
276 export ANDROID_PYTHONPATH=$T/development/python-packages:
277 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800278
Colin Crosse97e6932017-06-30 16:01:45 -0700279 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
280 export JAVA_HOME=$ANDROID_JAVA_HOME
281 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
282 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
283 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500284
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800285 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700286 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
287 export OUT=$ANDROID_PRODUCT_OUT
288
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700289 unset ANDROID_HOST_OUT
290 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
291
Simran Basidd050ed2017-02-13 13:46:48 -0800292 unset ANDROID_HOST_OUT_TESTCASES
293 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
294
295 unset ANDROID_TARGET_OUT_TESTCASES
296 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
297
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800298 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700299 # TODO: fix the path
300 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
301}
302
303function printconfig()
304{
Christopher Ferris55257d22017-03-23 11:08:58 -0700305 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800306 if [ ! "$T" ]; then
307 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
308 return
309 fi
310 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700311}
312
313function set_stuff_for_environment()
314{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800315 setpaths
316 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700317
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800318 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700319 # With this environment variable new GCC can apply colors to warnings/errors
320 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 -0700321 export ASAN_OPTIONS=detect_leaks=0
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700322}
323
324function set_sequence_number()
325{
Colin Cross88737132017-03-21 17:41:03 -0700326 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700327}
328
Makoto Onukida971062018-06-18 10:15:19 -0700329# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
330function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800331 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700332 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800333 *:"$cmd":*)
334 return 1
335 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700336 esac
337 return 0
338}
339
Kenny Root52aa81c2011-07-15 11:07:06 -0700340function addcompletions()
341{
342 local T dir f
343
Jim Tanga881a252018-06-19 16:34:41 +0800344 # Keep us from trying to run in something that's neither bash nor zsh.
345 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700346 return
347 fi
348
349 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800350 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700351 return
352 fi
353
Jim Tanga881a252018-06-19 16:34:41 +0800354 local completion_files=(
355 system/core/adb/adb.bash
356 system/core/fastboot/fastboot.bash
357 tools/tradefederation/core/atest/atest_completion.sh
358 )
Makoto Onukida971062018-06-18 10:15:19 -0700359 # Completion can be disabled selectively to allow users to use non-standard completion.
360 # e.g.
361 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
362 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800363 for f in ${completion_files[*]}; do
364 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700365 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700366 fi
367 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700368
Makoto Onukida971062018-06-18 10:15:19 -0700369 if should_add_completion bit ; then
370 complete -C "bit --tab" bit
371 fi
Jim Tanga881a252018-06-19 16:34:41 +0800372 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800373
dimitry73b84812018-12-11 18:06:00 +0100374 complete -F _complete_android_module_names gomod
375 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700376}
377
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700378function choosetype()
379{
380 echo "Build type choices are:"
381 echo " 1. release"
382 echo " 2. debug"
383 echo
384
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800385 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700386 DEFAULT_NUM=1
387 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700388
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800389 export TARGET_BUILD_TYPE=
390 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700391 while [ -z $TARGET_BUILD_TYPE ]
392 do
393 echo -n "Which would you like? ["$DEFAULT_NUM"] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800394 if [ -z "$1" ] ; then
395 read ANSWER
396 else
397 echo $1
398 ANSWER=$1
399 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400 case $ANSWER in
401 "")
402 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
403 ;;
404 1)
405 export TARGET_BUILD_TYPE=release
406 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800407 release)
408 export TARGET_BUILD_TYPE=release
409 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700410 2)
411 export TARGET_BUILD_TYPE=debug
412 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800413 debug)
414 export TARGET_BUILD_TYPE=debug
415 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700416 *)
417 echo
418 echo "I didn't understand your response. Please try again."
419 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700420 ;;
421 esac
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800422 if [ -n "$1" ] ; then
423 break
424 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700425 done
426
Ying Wang08800fd2016-03-03 20:57:21 -0800427 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700428 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800429 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700430}
431
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800432#
433# This function isn't really right: It chooses a TARGET_PRODUCT
434# based on the list of boards. Usually, that gets you something
435# that kinda works with a generic product, but really, you should
436# pick a product by name.
437#
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700438function chooseproduct()
439{
Christopher Ferris55257d22017-03-23 11:08:58 -0700440 local default_value
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700441 if [ "x$TARGET_PRODUCT" != x ] ; then
442 default_value=$TARGET_PRODUCT
443 else
Ying Wang0a76df52015-06-08 11:57:26 -0700444 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445 fi
446
Ying Wang08800fd2016-03-03 20:57:21 -0800447 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800448 export TARGET_PRODUCT=
449 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450 while [ -z "$TARGET_PRODUCT" ]
451 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700452 echo -n "Which product would you like? [$default_value] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800453 if [ -z "$1" ] ; then
454 read ANSWER
455 else
456 echo $1
457 ANSWER=$1
458 fi
459
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700460 if [ -z "$ANSWER" ] ; then
461 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800462 else
463 if check_product $ANSWER
464 then
465 export TARGET_PRODUCT=$ANSWER
466 else
467 echo "** Not a valid product: $ANSWER"
468 fi
469 fi
470 if [ -n "$1" ] ; then
471 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700472 fi
473 done
474
Ying Wang08800fd2016-03-03 20:57:21 -0800475 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700476 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800477 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700478}
479
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800480function choosevariant()
481{
482 echo "Variant choices are:"
483 local index=1
484 local v
485 for v in ${VARIANT_CHOICES[@]}
486 do
487 # The product name is the name of the directory containing
488 # the makefile we found, above.
489 echo " $index. $v"
490 index=$(($index+1))
491 done
492
493 local default_value=eng
494 local ANSWER
495
496 export TARGET_BUILD_VARIANT=
497 while [ -z "$TARGET_BUILD_VARIANT" ]
498 do
499 echo -n "Which would you like? [$default_value] "
500 if [ -z "$1" ] ; then
501 read ANSWER
502 else
503 echo $1
504 ANSWER=$1
505 fi
506
507 if [ -z "$ANSWER" ] ; then
508 export TARGET_BUILD_VARIANT=$default_value
509 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
510 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Kan-Ru Chen07453762010-07-05 15:53:47 +0800511 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[$(($ANSWER-1))]}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800512 fi
513 else
514 if check_variant $ANSWER
515 then
516 export TARGET_BUILD_VARIANT=$ANSWER
517 else
518 echo "** Not a valid variant: $ANSWER"
519 fi
520 fi
521 if [ -n "$1" ] ; then
522 break
523 fi
524 done
525}
526
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700527function choosecombo()
528{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700529 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700530
531 echo
532 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700533 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700534
535 echo
536 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700537 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800538
539 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800540 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700541 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800542 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800543 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700544}
545
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800546# Clear this variable. It will be built up again when the vendorsetup.sh
547# files are included at the end of this file.
548unset LUNCH_MENU_CHOICES
549function add_lunch_combo()
550{
551 local new_combo=$1
552 local c
553 for c in ${LUNCH_MENU_CHOICES[@]} ; do
554 if [ "$new_combo" = "$c" ] ; then
555 return
556 fi
557 done
558 LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
559}
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 Willemsenaf2e1f82018-04-04 15:41:41 -0700571 for choice in $(TARGET_BUILD_APPS= LUNCH_MENU_CHOICES="${LUNCH_MENU_CHOICES[@]}" 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 Willemsenaf2e1f82018-04-04 15:41:41 -0700599 local choices=($(TARGET_BUILD_APPS= LUNCH_MENU_CHOICES="${LUNCH_MENU_CHOICES[@]}" get_build_var COMMON_LUNCH_CHOICES))
600 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
669 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= LUNCH_MENU_CHOICES="${LUNCH_MENU_CHOICES[@]}" get_build_var COMMON_LUNCH_CHOICES)
670 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
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700987function cproj()
988{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700989 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700990 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700991 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700992 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
993 T=$PWD
994 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800995 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700996 return
997 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800998 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700999 done
Ying Wang9cd17642012-12-13 10:52:07 -08001000 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -07001001 echo "can't find Android.mk"
1002}
1003
Daniel Sandler47e0a882013-07-30 13:23:52 -04001004# simplified version of ps; output in the form
1005# <pid> <procname>
1006function qpid() {
1007 local prepend=''
1008 local append=''
1009 if [ "$1" = "--exact" ]; then
1010 prepend=' '
1011 append='$'
1012 shift
1013 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -08001014 echo "usage: qpid [[--exact] <process name|pid>"
1015 return 255
1016 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001017
1018 local EXE="$1"
1019 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -08001020 qpid | \grep "$prepend$EXE$append"
1021 else
1022 adb shell ps \
1023 | tr -d '\r' \
1024 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
1025 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -04001026}
1027
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001028# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001029# that has the core-file-size limit set correctly
1030#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001031# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001032# if its core-file-size limit is not set already.
1033# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
1034
Iliyan Malcheve675cfb2014-11-03 17:04:47 -08001035function coredump_setup()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001036{
Ying Wang08800fd2016-03-03 20:57:21 -08001037 echo "Getting root...";
1038 adb root;
1039 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001040
Ying Wang08800fd2016-03-03 20:57:21 -08001041 echo "Remounting root partition read-write...";
1042 adb shell mount -w -o remount -t rootfs rootfs;
1043 sleep 1;
1044 adb wait-for-device;
1045 adb shell mkdir -p /cores;
1046 adb shell mount -t tmpfs tmpfs /cores;
1047 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001048
Ying Wang08800fd2016-03-03 20:57:21 -08001049 echo "Granting SELinux permission to dump in /cores...";
1050 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001051
Ying Wang08800fd2016-03-03 20:57:21 -08001052 echo "Set core pattern.";
1053 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001054
Ying Wang08800fd2016-03-03 20:57:21 -08001055 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001056}
1057
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001058# coredump_enable - enable core dumps for the specified process
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001059# $1 = PID of process (e.g., $(pid mediaserver))
1060#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001061# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001062# dump to actually be generated.
1063
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001064function coredump_enable()
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001065{
Ying Wang08800fd2016-03-03 20:57:21 -08001066 local PID=$1;
1067 if [ -z "$PID" ]; then
1068 printf "Expecting a PID!\n";
1069 return;
1070 fi;
1071 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -08001072 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001073}
1074
1075# core - send SIGV and pull the core for process
1076# $1 = PID of process (e.g., $(pid mediaserver))
1077#
Iliyan Malchevaf5de972014-11-04 20:57:37 -08001078# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001079# enabled globally.
1080
1081function core()
1082{
Ying Wang08800fd2016-03-03 20:57:21 -08001083 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001084
Ying Wang08800fd2016-03-03 20:57:21 -08001085 if [ -z "$PID" ]; then
1086 printf "Expecting a PID!\n";
1087 return;
1088 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001089
Ying Wang08800fd2016-03-03 20:57:21 -08001090 local CORENAME=core.$PID;
1091 local COREPATH=/cores/$CORENAME;
1092 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001093
Ying Wang08800fd2016-03-03 20:57:21 -08001094 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001095
Ying Wang08800fd2016-03-03 20:57:21 -08001096 local done=0;
1097 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
1098 printf "\tSending SIG%s to %d...\n" $SIG $PID;
1099 adb shell kill -$SIG $PID;
1100 sleep 1;
1101 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001102
Ying Wang08800fd2016-03-03 20:57:21 -08001103 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
1104 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -07001105}
1106
Christopher Tate744ee802009-11-12 15:33:08 -08001107# systemstack - dump the current stack trace of all threads in the system process
1108# to the usual ANR traces file
1109function systemstack()
1110{
Jeff Sharkeyf5824372013-02-19 17:00:46 -08001111 stacks system_server
1112}
1113
Michael Wrightaeed7212014-06-19 19:58:12 -07001114# Read the ELF header from /proc/$PID/exe to determine if the process is
1115# 64-bit.
Ben Chengfba67bf2014-02-25 10:27:07 -08001116function is64bit()
1117{
1118 local PID="$1"
1119 if [ "$PID" ] ; then
Elliott Hughesd3e47252018-11-15 16:32:14 -08001120 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -08001121 echo "64"
1122 else
1123 echo ""
1124 fi
1125 else
1126 echo ""
1127 fi
1128}
1129
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001130case `uname -s` in
1131 Darwin)
1132 function sgrep()
1133 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001134 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 -04001135 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001136 }
1137
1138 ;;
1139 *)
1140 function sgrep()
1141 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001142 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 -04001143 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001144 }
1145 ;;
1146esac
1147
Raghu Gandham8da43102012-07-25 19:57:22 -07001148function gettargetarch
1149{
1150 get_build_var TARGET_ARCH
1151}
1152
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001153function ggrep()
1154{
Mike Frysinger5e479732015-09-22 18:13:48 -04001155 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
1156 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -07001157}
1158
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001159function jgrep()
1160{
Mike Frysinger5e479732015-09-22 18:13:48 -04001161 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1162 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001163}
1164
1165function cgrep()
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 '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
1168 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001169}
1170
1171function resgrep()
1172{
Christopher Ferris55257d22017-03-23 11:08:58 -07001173 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001174 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1175 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1176 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001177}
1178
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001179function mangrep()
1180{
Mike Frysinger5e479732015-09-22 18:13:48 -04001181 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1182 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001183}
1184
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001185function sepgrep()
1186{
Mike Frysinger5e479732015-09-22 18:13:48 -04001187 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1188 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001189}
1190
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001191function rcgrep()
1192{
Mike Frysinger5e479732015-09-22 18:13:48 -04001193 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1194 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001195}
1196
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001197case `uname -s` in
1198 Darwin)
1199 function mgrep()
1200 {
David Grossd1d6fc52017-05-10 10:49:44 -07001201 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 -04001202 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001203 }
1204
1205 function treegrep()
1206 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001207 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 -04001208 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001209 }
1210
1211 ;;
1212 *)
1213 function mgrep()
1214 {
David Grossd1d6fc52017-05-10 10:49:44 -07001215 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 -04001216 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001217 }
1218
1219 function treegrep()
1220 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001221 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 -04001222 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001223 }
1224
1225 ;;
1226esac
1227
1228function getprebuilt
1229{
1230 get_abs_build_var ANDROID_PREBUILTS
1231}
1232
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001233function tracedmdump()
1234{
Christopher Ferris55257d22017-03-23 11:08:58 -07001235 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001236 if [ ! "$T" ]; then
1237 echo "Couldn't locate the top of the tree. Try setting TOP."
1238 return
1239 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001240 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001241 local arch=$(gettargetarch)
1242 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001243
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001244 local TRACE=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001245 if [ ! "$TRACE" ] ; then
1246 echo "usage: tracedmdump tracename"
1247 return
1248 fi
1249
Jack Veenstra60116fc2009-04-09 18:12:34 -07001250 if [ ! -r "$KERNEL" ] ; then
1251 echo "Error: cannot find kernel: '$KERNEL'"
1252 return
1253 fi
1254
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001255 local BASETRACE=$(basename $TRACE)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001256 if [ "$BASETRACE" = "$TRACE" ] ; then
1257 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1258 fi
1259
1260 echo "post-processing traces..."
1261 rm -f $TRACE/qtrace.dexlist
1262 post_trace $TRACE
1263 if [ $? -ne 0 ]; then
1264 echo "***"
1265 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1266 echo "***"
1267 return
1268 fi
1269 echo "generating dexlist output..."
1270 /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
1271 echo "generating dmtrace data..."
1272 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1273 echo "generating html file..."
1274 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1275 echo "done, see $TRACE/dmtrace.html for details"
1276 echo "or run:"
1277 echo " traceview $TRACE/dmtrace"
1278}
1279
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001280# communicate with a running device or emulator, set up necessary state,
1281# and run the hat command.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001282function runhat()
1283{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001284 # process standard adb options
1285 local adbTarget=""
Andy McFaddenb6289852010-07-12 08:00:19 -07001286 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001287 adbTarget=$1
1288 shift 1
Andy McFaddenb6289852010-07-12 08:00:19 -07001289 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001290 adbTarget="$1 $2"
1291 shift 2
1292 fi
1293 local adbOptions=${adbTarget}
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001294 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001295
1296 # runhat options
1297 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001298
1299 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001300 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001301 return
1302 fi
1303
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001304 # confirm hat is available
1305 if [ -z $(which hat) ]; then
1306 echo "hat is not available in this configuration."
1307 return
1308 fi
1309
Andy McFaddenb6289852010-07-12 08:00:19 -07001310 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001311 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001312 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001313 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001314 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001315 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001316 echo -n "> "
1317 read
1318
The Android Open Source Project88b60792009-03-03 19:28:42 -08001319 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001320
The Android Open Source Project88b60792009-03-03 19:28:42 -08001321 echo "Retrieving file $devFile..."
1322 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001323
The Android Open Source Project88b60792009-03-03 19:28:42 -08001324 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001325
The Android Open Source Project88b60792009-03-03 19:28:42 -08001326 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001327 echo "View the output by pointing your browser at http://localhost:7000/"
1328 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001329 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001330}
1331
1332function getbugreports()
1333{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001334 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001335
1336 if [ ! "$reports" ]; then
1337 echo "Could not locate any bugreports."
1338 return
1339 fi
1340
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001341 local report
1342 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001343 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001344 echo "/sdcard/bugreports/${report}"
1345 adb pull /sdcard/bugreports/${report} ${report}
1346 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001347 done
1348}
1349
Victoria Lease1b296b42012-08-21 15:44:06 -07001350function getsdcardpath()
1351{
1352 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1353}
1354
1355function getscreenshotpath()
1356{
1357 echo "$(getsdcardpath)/Pictures/Screenshots"
1358}
1359
1360function getlastscreenshot()
1361{
1362 local screenshot_path=$(getscreenshotpath)
1363 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
1364 if [ "$screenshot" = "" ]; then
1365 echo "No screenshots found."
1366 return
1367 fi
1368 echo "${screenshot}"
1369 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1370}
1371
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001372function startviewserver()
1373{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001374 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001375 if [ $# -gt 0 ]; then
1376 port=$1
1377 fi
1378 adb shell service call window 1 i32 $port
1379}
1380
1381function stopviewserver()
1382{
1383 adb shell service call window 2
1384}
1385
1386function isviewserverstarted()
1387{
1388 adb shell service call window 3
1389}
1390
Romain Guyb84049a2010-10-04 16:56:11 -07001391function key_home()
1392{
1393 adb shell input keyevent 3
1394}
1395
1396function key_back()
1397{
1398 adb shell input keyevent 4
1399}
1400
1401function key_menu()
1402{
1403 adb shell input keyevent 82
1404}
1405
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001406function smoketest()
1407{
1408 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1409 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1410 return
1411 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001412 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001413 if [ ! "$T" ]; then
1414 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1415 return
1416 fi
1417
Ying Wang9cd17642012-12-13 10:52:07 -08001418 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001419 adb uninstall com.android.smoketest > /dev/null &&
1420 adb uninstall com.android.smoketest.tests > /dev/null &&
1421 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1422 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1423 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1424}
1425
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001426# simple shortcut to the runtest command
1427function runtest()
1428{
Christopher Ferris55257d22017-03-23 11:08:58 -07001429 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001430 if [ ! "$T" ]; then
1431 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1432 return
1433 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001434 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001435}
1436
The Android Open Source Project88b60792009-03-03 19:28:42 -08001437function godir () {
1438 if [[ -z "$1" ]]; then
1439 echo "Usage: godir <regex>"
1440 return
1441 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001442 local T=$(gettop)
1443 local FILELIST
Brian Carlstromf2257422015-09-30 20:28:54 -07001444 if [ ! "$OUT_DIR" = "" ]; then
1445 mkdir -p $OUT_DIR
1446 FILELIST=$OUT_DIR/filelist
1447 else
1448 FILELIST=$T/filelist
1449 fi
1450 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001451 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001452 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001453 echo " Done"
1454 echo ""
1455 fi
1456 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001457 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001458 if [[ ${#lines[@]} = 0 ]]; then
1459 echo "Not found"
1460 return
1461 fi
1462 local pathname
1463 local choice
1464 if [[ ${#lines[@]} > 1 ]]; then
1465 while [[ -z "$pathname" ]]; do
1466 local index=1
1467 local line
1468 for line in ${lines[@]}; do
1469 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001470 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001471 done
1472 echo
1473 echo -n "Select one: "
1474 unset choice
1475 read choice
1476 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1477 echo "Invalid choice"
1478 continue
1479 fi
Kan-Ru Chen07453762010-07-05 15:53:47 +08001480 pathname=${lines[$(($choice-1))]}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001481 done
1482 else
The Android Open Source Project88b60792009-03-03 19:28:42 -08001483 pathname=${lines[0]}
1484 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001485 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001486}
1487
Steven Moreland62054a42018-12-06 10:11:40 -08001488# Update module-info.json in out.
1489function refreshmod() {
1490 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1491 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1492 return 1
1493 fi
1494
1495 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1496
1497 # for the output of the next command
1498 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1499
1500 # Note, can't use absolute path because of the way make works.
1501 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1502 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1503}
1504
1505# List all modules for the current device, as cached in module-info.json. If any build change is
1506# made and it should be reflected in the output, you should run 'refreshmod' first.
1507function allmod() {
1508 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1509 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1510 return 1
1511 fi
1512
1513 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1514 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1515 refreshmod || return 1
1516 fi
1517
1518 python -c "import json; print '\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys()))"
1519}
1520
Rett Berg78d1c932019-01-24 14:34:23 -08001521# 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 -08001522# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001523function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001524 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1525 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1526 return 1
1527 fi
1528
1529 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001530 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001531 return 1
1532 fi
1533
1534 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1535 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1536 refreshmod || return 1
1537 fi
1538
1539 local relpath=$(python -c "import json, os
1540module = '$1'
1541module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1542if module not in module_info:
1543 exit(1)
1544print module_info[module]['path'][0]" 2>/dev/null)
1545
1546 if [ -z "$relpath" ]; then
1547 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1548 return 1
1549 else
Rett Berg78d1c932019-01-24 14:34:23 -08001550 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001551 fi
1552}
1553
Rett Berg78d1c932019-01-24 14:34:23 -08001554# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1555# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1556function gomod() {
1557 if [[ $# -ne 1 ]]; then
1558 echo "usage: gomod <module>" >&2
1559 return 1
1560 fi
1561
1562 local path="$(pathmod $@)"
1563 if [ -z "$path" ]; then
1564 return 1
1565 fi
1566 cd $path
1567}
1568
dimitry73b84812018-12-11 18:06:00 +01001569function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001570 local word=${COMP_WORDS[COMP_CWORD]}
1571 COMPREPLY=( $(allmod | grep -E "^$word") )
1572}
1573
Alex Rayf0d08eb2013-03-08 15:15:06 -08001574# Print colored exit condition
1575function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001576 "$@"
1577 local retval=$?
1578 if [ $retval -ne 0 ]
1579 then
Jacky Cao89483b82015-05-15 22:12:53 +08001580 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001581 else
Jacky Cao89483b82015-05-15 22:12:53 +08001582 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001583 fi
1584 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001585}
1586
Ying Wanged21d4c2014-08-24 22:14:19 -07001587function get_make_command()
1588{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001589 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1590 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001591 # Always use the real make if -C is passed in
1592 for arg in "$@"; do
1593 if [[ $arg == -C* ]]; then
1594 echo command make
1595 return
1596 fi
1597 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001598 echo build/soong/soong_ui.bash --make-mode
1599 else
1600 echo command make
1601 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001602}
1603
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001604function _wrap_build()
Ed Heylcc6be0a2014-06-18 14:55:58 -07001605{
1606 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001607 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001608 local ret=$?
1609 local end_time=$(date +"%s")
1610 local tdiff=$(($end_time-$start_time))
1611 local hours=$(($tdiff / 3600 ))
1612 local mins=$((($tdiff % 3600) / 60))
1613 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001614 local ncolors=$(tput colors 2>/dev/null)
1615 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001616 color_failed=$'\E'"[0;31m"
1617 color_success=$'\E'"[0;32m"
1618 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001619 else
1620 color_failed=""
1621 color_success=""
1622 color_reset=""
1623 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001624 echo
1625 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001626 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001627 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001628 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001629 fi
1630 if [ $hours -gt 0 ] ; then
1631 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
1632 elif [ $mins -gt 0 ] ; then
1633 printf "(%02g:%02g (mm:ss))" $mins $secs
1634 elif [ $secs -gt 0 ] ; then
1635 printf "(%s seconds)" $secs
1636 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001637 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001638 echo
1639 return $ret
1640}
1641
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001642function make()
1643{
Dan Willemsene9842242017-07-28 13:00:13 -07001644 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001645}
1646
David Zeuthen1b126ff2015-09-30 17:10:48 -04001647function provision()
1648{
1649 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1650 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1651 return 1
1652 fi
1653 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1654 echo "There is no provisioning script for the device." >&2
1655 return 1
1656 fi
1657
1658 # Check if user really wants to do this.
1659 if [ "$1" = "--no-confirmation" ]; then
1660 shift 1
1661 else
1662 echo "This action will reflash your device."
1663 echo ""
1664 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1665 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001666 echo -n "Are you sure you want to do this (yes/no)? "
1667 read
David Zeuthen1b126ff2015-09-30 17:10:48 -04001668 if [[ "${REPLY}" != "yes" ]] ; then
1669 echo "Not taking any action. Exiting." >&2
1670 return 1
1671 fi
1672 fi
1673 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1674}
1675
Simran Basi424b8762017-08-23 12:03:04 -07001676function atest()
1677{
Jim Tangf258e7e2018-11-01 18:00:05 +08001678 # Let's use the built version over the prebuilt, then source code.
1679 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
1680 local built_atest=${ANDROID_HOST_OUT}/bin/atest
1681 local prebuilt_atest="$(gettop)"/prebuilts/asuite/atest/$os_arch/atest
1682 if [[ -x $built_atest ]]; then
1683 $built_atest "$@"
1684 elif [[ -x $prebuilt_atest ]]; then
1685 $prebuilt_atest "$@"
1686 else
1687 # TODO: once prebuilt atest released, remove the source code section
1688 # and change the location of atest_completion.sh in addcompletions().
1689 "$(gettop)"/tools/tradefederation/core/atest/atest.py "$@"
1690 fi
Simran Basi424b8762017-08-23 12:03:04 -07001691}
1692
Jim Tanga881a252018-06-19 16:34:41 +08001693# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001694function enable_zsh_completion() {
1695 # Don't override user's options if bash-style completion is already enabled.
1696 if ! declare -f complete >/dev/null; then
1697 autoload -U compinit && compinit
1698 autoload -U bashcompinit && bashcompinit
1699 fi
Jim Tanga881a252018-06-19 16:34:41 +08001700}
1701
1702function validate_current_shell() {
1703 local current_sh="$(ps -o command -p $$)"
1704 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001705 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001706 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001707 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001708 *zsh*)
1709 function check_type() { type "$1"; }
Patrik Fimmldf248e62018-10-15 18:15:12 +02001710 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001711 *)
Jim Tanga881a252018-06-19 16:34:41 +08001712 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 -07001713 ;;
1714 esac
Jim Tanga881a252018-06-19 16:34:41 +08001715}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001716
Kevin Chengbf89aff2018-05-22 14:07:50 -07001717function acloud()
1718{
1719 # Let's use the built version over the prebuilt.
1720 local built_acloud=${ANDROID_HOST_OUT}/bin/acloud
1721 if [ -f $built_acloud ]; then
1722 $built_acloud "$@"
1723 return $?
1724 fi
1725
1726 local host_os_arch=$(get_build_var HOST_PREBUILT_TAG)
1727 case $host_os_arch in
1728 linux-x86) "$(gettop)"/prebuilts/asuite/acloud/linux-x86/acloud "$@"
1729 ;;
Kevin Chengf0497e42018-12-03 01:01:27 -08001730 darwin-x86) "$(gettop)"/prebuilts/asuite/acloud/darwin-x86/acloud "$@"
1731 ;;
Kevin Chengbf89aff2018-05-22 14:07:50 -07001732 *)
1733 echo "acloud is not supported on your host arch: $host_os_arch"
1734 ;;
1735 esac
1736}
1737
patricktu345c9ae2018-11-29 15:25:40 +08001738function aidegen()
1739{
1740 # Always use the prebuilt version.
1741 local host_os_arch=$(get_build_var HOST_PREBUILT_TAG)
1742 case $host_os_arch in
1743 linux-x86) "$(gettop)"/prebuilts/asuite/aidegen/linux-x86/aidegen "$@"
1744 ;;
1745 *)
1746 echo "aidegen is not supported on your host arch: $host_os_arch"
1747 ;;
1748 esac
1749}
1750
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001751# Execute the contents of any vendorsetup.sh files we can find.
Jim Tanga881a252018-06-19 16:34:41 +08001752function source_vendorsetup() {
1753 for dir in device vendor product; do
1754 for f in $(test -d $dir && \
1755 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
1756 echo "including $f"; . $f
1757 done
1758 done
1759}
Kenny Root52aa81c2011-07-15 11:07:06 -07001760
Jim Tanga881a252018-06-19 16:34:41 +08001761validate_current_shell
1762source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001763addcompletions