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