vulkan: Combine wsi and util makefiles
[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 vulking using vk.xml."""
23
24 from __future__ import print_function
25 import os
26 import textwrap
27 import xml.etree.cElementTree as et
28
29 from mako.template import Template
30
31 VK_XML = os.path.join(os.path.dirname(__file__), '..', 'registry', 'vk.xml')
32
33 COPYRIGHT = textwrap.dedent(u"""\
34 * Copyright © 2017 Intel Corporation
35 *
36 * Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to deal
38 * in the Software without restriction, including without limitation the rights
39 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40 * copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
42 *
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
52 * SOFTWARE.""")
53
54 C_TEMPLATE = Template(textwrap.dedent(u"""\
55 /* Autogenerated file -- do not edit
56 * generated by ${file}
57 *
58 ${copyright}
59 */
60
61 #include <vulkan/vulkan.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 enum.values:
72 case ${v}:
73 return "${v}";
74 % endfor
75 default:
76 unreachable("Undefined enum value.");
77 }
78 }
79 %endfor"""),
80 output_encoding='utf-8')
81
82 H_TEMPLATE = Template(textwrap.dedent(u"""\
83 /* Autogenerated file -- do not edit
84 * generated by ${file}
85 *
86 ${copyright}
87 */
88
89 #ifndef MESA_VK_ENUM_TO_STR_H
90 #define MESA_VK_ENUM_TO_STR_H
91
92 #include <vulkan/vulkan.h>
93
94 % for enum in enums:
95 const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
96 % endfor
97
98 #endif"""),
99 output_encoding='utf-8')
100
101
102 class EnumFactory(object):
103 """Factory for creating enums."""
104
105 def __init__(self, type_):
106 self.registry = {}
107 self.type = type_
108
109 def __call__(self, name):
110 try:
111 return self.registry[name]
112 except KeyError:
113 n = self.registry[name] = self.type(name)
114 return n
115
116
117 class VkEnum(object):
118 """Simple struct-like class representing a single Vulkan Enum."""
119
120 def __init__(self, name, values=None):
121 self.name = name
122 self.values = values or []
123
124
125 def xml_parser(filename):
126 """Parse the XML file and return parsed data.
127
128 This parser is a memory efficient iterative XML parser that returns a list
129 of VkEnum objects.
130 """
131 efactory = EnumFactory(VkEnum)
132
133 with open(filename, 'rb') as f:
134 context = iter(et.iterparse(f, events=('start', 'end')))
135
136 # This gives the root element, since goal is to iterate over the
137 # elements without building a tree, this allows the root to be cleared
138 # (erase the elements) after the children have been processed.
139 _, root = next(context)
140
141 for event, elem in context:
142 if event == 'end' and elem.tag == 'enums':
143 type_ = elem.attrib.get('type')
144 if type_ == 'enum':
145 enum = efactory(elem.attrib['name'])
146 enum.values.extend([e.attrib['name'] for e in elem
147 if e.tag == 'enum'])
148 elif event == 'end' and elem.tag == 'extension':
149 if elem.attrib['supported'] != 'vulkan':
150 continue
151 for e in elem.findall('.//enum[@extends][@offset]'):
152 enum = efactory(e.attrib['extends'])
153 enum.values.append(e.attrib['name'])
154
155 root.clear()
156
157 return efactory.registry.values()
158
159
160 def main():
161 enums = xml_parser(VK_XML)
162 for template, file_ in [(C_TEMPLATE, 'util/vk_enum_to_str.c'),
163 (H_TEMPLATE, 'util/vk_enum_to_str.h')]:
164 with open(file_, 'wb') as f:
165 f.write(template.render(
166 file=os.path.basename(__file__),
167 enums=enums,
168 copyright=COPYRIGHT))
169
170
171 if __name__ == '__main__':
172 main()