2 # Copyright © 2017 Intel Corporation
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:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
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
22 """Create enum to string functions for vulkan using vk.xml."""
24 from __future__
import print_function
28 import xml
.etree
.cElementTree
as et
30 from mako
.template
import Template
32 COPYRIGHT
= textwrap
.dedent(u
"""\
33 * Copyright © 2017 Intel Corporation
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:
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
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
53 C_TEMPLATE
= Template(textwrap
.dedent(u
"""\
54 /* Autogenerated file -- do not edit
55 * generated by ${file}
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"
68 vk_${enum.name[2:]}_to_str(${enum.name} input)
71 % for v in enum.values:
72 % if v in FOREIGN_ENUM_VALUES:
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Wswitch"
79 % if v in FOREIGN_ENUM_VALUES:
80 #pragma GCC diagnostic pop
85 unreachable("Undefined enum value.");
89 output_encoding
='utf-8')
91 H_TEMPLATE
= Template(textwrap
.dedent(u
"""\
92 /* Autogenerated file -- do not edit
93 * generated by ${file}
98 #ifndef MESA_VK_ENUM_TO_STR_H
99 #define MESA_VK_ENUM_TO_STR_H
101 #include <vulkan/vulkan.h>
102 #include <vulkan/vk_android_native_buffer.h>
105 const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
109 output_encoding
='utf-8')
111 # These enums are defined outside their respective enum blocks, and thus cause
113 FOREIGN_ENUM_VALUES
= [
114 "VK_STRUCTURE_TYPE_NATIVE_BUFFER_ANDROID",
118 class EnumFactory(object):
119 """Factory for creating enums."""
121 def __init__(self
, type_
):
125 def __call__(self
, name
):
127 return self
.registry
[name
]
129 n
= self
.registry
[name
] = self
.type(name
)
133 class VkEnum(object):
134 """Simple struct-like class representing a single Vulkan Enum."""
136 def __init__(self
, name
, values
=None):
138 self
.values
= values
or []
141 def parse_xml(efactory
, filename
):
142 """Parse the XML file. Accumulate results into the efactory.
144 This parser is a memory efficient iterative XML parser that returns a list
148 with
open(filename
, 'rb') as f
:
149 context
= iter(et
.iterparse(f
, events
=('start', 'end')))
151 # This gives the root element, since goal is to iterate over the
152 # elements without building a tree, this allows the root to be cleared
153 # (erase the elements) after the children have been processed.
154 _
, root
= next(context
)
156 for event
, elem
in context
:
157 if event
== 'end' and elem
.tag
== 'enums':
158 type_
= elem
.attrib
.get('type')
160 enum
= efactory(elem
.attrib
['name'])
161 enum
.values
.extend([e
.attrib
['name'] for e
in elem
163 elif event
== 'end' and elem
.tag
== 'extension':
164 if elem
.attrib
['supported'] != 'vulkan':
166 for e
in elem
.findall('.//enum[@extends][@offset]'):
167 enum
= efactory(e
.attrib
['extends'])
168 enum
.values
.append(e
.attrib
['name'])
174 parser
= argparse
.ArgumentParser()
175 parser
.add_argument('--xml', required
=True,
176 help='Vulkan API XML files',
179 parser
.add_argument('--outdir',
180 help='Directory to put the generated files in',
183 args
= parser
.parse_args()
185 efactory
= EnumFactory(VkEnum
)
186 for filename
in args
.xml_files
:
187 parse_xml(efactory
, filename
)
188 enums
= sorted(efactory
.registry
.values(), key
=lambda e
: e
.name
)
190 for template
, file_
in [(C_TEMPLATE
, os
.path
.join(args
.outdir
, 'vk_enum_to_str.c')),
191 (H_TEMPLATE
, os
.path
.join(args
.outdir
, 'vk_enum_to_str.h'))]:
192 with
open(file_
, 'wb') as f
:
193 f
.write(template
.render(
194 file=os
.path
.basename(__file__
),
197 FOREIGN_ENUM_VALUES
=FOREIGN_ENUM_VALUES
))
200 if __name__
== '__main__':