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