3 # Copyright (C) 2010 The Android Open Source Project
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # Usage: dex-preopt [options] path/to/input.jar path/to/output.odex
20 # This tool runs a host build of dalvikvm in order to preoptimize dex
21 # files that will be run on a device.
23 # The input may be any sort of jar file (including .apk files), as long
24 # as it contains a classes.dex file. Note that optimized versions of
25 # bootstrap classes must be created before this can be run on other files;
26 # use the "--bootstrap" option to do this.
28 # The "output.odex" file must not already exist.
30 # This is expected to be running in a user build environment, where
31 # "dexopt" is available on the host.
34 # --build-dir=path/to/out -- Specify where the base of the build tree is.
35 # This is typically a directory named "out". If not specified, it is
36 # assumed to be the current directory. The specified input and output
37 # paths are taken to be relative to this directory.
38 # --dexopt=path/to/dexopt -- Specify the path to the dexopt executable.
39 # If unspecified, there must be a unique subdirectory of the build-dir
40 # that looks like host/ARCH/bin which must contain dexopt.
41 # --product-dir=path/to/product -- Specify the path, relative to the build
42 # directory, where the product tree to be used is. This directory should
43 # contain the boot classpath jar files. If not specified, then there
44 # must be a unique directory in the build named "target/product/NAME",
45 # and this is the directory that will be used.
46 # --boot-dir=path/to/bootclasspath -- Specify the path, relative to the
47 # product directory, of the directory where the boot classpath files
48 # reside. If not specified, this defaults to "system/framework"
49 # --boot-jars=list:of:jar:base:names -- Specify the list of base names
50 # of bootstrap classpath elements, colon-separated. Order is significant
51 # and must match the BOOTCLASSPATH that is eventually specified at
52 # runtime on the device. This defaults to "core". However, this really
53 # needs to match the target product's BOOTCLASSPATH, which, as of this
54 # writing, doesn't have a super-strict way of being defined within the
55 # build. You can find variations of it in different init.rc files under
56 # system/core/rootdir or under product-specific directories.
57 # --bootstrap -- Process the bootstrap classes. If this is specified,
58 # then, instead of processing a specified input file, no other arguments
59 # are taken, and what is processed is the entirety of the boot jar
61 # --verify={none,remote,all} -- Specify what level of verification to
62 # do. Defaults to "all".
63 # --optimize={none,verified,all} -- Specify which classes to optimize.
64 # Defaults to "verified".
65 # --no-register-maps -- Indicate that the output should not contain
66 # register maps. By default, register maps are created and included.
67 # --uniprocessor -- Indicate that the output should target a uniprocessor.
68 # By default, optimizations will be made that specifically target
69 # SMP processors (which will merely be superfluous on uniprocessors).
76 bootDir='system/framework'
84 optimizeFlags='' # built up from the more human-friendly options
85 bogus='no' # indicates if there was an error during processing arguments
87 # Iterate over the arguments looking for options.
91 if [ "x${origOption}" = "x--" ]; then
92 # A raw "--" signals the end of option processing.
97 # Parse the option into components.
98 optionBeforeValue=`expr -- "${origOption}" : '--\([^=]*\)='`
100 if [ "$?" = '0' ]; then
101 # Option has the form "--option=value".
102 option="${optionBeforeValue}"
103 value=`expr -- "${origOption}" : '--[^=]*=\(.*\)'`
106 option=`expr -- "${origOption}" : '--\(.*\)'`
107 if [ "$?" = '1' ]; then
111 # Option has the form "--option".
117 # Interpret the option
118 if [ "${option}" = 'build-dir' -a "${hasValue}" = 'yes' ]; then
120 elif [ "${option}" = 'dexopt' -a "${hasValue}" = 'yes' ]; then
122 elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then
124 elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then
125 productDir="${value}"
126 elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then
128 elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
130 elif [ "${option}" = 'verify' -a "${hasValue}" = 'yes' ]; then
132 elif [ "${option}" = 'optimize' -a "${hasValue}" = 'yes' ]; then
133 doOptimize="${value}"
134 elif [ "${option}" = 'no-register-maps' -a "${hasValue}" = 'no' ]; then
136 elif [ "${option}" = 'uniprocessor' -a "${hasValue}" = 'no' ]; then
139 echo "unknown option: ${origOption}" 1>&2
144 # Check and set up the input and output files. In the case of bootstrap
145 # processing, verify that no files are specified.
148 if [ "${bootstrap}" = 'yes' ]; then
149 if [ "$#" != '0' ]; then
150 echo "unexpected arguments in --bootstrap mode" 1>&2
153 elif [ "$#" != '2' ]; then
154 echo "must specify input and output files (and no more arguments)" 1>&2
158 # Sanity-check the specified build directory.
159 if [ "x${buildDir}" = 'x' ]; then
160 echo "must specify build directory" 1>&2
162 elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
163 echo "build-dir is not a writable directory: ${buildDir}" 1>&2
167 # Sanity-check the specified boot classpath directory.
168 if [ "x${bootDir}" = 'x' ]; then
169 echo "must specify boot classpath directory" 1>&2
173 # Sanity-check the specified boot jar list.
174 if [ "x${bootJars}" = 'x' ]; then
175 echo "must specify non-empty boot-jars list" 1>&2
179 # Sanity-check and expand the verify option.
180 if [ "x${doVerify}" = 'xnone' ]; then
181 optimizeFlags="${optimizeFlags},v=n"
182 elif [ "x${doVerify}" = 'xremote' ]; then
183 optimizeFlags="${optimizeFlags},v=r"
184 elif [ "x${doVerify}" = 'xall' ]; then
185 optimizeFlags="${optimizeFlags},v=a"
187 echo "bad value for --verify: ${doVerify}" 1>&2
191 # Sanity-check and expand the optimize option.
192 if [ "x${doOptimize}" = 'xnone' ]; then
193 optimizeFlags="${optimizeFlags},o=n"
194 elif [ "x${doOptimize}" = 'xverified' ]; then
195 optimizeFlags="${optimizeFlags},o=v"
196 elif [ "x${doOptimize}" = 'xall' ]; then
197 optimizeFlags="${optimizeFlags},o=a"
199 echo "bad value for --optimize: ${doOptimize}" 1>&2
203 # Expand the register maps selection, if necessary.
204 if [ "${doRegisterMaps}" = 'yes' ]; then
205 optimizeFlags="${optimizeFlags},m=y"
208 # Expand the uniprocessor directive, if necessary.
209 if [ "${doUniprocessor}" = 'yes' ]; then
210 optimizeFlags="${optimizeFlags},u=y"
212 optimizeFlags="${optimizeFlags},u=n"
215 # Kill off the spare comma in optimizeFlags.
216 optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'`
218 # Error out if there was trouble.
219 if [ "${bogus}" = 'yes' ]; then
220 # There was an error during option processing.
221 echo "usage: $0" 1>&2
222 echo ' [--build-dir=path/to/out] [--dexopt=path/to/dexopt]' 1>&2
223 echo ' [--product-dir=path/to/product] [--boot-dir=name]' 1>&2
224 echo ' [--boot-jars=list:of:names] [--bootstrap]' 1>&2
225 echo ' [--verify=type] [--optimize=type] [--no-register-maps]' 1>&2
226 echo ' [--uniprocessor] path/to/input.jar path/to/output.odex' 1>&2
230 # Cd to the build directory, un-symlinkifying it for clarity.
234 # If needed, find the default product directory.
235 if [ "x${productDir}" = 'x' ]; then
236 productDir="`ls target/product`"
237 if [ "$?" != '0' ]; then
238 echo "can't find product directory" 1>&2
240 elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then
241 echo "ambiguous product directory" 1>&2
244 productDir="target/product/${productDir}"
247 # Verify the product directory.
248 if [ ! '(' -d "${productDir}" -a -w "${productDir}" ')' ]; then
249 echo "product-dir is not a writable directory: ${productDir}" 1>&2
253 # Expand and verify the boot classpath directory. We add "/./" here to
254 # separate the build system part of the path from the target system
255 # suffix part of the path. The dexopt executable (deep inside the vm
256 # really) uses this to know how to generate the names of the
257 # dependencies (since we don't want the device files to contain bits
258 # of pathname from the host build system).
259 productBootDir="${productDir}/./${bootDir}"
260 if [ ! '(' -d "${productBootDir}" -a -w "${productBootDir}" ')' ]; then
261 echo "boot-dir is not a writable directory: ${productBootDir}" 1>&2
265 # Find the dexopt binary if necesasry, and verify it.
266 if [ "x${dexopt}" = 'x' ]; then
267 dexopt="`ls host/*/bin/dexopt`"
268 if [ "$?" != '0' ]; then
269 echo "can't find dexopt binary" 1>&2
271 elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then
272 echo "ambiguous host directory" 1>&2
276 if [ ! -x "${dexopt}" ]; then
277 echo "dexopt binary is not executable: ${dexopt}" 1>&2
281 # Expand the bootJars into paths that are relative from the build
282 # directory, maintaining the colon separators.
283 BOOTCLASSPATH=`echo ":${bootJars}" | \
284 sed "s!:\([^:]*\)!:${productBootDir}/\1.jar!g" | \
288 if [ "${bootstrap}" = 'yes' ]; then
289 # Split the boot classpath into separate elements and iterate over them,
290 # processing each, in order.
291 elements=`echo "${BOOTCLASSPATH}" | sed 's/:/ /g'`
293 for inputFile in $elements; do
294 echo "Processing ${inputFile}" 1>&2
295 outputFile="`dirname ${inputFile}`/`basename ${inputFile} .jar`.odex"
296 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
298 if [ "${status}" != '0' ]; then
303 echo "Processing ${inputFile}" 1>&2
305 bootJarFile=`expr -- "${inputFile}" : "${productDir}/${bootDir}/\(.*\)"`
306 if [ "x${bootJarFile}" != 'x' ]; then
307 # The input file is in the boot classpath directory, so it needs
308 # to have "/./" inserted into it (see longer description above).
309 inputFile="${productBootDir}/${bootJarFile}"
312 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
315 if [ "${status}" != '0' ]; then