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 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-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') 38 return parser.parse_args(args) 39 40# Special keyword that means that the context should be added to class loader 41# context regardless of the target SDK version. 42any_sdk = 'any' 43 44# We assume that the order of context arguments passed to this script is 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. 48def construct_context(versioned_contexts, target_sdk): 49 context = [] 50 for [sdk, ctx] in versioned_contexts: 51 if sdk == any_sdk or compare_version_gt(sdk, target_sdk): 52 context.append(ctx) 53 return context 54 55def construct_contexts(args): 56 host_context = construct_context(args.host_contexts, args.sdk) 57 target_context = construct_context(args.target_contexts, args.sdk) 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 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') 68 if not args.host_contexts: 69 args.host_contexts = [] 70 if not args.target_contexts: 71 args.target_contexts = [] 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 80if __name__ == '__main__': 81 main() 82