1#!/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#
18# Usage: dex-preopt [options] path/to/input.jar path/to/output.odex
19#
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#
28# The "output.odex" file must not already exist.
29#
30# This is expected to be running in a user build environment, where
31# "dexopt" is available on the host.
32#
33# Options:
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
60#     list, in order.
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).
70#
71
72# Defaults.
73dexopt=''
74buildDir='.'
75productDir=''
76bootDir='system/framework'
77bootstrap='no'
78doVerify='all'
79doOptimize='verified'
80doRegisterMaps='yes'
81doUniprocessor='no'
82bootJars='core'
83
84optimizeFlags='' # built up from the more human-friendly options
85bogus='no' # indicates if there was an error during processing arguments
86
87# Iterate over the arguments looking for options.
88while true; do
89    origOption="$1"
90
91    if [ "x${origOption}" = "x--" ]; then
92        # A raw "--" signals the end of option processing.
93        shift
94        break
95    fi
96
97    # Parse the option into components.
98    optionBeforeValue=`expr -- "${origOption}" : '--\([^=]*\)='`
99
100    if [ "$?" = '0' ]; then
101        # Option has the form "--option=value".
102        option="${optionBeforeValue}"
103        value=`expr -- "${origOption}" : '--[^=]*=\(.*\)'`
104        hasValue='yes'
105    else
106        option=`expr -- "${origOption}" : '--\(.*\)'`
107        if [ "$?" = '1' ]; then
108            # Not an option.
109            break
110        fi
111        # Option has the form "--option".
112        value=""
113        hasValue='no'
114    fi
115    shift
116
117    # Interpret the option
118    if [ "${option}" = 'build-dir' -a "${hasValue}" = 'yes' ]; then
119        buildDir="${value}"
120    elif [ "${option}" = 'dexopt' -a "${hasValue}" = 'yes' ]; then
121        dexopt="${value}"
122    elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then
123        bootDir="${value}"
124    elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then
125        productDir="${value}"
126    elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then
127        bootJars="${value}"
128    elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
129        bootstrap='yes'
130    elif [ "${option}" = 'verify' -a "${hasValue}" = 'yes' ]; then
131        doVerify="${value}"
132    elif [ "${option}" = 'optimize' -a "${hasValue}" = 'yes' ]; then
133        doOptimize="${value}"
134    elif [ "${option}" = 'no-register-maps' -a "${hasValue}" = 'no' ]; then
135        doRegisterMaps='no'
136    elif [ "${option}" = 'uniprocessor' -a "${hasValue}" = 'no' ]; then
137        doUniprocessor='yes'
138    else
139        echo "unknown option: ${origOption}" 1>&2
140        bogus='yes'
141    fi
142done
143
144# Check and set up the input and output files. In the case of bootstrap
145# processing, verify that no files are specified.
146inputFile=$1
147outputFile=$2
148if [ "${bootstrap}" = 'yes' ]; then
149    if [ "$#" != '0' ]; then
150        echo "unexpected arguments in --bootstrap mode" 1>&2
151        bogus=yes
152    fi
153elif [ "$#" != '2' ]; then
154    echo "must specify input and output files (and no more arguments)" 1>&2
155    bogus=yes
156fi
157
158# Sanity-check the specified build directory.
159if [ "x${buildDir}" = 'x' ]; then
160    echo "must specify build directory" 1>&2
161    bogus=yes
162elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
163    echo "build-dir is not a writable directory: ${buildDir}" 1>&2
164    bogus=yes
165fi
166
167# Sanity-check the specified boot classpath directory.
168if [ "x${bootDir}" = 'x' ]; then
169    echo "must specify boot classpath directory" 1>&2
170    bogus=yes
171fi
172
173# Sanity-check the specified boot jar list.
174if [ "x${bootJars}" = 'x' ]; then
175    echo "must specify non-empty boot-jars list" 1>&2
176    bogus=yes
177fi
178
179# Sanity-check and expand the verify option.
180if [ "x${doVerify}" = 'xnone' ]; then
181    optimizeFlags="${optimizeFlags},v=n"
182elif [ "x${doVerify}" = 'xremote' ]; then
183    optimizeFlags="${optimizeFlags},v=r"
184elif [ "x${doVerify}" = 'xall' ]; then
185    optimizeFlags="${optimizeFlags},v=a"
186else
187    echo "bad value for --verify: ${doVerify}" 1>&2
188    bogus=yes
189fi
190
191# Sanity-check and expand the optimize option.
192if [ "x${doOptimize}" = 'xnone' ]; then
193    optimizeFlags="${optimizeFlags},o=n"
194elif [ "x${doOptimize}" = 'xverified' ]; then
195    optimizeFlags="${optimizeFlags},o=v"
196elif [ "x${doOptimize}" = 'xall' ]; then
197    optimizeFlags="${optimizeFlags},o=a"
198else
199    echo "bad value for --optimize: ${doOptimize}" 1>&2
200    bogus=yes
201fi
202
203# Expand the register maps selection, if necessary.
204if [ "${doRegisterMaps}" = 'yes' ]; then
205    optimizeFlags="${optimizeFlags},m=y"
206fi
207
208# Expand the uniprocessor directive, if necessary.
209if [ "${doUniprocessor}" = 'yes' ]; then
210    optimizeFlags="${optimizeFlags},u=y"
211else
212    optimizeFlags="${optimizeFlags},u=n"
213fi
214
215# Kill off the spare comma in optimizeFlags.
216optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'`
217
218# Error out if there was trouble.
219if [ "${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
227    exit 1
228fi
229
230# Cd to the build directory, un-symlinkifying it for clarity.
231cd "${buildDir}"
232cd "`/bin/pwd`"
233
234# If needed, find the default product directory.
235if [ "x${productDir}" = 'x' ]; then
236    productDir="`ls target/product`"
237    if [ "$?" != '0' ]; then
238        echo "can't find product directory" 1>&2
239        exit 1
240    elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then
241        echo "ambiguous product directory" 1>&2
242        exit 1
243    fi
244    productDir="target/product/${productDir}"
245fi
246
247# Verify the product directory.
248if [ ! '(' -d "${productDir}" -a -w "${productDir}" ')' ]; then
249    echo "product-dir is not a writable directory: ${productDir}" 1>&2
250    exit 1
251fi
252
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).
259productBootDir="${productDir}/./${bootDir}"
260if [ ! '(' -d "${productBootDir}" -a -w "${productBootDir}" ')' ]; then
261    echo "boot-dir is not a writable directory: ${productBootDir}" 1>&2
262    exit 1
263fi
264
265# Find the dexopt binary if necesasry, and verify it.
266if [ "x${dexopt}" = 'x' ]; then
267    dexopt="`ls host/*/bin/dexopt`"
268    if [ "$?" != '0' ]; then
269        echo "can't find dexopt binary" 1>&2
270        exit 1
271    elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then
272        echo "ambiguous host directory" 1>&2
273        exit 1
274    fi
275fi
276if [ ! -x "${dexopt}" ]; then
277    echo "dexopt binary is not executable: ${dexopt}" 1>&2
278    exit 1
279fi
280
281# Expand the bootJars into paths that are relative from the build
282# directory, maintaining the colon separators.
283BOOTCLASSPATH=`echo ":${bootJars}" | \
284    sed "s!:\([^:]*\)!:${productBootDir}/\1.jar!g" | \
285    sed 's/^://'`
286export BOOTCLASSPATH
287
288if [ "${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'`
292
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}"
297        status="$?"
298        if [ "${status}" != '0' ]; then
299            exit "${status}"
300        fi
301    done
302else
303    echo "Processing ${inputFile}" 1>&2
304
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}"
310    fi
311
312    "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}"
313
314    status="$?"
315    if [ "${status}" != '0' ]; then
316        exit "${status}"
317    fi
318fi
319
320echo "Done!" 1>&2
321