1#!/bin/bash
2#
3# Copyright (C) 2018 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
17set -e
18
19# target_sdk_version: parsed from manifest
20#
21# outputs
22# class_loader_context_arg: final class loader conext arg
23# stored_class_loader_context_arg: final stored class loader context arg
24
25if [ -z "${target_sdk_version}" ]; then
26    echo "ERROR: target_sdk_version not set"
27    exit 2
28fi
29
30# The hidl.manager shared library has a dependency on hidl.base. We'll manually
31# add that information to the class loader context if we see those libraries.
32hidl_manager="android.hidl.manager-V1.0-java"
33hidl_base="android.hidl.base-V1.0-java"
34
35function add_to_contexts {
36  for i in $1; do
37    if [[ -z "${class_loader_context}" ]]; then
38      export class_loader_context="PCL[$i]"
39    else
40      export class_loader_context+="#PCL[$i]"
41    fi
42    if [[ $i == *"$hidl_manager"* ]]; then
43      export class_loader_context+="{PCL[${i/$hidl_manager/$hidl_base}]}"
44    fi
45  done
46
47  for i in $2; do
48    if [[ -z "${stored_class_loader_context}" ]]; then
49      export stored_class_loader_context="PCL[$i]"
50    else
51      export stored_class_loader_context+="#PCL[$i]"
52    fi
53    if [[ $i == *"$hidl_manager"* ]]; then
54      export stored_class_loader_context+="{PCL[${i/$hidl_manager/$hidl_base}]}"
55    fi
56  done
57}
58
59# The order below must match what the package manager also computes for
60# class loader context.
61
62if [[ "${target_sdk_version}" -lt "28" ]]; then
63  add_to_contexts "${conditional_host_libs_28}" "${conditional_target_libs_28}"
64fi
65
66if [[ "${target_sdk_version}" -lt "29" ]]; then
67  add_to_contexts "${conditional_host_libs_29}" "${conditional_target_libs_29}"
68fi
69
70add_to_contexts "${dex_preopt_host_libraries}" "${dex_preopt_target_libraries}"
71
72# Generate the actual context string.
73export class_loader_context_arg="--class-loader-context=PCL[]{${class_loader_context}}"
74export stored_class_loader_context_arg="--stored-class-loader-context=PCL[]{${stored_class_loader_context}}"
75