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