# Copyright (C) 2020 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.

# This script is used on host and device. It uses a common subset
# shell dialect that should work on the host (e.g. bash), and
# Android (e.g. mksh).

# The purpose of this script is to invoke dex2oat with the right
# boot classpath and bootclasspath locations.

# Follow all sym links to get the program name.
if [[ -n "$BASH_SOURCE" ]]; then
  PROG_NAME="$BASH_SOURCE"
else
  PROG_NAME="$0"
fi
while [ -h "$PROG_NAME" ]; do
  # On Mac OS, readlink -f doesn't work.
  PROG_NAME="$(readlink "$PROG_NAME")"
done

PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
ANDROID_ROOT="$(cd $PROG_DIR/..; pwd -P)"

declare -a args=("$@")
arg_idx=0
while true; do
  if [[ $1 == "-Xbootclasspath:*" ]]; then
    DEX2OAT_BCP=$1
    # Remove '-Xbootclasspath:' from the arguments.
    DEX2OAT_BCP=${DEX2OAT_BCP##-Xbootclasspath:}
    unset args[arg_idx]
    shift
  elif [[ $1 == "-Xbootclasspath-locations:*" ]]; then
    DEX2OAT_BCP_LOCS=$1
    # Remove '-Xbootclasspath-locations:' from the argument.
    DEX2OAT_BCP_LOCS=${DEX2OAT_BCP_LOCS##-Xbootclasspath-locations:}
    unset args[arg_idx]
    shift
  elif [[ $1 == "--32" ]]; then
    BITNESS=32
    LD_LIBRARY_PATH=$ANDROID_ROOT/lib:$LD_LIBRARY_PATH
    unset args[arg_idx]
    shift
  elif [[ $1 == "--64" ]]; then
    BITNESS=64
    LD_LIBRARY_PATH=$ANDROID_ROOT/lib64:$LD_LIBRARY_PATH
    unset args[arg_idx]
    shift
  elif [[ "$1" == "" ]]; then
    break
  else
    shift
  fi
  arg_idx=$((arg_idx + 1))
done

if [ -z "$BITNESS" ]; then
  echo "Either --32 or --64 is required as argument to specify bitness"
  exit 1
fi

# Create boot class path filename or location list.
# It takes one optional argument which is the prefix to be inserted before each entry.
function get_boot_class_path() {
  # Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
  local modules="core-oj core-libart okhttp bouncycastle apache-xml core-icu4j conscrypt"
  local prefix="$1"
  local result=""
  local separator=""
  for module in ${modules}; do
    case "$module" in
      (conscrypt)  local apex="com.android.conscrypt";;
      (core-icu4j) local apex="com.android.i18n";;
      (*)          local apex="com.android.art";;
    esac
    result+="${separator}${prefix}/apex/${apex}/javalib/${module}.jar"
    separator=":"
  done
  echo "$result"
}

# Create default boot class path if none was provided.
if [[ "$DEX2OAT_BCP" = "" ]]; then
  ANDROID_ROOT_MINUS_PWD="${ANDROID_ROOT#$PWD/}"  # For example: out/host/linux-x86
  if [[ "$ANDROID_ROOT_MINUS_PWD" == */host/* ]]; then
    DEX2OAT_BCP="$(get_boot_class_path $ANDROID_ROOT)"
    DEX2OAT_BCP_LOCS="$(get_boot_class_path $ANDROID_ROOT_MINUS_PWD)"
  elif [[ "$ANDROID_ROOT_MINUS_PWD" == */target/* ]]; then
    DEX2OAT_BCP="$(get_boot_class_path $ANDROID_ROOT)"
    DEX2OAT_BCP_LOCS="$(get_boot_class_path)"
  else
    echo "Can not determine whether are running on host or target"
    exit 1
  fi
fi

# If the dex2oat binary with the bitness as a suffix doesn't exist,
# try with a dex2oat without suffix.
DEX2OAT_SUFFIX=$BITNESS
if [[ ! -f $ANDROID_ROOT/bin/dex2oat${DEX2OAT_SUFFIX} ]]; then
  DEX2OAT_SUFFIX=""
fi

LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
  $ANDROID_ROOT/bin/dex2oat${DEX2OAT_SUFFIX} \
    --android-root=$ANDROID_ROOT \
    --runtime-arg -Xbootclasspath:$DEX2OAT_BCP \
    --runtime-arg -Xbootclasspath-locations:$DEX2OAT_BCP_LOCS \
    ${args[@]}