aboutsummaryrefslogtreecommitdiff
path: root/scripts/modinfo-generate.py
blob: b1538fcced77e01805f6c8ff4d52b1f887a7ebc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys

def print_array(name, values):
    if len(values) == 0:
        return
    list = ", ".join(values)
    print("    .%s = ((const char*[]){ %s, NULL })," % (name, list))

def parse_line(line):
    kind = ""
    data = ""
    get_kind = False
    get_data = False
    for item in line.split():
        if item == "MODINFO_START":
            get_kind = True
            continue
        if item.startswith("MODINFO_END"):
            get_data = False
            continue
        if get_kind:
            kind = item
            get_kind = False
            get_data = True
            continue
        if get_data:
            data += " " + item
            continue
    return (kind, data)

def generate(name, lines, enabled):
    arch = ""
    objs = []
    deps = []
    opts = []
    for line in lines:
        if line.find("MODINFO_START") != -1:
            (kind, data) = parse_line(line)
            if kind == 'obj':
                objs.append(data)
            elif kind == 'dep':
                deps.append(data)
            elif kind == 'opts':
                opts.append(data)
            elif kind == 'arch':
                arch = data;
            elif kind == 'kconfig':
                # don't add a module which dependency is not enabled
                # in kconfig
                if data.strip() not in enabled:
                    print("    /* module {} isn't enabled in Kconfig. */"
                          .format(data.strip()))
                    print("/* },{ */")
                    return None
            else:
                print("unknown:", kind)
                exit(1)

    print("    .name = \"%s\"," % name)
    if arch != "":
        print("    .arch = %s," % arch)
    print_array("objs", objs)
    print_array("deps", deps)
    print_array("opts", opts)
    print("},{")
    return {dep.strip('" ') for dep in deps}

def print_pre():
    print("/* generated by scripts/modinfo-generate.py */")
    print("#include \"qemu/osdep.h\"")
    print("#include \"qemu/module.h\"")
    print("const QemuModinfo qemu_modinfo[] = {{")

def print_post():
    print("    /* end of list */")
    print("}};")

def main(args):
    if len(args) < 3 or args[0] != '--devices':
        print('Expected: modinfo-generate.py --devices '
              'config-device.mak [modinfo files]', file=sys.stderr)
        exit(1)

    # get all devices enabled in kconfig, from *-config-device.mak
    enabled = set()
    with open(args[1]) as file:
        for line in file.readlines():
            config = line.split('=')
            if config[1].rstrip() == 'y':
                enabled.add(config[0][7:]) # remove CONFIG_

    deps = set()
    modules = set()
    print_pre()
    for modinfo in args[2:]:
        with open(modinfo) as f:
            lines = f.readlines()
        print("    /* %s */" % modinfo)
        (basename, _) = os.path.splitext(modinfo)
        moddeps = generate(basename, lines, enabled)
        if moddeps is not None:
            modules.add(basename)
            deps.update(moddeps)
    print_post()

    error = False
    for dep in deps.difference(modules):
        print("Dependency {} cannot be satisfied".format(dep),
              file=sys.stderr)
        error = True

    if error:
        exit(1)

if __name__ == "__main__":
    main(sys.argv[1:])