Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 1 | #!/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 | |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import argparse |
| 22 | import sys |
| 23 | |
| 24 | from manifest import compare_version_gt |
| 25 | |
| 26 | |
| 27 | def 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 Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 32 | 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 Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 38 | return parser.parse_args(args) |
| 39 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 40 | # Special keyword that means that the context should be added to class loader |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 41 | # context regardless of the target SDK version. |
| 42 | any_sdk = 'any' |
| 43 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 44 | # We assume that the order of context arguments passed to this script is |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 45 | # 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 Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 48 | def construct_context(versioned_contexts, target_sdk): |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 49 | context = [] |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 50 | for [sdk, ctx] in versioned_contexts: |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 51 | if sdk == any_sdk or compare_version_gt(sdk, target_sdk): |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 52 | context.append(ctx) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 53 | return context |
| 54 | |
| 55 | def construct_contexts(args): |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 56 | host_context = construct_context(args.host_contexts, args.sdk) |
| 57 | target_context = construct_context(args.target_contexts, args.sdk) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 58 | 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 | |
| 62 | def 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 Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 68 | if not args.host_contexts: |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 69 | args.host_contexts = [] |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 70 | if not args.target_contexts: |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 71 | args.target_contexts = [] |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 72 | |
| 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 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |