blob: 5f78496c3ffa92ad7dfa05e34a6c30dc5c9f79dd [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
David Brazdil4846d132015-01-15 19:07:08 +000057DX_FLAGS=""
Igor Murashkin158f35c2015-06-10 15:55:30 -070058SKIP_DX_MERGER="false"
Alex Light6a439bc2015-10-26 17:52:36 -070059EXPERIMENTAL=""
60
61# Setup experimental flag mappings in a bash associative array.
62declare -A JACK_EXPERIMENTAL_ARGS
63JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8"
64JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8"
David Brazdil4846d132015-01-15 19:07:08 +000065
66while true; do
67 if [ "x$1" = "x--dx-option" ]; then
68 shift
69 option="$1"
70 DX_FLAGS="${DX_FLAGS} $option"
71 shift
Alex Lighteb7c1442015-08-31 13:17:42 -070072 elif [ "x$1" = "x--jvm" ]; then
73 shift
Alex Light6a439bc2015-10-26 17:52:36 -070074 elif [ "x$1" = "x--no-src" ]; then
75 HAS_SRC=false
76 shift
77 elif [ "x$1" = "x--no-src2" ]; then
78 HAS_SRC2=false
79 shift
80 elif [ "x$1" = "x--no-src-multidex" ]; then
81 HAS_SRC_MULTIDEX=false
82 shift
Alex Lightfedd91d2016-01-07 14:49:16 -080083 elif [ "x$1" = "x--no-smali-multidex" ]; then
84 HAS_SMALI_MULTIDEX=false
85 shift
Alex Light6a439bc2015-10-26 17:52:36 -070086 elif [ "x$1" = "x--no-src-ex" ]; then
87 HAS_SRC_EX=false
88 shift
89 elif [ "x$1" = "x--no-smali" ]; then
90 HAS_SMALI=false
91 shift
92 elif [ "x$1" = "x--experimental" ]; then
93 shift
94 EXPERIMENTAL="${EXPERIMENTAL} $1"
95 shift
David Brazdil4846d132015-01-15 19:07:08 +000096 elif expr "x$1" : "x--" >/dev/null 2>&1; then
97 echo "unknown $0 option: $1" 1>&2
98 exit 1
99 else
100 break
101 fi
102done
103
Alex Light6a439bc2015-10-26 17:52:36 -0700104# Add args from the experimental mappings.
105for experiment in ${EXPERIMENTAL}; do
106 JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
107done
108
Stephen Kyle40d35182014-10-03 13:47:56 +0100109if [ -e classes.dex ]; then
110 zip $TEST_NAME.jar classes.dex
111 exit 0
112fi
113
Alex Light6a439bc2015-10-26 17:52:36 -0700114if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
Igor Murashkin158f35c2015-06-10 15:55:30 -0700115 # No src directory? Then forget about trying to run dx.
116 SKIP_DX_MERGER="true"
117fi
118
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100119if [ ${USE_JACK} = "true" ]; then
120 # Jack toolchain
Alex Light6a439bc2015-10-26 17:52:36 -0700121 if [ "${HAS_SRC}" = "true" ]; then
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100122 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
123 # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
124 # when creating the output .dex file.
125 ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
126 jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
127 jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
128 jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
129 else
130 ${JACK} ${JACK_ARGS} --output-jack src.jack src
131 fi
132 jack_extra_args="${jack_extra_args} --import src.jack"
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100133 fi
134
Alex Light6a439bc2015-10-26 17:52:36 -0700135 if [ "${HAS_SRC2}" = "true" ]; then
136 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100137 # In case of duplicate classes, we want to take into account the classes from src2. Therefore
138 # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
139 jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
140 jack_extra_args="--import src2.jack ${jack_extra_args}"
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100141 fi
142
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100143 # Compile jack files into a DEX file.
Alex Light6a439bc2015-10-26 17:52:36 -0700144 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
Sebastien Hertz4856ca72016-03-03 18:08:17 +0100145 ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
Alex Light6a439bc2015-10-26 17:52:36 -0700146 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100147else
148 # Legacy toolchain with javac+dx
Alex Light6a439bc2015-10-26 17:52:36 -0700149 if [ "${HAS_SRC}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100150 mkdir classes
Alex Light6a439bc2015-10-26 17:52:36 -0700151 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100152 fi
153
Alex Light6a439bc2015-10-26 17:52:36 -0700154 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100155 mkdir classes2
156 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
157 if [ ${NEED_DEX} = "true" ]; then
158 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
159 --dump-width=1000 ${DX_FLAGS} classes2
160 fi
161 fi
162
Alex Light6a439bc2015-10-26 17:52:36 -0700163 if [ "${HAS_SRC2}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100164 mkdir -p classes
Alex Light6a439bc2015-10-26 17:52:36 -0700165 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100166 fi
167
Alex Light6a439bc2015-10-26 17:52:36 -0700168 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
169 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
170 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
171 --dump-width=1000 ${DX_FLAGS} classes
172 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100173 fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100174fi
175
Alex Light6a439bc2015-10-26 17:52:36 -0700176if [ "${HAS_SMALI}" = "true" ]; then
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100177 # Compile Smali classes
Alex Light6a439bc2015-10-26 17:52:36 -0700178 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Igor Murashkin158f35c2015-06-10 15:55:30 -0700179
180 # Don't bother with dexmerger if we provide our own main function in a smali file.
181 if [ ${SKIP_DX_MERGER} = "false" ]; then
182 ${DXMERGER} classes.dex classes.dex smali_classes.dex
183 else
184 mv smali_classes.dex classes.dex
185 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700186fi
TDYa127b92bcab2012-04-08 00:09:51 -0700187
Alex Lightfedd91d2016-01-07 14:49:16 -0800188if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
189 # Compile Smali classes
190 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
191
192 # Don't bother with dexmerger if we provide our own main function in a smali file.
193 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
194 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
195 else
196 mv smali_classes2.dex classes2.dex
197 fi
198fi
199
200
Alex Light6a439bc2015-10-26 17:52:36 -0700201if [ ${HAS_SRC_EX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100202 if [ ${USE_JACK} = "true" ]; then
203 # Rename previous "classes.dex" so it is not overwritten.
204 mv classes.dex classes-1.dex
205 #TODO find another way to append src.jack to the jack classpath
Alex Light6a439bc2015-10-26 17:52:36 -0700206 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100207 zip $TEST_NAME-ex.jar classes.dex
208 # Restore previous "classes.dex" so it can be zipped.
209 mv classes-1.dex classes.dex
210 else
211 mkdir classes-ex
Alex Light6a439bc2015-10-26 17:52:36 -0700212 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100213 if [ ${NEED_DEX} = "true" ]; then
214 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
215 --dump-width=1000 ${DX_FLAGS} classes-ex
jeffhao5d1ac922011-09-29 17:41:15 -0700216
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100217 # quick shuffle so that the stored name is "classes.dex"
218 mv classes.dex classes-1.dex
219 mv classes-ex.dex classes.dex
220 zip $TEST_NAME-ex.jar classes.dex
221 mv classes.dex classes-ex.dex
222 mv classes-1.dex classes.dex
223 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700224 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700225fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100226
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000227# Create a single jar with two dex files for multidex.
Alex Lightfedd91d2016-01-07 14:49:16 -0800228if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100229 zip $TEST_NAME.jar classes.dex classes2.dex
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000230elif [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray12508612014-11-05 12:34:24 +0000231 zip $TEST_NAME.jar classes.dex
232fi