xref: /openbmc/qemu/scripts/modinfo-collect.py (revision 1580b897)
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        print("MODINFO_DEBUG src %s" % src)
55        command = find_command(src, target, compile_commands)
56        cmdline = process_command(src, command)
57        print("MODINFO_DEBUG cmd", cmdline)
58        result = subprocess.run(cmdline, stdout = subprocess.PIPE,
59                                universal_newlines = True)
60        if result.returncode != 0:
61            sys.exit(result.returncode)
62        for line in result.stdout.split('\n'):
63            if line.find('MODINFO') != -1:
64                print(line)
65
66if __name__ == "__main__":
67    main(sys.argv[1:])
68