xref: /qemu/scripts/modules/module_block.py (revision ab9056ff)
1#!/usr/bin/python
2#
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
13from __future__ import print_function
14import sys
15import os
16
17def get_string_struct(line):
18    data = line.split()
19
20    # data[0] -> struct element name
21    # data[1] -> =
22    # data[2] -> value
23
24    return data[2].replace('"', '')[:-1]
25
26def 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 + '",')
33
34    text = '\n        '.join(lines)
35    fheader.write('\n    {\n        ' + text + '\n    },')
36
37def 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 = ""
56
57def print_top(fheader):
58    fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
59/*
60 * QEMU Block Module Infrastructure
61 *
62 * Authors:
63 *  Marc Mari       <markmb@redhat.com>
64 */
65
66''')
67
68    fheader.write('''#ifndef QEMU_MODULE_BLOCK_H
69#define QEMU_MODULE_BLOCK_H
70
71static const struct {
72    const char *format_name;
73    const char *protocol_name;
74    const char *library_name;
75} block_driver_modules[] = {''')
76
77def print_bottom(fheader):
78    fheader.write('''
79};
80
81#endif
82''')
83
84# First argument: output file
85# All other arguments: modules source files (.c)
86output_file = sys.argv[1]
87with open(output_file, 'w') as fheader:
88    print_top(fheader)
89
90    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)
96
97    print_bottom(fheader)
98
99sys.exit(0)
100