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
23 #ifndef UI_OZONE_PLATFORM_LIST_H_
24 #define UI_OZONE_PLATFORM_LIST_H_
28 const int kPlatformWayland = 0;
29 const int kPlatformDri = 1;
31 extern const char *kPlatformNames[kPlatformCount];
37 #include "ui/ozone/platform_list.h"
41 const char *kPlatformNames[] = {
42 "wayland", // kPlatformWayland
43 "dri", // kPlatformDri
48 #endif // UI_OZONE_PLATFORM_LIST_H_
60 def GetConstantName(name
):
61 """Determine name of static constructor function from platform name.
63 We just capitalize the platform name and prepend "CreateOzonePlatform".
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
:
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')
85 out
.write('#ifndef UI_OZONE_PLATFORM_LIST_H_\n')
86 out
.write('#define UI_OZONE_PLATFORM_LIST_H_\n')
89 out
.write('namespace ui {\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
))
98 out
.write('const int kPlatformCount = %d;\n' % len(platforms
))
101 # Declaration for names list.
102 out
.write('extern const char* kPlatformNames[kPlatformCount];\n')
105 out
.write('} // namespace ui\n')
108 out
.write('#endif // UI_OZONE_PLATFORM_LIST_H_\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')
118 out
.write('#include "ui/ozone/platform_list.h"\n')
121 out
.write('namespace ui {\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
)))
133 out
.write('} // namespace ui\n')
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}.
155 if options
.output_cc
:
156 out_cc
= open(options
.output_cc
, 'wb')
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
:
170 if options
.output_txt
:
176 if __name__
== '__main__':
177 sys
.exit(main(sys
.argv
[1:]))