summaryrefslogtreecommitdiff
path: root/tools/dex-preopt
blob: a9b75a49e37e8db590c8baa27bfc0e42f977b44e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/bash
#
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Usage: dex-preopt [options] path/to/input.jar path/to/output.odex
#
# This tool runs a host build of dalvikvm in order to preoptimize dex
# files that will be run on a device.
#
# The input may be any sort of jar file (including .apk files), as long
# as it contains a classes.dex file. Note that optimized versions of
# bootstrap classes must be created before this can be run on other files;
# use the "--bootstrap" option to do this.
#
# The "output.odex" file must not already exist.
#
# This is expected to be running in a user build environment, where
# "dexopt" is available on the host.
#
# Options:
#   --build-dir=path/to/out -- Specify where the base of the build tree is.
#     This is typically a directory named "out". If not specified, it is
#     assumed to be the current directory. The specified input and output
#     paths are taken to be relative to this directory.
#   --dexopt=path/to/dexopt -- Specify the path to the dexopt executable.
#     If unspecified, there must be a unique subdirectory of the build-dir
#     that looks like host/ARCH/bin which must contain dexopt.
#   --product-dir=path/to/product -- Specify the path, relative to the build
#     directory, where the product tree to be used is. This directory should
#     contain the boot classpath jar files. If not specified, then there
#     must be a unique directory in the build named "target/product/NAME",
#     and this is the directory that will be used.
#   --boot-dir=path/to/bootclasspath -- Specify the path, relative to the
#     product directory, of the directory where the boot classpath files
#     reside. If not specified, this defaults to "system/framework"
#   --boot-jars=list:of:jar:base:names -- Specify the list of base names
#     of bootstrap classpath elements, colon-separated. Order is significant
#     and must match the BOOTCLASSPATH that is eventually specified at
#     runtime on the device. This defaults to "core". However, this really
#     needs to match the target product's BOOTCLASSPATH, which, as of this
#     writing, doesn't have a super-strict way of being defined within the
#     build. You can find variations of it in different init.rc files under
#     system/core/rootdir or under product-specific directories.
#   --bootstrap -- Process the bootstrap classes. If this is specified,
#     then, instead of processing a specified input file, no other arguments
#     are taken, and what is processed is the entirety of the boot jar
#     list, in order.
#   --verify={none,remote,all} -- Specify what level of verification to
#     do. Defaults to "all".
#   --optimize={none,verified,all} -- Specify which classes to optimize.
#     Defaults to "verified".
#   --no-register-maps -- Indicate that the output should not contain
#     register maps. By default, register maps are created and included.
#   --uniprocessor -- Indicate that the output should target a uniprocessor.
#     By default, optimizations will be made that specifically target
#     SMP processors (which will merely be superfluous on uniprocessors).
#

# Defaults.
dexopt=''
buildDir='.'
productDir=''
bootDir='system/framework'
bootstrap='no'
doVerify='all'
doOptimize='verified'
doRegisterMaps='yes'
doUniprocessor='no'
bootJars='core'

optimizeFlags='' # built up from the more human-friendly options
bogus='no' # indicates if there was an error during processing arguments

# Iterate over the arguments looking for options.
while true; do
    origOption="$1"

    if [ "x${origOption}" = "x--" ]; then
        # A raw "--" signals the end of option processing.
        shift
        break
    fi

    # Parse the option into components.
    optionBeforeValue=`expr -- "${origOption}" : '--\([^=]*\)='`

    if [ "$?" = '0' ]; then
        # Option has the form "--option=value".
        option="${optionBeforeValue}"
        value=`expr -- "${origOption}" : '--[^=]*=\(.*\)'`
        hasValue='yes'
    else
        option=`expr -- "${origOption}" : '--\(.*\)'`
        if [ "$?" = '1' ]; then
            # Not an option.
            break
        fi
        # Option has the form "--option".
        value=""
        hasValue='no'
    fi
    shift

    # Interpret the option
    if [ "${option}" = 'build-dir' -a "${hasValue}" = 'yes' ]; then
        buildDir="${value}"
    elif [ "${option}" = 'dexopt' -a "${hasValue}" = 'yes' ]; then
        dexopt="${value}"
    elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then
        bootDir="${value}"
    elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then
        productDir="${value}"
    elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then
        bootJars="${value}"
    elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
        bootstrap='yes'
    elif [ "${option}" = 'verify' -a "${hasValue}" = 'yes' ]; then
        doVerify="${value}"
    elif [ "${option}" = 'optimize' -a "${hasValue}" = 'yes' ]; then
        doOptimize="${value}"
    elif [ "${option}" = 'no-register-maps' -a "${hasValue}" = 'no' ]; then
        doRegisterMaps='no'
    elif [ "${option}" = 'uniprocessor' -a "${hasValue}" = 'no' ]; then
        doUniprocessor='yes'
    else
        echo "unknown option: ${origOption}" 1>&2
        bogus='yes'
    fi
done

# Check and set up the input and output files. In the case of bootstrap
# processing, verify that no files are specified.
inputFile=$1
outputFile=$2
if [ "${bootstrap}" = 'yes' ]; then
    if [ "$#" != '0' ]; then
        echo "unexpected arguments in --bootstrap mode" 1>&2
        bogus=yes
    fi
elif [ "$#" != '2' ]; then
    echo "must specify input and output files (and no more arguments)" 1>&2
    bogus=yes
fi

# Sanity-check the specified build directory.
if [ "x${buildDir}" = 'x' ]; then
    echo "must specify build directory" 1>&2
    bogus=yes
elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
    echo "build-dir is not a writable directory: ${buildDir}" 1>&2
    bogus=yes
