xref: /openbmc/qemu/scripts/modinfo-collect.py (revision f279852b)
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    out = []
22    for item in shlex.split(command):
23        if skip:
24            skip = False
25            continue
26        if item == '-MF' or item == '-MQ' or item == '-o':
27            skip = True
28            continue
29        if item == '-c':
30            skip = True
31            continue
32        out.append(item)
33    out.append('-DQEMU_MODINFO')
34    out.append('-E')
35    out.append(src)
36    return out
37
38def main(args):
39    target = ''
40    if args[0] == '--target':
41        args.pop(0)
42        target = args.pop(0)
43        print("MODINFO_DEBUG target %s" % target)
44        arch = target[:-8] # cut '-softmmu'
45        print("MODINFO_START arch \"%s\" MODINFO_END" % arch)
46    with open('compile_commands.json') as f:
47        compile_commands = json.load(f)
48    for src in args:
49        if not src.endswith('.c'):
50            print("MODINFO_DEBUG skip %s" % src)
51            continue
52        print("MODINFO_DEBUG src %s" % src)
53        command = find_command(src, target, compile_commands)
54        cmdline = process_command(src, command)
55        print("MODINFO_DEBUG cmd", cmdline)
56        result = subprocess.run(cmdline, stdout = subprocess.PIPE,
57                                universal_newlines = True)
58        if result.returncode != 0:
59            sys.exit(result.returncode)
60        for line in result.stdout.split('\n'):
61            if line.find('MODINFO') != -1:
62                print(line)
63
64if __name__ == "__main__":
65    main(sys.argv[1:])
66