xref: /openbmc/qemu/scripts/modinfo-generate.py (revision 1580b897)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4import os
5import sys
6
7def print_array(name, values):
8    if len(values) == 0:
9        return
10    list = ", ".join(values)
11    print("    .%s = ((const char*[]){ %s, NULL })," % (name, list))
12
13def parse_line(line):
14    kind = ""
15    data = ""
16    get_kind = False
17    get_data = False
18    for item in line.split():
19        if item == "MODINFO_START":
20            get_kind = True
21            continue
22        if item.startswith("MODINFO_END"):
23            get_data = False
24            continue
25        if get_kind:
26            kind = item
27            get_kind = False
28            get_data = True
29            continue
30        if get_data:
31            data += " " + item
32            continue
33    return (kind, data)
34
35def generate(name, lines):
36    arch = ""
37    objs = []
38    deps = []
39    opts = []
40    for line in lines:
41        if line.find("MODINFO_START") != -1:
42            (kind, data) = parse_line(line)
43            if kind == 'obj':
44                objs.append(data)
45            elif kind == 'dep':
46                deps.append(data)
47            elif kind == 'opts':
48                opts.append(data)
49            elif kind == 'arch':
50                arch = data;
51            else:
52                print("unknown:", kind)
53                exit(1)
54
55    print("    .name = \"%s\"," % name)
56    if arch != "":
57        print("    .arch = %s," % arch)
58    print_array("objs", objs)
59    print_array("deps", deps)
60    print_array("opts", opts)
61    print("},{");
62    return deps
63
64def print_pre():
65    print("/* generated by scripts/modinfo-generate.py */")
66    print("#include \"qemu/osdep.h\"")
67    print("#include \"qemu/module.h\"")
68    print("const QemuModinfo qemu_modinfo[] = {{")
69
70def print_post():
71    print("    /* end of list */")
72    print("}};")
73
74def main(args):
75    deps = {}
76    print_pre()
77    for modinfo in args:
78        with open(modinfo) as f:
79            lines = f.readlines()
80        print("    /* %s */" % modinfo)
81        (basename, ext) = os.path.splitext(modinfo)
82        deps[basename] = generate(basename, lines)
83    print_post()
84
85    flattened_deps = {flat.strip('" ') for dep in deps.values() for flat in dep}
86    error = False
87    for dep in flattened_deps:
88        if dep not in deps.keys():
89            print("Dependency {} cannot be satisfied".format(dep),
90                  file=sys.stderr)
91            error = True
92
93    if error:
94        exit(1)
95
96if __name__ == "__main__":
97    main(sys.argv[1:])
98