blob: 689f33c0f2919508341712a1cde453f8f144d357 [file] [log] [blame]
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +02001#!/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;
Jose R. Ziviani24ce7aa2022-05-28 00:20:23 +020051 elif kind == 'kconfig':
52 pass # ignore
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +020053 else:
54 print("unknown:", kind)
55 exit(1)
56
57 print(" .name = \"%s\"," % name)
58 if arch != "":
59 print(" .arch = %s," % arch)
60 print_array("objs", objs)
61 print_array("deps", deps)
62 print_array("opts", opts)
63 print("},{");
Jose R. Zivianiaf19eec2021-06-24 12:38:06 +020064 return deps
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +020065
66def print_pre():
67 print("/* generated by scripts/modinfo-generate.py */")
68 print("#include \"qemu/osdep.h\"")
69 print("#include \"qemu/module.h\"")
70 print("const QemuModinfo qemu_modinfo[] = {{")
71
72def print_post():
73 print(" /* end of list */")
74 print("}};")
75
76def main(args):
Jose R. Zivianiaf19eec2021-06-24 12:38:06 +020077 deps = {}
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +020078 print_pre()
79 for modinfo in args:
80 with open(modinfo) as f:
81 lines = f.readlines()
82 print(" /* %s */" % modinfo)
83 (basename, ext) = os.path.splitext(modinfo)
Jose R. Zivianiaf19eec2021-06-24 12:38:06 +020084 deps[basename] = generate(basename, lines)
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +020085 print_post()
86
Jose R. Zivianiaf19eec2021-06-24 12:38:06 +020087 flattened_deps = {flat.strip('" ') for dep in deps.values() for flat in dep}
88 error = False
89 for dep in flattened_deps:
90 if dep not in deps.keys():
91 print("Dependency {} cannot be satisfied".format(dep),
92 file=sys.stderr)
93 error = True
94
95 if error:
96 exit(1)
97
Gerd Hoffmann5ebbfec2021-06-24 12:38:05 +020098if __name__ == "__main__":
99 main(sys.argv[1:])