Pre-alpha cut of the new dex preoptimization script.

This will successfully find all the right directories etc., but it doesn't
do any actual dex processing yet. Stay tuned!

Change-Id: I4926308231366d6bae7b6a4594b4edb99192bf42
diff --git a/tools/dex-preopt b/tools/dex-preopt
new file mode 100755
index 0000000..3936eec
--- /dev/null
+++ b/tools/dex-preopt
@@ -0,0 +1,180 @@
+#!/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.dex
+#
+# 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.dex" 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=name -- Specify the name of the product to work on. There
+#     should be a directory with this name under build-dir/product. If
+#     not specified, then there must only be one such directory, and that
+#     one will be used.
+#
+
+buildDir="."
+product=""
+bootstrap="no"
+bogus="no"
+
+# 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}" = 'product' -a "${hasValue}" = 'yes' ]; then
+        product="${value}"
+    elif [ "${option}" = 'bootstrap' -a "${hasValue}" = 'no' ]; then
+        bootstrap="yes"
+    else
+        echo "unknown option: ${origOption}" 1>&2
+        bogus="yes"
+    fi
+done
+
+if [ "${bogus}" = 'yes' ]; then
+    # There was an error during option processing.
+    echo "usage: $0 [--bootstrap] [--build-dir=path/to/out]" 1>&2
+    echo "  path/to/input.jar path/to/output.dex" 1>&2
+    exit 1
+fi
+
+# Sanity-check the specified build directory.
+if [ "x${buildDir}" = 'x' ]; then
+    echo "must specify build directory" 1>&2
+    exit 1
+elif [ ! '(' -d "${buildDir}" -a -w "${buildDir}" ')' ]; then
+    echo "build directory is not a writable directory" 1>&2
+    exit 1
+fi
+
+# Cd to the build directory, un-symlinkifying it for clarity.
+cd "${buildDir}"
+cd "`/bin/pwd`"
+
+# Verify and expand the product directory, picking up the default
+# if appropriate.
+if [ "x${product}" = 'x' ]; then
+    # Find the unique product directory.
+    product="`ls target/product`"
+    if [ "$?" != '0' ]; then
+        echo "can't find product directory" 1>&2
+        exit 1
+    elif [ `expr -- "${product}" : ".* "` != '0' ]; then
+        echo "ambiguous product directory" 1>&2
+        exit 1
+    fi
+fi
+
+# Expand the product directory.
+product="target/product/${product}"
+
+if [ ! '(' -d "${product}" -a -w "${product}" ')' ]; then
+    echo "product directory is not a writable directory" 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
+
+
+##
+## TODO: The rest of this is unmodified from fadden's original prototype.
+##
+exit 2
+
+basedir=$rootdir/out/host/linux-x86/pr/sim
+javadir=$basedir/system/framework
+
+# this is where we find bin/dexopt
+export ANDROID_ROOT=$basedir/system
+# location of "dalvik-cache" directory with optimized bootstrap DEX files
+export ANDROID_DATA=$basedir/data
+
+# bootstrap class path, must match what's on the device in init.rc
+export BOOTCLASSPATH=$javadir/core.jar:$javadir/ext.jar:$javadir/framework.jar:$javadir/android.policy.jar:$javadir/services.jar
+
+# dalvik-cache naming convention
+pfx=$ANDROID_DATA/dalvik-cache/work@gitdalvik-dev@out@host@linux-x86@pr@sim@system@framework@
+sfx=@classes.dex
+
+# configure as appropriate for debuggging
+export ANDROID_LOG_TAGS='*:d'
+
+if [ "$1" = "--mkboot" ]; then
+       echo making bootstrap classes
+       dexopt-wrapper $javadir/core.jar ${pfx}core.jar${sfx}
+       dexopt-wrapper $javadir/ext.jar ${pfx}ext.jar${sfx}
+       dexopt-wrapper $javadir/framework.jar ${pfx}framework.jar${sfx}
+       dexopt-wrapper $javadir/android.policy.jar ${pfx}android.policy.jar${sfx}
+       dexopt-wrapper $javadir/services.jar ${pfx}services.jar${sfx}
+       exit 0
+else
+       #echo Using bootstrap classes from $BOOTCLASSPATH
+       exec dexopt-wrapper $*
+fi
\ No newline at end of file