Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 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 | # |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 18 | # Usage: dex-preopt [options] path/to/input.jar path/to/output.odex |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 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 | # |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 28 | # The "output.odex" file must not already exist. |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 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 | # --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 Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 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" |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 47 | # --boot-jars=list:of:jar:base:names -- Specify the list of base names |
Dan Bornstein | def30fc | 2010-09-14 16:15:49 -0700 | [diff] [blame] | 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. |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 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. |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 61 | # |
| 62 | |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 63 | # Defaults. |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 64 | buildDir="." |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 65 | productDir="" |
| 66 | bootDir="system/framework" |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 67 | bootstrap="no" |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 68 | doVerify="all" |
| 69 | doOptimize="verified" |
| 70 | doRegisterMaps="yes" |
| 71 | bootJars="core" |
| 72 | |
| 73 | optimizeFlags="" # built up from the more human-friendly options |
| 74 | bogus="no" # indicates if there was an error during processing arguments |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 75 | |
| 76 | # Iterate over the arguments looking for options. |
| 77 | while 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 Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 109 | elif [ "${option}" = 'boot-dir' -a "${hasValue}" = 'yes' ]; then |
| 110 | bootDir="${value}" |
| 111 | elif [ "${option}" = 'product-dir' -a "${hasValue}" = 'yes' ]; then |
| 112 | productDir="${value}" |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 113 | elif [ "${option}" = 'boot-jars' -a "${hasValue}" = 'yes' ]; then |
| 114 | bootJars="${value}" |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 115 | elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then |
| 116 | bootstrap="yes" |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 117 | 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 Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 123 | else |
| 124 | echo "unknown option: ${origOption}" 1>&2 |
| 125 | bogus="yes" |
| 126 | fi |
| 127 | done |
| 128 | |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 129 | # Check and set up the input and output files. In the case of bootstrap |
| 130 | # processing, verify that no files are specified. |
| 131 | inputFile=$1 |
| 132 | outputFile=$2 |
| 133 | if [ "${bootstrap}" = "yes" ]; then |
| 134 | if [ "$#" != "0" ]; then |
| 135 | echo "unexpected arguments in --bootstrap mode" 1>&2 |
| 136 | bogus=yes |
| 137 | fi |
| 138 | elif [ "$#" != "2" ]; then |
| 139 | echo "must specify input and output files (and no more arguments)" 1>&2 |
| 140 | bogus=yes |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 141 | fi |
| 142 | |
| 143 | # Sanity-check the specified build directory. |
| 144 | if [ "x${buildDir}" = 'x' ]; then |
| 145 | echo "must specify build directory" 1>&2 |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 146 | bogus=yes |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 147 | elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 148 | echo "build-dir is not a writable directory: ${buildDir}" 1>&2 |
| 149 | bogus=yes |
| 150 | fi |
| 151 | |
| 152 | # Sanity-check the specified boot classpath directory. |
| 153 | if [ "x${bootDir}" = 'x' ]; then |
| 154 | echo "must specify boot classpath directory" 1>&2 |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 155 | bogus=yes |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 156 | fi |
| 157 | |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 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 |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 161 | bogus=yes |
| 162 | fi |
| 163 | |
| 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" |
| 171 | else |
| 172 | echo "bad value for --verify: ${doVerify}" 1>&2 |
| 173 | bogus=yes |
| 174 | fi |
| 175 | |
| 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" |
| 183 | else |
| 184 | echo "bad value for --optimize: ${doOptimize}" 1>&2 |
| 185 | bogus=yes |
| 186 | fi |
| 187 | |
| 188 | # Expand the register maps selection, if necessary. |
| 189 | if [ "${doRegisterMaps}" = "yes" ]; then |
| 190 | optimizeFlags="${optimizeFlags},m=y" |
| 191 | fi |
| 192 | |
| 193 | # Kill off the spare comma in optimizeFlags. |
| 194 | optimizeFlags=`echo ${optimizeFlags} | sed 's/^,//'` |
| 195 | |
| 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 |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 200 | echo " [--product-dir=path/to/product] [--boot-dir=name]" 1>&2 |
| 201 | echo " [--boot-jars=list:of:names]" 1>&2 |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 202 | echo " [--verify=type] [--optimize=type] [--no-register-maps]" 1>&2 |
| 203 | echo " path/to/input.jar path/to/output.odex" 1>&2 |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 204 | exit 1 |
| 205 | fi |
| 206 | |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 207 | # Cd to the build directory, un-symlinkifying it for clarity. |
| 208 | cd "${buildDir}" |
| 209 | cd "`/bin/pwd`" |
| 210 | |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 211 | # If needed, find the default product directory. |
| 212 | if [ "x${productDir}" = 'x' ]; then |
| 213 | productDir="`ls target/product`" |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 214 | if [ "$?" != '0' ]; then |
| 215 | echo "can't find product directory" 1>&2 |
| 216 | exit 1 |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 217 | elif [ `expr -- "${productDir}" : ".* "` != '0' ]; then |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 218 | echo "ambiguous product directory" 1>&2 |
| 219 | exit 1 |
| 220 | fi |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 221 | productDir="target/product/${productDir}" |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 222 | fi |
| 223 | |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 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 |
| 227 | exit 1 |
| 228 | fi |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 229 | |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 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 |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 239 | exit 1 |
| 240 | fi |
| 241 | |
| 242 | # Find the dexopt binary. |
| 243 | dexopt="`ls host/*/bin/dexopt`" |
| 244 | if [ "$?" != '0' ]; then |
| 245 | echo "can't find dexopt binary" 1>&2 |
| 246 | exit 1 |
| 247 | elif [ `expr -- "${dexopt}" : ".* "` != '0' ]; then |
| 248 | echo "ambiguous host directory" 1>&2 |
| 249 | exit 1 |
| 250 | fi |
| 251 | |
Dan Bornstein | 32bc078 | 2010-09-13 17:30:10 -0700 | [diff] [blame] | 252 | # Expand the bootJars into paths that are relative from the build |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 253 | # directory, maintaining the colon separators. |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 254 | BOOTCLASSPATH=`echo ":${bootJars}" | \ |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 255 | sed "s!:\([^:]*\)!:${bootDir}/\1.jar!g" | \ |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 256 | sed 's/^://'` |
| 257 | export BOOTCLASSPATH |
| 258 | |
| 259 | if [ "${bootstrap}" = "yes" ]; then |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 260 | # 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 Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 265 | echo "Processing ${inputFile}" 1>&2 |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 266 | outputFile="`dirname ${inputFile}`/`basename ${inputFile} .jar`.odex" |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 267 | "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}" |
| 268 | status="$?" |
| 269 | if [ "${status}" != "0" ]; then |
| 270 | exit "${status}" |
| 271 | fi |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 272 | done |
| 273 | else |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 274 | echo "Processing ${inputFile}" 1>&2 |
Dan Bornstein | 2b9e8c1 | 2010-09-17 12:54:07 -0700 | [diff] [blame^] | 275 | "${dexopt}" --preopt "${inputFile}" "${outputFile}" "${optimizeFlags}" |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 276 | |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 277 | status="$?" |
| 278 | if [ "${status}" != "0" ]; then |
| 279 | exit "${status}" |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 280 | fi |
Dan Bornstein | 20228de | 2010-09-13 13:40:33 -0700 | [diff] [blame] | 281 | fi |
Dan Bornstein | ab9d89f | 2010-09-12 13:03:27 -0700 | [diff] [blame] | 282 | |
Dan Bornstein | 9660483 | 2010-09-13 15:37:28 -0700 | [diff] [blame] | 283 | echo "Done!" 1>&2 |