vulkan: make generated enum to strings helpers available from c++
[mesa.git] / src / vulkan / util / gen_enum_to_str.py
1 # encoding=utf-8
2 # Copyright © 2017 Intel Corporation
3
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
10
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
13
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21
22 """Create enum to string functions for vulkan using vk.xml."""
23
24 from __future__ import print_function
25 import argparse
26 import os
27 import textwrap
28 import xml.etree.cElementTree as et
29
30 from mako.template import Template
31
32 COPYRIGHT = textwrap.dedent(u"""\
33 * Copyright © 2017 Intel Corporation
34 *
35 * Permission is hereby granted, free of charge, to any person obtaining a copy
36 * of this software and associated documentation files (the "Software"), to deal
37 * in the Software without restriction, including without limitation the rights
38 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39 * copies of the Software, and to permit persons to whom the Software is
40 * furnished to do so, subject to the following conditions:
41 *
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
51 * SOFTWARE.""")
52
53 C_TEMPLATE = Template(textwrap.dedent(u"""\
54 /* Autogenerated file -- do not edit
55 * generated by ${file}
56 *
57 ${copyright}
58 */
59
60 #include <vulkan/vulkan.h>
61 #include <vulkan/vk_android_native_buffer.h>
62 #include "util/macros.h"
63 #include "vk_enum_to_str.h"
64
65 % for enum in enums:
66
67 const char *
68 vk_${enum.name[2:]}_to_str(${enum.name} input)
69 {
70 switch(input) {
71 % for v in sorted(enum.values.keys()):
72 % if enum.values[v] in FOREIGN_ENUM_VALUES:
73
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Wswitch"
76 % endif
77 case ${v}:
78 return "${enum.values[v]}";
79 % if enum.values[v] in FOREIGN_ENUM_VALUES:
80 #pragma GCC diagnostic pop
81
82 % endif
83 % endfor
84 default:
85 unreachable("Undefined enum value.");
86 }
87 }
88 %endfor"""),
89 output_encoding='utf-8')
90
91 H_TEMPLATE = Template(textwrap.dedent(u"""\
92 /* Autogenerated file -- do not edit
93 * generated by ${file}
94 *
95 ${copyright}
96 */
97
98 #ifndef MESA_VK_ENUM_TO_STR_H
99 #define MESA_VK_ENUM_TO_STR_H
100
101 #include <vulkan/vulkan.h>
102 #include <vulkan/vk_android_native_buffer.h>
103
104 #ifdef __cplusplus
105 extern "C" {
106 #endif
107
108 % for ext in extensions:
109 #define _${ext.name}_number (${ext.number})
110 % endfor
111
112 % for enum in enums:
113 const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
114 % endfor
115
116 #ifdef __cplusplus
117 } /* extern "C" */
118 #endif
119
120 #endif"""),
121 output_encoding='utf-8')
122
123 # These enums are defined outside their respective enum blocks, and thus cause
124 # -Wswitch warnings.
125 FOREIGN_ENUM_VALUES = [
126 "VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID",
127 ]
128
129
130 class NamedFactory(object):
131 """Factory for creating enums."""
132
133 def __init__(self, type_):
134 self.registry = {}
135 self.type = type_
136
137 def __call__(self, name, **kwargs):
138 try:
139 return self.registry[name]
140 except KeyError:
141 n = self.registry[name] = self.type(name, **kwargs)
142 return n
143
144 def get(self, name):
145 return self.registry.get(name)
146
147
148 class VkExtension(object):
149 """Simple struct-like class representing extensions"""
150
151 def __init__(self, name, number=None):
152 self.name = name
153 self.number = number
154
155
156 class VkEnum(object):
157 """Simple struct-like class representing a single Vulkan Enum."""
158
159 def __init__(self, name, values=None):
160 self.name = name
161 # Maps numbers to names
162 self.values = values or dict()
163 self.name_to_value = dict()
164
165 def add_value(self, name, value=None,
166 extnum=None, offset=None,
167 error=False):
168 assert value is not None or extnum is not None
169 if value is None:
170 value = 1000000000 + (extnum - 1) * 1000 + offset
171 if error:
172 value = -value
173
174 self.name_to_value[name] = value
175 if value not in self.values:
176 self.values[value] = name
177 elif len(self.values[value]) > len(name):
178 self.values[value] = name
179
180 def add_value_from_xml(self, elem, extension=None):
181 if 'value' in elem.attrib:
182 self.add_value(elem.attrib['name'],
183 value=int(elem.attrib['value'], base=0))
184 elif 'alias' in elem.attrib:
185 self.add_value(elem.attrib['name'],
186 value=self.name_to_value[elem.attrib['alias']])
187 else:
188 error = 'dir' in elem.attrib and elem.attrib['dir'] == '-'
189 if 'extnumber' in elem.attrib:
190 extnum = int(elem.attrib['extnumber'])
191 else:
192 extnum = extension.number
193 self.add_value(elem.attrib['name'],
194 extnum=extnum,
195 offset=int(elem.attrib['offset']),
196 error=error)
197
198
199 def parse_xml(enum_factory, ext_factory, filename):
200 """Parse the XML file. Accumulate results into the factories.
201
202 This parser is a memory efficient iterative XML parser that returns a list
203 of VkEnum objects.
204 """
205
206 xml = et.parse(filename)
207
208 for enum_type in xml.findall('./enums[@type="enum"]'):
209 enum = enum_factory(enum_type.attrib['name'])
210 for value in enum_type.findall('./enum'):
211 enum.add_value_from_xml(value)
212
213 for value in xml.findall('./feature/require/enum[@extends]'):
214 enum = enum_factory.get(value.attrib['extends'])
215 if enum is not None:
216 enum.add_value_from_xml(value)
217
218 for ext_elem in xml.findall('./extensions/extension[@supported="vulkan"]'):
219 extension = ext_factory(ext_elem.attrib['name'],
220 number=int(ext_elem.attrib['number']))
221
222 for value in ext_elem.findall('./require/enum[@extends]'):
223 enum = enum_factory.get(value.attrib['extends'])
224 if enum is not None:
225 enum.add_value_from_xml(value, extension)
226
227
228 def main():
229 parser = argparse.ArgumentParser()
230 parser.add_argument('--xml', required=True,
231 help='Vulkan API XML files',
232 action='append',
233 dest='xml_files')
234 parser.add_argument('--outdir',
235 help='Directory to put the generated files in',
236 required=True)
237
238 args = parser.parse_args()
239
240 enum_factory = NamedFactory(VkEnum)
241 ext_factory = NamedFactory(VkExtension)
242 for filename in args.xml_files:
243 parse_xml(enum_factory, ext_factory, filename)
244 enums = sorted(enum_factory.registry.values(), key=lambda e: e.name)
245 extensions = sorted(ext_factory.registry.values(), key=lambda e: e.name)
246
247 for template, file_ in [(C_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.c')),
248 (H_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.h'))]:
249 with open(file_, 'wb') as f:
250 f.write(template.render(
251 file=os.path.basename(__file__),
252 enums=enums,
253 extensions=extensions,
254 copyright=COPYRIGHT,
255 FOREIGN_ENUM_VALUES=FOREIGN_ENUM_VALUES))
256
257
258 if __name__ == '__main__':
259 main()