generate_ozone_platform_list.py revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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
6"""Code generator for Ozone platform list.
7
8This script takes as arguments a list of platform names and generates a C++
9source file containing a list of those platforms.
10
11Each platform gets an integer identifier that is used to find objects for that
12platform (particularly constructors for platform-specific objects).
13
14Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland
15
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_
25
26  namespace ui {
27
28  const int kPlatformWayland = 0;
29  const int kPlatformDri = 1;
30
31  extern const char *kPlatformNames[kPlatformCount];
32
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
44  };
45
46  }  // namespace ui
47
48  #endif  // UI_OZONE_PLATFORM_LIST_H_
49
50"""
51
52import optparse
53import os
54import collections
55import re
56import sys
57import string
58
59
60def GetConstantName(name):
61  """Determine name of static constructor function from platform name.
62
63  We just capitalize the platform name and prepend "CreateOzonePlatform".
64  """
65
66  return 'kPlatform' + string.capitalize(name)
67
68
69def GeneratePlatformListText(out, platforms):
70  """Generate text file with list of platform names, in platform id order."""
71
72  for platform in platforms:
73    out.write(platform)
74    out.write('\n')
75
76  out.write('\n')
77
78
79def 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')
87  out.write('\n')
88
89  out.write('namespace ui {\n')
90  out.write('\n')
91
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')
96
97  # Platform count.
98  out.write('const int kPlatformCount = %d;\n' % len(platforms))
99  out.write('\n')
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
112def 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)))
130  out.write('};\n')
131  out.write('\n')
132
133  out.write('}  // namespace ui\n')
134  out.write('\n')
135
136
137def 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)
144
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)
150
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')
161
162  GeneratePlatformListText(out_txt, platforms)
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()
172
173  return 0
174
175
176if __name__ == '__main__':
177  sys.exit(main(sys.argv[1:]))
178