Convert remaining error_report() to warn_report()
[qemu/kevin.git] / scripts / modules / module_block.py
blob3f73007640079b9549f85db02eb6e9bb93f78bb0
1 #!/usr/bin/python
3 # Module information generator
5 # Copyright Red Hat, Inc. 2015 - 2016
7 # Authors:
8 # Marc Mari <markmb@redhat.com>
10 # This work is licensed under the terms of the GNU GPL, version 2.
11 # See the COPYING file in the top-level directory.
13 from __future__ import print_function
14 import sys
15 import os
17 def get_string_struct(line):
18 data = line.split()
20 # data[0] -> struct element name
21 # data[1] -> =
22 # data[2] -> value
24 return data[2].replace('"', '')[:-1]
26 def add_module(fheader, library, format_name, protocol_name):
27 lines = []
28 lines.append('.library_name = "' + library + '",')
29 if format_name != "":
30 lines.append('.format_name = "' + format_name + '",')
31 if protocol_name != "":
32 lines.append('.protocol_name = "' + protocol_name + '",')
34 text = '\n '.join(lines)
35 fheader.write('\n {\n ' + text + '\n },')
37 def process_file(fheader, filename):
38 # This parser assumes the coding style rules are being followed
39 with open(filename, "r") as cfile:
40 found_start = False
41 library, _ = os.path.splitext(os.path.basename(filename))
42 for line in cfile:
43 if found_start:
44 line = line.replace('\n', '')
45 if line.find(".format_name") != -1:
46 format_name = get_string_struct(line)
47 elif line.find(".protocol_name") != -1:
48 protocol_name = get_string_struct(line)
49 elif line == "};":
50 add_module(fheader, library, format_name, protocol_name)
51 found_start = False
52 elif line.find("static BlockDriver") != -1:
53 found_start = True
54 format_name = ""
55 protocol_name = ""
57 def print_top(fheader):
58 fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
60 * QEMU Block Module Infrastructure
62 * Authors:
63 * Marc Mari <markmb@redhat.com>
66 ''')
68 fheader.write('''#ifndef QEMU_MODULE_BLOCK_H
69 #define QEMU_MODULE_BLOCK_H
71 #include "qemu-common.h"
73 static const struct {
74 const char *format_name;
75 const char *protocol_name;
76 const char *library_name;
77 } block_driver_modules[] = {''')
79 def print_bottom(fheader):
80 fheader.write('''
83 #endif
84 ''')
86 # First argument: output file
87 # All other arguments: modules source files (.c)
88 output_file = sys.argv[1]
89 with open(output_file, 'w') as fheader:
90 print_top(fheader)
92 for filename in sys.argv[2:]:
93 if os.path.isfile(filename):
94 process_file(fheader, filename)
95 else:
96 print("File " + filename + " does not exist.", file=sys.stderr)
97 sys.exit(1)
99 print_bottom(fheader)
101 sys.exit(0)