blob: d94968fd3a2b3bd5766befb2f9ccf7478320c25e [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
Alex Lightdba61482016-12-21 08:20:29 -080072JACK_EXPERIMENTAL_ARGS["agents"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
Yohann Roussel4b495672016-03-21 16:26:02 +010073JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
74JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
mikaelpeltier3233dcc2016-11-18 14:23:45 +010075JACK_EXPERIMENTAL_ARGS["method-handles"]="-D jack.java.source.version=1.7 -D jack.android.min-api-level=o-b1"
Alex Lighte4893ee2017-02-09 15:28:53 -080076JACK_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
David Brazdil4846d132015-01-15 19:07:08 +000077
Alex Lightb55f1ac2016-04-12 15:50:55 -070078declare -A SMALI_EXPERIMENTAL_ARGS
79SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api-level 24"
Narayan Kamath9823e782016-08-03 12:46:58 +010080SMALI_EXPERIMENTAL_ARGS["method-handles"]="--api-level 26"
Alex Lightdba61482016-12-21 08:20:29 -080081SMALI_EXPERIMENTAL_ARGS["agents"]="--api-level 26"
Alex Lightb55f1ac2016-04-12 15:50:55 -070082
Alex Lightea1e7702016-07-11 13:39:55 -070083declare -A JAVAC_EXPERIMENTAL_ARGS
84JAVAC_EXPERIMENTAL_ARGS["default-methods"]="-source 1.8 -target 1.8"
85JAVAC_EXPERIMENTAL_ARGS["lambdas"]="-source 1.8 -target 1.8"
Narayan Kamath9823e782016-08-03 12:46:58 +010086JAVAC_EXPERIMENTAL_ARGS["method-handles"]="-source 1.8 -target 1.8"
Alex Lighte4893ee2017-02-09 15:28:53 -080087# We need to leave javac at default 1.7 so that dx will continue to work
Alex Lightea1e7702016-07-11 13:39:55 -070088JAVAC_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-source 1.7 -target 1.7"
Alex Lightdba61482016-12-21 08:20:29 -080089JAVAC_EXPERIMENTAL_ARGS["agents"]="-source 1.8 -target 1.8"
Alex Lightea1e7702016-07-11 13:39:55 -070090
David Brazdil4846d132015-01-15 19:07:08 +000091while true; do
92 if [ "x$1" = "x--dx-option" ]; then
93 shift
Alex Lightb55f1ac2016-04-12 15:50:55 -070094 on="$1"
David Brazdil4846d132015-01-15 19:07:08 +000095 DX_FLAGS="${DX_FLAGS} $option"
96 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070097 elif [ "x$1" = "x--jvm" ]; then
98 shift
Alex Light6a439bc2015-10-26 17:52:36 -070099 elif [ "x$1" = "x--no-src" ]; then
100 HAS_SRC=false
101 shift
102 elif [ "x$1" = "x--no-src2" ]; then
103 HAS_SRC2=false
104 shift
105 elif [ "x$1" = "x--no-src-multidex" ]; then
106 HAS_SRC_MULTIDEX=false
107 shift
Alex Lightfedd91d2016-01-07 14:49:16 -0800108 elif [ "x$1" = "x--no-smali-multidex" ]; then
109 HAS_SMALI_MULTIDEX=false
110 shift
Alex Light6a439bc2015-10-26 17:52:36 -0700111 elif [ "x$1" = "x--no-src-ex" ]; then
112 HAS_SRC_EX=false
113 shift
114 elif [ "x$1" = "x--no-smali" ]; then
115 HAS_SMALI=false
116 shift
117 elif [ "x$1" = "x--experimental" ]; then
118 shift
Alex Lightea1e7702016-07-11 13:39:55 -0700119 # We have a specific experimental configuration so don't use the default.
120 DEFAULT_EXPERIMENT=""
Alex Light6a439bc2015-10-26 17:52:36 -0700121 EXPERIMENTAL="${EXPERIMENTAL} $1"
122 shift
David Brazdil4846d132015-01-15 19:07:08 +0000123 elif expr "x$1" : "x--" >/dev/null 2>&1; then
124 echo "unknown $0 option: $1" 1>&2
125 exit 1
126 else
127 break
128 fi
129done
130
Alex Lightea1e7702016-07-11 13:39:55 -0700131# Be sure to get any default arguments if not doing any experiments.
132EXPERIMENTAL="${EXPERIMENTAL} ${DEFAULT_EXPERIMENT}"
133
Sebastien Hertz361ad552017-01-04 16:07:57 +0100134if [ "${JACK_SERVER}" = "false" ]; then
135 # Run in single-threaded mode for the continuous buildbot.
136 JACK_ARGS="${JACK_ARGS} -D sched.runner=single-threaded"
137else
138 # Run with 4 threads to reduce memory footprint and thread contention.
139 JACK_ARGS="${JACK_ARGS} -D sched.runner=multi-threaded"
140 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.kind=fixed"
141 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.fixed.count=4"
142fi
143
Alex Light6a439bc2015-10-26 17:52:36 -0700144# Add args from the experimental mappings.
145for experiment in ${EXPERIMENTAL}; do
146 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightb55f1ac2016-04-12 15:50:55 -0700147 SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightea1e7702016-07-11 13:39:55 -0700148 JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Light6a439bc2015-10-26 17:52:36 -0700149done
150
Stephen Kyle40d35182014-10-03 13:47:56 +0100151if [ -e classes.dex ]; then
152 zip $TEST_NAME.jar classes.dex
153 exit 0
154fi
155
Alex Light6a439bc2015-10-26 17:52:36 -0700156if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700157 # No src directory? Then forget about trying to run dx.
158 SKIP_DX_MERGER="true"
159fi
160
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000161if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
162 mkdir classes
163 mkdir classes-ex
164 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
165 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
166 if [ ${USE_JACK} = "true" ]; then
167 jar cf classes.jill.jar -C classes .
168 jar cf classes-ex.jill.jar -C classes-ex .
169
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000170 ${JACK} --import classes-ex.jill.jar --output-dex .
171 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000172 ${JACK} --import classes.jill.jar --output-dex .
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000173 else
174 if [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000175 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes.dex --dump-width=1000 classes-ex
176 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000177 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex --dump-width=1000 classes
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100178 fi
Alex Light6a439bc2015-10-26 17:52:36 -0700179 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100180else
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100181 if [ ${USE_JACK} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000182 # Jack toolchain
183 if [ "${HAS_SRC}" = "true" ]; then
184 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
185 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
186 # when creating the output .dex file.
187 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
188 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
189 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
190 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
191 else
192 ${JACK} ${JACK_ARGS} --output-jack src.jack src
193 fi
194 jack_extra_args="${jack_extra_args} --import src.jack"
195 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700196
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000197 if [ "${HAS_SRC2}" = "true" ]; then
198 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
199 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
200 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
201 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
202 jack_extra_args="--import src2.jack ${jack_extra_args}"
203 fi
204
205 # Compile jack files into a DEX file.
206 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
207 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
208 fi
209 else
210 # Legacy toolchain with javac+dx
211 if [ "${HAS_SRC}" = "true" ]; then
212 mkdir classes
213 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
214 fi
215
216 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
217 mkdir classes2
218 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
219 if [ ${NEED_DEX} = "true" ]; then
220 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
221 --dump-width=1000 ${DX_FLAGS} classes2
222 fi
223 fi
224
225 if [ "${HAS_SRC2}" = "true" ]; then
226 mkdir -p classes
227 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
228 fi
229
230 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
231 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
232 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
233 --dump-width=1000 ${DX_FLAGS} classes
234 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100235 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700236 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000237fi
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000238
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000239if [ "${HAS_SMALI}" = "true" ]; then
240 # Compile Smali classes
241 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000242
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000243 # Don't bother with dexmerger if we provide our own main function in a smali file.
244 if [ ${SKIP_DX_MERGER} = "false" ]; then
245 ${DXMERGER} classes.dex classes.dex smali_classes.dex
246 else
247 mv smali_classes.dex classes.dex
248 fi
249fi
250
251if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
252 # Compile Smali classes
253 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
254
255 # Don't bother with dexmerger if we provide our own main function in a smali file.
256 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
257 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
258 else
259 mv smali_classes2.dex classes2.dex
260 fi
261fi
262
263
264if [ ${HAS_SRC_EX} = "true" ]; then
265 if [ ${USE_JACK} = "true" ]; then
266 # Rename previous "classes.dex" so it is not overwritten.
267 mv classes.dex classes-1.dex
268 #TODO find another way to append src.jack to the jack classpath
269 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
270 zip $TEST_NAME-ex.jar classes.dex
271 # Restore previous "classes.dex" so it can be zipped.
272 mv classes-1.dex classes.dex
273 else
274 mkdir classes-ex
275 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
276 if [ ${NEED_DEX} = "true" ]; then
277 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
278 --dump-width=1000 ${DX_FLAGS} classes-ex
279
280 # quick shuffle so that the stored name is "classes.dex"
281 mv classes.dex classes-1.dex
282 mv classes-ex.dex classes.dex
283 zip $TEST_NAME-ex.jar classes.dex
284 mv classes.dex classes-ex.dex
285 mv classes-1.dex classes.dex
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000286 fi
287 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000288fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100289
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000290# Create a single jar with two dex files for multidex.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000291if [ ${NEED_DEX} = "true" ]; then
292 if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
293 zip $TEST_NAME.jar classes.dex classes2.dex
294 else
295 zip $TEST_NAME.jar classes.dex
296 fi
Nicolas Geoffray12508612014-11-05 12:34:24 +0000297fi