| #!/bin/bash |
| # |
| # Copyright (C) 2008 The Android Open Source Project |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # Stop if something fails. |
| set -e |
| |
| if [[ $# -le 0 ]]; then |
| echo 'Error:' '$0 should have the parameters from the "build" script forwarded to it' >&2 |
| echo 'Error: An example of how do it correctly is ./default-build "$@"' |
| exit 1 |
| fi |
| |
| # Set default values for directories. |
| if [ -d smali ]; then |
| HAS_SMALI=true |
| else |
| HAS_SMALI=false |
| fi |
| |
| # .j files in jasmin get compiled into classes.jar |
| if [ -d jasmin ]; then |
| HAS_JASMIN=true |
| else |
| HAS_JASMIN=false |
| fi |
| |
| if [ -d src ]; then |
| HAS_SRC=true |
| else |
| HAS_SRC=false |
| fi |
| |
| # .java files in src-art get compiled with libcore on the bootclasspath |
| if [ -d src-art ]; then |
| HAS_SRC_ART=true |
| else |
| HAS_SRC_ART=false |
| fi |
| |
| if [ -d src2 ]; then |
| HAS_SRC2=true |
| else |
| HAS_SRC2=false |
| fi |
| |
| if [ -d src-multidex ]; then |
| HAS_SRC_MULTIDEX=true |
| else |
| HAS_SRC_MULTIDEX=false |
| fi |
| |
| if [ -d smali-multidex ]; then |
| HAS_SMALI_MULTIDEX=true |
| else |
| HAS_SMALI_MULTIDEX=false |
| fi |
| |
| # .j files in jasmin-multidex get compiled into classes2.jar |
| if [ -d jasmin-multidex ]; then |
| HAS_JASMIN_MULTIDEX=true |
| else |
| HAS_JASMIN_MULTIDEX=false |
| fi |
| |
| if [ -d src-ex ]; then |
| HAS_SRC_EX=true |
| else |
| HAS_SRC_EX=false |
| fi |
| |
| if [ -d src-dex2oat-unresolved ]; then |
| HAS_SRC_DEX2OAT_UNRESOLVED=true |
| else |
| HAS_SRC_DEX2OAT_UNRESOLVED=false |
| fi |
| |
| if [ -f api-light-greylist.txt -o -f api-dark-greylist.txt -o -f api-blacklist.txt ]; then |
| HAS_HIDDENAPI_SPEC=true |
| else |
| HAS_HIDDENAPI_SPEC=false |
| fi |
| |
| # USE_HIDDENAPI=false run-test... will disable hiddenapi. |
| if [ -z "${USE_HIDDENAPI}" ]; then |
| USE_HIDDENAPI=true |
| fi |
| |
| # DESUGAR=false run-test... will disable desugar. |
| if [[ "$DESUGAR" == false ]]; then |
| USE_DESUGAR=false |
| fi |
| |
| # Allow overriding ZIP_COMPRESSION_METHOD with e.g. 'store' |
| ZIP_COMPRESSION_METHOD="deflate" |
| # Align every ZIP file made by calling $ZIPALIGN command? |
| WITH_ZIP_ALIGN=false |
| ZIP_ALIGN_BYTES="-1" |
| |
| DX_FLAGS="--min-sdk-version=26" |
| DX_VM_FLAGS="" |
| EXPERIMENTAL="" |
| |
| BUILD_MODE="target" |
| DEV_MODE="no" |
| |
| # The key for default arguments if no experimental things are enabled. |
| DEFAULT_EXPERIMENT="no-experiment" |
| |
| # Setup experimental flag mappings in a bash associative array. |
| declare -A SMALI_EXPERIMENTAL_ARGS |
| SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api 24" |
| SMALI_EXPERIMENTAL_ARGS["method-handles"]="--api 26" |
| SMALI_EXPERIMENTAL_ARGS["var-handles"]="--api 26" |
| SMALI_EXPERIMENTAL_ARGS["agents"]="--api 26" |
| |
| declare -A JAVAC_EXPERIMENTAL_ARGS |
| JAVAC_EXPERIMENTAL_ARGS["default-methods"]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS["lambdas"]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS["method-handles"]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS["parameter-annotations"]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS["var-handles"]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS[${DEFAULT_EXPERIMENT}]="-source 1.8 -target 1.8" |
| JAVAC_EXPERIMENTAL_ARGS["agents"]="-source 1.8 -target 1.8" |
| |
| declare -A DX_EXPERIMENTAL_ARGS |
| DX_EXPERIMENTAL_ARGS["method-handles"]="--min-sdk-version=26" |
| DX_EXPERIMENTAL_ARGS["parameter-annotations"]="--min-sdk-version=25" |
| DX_EXPERIMENTAL_ARGS["var-handles"]="--min-sdk-version=28" |
| |
| while true; do |
| if [ "x$1" = "x--dx-option" ]; then |
| shift |
| option="$1" |
| DX_FLAGS="${DX_FLAGS} $option" |
| shift |
| elif [ "x$1" = "x--dx-vm-option" ]; then |
| shift |
| option="$1" |
| DX_VM_FLAGS="${DX_VM_FLAGS} $option" |
| shift |
| elif [ "x$1" = "x--no-src" ]; then |
| HAS_SRC=false |
| shift |
| elif [ "x$1" = "x--no-src2" ]; then |
| HAS_SRC2=false |
| shift |
| elif [ "x$1" = "x--no-src-multidex" ]; then |
| HAS_SRC_MULTIDEX=false |
| shift |
| elif [ "x$1" = "x--no-smali-multidex" ]; then |
| HAS_SMALI_MULTIDEX=false |
| shift |
| elif [ "x$1" = "x--no-src-ex" ]; then |
| HAS_SRC_EX=false |
| shift |
| elif [ "x$1" = "x--no-smali" ]; then |
| HAS_SMALI=false |
| shift |
| elif [ "x$1" = "x--no-jasmin" ]; then |
| HAS_JASMIN=false |
| shift |
| elif [ "x$1" = "x--experimental" ]; then |
| shift |
| # We have a specific experimental configuration so don't use the default. |
| DEFAULT_EXPERIMENT="" |
| EXPERIMENTAL="${EXPERIMENTAL} $1" |
| shift |
| elif [ "x$1" = "x--zip-compression-method" ]; then |
| # Allow using different zip compression method, e.g. 'store' |
| shift |
| ZIP_COMPRESSION_METHOD="$1" |
| shift |
| elif [ "x$1" = "x--zip-align" ]; then |
| # Align ZIP entries to some # of bytes. |
| shift |
| WITH_ZIP_ALIGN=true |
| ZIP_ALIGN_BYTES="$1" |
| shift |
| elif [ "x$1" = "x--host" ]; then |
| BUILD_MODE="host" |
| shift |
| elif [ "x$1" = "x--target" ]; then |
| BUILD_MODE="target" |
| shift |
| elif [ "x$1" = "x--jvm" ]; then |
| BUILD_MODE="jvm" |
| shift |
| elif [ "x$1" = "x--dev" ]; then |
| DEV_MODE="yes" |
| shift |
| elif expr "x$1" : "x--" >/dev/null 2>&1; then |
| echo "unknown $0 option: $1" 1>&2 |
| exit 1 |
| else |
| break |
| fi |
| done |
| |
| if [[ $BUILD_MODE == jvm ]]; then |
| # Does not need desugar on jvm because it supports the latest functionality. |
| USE_DESUGAR=false |
| # Do not attempt to build src-art directories on jvm, it would fail without libcore. |
| HAS_SRC_ART=false |
| fi |
| |
| # Be sure to get any default arguments if not doing any experiments. |
| EXPERIMENTAL="${EXPERIMENTAL} ${DEFAULT_EXPERIMENT}" |
| |
| # Add args from the experimental mappings. |
| for experiment in ${EXPERIMENTAL}; do |
| SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}" |
| JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}" |
| DX_FLAGS="${DX_FLAGS} ${DX_EXPERIMENTAL_ARGS[${experiment}]}" |
| done |
| |
| ######################################### |
| |
| # Catch all commands to 'ZIP' and prepend extra flags. |
| # Optionally, zipalign results to some alignment. |
| function zip() { |
| local zip_target="$1" |
| local entry_src="$2" |
| shift 2 |
| |
| command zip --compression-method "$ZIP_COMPRESSION_METHOD" "$zip_target" "$entry_src" "$@" |
| |
| if "$WITH_ZIP_ALIGN"; then |
| # zipalign does not operate in-place, so write results to a temp file. |
| local tmp_file="$(mktemp)" |
| "$ZIPALIGN" -f "$ZIP_ALIGN_BYTES" "$zip_target" "$tmp_file" |
| # replace original zip target with our temp file. |
| mv "$tmp_file" "$zip_target" |
| fi |
| } |
| |
| function make_jasmin() { |
| local out_directory="$1" |
| shift |
| local jasmin_sources=("$@") |
| |
| mkdir -p "$out_directory" |
| |
| if [[ $DEV_MODE == yes ]]; then |
| echo ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}" |
| ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}" |
| else |
| ${JASMIN} -d "$out_directory" "${jasmin_sources[@]}" >/dev/null |
| fi |
| } |
| |
| function desugar() { |
| local desugar_args="--mode=$BUILD_MODE" |
| |
| if [[ $DEV_MODE == yes ]]; then |
| desugar_args="$desugar_args --show-commands" |
| fi |
| |
| "$DESUGAR" --core-only $desugar_args "$@" |
| } |
| |
| # Like regular javac but may include libcore on the bootclasspath. |
| function javac_with_bootclasspath { |
| local helper_args="--mode=$BUILD_MODE" |
| |
| if [[ $DEV_MODE == yes ]]; then |
| helper_args="$helper_args --show-commands" |
| fi |
| |
| # build with libcore for host and target, or openjdk for jvm |
| "$ANDROID_BUILD_TOP/art/tools/javac-helper.sh" --core-only $helper_args ${JAVAC_ARGS} "$@" |
| } |
| |
| # Make a "dex" file given a directory of classes in $1. |
| # Also calls desugar on the classes first to convert lambdas. |
| function make_dex() { |
| local name="$1" |
| |
| local dx_input |
| if [[ "$USE_DESUGAR" == "true" ]]; then |
| # Make a jar first so desugar doesn't need every .class file individually. |
| jar cf "$name.before-desugar.jar" -C "$name" . |
| |
| dx_input="${name}.desugar.jar" |
| |
| # Make desugared JAR. |
| desugar --input "$name.before-desugar.jar" --output "$dx_input" |
| else |
| dx_input="${name}" |
| fi |
| |
| local dexer="${DX}" |
| if [[ "${USE_D8}" != "false" ]]; then |
| dexer="${ANDROID_HOST_OUT}/bin/d8-compat-dx" |
| fi |
| |
| # Make dex file from desugared JAR. |
| ${dexer} -JXmx256m ${DX_VM_FLAGS} --debug --dex --output=${name}.dex ${DX_FLAGS} "${dx_input}" |
| } |
| |
| # Merge all the dex files in $1..$N into $1. Skip non-existing files, but at least 1 file must exist. |
| function make_dexmerge() { |
| # Dex file that acts as the destination. |
| local dst_file="$1" |
| |
| # Dex files that act as the source. |
| local dex_files_to_merge=() |
| |
| # Skip any non-existing files. |
| while [[ $# -gt 0 ]]; do |
| if [[ -e "$1" ]]; then |
| dex_files_to_merge+=("$1") |
| fi |
| shift |
| done |
| |
| # Skip merge if we are not merging anything. IE: input = output. |
| if [[ "${#dex_files_to_merge[@]}" -eq "1" ]]; then |
| local single_input=${dex_files_to_merge[0]} |
| if [[ "$dst_file" != "$single_input" ]]; then |
| mv "$single_input" "$dst_file"; |
| return |
| fi |
| fi |
| |
| # We assume the dexer did all the API level checks and just merge away. |
| mkdir d8_merge_out |
| ${DXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}" |
| |
| if [[ -e "./d8_merge_out/classes2.dex" ]]; then |
| echo "Cannot merge all dex files into a single dex" |
| exit 1 |
| fi |
| |
| mv ./d8_merge_out/classes.dex "$dst_file"; |
| rmdir d8_merge_out |
| } |
| |
| function make_hiddenapi() { |
| local args=() |
| while [[ $# -gt 0 ]]; do |
| args+=("--dex=$1") |
| shift |
| done |
| if [ -f api-light-greylist.txt ]; then |
| args+=("--light-greylist=api-light-greylist.txt") |
| fi |
| if [ -f api-dark-greylist.txt ]; then |
| args+=("--dark-greylist=api-dark-greylist.txt") |
| fi |
| if [ -f api-blacklist.txt ]; then |
| args+=("--blacklist=api-blacklist.txt") |
| fi |
| ${HIDDENAPI} "${args[@]}" |
| } |
| |
| # Print the directory name only if it exists. |
| function maybe_dir() { |
| local dirname="$1" |
| if [[ -d "$dirname" ]]; then |
| echo "$dirname" |
| fi |
| } |
| |
| if [ -e classes.dex ]; then |
| zip $TEST_NAME.jar classes.dex |
| exit 0 |
| fi |
| |
| # Helper function for a common test. Evaluate with $(has_mutlidex). |
| function has_multidex() { |
| echo [ ${HAS_SRC_MULTIDEX} = "true" \ |
| -o ${HAS_JASMIN_MULTIDEX} = "true" \ |
| -o ${HAS_SMALI_MULTIDEX} = "true" ] |
| } |
| |
| if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then |
| mkdir -p classes |
| mkdir classes-ex |
| javac_with_bootclasspath -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'` |
| javac_with_bootclasspath -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'` |
| if [ ${NEED_DEX} = "true" ]; then |
| make_dex classes-ex |
| mv classes-ex.dex classes.dex # rename it so it shows up as "classes.dex" in the zip file. |
| zip ${TEST_NAME}-ex.jar classes.dex |
| make_dex classes |
| fi |
| else |
| if [ "${HAS_SRC}" = "true" ]; then |
| mkdir -p classes |
| javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src -name '*.java'` |
| fi |
| |
| if [ "${HAS_SRC_ART}" = "true" ]; then |
| mkdir -p classes |
| javac_with_bootclasspath -implicit:none -classpath src-multidex -d classes `find src-art -name '*.java'` |
| fi |
| |
| if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then |
| mkdir classes2 |
| javac_with_bootclasspath -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'` |
| if [ ${NEED_DEX} = "true" ]; then |
| make_dex classes2 |
| fi |
| fi |
| |
| if [ "${HAS_SRC2}" = "true" ]; then |
| mkdir -p classes |
| javac_with_bootclasspath -classpath classes -d classes `find src2 -name '*.java'` |
| fi |
| |
| if [[ "${HAS_SRC}" == "true" || "${HAS_SRC2}" == "true" || "${HAS_SRC_ART}" == "true" ]]; then |
| if [ ${NEED_DEX} = "true" ]; then |
| make_dex classes |
| fi |
| fi |
| fi |
| |
| if [[ "${HAS_JASMIN}" == true ]]; then |
| # Compile Jasmin classes as if they were part of the classes.dex file. |
| make_jasmin jasmin_classes $(find 'jasmin' -name '*.j') |
| if [[ "${NEED_DEX}" == "true" ]]; then |
| # Disable desugar because it won't handle intentional linkage errors. |
| USE_DESUGAR=false make_dex jasmin_classes |
| make_dexmerge classes.dex jasmin_classes.dex |
| else |
| # Move jasmin classes into classes directory so that they are picked up with -cp classes. |
| mkdir -p classes |
| mv jasmin_classes/* classes |
| fi |
| fi |
| |
| if [ "${HAS_SMALI}" = "true" -a ${NEED_DEX} = "true" ]; then |
| # Compile Smali classes |
| ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'` |
| if [[ ! -s smali_classes.dex ]] ; then |
| echo ${SMALI} produced no output. >&2 |
| exit 1 |
| fi |
| # Merge smali files into classes.dex, this takes priority over any jasmin files. |
| make_dexmerge classes.dex smali_classes.dex |
| fi |
| |
| # Compile Jasmin classes in jasmin-multidex as if they were part of the classes2.jar |
| if [[ "$HAS_JASMIN_MULTIDEX" == true ]]; then |
| make_jasmin jasmin_classes2 $(find 'jasmin-multidex' -name '*.j') |
| |
| if [[ "${NEED_DEX}" == "true" ]]; then |
| # Disable desugar because it won't handle intentional linkage errors. |
| USE_DESUGAR=false make_dex jasmin_classes2 |
| |
| # Merge jasmin_classes2.dex into classes2.dex |
| make_dexmerge classes2.dex jasmin_classes2.dex |
| else |
| # Move jasmin classes into classes2 directory so that they are picked up with -cp classes2. |
| mkdir -p classes2 |
| mv jasmin_classes2/* classes2 |
| fi |
| fi |
| |
| if [ "${HAS_SMALI_MULTIDEX}" = "true" -a ${NEED_DEX} = "true" ]; then |
| # Compile Smali classes |
| ${SMALI} -JXmx512m assemble ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'` |
| |
| # Merge smali_classes2.dex into classes2.dex |
| make_dexmerge classes2.dex smali_classes2.dex |
| fi |
| |
| |
| if [ ${HAS_SRC_EX} = "true" ]; then |
| # Build src-ex into classes-ex. |
| # Includes 'src', 'src-art' source when compiling classes-ex, but exclude their .class files. |
| if [[ "${HAS_SRC}" == "true" ]]; then |
| mkdir -p classes-tmp-for-ex |
| javac_with_bootclasspath -d classes-tmp-for-ex `find src -name '*.java'` |
| src_tmp_for_ex="-cp classes-tmp-for-ex" |
| fi |
| if [[ "${HAS_SRC_ART}" == "true" ]]; then |
| mkdir -p classes-tmp-for-ex |
| javac_with_bootclasspath -d classes-tmp-for-ex `find src-art -name '*.java'` |
| src_tmp_for_ex="-cp classes-tmp-for-ex" |
| fi |
| mkdir classes-ex |
| javac_with_bootclasspath -d classes-ex $src_tmp_for_ex `find src-ex -name '*.java'` |
| fi |
| |
| if [[ -d classes-ex ]] && [ ${NEED_DEX} = "true" ]; then |
| make_dex classes-ex |
| |
| # Apply hiddenapi on the dex files if the test has API list file(s). |
| if [ ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then |
| make_hiddenapi classes-ex.dex |
| fi |
| |
| # quick shuffle so that the stored name is "classes.dex" |
| mv classes.dex classes-1.dex |
| mv classes-ex.dex classes.dex |
| zip $TEST_NAME-ex.jar classes.dex |
| mv classes.dex classes-ex.dex |
| mv classes-1.dex classes.dex |
| fi |
| |
| # Apply hiddenapi on the dex files if the test has API list file(s). |
| if [ ${NEED_DEX} = "true" -a ${USE_HIDDENAPI} = "true" -a ${HAS_HIDDENAPI_SPEC} = "true" ]; then |
| if $(has_multidex); then |
| make_hiddenapi classes.dex classes2.dex |
| else |
| make_hiddenapi classes.dex |
| fi |
| fi |
| |
| # Create a single dex jar with two dex files for multidex. |
| if [ ${NEED_DEX} = "true" ]; then |
| if $(has_multidex); then |
| zip $TEST_NAME.jar classes.dex classes2.dex |
| else |
| zip $TEST_NAME.jar classes.dex |
| fi |
| fi |