blob: 6e855ec30a5d3ed03a92535fbacae38331693b3b [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
Alex Light6a439bc2015-10-26 17:52:36 -0700119if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100120 # Jack does not support this configuration unless we specify how to partition the DEX file
121 # with a .jpp file.
122 USE_JACK="false"
123fi
124
125if [ ${USE_JACK} = "true" ]; then
126 # Jack toolchain
Alex Light6a439bc2015-10-26 17:52:36 -0700127 if [ "${HAS_SRC}" = "true" ]; then
128 ${JACK} ${JACK_ARGS} --output-jack src.jack src
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100129 imported_jack_files="--import src.jack"
130 fi
131
Alex Light6a439bc2015-10-26 17:52:36 -0700132 if [ "${HAS_SRC2}" = "true" ]; then
133 ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100134 imported_jack_files="--import src2.jack ${imported_jack_files}"
135 fi
136
137 # Compile jack files into a DEX file. We set jack.import.type.policy=keep-first to consider
138 # class definitions from src2 first.
Alex Light6a439bc2015-10-26 17:52:36 -0700139 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
140 ${JACK} ${JACK_ARGS} ${imported_jack_files} -D jack.import.type.policy=keep-first --output-dex .
141 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100142else
143 # Legacy toolchain with javac+dx
Alex Light6a439bc2015-10-26 17:52:36 -0700144 if [ "${HAS_SRC}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100145 mkdir classes
Alex Light6a439bc2015-10-26 17:52:36 -0700146 ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100147 fi
148
Alex Light6a439bc2015-10-26 17:52:36 -0700149 if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100150 mkdir classes2
151 ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
152 if [ ${NEED_DEX} = "true" ]; then
153 ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
154 --dump-width=1000 ${DX_FLAGS} classes2
155 fi
156 fi
157
Alex Light6a439bc2015-10-26 17:52:36 -0700158 if [ "${HAS_SRC2}" = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100159 mkdir -p classes
Alex Light6a439bc2015-10-26 17:52:36 -0700160 ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100161 fi
162
Alex Light6a439bc2015-10-26 17:52:36 -0700163 if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
164 if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
165 ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
166 --dump-width=1000 ${DX_FLAGS} classes
167 fi
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100168 fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100169fi
170
Alex Light6a439bc2015-10-26 17:52:36 -0700171if [ "${HAS_SMALI}" = "true" ]; then
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100172 # Compile Smali classes
Alex Light6a439bc2015-10-26 17:52:36 -0700173 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
Igor Murashkin158f35c2015-06-10 15:55:30 -0700174
175 # Don't bother with dexmerger if we provide our own main function in a smali file.
176 if [ ${SKIP_DX_MERGER} = "false" ]; then
177 ${DXMERGER} classes.dex classes.dex smali_classes.dex
178 else
179 mv smali_classes.dex classes.dex
180 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700181fi
TDYa127b92bcab2012-04-08 00:09:51 -0700182
Alex Lightfedd91d2016-01-07 14:49:16 -0800183if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
184 # Compile Smali classes
185 ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
186
187 # Don't bother with dexmerger if we provide our own main function in a smali file.
188 if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
189 ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
190 else
191 mv smali_classes2.dex classes2.dex
192 fi
193fi
194
195
Alex Light6a439bc2015-10-26 17:52:36 -0700196if [ ${HAS_SRC_EX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100197 if [ ${USE_JACK} = "true" ]; then
198 # Rename previous "classes.dex" so it is not overwritten.
199 mv classes.dex classes-1.dex
200 #TODO find another way to append src.jack to the jack classpath
Alex Light6a439bc2015-10-26 17:52:36 -0700201 ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100202 zip $TEST_NAME-ex.jar classes.dex
203 # Restore previous "classes.dex" so it can be zipped.
204 mv classes-1.dex classes.dex
205 else
206 mkdir classes-ex
Alex Light6a439bc2015-10-26 17:52:36 -0700207 ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100208 if [ ${NEED_DEX} = "true" ]; then
209 ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
210 --dump-width=1000 ${DX_FLAGS} classes-ex
jeffhao5d1ac922011-09-29 17:41:15 -0700211
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100212 # quick shuffle so that the stored name is "classes.dex"
213 mv classes.dex classes-1.dex
214 mv classes-ex.dex classes.dex
215 zip $TEST_NAME-ex.jar classes.dex
216 mv classes.dex classes-ex.dex
217 mv classes-1.dex classes.dex
218 fi
Elliott Hughesc717eef2012-06-15 16:01:26 -0700219 fi
jeffhao5d1ac922011-09-29 17:41:15 -0700220fi
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100221
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000222# Create a single jar with two dex files for multidex.
Alex Lightfedd91d2016-01-07 14:49:16 -0800223if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
Sebastien Hertz19ac0272015-02-24 17:39:50 +0100224 zip $TEST_NAME.jar classes.dex classes2.dex
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000225elif [ ${NEED_DEX} = "true" ]; then
Nicolas Geoffray12508612014-11-05 12:34:24 +0000226 zip $TEST_NAME.jar classes.dex
227fi