fi

# Sanity-check the specified boot classpath directory.
if [ "x${bootDir}" = 'x' ]; then
    echo "must specify boot classpath directory" 1>&2
    bogus=yes
fi

# Sanity-check the specified boot jar list.
if [ "x${bootJars}" = 'x' ]; then
    echo "must specify non-empty boot-jars list" 1>&2
    bogus=yes
fi

# Sanity-check and expand the verify option.
if [ "x${doVerify}" = 'xnone' ]; then
    optimizeFlags="${optimizeFlags},v=n"
elif [ "x${doVerify}" = 'xremote' ]; then
    optimizeFlags="${optimizeFlags},v=r"
elif [ "x${doVerify}" = 'xall' ]; then
    optimizeFlags="${optimizeFlags},v=a"
else
    echo "bad value for --verify: ${doVerify}" 1>&2
    bogus=yes
fi

# Sanity-check and expand the optimize option.
if [ "x${doOptimize}" = 'xnone' ]; then
    optimizeFlags="${optimizeFlags},o=n"
elif [ "x${doOptimize}" = 'xverified' ]; then
    optimizeFlags="${optimizeFlags},o=v"
elif [ "x${doOptimize}" = 'xall' ]; then
    optimizeFlags="${optimizeFlags},o=a"
else
    echo "bad value for --optimize: ${doOptimize}" 1>&2
    bogus=yes
fi

# Expand the register maps selection, if necessary.
if [ "${doRegisterMaps}" = 'yes' ]; then
    optimizeFlags="${optimizeFlags},m=y"
fi

# Expand the uniprocessor directive, if necessary.
if [ "${doUniprocessor}" = 'yes' ]; then
    optimizeFlags="${optimizeFlags},u=y"
else
    optimizeFlags="${optimizeFlags},u=n"
fi

# Kill off the spare comma in optimizeFlags.
optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'`

# Error out if there was trouble.
if [ "${bogus}" = 'yes' ]; then
    # There was an error during option processing.
    echo "usage: $0" 1>&2
    echo '  [--build-dir=path/to/out] [--dexopt=path/to/dexopt]' 1>&2
    echo '  [--product-dir=path/to/product] [--boot-dir=name]' 1>&2
    echo '  [--boot-jars=list:of:names] [--bootstrap]' 1>&2
    echo '  [--verify=type] [--optimize=type] [--no-register-maps]' 1>&2
    echo '  [--uniprocessor] path/to/input.jar path/to/output.odex' 1>&2
    exit 1
fi

# Cd to the build directory, un-symlinkifying it for clarity.
cd "${buildDir}"
cd "`/bin/pwd`"

# If needed, find the default product directory.
if [ "x${productDir}" = 'x' ]; then
    productDir="`ls target/product`"
    if [ "$?" != '0' ]; then
        echo "can't find product directory" 1>&2
        exit 1
    elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then
        echo "ambiguous product directory" 1>&2
        exit 1
    fi
    productDir="target/product/${productDir}"
fi

# Verify the product directory.
if [ ! '(' -d "${productDir}" -a -w "${productDir}" ')' ]; then
    echo "product-dir is not a writable directory: ${productDir}" 1>&2
    exit 1
fi

# Expand and verify the boot classpath directory. We add "/./" here to
# separate the build system part of the path from the target system
# suffix part of the path. The dexopt executable (deep inside the vm
# really) uses this to know how to generate the names of the
# dependencies (since we don't want the device files to contain bits
# of pathname from the host build system).
productBootDir="${productDir}/./${bootDir}"
if [ ! '(' -d "${productBootDir}" -a -w "${productBootDir}" ')' ]; then
    echo "boot-dir is not a writable directory: ${productBootDir}" 1>&2
    exit 1
fi

# Find the dexopt binary if necesasry, and verify it.
if [ "x${dexopt}" = 'x' ]; then
    dexopt="`ls host/*/bin/dexopt`"
    if [ "$?" != '0' ]; then
        echo "can't find dexopt binary" 1>&2
        exit 1
    elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then
        echo "ambiguous host directory" 1>&2
        exit 1
    fi
fi
if [ ! -x "${dexopt}" ]; then
    echo "dexopt binary is not executable: ${dexopt}" 1>&2
    exit 1
fi

# Expand the bootJars into paths that are relative from the build
# directory, maintaining the colon separators.
BOOTCLASSPATH=`echo ":${bootJars}" | \
    sed "s!:\([^:]*\)!:${productBootDir}/\1.jar!g" | \
    sed 's/^://'`
export BOOTCLASSPATH

if [ "${bootstrap}" = 'yes' ]; then
    # Split the boot classpath into separate elements and iterate over them,
    # processing each, in order.
    elements=`echo "${BOOTCLASSPATH}" | sed 's/:/ /g'`

    for inputFile in $elements; do
        echo "Processing ${inputFile}" 1>&2
        outputFile="`dirname ${inputFile}`/`basename ${inputFile} .jar`.odex"
        "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
        status="$?"
        if [ "${status}" != '0' ]; then
            exit "${status}"
        fi
    done
else
    echo "Processing ${inputFile}" 1>&2

    bootJarFile=`expr -- "${inputFile}" : "${productDir}/${bootDir}/\(.*\)"`
    if [ "x${bootJarFile}" != 'x' ]; then
        # The input file is in the boot classpath directory, so it needs
        # to have "/./" inserted into it (see longer description above).
        inputFile="${productBootDir}/${bootJarFile}"
    fi

    "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"

    status="$?"
    if [ "${status}" != '0' ]; then
        exit "${status}"
    fi
fi

echo "Done!" 1>&2