modinfo-collect.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. arg = False
  19. out = []
  20. for item in shlex.split(command):
  21. if arg:
  22. out.append(x)
  23. arg = False
  24. continue
  25. if skip:
  26. skip = False
  27. continue
  28. if item == '-MF' or item == '-MQ' or item == '-o':
  29. skip = True
  30. continue
  31. if item == '-c':
  32. skip = True
  33. continue
  34. out.append(item)
  35. out.append('-DQEMU_MODINFO')
  36. out.append('-E')
  37. out.append(src)
  38. return out
  39. def main(args):
  40. target = ''
  41. if args[0] == '--target':
  42. args.pop(0)
  43. target = args.pop(0)
  44. print("MODINFO_DEBUG target %s" % target)
  45. arch = target[:-8] # cut '-softmmu'
  46. print("MODINFO_START arch \"%s\" MODINFO_END" % arch)
  47. with open('compile_commands.json') as f:
  48. compile_commands = json.load(f)
  49. for src in args:
  50. print("MODINFO_DEBUG src %s" % src)
  51. command = find_command(src, target, compile_commands)
  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. if __name__ == "__main__":
  62. main(sys.argv[1:])