module_block.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/python
  2. #
  3. # Module information generator
  4. #
  5. # Copyright Red Hat, Inc. 2015 - 2016
  6. #
  7. # Authors:
  8. # Marc Mari <markmb@redhat.com>
  9. #
  10. # This work is licensed under the terms of the GNU GPL, version 2.
  11. # See the COPYING file in the top-level directory.
  12. from __future__ import print_function
  13. import sys
  14. import os
  15. def get_string_struct(line):
  16. data = line.split()
  17. # data[0] -> struct element name
  18. # data[1] -> =
  19. # data[2] -> value
  20. return data[2].replace('"', '')[:-1]
  21. def add_module(fheader, library, format_name, protocol_name):
  22. lines = []
  23. lines.append('.library_name = "' + library + '",')
  24. if format_name != "":
  25. lines.append('.format_name = "' + format_name + '",')
  26. if protocol_name != "":
  27. lines.append('.protocol_name = "' + protocol_name + '",')
  28. text = '\n '.join(lines)
  29. fheader.write('\n {\n ' + text + '\n },')
  30. def process_file(fheader, filename):
  31. # This parser assumes the coding style rules are being followed
  32. with open(filename, "r") as cfile:
  33. found_start = False
  34. library, _ = os.path.splitext(os.path.basename(filename))
  35. for line in cfile:
  36. if found_start:
  37. line = line.replace('\n', '')
  38. if line.find(".format_name") != -1:
  39. format_name = get_string_struct(line)
  40. elif line.find(".protocol_name") != -1:
  41. protocol_name = get_string_struct(line)
  42. elif line == "};":
  43. add_module(fheader, library, format_name, protocol_name)
  44. found_start = False
  45. elif line.find("static BlockDriver") != -1:
  46. found_start = True
  47. format_name = ""
  48. protocol_name = ""
  49. def print_top(fheader):
  50. fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  51. /*
  52. * QEMU Block Module Infrastructure
  53. *
  54. * Authors:
  55. * Marc Mari <markmb@redhat.com>
  56. */
  57. ''')
  58. fheader.write('''#ifndef QEMU_MODULE_BLOCK_H
  59. #define QEMU_MODULE_BLOCK_H
  60. static const struct {
  61. const char *format_name;
  62. const char *protocol_name;
  63. const char *library_name;
  64. } block_driver_modules[] = {''')
  65. def print_bottom(fheader):
  66. fheader.write('''
  67. };
  68. #endif
  69. ''')
  70. # First argument: output file
  71. # All other arguments: modules source files (.c)
  72. output_file = sys.argv[1]
  73. with open(output_file, 'w') as fheader:
  74. print_top(fheader)
  75. for filename in sys.argv[2:]:
  76. if os.path.isfile(filename):
  77. process_file(fheader, filename)
  78. else:
  79. print("File " + filename + " does not exist.", file=sys.stderr)
  80. sys.exit(1)
  81. print_bottom(fheader)
  82. sys.exit(0)