1#!/bin/bash
2#
3# Copyright 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17if [[ ${#@} != 1 ]]; then
18  cat <<EOF
19Usage
20  host_bcp <image> | xargs <art-host-tool> ...
21Extracts boot class path locations from <image> and outputs the appropriate
22  --runtime-arg -Xbootclasspath:...
23  --runtime-arg -Xbootclasspath-locations:...
24arguments for many ART host tools based on the \$ANDROID_PRODUCT_OUT variable
25and existing \$ANDROID_PRODUCT_OUT/apex/com.android.runtime* paths.
26EOF
27  exit 1
28fi
29
30IMAGE=$1
31if [[ ! -e ${IMAGE} ]]; then
32  IMAGE=${ANDROID_PRODUCT_OUT}$1
33  if [[ ! -e ${IMAGE} ]]; then
34    echo "Neither $1 nor ${ANDROID_PRODUCT_OUT}$1 exists."
35    exit 1
36  fi
37fi
38BCPL=`grep -az -A1 -E '^bootclasspath$' ${IMAGE} 2>/dev/null | \
39      xargs -0 echo | gawk '{print $2}'`
40if [[ "x${BCPL}" == "x" ]]; then
41  echo "Failed to extract boot class path locations from $1."
42  exit 1
43fi
44
45RUNTIME_APEX=/apex/com.android.runtime
46RUNTIME_APEX_SELECTED=
47for d in `ls -1 -d ${ANDROID_PRODUCT_OUT}${RUNTIME_APEX}* 2>/dev/null`; do
48  if [[ "x${RUNTIME_APEX_SELECTED}" != "x" ]]; then
49    echo "Multiple Runtime apex dirs: ${RUNTIME_APEX_SELECTED}, ${d}."
50    exit 1
51  fi
52  RUNTIME_APEX_SELECTED=${d}
53done
54if [[ "x${RUNTIME_APEX_SELECTED}" == "x" ]]; then
55  echo "No Runtime apex dir."
56  exit 1
57fi
58
59BCP=
60OLD_IFS=${IFS}
61IFS=:
62for COMPONENT in ${BCPL}; do
63  HEAD=${ANDROID_PRODUCT_OUT}
64  TAIL=${COMPONENT}
65  if [[ ${COMPONENT:0:${#RUNTIME_APEX}} = ${RUNTIME_APEX} ]]; then
66    HEAD=${RUNTIME_APEX_SELECTED}
67    TAIL=${COMPONENT:${#RUNTIME_APEX}}
68  fi
69  BCP="${BCP}:${HEAD}${TAIL}"
70done
71IFS=${OLD_IFS}
72BCP=${BCP:1}  # Strip leading ':'.
73
74echo --runtime-arg -Xbootclasspath:${BCP} \
75     --runtime-arg -Xbootclasspath-locations:${BCPL}
76