blob: a0408aaf79a245a85047150e366a128fce368e7f [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
Alex Lightea1e7702016-07-11 13:39:55 -070067# The key for default arguments if no experimental things are enabled.
68DEFAULT_EXPERIMENT="no-experiment"
69
Alex Light6a439bc2015-10-26 17:52:36 -070070# Setup experimental flag mappings in a bash associative array.
71declare -A JACK_EXPERIMENTAL_ARGS
Yohann Roussel4b495672016-03-21 16:26:02 +010072JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
73JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
mikaelpeltier3233dcc2016-11-18 14:23:45 +010074JACK_EXPERIMENTAL_ARGS["method-handles"]="-D jack.java.source.version=1.7 -D jack.android.min-api-level=o-b1"
David Brazdil4846d132015-01-15 19:07:08 +000075
Alex Lightb55f1ac2016-04-12 15:50:55 -070076declare -A SMALI_EXPERIMENTAL_ARGS
77SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api-level 24"
Narayan Kamath9823e782016-08-03 12:46:58 +010078SMALI_EXPERIMENTAL_ARGS["method-handles"]="--api-level 26"
Alex Lightb55f1ac2016-04-12 15:50:55 -070079
Alex Lightea1e7702016-07-11 13:39:55 -070080declare -A JAVAC_EXPERIMENTAL_ARGS
81JAVAC_EXPERIMENTAL_ARGS["default-methods"]="-source 1.8 -target 1.8"
82JAVAC_EXPERIMENTAL_ARGS["lambdas"]="-source 1.8 -target 1.8"
Narayan Kamath9823e782016-08-03 12:46:58 +010083JAVAC_EXPERIMENTAL_ARGS["method-handles"]="-source 1.8 -target 1.8"
Alex Lightea1e7702016-07-11 13:39:55 -070084JAVAC_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-source 1.7 -target 1.7"
85
David Brazdil4846d132015-01-15 19:07:08 +000086while true; do
87 if [ "x$1" = "x--dx-option" ]; then
88 shift
Alex Lightb55f1ac2016-04-12 15:50:55 -070089 on="$1"
David Brazdil4846d132015-01-15 19:07:08 +000090 DX_FLAGS="${DX_FLAGS} $option"
91 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070092 elif [ "x$1" = "x--jvm" ]; then
93 shift
Alex Light6a439bc2015-10-26 17:52:36 -070094 elif [ "x$1" = "x--no-src" ]; then
95 HAS_SRC=false
96 shift
97 elif [ "x$1" = "x--no-src2" ]; then
98 HAS_SRC2=false
99 shift
100 elif [ "x$1" = "x--no-src-multidex" ]; then
101 HAS_SRC_MULTIDEX=false
102 shift
Alex Lightfedd91d2016-01-07 14:49:16 -0800103 elif [ "x$1" = "x--no-smali-multidex" ]; then
104 HAS_SMALI_MULTIDEX=false
105 shift
Alex Light6a439bc2015-10-26 17:52:36 -0700106 elif [ "x$1" = "x--no-src-ex" ]; then
107 HAS_SRC_EX=false
108 shift
109 elif [ "x$1" = "x--no-smali" ]; then
110 HAS_SMALI=false
111 shift
112 elif [ "x$1" = "x--experimental" ]; then
113 shift
Alex Lightea1e7702016-07-11 13:39:55 -0700114 # We have a specific experimental configuration so don't use the default.
115 DEFAULT_EXPERIMENT=""
Alex Light6a439bc2015-10-26 17:52:36 -0700116 EXPERIMENTAL="${EXPERIMENTAL} $1"
117 shift
David Brazdil4846d132015-01-15 19:07:08 +0000118 elif expr "x$1" : "x--" >/dev/null 2>&1; then
119 echo "unknown $0 option: $1" 1>&2
120 exit 1
121 else
122 break
123 fi
124done
125
Alex Lightea1e7702016-07-11 13:39:55 -0700126# Be sure to get any default arguments if not doing any experiments.
127EXPERIMENTAL="${EXPERIMENTAL} ${DEFAULT_EXPERIMENT}"
128
Sebastien Hertz361ad552017-01-04 16:07:57 +0100129if [ "${JACK_SERVER}" = "false" ]; then
130 # Run in single-threaded mode for the continuous buildbot.
131 JACK_ARGS="${JACK_ARGS} -D sched.runner=single-threaded"
132else
133 # Run with 4 threads to reduce memory footprint and thread contention.
134 JACK_ARGS="${JACK_ARGS} -D sched.runner=multi-threaded"
135 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.kind=fixed"
136 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.fixed.count=4"
137fi
138
Alex Light6a439bc2015-10-26 17:52:36 -0700139# Add args from the experimental mappings.
140for experiment in ${EXPERIMENTAL}; do
141 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightb55f1ac2016-04-12 15:50:55 -0700142 SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightea1e7702016-07-11 13:39:55 -0700143 JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Light6a439bc2015-10-26 17:52:36 -0700144done
145
Stephen Kyle40d35182014-10-03 13:47:56 +0100146if [ -e classes.dex ]; then
147 zip $TEST_NAME.jar classes.dex
148 exit 0
149fi
150
Alex Light6a439bc2015-10-26 17:52:36 -0700151if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700152 # No src directory? Then forget about trying to run dx.
153 SKIP_DX_MERGER="true"
154fi
155
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000156if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
157 mkdir classes
158 mkdir classes-ex
159 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
160 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
161 if [ ${USE_JACK} = "true" ]; then
162 jar cf classes.jill.jar -C classes .
163 jar cf classes-ex.jill.jar -C classes-ex .
164
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000165 ${JACK} --import classes-ex.jill.jar --output-dex .
166 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000167 ${JACK} --import classes.jill.jar --output-dex .
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000168 else
169 if [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000170 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes.dex --dump-width=1000 classes-ex
171 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000172 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex --dump-width=1000 classes
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100173 fi
Alex Light6a439bc2015-10-26 17:52:36 -0700174 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100175else
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100176 if [ ${USE_JACK} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000177 # Jack toolchain
178 if [ "${HAS_SRC}" = "true" ]; then
179 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
180 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
181 # when creating the output .dex file.
182 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
183 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
184 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
185 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
186 else
187 ${JACK} ${JACK_ARGS} --output-jack src.jack src
188 fi
189 jack_extra_args="${jack_extra_args} --import src.jack"
190 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700191
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000192 if [ "${HAS_SRC2}" = "true" ]; then
193 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
194 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
195 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
196 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
197 jack_extra_args="--import src2.jack ${jack_extra_args}"
198 fi
199
200 # Compile jack files into a DEX file.
201 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
202 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
203 fi
204 else
205 # Legacy toolchain with javac+dx
206 if [ "${HAS_SRC}" = "true" ]; then
207 mkdir classes
208 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
209 fi
210
211 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
212 mkdir classes2
213 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
214 if [ ${NEED_DEX} = "true" ]; then
215 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
216 --dump-width=1000 ${DX_FLAGS} classes2
217 fi
218 fi
219
220 if [ "${HAS_SRC2}" = "true" ]; then
221 mkdir -p classes
222 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
223 fi
224
225 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
226 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
227 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
228 --dump-width=1000 ${DX_FLAGS} classes
229 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100230 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700231 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000232fi
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000233
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000234if [ "${HAS_SMALI}" = "true" ]; then
235 # Compile Smali classes
236 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000237
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000238 # Don't bother with dexmerger if we provide our own main function in a smali file.
239 if [ ${SKIP_DX_MERGER} = "false" ]; then
240 ${DXMERGER} classes.dex classes.dex smali_classes.dex
241 else
242 mv smali_classes.dex classes.dex
243 fi
244fi
245
246if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
247 # Compile Smali classes
248 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
249
250 # Don't bother with dexmerger if we provide our own main function in a smali file.
251 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
252 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
253 else
254 mv smali_classes2.dex classes2.dex
255 fi
256fi
257
258
259if [ ${HAS_SRC_EX} = "true" ]; then
260 if [ ${USE_JACK} = "true" ]; then
261 # Rename previous "classes.dex" so it is not overwritten.
262 mv classes.dex classes-1.dex
263 #TODO find another way to append src.jack to the jack classpath
264 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
265 zip $TEST_NAME-ex.jar classes.dex
266 # Restore previous "classes.dex" so it can be zipped.
267 mv classes-1.dex classes.dex
268 else
269 mkdir classes-ex
270 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
271 if [ ${NEED_DEX} = "true" ]; then
272 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
273 --dump-width=1000 ${DX_FLAGS} classes-ex
274
275 # quick shuffle so that the stored name is "classes.dex"
276 mv classes.dex classes-1.dex
277 mv classes-ex.dex classes.dex
278 zip $TEST_NAME-ex.jar classes.dex
279 mv classes.dex classes-ex.dex
280 mv classes-1.dex classes.dex
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000281 fi
282 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000283fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100284
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000285# Create a single jar with two dex files for multidex.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000286if [ ${NEED_DEX} = "true" ]; then
287 if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
288 zip $TEST_NAME.jar classes.dex classes2.dex
289 else
290 zip $TEST_NAME.jar classes.dex
291 fi
Nicolas Geoffray12508612014-11-05 12:34:24 +0000292fi