2 # Copyright 2014 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 PlatformObject<> constructor list.
8 This script takes as arguments a list of platform names as a text file and
9 a list of types and generates a C++ source file containing a list of
10 the constructors for that object in platform order.
12 Example Output: ./ui/ozone/generate_constructor_list.py \
15 --export OZONE_EXPORT \
17 --typename OzonePlatform \
18 --include '"ui/ozone/ozone_platform.h"'
20 // DO NOT MODIFY. GENERATED BY generate_constructor_list.py
22 #include "ui/ozone/platform_object_internal.h"
24 #include "ui/ozone/ozone_platform.h"
28 OzonePlatform* CreateOzonePlatformTest();
29 OzonePlatform* CreateOzonePlatformDri();
35 typedef ui::OzonePlatform* (*OzonePlatformConstructor)();
37 template <> const OzonePlatformConstructor
38 PlatformConstructorList<ui::OzonePlatform>::kConstructors[] = {
39 &ui::CreateOzonePlatformTest,
40 &ui::CreateOzonePlatformDri,
43 template class OZONE_EXPORT PlatformObject<ui::OzonePlatform>;
56 def GetTypedefName(typename
):
57 """Determine typedef name of constructor for typename.
59 This is just typename + "Constructor".
62 return typename
+ 'Constructor'
65 def GetConstructorName(typename
, platform
):
66 """Determine name of static constructor function from platform name.
68 This is just "Create" + typename + platform.
71 return 'Create' + typename
+ string
.capitalize(platform
)
74 def GenerateConstructorList(out
, namespace
, export
, typenames
, platforms
,
76 """Generate static array containing a list of constructors."""
78 out
.write('// DO NOT MODIFY. GENERATED BY generate_constructor_list.py\n')
81 out
.write('#include "ui/ozone/platform_object_internal.h"\n')
84 for include
in includes
:
85 out
.write('#include %(include)s\n' % {'include': include
})
88 out
.write('namespace %(namespace)s {\n' % {'namespace': namespace
})
91 # Declarations of constructor functions.
92 for typename
in typenames
:
93 for platform
in platforms
:
94 constructor
= GetConstructorName(typename
, platform
)
95 out
.write('%(typename)s* %(constructor)s();\n'
96 % {'typename': typename
,
97 'constructor': constructor
})
100 out
.write('} // namespace %(namespace)s\n' % {'namespace': namespace
})
103 out
.write('namespace ui {\n')
106 # Handy typedefs for constructor types.
107 for typename
in typenames
:
108 out
.write('typedef %(typename)s* (*%(typedef)s)();\n'
109 % {'typename': namespace
+ '::' + typename
,
110 'typedef': GetTypedefName(typename
)})
113 # The actual constructor lists.
114 for typename
in typenames
:
115 out
.write('template <> const %(typedef)s\n'
116 % {'typedef': GetTypedefName(typename
)})
117 out
.write('PlatformConstructorList<%(typename)s>::kConstructors[] = {\n'
118 % {'typename': namespace
+ '::' + typename
})
119 for platform
in platforms
:
120 constructor
= GetConstructorName(typename
, platform
)
121 out
.write(' &%(namespace)s::%(constructor)s,\n'
122 % {'namespace': namespace
, 'constructor': constructor
})
126 # Exported template instantiation.
127 for typename
in typenames
:
128 out
.write('template class %(export)s PlatformObject<%(typename)s>;\n'
129 % {'export': export
, 'typename': namespace
+ '::' + typename
})
132 out
.write('} // namespace ui\n')
137 parser
= optparse
.OptionParser()
138 parser
.add_option('--namespace', default
='ozone')
139 parser
.add_option('--export', default
='OZONE_EXPORT')
140 parser
.add_option('--platform_list')
141 parser
.add_option('--output_cc')
142 parser
.add_option('--include', action
='append', default
=[])
143 parser
.add_option('--platform', action
='append', default
=[])
144 parser
.add_option('--typename', action
='append', default
=[])
145 options
, _
= parser
.parse_args(argv
)
147 platforms
= list(options
.platform
)
148 typenames
= list(options
.typename
)
149 includes
= list(options
.include
)
151 if options
.platform_list
:
152 platforms
= open(options
.platform_list
, 'r').read().strip().split('\n')
155 sys
.stderr
.write('No platforms are selected!')
158 # Write to standard output or file specified by --output_cc.
160 if options
.output_cc
:
161 out_cc
= open(options
.output_cc
, 'wb')
163 GenerateConstructorList(out_cc
, options
.namespace
, options
.export
,
164 typenames
, platforms
, includes
)
166 if options
.output_cc
:
172 if __name__
== '__main__':
173 sys
.exit(main(sys
.argv
[1:]))