#!/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

function fail() {
  echo "$*" >&2
  exit 1
}

if [[ $# -le 0 ]]; then
  echo 'Error:' '$0 should have the parameters from the "build" script forwarded to it' >&2
  fail '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 desugaring.
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"

BUILD_MODE="target"
DEV_MODE="no"

DEFAULT_EXPERIMENT="no-experiment"

# The key for default arguments if no experimental things are enabled.
EXPERIMENTAL=$DEFAULT_EXPERIMENT

# Setup experimental API level mappings in a bash associative array.
declare -A EXPERIMENTAL_API_LEVEL
EXPERIMENTAL_API_LEVEL[${DEFAULT_EXPERIMENT}]="26"
EXPERIMENTAL_API_LEVEL["default-methods"]="24"
EXPERIMENTAL_API_LEVEL["parameter-annotations"]="25"
EXPERIMENTAL_API_LEVEL["agents"]="26"
EXPERIMENTAL_API_LEVEL["method-handles"]="26"
EXPERIMENTAL_API_LEVEL["var-handles"]="28"

while true; do
  if [ "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--api-level" ]; then
    shift
    EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]=$1
    shift
  elif [ "x$1" = "x--experimental" ]; then
    shift
    # We have a specific experimental configuration so don't use the default.
    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
    fail "unknown $0 option: $1"
  else
    break
  fi
done

if [[ $BUILD_MODE == jvm ]]; then
  # Does not need desugaring 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

# Set API level for smali and d8.
API_LEVEL="${EXPERIMENTAL_API_LEVEL[${EXPERIMENTAL}]}"

# Add API level arguments to smali and dx
SMALI_ARGS="${SMALI_ARGS} --api $API_LEVEL"
D8_FLAGS="${D8_FLAGS} --min-api $API_LEVEL"

#########################################

# 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
}

# 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. This will be
# packaged in a jar file.
function make_dex() {
  local name="$1"
  local d8_inputs=$(find $name -name '*.class' -type f)
  local d8_output=${name}.jar
  local dex_output=${name}.dex
  local d8_local_flags=""
  if [[ "$USE_DESUGAR" = "true" ]]; then
    local boot_class_path_list=$($ANDROID_BUILD_TOP/art/tools/bootjars.sh --$BUILD_MODE --core --path)
    for boot_class_path_element in $boot_class_path_list; do
      d8_local_flags="$d8_local_flags --classpath $boot_class_path_element"
    done
  else
    d8_local_flags="$d8_local_flags --no-desugaring"
  fi
  if [ "$DEV_MODE" = "yes" ]; then
    echo ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs
  fi
  ${D8} ${D8_FLAGS} $d8_local_flags --output $d8_output $d8_inputs

  # D8 outputs to JAR files today rather than DEX files as DX used
  # to. To compensate, we extract the DEX from d8's output to meet the
  # expectations of make_dex callers.
  if [ "$DEV_MODE" = "yes" ]; then
    echo unzip -p $d8_output classes.dex \> $dex_output
  fi
  unzip -p $d8_output classes.dex > $dex_output
}

# 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
  ${DEXMERGER} --min-api 1000 --output ./d8_merge_out "${dex_files_to_merge[@]}"

  if [[ -e "./d8_merge_out/classes2.dex" ]]; then
    fail "Cannot merge all dex files into a single dex"
  fi

  mv ./d8_merge_out/classes.dex "$dst_file";
  rmdir d8_merge_out
}

function make_hiddenapi() {
  local args=( "encode" )
  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 the classes directory is not-empty, package classes in a DEX file. NB some
  # tests provide classes rather than java files.
  if [ "$(ls -A classes)" ]; 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
    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
    fail "${SMALI} produced no output."
  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
    make_dex jasmin_classes2
    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 [ -f classes2.dex ] ; then
    zip $TEST_NAME.jar classes.dex classes2.dex
  else
    zip $TEST_NAME.jar classes.dex
  fi
fi
