1#! /bin/sh
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# This script is used on host and device. It uses a common subset
17# shell dialect that should work on the host (e.g. bash), and
18# Android (e.g. mksh). Try to switch to bash if the shebang above
19# has launched a pessimal shell on host.
20if [ -z "$KSH_VERSION" -a -z "$BASH_VERSION" -a -n "$(which bash)" ]; then
21  exec bash -c ". $0" -- "$@"
22fi
23
24# The purpose of this script is to invoke dex2oat with the right
25# boot classpath and bootclasspath locations.
26
27# Follow all sym links to get the program name.
28if [[ -n "$BASH_SOURCE" ]]; then
29  PROG_NAME="$BASH_SOURCE"
30else
31  PROG_NAME="$0"
32fi
33while [ -h "$PROG_NAME" ]; do
34  # On Mac OS, readlink -f doesn't work.
35  PROG_NAME="$(readlink "$PROG_NAME")"
36done
37
38PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
39ANDROID_ROOT="$(cd $PROG_DIR/..; pwd -P)"
40
41declare -a args=("$@")
42arg_idx=0
43while true; do
44  if [[ $1 == "-Xbootclasspath:*" ]]; then
45    DEX2OAT_BCP=$1
46    # Remove '-Xbootclasspath:' from the arguments.
47    DEX2OAT_BCP=${DEX2OAT_BCP##-Xbootclasspath:}
48    unset args[arg_idx]
49    shift
50  elif [[ $1 == "-Xbootclasspath-locations:*" ]]; then
51    DEX2OAT_BCP_LOCS=$1
52    # Remove '-Xbootclasspath-locations:' from the argument.
53    DEX2OAT_BCP_LOCS=${DEX2OAT_BCP_LOCS##-Xbootclasspath-locations:}
54    unset args[arg_idx]
55    shift
56  elif [[ $1 == "--32" ]]; then
57    BITNESS=32
58    LD_LIBRARY_PATH=$ANDROID_ROOT/lib:$LD_LIBRARY_PATH
59    unset args[arg_idx]
60    shift
61  elif [[ $1 == "--64" ]]; then
62    BITNESS=64
63    LD_LIBRARY_PATH=$ANDROID_ROOT/lib64:$LD_LIBRARY_PATH
64    unset args[arg_idx]
65    shift
66  elif [[ "$1" == "" ]]; then
67    break
68  else
69    shift
70  fi
71  arg_idx=$((arg_idx + 1))
72done
73
74if [ -z "$BITNESS" ]; then
75  echo "Either --32 or --64 is required as argument to specify bitness"
76  exit 1
77fi
78
79# Create boot class path filename or location list.
80# It takes one optional argument which is the prefix to be inserted before each entry.
81function get_boot_class_path() {
82  # Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
83  local modules="core-oj core-libart okhttp bouncycastle apache-xml core-icu4j conscrypt"
84  local prefix="$1"
85  local result=""
86  local separator=""
87  for module in ${modules}; do
88    case "$module" in
89      (conscrypt)  local apex="com.android.conscrypt";;
90      (core-icu4j) local apex="com.android.i18n";;
91      (*)          local apex="com.android.art";;
92    esac
93    result+="${separator}${prefix}/apex/${apex}/javalib/${module}.jar"
94    separator=":"
95  done
96  echo "$result"
97}
98
99# Create default boot class path if none was provided.
100if [[ "$DEX2OAT_BCP" = "" ]]; then
101  ANDROID_ROOT_MINUS_PWD="${ANDROID_ROOT#$PWD/}"  # For example: out/host/linux-x86
102  if [[ "$ANDROID_ROOT_MINUS_PWD" == */host/* ]]; then
103    DEX2OAT_BCP="$(get_boot_class_path $ANDROID_ROOT)"
104    DEX2OAT_BCP_LOCS="$(get_boot_class_path $ANDROID_ROOT_MINUS_PWD)"
105  elif [[ "$ANDROID_ROOT_MINUS_PWD" == */target/* ]]; then
106    DEX2OAT_BCP="$(get_boot_class_path $ANDROID_ROOT)"
107    DEX2OAT_BCP_LOCS="$(get_boot_class_path)"
108  else
109    echo "Can not determine whether are running on host or target"
110    exit 1
111  fi
112fi
113
114# If the dex2oat binary with the bitness as a suffix doesn't exist,
115# try with a dex2oat without suffix.
116DEX2OAT_SUFFIX=$BITNESS
117if [[ ! -f $ANDROID_ROOT/bin/dex2oat${DEX2OAT_SUFFIX} ]]; then
118  DEX2OAT_SUFFIX=""
119fi
120
121LD_LIBRARY_PATH=$LD_LIBRARY_PATH \
122  $ANDROID_ROOT/bin/dex2oat${DEX2OAT_SUFFIX} \
123    --android-root=$ANDROID_ROOT \
124    --runtime-arg -Xbootclasspath:$DEX2OAT_BCP \
125    --runtime-arg -Xbootclasspath-locations:$DEX2OAT_BCP_LOCS \
126    ${args[@]}
127