blob: 962ae3804142cf08ed94ec6a46243f03ba2b6568 [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
Alex Lightb55f1ac2016-04-12 15:50:55 -070072declare -A SMALI_EXPERIMENTAL_ARGS
73SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api-level 24"
74
David Brazdil4846d132015-01-15 19:07:08 +000075while true; do
76 if [ "x$1" = "x--dx-option" ]; then
77 shift
Alex Lightb55f1ac2016-04-12 15:50:55 -070078 on="$1"
David Brazdil4846d132015-01-15 19:07:08 +000079 DX_FLAGS="${DX_FLAGS} $option"
80 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070081 elif [ "x$1" = "x--jvm" ]; then
82 shift
Alex Light6a439bc2015-10-26 17:52:36 -070083 elif [ "x$1" = "x--no-src" ]; then
84 HAS_SRC=false
85 shift
86 elif [ "x$1" = "x--no-src2" ]; then
87 HAS_SRC2=false
88 shift
89 elif [ "x$1" = "x--no-src-multidex" ]; then
90 HAS_SRC_MULTIDEX=false
91 shift
Alex Lightfedd91d2016-01-07 14:49:16 -080092 elif [ "x$1" = "x--no-smali-multidex" ]; then
93 HAS_SMALI_MULTIDEX=false
94 shift
Alex Light6a439bc2015-10-26 17:52:36 -070095 elif [ "x$1" = "x--no-src-ex" ]; then
96 HAS_SRC_EX=false
97 shift
98 elif [ "x$1" = "x--no-smali" ]; then
99 HAS_SMALI=false
100 shift
101 elif [ "x$1" = "x--experimental" ]; then
102 shift
103 EXPERIMENTAL="${EXPERIMENTAL} $1"
104 shift
David Brazdil4846d132015-01-15 19:07:08 +0000105 elif expr "x$1" : "x--" >/dev/null 2>&1; then
106 echo "unknown $0 option: $1" 1>&2
107 exit 1
108 else
109 break
110 fi
111done
112
Alex Light6a439bc2015-10-26 17:52:36 -0700113# Add args from the experimental mappings.
114for experiment in ${EXPERIMENTAL}; do
115 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightb55f1ac2016-04-12 15:50:55 -0700116 SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Light6a439bc2015-10-26 17:52:36 -0700117done
118
Stephen Kyle40d35182014-10-03 13:47:56 +0100119if [ -e classes.dex ]; then
120 zip $TEST_NAME.jar classes.dex
121 exit 0
122fi
123
Alex Light6a439bc2015-10-26 17:52:36 -0700124if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700125 # No src directory? Then forget about trying to run dx.
126 SKIP_DX_MERGER="true"
127fi
128
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000129if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
130 mkdir classes
131 mkdir classes-ex
132 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
133 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
134 if [ ${USE_JACK} = "true" ]; then
135 jar cf classes.jill.jar -C classes .
136 jar cf classes-ex.jill.jar -C classes-ex .
137
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000138 ${JACK} --import classes-ex.jill.jar --output-dex .
139 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000140 ${JACK} --import classes.jill.jar --output-dex .
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000141 else
142 if [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000143 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes.dex --dump-width=1000 classes-ex
144 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000145 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex --dump-width=1000 classes
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100146 fi
Alex Light6a439bc2015-10-26 17:52:36 -0700147 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100148else
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100149 if [ ${USE_JACK} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000150 # Jack toolchain
151 if [ "${HAS_SRC}" = "true" ]; then
152 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
153 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
154 # when creating the output .dex file.
155 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
156 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
157 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
158 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
159 else
160 ${JACK} ${JACK_ARGS} --output-jack src.jack src
161 fi
162 jack_extra_args="${jack_extra_args} --import src.jack"
163 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700164
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000165 if [ "${HAS_SRC2}" = "true" ]; then
166 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
167 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
168 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
169 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
170 jack_extra_args="--import src2.jack ${jack_extra_args}"
171 fi
172
173 # Compile jack files into a DEX file.
174 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
175 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
176 fi
177 else
178 # Legacy toolchain with javac+dx
179 if [ "${HAS_SRC}" = "true" ]; then
180 mkdir classes
181 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
182 fi
183
184 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
185 mkdir classes2
186 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
187 if [ ${NEED_DEX} = "true" ]; then
188 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
189 --dump-width=1000 ${DX_FLAGS} classes2
190 fi
191 fi
192
193 if [ "${HAS_SRC2}" = "true" ]; then
194 mkdir -p classes
195 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
196 fi
197
198 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
199 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
200 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
201 --dump-width=1000 ${DX_FLAGS} classes
202 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100203 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700204 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000205fi
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000206
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000207if [ "${HAS_SMALI}" = "true" ]; then
208 # Compile Smali classes
209 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000210
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000211 # Don't bother with dexmerger if we provide our own main function in a smali file.
212 if [ ${SKIP_DX_MERGER} = "false" ]; then
213 ${DXMERGER} classes.dex classes.dex smali_classes.dex
214 else
215 mv smali_classes.dex classes.dex
216 fi
217fi
218
219if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
220 # Compile Smali classes
221 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
222
223 # Don't bother with dexmerger if we provide our own main function in a smali file.
224 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
225 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
226 else
227 mv smali_classes2.dex classes2.dex
228 fi
229fi
230
231
232if [ ${HAS_SRC_EX} = "true" ]; then
233 if [ ${USE_JACK} = "true" ]; then
234 # Rename previous "classes.dex" so it is not overwritten.
235 mv classes.dex classes-1.dex
236 #TODO find another way to append src.jack to the jack classpath
237 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
238 zip $TEST_NAME-ex.jar classes.dex
239 # Restore previous "classes.dex" so it can be zipped.
240 mv classes-1.dex classes.dex
241 else
242 mkdir classes-ex
243 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
244 if [ ${NEED_DEX} = "true" ]; then
245 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
246 --dump-width=1000 ${DX_FLAGS} classes-ex
247
248 # quick shuffle so that the stored name is "classes.dex"
249 mv classes.dex classes-1.dex
250 mv classes-ex.dex classes.dex
251 zip $TEST_NAME-ex.jar classes.dex
252 mv classes.dex classes-ex.dex
253 mv classes-1.dex classes.dex
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000254 fi
255 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000256fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100257
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000258# Create a single jar with two dex files for multidex.
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000259if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
260 zip $TEST_NAME.jar classes.dex classes2.dex
261elif [ ${NEED_DEX} = "true" ]; then
262 zip $TEST_NAME.jar classes.dex
Nicolas Geoffray12508612014-11-05 12:34:24 +0000263fi