jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2008 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 | # Stop if something fails. |
| 18 | set -e |
| 19 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 20 | # Set default values for directories. |
| 21 | if [ -d smali ]; then |
| 22 | HAS_SMALI=true |
| 23 | else |
| 24 | HAS_SMALI=false |
| 25 | fi |
| 26 | |
| 27 | if [ -d src ]; then |
| 28 | HAS_SRC=true |
| 29 | else |
| 30 | HAS_SRC=false |
| 31 | fi |
| 32 | |
| 33 | if [ -d src2 ]; then |
| 34 | HAS_SRC2=true |
| 35 | else |
| 36 | HAS_SRC2=false |
| 37 | fi |
| 38 | |
| 39 | if [ -d src-multidex ]; then |
| 40 | HAS_SRC_MULTIDEX=true |
| 41 | else |
| 42 | HAS_SRC_MULTIDEX=false |
| 43 | fi |
| 44 | |
| 45 | if [ -d src-ex ]; then |
| 46 | HAS_SRC_EX=true |
| 47 | else |
| 48 | HAS_SRC_EX=false |
| 49 | fi |
| 50 | |
David Brazdil | 4846d13 | 2015-01-15 19:07:08 +0000 | [diff] [blame] | 51 | DX_FLAGS="" |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 52 | SKIP_DX_MERGER="false" |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 53 | EXPERIMENTAL="" |
| 54 | |
| 55 | # Setup experimental flag mappings in a bash associative array. |
| 56 | declare -A JACK_EXPERIMENTAL_ARGS |
| 57 | JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8" |
| 58 | JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8" |
David Brazdil | 4846d13 | 2015-01-15 19:07:08 +0000 | [diff] [blame] | 59 | |
| 60 | while true; do |
| 61 | if [ "x$1" = "x--dx-option" ]; then |
| 62 | shift |
| 63 | option="$1" |
| 64 | DX_FLAGS="${DX_FLAGS} $option" |
| 65 | shift |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 66 | elif [ "x$1" = "x--jvm" ]; then |
| 67 | shift |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 68 | elif [ "x$1" = "x--no-src" ]; then |
| 69 | HAS_SRC=false |
| 70 | shift |
| 71 | elif [ "x$1" = "x--no-src2" ]; then |
| 72 | HAS_SRC2=false |
| 73 | shift |
| 74 | elif [ "x$1" = "x--no-src-multidex" ]; then |
| 75 | HAS_SRC_MULTIDEX=false |
| 76 | shift |
| 77 | elif [ "x$1" = "x--no-src-ex" ]; then |
| 78 | HAS_SRC_EX=false |
| 79 | shift |
| 80 | elif [ "x$1" = "x--no-smali" ]; then |
| 81 | HAS_SMALI=false |
| 82 | shift |
| 83 | elif [ "x$1" = "x--experimental" ]; then |
| 84 | shift |
| 85 | EXPERIMENTAL="${EXPERIMENTAL} $1" |
| 86 | shift |
David Brazdil | 4846d13 | 2015-01-15 19:07:08 +0000 | [diff] [blame] | 87 | elif expr "x$1" : "x--" >/dev/null 2>&1; then |
| 88 | echo "unknown $0 option: $1" 1>&2 |
| 89 | exit 1 |
| 90 | else |
| 91 | break |
| 92 | fi |
| 93 | done |
| 94 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 95 | # Add args from the experimental mappings. |
| 96 | for experiment in ${EXPERIMENTAL}; do |
| 97 | JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}" |
| 98 | done |
| 99 | |
Stephen Kyle | 40d3518 | 2014-10-03 13:47:56 +0100 | [diff] [blame] | 100 | if [ -e classes.dex ]; then |
| 101 | zip $TEST_NAME.jar classes.dex |
| 102 | exit 0 |
| 103 | fi |
| 104 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 105 | if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 106 | # No src directory? Then forget about trying to run dx. |
| 107 | SKIP_DX_MERGER="true" |
| 108 | fi |
| 109 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 110 | if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 111 | # Jack does not support this configuration unless we specify how to partition the DEX file |
| 112 | # with a .jpp file. |
| 113 | USE_JACK="false" |
| 114 | fi |
| 115 | |
| 116 | if [ ${USE_JACK} = "true" ]; then |
| 117 | # Jack toolchain |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 118 | if [ "${HAS_SRC}" = "true" ]; then |
| 119 | ${JACK} ${JACK_ARGS} --output-jack src.jack src |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 120 | imported_jack_files="--import src.jack" |
| 121 | fi |
| 122 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 123 | if [ "${HAS_SRC2}" = "true" ]; then |
| 124 | ${JACK} ${JACK_ARGS} --output-jack src2.jack src2 |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 125 | imported_jack_files="--import src2.jack ${imported_jack_files}" |
| 126 | fi |
| 127 | |
| 128 | # Compile jack files into a DEX file. We set jack.import.type.policy=keep-first to consider |
| 129 | # class definitions from src2 first. |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 130 | if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then |
| 131 | ${JACK} ${JACK_ARGS} ${imported_jack_files} -D jack.import.type.policy=keep-first --output-dex . |
| 132 | fi |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 133 | else |
| 134 | # Legacy toolchain with javac+dx |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 135 | if [ "${HAS_SRC}" = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 136 | mkdir classes |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 137 | ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'` |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 138 | fi |
| 139 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 140 | if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 141 | mkdir classes2 |
| 142 | ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'` |
| 143 | if [ ${NEED_DEX} = "true" ]; then |
| 144 | ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \ |
| 145 | --dump-width=1000 ${DX_FLAGS} classes2 |
| 146 | fi |
| 147 | fi |
| 148 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 149 | if [ "${HAS_SRC2}" = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 150 | mkdir -p classes |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 151 | ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'` |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 152 | fi |
| 153 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 154 | if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then |
| 155 | if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then |
| 156 | ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \ |
| 157 | --dump-width=1000 ${DX_FLAGS} classes |
| 158 | fi |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 159 | fi |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 160 | fi |
| 161 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 162 | if [ "${HAS_SMALI}" = "true" ]; then |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 163 | # Compile Smali classes |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 164 | ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'` |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 165 | |
| 166 | # Don't bother with dexmerger if we provide our own main function in a smali file. |
| 167 | if [ ${SKIP_DX_MERGER} = "false" ]; then |
| 168 | ${DXMERGER} classes.dex classes.dex smali_classes.dex |
| 169 | else |
| 170 | mv smali_classes.dex classes.dex |
| 171 | fi |
Elliott Hughes | c717eef | 2012-06-15 16:01:26 -0700 | [diff] [blame] | 172 | fi |
TDYa127 | b92bcab | 2012-04-08 00:09:51 -0700 | [diff] [blame] | 173 | |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 174 | if [ ${HAS_SRC_EX} = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 175 | if [ ${USE_JACK} = "true" ]; then |
| 176 | # Rename previous "classes.dex" so it is not overwritten. |
| 177 | mv classes.dex classes-1.dex |
| 178 | #TODO find another way to append src.jack to the jack classpath |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 179 | ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 180 | zip $TEST_NAME-ex.jar classes.dex |
| 181 | # Restore previous "classes.dex" so it can be zipped. |
| 182 | mv classes-1.dex classes.dex |
| 183 | else |
| 184 | mkdir classes-ex |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 185 | ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'` |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 186 | if [ ${NEED_DEX} = "true" ]; then |
| 187 | ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \ |
| 188 | --dump-width=1000 ${DX_FLAGS} classes-ex |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 189 | |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 190 | # quick shuffle so that the stored name is "classes.dex" |
| 191 | mv classes.dex classes-1.dex |
| 192 | mv classes-ex.dex classes.dex |
| 193 | zip $TEST_NAME-ex.jar classes.dex |
| 194 | mv classes.dex classes-ex.dex |
| 195 | mv classes-1.dex classes.dex |
| 196 | fi |
Elliott Hughes | c717eef | 2012-06-15 16:01:26 -0700 | [diff] [blame] | 197 | fi |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 198 | fi |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 199 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 200 | # Create a single jar with two dex files for multidex. |
Alex Light | 6a439bc | 2015-10-26 17:52:36 -0700 | [diff] [blame] | 201 | if [ ${HAS_SRC_MULTIDEX} = "true" ]; then |
Sebastien Hertz | 19ac027 | 2015-02-24 17:39:50 +0100 | [diff] [blame] | 202 | zip $TEST_NAME.jar classes.dex classes2.dex |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 203 | elif [ ${NEED_DEX} = "true" ]; then |
Nicolas Geoffray | 1250861 | 2014-11-05 12:34:24 +0000 | [diff] [blame] | 204 | zip $TEST_NAME.jar classes.dex |
| 205 | fi |