1#!/usr/bin/env python3
2
3#
4# Copyright (C) 2018 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from __future__ import print_function
20
21import argparse
22import csv
23import itertools
24import os
25import re
26import sys
27
28import vndk
29
30
31def _parse_args():
32    """Parse command line options."""
33    parser = argparse.ArgumentParser()
34    parser.add_argument('root_bp',
35                        help='path to Android.bp in ANDROID_BUILD_TOP')
36    parser.add_argument('-o', '--output', help='path to output file')
37    parser.add_argument('--exclude',
38                        help='regular expression for the excluded directories')
39    parser.add_argument('--select',
40                        help='regular expression for the selected directories')
41    parser.add_argument('--namespace', action='append', default=[''],
42                        help='extra module namespaces')
43    return parser.parse_args()
44
45
46def print_vndk_module_csv(output_file, module_dicts, root_dir, exclude, select):
47    """Print vndk module list to output file."""
48
49    all_libs = module_dicts.all_libs
50    vndk_libs = module_dicts.vndk_libs
51    vndk_sp_libs = module_dicts.vndk_sp_libs
52    vendor_available_libs = module_dicts.vendor_available_libs
53
54    module_names = sorted(set(
55        itertools.chain(vndk_libs, vndk_sp_libs, vendor_available_libs)))
56
57    root_dir_prefix_len = len(root_dir) + 1
58
59    writer = csv.writer(output_file, lineterminator='\n')
60    writer.writerow(('name', 'vndk', 'vndk_sp', 'vendor_available', 'rule'))
61    for name in module_names:
62        rule = all_libs[name].rule
63        path = all_libs[name].get_property('_path')[root_dir_prefix_len:]
64        if select and not select.match(path):
65            continue
66        if exclude and exclude.match(path):
67            continue
68        if '_header' not in rule and '_static' not in rule and \
69                rule != 'toolchain_library':
70            writer.writerow((name,
71                             name in vndk_libs,
72                             name in vndk_sp_libs,
73                             name in vendor_available_libs,
74                             rule))
75
76
77def main():
78    """Main function."""
79
80    args = _parse_args()
81
82    # Convert select/exclude regular expressions
83    select = re.compile(args.select) if args.select else None
84    exclude = re.compile(args.exclude) if args.exclude else None
85
86    # Parse Blueprint files and get VNDK libs
87    module_dicts = vndk.ModuleClassifier.create_from_root_bp(
88        args.root_bp, args.namespace)
89
90    root_dir = os.path.dirname(args.root_bp)
91
92    if args.output:
93        with open(args.output, 'w') as output_file:
94            print_vndk_module_csv(
95                output_file, module_dicts, root_dir, exclude, select)
96    else:
97        print_vndk_module_csv(
98            sys.stdout, module_dicts, root_dir, exclude, select)
99
100
101if __name__ == '__main__':
102    main()
103