1#!/usr/bin/env python3
2#
3# © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6
7from Cheetah.Template import Template
8
9import argparse
10import subprocess
11import sys
12
13
14class Object:
15    __slots__ = 'name', 'config'
16
17    def __init__(self, name):
18        items = name.split(',')
19        self.name = items[0]
20        self.config = items[1:]
21
22    def __str__(self):
23        return self.name
24
25    def type_enum(self):
26        return "OBJECT_TYPE_{:s}".format(self.name.upper())
27
28    def rcu_destroy_enum(self):
29        return "RCU_UPDATE_CLASS_{:s}_DESTROY".format(self.name.upper())
30
31
32def main():
33    args = argparse.ArgumentParser()
34
35    mode_args = args.add_mutually_exclusive_group(required=True)
36    mode_args.add_argument('-t', '--template',
37                           type=argparse.FileType('r', encoding="utf-8"),
38                           help="Template file used to generate output")
39
40    args.add_argument('-o', '--output',
41                      type=argparse.FileType('w', encoding="utf-8"),
42                      default=sys.stdout, help="Write output to file")
43    args.add_argument("-f", "--formatter",
44                      help="specify clang-format to format the code")
45    args.add_argument('input', metavar='INPUT', nargs='+', action='append',
46                      help="List of objects to process")
47    options = args.parse_args()
48
49    object_list = [Object(o) for group in options.input for o in group]
50
51    output = "// Automatically generated. Do not modify.\n"
52    output += "\n"
53
54    ns = {'object_list': object_list}
55    output += str(Template(file=options.template, searchList=ns))
56
57    if options.formatter:
58        ret = subprocess.run([options.formatter], input=output.encode("utf-8"),
59                             stdout=subprocess.PIPE)
60        output = ret.stdout.decode("utf-8")
61        if ret.returncode != 0:
62            raise Exception("failed to format output:\n ", ret.stderr)
63
64    options.output.write(output)
65
66
67if __name__ == '__main__':
68    main()
69