2
0

qemu-plugin-symbols.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Extract QEMU Plugin API symbols from a header file
  5. #
  6. # Copyright 2024 Linaro Ltd
  7. #
  8. # Author: Pierrick Bouvier <pierrick.bouvier@linaro.org>
  9. #
  10. # This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. # See the COPYING file in the top-level directory.
  12. #
  13. # SPDX-License-Identifier: GPL-2.0-or-later
  14. import argparse
  15. import re
  16. def extract_symbols(plugin_header):
  17. with open(plugin_header) as file:
  18. content = file.read()
  19. # Remove QEMU_PLUGIN_API macro definition.
  20. content = content.replace('#define QEMU_PLUGIN_API', '')
  21. expected = content.count('QEMU_PLUGIN_API')
  22. # Find last word between QEMU_PLUGIN_API and (, matching on several lines.
  23. # We use *? non-greedy quantifier.
  24. syms = re.findall(r'QEMU_PLUGIN_API.*?(\w+)\s*\(', content, re.DOTALL)
  25. syms.sort()
  26. # Ensure we found as many symbols as API markers.
  27. assert len(syms) == expected
  28. return syms
  29. def main() -> None:
  30. parser = argparse.ArgumentParser(description='Extract QEMU plugin symbols')
  31. parser.add_argument('plugin_header', help='Path to QEMU plugin header.')
  32. args = parser.parse_args()
  33. syms = extract_symbols(args.plugin_header)
  34. print('{')
  35. for s in syms:
  36. print(" {};".format(s))
  37. print('};')
  38. if __name__ == '__main__':
  39. main()