2 # -*- coding: utf-8 -*-
10 def find_command(src
, target
, compile_commands
):
11 for command
in compile_commands
:
12 if command
['file'] != src
:
14 if target
!= '' and command
['command'].find(target
) == -1:
16 return command
['command']
19 def process_command(src
, command
):
23 for item
in shlex
.split(command
):
31 if item
== '-MF' or item
== '-MQ' or item
== '-o':
38 out
.append('-DQEMU_MODINFO')
45 if args
[0] == '--target':
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
)
54 if not src
.endswith('.c'):
55 print("MODINFO_DEBUG skip %s" % src
)
57 print("MODINFO_DEBUG src %s" % src
)
58 command
= find_command(src
, target
, compile_commands
)
59 cmdline
= process_command(src
, command
)
60 print("MODINFO_DEBUG cmd", cmdline
)
61 result
= subprocess
.run(cmdline
, stdout
= subprocess
.PIPE
,
62 universal_newlines
= True)
63 if result
.returncode
!= 0:
64 sys
.exit(result
.returncode
)
65 for line
in result
.stdout
.split('\n'):
66 if line
.find('MODINFO') != -1:
69 if __name__
== "__main__":