xref: /openbmc/qemu/scripts/modinfo-collect.py (revision 0fbb5d2d)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4import os
5import sys
6import json
7import shlex
8import subprocess
9
10def find_command(src, target, compile_commands):
11    for command in compile_commands:
12        if command['file'] != src:
13            continue
14        if target != '' and command['command'].find(target) == -1:
15            continue
16        return command['command']
17    return 'false'
18
19def process_command(src, command):
20    skip = False
21    arg = False
22    out = []
23    for item in shlex.split(command):
24        if arg:
25            out.append(x)
26            arg = False
27            continue
28        if skip:
29            skip = False
30            continue
31        if item == '-MF' or item == '-MQ' or item == '-o':
32            skip = True
33            continue
34        if item == '-c':
35            skip = True
36            continue
37        out.append(item)
38    out.append('-DQEMU_MODINFO')
39    out.append('-E')
40    out.append(src)
41    return out
42
43def main(args):
44    target = ''
45    if args[0] == '--target':
46        args.pop(0)
47        target = args.pop(0)
48        print("MODINFO_DEBUG target %s" % target)
49        arch = target[:-8] # cut '-softmmu'
50        print("MODINFO_START arch \"%s\" MODINFO_END" % arch)
51    with open('compile_commands.json') as f:
52        compile_commands = json.load(f)
53    for src in args:
54        if not src.endswith('.c'):
55            print("MODINFO_DEBUG skip %s" % src)
56            continue
57        print("MODINFO_DEBUG src %s" % src)
58        command = find_command(src, target, compile_commands)
59        cmdline = process_command(src, command)
60        print("MODINFO_DEBUG cmd", cmdline)
61        result = subprocess.run(cmdline, stdout = subprocess.PIPE,
62                                universal_newlines = True)
63        if result.returncode != 0:
64            sys.exit(result.returncode)
65        for line in result.stdout.split('\n'):
66            if line.find('MODINFO') != -1:
67                print(line)
68
69if __name__ == "__main__":
70    main(sys.argv[1:])
71