generate_ozone_platform_list.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python
  2. # Copyright 2013 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Code generator for Ozone platform list.
  6. This script takes as arguments a list of platform names and generates a C++
  7. source file containing a list of those platforms.
  8. Each platform gets an integer identifier that is used to find objects for that
  9. platform (particularly constructors for platform-specific objects).
  10. Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland
  11. // platform_list.txt
  12. wayland
  13. dri
  14. // platform_list.h
  15. #ifndef UI_OZONE_PLATFORM_LIST_H_
  16. #define UI_OZONE_PLATFORM_LIST_H_
  17. namespace ui {
  18. const int kPlatformWayland = 0;
  19. const int kPlatformDri = 1;
  20. extern const char *kPlatformNames[kPlatformCount];
  21. } // namespace ui
  22. // platform_list.cc
  23. #include "ui/ozone/platform_list.h"
  24. namespace ui {
  25. const char *kPlatformNames[] = {
  26. "wayland", // kPlatformWayland
  27. "dri", // kPlatformDri
  28. };
  29. } // namespace ui
  30. #endif // UI_OZONE_PLATFORM_LIST_H_
  31. """
  32. try:
  33. from StringIO import StringIO # for Python 2
  34. except ImportError:
  35. from io import StringIO # for Python 3
  36. import optparse
  37. import os
  38. import collections
  39. import re
  40. import sys
  41. def GetConstantName(name):
  42. """Determine name of static constructor function from platform name.
  43. We just capitalize the platform name and prepend "CreateOzonePlatform".
  44. """
  45. return 'kPlatform' + name.capitalize()
  46. def GeneratePlatformListText(out, platforms):
  47. """Generate text file with list of platform names, in platform id order."""
  48. for platform in platforms:
  49. out.write(platform)
  50. out.write('\n')
  51. out.write('\n')
  52. def GeneratePlatformListHeader(out, platforms):
  53. """Generate ids of ozone platforms & declaration of static names array."""
  54. out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
  55. out.write('\n')
  56. out.write('#ifndef UI_OZONE_PLATFORM_LIST_H_\n')
  57. out.write('#define UI_OZONE_PLATFORM_LIST_H_\n')
  58. out.write('\n')
  59. out.write('namespace ui {\n')
  60. out.write('\n')
  61. # Prototypes for platform initializers.
  62. for plat_id, plat_name in enumerate(platforms):
  63. out.write('const int %s = %d;\n' % (GetConstantName(plat_name), plat_id))
  64. out.write('\n')
  65. # Platform count.
  66. out.write('const int kPlatformCount = %d;\n' % len(platforms))
  67. out.write('\n')
  68. # Declaration for names list.
  69. out.write('extern const char* kPlatformNames[kPlatformCount];\n')
  70. out.write('\n')
  71. out.write('} // namespace ui\n')
  72. out.write('\n')
  73. out.write('#endif // UI_OZONE_PLATFORM_LIST_H_\n')
  74. out.write('\n')
  75. def GeneratePlatformListSource(out, platforms):
  76. """Generate static array containing a list of ozone platforms."""
  77. out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
  78. out.write('\n')
  79. out.write('#include "ui/ozone/platform_list.h"\n')
  80. out.write('\n')
  81. out.write('namespace ui {\n')
  82. out.write('\n')
  83. # Definition of names list.
  84. out.write('const char* kPlatformNames[] = {\n')
  85. # Prototypes for platform initializers.
  86. for plat_name in platforms:
  87. out.write(' "%s", // %s\n' % (plat_name, GetConstantName(plat_name)))
  88. out.write('};\n')
  89. out.write('\n')
  90. out.write('} // namespace ui\n')
  91. out.write('\n')
  92. def main(argv):
  93. parser = optparse.OptionParser()
  94. parser.add_option('--output_cc')
  95. parser.add_option('--output_h')
  96. parser.add_option('--output_txt')
  97. parser.add_option('--default')
  98. options, platforms = parser.parse_args(argv)
  99. # Reorder the platforms when --default is specified.
  100. # The default platform must appear first in the platform list.
  101. if options.default and options.default in platforms:
  102. platforms.remove(options.default)
  103. platforms.insert(0, options.default)
  104. # Write to standard output or file specified by --output_{cc,h}.
  105. out_cc = getattr(sys.stdout, 'buffer', sys.stdout)
  106. out_h = getattr(sys.stdout, 'buffer', sys.stdout)
  107. out_txt = getattr(sys.stdout, 'buffer', sys.stdout)
  108. if options.output_cc:
  109. out_cc = open(options.output_cc, 'wb')
  110. if options.output_h:
  111. out_h = open(options.output_h, 'wb')
  112. if options.output_txt:
  113. out_txt = open(options.output_txt, 'wb')
  114. out_txt_str = StringIO()
  115. out_h_str = StringIO()
  116. out_cc_str = StringIO()
  117. GeneratePlatformListText(out_txt_str, platforms)
  118. out_txt.write(out_txt_str.getvalue().encode('utf-8'))
  119. GeneratePlatformListHeader(out_h_str, platforms)
  120. out_h.write(out_h_str.getvalue().encode('utf-8'))
  121. GeneratePlatformListSource(out_cc_str, platforms)
  122. out_cc.write(out_cc_str.getvalue().encode('utf-8'))
  123. if options.output_cc:
  124. out_cc.close()
  125. if options.output_h:
  126. out_h.close()
  127. if options.output_txt:
  128. out_txt.close()
  129. return 0
  130. if __name__ == '__main__':
  131. sys.exit(main(sys.argv[1:]))