#!/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: # --bootstrap -- Process the bootstrap classes. # --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. # --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. # --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. # # Defaults. buildDir="." productDir="" bootDir="system/framework" bootstrap="no" doVerify="all" doOptimize="verified" doRegisterMaps="yes" 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}" = '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" 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 # 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 [--bootstrap] [--build-dir=path/to/out]" 1>&2 echo " [--product-dir=path/to/product] [--boot-dir=name]" 1>&2 echo " [--boot-jars=list:of:names]" 1>&2 echo " [--verify=type] [--optimize=type] [--no-register-maps]" 1>&2 echo " 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). bootDir="${productDir}/./${bootDir}" if [ ! '(' -d "${bootDir}" -a -w "${bootDir}" ')' ]; then echo "boot-dir is not a writable directory: ${bootDir}" 1>&2 exit 1 fi # Find the dexopt binary. 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 # Expand the bootJars into paths that are relative from the build # directory, maintaining the colon separators. BOOTCLASSPATH=`echo ":${bootJars}" | \ sed "s!:\([^:]*\)!:${bootDir}/\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 "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}" status="$?" if [ "${status}" != "0" ]; then exit "${status}" fi fi echo "Done!" 1>&2