blob: 5977bda18677e885a65a9e8f24247bcb8ff15b49 [file] [log] [blame]
Dan Bornsteinab9d89f2010-09-12 13:03:27 -07001#!/bin/bash
2#
3# Copyright (C) 2010 The Android Open Source Project
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16
17#
Dan Bornstein20228de2010-09-13 13:40:33 -070018# Usage: dex-preopt [options] path/to/input.jar path/to/output.odex
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070019#
20# This tool runs a host build of dalvikvm in order to preoptimize dex
21# files that will be run on a device.
22#
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.
27#
Dan Bornstein2b9e8c12010-09-17 12:54:07 -070028# The "output.odex" file must not already exist.
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070029#
30# This is expected to be running in a user build environment, where
31# "dexopt" is available on the host.
32#
33# Options:
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.
Dan Bornstein2b9e8c12010-09-17 12:54:07 -070039# --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"
Dan Bornstein20228de2010-09-13 13:40:33 -070047# --boot-jars=list:of:jar:base:names -- Specify the list of base names
Dan Bornsteindef30fc2010-09-14 16:15:49 -070048# 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.
Dan Bornstein96604832010-09-13 15:37:28 -070055# --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.
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070061#
62
Dan Bornstein96604832010-09-13 15:37:28 -070063# Defaults.
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070064buildDir="."
Dan Bornstein2b9e8c12010-09-17 12:54:07 -070065productDir=""
66bootDir="system/framework"
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070067bootstrap="no"
Dan Bornstein96604832010-09-13 15:37:28 -070068doVerify="all"
69doOptimize="verified"
70doRegisterMaps="yes"
71bootJars="core"
72
73optimizeFlags="" # built up from the more human-friendly options
74bogus="no" # indicates if there was an error during processing arguments
Dan Bornsteinab9d89f2010-09-12 13:03:27 -070075
76# Iterate over the arguments looking for options.
77while true; do
78 origOption="$1"
79
80 if [ "x${origOption}" = "x--" ]; then
81 # A raw "--" signals the end of option processing.
82 shift
83 break
84 fi
85
86 # Parse the option into components.
87 optionBeforeValue=`expr -- "${origOption}" : '--\([^=]*\)='`
88
89 if [ "$?" = '0' ]; then
90 # Option has the form "--option=value".
91 option="${optionBeforeValue}"
92 value=`expr -- "${origOption}" : '--[^=]*=\(.*\)'`
93 hasValue="yes"
94 else
95 option=`expr -- "${origOption}" : '--\(.*\)'`
96 if [ "$?" = '1' ]; then
97 # Not an option.
98 break
99 fi
100 # Option has the form "--option".
101 value=""
102 hasValue="no"
103 fi
104 shift
105
106 # Interpret the option
107 if [ "${option}" = 'build-dir' -a "${hasValue}" = 'yes' ]; then
108 buildDir="${value}"
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700109 elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then
110 bootDir="${value}"
111 elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then
112 productDir="${value}"
Dan Bornstein20228de2010-09-13 13:40:33 -0700113 elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then
114 bootJars="${value}"
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700115 elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
116 bootstrap="yes"
Dan Bornstein96604832010-09-13 15:37:28 -0700117 elif [ "${option}" = 'verify' -a "${hasValue}" = 'yes' ]; then
118 doVerify="${value}"
119 elif [ "${option}" = 'optimize' -a "${hasValue}" = 'yes' ]; then
120 doOptimize="${value}"
121 elif [ "${option}" = 'no-register-maps' -a "${hasValue}" = 'no' ]; then
122 doRegisterMaps="no"
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700123 else
124 echo "unknown option: ${origOption}" 1>&2
125 bogus="yes"
126 fi
127done
128
Dan Bornstein96604832010-09-13 15:37:28 -0700129# Check and set up the input and output files. In the case of bootstrap
130# processing, verify that no files are specified.
131inputFile=$1
132outputFile=$2
133if [ "${bootstrap}" = "yes" ]; then
134 if [ "$#" != "0" ]; then
135 echo "unexpected arguments in --bootstrap mode" 1>&2
136 bogus=yes
137 fi
138elif [ "$#" != "2" ]; then
139 echo "must specify input and output files (and no more arguments)" 1>&2
140 bogus=yes
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700141fi
142
143# Sanity-check the specified build directory.
144if [ "x${buildDir}" = 'x' ]; then
145 echo "must specify build directory" 1>&2
Dan Bornstein96604832010-09-13 15:37:28 -0700146 bogus=yes
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700147elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700148 echo "build-dir is not a writable directory: ${buildDir}" 1>&2
149 bogus=yes
150fi
151
152# Sanity-check the specified boot classpath directory.
153if [ "x${bootDir}" = 'x' ]; then
154 echo "must specify boot classpath directory" 1>&2
Dan Bornstein96604832010-09-13 15:37:28 -0700155 bogus=yes
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700156fi
157
Dan Bornstein20228de2010-09-13 13:40:33 -0700158# Sanity-check the specified boot jar list.
159if [ "x${bootJars}" = 'x' ]; then
160 echo "must specify non-empty boot-jars list" 1>&2
Dan Bornstein96604832010-09-13 15:37:28 -0700161 bogus=yes
162fi
163
164# Sanity-check and expand the verify option.
165if [ "x${doVerify}" = "xnone" ]; then
166 optimizeFlags="${optimizeFlags},v=n"
167elif [ "x${doVerify}" = "xremote" ]; then
168 optimizeFlags="${optimizeFlags},v=r"
169elif [ "x${doVerify}" = "xall" ]; then
170 optimizeFlags="${optimizeFlags},v=a"
171else
172 echo "bad value for --verify: ${doVerify}" 1>&2
173 bogus=yes
174fi
175
176# Sanity-check and expand the optimize option.
177if [ "x${doOptimize}" = "xnone" ]; then
178 optimizeFlags="${optimizeFlags},o=n"
179elif [ "x${doOptimize}" = "xverified" ]; then
180 optimizeFlags="${optimizeFlags},o=v"
181elif [ "x${doOptimize}" = "xall" ]; then
182 optimizeFlags="${optimizeFlags},o=a"
183else
184 echo "bad value for --optimize: ${doOptimize}" 1>&2
185 bogus=yes
186fi
187
188# Expand the register maps selection, if necessary.
189if [ "${doRegisterMaps}" = "yes" ]; then
190 optimizeFlags="${optimizeFlags},m=y"
191fi
192
193# Kill off the spare comma in optimizeFlags.
194optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'`
195
196# Error out if there was trouble.
197if [ "${bogus}" = 'yes' ]; then
198 # There was an error during option processing.
199 echo "usage: $0 [--bootstrap] [--build-dir=path/to/out]" 1>&2
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700200 echo " [--product-dir=path/to/product] [--boot-dir=name]" 1>&2
201 echo " [--boot-jars=list:of:names]" 1>&2
Dan Bornstein96604832010-09-13 15:37:28 -0700202 echo " [--verify=type] [--optimize=type] [--no-register-maps]" 1>&2
203 echo " path/to/input.jar path/to/output.odex" 1>&2
Dan Bornstein20228de2010-09-13 13:40:33 -0700204 exit 1
205fi
206
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700207# Cd to the build directory, un-symlinkifying it for clarity.
208cd "${buildDir}"
209cd "`/bin/pwd`"
210
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700211# If needed, find the default product directory.
212if [ "x${productDir}" = 'x' ]; then
213 productDir="`ls target/product`"
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700214 if [ "$?" != '0' ]; then
215 echo "can't find product directory" 1>&2
216 exit 1
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700217 elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700218 echo "ambiguous product directory" 1>&2
219 exit 1
220 fi
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700221 productDir="target/product/${productDir}"
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700222fi
223
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700224# Verify the product directory.
225if [ ! '(' -d "${productDir}" -a -w "${productDir}" ')' ]; then
226 echo "product-dir is not a writable directory: ${productDir}" 1>&2
227 exit 1
228fi
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700229
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700230# 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).
236bootDir="${productDir}/./${bootDir}"
237if [ ! '(' -d "${bootDir}" -a -w "${bootDir}" ')' ]; then
238 echo "boot-dir is not a writable directory: ${bootDir}" 1>&2
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700239 exit 1
240fi
241
242# Find the dexopt binary.
243dexopt="`ls host/*/bin/dexopt`"
244if [ "$?" != '0' ]; then
245 echo "can't find dexopt binary" 1>&2
246 exit 1
247elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then
248 echo "ambiguous host directory" 1>&2
249 exit 1
250fi
251
Dan Bornstein32bc0782010-09-13 17:30:10 -0700252# Expand the bootJars into paths that are relative from the build
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700253# directory, maintaining the colon separators.
Dan Bornstein20228de2010-09-13 13:40:33 -0700254BOOTCLASSPATH=`echo ":${bootJars}" | \
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700255 sed "s!:\([^:]*\)!:${bootDir}/\1.jar!g" | \
Dan Bornstein20228de2010-09-13 13:40:33 -0700256 sed 's/^://'`
257export BOOTCLASSPATH
258
259if [ "${bootstrap}" = "yes" ]; then
Dan Bornstein20228de2010-09-13 13:40:33 -0700260 # Split the boot classpath into separate elements and iterate over them,
261 # processing each, in order.
262 elements=`echo "${BOOTCLASSPATH}" | sed 's/:/ /g'`
263
264 for inputFile in $elements; do
Dan Bornstein96604832010-09-13 15:37:28 -0700265 echo "Processing ${inputFile}" 1>&2
Dan Bornstein20228de2010-09-13 13:40:33 -0700266 outputFile="`dirname ${inputFile}`/`basename ${inputFile} .jar`.odex"
Dan Bornstein96604832010-09-13 15:37:28 -0700267 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
268 status="$?"
269 if [ "${status}" != "0" ]; then
270 exit "${status}"
271 fi
Dan Bornstein20228de2010-09-13 13:40:33 -0700272 done
273else
Dan Bornstein96604832010-09-13 15:37:28 -0700274 echo "Processing ${inputFile}" 1>&2
Dan Bornstein2b9e8c12010-09-17 12:54:07 -0700275 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
Dan Bornstein20228de2010-09-13 13:40:33 -0700276
Dan Bornstein96604832010-09-13 15:37:28 -0700277 status="$?"
278 if [ "${status}" != "0" ]; then
279 exit "${status}"
Dan Bornstein20228de2010-09-13 13:40:33 -0700280 fi
Dan Bornstein20228de2010-09-13 13:40:33 -0700281fi
Dan Bornsteinab9d89f2010-09-12 13:03:27 -0700282
Dan Bornstein96604832010-09-13 15:37:28 -0700283echo "Done!" 1>&2