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