Blink roll 174234:174238
[chromium-blink-merge.git] / ui / ozone / generate_ozone_platform_list.py
blobdf85a1eab92c136eb5d714d5968c14f53d5c1284
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. Each list entry contains the
10 name and a function pointer to the initializer for that platform.
12 Example Output: ./generate_ozone_platform_list.py --default wayland dri wayland
14 #include "ui/ozone/ozone_platform_list.h"
16 namespace ui {
18 OzonePlatform* CreateOzonePlatformDri();
19 OzonePlatform* CreateOzonePlatformWayland();
21 const OzonePlatformListEntry kOzonePlatforms[] = {
22 { "wayland", &CreateOzonePlatformWayland },
23 { "dri", &CreateOzonePlatformDri },
26 const int kOzonePlatformCount = 2;
28 } // namespace ui
29 """
31 import optparse
32 import os
33 import collections
34 import re
35 import sys
36 import string
39 def GetConstructorName(name):
40 """Determine name of static constructor function from platform name.
42 We just capitalize the platform name and prepend "CreateOzonePlatform".
43 """
45 return 'CreateOzonePlatform' + string.capitalize(name)
48 def GeneratePlatformList(out, platforms):
49 """Generate static array containing a list of ozone platforms."""
51 out.write('#include "ui/ozone/ozone_platform_list.h"\n')
52 out.write('\n')
54 out.write('namespace ui {\n')
55 out.write('\n')
57 # Prototypes for platform initializers.
58 for platform in platforms:
59 out.write('OzonePlatform* %s();\n' % GetConstructorName(platform))
60 out.write('\n')
62 # List of platform names and initializers.
63 out.write('const OzonePlatformListEntry kOzonePlatforms[] = {\n')
64 for platform in platforms:
65 out.write(' { "%s", &%s },\n' % (platform, GetConstructorName(platform)))
66 out.write('};\n')
67 out.write('\n')
69 out.write('const int kOzonePlatformCount = %d;\n' % len(platforms))
70 out.write('\n')
72 out.write('} // namespace ui\n')
75 def main(argv):
76 parser = optparse.OptionParser()
77 parser.add_option('--output_file')
78 parser.add_option('--default')
79 options, platforms = parser.parse_args(argv)
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')
86 # Reorder the platforms when --default is specified.
87 # The default platform must appear first in the platform list.
88 if options.default and options.default in platforms:
89 platforms.remove(options.default)
90 platforms.insert(0, options.default)
92 GeneratePlatformList(out, platforms)
94 if options.output_file:
95 out.close()
97 return 0
100 if __name__ == '__main__':
101 sys.exit(main(sys.argv[1:]))