2
0

module_block.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  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. import sys
  13. import os
  14. def get_string_struct(line):
  15. data = line.split()
  16. # data[0] -> struct element name
  17. # data[1] -> =
  18. # data[2] -> value
  19. return data[2].replace('"', '')[:-1]
  20. def add_module(fheader, library, format_name, protocol_name):
  21. lines = []
  22. lines.append('.library_name = "' + library + '",')
  23. if format_name != "":
  24. lines.append('.format_name = "' + format_name + '",')
  25. if protocol_name != "":
  26. lines.append('.protocol_name = "' + protocol_name + '",')
  27. text = '\n '.join(lines)
  28. fheader.write('\n {\n ' + text + '\n },')
  29. def process_file(fheader, filename):
  30. # This parser assumes the coding style rules are being followed
  31. with open(filename, "r") as cfile:
  32. found_start = False
  33. library, _ = os.path.splitext(os.path.basename(filename))
  34. for line in cfile:
  35. if found_start:
  36. line = line.replace('\n', '')
  37. if line.find(".format_name") != -1:
  38. format_name = get_string_struct(line)
  39. elif line.find(".protocol_name") != -1:
  40. protocol_name = get_string_struct(line)
  41. elif line == "};":
  42. add_module(fheader, library, format_name, protocol_name)
  43. found_start = False
  44. elif line.find("static BlockDriver") != -1:
  45. found_start = True
  46. format_name = ""
  47. protocol_name = ""
  48. def print_top(fheader):
  49. fheader.write('''/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  50. /*
  51. * QEMU Block Module Infrastructure
  52. *
  53. * Authors:
  54. * Marc Mari <markmb@redhat.com>
  55. */
  56. ''')
  57. fheader.write('''#ifndef QEMU_MODULE_BLOCK_H
  58. #define QEMU_MODULE_BLOCK_H
  59. static const struct {
  60. const char *format_name;
  61. const char *protocol_name;
  62. const char *library_name;
  63. } block_driver_modules[] = {''')
  64. def print_bottom(fheader):
  65. fheader.write('''
  66. };
  67. #endif
  68. ''')
  69. if __name__ == '__main__':
  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)