blob: 3f601c308fe59532763101a8a12b6283d02de622 [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):
Spandan Dasec555f12021-08-25 18:42:40 +000028 """Parse commandline arguments."""
29 parser = argparse.ArgumentParser()
30 parser.add_argument(
31 '--target-sdk-version',
32 default='',
33 dest='sdk',
34 help='specify target SDK version (as it appears in the manifest)')
35 parser.add_argument(
36 '--host-context-for-sdk',
37 dest='host_contexts',
38 action='append',
39 nargs=2,
40 metavar=('sdk', 'context'),
41 help='specify context on host for a given SDK version or "any" version')
42 parser.add_argument(
43 '--target-context-for-sdk',
44 dest='target_contexts',
45 action='append',
46 nargs=2,
47 metavar=('sdk', 'context'),
48 help='specify context on target for a given SDK version or "any" '
49 'version'
50 )
51 return parser.parse_args(args)
52
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010053
Ulya Trafimovich8130c482020-10-07 15:17:13 +010054# Special keyword that means that the context should be added to class loader
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010055# context regardless of the target SDK version.
56any_sdk = 'any'
57
Spandan Dasec555f12021-08-25 18:42:40 +000058
Ulya Trafimovich8130c482020-10-07 15:17:13 +010059# We assume that the order of context arguments passed to this script is
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010060# correct (matches the order computed by package manager). It is possible to
61# sort them here, but Soong needs to use deterministic order anyway, so it can
62# as well use the correct order.
Ulya Trafimovich8130c482020-10-07 15:17:13 +010063def construct_context(versioned_contexts, target_sdk):
Spandan Dasec555f12021-08-25 18:42:40 +000064 context = []
65 for [sdk, ctx] in versioned_contexts:
66 if sdk == any_sdk or compare_version_gt(sdk, target_sdk):
67 context.append(ctx)
68 return context
69
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010070
71def construct_contexts(args):
Spandan Dasec555f12021-08-25 18:42:40 +000072 host_context = construct_context(args.host_contexts, args.sdk)
73 target_context = construct_context(args.target_contexts, args.sdk)
74 context_sep = '#'
75 return (
76 'class_loader_context_arg=--class-loader-context=PCL[]{%s} ; ' %
77 context_sep.join(host_context) +
78 'stored_class_loader_context_arg=--stored-class-loader-context=PCL[]{%s}' #pylint: disable=line-too-long
79 % context_sep.join(target_context))
80
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010081
82def main():
Spandan Dasec555f12021-08-25 18:42:40 +000083 """Program entry point."""
84 try:
85 args = parse_args(sys.argv[1:])
86 if not args.sdk:
87 raise SystemExit('target sdk version is not set')
88 if not args.host_contexts:
89 args.host_contexts = []
90 if not args.target_contexts:
91 args.target_contexts = []
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010092
Spandan Dasec555f12021-08-25 18:42:40 +000093 print(construct_contexts(args))
Ulya Trafimovich5f364b62020-06-30 12:39:01 +010094
Spandan Dasec555f12021-08-25 18:42:40 +000095 # pylint: disable=broad-except
96 except Exception as err:
97 print('error: ' + str(err), file=sys.stderr)
98 sys.exit(-1)
99
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100100
101if __name__ == '__main__':
Spandan Dasec555f12021-08-25 18:42:40 +0000102 main()