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

# Set default values for directories.
if [ -d smali ]; then
  HAS_SMALI=true
else
  HAS_SMALI=false
fi

if [ -d src ]; then
  HAS_SRC=true
else
  HAS_SRC=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

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

DX_FLAGS=""
SKIP_DX_MERGER="false"
EXPERIMENTAL=""

# 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 JACK_EXPERIMENTAL_ARGS
JACK_EXPERIMENTAL_ARGS["default-methods"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
JACK_EXPERIMENTAL_ARGS["lambdas"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=24"
JACK_EXPERIMENTAL_ARGS["method-handles"]="-D jack.java.source.version=1.8 -D jack.android.min-api-level=26"

declare -A SMALI_EXPERIMENTAL_ARGS
SMALI_EXPERIMENTAL_ARGS["default-methods"]="--api-level 24"
SMALI_EXPERIMENTAL_ARGS["method-handles"]="--api-level 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[${DEFAULT_EXPERIMENT}]="-source 1.7 -target 1.7"

while true; do
  if [ "x$1" = "x--dx-option" ]; then
    shift
    on="$1"
    DX_FLAGS="${DX_FLAGS} $option"
    shift
  elif [ "x$1" = "x--jvm" ]; then
    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--experimental" ]; then
    shift
    # We have a specific experimental configuration so don't use the default.
    DEFAULT_EXPERIMENT=""
    EXPERIMENTAL="${EXPERIMENTAL} $1"
    shift
  elif expr "x$1" : "x--" >/dev/null 2>&1; then
    echo "unknown $0 option: $1" 1>&2
    exit 1
  else
    break
  fi
done

# 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
  JACK_ARGS="${JACK_ARGS} ${JACK_EXPERIMENTAL_ARGS[${experiment}]}"
  SMALI_ARGS="${SMALI_ARGS} ${SMALI_EXPERIMENTAL_ARGS[${experiment}]}"
  JAVAC_ARGS="${JAVAC_ARGS} ${JAVAC_EXPERIMENTAL_ARGS[${experiment}]}"
done

if [ -e classes.dex ]; then
  zip $TEST_NAME.jar classes.dex
  exit 0
fi

if ! [ "${HAS_SRC}" = "true" ] && ! [ "${HAS_SRC2}" = "true" ]; then
  # No src directory? Then forget about trying to run dx.
  SKIP_DX_MERGER="true"
fi

if [ ${HAS_SRC_DEX2OAT_UNRESOLVED} = "true" ]; then
  mkdir classes
  mkdir classes-ex
  ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src-dex2oat-unresolved -d classes `find src -name '*.java'`
  ${JAVAC} ${JAVAC_ARGS} -implicit:none -sourcepath src -d classes-ex `find src-dex2oat-unresolved -name '*.java'`
  if [ ${USE_JACK} = "true" ]; then
    jar cf classes.jill.jar -C classes .
    jar cf classes-ex.jill.jar -C classes-ex .

    ${JACK} --import classes-ex.jill.jar --output-dex .
    zip ${TEST_NAME}-ex.jar classes.dex
    ${JACK} --import classes.jill.jar --output-dex .
  else
    if [ ${NEED_DEX} = "true" ]; then
      ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes.dex --dump-width=1000 classes-ex
      zip ${TEST_NAME}-ex.jar classes.dex
      ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex --dump-width=1000 classes
    fi
  fi
else
  if [ ${USE_JACK} = "true" ]; then
    # Jack toolchain
    if [ "${HAS_SRC}" = "true" ]; then
      if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
        # Compile src and src-multidex in the same .jack file. We will apply multidex partitioning
        # when creating the output .dex file.
        ${JACK} ${JACK_ARGS} --output-jack src.jack src src src-multidex
        jack_extra_args="${jack_extra_args} -D jack.dex.output.policy=minimal-multidex"
        jack_extra_args="${jack_extra_args} -D jack.preprocessor=true"
        jack_extra_args="${jack_extra_args} -D jack.preprocessor.file=multidex.jpp"
      else
        ${JACK} ${JACK_ARGS} --output-jack src.jack src
      fi
      jack_extra_args="${jack_extra_args} --import src.jack"
    fi

    if [ "${HAS_SRC2}" = "true" ]; then
      ${JACK} ${JACK_ARGS} --output-jack src2.jack src2
      # In case of duplicate classes, we want to take into account the classes from src2. Therefore
      # we apply the 'keep-first' policy and import src2.jack file *before* the src.jack file.
      jack_extra_args="${jack_extra_args} -D jack.import.type.policy=keep-first"
      jack_extra_args="--import src2.jack ${jack_extra_args}"
    fi

    # Compile jack files into a DEX file.
    if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
      ${JACK} ${JACK_ARGS} ${jack_extra_args} --output-dex .
    fi
  else
    # Legacy toolchain with javac+dx
    if [ "${HAS_SRC}" = "true" ]; then
      mkdir classes
      ${JAVAC} ${JAVAC_ARGS} -implicit:none -classpath src-multidex -d classes `find src -name '*.java'`
    fi

    if [ "${HAS_SRC_MULTIDEX}" = "true" ]; then
      mkdir classes2
      ${JAVAC} -implicit:none -classpath src -d classes2 `find src-multidex -name '*.java'`
      if [ ${NEED_DEX} = "true" ]; then
        ${DX} -JXmx256m --debug --dex --dump-to=classes2.lst --output=classes2.dex \
          --dump-width=1000 ${DX_FLAGS} classes2
      fi
    fi

    if [ "${HAS_SRC2}" = "true" ]; then
      mkdir -p classes
      ${JAVAC} ${JAVAC_ARGS} -d classes `find src2 -name '*.java'`
    fi

    if [ "${HAS_SRC}" = "true" ] || [ "${HAS_SRC2}" = "true" ]; then
      if [ ${NEED_DEX} = "true" -a ${SKIP_DX_MERGER} = "false" ]; then
        ${DX} -JXmx256m --debug --dex --dump-to=classes.lst --output=classes.dex \
          --dump-width=1000 ${DX_FLAGS} classes
      fi
    fi
  fi
fi

if [ "${HAS_SMALI}" = "true" ]; then
  # Compile Smali classes
  ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes.dex `find smali -name '*.smali'`

  # Don't bother with dexmerger if we provide our own main function in a smali file.
  if [ ${SKIP_DX_MERGER} = "false" ]; then
    ${DXMERGER} classes.dex classes.dex smali_classes.dex
  else
    mv smali_classes.dex classes.dex
  fi
fi

if [ "${HAS_SMALI_MULTIDEX}" = "true" ]; then
  # Compile Smali classes
  ${SMALI} -JXmx512m ${SMALI_ARGS} --output smali_classes2.dex `find smali-multidex -name '*.smali'`

  # Don't bother with dexmerger if we provide our own main function in a smali file.
  if [ ${HAS_SRC_MULTIDEX} = "true" ]; then
    ${DXMERGER} classes2.dex classes2.dex smali_classes2.dex
  else
    mv smali_classes2.dex classes2.dex
  fi
fi


if [ ${HAS_SRC_EX} = "true" ]; then
  if [ ${USE_JACK} = "true" ]; then
      # Rename previous "classes.dex" so it is not overwritten.
      mv classes.dex classes-1.dex
      #TODO find another way to append src.jack to the jack classpath
      ${JACK}:src.jack ${JACK_ARGS} --output-dex . src-ex
      zip $TEST_NAME-ex.jar classes.dex
      # Restore previous "classes.dex" so it can be zipped.
      mv classes-1.dex classes.dex
  else
    mkdir classes-ex
    ${JAVAC} ${JAVAC_ARGS} -d classes-ex -cp classes `find src-ex -name '*.java'`
    if [ ${NEED_DEX} = "true" ]; then
      ${DX} -JXmx256m --debug --dex --dump-to=classes-ex.lst --output=classes-ex.dex \
        --dump-width=1000 ${DX_FLAGS} classes-ex

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

# Create a single jar with two dex files for multidex.
if [ ${HAS_SRC_MULTIDEX} = "true" ] || [ ${HAS_SMALI_MULTIDEX} = "true" ]; then
  zip $TEST_NAME.jar classes.dex classes2.dex
elif [ ${NEED_DEX} = "true" ]; then
  zip $TEST_NAME.jar classes.dex
fi
