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 # --bootstrap -- Process the bootstrap classes.
35 # --build-dir=path/to/out -- Specify where the base of the build tree is.
36 # This is typically a directory named "out". If not specified, it is
37 # assumed to be the current directory. The specified input and output
38 # paths are taken to be relative to this directory.
39 # --product-dir=path/to/product -- Specify the path, relative to the build
40 # directory, where the product tree to be used is. This directory should
41 # contain the boot classpath jar files. If not specified, then there
42 # must be a unique directory in the build named "target/product/NAME",
43 # and this is the directory that will be used.
44 # --boot-dir=path/to/bootclasspath -- Specify the path, relative to the
45 # product directory, of the directory where the boot classpath files
46 # reside. If not specified, this defaults to "system/framework"
47 # --boot-jars=list:of:jar:base:names -- Specify the list of base names
48 # of bootstrap classpath elements, colon-separated. Order is significant
49 # and must match the BOOTCLASSPATH that is eventually specified at
50 # runtime on the device. This defaults to "core". However, this really
51 # needs to match the target product's BOOTCLASSPATH, which, as of this
52 # writing, doesn't have a super-strict way of being defined within the
53 # build. You can find variations of it in different init.rc files under
54 # system/core/rootdir or under product-specific directories.
55 # --verify={none,remote,all} -- Specify what level of verification to
56 # do. Defaults to "all".
57 # --optimize={none,verified,all} -- Specify which classes to optimize.
58 # Defaults to "verified".
59 # --no-register-maps -- Indicate that the output should not contain
60 # register maps. By default, register maps are created and included.
66 bootDir="system/framework"
73 optimizeFlags="" # built up from the more human-friendly options
74 bogus="no" # indicates if there was an error during processing arguments
76 # Iterate over the arguments looking for options.
80 if [ "x${origOption}" = "x--" ]; then
81 # A raw "--" signals the end of option processing.
86 # Parse the option into components.
87 optionBeforeValue=`expr -- "${origOption}" : '--\([^=]*\)='`
89 if [ "$?" = '0' ]; then
90 # Option has the form "--option=value".
91 option="${optionBeforeValue}"
92 value=`expr -- "${origOption}" : '--[^=]*=\(.*\)'`
95 option=`expr -- "${origOption}" : '--\(.*\)'`
96 if [ "$?" = '1' ]; then
100 # Option has the form "--option".
106 # Interpret the option
107 if [ "${option}" = 'build-dir' -a "${hasValue}" = 'yes' ]; then
109 elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then
111 elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then
112 productDir="${value}"
113 elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then
115 elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
117 elif [ "${option}" = 'verify' -a "${hasValue}" = 'yes' ]; then
119 elif [ "${option}" = 'optimize' -a "${hasValue}" = 'yes' ]; then
120 doOptimize="${value}"
121 elif [ "${option}" = 'no-register-maps' -a "${hasValue}" = 'no' ]; then
124 echo "unknown option: ${origOption}" 1>&2
129 # Check and set up the input and output files. In the case of bootstrap
130 # processing, verify that no files are specified.
133 if [ "${bootstrap}" = "yes" ]; then
134 if [ "$#" != "0" ]; then
135 echo "unexpected arguments in --bootstrap mode" 1>&2
138 elif [ "$#" != "2" ]; then
139 echo "must specify input and output files (and no more arguments)" 1>&2
143 # Sanity-check the specified build directory.
144 if [ "x${buildDir}" = 'x' ]; then
145 echo "must specify build directory" 1>&2
147 elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
148 echo "build-dir is not a writable directory: ${buildDir}" 1>&2
152 # Sanity-check the specified boot classpath directory.
153 if [ "x${bootDir}" = 'x' ]; then
154 echo "must specify boot classpath directory" 1>&2
158 # Sanity-check the specified boot jar list.
159 if [ "x${bootJars}" = 'x' ]; then
160 echo "must specify non-empty boot-jars list" 1>&2
164 # Sanity-check and expand the verify option.
165 if [ "x${doVerify}" = "xnone" ]; then
166 optimizeFlags="${optimizeFlags},v=n"
167 elif [ "x${doVerify}" = "xremote" ]; then
168 optimizeFlags="${optimizeFlags},v=r"
169 elif [ "x${doVerify}" = "xall" ]; then
170 optimizeFlags="${optimizeFlags},v=a"
172 echo "bad value for --verify: ${doVerify}" 1>&2
176 # Sanity-check and expand the optimize option.
177 if [ "x${doOptimize}" = "xnone" ]; then
178 optimizeFlags="${optimizeFlags},o=n"
179 elif [ "x${doOptimize}" = "xverified" ]; then
180 optimizeFlags="${optimizeFlags},o=v"
181 elif [ "x${doOptimize}" = "xall" ]; then
182 optimizeFlags="${optimizeFlags},o=a"
184 echo "bad value for --optimize: ${doOptimize}" 1>&2
188 # Expand the register maps selection, if necessary.
189 if [ "${doRegisterMaps}" = "yes" ]; then
190 optimizeFlags="${optimizeFlags},m=y"
193 # Kill off the spare comma in optimizeFlags.
194 optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'`
196 # Error out if there was trouble.
197 if [ "${bogus}" = 'yes' ]; then
198 # There was an error during option processing.
199 echo "usage: $0 [--bootstrap] [--build-dir=path/to/out]" 1>&2
200 echo " [--product-dir=path/to/product] [--boot-dir=name]" 1>&2
201 echo " [--boot-jars=list:of:names]" 1>&2
202 echo " [--verify=type] [--optimize=type] [--no-register-maps]" 1>&2
203 echo " path/to/input.jar path/to/output.odex" 1>&2
207 # Cd to the build directory, un-symlinkifying it for clarity.
211 # If needed, find the default product directory.
212 if [ "x${productDir}" = 'x' ]; then
213 productDir="`ls target/product`"
214 if [ "$?" != '0' ]; then
215 echo "can't find product directory" 1>&2
217 elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then
218 echo "ambiguous product directory" 1>&2
221 productDir="target/product/${productDir}"
224 # Verify the product directory.
225 if [ ! '(' -d "${productDir}" -a -w "${productDir}" ')' ]; then
226 echo "product-dir is not a writable directory: ${productDir}" 1>&2
230 # Expand and verify the boot classpath directory. We add "/./" here to
231 # separate the build system part of the path from the target system
232 # suffix part of the path. The dexopt executable (deep inside the vm
233 # really) uses this to know how to generate the names of the
234 # dependencies (since we don't want the device files to contain bits
235 # of pathname from the host build system).
236 bootDir="${productDir}/./${bootDir}"
237 if [ ! '(' -d "${bootDir}" -a -w "${bootDir}" ')' ]; then
238 echo "boot-dir is not a writable directory: ${bootDir}" 1>&2
242 # Find the dexopt binary.
243 dexopt="`ls host/*/bin/dexopt`"
244 if [ "$?" != '0' ]; then
245 echo "can't find dexopt binary" 1>&2
247 elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then
248 echo "ambiguous host directory" 1>&2
252 # Expand the bootJars into paths that are relative from the build
253 # directory, maintaining the colon separators.
254 BOOTCLASSPATH=`echo ":${bootJars}" | \
255 sed "s!:\([^:]*\)!:${bootDir}/\1.jar!g" | \
259 if [ "${bootstrap}" = "yes" ]; then
260 # Split the boot classpath into separate elements and iterate over them,
261 # processing each, in order.
262 elements=`echo "${BOOTCLASSPATH}" | sed 's/:/ /g'`
264 for inputFile in $elements; do
265 echo "Processing ${inputFile}" 1>&2
266 outputFile="`dirname ${inputFile}`/`basename ${inputFile} .jar`.odex"
267 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
269 if [ "${status}" != "0" ]; then
274 echo "Processing ${inputFile}" 1>&2
275 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
278 if [ "${status}" != "0" ]; then