blob: 8717fe305660361276266693c0c56b4ddac69fd6 [file] [log] [blame]
Ulya Trafimovich5f364b62020-06-30 12:39:01 +01001#!/usr/bin/env python
2#
3# Copyright (C) 2020 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#
17"""A tool for constructing class loader context."""
18
19from __future__ import print_function
20
21import argparse
22import sys
23
24from manifest import compare_version_gt
25
26
27def parse_args(args):
28 """Parse commandline arguments."""
29 parser = argparse.ArgumentParser()
30 parser.add_argument('--target-sdk-version', default='', dest='sdk',
31 help='specify target SDK version (as it appears in the manifest)')
32 parser.add_argument('--host-classpath-for-sdk', dest='host_classpaths',
33 action='append', nargs=2, metavar=('sdk','classpath'),
34 help='specify classpath on host for a given SDK version or "any" version')
35 parser.add_argument('--target-classpath-for-sdk', dest='target_classpaths',
36 action='append', nargs=2, metavar=('sdk','classpath'),
37 help='specify classpath on target for a given SDK version or "any" version')
38 return parser.parse_args(args)
39
40# The hidl.manager shared library has a dependency on hidl.base. We manually
41# add that information to the class loader context if we see those libraries.
42HIDL_MANAGER = 'android.hidl.manager-V1.0-java'
43HIDL_BASE = 'android.hidl.base-V1.0-java'
44
45# Special keyword that means that the classpath should be added to class loader
46# context regardless of the target SDK version.
47any_sdk = 'any'
48
49# We assume that the order of classpath arguments passed to this script is
50# correct (matches the order computed by package manager). It is possible to
51# sort them here, but Soong needs to use deterministic order anyway, so it can
52# as well use the correct order.
53def construct_context(versioned_classpaths, target_sdk):
54 context = []
55 for [sdk, classpath] in versioned_classpaths:
56 if sdk == any_sdk or compare_version_gt(sdk, target_sdk):
57 for jar in classpath.split(':'):
58 pcl = 'PCL[%s]' % jar
59 if HIDL_MANAGER in jar:
60 pcl += '{PCL[%s]}' % jar.replace(HIDL_MANAGER, HIDL_BASE, 1)
61 context.append(pcl)
62 return context
63
64def construct_contexts(args):
65 host_context = construct_context(args.host_classpaths, args.sdk)
66 target_context = construct_context(args.target_classpaths, args.sdk)
67 context_sep = '#'
68 return ('class_loader_context_arg=--class-loader-context=PCL[]{%s} ; ' % context_sep.join(host_context) +
69 'stored_class_loader_context_arg=--stored-class-loader-context=PCL[]{%s}' % context_sep.join(target_context))
70
71def main():
72 """Program entry point."""
73 try:
74 args = parse_args(sys.argv[1:])
75 if not args.sdk:
76 raise SystemExit('target sdk version is not set')
77 if not args.host_classpaths:
78 raise SystemExit('host classpath is not set')
79 if not args.target_classpaths:
80 raise SystemExit('target classpath is not set')
81
82 print(construct_contexts(args))
83
84 # pylint: disable=broad-except
85 except Exception as err:
86 print('error: ' + str(err), file=sys.stderr)
87 sys.exit(-1)
88
89if __name__ == '__main__':
90 main()