Add ICU message format support
[chromium-blink-merge.git] / ui / ozone / generate_ozone_platform_list.py
blobd47c398259b97243d700df86ad4ddd88bf998387
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.
6 """Code generator for Ozone platform list.
8 This script takes as arguments a list of platform names and generates a C++
9 source file containing a list of those platforms.
11 Each platform gets an integer identifier that is used to find objects for that
12 platform (particularly constructors for platform-specific objects).
14 Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland
16 // platform_list.txt
18 wayland
19 dri
21 // platform_list.h
23 #ifndef UI_OZONE_PLATFORM_LIST_H_
24 #define UI_OZONE_PLATFORM_LIST_H_
26 namespace ui {
28 const int kPlatformWayland = 0;
29 const int kPlatformDri = 1;
31 extern const char *kPlatformNames[kPlatformCount];
33 } // namespace ui
35 // platform_list.cc
37 #include "ui/ozone/platform_list.h"
39 namespace ui {
41 const char *kPlatformNames[] = {
42 "wayland", // kPlatformWayland
43 "dri", // kPlatformDri
46 } // namespace ui
48 #endif // UI_OZONE_PLATFORM_LIST_H_
50 """
52 import optparse
53 import os
54 import collections
55 import re
56 import sys
57 import string
60 def GetConstantName(name):
61 """Determine name of static constructor function from platform name.
63 We just capitalize the platform name and prepend "CreateOzonePlatform".
64 """
66 return 'kPlatform' + string.capitalize(name)
69 def GeneratePlatformListText(out, platforms):
70 """Generate text file with list of platform names, in platform id order."""
72 for platform in platforms:
73 out.write(platform)
74 out.write('\n')
76 out.write('\n')
79 def GeneratePlatformListHeader(out, platforms):
80 """Generate ids of ozone platforms & declaration of static names array."""
82 out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
83 out.write('\n')
85 out.write('#ifndef UI_OZONE_PLATFORM_LIST_H_\n')
86 out.write('#define UI_OZONE_PLATFORM_LIST_H_\n')
87 out.write('\n')
89 out.write('namespace ui {\n')
90 out.write('\n')
92 # Prototypes for platform initializers.
93 for plat_id, plat_name in enumerate(platforms):
94 out.write('const int %s = %d;\n' % (GetConstantName(plat_name), plat_id))
95 out.write('\n')
97 # Platform count.
98 out.write('const int kPlatformCount = %d;\n' % len(platforms))
99 out.write('\n')
101 # Declaration for names list.
102 out.write('extern const char* kPlatformNames[kPlatformCount];\n')
103 out.write('\n')
105 out.write('} // namespace ui\n')
106 out.write('\n')
108 out.write('#endif // UI_OZONE_PLATFORM_LIST_H_\n')
109 out.write('\n')
112 def GeneratePlatformListSource(out, platforms):
113 """Generate static array containing a list of ozone platforms."""
115 out.write('// DO NOT MODIFY. GENERATED BY generate_ozone_platform_list.py\n')
116 out.write('\n')
118 out.write('#include "ui/ozone/platform_list.h"\n')
119 out.write('\n')
121 out.write('namespace ui {\n')
122 out.write('\n')
124 # Definition of names list.
125 out.write('const char* kPlatformNames[] = {\n')
127 # Prototypes for platform initializers.
128 for plat_name in platforms:
129 out.write(' "%s", // %s\n' % (plat_name, GetConstantName(plat_name)))
130 out.write('};\n')
131 out.write('\n')
133 out.write('} // namespace ui\n')
134 out.write('\n')
137 def main(argv):
138 parser = optparse.OptionParser()
139 parser.add_option('--output_cc')
140 parser.add_option('--output_h')
141 parser.add_option('--output_txt')
142 parser.add_option('--default')
143 options, platforms = parser.parse_args(argv)
145 # Reorder the platforms when --default is specified.
146 # The default platform must appear first in the platform list.
147 if options.default and options.default in platforms:
148 platforms.remove(options.default)
149 platforms.insert(0, options.default)
151 # Write to standard output or file specified by --output_{cc,h}.
152 out_cc = sys.stdout
153 out_h = sys.stdout
154 out_txt = sys.stdout
155 if options.output_cc:
156 out_cc = open(options.output_cc, 'wb')
157 if options.output_h:
158 out_h = open(options.output_h, 'wb')
159 if options.output_txt:
160 out_txt = open(options.output_txt, 'wb')
162 GeneratePlatformListText(out_txt, platforms)
163 GeneratePlatformListHeader(out_h, platforms)
164 GeneratePlatformListSource(out_cc, platforms)
166 if options.output_cc:
167 out_cc.close()
168 if options.output_h:
169 out_h.close()
170 if options.output_txt:
171 out_txt.close()
173 return 0
176 if __name__ == '__main__':
177 sys.exit(main(sys.argv[1:]))