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