• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
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
53
54# Special keyword that means that the context should be added to class loader
55# context regardless of the target SDK version.
56any_sdk = 'any'
57
58
59# We assume that the order of context arguments passed to this script is
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.
63def construct_context(versioned_contexts, target_sdk):
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
70
71def construct_contexts(args):
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
81
82def main():
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 = []
92
93        print(construct_contexts(args))
94
95    # pylint: disable=broad-except
96    except Exception as err:
97        print('error: ' + str(err), file=sys.stderr)
98        sys.exit(-1)
99
100
101if __name__ == '__main__':
102    main()
103