blob: 1109df827d5f222fe78463379c6a7ff0db08e1c6 [file] [log] [blame]
Philippe Mathieu-Daudé5aa62802020-05-12 12:32:37 +02001#!/usr/bin/env python3
Marc Mari0c0c1fd2016-08-12 09:27:02 -04002#
3# Module information generator
4#
5# Copyright Red Hat, Inc. 2015 - 2016
6#
7# Authors:
8# Marc Mari <markmb@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2.
11# See the COPYING file in the top-level directory.
12
Marc Mari0c0c1fd2016-08-12 09:27:02 -040013import sys
14import os
15
16def get_string_struct(line):
17 data = line.split()
18
19 # data[0] -> struct element name
20 # data[1] -> =
21 # data[2] -> value
22
23 return data[2].replace('"', '')[:-1]
24
25def add_module(fheader, library, format_name, protocol_name):
26 lines = []
27 lines.append('.library_name = "' + library + '",')
28 if format_name != "":
29 lines.append('.format_name = "' + format_name + '",')
30 if protocol_name != "":
31 lines.append('.protocol_name = "' + protocol_name + '",')
32
33 text = '\n '.join(lines)
34 fheader.write('\n {\n ' + text + '\n },')
35
36def process_file(fheader, filename):
37 # This parser assumes the coding style rules are being followed
38 with open(filename, "r") as cfile:
Marc Mari0c0c1fd2016-08-12 09:27:02 -040039 found_start = False
40 library, _ = os.path.splitext(os.path.basename(filename))
41 for line in cfile:
42 if found_start:
43 line = line.replace('\n', '')
44 if line.find(".format_name") != -1:
45 format_name = get_string_struct(line)
46 elif line.find(".protocol_name") != -1:
47 protocol_name = get_string_struct(line)
48 elif line == "};":
49 add_module(fheader, library, format_name, protocol_name)
50 found_start = False
51 elif line.find("static BlockDriver") != -1:
Marc Mari0c0c1fd2016-08-12 09:27:02 -040052 found_start = True
53 format_name = ""
54 protocol_name = ""
55
Marc Mari0c0c1fd2016-08-12 09:27:02 -040056def print_top(fheader):
57 fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
58/*
59 * QEMU Block Module Infrastructure
60 *
61 * Authors:
62 * Marc Mari <markmb@redhat.com>
63 */
64
65''')
66
67 fheader.write('''#ifndef QEMU_MODULE_BLOCK_H
68#define QEMU_MODULE_BLOCK_H
69
Marc Mari0c0c1fd2016-08-12 09:27:02 -040070static const struct {
71 const char *format_name;
72 const char *protocol_name;
73 const char *library_name;
74} block_driver_modules[] = {''')
75
76def print_bottom(fheader):
77 fheader.write('''
78};
79
80#endif
81''')
82
Philippe Mathieu-Daudé5aa62802020-05-12 12:32:37 +020083if __name__ == '__main__':
84 # First argument: output file
85 # All other arguments: modules source files (.c)
86 output_file = sys.argv[1]
87 with open(output_file, 'w') as fheader:
88 print_top(fheader)
Marc Mari0c0c1fd2016-08-12 09:27:02 -040089
Philippe Mathieu-Daudé5aa62802020-05-12 12:32:37 +020090 for filename in sys.argv[2:]:
91 if os.path.isfile(filename):
92 process_file(fheader, filename)
93 else:
94 print("File " + filename + " does not exist.", file=sys.stderr)
95 sys.exit(1)
Marc Mari0c0c1fd2016-08-12 09:27:02 -040096
Philippe Mathieu-Daudé5aa62802020-05-12 12:32:37 +020097 print_bottom(fheader)
Marc Mari0c0c1fd2016-08-12 09:27:02 -040098
Philippe Mathieu-Daudé5aa62802020-05-12 12:32:37 +020099 sys.exit(0)