blob: 7242428f1e9e80edea99af7ec327623b316fd405 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001#!/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.
18set -e
19
Alex Light6a439bc2015-10-26 17:52:36 -070020# Set default values for directories.
21if [ -d smali ]; then
22 HAS_SMALI=true
23else
24 HAS_SMALI=false
25fi
26
27if [ -d src ]; then
28 HAS_SRC=true
29else
30 HAS_SRC=false
31fi
32
33if [ -d src2 ]; then
34 HAS_SRC2=true
35else
36 HAS_SRC2=false
37fi
38
39if [ -d src-multidex ]; then
40 HAS_SRC_MULTIDEX=true
41else
42 HAS_SRC_MULTIDEX=false
43fi
44
45if [ -d src-ex ]; then
46 HAS_SRC_EX=true
47else
48 HAS_SRC_EX=false
49fi
50
David Brazdil4846d132015-01-15 19:07:08 +000051DX_FLAGS=""
Igor Murashkin158f35c2015-06-10 15:55:30 -070052SKIP_DX_MERGER="false"
Alex Light6a439bc2015-10-26 17:52:36 -070053EXPERIMENTAL=""
54
55# Setup experimental flag mappings in a bash associative array.
56declare -A JACK_EXPERIMENTAL_ARGS
57JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8"
58JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8"
David Brazdil4846d132015-01-15 19:07:08 +000059
60while true; do
61 if [ "x$1" = "x--dx-option" ]; then
62 shift
63 option="$1"
64 DX_FLAGS="${DX_FLAGS} $option"
65 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070066 elif [ "x$1" = "x--jvm" ]; then
67 shift
Alex Light6a439bc2015-10-26 17:52:36 -070068 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 Brazdil4846d132015-01-15 19:07:08 +000087 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
93done
94
Alex Light6a439bc2015-10-26 17:52:36 -070095# Add args from the experimental mappings.
96for experiment in ${EXPERIMENTAL}; do
97 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
98done
99
Stephen Kyle40d35182014-10-03 13:47:56 +0100100if [ -e classes.dex ]; then
101 zip $TEST_NAME.jar classes.dex
102 exit 0
103fi
104
Alex Light6a439bc2015-10-26 17:52:36 -0700105if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700106 # No src directory? Then forget about trying to run dx.
107 SKIP_DX_MERGER="true"
108fi
109
Alex Light6a439bc2015-10-26 17:52:36 -0700110if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100111 # Jack does not support this configuration unless we specify how to partition the DEX file
112 # with a .jpp file.
113 USE_JACK="false"
114fi
115
116if [ ${USE_JACK} = "true" ]; then
117 # Jack toolchain
Alex Light6a439bc2015-10-26 17:52:36 -0700118 if [ "${HAS_SRC}" = "true" ]; then
119 ${JACK} ${JACK_ARGS} --output-jack src.jack src
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100120 imported_jack_files="--import src.jack"
121 fi
122
Alex Light6a439bc2015-10-26 17:52:36 -0700123 if [ "${HAS_SRC2}" = "true" ]; then
124 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100125 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 Light6a439bc2015-10-26 17:52:36 -0700130 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 Hertz19ac0272015-02-24 17:39:50 +0100133else
134 # Legacy toolchain with javac+dx
Alex Light6a439bc2015-10-26 17:52:36 -0700135 if [ "${HAS_SRC}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100136 mkdir classes
Alex Light6a439bc2015-10-26 17:52:36 -0700137 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100138 fi
139
Alex Light6a439bc2015-10-26 17:52:36 -0700140 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100141 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 Light6a439bc2015-10-26 17:52:36 -0700149 if [ "${HAS_SRC2}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100150 mkdir -p classes
Alex Light6a439bc2015-10-26 17:52:36 -0700151 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100152 fi
153
Alex Light6a439bc2015-10-26 17:52:36 -0700154 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 Hertz19ac0272015-02-24 17:39:50 +0100159 fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100160fi
161
Alex Light6a439bc2015-10-26 17:52:36 -0700162if [ "${HAS_SMALI}" = "true" ]; then
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100163 # Compile Smali classes
Alex Light6a439bc2015-10-26 17:52:36 -0700164 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Igor Murashkin158f35c2015-06-10 15:55:30 -0700165
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 Hughesc717eef2012-06-15 16:01:26 -0700172fi
TDYa127b92bcab2012-04-08 00:09:51 -0700173
Alex Light6a439bc2015-10-26 17:52:36 -0700174if [ ${HAS_SRC_EX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100175 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 Light6a439bc2015-10-26 17:52:36 -0700179 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100180 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 Light6a439bc2015-10-26 17:52:36 -0700185 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100186 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
jeffhao5d1ac922011-09-29 17:41:15 -0700189
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100190 # 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 Hughesc717eef2012-06-15 16:01:26 -0700197 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700198fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100199
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000200# Create a single jar with two dex files for multidex.
Alex Light6a439bc2015-10-26 17:52:36 -0700201if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100202 zip $TEST_NAME.jar classes.dex classes2.dex
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000203elif [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray12508612014-11-05 12:34:24 +0000204 zip $TEST_NAME.jar classes.dex
205fi