blob: 0ec7e6ffd2e1a31e8e33d2b4c8143d6518027a80 [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.
Elliott Hughesf71c05a2020-03-06 16:46:59 -080011- tapas: tapas [<App1> <App2> ...] [arm|x86|arm64|x86_64] [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.
Dan Willemsen67074fe2019-10-30 12:35:34 -070014- mm: Builds and installs all of the modules in the current directory, and their
15 dependencies.
16- mmm: Builds and installs all of the modules in the supplied directories, and their
17 dependencies.
Steven Moreland62054a42018-12-06 10:11:40 -080018 To limit the modules being built use the syntax: mmm dir/:target1,target2.
Dan Willemsen67074fe2019-10-30 12:35:34 -070019- mma: Same as 'mm'
20- mmma: Same as 'mmm'
Steven Moreland62054a42018-12-06 10:11:40 -080021- provision: Flash device with all required partitions. Options will be passed on to fastboot.
22- cgrep: Greps on all local C/C++ files.
23- ggrep: Greps on all local Gradle files.
Orion Hodson831472d2019-10-25 11:35:15 +010024- gogrep: Greps on all local Go files.
Steven Moreland62054a42018-12-06 10:11:40 -080025- jgrep: Greps on all local Java files.
26- resgrep: Greps on all local res/*.xml files.
27- mangrep: Greps on all local AndroidManifest.xml files.
Jaewoong Jung892d0fe2019-05-04 10:06:28 -070028- mgrep: Greps on all local Makefiles and *.bp files.
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -060029- owngrep: Greps on all local OWNERS files.
Steven Moreland62054a42018-12-06 10:11:40 -080030- sepgrep: Greps on all local sepolicy files.
31- sgrep: Greps on all local source files.
32- godir: Go to the directory containing a file.
33- allmod: List all modules.
34- gomod: Go to the directory containing a module.
Rett Berg78d1c932019-01-24 14:34:23 -080035- pathmod: Get the directory containing a module.
Steven Morelandab25c7a2020-04-09 12:05:34 -070036- refreshmod: Refresh list of modules for allmod/gomod/pathmod.
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070037
Roland Levillain39341922015-10-20 12:48:19 +010038Environment options:
Steven Moreland115d1f52019-09-26 16:30:28 -070039- SANITIZE_HOST: Set to 'address' to use ASAN for all host modules.
Sasha Smundak9f27cc02019-01-31 13:25:31 -080040- ANDROID_QUIET_BUILD: set to 'true' to display only the essential messages.
Dan Albert4ae5d4b2014-10-31 16:23:08 -070041
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070042Look at the source to view more functions. The complete list is:
43EOF
Christopher Ferris55257d22017-03-23 11:08:58 -070044 local T=$(gettop)
45 local A=""
46 local i
Jacky Cao89483b82015-05-15 22:12:53 +080047 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 -070048 A="$A $i"
49 done
50 echo $A
51}
52
Ying Wang08800fd2016-03-03 20:57:21 -080053# Get all the build variables needed by this script in a single call to the build system.
Matt Alexanderd9c56562020-05-21 10:49:17 +000054function build_build_var_cache()
55{
Christopher Ferris55257d22017-03-23 11:08:58 -070056 local T=$(gettop)
Ying Wang08800fd2016-03-03 20:57:21 -080057 # Grep out the variable names from the script.
Jim Tanga881a252018-06-19 16:34:41 +080058 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' ' '`)
59 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 -080060 # Call the build system to dump the "<val>=<value>" pairs as a shell script.
Steven Moreland05402962018-01-05 12:13:11 -080061 build_dicts_script=`\builtin cd $T; build/soong/soong_ui.bash --dumpvars-mode \
Jim Tanga881a252018-06-19 16:34:41 +080062 --vars="${cached_vars[*]}" \
63 --abs-vars="${cached_abs_vars[*]}" \
Dan Willemsenaf88c412017-07-14 11:29:44 -070064 --var-prefix=var_cache_ \
65 --abs-var-prefix=abs_var_cache_`
Ying Wang08800fd2016-03-03 20:57:21 -080066 local ret=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +000067 if [ $ret -ne 0 ]
68 then
Ying Wang08800fd2016-03-03 20:57:21 -080069 unset build_dicts_script
70 return $ret
71 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -070072 # Execute the script to store the "<val>=<value>" pairs as shell variables.
Ying Wang08800fd2016-03-03 20:57:21 -080073 eval "$build_dicts_script"
Ying Wang08800fd2016-03-03 20:57:21 -080074 ret=$?
Ying Wangf0cb3972016-03-04 13:56:23 -080075 unset build_dicts_script
Matt Alexanderd9c56562020-05-21 10:49:17 +000076 if [ $ret -ne 0 ]
77 then
Ying Wang08800fd2016-03-03 20:57:21 -080078 return $ret
79 fi
80 BUILD_VAR_CACHE_READY="true"
81}
82
Ying Wangf0cb3972016-03-04 13:56:23 -080083# Delete the build var cache, so that we can still call into the build system
Ying Wang08800fd2016-03-03 20:57:21 -080084# to get build variables not listed in this script.
Matt Alexanderd9c56562020-05-21 10:49:17 +000085function destroy_build_var_cache()
86{
Ying Wang08800fd2016-03-03 20:57:21 -080087 unset BUILD_VAR_CACHE_READY
Christopher Ferris55257d22017-03-23 11:08:58 -070088 local v
Ying Wang08800fd2016-03-03 20:57:21 -080089 for v in $cached_vars; do
90 unset var_cache_$v
91 done
92 unset cached_vars
93 for v in $cached_abs_vars; do
94 unset abs_var_cache_$v
95 done
96 unset cached_abs_vars
97}
98
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -070099# Get the value of a build variable as an absolute path.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000100function get_abs_build_var()
101{
102 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
103 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800104 eval "echo \"\${abs_var_cache_$1}\""
Matt Alexanderd9c56562020-05-21 10:49:17 +0000105 return
Ying Wang08800fd2016-03-03 20:57:21 -0800106 fi
107
Christopher Ferris55257d22017-03-23 11:08:58 -0700108 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700109 if [ ! "$T" ]; then
110 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
111 return
112 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700113 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700114}
115
116# Get the exact value of a build variable.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000117function get_build_var()
118{
119 if [ "$BUILD_VAR_CACHE_READY" = "true" ]
120 then
Vishwath Mohan7d35f002016-03-11 10:00:40 -0800121 eval "echo \"\${var_cache_$1}\""
Roland Levillain23c46cf2020-03-31 16:11:05 +0100122 return 0
Ying Wang08800fd2016-03-03 20:57:21 -0800123 fi
124
Christopher Ferris55257d22017-03-23 11:08:58 -0700125 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700126 if [ ! "$T" ]; then
127 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
Roland Levillain23c46cf2020-03-31 16:11:05 +0100128 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700129 fi
Dan Willemsenaf88c412017-07-14 11:29:44 -0700130 (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800131}
132
133# check to see if the supplied product is one we can build
Matt Alexanderd9c56562020-05-21 10:49:17 +0000134function check_product()
135{
Christopher Ferris55257d22017-03-23 11:08:58 -0700136 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800137 if [ ! "$T" ]; then
138 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
139 return
140 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700141 TARGET_PRODUCT=$1 \
142 TARGET_BUILD_VARIANT= \
143 TARGET_BUILD_TYPE= \
Joe Onoratoda12daf2010-06-09 18:18:31 -0700144 TARGET_BUILD_APPS= \
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800145 get_build_var TARGET_DEVICE > /dev/null
146 # hide successful answers, but allow the errors to show
147}
148
149VARIANT_CHOICES=(user userdebug eng)
150
151# check to see if the supplied variant is valid
Matt Alexanderd9c56562020-05-21 10:49:17 +0000152function check_variant()
153{
Christopher Ferris55257d22017-03-23 11:08:58 -0700154 local v
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800155 for v in ${VARIANT_CHOICES[@]}
156 do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000157 if [ "$v" = "$1" ]
158 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800159 return 0
160 fi
161 done
162 return 1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700163}
164
Matt Alexanderd9c56562020-05-21 10:49:17 +0000165function setpaths()
166{
Christopher Ferris55257d22017-03-23 11:08:58 -0700167 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700168 if [ ! "$T" ]; then
169 echo "Couldn't locate the top of the tree. Try setting TOP."
170 return
171 fi
172
173 ##################################################################
174 # #
175 # Read me before you modify this code #
176 # #
177 # This function sets ANDROID_BUILD_PATHS to what it is adding #
178 # to PATH, and the next time it is run, it removes that from #
179 # PATH. This is required so lunch can be run more than once #
180 # and still have working paths. #
181 # #
182 ##################################################################
183
Raphael Mollc639c782011-06-20 17:25:01 -0700184 # Note: on windows/cygwin, ANDROID_BUILD_PATHS will contain spaces
185 # due to "C:\Program Files" being in the path.
186
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700187 # out with the old
Matt Alexanderd9c56562020-05-21 10:49:17 +0000188 if [ -n "$ANDROID_BUILD_PATHS" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700189 export PATH=${PATH/$ANDROID_BUILD_PATHS/}
190 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000191 if [ -n "$ANDROID_PRE_BUILD_PATHS" ] ; then
Doug Zongker29034982011-04-22 08:16:56 -0700192 export PATH=${PATH/$ANDROID_PRE_BUILD_PATHS/}
Ying Wangaa1c9b52012-11-26 20:51:59 -0800193 # strip leading ':', if any
194 export PATH=${PATH/:%/}
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500195 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700196
197 # and in with the new
Christopher Ferris55257d22017-03-23 11:08:58 -0700198 local prebuiltdir=$(getprebuilt)
199 local gccprebuiltdir=$(get_abs_build_var ANDROID_GCC_PREBUILTS)
Raphael732936d2011-06-22 14:35:32 -0700200
Ben Cheng8bc4c432012-11-16 13:29:13 -0800201 # defined in core/config.mk
Christopher Ferris55257d22017-03-23 11:08:58 -0700202 local targetgccversion=$(get_build_var TARGET_GCC_VERSION)
203 local targetgccversion2=$(get_build_var 2ND_TARGET_GCC_VERSION)
Ben Cheng15266702012-12-10 16:04:39 -0800204 export TARGET_GCC_VERSION=$targetgccversion
Ben Cheng8bc4c432012-11-16 13:29:13 -0800205
Raphael Mollc639c782011-06-20 17:25:01 -0700206 # The gcc toolchain does not exists for windows/cygwin. In this case, do not reference it.
Ben Chengfba67bf2014-02-25 10:27:07 -0800207 export ANDROID_TOOLCHAIN=
208 export ANDROID_TOOLCHAIN_2ND_ARCH=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200209 local ARCH=$(get_build_var TARGET_ARCH)
Christopher Ferris55257d22017-03-23 11:08:58 -0700210 local toolchaindir toolchaindir2=
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200211 case $ARCH in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000212 x86) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Mark D Horn7d0ede72012-03-14 14:20:30 -0700213 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000214 x86_64) toolchaindir=x86/x86_64-linux-android-$targetgccversion/bin
Pavel Chupinfd82a492012-11-26 09:50:07 +0400215 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000216 arm) toolchaindir=arm/arm-linux-androideabi-$targetgccversion/bin
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200217 ;;
Matt Alexanderd9c56562020-05-21 10:49:17 +0000218 arm64) toolchaindir=aarch64/aarch64-linux-android-$targetgccversion/bin;
219 toolchaindir2=arm/arm-linux-androideabi-$targetgccversion2/bin
Ben Chengdb4fc202013-10-04 16:02:59 -0700220 ;;
David 'Digit' Turner2056c252012-04-17 14:50:47 +0200221 *)
222 echo "Can't find toolchain for unknown architecture: $ARCH"
223 toolchaindir=xxxxxxxxx
Mark D Horn7d0ede72012-03-14 14:20:30 -0700224 ;;
225 esac
Jing Yuf5172c72012-03-29 20:45:50 -0700226 if [ -d "$gccprebuiltdir/$toolchaindir" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800227 export ANDROID_TOOLCHAIN=$gccprebuiltdir/$toolchaindir
Raphael Mollc639c782011-06-20 17:25:01 -0700228 fi
Raphael732936d2011-06-22 14:35:32 -0700229
Christopher Ferris55257d22017-03-23 11:08:58 -0700230 if [ "$toolchaindir2" -a -d "$gccprebuiltdir/$toolchaindir2" ]; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800231 export ANDROID_TOOLCHAIN_2ND_ARCH=$gccprebuiltdir/$toolchaindir2
232 fi
233
Jeff Vander Stoep5f50f052015-06-12 09:56:39 -0700234 export ANDROID_DEV_SCRIPTS=$T/development/scripts:$T/prebuilts/devtools/tools:$T/external/selinux/prebuilts/bin
Yueyao Zhuefc786a2017-04-07 14:11:54 -0700235
236 # add kernel specific binaries
237 case $(uname -s) in
238 Linux)
239 export ANDROID_DEV_SCRIPTS=$ANDROID_DEV_SCRIPTS:$T/prebuilts/misc/linux-x86/dtc:$T/prebuilts/misc/linux-x86/libufdt
240 ;;
241 *)
242 ;;
243 esac
244
Torne (Richard Coles)0091bae2017-10-03 16:31:18 -0400245 ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
246 if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
247 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
248 fi
Yi Kongdfd00b12019-05-21 16:00:04 -0700249 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS
250
251 # Append llvm binutils prebuilts path to ANDROID_BUILD_PATHS.
252 local ANDROID_LLVM_BINUTILS=$(get_abs_build_var ANDROID_CLANG_PREBUILTS)/llvm-binutils-stable
253 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_LLVM_BINUTILS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200254
Stephen Hinesaa8d72c2020-02-04 09:15:18 -0800255 # Set up ASAN_SYMBOLIZER_PATH for SANITIZE_HOST=address builds.
256 export ASAN_SYMBOLIZER_PATH=$ANDROID_LLVM_BINUTILS/llvm-symbolizer
257
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200258 # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
259 # to ensure that the corresponding 'emulator' binaries are used.
260 case $(uname -s) in
261 Darwin)
262 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/darwin-x86_64
263 ;;
264 Linux)
265 ANDROID_EMULATOR_PREBUILTS=$T/prebuilts/android-emulator/linux-x86_64
266 ;;
267 *)
268 ANDROID_EMULATOR_PREBUILTS=
269 ;;
270 esac
271 if [ -n "$ANDROID_EMULATOR_PREBUILTS" -a -d "$ANDROID_EMULATOR_PREBUILTS" ]; then
Yi Kongdfd00b12019-05-21 16:00:04 -0700272 ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_EMULATOR_PREBUILTS
David 'Digit' Turner94d16e52014-05-05 16:13:50 +0200273 export ANDROID_EMULATOR_PREBUILTS
274 fi
275
Jim Tangb3fda302018-12-22 10:24:55 +0800276 # Append asuite prebuilts path to ANDROID_BUILD_PATHS.
277 local os_arch=$(get_build_var HOST_PREBUILT_TAG)
Ryan Prichard8426a192019-07-15 18:05:29 -0700278 local ACLOUD_PATH="$T/prebuilts/asuite/acloud/$os_arch"
279 local AIDEGEN_PATH="$T/prebuilts/asuite/aidegen/$os_arch"
280 local ATEST_PATH="$T/prebuilts/asuite/atest/$os_arch"
281 export ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ACLOUD_PATH:$AIDEGEN_PATH:$ATEST_PATH:
Jim Tangb3fda302018-12-22 10:24:55 +0800282
Ryan Prichard8426a192019-07-15 18:05:29 -0700283 export PATH=$ANDROID_BUILD_PATHS$PATH
Jim Tang22f4c322018-12-17 15:21:53 +0800284
285 # out with the duplicate old
286 if [ -n $ANDROID_PYTHONPATH ]; then
287 export PYTHONPATH=${PYTHONPATH//$ANDROID_PYTHONPATH/}
288 fi
289 # and in with the new
290 export ANDROID_PYTHONPATH=$T/development/python-packages:
Jim Tangc4dba1d2019-07-25 16:54:27 +0800291 if [ -n $VENDOR_PYTHONPATH ]; then
292 ANDROID_PYTHONPATH=$ANDROID_PYTHONPATH$VENDOR_PYTHONPATH
293 fi
Jim Tang22f4c322018-12-17 15:21:53 +0800294 export PYTHONPATH=$ANDROID_PYTHONPATH$PYTHONPATH
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800295
Colin Crosse97e6932017-06-30 16:01:45 -0700296 export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
297 export JAVA_HOME=$ANDROID_JAVA_HOME
298 export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
299 export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
300 export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
Jeff Hamilton4a1c70e2010-06-21 18:26:38 -0500301
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800302 unset ANDROID_PRODUCT_OUT
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700303 export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
304 export OUT=$ANDROID_PRODUCT_OUT
305
Jeff Brown8fd5cce2011-03-24 17:03:06 -0700306 unset ANDROID_HOST_OUT
307 export ANDROID_HOST_OUT=$(get_abs_build_var HOST_OUT)
308
Simran Basidd050ed2017-02-13 13:46:48 -0800309 unset ANDROID_HOST_OUT_TESTCASES
310 export ANDROID_HOST_OUT_TESTCASES=$(get_abs_build_var HOST_OUT_TESTCASES)
311
312 unset ANDROID_TARGET_OUT_TESTCASES
313 export ANDROID_TARGET_OUT_TESTCASES=$(get_abs_build_var TARGET_OUT_TESTCASES)
314
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800315 # needed for building linux on MacOS
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700316 # TODO: fix the path
317 #export HOST_EXTRACFLAGS="-I "$T/system/kernel_headers/host_include
318}
319
Matt Alexanderd9c56562020-05-21 10:49:17 +0000320function printconfig()
321{
Christopher Ferris55257d22017-03-23 11:08:58 -0700322 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800323 if [ ! "$T" ]; then
324 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
325 return
326 fi
327 get_build_var report_config
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700328}
329
Matt Alexanderd9c56562020-05-21 10:49:17 +0000330function set_stuff_for_environment()
331{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800332 setpaths
333 set_sequence_number
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700334
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800335 export ANDROID_BUILD_TOP=$(gettop)
Ben Chengaac3f812013-08-13 14:38:15 -0700336 # With this environment variable new GCC can apply colors to warnings/errors
337 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700338}
339
Matt Alexanderd9c56562020-05-21 10:49:17 +0000340function set_sequence_number()
341{
Colin Cross88737132017-03-21 17:41:03 -0700342 export BUILD_ENV_SEQUENCE_NUMBER=13
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700343}
344
Makoto Onukida971062018-06-18 10:15:19 -0700345# Takes a command name, and check if it's in ENVSETUP_NO_COMPLETION or not.
346function should_add_completion() {
Jim Tanga881a252018-06-19 16:34:41 +0800347 local cmd="$(basename $1| sed 's/_completion//' |sed 's/\.\(.*\)*sh$//')"
Makoto Onukida971062018-06-18 10:15:19 -0700348 case :"$ENVSETUP_NO_COMPLETION": in
Jim Tanga881a252018-06-19 16:34:41 +0800349 *:"$cmd":*)
350 return 1
351 ;;
Makoto Onukida971062018-06-18 10:15:19 -0700352 esac
353 return 0
354}
355
Matt Alexanderd9c56562020-05-21 10:49:17 +0000356function addcompletions()
357{
Kenny Root52aa81c2011-07-15 11:07:06 -0700358 local T dir f
359
Jim Tanga881a252018-06-19 16:34:41 +0800360 # Keep us from trying to run in something that's neither bash nor zsh.
361 if [ -z "$BASH_VERSION" -a -z "$ZSH_VERSION" ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700362 return
363 fi
364
365 # Keep us from trying to run in bash that's too old.
Jim Tanga881a252018-06-19 16:34:41 +0800366 if [ -n "$BASH_VERSION" -a ${BASH_VERSINFO[0]} -lt 3 ]; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700367 return
368 fi
369
Jim Tanga881a252018-06-19 16:34:41 +0800370 local completion_files=(
371 system/core/adb/adb.bash
372 system/core/fastboot/fastboot.bash
Jim Tangb3fda302018-12-22 10:24:55 +0800373 tools/asuite/asuite.sh
Jim Tanga881a252018-06-19 16:34:41 +0800374 )
Makoto Onukida971062018-06-18 10:15:19 -0700375 # Completion can be disabled selectively to allow users to use non-standard completion.
376 # e.g.
377 # ENVSETUP_NO_COMPLETION=adb # -> disable adb completion
378 # ENVSETUP_NO_COMPLETION=adb:bit # -> disable adb and bit completion
Jim Tanga881a252018-06-19 16:34:41 +0800379 for f in ${completion_files[*]}; do
380 if [ -f "$f" ] && should_add_completion "$f"; then
Kenny Root52aa81c2011-07-15 11:07:06 -0700381 . $f
Elliott Hughesce18dd42018-04-03 13:49:48 -0700382 fi
383 done
Joe Onorato002a6c72016-10-20 16:39:49 -0700384
Matt Alexanderd9c56562020-05-21 10:49:17 +0000385 if should_add_completion bit ; then
Makoto Onukida971062018-06-18 10:15:19 -0700386 complete -C "bit --tab" bit
387 fi
Anton Hanssonece9c482019-02-04 18:15:39 +0000388 if [ -z "$ZSH_VERSION" ]; then
389 # Doesn't work in zsh.
390 complete -o nospace -F _croot croot
391 fi
Jim Tanga881a252018-06-19 16:34:41 +0800392 complete -F _lunch lunch
Steven Moreland62054a42018-12-06 10:11:40 -0800393
dimitry73b84812018-12-11 18:06:00 +0100394 complete -F _complete_android_module_names gomod
395 complete -F _complete_android_module_names m
Kenny Root52aa81c2011-07-15 11:07:06 -0700396}
397
Matt Alexanderd9c56562020-05-21 10:49:17 +0000398function choosetype()
399{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700400 echo "Build type choices are:"
401 echo " 1. release"
402 echo " 2. debug"
403 echo
404
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800405 local DEFAULT_NUM DEFAULT_VALUE
Jeff Browne33ba4c2011-07-11 22:11:46 -0700406 DEFAULT_NUM=1
407 DEFAULT_VALUE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700408
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800409 export TARGET_BUILD_TYPE=
410 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700411 while [ -z $TARGET_BUILD_TYPE ]
412 do
413 echo -n "Which would you like? ["$DEFAULT_NUM"] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000414 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800415 read ANSWER
416 else
417 echo $1
418 ANSWER=$1
419 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700420 case $ANSWER in
421 "")
422 export TARGET_BUILD_TYPE=$DEFAULT_VALUE
423 ;;
424 1)
425 export TARGET_BUILD_TYPE=release
426 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800427 release)
428 export TARGET_BUILD_TYPE=release
429 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700430 2)
431 export TARGET_BUILD_TYPE=debug
432 ;;
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800433 debug)
434 export TARGET_BUILD_TYPE=debug
435 ;;
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700436 *)
437 echo
438 echo "I didn't understand your response. Please try again."
439 echo
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700440 ;;
441 esac
Matt Alexanderd9c56562020-05-21 10:49:17 +0000442 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800443 break
444 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700445 done
446
Ying Wang08800fd2016-03-03 20:57:21 -0800447 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700448 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800449 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700450}
451
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800452#
453# This function isn't really right: It chooses a TARGET_PRODUCT
454# based on the list of boards. Usually, that gets you something
455# that kinda works with a generic product, but really, you should
456# pick a product by name.
457#
Matt Alexanderd9c56562020-05-21 10:49:17 +0000458function chooseproduct()
459{
Christopher Ferris55257d22017-03-23 11:08:58 -0700460 local default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000461 if [ "x$TARGET_PRODUCT" != x ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700462 default_value=$TARGET_PRODUCT
463 else
Ying Wang0a76df52015-06-08 11:57:26 -0700464 default_value=aosp_arm
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700465 fi
466
Ying Wang08800fd2016-03-03 20:57:21 -0800467 export TARGET_BUILD_APPS=
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800468 export TARGET_PRODUCT=
469 local ANSWER
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700470 while [ -z "$TARGET_PRODUCT" ]
471 do
Joe Onorato8849aed2009-04-29 15:56:47 -0700472 echo -n "Which product would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000473 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800474 read ANSWER
475 else
476 echo $1
477 ANSWER=$1
478 fi
479
Matt Alexanderd9c56562020-05-21 10:49:17 +0000480 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700481 export TARGET_PRODUCT=$default_value
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800482 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000483 if check_product $ANSWER
484 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800485 export TARGET_PRODUCT=$ANSWER
486 else
487 echo "** Not a valid product: $ANSWER"
488 fi
489 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000490 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800491 break
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700492 fi
493 done
494
Ying Wang08800fd2016-03-03 20:57:21 -0800495 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700496 set_stuff_for_environment
Ying Wang08800fd2016-03-03 20:57:21 -0800497 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700498}
499
Matt Alexanderd9c56562020-05-21 10:49:17 +0000500function choosevariant()
501{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800502 echo "Variant choices are:"
503 local index=1
504 local v
505 for v in ${VARIANT_CHOICES[@]}
506 do
507 # The product name is the name of the directory containing
508 # the makefile we found, above.
509 echo " $index. $v"
510 index=$(($index+1))
511 done
512
513 local default_value=eng
514 local ANSWER
515
516 export TARGET_BUILD_VARIANT=
517 while [ -z "$TARGET_BUILD_VARIANT" ]
518 do
519 echo -n "Which would you like? [$default_value] "
Matt Alexanderd9c56562020-05-21 10:49:17 +0000520 if [ -z "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800521 read ANSWER
522 else
523 echo $1
524 ANSWER=$1
525 fi
526
Matt Alexanderd9c56562020-05-21 10:49:17 +0000527 if [ -z "$ANSWER" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800528 export TARGET_BUILD_VARIANT=$default_value
Matt Alexanderd9c56562020-05-21 10:49:17 +0000529 elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
530 if [ "$ANSWER" -le "${#VARIANT_CHOICES[@]}" ] ; then
Guillaume Chelfice000fd2019-10-03 12:02:46 +0200531 export TARGET_BUILD_VARIANT=${VARIANT_CHOICES[@]:$(($ANSWER-1)):1}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800532 fi
533 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000534 if check_variant $ANSWER
535 then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800536 export TARGET_BUILD_VARIANT=$ANSWER
537 else
538 echo "** Not a valid variant: $ANSWER"
539 fi
540 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +0000541 if [ -n "$1" ] ; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800542 break
543 fi
544 done
545}
546
Matt Alexanderd9c56562020-05-21 10:49:17 +0000547function choosecombo()
548{
Jeff Browne33ba4c2011-07-11 22:11:46 -0700549 choosetype $1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700550
551 echo
552 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700553 chooseproduct $2
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700554
555 echo
556 echo
Jeff Browne33ba4c2011-07-11 22:11:46 -0700557 choosevariant $3
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800558
559 echo
Ying Wang08800fd2016-03-03 20:57:21 -0800560 build_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700561 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800562 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800563 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700564}
565
Matt Alexanderd9c56562020-05-21 10:49:17 +0000566function add_lunch_combo()
567{
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800568 if [ -n "$ZSH_VERSION" ]; then
569 echo -n "${funcfiletrace[1]}: "
570 else
571 echo -n "${BASH_SOURCE[1]}:${BASH_LINENO[0]}: "
572 fi
573 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 -0800574}
575
Matt Alexanderd9c56562020-05-21 10:49:17 +0000576function print_lunch_menu()
577{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700578 local uname=$(uname)
Roland Levillain23c46cf2020-03-31 16:11:05 +0100579 local choices
580 choices=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES 2>/dev/null)
581 local ret=$?
582
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700583 echo
584 echo "You're building on" $uname
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700585 echo
Roland Levillain23c46cf2020-03-31 16:11:05 +0100586
Matt Alexanderd9c56562020-05-21 10:49:17 +0000587 if [ $ret -ne 0 ]
588 then
Roland Levillain23c46cf2020-03-31 16:11:05 +0100589 echo "Warning: Cannot display lunch menu."
590 echo
591 echo "Note: You can invoke lunch with an explicit target:"
592 echo
593 echo " usage: lunch [target]" >&2
594 echo
595 return
596 fi
597
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700598 echo "Lunch menu... pick a combo:"
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800599
600 local i=1
601 local choice
Dan Willemsen91763e92019-10-03 15:13:12 -0700602 for choice in $(echo $choices)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800603 do
604 echo " $i. $choice"
605 i=$(($i+1))
606 done
607
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700608 echo
609}
610
Matt Alexanderd9c56562020-05-21 10:49:17 +0000611function lunch()
612{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800613 local answer
614
Steven Moreland92793dc2020-02-25 18:30:18 -0800615 if [[ $# -gt 1 ]]; then
616 echo "usage: lunch [target]" >&2
617 return 1
618 fi
619
620 if [ "$1" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800621 answer=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700622 else
623 print_lunch_menu
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700624 echo -n "Which would you like? [aosp_arm-eng] "
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800625 read answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700626 fi
627
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800628 local selection=
629
Matt Alexanderd9c56562020-05-21 10:49:17 +0000630 if [ -z "$answer" ]
631 then
Jean-Baptiste Queru324c1232013-03-22 15:53:54 -0700632 selection=aosp_arm-eng
Matt Alexanderd9c56562020-05-21 10:49:17 +0000633 elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
634 then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800635 local choices=($(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES))
Matt Alexanderd9c56562020-05-21 10:49:17 +0000636 if [ $answer -le ${#choices[@]} ]
637 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800638 # array in zsh starts from 1 instead of 0.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000639 if [ -n "$ZSH_VERSION" ]
640 then
Jim Tang0e3397b2018-10-03 18:25:50 +0800641 selection=${choices[$(($answer))]}
642 else
643 selection=${choices[$(($answer-1))]}
644 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800645 fi
Colin Cross88737132017-03-21 17:41:03 -0700646 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800647 selection=$answer
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700648 fi
649
Joe Onoratoda12daf2010-06-09 18:18:31 -0700650 export TARGET_BUILD_APPS=
651
Colin Cross88737132017-03-21 17:41:03 -0700652 local product variant_and_version variant version
653
654 product=${selection%%-*} # Trim everything after first dash
655 variant_and_version=${selection#*-} # Trim everything up to first dash
656 if [ "$variant_and_version" != "$selection" ]; then
657 variant=${variant_and_version%%-*}
658 if [ "$variant" != "$variant_and_version" ]; then
659 version=${variant_and_version#*-}
660 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700661 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800662
Matt Alexanderd9c56562020-05-21 10:49:17 +0000663 if [ -z "$product" ]
664 then
Ying Wang08800fd2016-03-03 20:57:21 -0800665 echo
Colin Cross88737132017-03-21 17:41:03 -0700666 echo "Invalid lunch combo: $selection"
Jeff Browne33ba4c2011-07-11 22:11:46 -0700667 return 1
668 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800669
Colin Cross88737132017-03-21 17:41:03 -0700670 TARGET_PRODUCT=$product \
671 TARGET_BUILD_VARIANT=$variant \
672 TARGET_PLATFORM_VERSION=$version \
673 build_build_var_cache
Matt Alexanderd9c56562020-05-21 10:49:17 +0000674 if [ $? -ne 0 ]
675 then
Colin Cross88737132017-03-21 17:41:03 -0700676 return 1
677 fi
678
679 export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
680 export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
Colin Crossb105e362017-05-01 14:21:28 -0700681 if [ -n "$version" ]; then
682 export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
683 else
684 unset TARGET_PLATFORM_VERSION
685 fi
Jeff Browne33ba4c2011-07-11 22:11:46 -0700686 export TARGET_BUILD_TYPE=release
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700687
688 echo
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800689
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700690 set_stuff_for_environment
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800691 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800692 destroy_build_var_cache
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700693}
694
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700695unset COMMON_LUNCH_CHOICES_CACHE
Jeff Davidson513d7a42010-08-02 10:00:44 -0700696# Tab completion for lunch.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000697function _lunch()
698{
Jeff Davidson513d7a42010-08-02 10:00:44 -0700699 local cur prev opts
700 COMPREPLY=()
701 cur="${COMP_WORDS[COMP_CWORD]}"
702 prev="${COMP_WORDS[COMP_CWORD-1]}"
703
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700704 if [ -z "$COMMON_LUNCH_CHOICES_CACHE" ]; then
Dan Willemsen5436c7e2019-02-11 21:31:47 -0800705 COMMON_LUNCH_CHOICES_CACHE=$(TARGET_BUILD_APPS= get_build_var COMMON_LUNCH_CHOICES)
Dan Willemsenaf2e1f82018-04-04 15:41:41 -0700706 fi
707
708 COMPREPLY=( $(compgen -W "${COMMON_LUNCH_CHOICES_CACHE}" -- ${cur}) )
Jeff Davidson513d7a42010-08-02 10:00:44 -0700709 return 0
710}
Jeff Davidson513d7a42010-08-02 10:00:44 -0700711
Joe Onoratoda12daf2010-06-09 18:18:31 -0700712# Configures the build to build unbundled apps.
Doug Zongker0d8179e2014-04-16 11:34:34 -0700713# Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000714function tapas()
715{
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700716 local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800717 local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|arm64|x86_64)$' | xargs)"
Doug Zongker0d8179e2014-04-16 11:34:34 -0700718 local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700719 local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Elliott Hughesf71c05a2020-03-06 16:46:59 -0800720 local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|arm64|x86_64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
Joe Onoratoda12daf2010-06-09 18:18:31 -0700721
Jeff Gaston9fb05d82017-08-21 18:27:00 -0700722 if [ "$showHelp" != "" ]; then
723 $(gettop)/build/make/tapasHelp.sh
724 return
725 fi
726
Ying Wang67f02922012-08-22 10:25:20 -0700727 if [ $(echo $arch | wc -w) -gt 1 ]; then
728 echo "tapas: Error: Multiple build archs supplied: $arch"
729 return
730 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700731 if [ $(echo $variant | wc -w) -gt 1 ]; then
732 echo "tapas: Error: Multiple build variants supplied: $variant"
733 return
734 fi
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700735 if [ $(echo $density | wc -w) -gt 1 ]; then
736 echo "tapas: Error: Multiple densities supplied: $density"
737 return
738 fi
Ying Wang67f02922012-08-22 10:25:20 -0700739
Ying Wang0a76df52015-06-08 11:57:26 -0700740 local product=aosp_arm
Ying Wang67f02922012-08-22 10:25:20 -0700741 case $arch in
Matt Alexanderd9c56562020-05-21 10:49:17 +0000742 x86) product=aosp_x86;;
743 arm64) product=aosp_arm64;;
744 x86_64) product=aosp_x86_64;;
Ying Wang67f02922012-08-22 10:25:20 -0700745 esac
Joe Onoratoda12daf2010-06-09 18:18:31 -0700746 if [ -z "$variant" ]; then
747 variant=eng
748 fi
Ying Wangc048c9b2010-06-24 15:08:33 -0700749 if [ -z "$apps" ]; then
750 apps=all
751 fi
Justin Morey29d225c2014-11-04 13:35:51 -0600752 if [ -z "$density" ]; then
753 density=alldpi
754 fi
Joe Onoratoda12daf2010-06-09 18:18:31 -0700755
Ying Wang67f02922012-08-22 10:25:20 -0700756 export TARGET_PRODUCT=$product
Joe Onoratoda12daf2010-06-09 18:18:31 -0700757 export TARGET_BUILD_VARIANT=$variant
Jeff Hamilton5069bd62014-09-04 21:28:00 -0700758 export TARGET_BUILD_DENSITY=$density
Joe Onoratoda12daf2010-06-09 18:18:31 -0700759 export TARGET_BUILD_TYPE=release
760 export TARGET_BUILD_APPS=$apps
761
Ying Wang08800fd2016-03-03 20:57:21 -0800762 build_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700763 set_stuff_for_environment
764 printconfig
Ying Wang08800fd2016-03-03 20:57:21 -0800765 destroy_build_var_cache
Joe Onoratoda12daf2010-06-09 18:18:31 -0700766}
767
Matt Alexanderd9c56562020-05-21 10:49:17 +0000768function gettop
769{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700770 local TOPFILE=build/make/core/envsetup.mk
Matt Alexanderd9c56562020-05-21 10:49:17 +0000771 if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
Brian Carlstroma5c4f172014-09-12 00:33:25 -0700772 # The following circumlocution ensures we remove symlinks from TOP.
773 (cd $TOP; PWD= /bin/pwd)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700774 else
Matt Alexanderd9c56562020-05-21 10:49:17 +0000775 if [ -f $TOPFILE ] ; then
Dan Bornsteind0b274d2009-11-24 15:48:50 -0800776 # The following circumlocution (repeated below as well) ensures
777 # that we record the true directory name and not one that is
778 # faked up with symlink names.
779 PWD= /bin/pwd
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700780 else
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -0800781 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700782 local T=
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700783 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
Ying Wang9cd17642012-12-13 10:52:07 -0800784 \cd ..
synergyb112a402013-07-05 19:47:47 -0700785 T=`PWD= /bin/pwd -P`
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700786 done
Ying Wang9cd17642012-12-13 10:52:07 -0800787 \cd $HERE
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700788 if [ -f "$T/$TOPFILE" ]; then
789 echo $T
790 fi
791 fi
792 fi
793}
794
Matt Alexanderd9c56562020-05-21 10:49:17 +0000795function croot()
796{
Christopher Ferris55257d22017-03-23 11:08:58 -0700797 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700798 if [ "$T" ]; then
Marie Janssen32ec50a2016-04-21 16:53:39 -0700799 if [ "$1" ]; then
800 \cd $(gettop)/$1
801 else
802 \cd $(gettop)
803 fi
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700804 else
805 echo "Couldn't locate the top of the tree. Try setting TOP."
806 fi
807}
808
Matt Alexanderd9c56562020-05-21 10:49:17 +0000809function _croot()
810{
Anton Hanssonece9c482019-02-04 18:15:39 +0000811 local T=$(gettop)
812 if [ "$T" ]; then
813 local cur="${COMP_WORDS[COMP_CWORD]}"
814 k=0
815 for c in $(compgen -d ${T}/${cur}); do
816 COMPREPLY[k++]=${c#${T}/}/
817 done
818 fi
819}
820
Matt Alexanderd9c56562020-05-21 10:49:17 +0000821function cproj()
822{
Colin Cross6cdc5d22017-10-20 11:37:33 -0700823 local TOPFILE=build/make/core/envsetup.mk
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700824 local HERE=$PWD
Christopher Ferris55257d22017-03-23 11:08:58 -0700825 local T=
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700826 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
827 T=$PWD
828 if [ -f "$T/Android.mk" ]; then
Ying Wang9cd17642012-12-13 10:52:07 -0800829 \cd $T
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700830 return
831 fi
Ying Wang9cd17642012-12-13 10:52:07 -0800832 \cd ..
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700833 done
Ying Wang9cd17642012-12-13 10:52:07 -0800834 \cd $HERE
Joe Onorato2a5d4d82009-07-30 10:23:21 -0700835 echo "can't find Android.mk"
836}
837
Daniel Sandler47e0a882013-07-30 13:23:52 -0400838# simplified version of ps; output in the form
839# <pid> <procname>
840function qpid() {
841 local prepend=''
842 local append=''
Matt Alexanderd9c56562020-05-21 10:49:17 +0000843 if [ "$1" = "--exact" ]; then
Daniel Sandler47e0a882013-07-30 13:23:52 -0400844 prepend=' '
845 append='$'
846 shift
Matt Alexanderd9c56562020-05-21 10:49:17 +0000847 elif [ "$1" = "--help" -o "$1" = "-h" ]; then
Ying Wang08800fd2016-03-03 20:57:21 -0800848 echo "usage: qpid [[--exact] <process name|pid>"
849 return 255
850 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400851
852 local EXE="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000853 if [ "$EXE" ] ; then
Ying Wang08800fd2016-03-03 20:57:21 -0800854 qpid | \grep "$prepend$EXE$append"
855 else
856 adb shell ps \
857 | tr -d '\r' \
858 | sed -e 1d -e 's/^[^ ]* *\([0-9]*\).* \([^ ]*\)$/\1 \2/'
859 fi
Daniel Sandler47e0a882013-07-30 13:23:52 -0400860}
861
Iliyan Malcheve675cfb2014-11-03 17:04:47 -0800862# coredump_setup - enable core dumps globally for any process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700863# that has the core-file-size limit set correctly
864#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800865# NOTE: You must call also coredump_enable for a specific process
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700866# if its core-file-size limit is not set already.
867# NOTE: Core dumps are written to ramdisk; they will not survive a reboot!
868
Matt Alexanderd9c56562020-05-21 10:49:17 +0000869function coredump_setup()
870{
871 echo "Getting root...";
872 adb root;
873 adb wait-for-device;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700874
Matt Alexanderd9c56562020-05-21 10:49:17 +0000875 echo "Remounting root partition read-write...";
876 adb shell mount -w -o remount -t rootfs rootfs;
877 sleep 1;
878 adb wait-for-device;
879 adb shell mkdir -p /cores;
880 adb shell mount -t tmpfs tmpfs /cores;
881 adb shell chmod 0777 /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700882
Matt Alexanderd9c56562020-05-21 10:49:17 +0000883 echo "Granting SELinux permission to dump in /cores...";
884 adb shell restorecon -R /cores;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700885
Matt Alexanderd9c56562020-05-21 10:49:17 +0000886 echo "Set core pattern.";
887 adb shell 'echo /cores/core.%p > /proc/sys/kernel/core_pattern';
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700888
Ying Wang08800fd2016-03-03 20:57:21 -0800889 echo "Done."
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700890}
891
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800892# coredump_enable - enable core dumps for the specified process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000893# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700894#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800895# NOTE: coredump_setup must have been called as well for a core
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700896# dump to actually be generated.
897
Matt Alexanderd9c56562020-05-21 10:49:17 +0000898function coredump_enable()
899{
900 local PID=$1;
Ying Wang08800fd2016-03-03 20:57:21 -0800901 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000902 printf "Expecting a PID!\n";
903 return;
904 fi;
905 echo "Setting core limit for $PID to infinite...";
Elliott Hughes910a3552016-03-07 13:53:53 -0800906 adb shell /system/bin/ulimit -p $PID -c unlimited
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700907}
908
909# core - send SIGV and pull the core for process
Matt Alexanderd9c56562020-05-21 10:49:17 +0000910# $1 = PID of process (e.g., $(pid mediaserver))
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700911#
Iliyan Malchevaf5de972014-11-04 20:57:37 -0800912# NOTE: coredump_setup must be called once per boot for core dumps to be
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700913# enabled globally.
914
Matt Alexanderd9c56562020-05-21 10:49:17 +0000915function core()
916{
917 local PID=$1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700918
Ying Wang08800fd2016-03-03 20:57:21 -0800919 if [ -z "$PID" ]; then
Matt Alexanderd9c56562020-05-21 10:49:17 +0000920 printf "Expecting a PID!\n";
921 return;
922 fi;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700923
Matt Alexanderd9c56562020-05-21 10:49:17 +0000924 local CORENAME=core.$PID;
925 local COREPATH=/cores/$CORENAME;
926 local SIG=SEGV;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700927
Matt Alexanderd9c56562020-05-21 10:49:17 +0000928 coredump_enable $1;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700929
Matt Alexanderd9c56562020-05-21 10:49:17 +0000930 local done=0;
Ying Wang08800fd2016-03-03 20:57:21 -0800931 while [ $(adb shell "[ -d /proc/$PID ] && echo -n yes") ]; do
Matt Alexanderd9c56562020-05-21 10:49:17 +0000932 printf "\tSending SIG%s to %d...\n" $SIG $PID;
933 adb shell kill -$SIG $PID;
934 sleep 1;
935 done;
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700936
Matt Alexanderd9c56562020-05-21 10:49:17 +0000937 adb shell "while [ ! -f $COREPATH ] ; do echo waiting for $COREPATH to be generated; sleep 1; done"
938 echo "Done: core is under $COREPATH on device.";
Iliyan Malchev248f4d52014-10-28 18:00:42 -0700939}
940
Christopher Tate744ee802009-11-12 15:33:08 -0800941# systemstack - dump the current stack trace of all threads in the system process
942# to the usual ANR traces file
Matt Alexanderd9c56562020-05-21 10:49:17 +0000943function systemstack()
944{
Jeff Sharkeyf5824372013-02-19 17:00:46 -0800945 stacks system_server
946}
947
Michael Wrightaeed7212014-06-19 19:58:12 -0700948# Read the ELF header from /proc/$PID/exe to determine if the process is
949# 64-bit.
Matt Alexanderd9c56562020-05-21 10:49:17 +0000950function is64bit()
951{
Ben Chengfba67bf2014-02-25 10:27:07 -0800952 local PID="$1"
Matt Alexanderd9c56562020-05-21 10:49:17 +0000953 if [ "$PID" ] ; then
954 if [[ "$(adb shell cat /proc/$PID/exe | xxd -l 1 -s 4 -p)" -eq "02" ]] ; then
Ben Chengfba67bf2014-02-25 10:27:07 -0800955 echo "64"
956 else
957 echo ""
958 fi
959 else
960 echo ""
961 fi
962}
963
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700964case `uname -s` in
965 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000966 function sgrep()
967 {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800968 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|proto)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400969 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700970 }
Matt Alexanderd9c56562020-05-21 10:49:17 +0000971
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700972 ;;
973 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +0000974 function sgrep()
975 {
Christopher Tatee2fe9592020-03-05 11:34:16 -0800976 find . -name .repo -prune -o -name .git -prune -o -type f -iregex '.*\.\(c\|h\|cc\|cpp\|hpp\|S\|java\|xml\|sh\|mk\|aidl\|vts\|proto\)' \
Mike Frysinger5e479732015-09-22 18:13:48 -0400977 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -0700978 }
979 ;;
980esac
981
Matt Alexanderd9c56562020-05-21 10:49:17 +0000982function gettargetarch
983{
Raghu Gandham8da43102012-07-25 19:57:22 -0700984 get_build_var TARGET_ARCH
985}
986
Matt Alexanderd9c56562020-05-21 10:49:17 +0000987function ggrep()
988{
Mike Frysinger5e479732015-09-22 18:13:48 -0400989 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.gradle" \
990 -exec grep --color -n "$@" {} +
Jon Boekenoogencbca56f2014-04-07 10:57:38 -0700991}
992
Matt Alexanderd9c56562020-05-21 10:49:17 +0000993function gogrep()
994{
Orion Hodson831472d2019-10-25 11:35:15 +0100995 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.go" \
996 -exec grep --color -n "$@" {} +
997}
998
Matt Alexanderd9c56562020-05-21 10:49:17 +0000999function jgrep()
1000{
Mike Frysinger5e479732015-09-22 18:13:48 -04001001 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.java" \
1002 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001003}
1004
Matt Alexanderd9c56562020-05-21 10:49:17 +00001005function cgrep()
1006{
Mike Frysinger5e479732015-09-22 18:13:48 -04001007 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' \) \
1008 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001009}
1010
Matt Alexanderd9c56562020-05-21 10:49:17 +00001011function resgrep()
1012{
Christopher Ferris55257d22017-03-23 11:08:58 -07001013 local dir
Mike Frysinger5e479732015-09-22 18:13:48 -04001014 for dir in `find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -name res -type d`; do
1015 find $dir -type f -name '*\.xml' -exec grep --color -n "$@" {} +
1016 done
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001017}
1018
Matt Alexanderd9c56562020-05-21 10:49:17 +00001019function mangrep()
1020{
Mike Frysinger5e479732015-09-22 18:13:48 -04001021 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'AndroidManifest.xml' \
1022 -exec grep --color -n "$@" {} +
Jeff Sharkey50b61e92013-03-08 10:20:47 -08001023}
1024
Matt Alexanderd9c56562020-05-21 10:49:17 +00001025function owngrep()
1026{
Jeff Sharkeyf17cddf2019-08-21 12:51:26 -06001027 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -type f -name 'OWNERS' \
1028 -exec grep --color -n "$@" {} +
1029}
1030
Matt Alexanderd9c56562020-05-21 10:49:17 +00001031function sepgrep()
1032{
Mike Frysinger5e479732015-09-22 18:13:48 -04001033 find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -name sepolicy -type d \
1034 -exec grep --color -n -r --exclude-dir=\.git "$@" {} +
Alex Klyubinba5fc8e2013-05-06 14:11:48 -07001035}
1036
Matt Alexanderd9c56562020-05-21 10:49:17 +00001037function rcgrep()
1038{
Mike Frysinger5e479732015-09-22 18:13:48 -04001039 find . -name .repo -prune -o -name .git -prune -o -name out -prune -o -type f -name "*\.rc*" \
1040 -exec grep --color -n "$@" {} +
Jeff Sharkeyea0068a2015-02-26 14:13:46 -08001041}
1042
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001043case `uname -s` in
1044 Darwin)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001045 function mgrep()
1046 {
Orion Hodson831472d2019-10-25 11:35:15 +01001047 find -E . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o \( -iregex '.*/(Makefile|Makefile\..*|.*\.make|.*\.mak|.*\.mk|.*\.bp)' -o -regex '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001048 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001049 }
1050
Matt Alexanderd9c56562020-05-21 10:49:17 +00001051 function treegrep()
1052 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001053 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 -04001054 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001055 }
1056
1057 ;;
1058 *)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001059 function mgrep()
1060 {
Orion Hodson831472d2019-10-25 11:35:15 +01001061 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 '(.*/)?(build|soong)/.*[^/]*\.go' \) -type f \
Mike Frysinger5e479732015-09-22 18:13:48 -04001062 -exec grep --color -n "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001063 }
1064
Matt Alexanderd9c56562020-05-21 10:49:17 +00001065 function treegrep()
1066 {
Aurelio Jargas67edd152018-11-06 17:25:11 +01001067 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 -04001068 -exec grep --color -n -i "$@" {} +
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001069 }
1070
1071 ;;
1072esac
1073
Matt Alexanderd9c56562020-05-21 10:49:17 +00001074function getprebuilt
1075{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001076 get_abs_build_var ANDROID_PREBUILTS
1077}
1078
Matt Alexanderd9c56562020-05-21 10:49:17 +00001079function tracedmdump()
1080{
Christopher Ferris55257d22017-03-23 11:08:58 -07001081 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001082 if [ ! "$T" ]; then
1083 echo "Couldn't locate the top of the tree. Try setting TOP."
1084 return
1085 fi
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001086 local prebuiltdir=$(getprebuilt)
Raghu Gandham8da43102012-07-25 19:57:22 -07001087 local arch=$(gettargetarch)
1088 local KERNEL=$T/prebuilts/qemu-kernel/$arch/vmlinux-qemu
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001089
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001090 local TRACE=$1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001091 if [ ! "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001092 echo "usage: tracedmdump tracename"
1093 return
1094 fi
1095
Matt Alexanderd9c56562020-05-21 10:49:17 +00001096 if [ ! -r "$KERNEL" ] ; then
Jack Veenstra60116fc2009-04-09 18:12:34 -07001097 echo "Error: cannot find kernel: '$KERNEL'"
1098 return
1099 fi
1100
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001101 local BASETRACE=$(basename $TRACE)
Matt Alexanderd9c56562020-05-21 10:49:17 +00001102 if [ "$BASETRACE" = "$TRACE" ] ; then
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001103 TRACE=$ANDROID_PRODUCT_OUT/traces/$TRACE
1104 fi
1105
1106 echo "post-processing traces..."
1107 rm -f $TRACE/qtrace.dexlist
1108 post_trace $TRACE
1109 if [ $? -ne 0 ]; then
1110 echo "***"
1111 echo "*** Error: malformed trace. Did you remember to exit the emulator?"
1112 echo "***"
1113 return
1114 fi
1115 echo "generating dexlist output..."
1116 /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
1117 echo "generating dmtrace data..."
1118 q2dm -r $ANDROID_PRODUCT_OUT/symbols $TRACE $KERNEL $TRACE/dmtrace || return
1119 echo "generating html file..."
1120 dmtracedump -h $TRACE/dmtrace >| $TRACE/dmtrace.html || return
1121 echo "done, see $TRACE/dmtrace.html for details"
1122 echo "or run:"
1123 echo " traceview $TRACE/dmtrace"
1124}
1125
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001126# communicate with a running device or emulator, set up necessary state,
1127# and run the hat command.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001128function runhat()
1129{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001130 # process standard adb options
1131 local adbTarget=""
Matt Alexanderd9c56562020-05-21 10:49:17 +00001132 if [ "$1" = "-d" -o "$1" = "-e" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001133 adbTarget=$1
1134 shift 1
Matt Alexanderd9c56562020-05-21 10:49:17 +00001135 elif [ "$1" = "-s" ]; then
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001136 adbTarget="$1 $2"
1137 shift 2
1138 fi
1139 local adbOptions=${adbTarget}
Matt Alexanderd9c56562020-05-21 10:49:17 +00001140 #echo adbOptions = ${adbOptions}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001141
1142 # runhat options
1143 local targetPid=$1
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001144
Matt Alexanderd9c56562020-05-21 10:49:17 +00001145 if [ "$targetPid" = "" ]; then
Andy McFaddenb6289852010-07-12 08:00:19 -07001146 echo "Usage: runhat [ -d | -e | -s serial ] target-pid"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001147 return
1148 fi
1149
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001150 # confirm hat is available
1151 if [ -z $(which hat) ]; then
1152 echo "hat is not available in this configuration."
1153 return
1154 fi
1155
Andy McFaddenb6289852010-07-12 08:00:19 -07001156 # issue "am" command to cause the hprof dump
Nick Kralevich9948b1e2014-07-18 15:45:38 -07001157 local devFile=/data/local/tmp/hprof-$targetPid
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001158 echo "Poking $targetPid and waiting for data..."
Dianne Hackborn6b9549f2012-09-26 15:00:59 -07001159 echo "Storing data at $devFile"
Andy McFaddenb6289852010-07-12 08:00:19 -07001160 adb ${adbOptions} shell am dumpheap $targetPid $devFile
The Android Open Source Project88b60792009-03-03 19:28:42 -08001161 echo "Press enter when logcat shows \"hprof: heap dump completed\""
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001162 echo -n "> "
1163 read
1164
The Android Open Source Project88b60792009-03-03 19:28:42 -08001165 local localFile=/tmp/$$-hprof
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001166
The Android Open Source Project88b60792009-03-03 19:28:42 -08001167 echo "Retrieving file $devFile..."
1168 adb ${adbOptions} pull $devFile $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001169
The Android Open Source Project88b60792009-03-03 19:28:42 -08001170 adb ${adbOptions} shell rm $devFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001171
The Android Open Source Project88b60792009-03-03 19:28:42 -08001172 echo "Running hat on $localFile"
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001173 echo "View the output by pointing your browser at http://localhost:7000/"
1174 echo ""
Dianne Hackborn6e4e1bb2011-11-10 15:19:51 -08001175 hat -JXmx512m $localFile
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001176}
1177
Matt Alexanderd9c56562020-05-21 10:49:17 +00001178function getbugreports()
1179{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001180 local reports=(`adb shell ls /sdcard/bugreports | tr -d '\r'`)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001181
1182 if [ ! "$reports" ]; then
1183 echo "Could not locate any bugreports."
1184 return
1185 fi
1186
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001187 local report
1188 for report in ${reports[@]}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001189 do
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001190 echo "/sdcard/bugreports/${report}"
1191 adb pull /sdcard/bugreports/${report} ${report}
1192 gunzip ${report}
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001193 done
1194}
1195
Matt Alexanderd9c56562020-05-21 10:49:17 +00001196function getsdcardpath()
1197{
Victoria Lease1b296b42012-08-21 15:44:06 -07001198 adb ${adbOptions} shell echo -n \$\{EXTERNAL_STORAGE\}
1199}
1200
Matt Alexanderd9c56562020-05-21 10:49:17 +00001201function getscreenshotpath()
1202{
Victoria Lease1b296b42012-08-21 15:44:06 -07001203 echo "$(getsdcardpath)/Pictures/Screenshots"
1204}
1205
Matt Alexanderd9c56562020-05-21 10:49:17 +00001206function getlastscreenshot()
1207{
Victoria Lease1b296b42012-08-21 15:44:06 -07001208 local screenshot_path=$(getscreenshotpath)
1209 local screenshot=`adb ${adbOptions} ls ${screenshot_path} | grep Screenshot_[0-9-]*.*\.png | sort -rk 3 | cut -d " " -f 4 | head -n 1`
Matt Alexanderd9c56562020-05-21 10:49:17 +00001210 if [ "$screenshot" = "" ]; then
Victoria Lease1b296b42012-08-21 15:44:06 -07001211 echo "No screenshots found."
1212 return
1213 fi
1214 echo "${screenshot}"
1215 adb ${adbOptions} pull ${screenshot_path}/${screenshot}
1216}
1217
Matt Alexanderd9c56562020-05-21 10:49:17 +00001218function startviewserver()
1219{
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001220 local port=4939
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001221 if [ $# -gt 0 ]; then
1222 port=$1
1223 fi
1224 adb shell service call window 1 i32 $port
1225}
1226
Matt Alexanderd9c56562020-05-21 10:49:17 +00001227function stopviewserver()
1228{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001229 adb shell service call window 2
1230}
1231
Matt Alexanderd9c56562020-05-21 10:49:17 +00001232function isviewserverstarted()
1233{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001234 adb shell service call window 3
1235}
1236
Matt Alexanderd9c56562020-05-21 10:49:17 +00001237function key_home()
1238{
Romain Guyb84049a2010-10-04 16:56:11 -07001239 adb shell input keyevent 3
1240}
1241
Matt Alexanderd9c56562020-05-21 10:49:17 +00001242function key_back()
1243{
Romain Guyb84049a2010-10-04 16:56:11 -07001244 adb shell input keyevent 4
1245}
1246
Matt Alexanderd9c56562020-05-21 10:49:17 +00001247function key_menu()
1248{
Romain Guyb84049a2010-10-04 16:56:11 -07001249 adb shell input keyevent 82
1250}
1251
Matt Alexanderd9c56562020-05-21 10:49:17 +00001252function smoketest()
1253{
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001254 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1255 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1256 return
1257 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001258 local T=$(gettop)
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001259 if [ ! "$T" ]; then
1260 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1261 return
1262 fi
1263
Ying Wang9cd17642012-12-13 10:52:07 -08001264 (\cd "$T" && mmm tests/SmokeTest) &&
The Android Open Source Projectb6c1cf62008-10-21 07:00:00 -07001265 adb uninstall com.android.smoketest > /dev/null &&
1266 adb uninstall com.android.smoketest.tests > /dev/null &&
1267 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTestApp.apk &&
1268 adb install $ANDROID_PRODUCT_OUT/data/app/SmokeTest.apk &&
1269 adb shell am instrument -w com.android.smoketest.tests/android.test.InstrumentationTestRunner
1270}
1271
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001272# simple shortcut to the runtest command
Matt Alexanderd9c56562020-05-21 10:49:17 +00001273function runtest()
1274{
Christopher Ferris55257d22017-03-23 11:08:58 -07001275 local T=$(gettop)
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001276 if [ ! "$T" ]; then
1277 echo "Couldn't locate the top of the tree. Try setting TOP." >&2
1278 return
1279 fi
Brett Chabot3fb149d2009-10-21 20:05:26 -07001280 ("$T"/development/testrunner/runtest.py $@)
Brett Chabot762748c2009-03-27 10:25:11 -07001281}
1282
The Android Open Source Project88b60792009-03-03 19:28:42 -08001283function godir () {
1284 if [[ -z "$1" ]]; then
1285 echo "Usage: godir <regex>"
1286 return
1287 fi
Christopher Ferris55257d22017-03-23 11:08:58 -07001288 local T=$(gettop)
1289 local FILELIST
Matt Alexanderd9c56562020-05-21 10:49:17 +00001290 if [ ! "$OUT_DIR" = "" ]; then
Brian Carlstromf2257422015-09-30 20:28:54 -07001291 mkdir -p $OUT_DIR
1292 FILELIST=$OUT_DIR/filelist
1293 else
1294 FILELIST=$T/filelist
1295 fi
1296 if [[ ! -f $FILELIST ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001297 echo -n "Creating index..."
Brian Carlstromf2257422015-09-30 20:28:54 -07001298 (\cd $T; find . -wholename ./out -prune -o -wholename ./.repo -prune -o -type f > $FILELIST)
The Android Open Source Project88b60792009-03-03 19:28:42 -08001299 echo " Done"
1300 echo ""
1301 fi
1302 local lines
Brian Carlstromf2257422015-09-30 20:28:54 -07001303 lines=($(\grep "$1" $FILELIST | sed -e 's/\/[^/]*$//' | sort | uniq))
Matt Alexanderd9c56562020-05-21 10:49:17 +00001304 if [[ ${#lines[@]} = 0 ]]; then
The Android Open Source Project88b60792009-03-03 19:28:42 -08001305 echo "Not found"
1306 return
1307 fi
1308 local pathname
1309 local choice
1310 if [[ ${#lines[@]} > 1 ]]; then
1311 while [[ -z "$pathname" ]]; do
1312 local index=1
1313 local line
1314 for line in ${lines[@]}; do
1315 printf "%6s %s\n" "[$index]" $line
Doug Zongker29034982011-04-22 08:16:56 -07001316 index=$(($index + 1))
The Android Open Source Project88b60792009-03-03 19:28:42 -08001317 done
1318 echo
1319 echo -n "Select one: "
1320 unset choice
1321 read choice
1322 if [[ $choice -gt ${#lines[@]} || $choice -lt 1 ]]; then
1323 echo "Invalid choice"
1324 continue
1325 fi
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001326 pathname=${lines[@]:$(($choice-1)):1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001327 done
1328 else
Guillaume Chelfice000fd2019-10-03 12:02:46 +02001329 pathname=${lines[@]:0:1}
The Android Open Source Project88b60792009-03-03 19:28:42 -08001330 fi
Ying Wang9cd17642012-12-13 10:52:07 -08001331 \cd $T/$pathname
The Android Open Source Project88b60792009-03-03 19:28:42 -08001332}
1333
Steven Moreland62054a42018-12-06 10:11:40 -08001334# Update module-info.json in out.
1335function refreshmod() {
1336 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1337 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1338 return 1
1339 fi
1340
1341 echo "Refreshing modules (building module-info.json). Log at $ANDROID_PRODUCT_OUT/module-info.json.build.log." >&2
1342
1343 # for the output of the next command
1344 mkdir -p $ANDROID_PRODUCT_OUT || return 1
1345
1346 # Note, can't use absolute path because of the way make works.
1347 m out/target/product/$(get_build_var TARGET_DEVICE)/module-info.json \
1348 > $ANDROID_PRODUCT_OUT/module-info.json.build.log 2>&1
1349}
1350
1351# List all modules for the current device, as cached in module-info.json. If any build change is
1352# made and it should be reflected in the output, you should run 'refreshmod' first.
1353function allmod() {
1354 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1355 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1356 return 1
1357 fi
1358
1359 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1360 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1361 refreshmod || return 1
1362 fi
1363
LuK1337b6a78192020-01-12 03:12:17 +01001364 python -c "import json; print('\n'.join(sorted(json.load(open('$ANDROID_PRODUCT_OUT/module-info.json')).keys())))"
Steven Moreland62054a42018-12-06 10:11:40 -08001365}
1366
Rett Berg78d1c932019-01-24 14:34:23 -08001367# 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 -08001368# is made, and it should be reflected in the output, you should run 'refreshmod' first.
Rett Berg78d1c932019-01-24 14:34:23 -08001369function pathmod() {
Steven Moreland62054a42018-12-06 10:11:40 -08001370 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1371 echo "No ANDROID_PRODUCT_OUT. Try running 'lunch' first." >&2
1372 return 1
1373 fi
1374
1375 if [[ $# -ne 1 ]]; then
Rett Berg78d1c932019-01-24 14:34:23 -08001376 echo "usage: pathmod <module>" >&2
Steven Moreland62054a42018-12-06 10:11:40 -08001377 return 1
1378 fi
1379
1380 if [ ! -f "$ANDROID_PRODUCT_OUT/module-info.json" ]; then
1381 echo "Could not find module-info.json. It will only be built once, and it can be updated with 'refreshmod'" >&2
1382 refreshmod || return 1
1383 fi
1384
1385 local relpath=$(python -c "import json, os
1386module = '$1'
1387module_info = json.load(open('$ANDROID_PRODUCT_OUT/module-info.json'))
1388if module not in module_info:
1389 exit(1)
LuK1337b6a78192020-01-12 03:12:17 +01001390print(module_info[module]['path'][0])" 2>/dev/null)
Steven Moreland62054a42018-12-06 10:11:40 -08001391
1392 if [ -z "$relpath" ]; then
1393 echo "Could not find module '$1' (try 'refreshmod' if there have been build changes?)." >&2
1394 return 1
1395 else
Rett Berg78d1c932019-01-24 14:34:23 -08001396 echo "$ANDROID_BUILD_TOP/$relpath"
Steven Moreland62054a42018-12-06 10:11:40 -08001397 fi
1398}
1399
Rett Berg78d1c932019-01-24 14:34:23 -08001400# Go to a specific module in the android tree, as cached in module-info.json. If any build change
1401# is made, and it should be reflected in the output, you should run 'refreshmod' first.
1402function gomod() {
1403 if [[ $# -ne 1 ]]; then
1404 echo "usage: gomod <module>" >&2
1405 return 1
1406 fi
1407
1408 local path="$(pathmod $@)"
1409 if [ -z "$path" ]; then
1410 return 1
1411 fi
1412 cd $path
1413}
1414
dimitry73b84812018-12-11 18:06:00 +01001415function _complete_android_module_names() {
Steven Moreland62054a42018-12-06 10:11:40 -08001416 local word=${COMP_WORDS[COMP_CWORD]}
1417 COMPREPLY=( $(allmod | grep -E "^$word") )
1418}
1419
Alex Rayf0d08eb2013-03-08 15:15:06 -08001420# Print colored exit condition
1421function pez {
Michael Wrighteb733842013-03-08 17:34:02 -08001422 "$@"
1423 local retval=$?
Matt Alexanderd9c56562020-05-21 10:49:17 +00001424 if [ $retval -ne 0 ]
1425 then
Jacky Cao89483b82015-05-15 22:12:53 +08001426 echo $'\E'"[0;31mFAILURE\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001427 else
Jacky Cao89483b82015-05-15 22:12:53 +08001428 echo $'\E'"[0;32mSUCCESS\e[00m"
Michael Wrighteb733842013-03-08 17:34:02 -08001429 fi
1430 return $retval
Alex Rayf0d08eb2013-03-08 15:15:06 -08001431}
1432
Matt Alexanderd9c56562020-05-21 10:49:17 +00001433function get_make_command()
1434{
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001435 # If we're in the top of an Android tree, use soong_ui.bash instead of make
1436 if [ -f build/soong/soong_ui.bash ]; then
Dan Willemsene9842242017-07-28 13:00:13 -07001437 # Always use the real make if -C is passed in
1438 for arg in "$@"; do
1439 if [[ $arg == -C* ]]; then
1440 echo command make
1441 return
1442 fi
1443 done
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001444 echo build/soong/soong_ui.bash --make-mode
1445 else
1446 echo command make
1447 fi
Ying Wanged21d4c2014-08-24 22:14:19 -07001448}
1449
Matt Alexanderd9c56562020-05-21 10:49:17 +00001450function _wrap_build()
1451{
Sasha Smundak9f27cc02019-01-31 13:25:31 -08001452 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
1453 "$@"
1454 return $?
1455 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001456 local start_time=$(date +"%s")
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001457 "$@"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001458 local ret=$?
1459 local end_time=$(date +"%s")
1460 local tdiff=$(($end_time-$start_time))
1461 local hours=$(($tdiff / 3600 ))
1462 local mins=$((($tdiff % 3600) / 60))
1463 local secs=$(($tdiff % 60))
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001464 local ncolors=$(tput colors 2>/dev/null)
1465 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
Jacky Cao89483b82015-05-15 22:12:53 +08001466 color_failed=$'\E'"[0;31m"
1467 color_success=$'\E'"[0;32m"
1468 color_reset=$'\E'"[00m"
Greg Hackmannd95c7f72014-06-23 14:05:06 -07001469 else
1470 color_failed=""
1471 color_success=""
1472 color_reset=""
1473 fi
Ed Heylcc6be0a2014-06-18 14:55:58 -07001474 echo
Matt Alexanderd9c56562020-05-21 10:49:17 +00001475 if [ $ret -eq 0 ] ; then
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001476 echo -n "${color_success}#### build completed successfully "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001477 else
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001478 echo -n "${color_failed}#### failed to build some targets "
Ed Heylcc6be0a2014-06-18 14:55:58 -07001479 fi
Matt Alexanderd9c56562020-05-21 10:49:17 +00001480 if [ $hours -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001481 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001482 elif [ $mins -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001483 printf "(%02g:%02g (mm:ss))" $mins $secs
Matt Alexanderd9c56562020-05-21 10:49:17 +00001484 elif [ $secs -gt 0 ] ; then
Ed Heylcc6be0a2014-06-18 14:55:58 -07001485 printf "(%s seconds)" $secs
1486 fi
Jacky Cao89483b82015-05-15 22:12:53 +08001487 echo " ####${color_reset}"
Ed Heylcc6be0a2014-06-18 14:55:58 -07001488 echo
1489 return $ret
1490}
1491
Patrice Arrudafa7204b2019-06-20 23:40:33 +00001492function _trigger_build()
1493(
1494 local -r bc="$1"; shift
1495 if T="$(gettop)"; then
1496 _wrap_build "$T/build/soong/soong_ui.bash" --build-mode --${bc} --dir="$(pwd)" "$@"
1497 else
1498 echo "Couldn't locate the top of the tree. Try setting TOP."
1499 fi
1500)
1501
1502function m()
1503(
1504 _trigger_build "all-modules" "$@"
1505)
1506
1507function mm()
1508(
1509 _trigger_build "modules-in-a-dir-no-deps" "$@"
1510)
1511
1512function mmm()
1513(
1514 _trigger_build "modules-in-dirs-no-deps" "$@"
1515)
1516
1517function mma()
1518(
1519 _trigger_build "modules-in-a-dir" "$@"
1520)
1521
1522function mmma()
1523(
1524 _trigger_build "modules-in-dirs" "$@"
1525)
1526
Matt Alexanderd9c56562020-05-21 10:49:17 +00001527function make()
1528{
Dan Willemsene9842242017-07-28 13:00:13 -07001529 _wrap_build $(get_make_command "$@") "$@"
Dan Willemsend41ec5a2017-07-12 16:14:50 -07001530}
1531
Matt Alexanderd9c56562020-05-21 10:49:17 +00001532function provision()
1533{
David Zeuthen1b126ff2015-09-30 17:10:48 -04001534 if [ ! "$ANDROID_PRODUCT_OUT" ]; then
1535 echo "Couldn't locate output files. Try running 'lunch' first." >&2
1536 return 1
1537 fi
1538 if [ ! -e "$ANDROID_PRODUCT_OUT/provision-device" ]; then
1539 echo "There is no provisioning script for the device." >&2
1540 return 1
1541 fi
1542
1543 # Check if user really wants to do this.
Matt Alexanderd9c56562020-05-21 10:49:17 +00001544 if [ "$1" = "--no-confirmation" ]; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001545 shift 1
1546 else
1547 echo "This action will reflash your device."
1548 echo ""
1549 echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
1550 echo ""
Marie Janssen4afc2c02015-11-10 10:41:15 -08001551 echo -n "Are you sure you want to do this (yes/no)? "
1552 read
Matt Alexanderd9c56562020-05-21 10:49:17 +00001553 if [[ "${REPLY}" != "yes" ]] ; then
David Zeuthen1b126ff2015-09-30 17:10:48 -04001554 echo "Not taking any action. Exiting." >&2
1555 return 1
1556 fi
1557 fi
1558 "$ANDROID_PRODUCT_OUT/provision-device" "$@"
1559}
1560
Jim Tanga881a252018-06-19 16:34:41 +08001561# Zsh needs bashcompinit called to support bash-style completion.
Patrik Fimmldf248e62018-10-15 18:15:12 +02001562function enable_zsh_completion() {
1563 # Don't override user's options if bash-style completion is already enabled.
1564 if ! declare -f complete >/dev/null; then
1565 autoload -U compinit && compinit
1566 autoload -U bashcompinit && bashcompinit
1567 fi
Jim Tanga881a252018-06-19 16:34:41 +08001568}
1569
1570function validate_current_shell() {
1571 local current_sh="$(ps -o command -p $$)"
1572 case "$current_sh" in
Raphael Moll70a86b02011-06-20 16:03:14 -07001573 *bash*)
Jim Tanga881a252018-06-19 16:34:41 +08001574 function check_type() { type -t "$1"; }
Raphael Moll70a86b02011-06-20 16:03:14 -07001575 ;;
Jim Tanga881a252018-06-19 16:34:41 +08001576 *zsh*)
1577 function check_type() { type "$1"; }
Matt Alexanderd9c56562020-05-21 10:49:17 +00001578 enable_zsh_completion ;;
Raphael Moll70a86b02011-06-20 16:03:14 -07001579 *)
Jim Tanga881a252018-06-19 16:34:41 +08001580 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 -07001581 ;;
1582 esac
Jim Tanga881a252018-06-19 16:34:41 +08001583}
The Android Open Source Projectdcc08f02008-12-17 18:03:49 -08001584
1585# Execute the contents of any vendorsetup.sh files we can find.
Dan Willemsend855a722019-02-12 15:52:36 -08001586# Unless we find an allowed-vendorsetup_sh-files file, in which case we'll only
1587# load those.
1588#
1589# This allows loading only approved vendorsetup.sh files
Jim Tanga881a252018-06-19 16:34:41 +08001590function source_vendorsetup() {
Jim Tangc4dba1d2019-07-25 16:54:27 +08001591 unset VENDOR_PYTHONPATH
Dan Willemsend855a722019-02-12 15:52:36 -08001592 allowed=
1593 for f in $(find -L device vendor product -maxdepth 4 -name 'allowed-vendorsetup_sh-files' 2>/dev/null | sort); do
1594 if [ -n "$allowed" ]; then
1595 echo "More than one 'allowed_vendorsetup_sh-files' file found, not including any vendorsetup.sh files:"
1596 echo " $allowed"
1597 echo " $f"
1598 return
1599 fi
1600 allowed="$f"
1601 done
1602
1603 allowed_files=
1604 [ -n "$allowed" ] && allowed_files=$(cat "$allowed")
Jim Tanga881a252018-06-19 16:34:41 +08001605 for dir in device vendor product; do
1606 for f in $(test -d $dir && \
1607 find -L $dir -maxdepth 4 -name 'vendorsetup.sh' 2>/dev/null | sort); do
Dan Willemsend855a722019-02-12 15:52:36 -08001608
1609 if [[ -z "$allowed" || "$allowed_files" =~ $f ]]; then
1610 echo "including $f"; . "$f"
1611 else
1612 echo "ignoring $f, not in $allowed"
1613 fi
Jim Tanga881a252018-06-19 16:34:41 +08001614 done
1615 done
1616}
Kenny Root52aa81c2011-07-15 11:07:06 -07001617
Jim Tanga881a252018-06-19 16:34:41 +08001618validate_current_shell
1619source_vendorsetup
Kenny Root52aa81c2011-07-15 11:07:06 -07001620addcompletions