1#!/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
18# This is an almost exact copy of `art/test/etc/default-build`. Only the parsing
19# of `dx` option has been overriden.
20
21# Stop if something fails.
22set -e
23
24# Set default values for directories.
25if [ -d smali ]; then
26  HAS_SMALI=true
27else
28  HAS_SMALI=false
29fi
30
31if [ -d src ]; then
32  HAS_SRC=true
33else
34  HAS_SRC=false
35fi
36
37if [ -d src2 ]; then
38  HAS_SRC2=true
39else
40  HAS_SRC2=false
41fi
42
43if [ -d src-multidex ]; then
44  HAS_SRC_MULTIDEX=true
45else
46  HAS_SRC_MULTIDEX=false
47fi
48
49if [ -d src-ex ]; then
50  HAS_SRC_EX=true
51else
52  HAS_SRC_EX=false
53fi
54
55DX_FLAGS=""
56SKIP_DX_MERGER="false"
57EXPERIMENTAL=""
58
59# Setup experimental flag mappings in a bash associative array.
60declare -A JACK_EXPERIMENTAL_ARGS
61JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
62JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
63
64while true; do
65  if [ "x$1" = "x--dx-option" ]; then
66    shift
67    option="$1"
68    # Make sure we run this test *with* `dx` optimizations.
69    if [ "x$option" != "x--no-optimize" ]; then
70      DX_FLAGS="${DX_FLAGS} $option"
71    fi
72    shift
73  elif [ "x$1" = "x--jvm" ]; then
74    shift
75  elif [ "x$1" = "x--no-src" ]; then
76    HAS_SRC=false
77    shift
78  elif [ "x$1" = "x--no-src2" ]; then
79    HAS_SRC2=false
80    shift
81  elif [ "x$1" = "x--no-src-multidex" ]; then
82    HAS_SRC_MULTIDEX=false
83    shift
84  elif [ "x$1" = "x--no-src-ex" ]; then
85    HAS_SRC_EX=false
86    shift
87  elif [ "x$1" = "x--no-smali" ]; then
88    HAS_SMALI=false
89    shift
90  elif [ "x$1" = "x--experimental" ]; then
91    shift
92    EXPERIMENTAL="${EXPERIMENTAL} $1"
93    shift
94  elif expr "x$1" : "x--" >/dev/null 2>&1; then
95    echo "unknown $0 option: $1" 1>&2
96    exit 1
97  else
98    break
99  fi
100done
101
102# Add args from the experimental mappings.
103for experiment in ${EXPERIMENTAL}; do
104  JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
105done
106
107if [ -e classes.dex ]; then
108  zip $TEST_NAME.jar classes.dex
109  exit 0
110fi
111
112if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
113  # No src directory? Then forget about trying to run dx.
114  SKIP_DX_MERGER="true"
115fi
116
117if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
118  # Jack does not support this configuration unless we specify how to partition the DEX file
119  # with a .jpp file.
120  USE_JACK="false"
121fi
122
123if [ ${USE_JACK} = "true" ]; then
124  # Jack toolchain
125  if [ "${HAS_SRC}" = "true" ]; then
126    ${JACK} ${JACK_ARGS} --output-jack src.jack src
127    imported_jack_files="--import src.jack"
128  fi
129
130  if [ "${HAS_SRC2}" = "true" ]; then
131    ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
132    imported_jack_files="--import src2.jack ${imported_jack_files}"
133  fi
134
135  # Compile jack files into a DEX file. We set jack.import.type.policy=keep-first to consider
136  # class definitions from src2 first.
137  if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
138    ${JACK} ${JACK_ARGS} ${imported_jack_files} -D jack.import.type.policy=keep-first --output-dex .
139  fi
140else
141  # Legacy toolchain with javac+dx
142  if [ "${HAS_SRC}" = "true" ]; then
143    mkdir classes
144    ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
145  fi
146
147  if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
148    mkdir classes2
149    ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
150    if [ ${NEED_DEX} = "true" ]; then
151      ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
152        --dump-width=1000 ${DX_FLAGS} classes2
153    fi
154  fi
155
156  if [ "${HAS_SRC2}" = "true" ]; then
157    mkdir -p classes
158    ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
159  fi
160
161  if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
162    if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
163      ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
164        --dump-width=1000 ${DX_FLAGS} classes
165    fi
166  fi
167fi
168
169if [ "${HAS_SMALI}" = "true" ]; then
170  # Compile Smali classes
171  ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
172
173  # Don't bother with dexmerger if we provide our own main function in a smali file.
174  if [ ${SKIP_DX_MERGER} = "false" ]; then
175    ${DXMERGER} classes.dex classes.dex smali_classes.dex
176  else
177    mv smali_classes.dex classes.dex
178  fi
179fi
180
181if [ ${HAS_SRC_EX} = "true" ]; then
182  if [ ${USE_JACK} = "true" ]; then
183      # Rename previous "classes.dex" so it is not overwritten.
184      mv classes.dex classes-1.dex
185      #TODO find another way to append src.jack to the jack classpath
186      ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
187      zip $TEST_NAME-ex.jar classes.dex
188      # Restore previous "classes.dex" so it can be zipped.
189      mv classes-1.dex classes.dex
190  else
191    mkdir classes-ex
192    ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
193    if [ ${NEED_DEX} = "true" ]; then
194      ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
195        --dump-width=1000 ${DX_FLAGS} classes-ex
196
197      # quick shuffle so that the stored name is "classes.dex"
198      mv classes.dex classes-1.dex
199      mv classes-ex.dex classes.dex
200      zip $TEST_NAME-ex.jar classes.dex
201      mv classes.dex classes-ex.dex
202      mv classes-1.dex classes.dex
203    fi
204  fi
205fi
206
207# Create a single jar with two dex files for multidex.
208if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
209  zip $TEST_NAME.jar classes.dex classes2.dex
210elif [ ${NEED_DEX} = "true" ]; then
211  zip $TEST_NAME.jar classes.dex
212fi
213