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# Stop if something fails.
18set -e
19
20function fail() {
21  echo "$*" >&2
22  exit 1
23}
24
25if [[ $# -le 0 ]]; then
26  echo 'Error:' '$0 should have the parameters from the "build" script forwarded to it' >&2
27  fail 'Error: An example of how do it correctly is ./default-build "$@"'
28  exit 1
29fi
30
31# Set default values for directories.
32if [ -d smali ]; then
33  HAS_SMALI=true
34else
35  HAS_SMALI=false
36fi
37
38# .j files in jasmin get compiled into classes.jar
39if [ -d jasmin ]; then
40  HAS_JASMIN=true
41else
42  HAS_JASMIN=false
43fi
44
45if [ -d src ]; then
46  HAS_SRC=true
47else
48  HAS_SRC=false
49fi
50
51# .java files in src-art get compiled with libcore on the bootclasspath
52if [ -d src-art ]; then
53  HAS_SRC_ART=true
54else
55  HAS_SRC_ART=false
56fi
57
58if [ -d src2 ]; then
59  HAS_SRC2=true
60else
61  HAS_SRC2=false
62fi
63
64if [ -d src-multidex ]; then
65  HAS_SRC_MULTIDEX=true
66else
67  HAS_SRC_MULTIDEX=false
68fi
69
70if [ -d smali-multidex ]; then
71  HAS_SMALI_MULTIDEX=true
72else
73  HAS_SMALI_MULTIDEX=false
74fi
75
76# .j files in jasmin-multidex get compiled into classes2.jar
77if [ -d jasmin-multidex ]; then
78  HAS_JASMIN_MULTIDEX=true
79else
80  HAS_JASMIN_MULTIDEX=false
81fi
82
83if [ -d smali-ex ]; then
84  HAS_SMALI_EX=true
85else
86  HAS_SMALI_EX=false
87fi
88
89if [ -d src-ex ]; then
90  HAS_SRC_EX=true
91else
92  HAS_SRC_EX=false
93fi
94
95if [ -d src-dex2oat-unresolved ]; then
96  HAS_SRC_DEX2OAT_UNRESOLVED=true
97else
98  HAS_SRC_DEX2OAT_UNRESOLVED=false
99fi
100
101if [ -f hiddenapi-flags.csv ]; then
102  HAS_HIDDENAPI_SPEC=true
103else
104  HAS_HIDDENAPI_SPEC=false
105fi
106
107# USE_HIDDENAPI=false run-test... will disable hiddenapi.
108if [ -z "${USE_HIDDENAPI}" ]; then
109  USE_HIDDENAPI=true
110fi
111
112# DESUGAR=false run-test... will disable desugaring.
113if [[ "$DESUGAR" == false ]]; then
114  USE_DESUGAR=false
115fi
116
117# Allow overriding ZIP_COMPRESSION_METHOD with e.g. 'store'
118ZIP_COMPRESSION_METHOD="deflate"
119# Align every ZIP file made by calling $ZIPALIGN command?
120WITH_ZIP_ALIGN=false
121ZIP_ALIGN_BYTES="-1"
122
123BUILD_MODE="target"
124DEV_MODE="no"
125
126DEFAULT_EXPERIMENT="no-experiment"
127
128# The key for default arguments if no experimental things are enabled.
129EXPERIMENTAL=$DEFAULT_EXPERIMENT
130
131# Setup experimental API level mappings in a bash associative array.
132declare -A EXPERIMENTAL_API_LEVEL
133EXPERIMENTAL_API_LEVEL[${DEFAULT_EXPERIMENT}]="26"
134EXPERIMENTAL_API_LEVEL["default-methods"]="24"
135EXPERIMENTAL_API_LEVEL["parameter-annotations"]="25"
136EXPERIMENTAL_API_LEVEL["agents"]="26"
137EXPERIMENTAL_API_LEVEL["method-handles"]="26"
138EXPERIMENTAL_API_LEVEL["var-handles"]="28"
139
140while true; do
141  if [ "x$1" = "x--no-src" ]; then
142    HAS_SRC=false
143    shift
144  elif [ "x$1" = "x--no-src2" ]; then
145    HAS_SRC2=false
146    shift
147  elif [ "x$1" = "x--no-src-multidex" ]; then
148    HAS_SRC_MULTIDEX=false
149    shift
150  elif [ "x$1" = "x--no-smali-multidex" ]; then
151    HAS_SMALI_MULTIDEX=false
152    shift
153  elif [ "x$1" = "x--no-src-ex" ]; then
154    HAS_SRC_EX=false
155    shift
156  elif [ "x$1" = "x--no-smali" ]; then
157    HAS_SMALI=false
158    shift
159  elif [ "x$1" = "x--no-jasmin" ]; then
160    HAS_JASMIN=false
161    shift
162  elif [ "x$1" = "x--api-level" ]; then
163    shift
164    EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]=$1
165    shift
166  elif [ "x$1" = "x--experimental" ]; then
167    shift
168    # We have a specific experimental configuration so don't use the default.
169    EXPERIMENTAL="$1"
170    shift
171  elif [ "x$1" = "x--zip-compression-method" ]; then
172    # Allow using different zip compression method, e.g. 'store'
173    shift
174    ZIP_COMPRESSION_METHOD="$1"
175    shift
176  elif [ "x$1" = "x--zip-align" ]; then
177    # Align ZIP entries to some # of bytes.
178    shift
179    WITH_ZIP_ALIGN=true
180    ZIP_ALIGN_BYTES="$1"
181    shift
182  elif [ "x$1" = "x--host" ]; then
183    BUILD_MODE="host"
184    shift
185  elif [ "x$1" = "x--target" ]; then
186    BUILD_MODE="target"
187    shift
188  elif [ "x$1" = "x--jvm" ]; then
189    BUILD_MODE="jvm"
190    shift
191  elif [ "x$1" = "x--dev" ]; then
192    DEV_MODE="yes"
193    shift
194  elif expr "x$1" : "x--" >/dev/null 2>&1; then
195    fail "unknown $0 option: $1"
196  else
197    break
198  fi
199done
200
201if [[ $BUILD_MODE == jvm ]]; then
202  # Does not need desugaring on jvm because it supports the latest functionality.
203  USE_DESUGAR=false
204  # Do not attempt to build src-art directories on jvm, it would fail without libcore.
205  HAS_SRC_ART=false
206fi
207
208# Set API level for smali and d8.
209API_LEVEL="${EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]}"
210
211# Add API level arguments to smali and dx
212SMALI_ARGS="${SMALI_ARGS} --api $API_LEVEL"
213D8_FLAGS="${D8_FLAGS} --min-api $API_LEVEL"
214
215#########################################
216
217# Catch all commands to 'ZIP' and prepend extra flags.
218# Optionally, zipalign results to some alignment.
219function zip() {
220  local zip_target="$1"
221  local entry_src="$2"
222  shift 2
223
224  command zip --compression-method "$ZIP_COMPRESSION_METHOD" "$zip_target" "$entry_src" "$@"
225
226  if "$WITH_ZIP_ALIGN"; then
227    # zipalign does not operate in-place, so write results to a temp file.
228    local tmp_file="$(mktemp)"
229    "$ZIPALIGN" -f "$ZIP_ALIGN_BYTES" "$zip_target" "$tmp_file"
230    # replace original zip target with our temp file.
231    mv "$tmp_file" "$zip_target"
232  fi
233}
234
235function make_jasmin() {
236  local out_directory="$1"
237  shift
238  local jasmin_sources=("$@")
239
240  mkdir -p "$out_directory"
241
242  if [[ $DEV_MODE == yes ]]; then
243    echo ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}"
244    ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}"
245  else
246    ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}" >/dev/null
247  fi
248}
249
250# Like regular javac but may include libcore on the bootclasspath.
251function javac_with_bootclasspath {
252  local helper_args="--mode=$BUILD_MODE"
253
254  if [[ $DEV_MODE == yes ]]; then
255    helper_args="$helper_args --show-commands"
256  fi
257
258  # build with libcore for host and target, or openjdk for jvm
259  "$ANDROID_BUILD_TOP/art/tools/javac-helper.sh" --core-only $helper_args ${JAVAC_ARGS} "$@"
260}
261
262# Make a "dex" file given a directory of classes in $1. This will be
263# packaged in a jar file.
264function make_dex() {
265  local name="$1"
266  local d8_inputs=$(find $name -name '*.class' -type f)
267  local d8_output=${name}.jar
268  local dex_output=${name}.dex
269  local d8_local_flags=""
270  if [[ "$USE_DESUGAR" = "true" ]]; then
271    local boot_class_path_list=$($ANDROID_BUILD_TOP/art/tools/bootjars.sh --$BUILD_MODE --core --path)
272    for boot_class_path_element in $boot_class_path_list; do
273      d8_local_flags="$d8_local_flags --classpath $boot_class_path_element"
274    done
275  else
276    d8_local_flags="$d8_local_flags --no-desugaring"
277  fi
278  if [ "$DEV_MODE" = "yes" ]; then
279    echo ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
280  fi
281  ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
282
283  # D8 outputs to JAR files today rather than DEX files as DX used
284  # to. To compensate, we extract the DEX from d8's output to meet the
285  # expectations of make_dex callers.
286  if [ "$DEV_MODE" = "yes" ]; then
287    echo unzip -p $d8_output classes.dex \> $dex_output
288  fi
289  unzip -p $d8_output classes.dex > $dex_output
290}
291
292# Merge all the dex files in $1..$N into $1. Skip non-existing files, but at least 1 file must exist.
293function make_dexmerge() {
294  # Dex file that acts as the destination.
295  local dst_file="$1"
296
297  # Dex files that act as the source.
298  local dex_files_to_merge=()
299
300  # Skip any non-existing files.
301  while [[ $# -gt 0 ]]; do
302    if [[ -e "$1" ]]; then
303      dex_files_to_merge+=("$1")
304    fi
305    shift
306  done
307
308  # Skip merge if we are not merging anything. IE: input = output.
309  if [[ "${#dex_files_to_merge[@]}" -eq "1" ]]; then
310    local single_input=${dex_files_to_merge[0]}
311    if [[ "$dst_file" != "$single_input" ]]; then
312     mv "$single_input" "$dst_file";
313     return
314    fi
315  fi
316
317  # We assume the dexer did all the API level checks and just merge away.
318  mkdir d8_merge_out
319  ${DEXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}"
320
321  if [[ -e "./d8_merge_out/classes2.dex" ]]; then
322    fail "Cannot merge all dex files into a single dex"
323  fi
324
325  mv ./d8_merge_out/classes.dex "$dst_file";
326  rmdir d8_merge_out
327}
328
329function make_hiddenapi() {
330  local args=( "encode" )
331  while [[ $# -gt 0 ]]; do
332    args+=("--input-dex=$1")
333    args+=("--output-dex=$1")
334    shift
335  done
336  args+=("--api-flags=hiddenapi-flags.csv")
337  args+=("--no-force-assign-all")
338  ${HIDDENAPI} "${args[@]}"
339}
340
341# Print the directory name only if it exists.
342function maybe_dir() {
343  local dirname="$1"
344  if [[ -d "$dirname" ]]; then
345    echo "$dirname"
346  fi
347}
348
349if [ -e classes.dex ]; then
350  zip $TEST_NAME.jar classes.dex
351  exit 0
352fi
353
354# Helper function for a common test. Evaluate with $(has_mutlidex).
355function has_multidex() {
356  echo [ ${HAS_SRC_MULTIDEX} = "true" \
357         -o ${HAS_JASMIN_MULTIDEX} = "true" \
358         -o ${HAS_SMALI_MULTIDEX} = "true" ]
359}
360
361if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
362  mkdir -p classes
363  mkdir classes-ex
364  javac_with_bootclasspath -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
365  javac_with_bootclasspath -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
366  if [ ${NEED_DEX} = "true" ]; then
367    make_dex classes-ex
368    mv classes-ex.dex classes.dex   # rename it so it shows up as "classes.dex" in the zip file.
369    zip ${TEST_NAME}-ex.jar classes.dex
370    make_dex classes
371  fi
372else
373  if [ "${HAS_SRC}" = "true" ]; then
374    mkdir -p classes
375    javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
376  fi
377
378  if [ "${HAS_SRC_ART}" = "true" ]; then
379    mkdir -p classes
380    javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src-art -name '*.java'`
381  fi
382
383  if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
384    mkdir classes2
385    javac_with_bootclasspath -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
386    if [ ${NEED_DEX} = "true" ]; then
387      make_dex classes2
388    fi
389  fi
390
391  if [ "${HAS_SRC2}" = "true" ]; then
392    mkdir -p classes
393    javac_with_bootclasspath -classpath classes -d classes `find src2 -name '*.java'`
394  fi
395
396  # If the classes directory is not-empty, package classes in a DEX file. NB some
397  # tests provide classes rather than java files.
398  if [ "$(ls -A classes)" ]; then
399    if [ ${NEED_DEX} = "true" ]; then
400      make_dex classes
401    fi
402  fi
403fi
404
405if [[ "${HAS_JASMIN}" == true ]]; then
406  # Compile Jasmin classes as if they were part of the classes.dex file.
407  make_jasmin jasmin_classes $(find 'jasmin' -name '*.j')
408  if [[ "${NEED_DEX}" == "true" ]]; then
409    make_dex jasmin_classes
410    make_dexmerge classes.dex jasmin_classes.dex
411  else
412    # Move jasmin classes into classes directory so that they are picked up with -cp classes.
413    mkdir -p classes
414    mv jasmin_classes/* classes
415  fi
416fi
417
418if [ "${HAS_SMALI}" = "true" -a ${NEED_DEX} = "true" ]; then
419  # Compile Smali classes
420  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`
421  if [[ ! -s smali_classes.dex ]] ; then
422    fail "${SMALI} produced no output."
423  fi
424  # Merge smali files into classes.dex, this takes priority over any jasmin files.
425  make_dexmerge classes.dex smali_classes.dex
426fi
427
428# Compile Jasmin classes in jasmin-multidex as if they were part of the classes2.jar
429if [[ "$HAS_JASMIN_MULTIDEX" == true ]]; then
430  make_jasmin jasmin_classes2 $(find 'jasmin-multidex' -name '*.j')
431
432  if [[ "${NEED_DEX}" == "true" ]]; then
433    make_dex jasmin_classes2
434    make_dexmerge classes2.dex jasmin_classes2.dex
435  else
436    # Move jasmin classes into classes2 directory so that they are picked up with -cp classes2.
437    mkdir -p classes2
438    mv jasmin_classes2/* classes2
439  fi
440fi
441
442if [ "${HAS_SMALI_MULTIDEX}" = "true" -a ${NEED_DEX} = "true" ]; then
443  # Compile Smali classes
444  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`
445
446  # Merge smali_classes2.dex into classes2.dex
447  make_dexmerge classes2.dex smali_classes2.dex
448fi
449
450if [ ${HAS_SRC_EX} = "true" ]; then
451  # Build src-ex into classes-ex.
452  # Includes 'src', 'src-art' source when compiling classes-ex, but exclude their .class files.
453  if [[ "${HAS_SRC}" == "true" ]]; then
454    mkdir -p classes-tmp-for-ex
455    javac_with_bootclasspath -d classes-tmp-for-ex `find src -name '*.java'`
456    src_tmp_for_ex="-cp classes-tmp-for-ex"
457  fi
458  if [[ "${HAS_SRC_ART}" == "true" ]]; then
459    mkdir -p classes-tmp-for-ex
460    javac_with_bootclasspath -d classes-tmp-for-ex `find src-art -name '*.java'`
461    src_tmp_for_ex="-cp classes-tmp-for-ex"
462  fi
463  mkdir -p classes-ex
464  javac_with_bootclasspath -d classes-ex $src_tmp_for_ex `find src-ex -name '*.java'`
465fi
466
467if [[ -d classes-ex ]] && [ ${NEED_DEX} = "true" ]; then
468  make_dex classes-ex
469fi
470
471if [ "${HAS_SMALI_EX}" = "true" -a ${NEED_DEX} = "true" ]; then
472  # Compile Smali classes
473  ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes-ex.dex `find smali-ex -name '*.smali'`
474  if [[ ! -s smali_classes-ex.dex ]] ; then
475    fail "${SMALI} produced no output."
476  fi
477  # Merge smali files into classes-ex.dex.
478  make_dexmerge classes-ex.dex smali_classes-ex.dex
479fi
480
481if [[ -f classes-ex.dex ]]; then
482  # Apply hiddenapi on the dex files if the test has API list file(s).
483  if [ ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then
484    make_hiddenapi classes-ex.dex
485  fi
486
487  # quick shuffle so that the stored name is "classes.dex"
488  mv classes.dex classes-1.dex
489  mv classes-ex.dex classes.dex
490  zip $TEST_NAME-ex.jar classes.dex
491  mv classes.dex classes-ex.dex
492  mv classes-1.dex classes.dex
493fi
494
495# Apply hiddenapi on the dex files if the test has API list file(s).
496if [ ${NEED_DEX} = "true" -a ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then
497  if $(has_multidex); then
498    make_hiddenapi classes.dex classes2.dex
499  else
500    make_hiddenapi classes.dex
501  fi
502fi
503
504# Create a single dex jar with two dex files for multidex.
505if [ ${NEED_DEX} = "true" ]; then
506  if [ -f classes2.dex ] ; then
507    zip $TEST_NAME.jar classes.dex classes2.dex
508  else
509    zip $TEST_NAME.jar classes.dex
510  fi
511fi
512