2
0

modinfo-collect.py 1.9 KB

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