blob: f0658baa2932583504a3a10b155c108e03bf32c5 [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)')
Ulya Trafimovich8130c482020-10-07 15:17:13 +010032 parser.add_argument('--host-context-for-sdk', dest='host_contexts',
33 action='append', nargs=2, metavar=('sdk','context'),
34 help='specify context on host for a given SDK version or "any" version')
35 parser.add_argument('--target-context-for-sdk', dest='target_contexts',
36 action='append', nargs=2, metavar=('sdk','context'),
37 help='specify context on target for a given SDK version or "any" version')
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010038 return parser.parse_args(args)
39
Ulya Trafimovich8130c482020-10-07 15:17:13 +010040# Special keyword that means that the context should be added to class loader
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010041# context regardless of the target SDK version.
42any_sdk = 'any'
43
Ulya Trafimovich8130c482020-10-07 15:17:13 +010044# We assume that the order of context arguments passed to this script is
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010045# correct (matches the order computed by package manager). It is possible to
46# sort them here, but Soong needs to use deterministic order anyway, so it can
47# as well use the correct order.
Ulya Trafimovich8130c482020-10-07 15:17:13 +010048def construct_context(versioned_contexts, target_sdk):
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010049 context = []
Ulya Trafimovich8130c482020-10-07 15:17:13 +010050 for [sdk, ctx] in versioned_contexts:
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010051 if sdk == any_sdk or compare_version_gt(sdk, target_sdk):
Ulya Trafimovich8130c482020-10-07 15:17:13 +010052 context.append(ctx)
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010053 return context
54
55def construct_contexts(args):
Ulya Trafimovich8130c482020-10-07 15:17:13 +010056 host_context = construct_context(args.host_contexts, args.sdk)
57 target_context = construct_context(args.target_contexts, args.sdk)
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010058 context_sep = '#'
59 return ('class_loader_context_arg=--class-loader-context=PCL[]{%s} ; ' % context_sep.join(host_context) +
60 'stored_class_loader_context_arg=--stored-class-loader-context=PCL[]{%s}' % context_sep.join(target_context))
61
62def main():
63 """Program entry point."""
64 try:
65 args = parse_args(sys.argv[1:])
66 if not args.sdk:
67 raise SystemExit('target sdk version is not set')
Ulya Trafimovich8130c482020-10-07 15:17:13 +010068 if not args.host_contexts:
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +000069 args.host_contexts = []
Ulya Trafimovich8130c482020-10-07 15:17:13 +010070 if not args.target_contexts:
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +000071 args.target_contexts = []
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010072
73 print(construct_contexts(args))
74
75 # pylint: disable=broad-except
76 except Exception as err:
77 print('error: ' + str(err), file=sys.stderr)
78 sys.exit(-1)
79
80if __name__ == '__main__':
81 main()