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 ]] && [[ ${#@} != 2 ]]; then
18  cat <<EOF
19Usage
20  host_bcp <image> [--use-first-dir] | 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.art* paths.
26If --use-first-dir is specified, the script will use the first apex dir instead
27of resulting in an error.
28EOF
29  exit 1
30fi
31
32IMAGE=$1
33USE_FIRST_DIR=false
34
35if [[ $2 == "--use-first-dir" ]]; then
36  USE_FIRST_DIR=true
37fi
38
39if [[ ! -e ${IMAGE} ]]; then
40  IMAGE=${ANDROID_PRODUCT_OUT}/$1
41  if [[ ! -e ${IMAGE} ]]; then
42    echo "Neither $1 nor ${ANDROID_PRODUCT_OUT}/$1 exists."
43    exit 1
44  fi
45fi
46BCPL=`grep -az -A1 -E '^bootclasspath$' ${IMAGE} 2>/dev/null | \
47      xargs -0 echo | gawk '{print $2}'`
48if [[ "x${BCPL}" == "x" ]]; then
49  echo "Failed to extract boot class path locations from $1."
50  exit 1
51fi
52
53MANIFEST=/apex_manifest.pb
54ART_APEX=/apex/com.android.art
55ART_APEX_SELECTED=
56for m in `ls -1 -d ${ANDROID_PRODUCT_OUT}{,/system}${ART_APEX}*${MANIFEST} 2>/dev/null`; do
57  d=${m:0:-${#MANIFEST}}
58  if [[ "x${ART_APEX_SELECTED}" != "x" ]]; then
59    if [[ $USE_FIRST_DIR == true ]]; then
60      break
61    fi
62    echo "Multiple ART APEX dirs: ${ART_APEX_SELECTED}, ${d}."
63    exit 1
64  fi
65  ART_APEX_SELECTED=${d}
66done
67if [[ "x${ART_APEX_SELECTED}" == "x" ]]; then
68  echo "No ART APEX dir."
69  exit 1
70fi
71
72BCP=
73OLD_IFS=${IFS}
74IFS=:
75for COMPONENT in ${BCPL}; do
76  HEAD=${ANDROID_PRODUCT_OUT}
77  TAIL=${COMPONENT}
78  if [[ ${COMPONENT:0:${#ART_APEX}} = ${ART_APEX} ]]; then
79    HEAD=${ART_APEX_SELECTED}
80    TAIL=${COMPONENT:${#ART_APEX}}
81  fi
82  if [[ ! -e $HEAD$TAIL ]]; then
83    echo "File does not exist: $HEAD$TAIL"
84    exit 1
85  fi
86  BCP="${BCP}:${HEAD}${TAIL}"
87done
88IFS=${OLD_IFS}
89BCP=${BCP:1}  # Strip leading ':'.
90
91echo --runtime-arg -Xbootclasspath:${BCP} \
92     --runtime-arg -Xbootclasspath-locations:${BCPL}
93