blob: 082772f44ebe102f0c33517f651e983712f6d45d [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
Igor Murashkine5181932017-06-15 16:03:50 -070063# DESUGAR=false run-test... will disable desugar.
64if [[ "$DESUGAR" == false ]]; then
65 USE_DESUGAR=false
66fi
67
Igor Murashkin271a0f82017-02-14 21:14:17 +000068# Allow overriding ZIP_COMPRESSION_METHOD with e.g. 'store'
69ZIP_COMPRESSION_METHOD="deflate"
70# Align every ZIP file made by calling $ZIPALIGN command?
71WITH_ZIP_ALIGN=false
72ZIP_ALIGN_BYTES="-1"
73
David Brazdil4846d132015-01-15 19:07:08 +000074DX_FLAGS=""
Igor Murashkin158f35c2015-06-10 15:55:30 -070075SKIP_DX_MERGER="false"
Alex Light6a439bc2015-10-26 17:52:36 -070076EXPERIMENTAL=""
77
Igor Murashkin2a337752017-06-16 14:34:40 +000078BUILD_MODE="target"
79DEV_MODE="no"
80
Alex Lightea1e7702016-07-11 13:39:55 -070081# The key for default arguments if no experimental things are enabled.
82DEFAULT_EXPERIMENT="no-experiment"
83
Alex Light6a439bc2015-10-26 17:52:36 -070084# Setup experimental flag mappings in a bash associative array.
85declare -A JACK_EXPERIMENTAL_ARGS
Alex Lightdba61482016-12-21 08:20:29 -080086JACK_EXPERIMENTAL_ARGS["agents"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
Yohann Roussel4b495672016-03-21 16:26:02 +010087JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
88JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
mikaelpeltier3233dcc2016-11-18 14:23:45 +010089JACK_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 -080090JACK_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 +000091
Alex Lightb55f1ac2016-04-12 15:50:55 -070092declare -A SMALI_EXPERIMENTAL_ARGS
Ben Gruver14fc9db2017-04-28 15:30:49 -070093SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api 24"
94SMALI_EXPERIMENTAL_ARGS["method-handles"]="--api 26"
95SMALI_EXPERIMENTAL_ARGS["agents"]="--api 26"
Alex Lightb55f1ac2016-04-12 15:50:55 -070096
Alex Lightea1e7702016-07-11 13:39:55 -070097declare -A JAVAC_EXPERIMENTAL_ARGS
98JAVAC_EXPERIMENTAL_ARGS["default-methods"]="-source 1.8 -target 1.8"
99JAVAC_EXPERIMENTAL_ARGS["lambdas"]="-source 1.8 -target 1.8"
Narayan Kamath9823e782016-08-03 12:46:58 +0100100JAVAC_EXPERIMENTAL_ARGS["method-handles"]="-source 1.8 -target 1.8"
Alex Lighte4893ee2017-02-09 15:28:53 -0800101# We need to leave javac at default 1.7 so that dx will continue to work
Alex Light4eec3c52017-04-14 13:17:26 -0700102JAVAC_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-source 1.8 -target 1.8"
Alex Lightdba61482016-12-21 08:20:29 -0800103JAVAC_EXPERIMENTAL_ARGS["agents"]="-source 1.8 -target 1.8"
Alex Lightea1e7702016-07-11 13:39:55 -0700104
David Brazdil4846d132015-01-15 19:07:08 +0000105while true; do
106 if [ "x$1" = "x--dx-option" ]; then
107 shift
Orion Hodsone1fb77f2017-02-27 13:57:18 +0000108 option="$1"
David Brazdil4846d132015-01-15 19:07:08 +0000109 DX_FLAGS="${DX_FLAGS} $option"
110 shift
Alex Lighteb7c1442015-08-31 13:17:42 -0700111 elif [ "x$1" = "x--jvm" ]; then
112 shift
Alex Light6a439bc2015-10-26 17:52:36 -0700113 elif [ "x$1" = "x--no-src" ]; then
114 HAS_SRC=false
115 shift
116 elif [ "x$1" = "x--no-src2" ]; then
117 HAS_SRC2=false
118 shift
119 elif [ "x$1" = "x--no-src-multidex" ]; then
120 HAS_SRC_MULTIDEX=false
121 shift
Alex Lightfedd91d2016-01-07 14:49:16 -0800122 elif [ "x$1" = "x--no-smali-multidex" ]; then
123 HAS_SMALI_MULTIDEX=false
124 shift
Alex Light6a439bc2015-10-26 17:52:36 -0700125 elif [ "x$1" = "x--no-src-ex" ]; then
126 HAS_SRC_EX=false
127 shift
128 elif [ "x$1" = "x--no-smali" ]; then
129 HAS_SMALI=false
130 shift
131 elif [ "x$1" = "x--experimental" ]; then
132 shift
Alex Lightea1e7702016-07-11 13:39:55 -0700133 # We have a specific experimental configuration so don't use the default.
134 DEFAULT_EXPERIMENT=""
Alex Light6a439bc2015-10-26 17:52:36 -0700135 EXPERIMENTAL="${EXPERIMENTAL} $1"
136 shift
Igor Murashkin271a0f82017-02-14 21:14:17 +0000137 elif [ "x$1" = "x--zip-compression-method" ]; then
138 # Allow using different zip compression method, e.g. 'store'
139 shift
140 ZIP_COMPRESSION_METHOD="$1"
141 shift
142 elif [ "x$1" = "x--zip-align" ]; then
143 # Align ZIP entries to some # of bytes.
144 shift
145 WITH_ZIP_ALIGN=true
146 ZIP_ALIGN_BYTES="$1"
147 shift
Igor Murashkin2a337752017-06-16 14:34:40 +0000148 elif [ "x$1" = "x--host" ]; then
149 BUILD_MODE="host"
150 shift
151 elif [ "x$1" = "x--target" ]; then
152 BUILD_MODE="target"
153 shift
154 elif [ "x$1" = "x--dev" ]; then
155 DEV_MODE="yes"
156 shift
David Brazdil4846d132015-01-15 19:07:08 +0000157 elif expr "x$1" : "x--" >/dev/null 2>&1; then
158 echo "unknown $0 option: $1" 1>&2
159 exit 1
160 else
161 break
162 fi
163done
164
Alex Lightea1e7702016-07-11 13:39:55 -0700165# Be sure to get any default arguments if not doing any experiments.
166EXPERIMENTAL="${EXPERIMENTAL} ${DEFAULT_EXPERIMENT}"
167
Sebastien Hertz361ad552017-01-04 16:07:57 +0100168if [ "${JACK_SERVER}" = "false" ]; then
169 # Run in single-threaded mode for the continuous buildbot.
170 JACK_ARGS="${JACK_ARGS} -D sched.runner=single-threaded"
171else
172 # Run with 4 threads to reduce memory footprint and thread contention.
173 JACK_ARGS="${JACK_ARGS} -D sched.runner=multi-threaded"
174 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.kind=fixed"
175 JACK_ARGS="${JACK_ARGS} -D sched.runner.thread.fixed.count=4"
176fi
177
Alex Light6a439bc2015-10-26 17:52:36 -0700178# Add args from the experimental mappings.
179for experiment in ${EXPERIMENTAL}; do
180 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightb55f1ac2016-04-12 15:50:55 -0700181 SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Lightea1e7702016-07-11 13:39:55 -0700182 JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}"
Alex Light6a439bc2015-10-26 17:52:36 -0700183done
184
Igor Murashkin271a0f82017-02-14 21:14:17 +0000185#########################################
186
187# Catch all commands to 'ZIP' and prepend extra flags.
188# Optionally, zipalign results to some alignment.
189function zip() {
190 local zip_target="$1"
191 local entry_src="$2"
192 shift 2
193
194 command zip --compression-method "$ZIP_COMPRESSION_METHOD" "$zip_target" "$entry_src" "$@"
195
196 if "$WITH_ZIP_ALIGN"; then
197 # zipalign does not operate in-place, so write results to a temp file.
198 local tmp_file="$(mktemp)"
199 "$ZIPALIGN" -f "$ZIP_ALIGN_BYTES" "$zip_target" "$tmp_file"
200 # replace original zip target with our temp file.
201 mv "$tmp_file" "$zip_target"
202 fi
203}
204
Igor Murashkin2a337752017-06-16 14:34:40 +0000205function desugar() {
206 local desugar_args=--mode=host
207 if [[ $BUILD_MODE == target ]]; then
208 desugar_args=--mode=target
209 fi
210
211 if [[ $DEV_MODE == yes ]]; then
212 desugar_args="$desugar_args --show-commands"
213 fi
214
Igor Murashkine5181932017-06-15 16:03:50 -0700215 "$DESUGAR" --core-only $desugar_args "$@"
Igor Murashkin2a337752017-06-16 14:34:40 +0000216}
217
218# Make a "dex" file given a directory of classes in $1.
219# Also calls desugar on the classes first to convert lambdas.
220function make_dex() {
221 local name="$1"
222
223 local dx_input
224 if [[ "$USE_DESUGAR" == "true" ]]; then
225 # Make a jar first so desugar doesn't need every .class file individually.
226 jar cf "$name.before-desugar.jar" -C "$name" .
227
228 dx_input="${name}.desugar.jar"
229
230 # Make desugared JAR.
231 desugar --input "$name.before-desugar.jar" --output "$dx_input"
232 else
233 dx_input="${name}"
234 fi
235
236 # Make dex file from desugared JAR.
237 ${DX} -JXmx256m --debug --dex --dump-to=${name}.lst --output=${name}.dex --dump-width=1000 ${DX_FLAGS} "${dx_input}"
238}
239
Stephen Kyle40d35182014-10-03 13:47:56 +0100240if [ -e classes.dex ]; then
241 zip $TEST_NAME.jar classes.dex
242 exit 0
243fi
244
Alex Light6a439bc2015-10-26 17:52:36 -0700245if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700246 # No src directory? Then forget about trying to run dx.
247 SKIP_DX_MERGER="true"
248fi
249
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000250if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
251 mkdir classes
252 mkdir classes-ex
253 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
254 ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
255 if [ ${USE_JACK} = "true" ]; then
256 jar cf classes.jill.jar -C classes .
257 jar cf classes-ex.jill.jar -C classes-ex .
258
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000259 ${JACK} --import classes-ex.jill.jar --output-dex .
260 zip ${TEST_NAME}-ex.jar classes.dex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000261 ${JACK} --import classes.jill.jar --output-dex .
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000262 else
263 if [ ${NEED_DEX} = "true" ]; then
Igor Murashkin2a337752017-06-16 14:34:40 +0000264 make_dex classes-ex
Igor Murashkin6c946b52017-06-16 14:27:47 -0700265 mv classes-ex.dex classes.dex # rename it so it shows up as "classes.dex" in the zip file.
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000266 zip ${TEST_NAME}-ex.jar classes.dex
Igor Murashkin2a337752017-06-16 14:34:40 +0000267 make_dex classes
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100268 fi
Alex Light6a439bc2015-10-26 17:52:36 -0700269 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100270else
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100271 if [ ${USE_JACK} = "true" ]; then
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000272 # Jack toolchain
273 if [ "${HAS_SRC}" = "true" ]; then
274 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
275 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
276 # when creating the output .dex file.
277 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
278 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
279 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
280 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
281 else
282 ${JACK} ${JACK_ARGS} --output-jack src.jack src
283 fi
284 jack_extra_args="${jack_extra_args} --import src.jack"
285 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700286
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000287 if [ "${HAS_SRC2}" = "true" ]; then
288 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
289 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
290 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
291 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
292 jack_extra_args="--import src2.jack ${jack_extra_args}"
293 fi
294
295 # Compile jack files into a DEX file.
296 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
297 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
298 fi
299 else
300 # Legacy toolchain with javac+dx
301 if [ "${HAS_SRC}" = "true" ]; then
302 mkdir classes
303 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
304 fi
305
306 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
307 mkdir classes2
308 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
309 if [ ${NEED_DEX} = "true" ]; then
Igor Murashkin2a337752017-06-16 14:34:40 +0000310 make_dex classes2
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000311 fi
312 fi
313
314 if [ "${HAS_SRC2}" = "true" ]; then
315 mkdir -p classes
316 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
317 fi
318
319 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
320 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
Igor Murashkin2a337752017-06-16 14:34:40 +0000321 make_dex classes
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000322 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100323 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700324 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000325fi
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000326
Andreas Gampe2b936812017-02-07 16:03:57 -0800327if [ "${HAS_SMALI}" = "true" -a ${NEED_DEX} = "true" ]; then
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000328 # Compile Smali classes
Ben Gruver14fc9db2017-04-28 15:30:49 -0700329 ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000330
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000331 # Don't bother with dexmerger if we provide our own main function in a smali file.
332 if [ ${SKIP_DX_MERGER} = "false" ]; then
333 ${DXMERGER} classes.dex classes.dex smali_classes.dex
334 else
335 mv smali_classes.dex classes.dex
336 fi
337fi
338
Andreas Gampe2b936812017-02-07 16:03:57 -0800339if [ "${HAS_SMALI_MULTIDEX}" = "true" -a ${NEED_DEX} = "true" ]; then
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000340 # Compile Smali classes
Ben Gruver14fc9db2017-04-28 15:30:49 -0700341 ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000342
343 # Don't bother with dexmerger if we provide our own main function in a smali file.
344 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
345 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
346 else
347 mv smali_classes2.dex classes2.dex
348 fi
349fi
350
351
352if [ ${HAS_SRC_EX} = "true" ]; then
353 if [ ${USE_JACK} = "true" ]; then
354 # Rename previous "classes.dex" so it is not overwritten.
355 mv classes.dex classes-1.dex
356 #TODO find another way to append src.jack to the jack classpath
357 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
358 zip $TEST_NAME-ex.jar classes.dex
359 # Restore previous "classes.dex" so it can be zipped.
360 mv classes-1.dex classes.dex
361 else
362 mkdir classes-ex
363 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
364 if [ ${NEED_DEX} = "true" ]; then
Igor Murashkin2a337752017-06-16 14:34:40 +0000365 make_dex classes-ex
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000366
367 # quick shuffle so that the stored name is "classes.dex"
368 mv classes.dex classes-1.dex
369 mv classes-ex.dex classes.dex
370 zip $TEST_NAME-ex.jar classes.dex
371 mv classes.dex classes-ex.dex
372 mv classes-1.dex classes.dex
Nicolas Geoffray404b5bf2016-03-16 12:39:17 +0000373 fi
374 fi
Nicolas Geoffray44fd0e52016-03-16 15:16:06 +0000375fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100376
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000377# Create a single jar with two dex files for multidex.
Vladimir Markoc5798bf2016-12-09 10:20:54 +0000378if [ ${NEED_DEX} = "true" ]; then
379 if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
380 zip $TEST_NAME.jar classes.dex classes2.dex
381 else
382 zip $TEST_NAME.jar classes.dex
383 fi
Nicolas Geoffray12508612014-11-05 12:34:24 +0000384fi