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): |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 28 | """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 Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 53 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 54 | # 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] | 55 | # context regardless of the target SDK version. |
| 56 | any_sdk = 'any' |
| 57 | |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 58 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 59 | # 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] | 60 | # 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 Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 63 | def construct_context(versioned_contexts, target_sdk): |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 64 | 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 Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 70 | |
| 71 | def construct_contexts(args): |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 72 | 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 Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 81 | |
| 82 | def main(): |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 83 | """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 Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 92 | |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 93 | print(construct_contexts(args)) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 94 | |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 95 | # pylint: disable=broad-except |
| 96 | except Exception as err: |
| 97 | print('error: ' + str(err), file=sys.stderr) |
| 98 | sys.exit(-1) |
| 99 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
Spandan Das | ec555f1 | 2021-08-25 18:42:40 +0000 | [diff] [blame] | 102 | main() |