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