blob: 48bd92bd61808857cb84b9a0c1e1eb3a0694e1b2 [file] [log] [blame]
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +02001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4import os
5import sys
6import json
7import shlex
8import subprocess
9
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020010def process_command(src, command):
11 skip = False
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020012 out = []
13 for item in shlex.split(command):
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020014 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)
Paolo Bonzini95d31312024-05-24 11:26:54 +020037
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020038 with open('compile_commands.json') as f:
Paolo Bonzini95d31312024-05-24 11:26:54 +020039 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']
Marc-André Lureau20f19712021-07-15 11:54:13 +040048 if not src.endswith('.c'):
49 print("MODINFO_DEBUG skip %s" % src)
50 continue
Paolo Bonzini95d31312024-05-24 11:26:54 +020051 command = entry['command']
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020052 print("MODINFO_DEBUG src %s" % src)
Gerd Hoffmannf5723ab2021-06-24 12:38:04 +020053 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:])