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
71#include "qemu-common.h"
72
73static const struct {
74    const char *format_name;
75    const char *protocol_name;
76    const char *library_name;
77} block_driver_modules[] = {''')
78
79def print_bottom(fheader):
80    fheader.write('''
81};
82
83#endif
84''')
85
86# First argument: output file
87# All other arguments: modules source files (.c)
88output_file = sys.argv[1]
89with open(output_file, 'w') as fheader:
90    print_top(fheader)
91
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)
98
99    print_bottom(fheader)
100
101sys.exit(0)
102