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"
18 OzonePlatform* CreateOzonePlatformDri();
19 OzonePlatform* CreateOzonePlatformWayland();
21 const OzonePlatformListEntry kOzonePlatforms[] = {
22 { "wayland", &CreateOzonePlatformWayland },
23 { "dri", &CreateOzonePlatformDri },
26 const int kOzonePlatformCount = 2;
39 def GetConstructorName(name
):
40 """Determine name of static constructor function from platform name.
42 We just capitalize the platform name and prepend "CreateOzonePlatform".
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')
54 out
.write('namespace ui {\n')
57 # Prototypes for platform initializers.
58 for platform
in platforms
:
59 out
.write('OzonePlatform* %s();\n' % GetConstructorName(platform
))
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
)))
69 out
.write('const int kOzonePlatformCount = %d;\n' % len(platforms
))
72 out
.write('} // namespace ui\n')
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.
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
:
100 if __name__
== '__main__':
101 sys
.exit(main(sys
.argv
[1:]))