blob: 3d84821bf01e6473201edc5b7a08fa0f280c3178 [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
Alex Lightfedd91d2016-01-07 14:49:16 -080045if [ -d smali-multidex ]; then
46 HAS_SMALI_MULTIDEX=true
47else
48 HAS_SMALI_MULTIDEX=false
49fi
50
Alex Light6a439bc2015-10-26 17:52:36 -070051if [ -d src-ex ]; then
52 HAS_SRC_EX=true
53else
54 HAS_SRC_EX=false
55fi
56
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +000057if [ -d src-dex2oat-unresolved ]; then
58 HAS_SRC_DEX2OAT_UNRESOLVED=true
59else
60 HAS_SRC_DEX2OAT_UNRESOLVED=false
61fi
62
David Brazdil4846d132015-01-15 19:07:08 +000063DX_FLAGS=""
Igor Murashkin158f35c2015-06-10 15:55:30 -070064SKIP_DX_MERGER="false"
Alex Light6a439bc2015-10-26 17:52:36 -070065EXPERIMENTAL=""
66
67# Setup experimental flag mappings in a bash associative array.
68declare -A JACK_EXPERIMENTAL_ARGS
Yohann Roussel4b495672016-03-21 16:26:02 +010069JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
70JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
David Brazdil4846d132015-01-15 19:07:08 +000071
72while true; do
73 if [ "x$1" = "x--dx-option" ]; then
74 shift
75 option="$1"
76 DX_FLAGS="${DX_FLAGS} $option"
77 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070078 elif [ "x$1" = "x--jvm" ]; then
79 shift
Alex Light6a439bc2015-10-26 17:52:36 -070080 elif [ "x$1" = "x--no-src" ]; then
81 HAS_SRC=false
82 shift
83 elif [ "x$1" = "x--no-src2" ]; then
84 HAS_SRC2=false
85 shift
86 elif [ "x$1" = "x--no-src-multidex" ]; then
87 HAS_SRC_MULTIDEX=false
88 shift
Alex Lightfedd91d2016-01-07 14:49:16 -080089 elif [ "x$1" = "x--no-smali-multidex" ]; then
90 HAS_SMALI_MULTIDEX=false
91 shift
Alex Light6a439bc2015-10-26 17:52:36 -070092 elif [ "x$1" = "x--no-src-ex" ]; then
93 HAS_SRC_EX=false
94 shift
95 elif [ "x$1" = "x--no-smali" ]; then
96 HAS_SMALI=false
97 shift
98 elif [ "x$1" = "x--experimental" ]; then
99 shift
100 EXPERIMENTAL="${EXPERIMENTAL} $1"
101 shift
David Brazdil4846d132015-01-15 19:07:08 +0000102 elif expr "x$1" : "x--" >/dev/null 2>&1; then
103 echo "unknown $0 option: $1" 1>&2
104 exit 1
105 else
106 break
107 fi
108done
109
Alex Light6a439bc2015-10-26 17:52:36 -0700110# Add args from the experimental mappings.
111for experiment in ${EXPERIMENTAL}; do
112 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
113done
114
Stephen Kyle40d35182014-10-03 13:47:56 +0100115if [ -e classes.dex ]; then
116 zip $TEST_NAME.jar classes.dex
117 exit 0
118fi
119
Alex Light6a439bc2015-10-26 17:52:36 -0700120if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700121 # No src directory? Then forget about trying to run dx.
122 SKIP_DX_MERGER="true"
123fi
124
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000125if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
126 mkdir classes
127 mkdir classes-ex
128 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
129 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
130 if [ ${USE_JACK} = "true" ]; then
131 jar cf classes.jill.jar -C classes .
132 jar cf classes-ex.jill.jar -C classes-ex .
133
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000134 ${JACK} --import classes-ex.jill.jar --output-dex .
135 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000136 ${JACK} --import classes.jill.jar --output-dex .
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000137 else
138 if [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000139 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes.dex --dump-width=1000 classes-ex
140 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000141 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex --dump-width=1000 classes
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100142 fi
Alex Light6a439bc2015-10-26 17:52:36 -0700143 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100144else
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100145 if [ ${USE_JACK} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000146 # Jack toolchain
147 if [ "${HAS_SRC}" = "true" ]; then
148 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
149 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
150 # when creating the output .dex file.
151 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
152 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
153 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
154 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
155 else
156 ${JACK} ${JACK_ARGS} --output-jack src.jack src
157 fi
158 jack_extra_args="${jack_extra_args} --import src.jack"
159 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700160
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000161 if [ "${HAS_SRC2}" = "true" ]; then
162 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
163 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
164 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
165 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
166 jack_extra_args="--import src2.jack ${jack_extra_args}"
167 fi
168
169 # Compile jack files into a DEX file.
170 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
171 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
172 fi
173 else
174 # Legacy toolchain with javac+dx
175 if [ "${HAS_SRC}" = "true" ]; then
176 mkdir classes
177 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
178 fi
179
180 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
181 mkdir classes2
182 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
183 if [ ${NEED_DEX} = "true" ]; then
184 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
185 --dump-width=1000 ${DX_FLAGS} classes2
186 fi
187 fi
188
189 if [ "${HAS_SRC2}" = "true" ]; then
190 mkdir -p classes
191 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
192 fi
193
194 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
195 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
196 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
197 --dump-width=1000 ${DX_FLAGS} classes
198 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100199 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700200 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000201fi
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000202
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000203if [ "${HAS_SMALI}" = "true" ]; then
204 # Compile Smali classes
205 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000206
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000207 # Don't bother with dexmerger if we provide our own main function in a smali file.
208 if [ ${SKIP_DX_MERGER} = "false" ]; then
209 ${DXMERGER} classes.dex classes.dex smali_classes.dex
210 else
211 mv smali_classes.dex classes.dex
212 fi
213fi
214
215if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
216 # Compile Smali classes
217 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
218
219 # Don't bother with dexmerger if we provide our own main function in a smali file.
220 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
221 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
222 else
223 mv smali_classes2.dex classes2.dex
224 fi
225fi
226
227
228if [ ${HAS_SRC_EX} = "true" ]; then
229 if [ ${USE_JACK} = "true" ]; then
230 # Rename previous "classes.dex" so it is not overwritten.
231 mv classes.dex classes-1.dex
232 #TODO find another way to append src.jack to the jack classpath
233 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
234 zip $TEST_NAME-ex.jar classes.dex
235 # Restore previous "classes.dex" so it can be zipped.
236 mv classes-1.dex classes.dex
237 else
238 mkdir classes-ex
239 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
240 if [ ${NEED_DEX} = "true" ]; then
241 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
242 --dump-width=1000 ${DX_FLAGS} classes-ex
243
244 # quick shuffle so that the stored name is "classes.dex"
245 mv classes.dex classes-1.dex
246 mv classes-ex.dex classes.dex
247 zip $TEST_NAME-ex.jar classes.dex
248 mv classes.dex classes-ex.dex
249 mv classes-1.dex classes.dex
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000250 fi
251 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000252fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100253
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000254# Create a single jar with two dex files for multidex.
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000255if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
256 zip $TEST_NAME.jar classes.dex classes2.dex
257elif [ ${NEED_DEX} = "true" ]; then
258 zip $TEST_NAME.jar classes.dex
Nicolas Geoffray12508612014-11-05 12:34:24 +0000259fi