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