-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml_to_pyx.py
406 lines (275 loc) · 9.21 KB
/
xml_to_pyx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from xml.etree.ElementTree import parse, tostring
import collections
import itertools
import os
# Bad/weird types we don't need or want to generate.
BAD_TYPES = {
"GLVULKANPROCNV",
"GLvdpauSurfaceNV",
"GLhalfNV",
"struct _cl_event",
"struct _cl_context",
"GLDEBUGPROCAMD",
"GLDEBUGPROCKHR",
"GLDEBUGPROCARB",
"GLDEBUGPROC",
"GLsync",
}
BAD_COMMANDS = {
"glAttachObjectARB",
"glDetachObjectARB",
}
def snarf(fn):
with open(os.path.join(os.path.dirname(__file__), fn)) as f:
return f.read() + "\n"
UGUUGL_PXD_HEADER = snarf("uguugl_pxd_header.pxd")
UGUUGL_PYX_HEADER = snarf("uguugl_pyx_header.pyx")
UGUU_PYX_HEADER = snarf("uguu_pyx_header.pyx")
GL_FEATURES = [
"GL_VERSION_1_0",
"GL_VERSION_1_1",
"GL_VERSION_1_2",
"GL_VERSION_1_3",
"GL_VERSION_1_4",
"GL_VERSION_1_5",
"GL_VERSION_2_0",
"GL_VERSION_2_1",
"GL_VERSION_3_0",
]
GLES_FEATURES = [
"GL_ES_VERSION_2_0",
]
def type_and_name(node):
name = node.findtext("name")
text = "".join(node.itertext()).strip()
type_ = text[:-len(name)]
return type_, name
def python_type(t):
"""
Converts the OpenGL type t into a Python type.
"""
if not "*" in t:
return t
print("Weird type", t)
class Command:
def __init__(self, node):
self.return_type = type_and_name(node.find("proto"))[0].strip()
self.parameters = [ ]
self.parameter_types = [ ]
for i in node.findall("param"):
t, n = type_and_name(i)
self.parameters.append(n)
self.parameter_types.append(t)
self.aliases = set()
def format_param_list(self):
l = [ ]
for name, type_ in zip(self.parameters, self.parameter_types):
l.append(f"{type_} {name}")
return "(" + ", ".join(l) + ")"
def format_proxy_call(self):
return "(" + ", ".join(self.parameters) + ")"
def typedef(self, name):
return "ctypedef {} (*{}){} nogil".format(self.return_type, name, self.format_param_list())
class Feature:
def __init__(self):
self.commands = set()
self.enums = set()
def from_node(self, node):
for i in node.findall("require/enum"):
self.enums.add(i.attrib["name"])
for i in node.findall("require/command"):
self.commands.add(i.attrib["name"])
def __or__(self, other):
rv = Feature()
rv.commands = self.commands | other.commands
rv.enums = self.enums | other.enums
return rv
def __and__(self, other):
rv = Feature()
rv.commands = self.commands & other.commands
rv.enums = self.enums & other.enums
return rv
class XMLToPYX:
def __init__(self):
self.root = parse("gl.xml").getroot()
self.types = [ ]
self.type_names = [ ]
self.convert_types()
# A map from command name to command.
self.commands = { }
self.find_commands()
# A map from enum name to value.
self.enums = collections.OrderedDict()
self.find_enums()
# A map from feature name to value.
self.features = { }
self.find_features()
self.select_features()
with open("gen/uguugl.pxd", "w") as f:
self.generate_uguugl_pxd(f)
with open("gen/uguugl.pyx", "w") as f:
self.generate_uguugl_pyx(f)
with open("gen/uguu.pyx", "w") as f:
self.generate_uguu_pyx(f)
def convert_types(self):
types = self.root.find('types')
for t in types:
if t.get("api", ""):
continue
name = t.find("name")
if name is None:
continue
name = name.text
if name in BAD_TYPES:
continue
self.type_names.append(name)
text = "".join(t.itertext())
text = text.replace(";", "")
text = text.replace("typedef", "ctypedef")
self.types.append(text)
def add_command(self, node):
name = type_and_name(node.find("proto"))[1]
if name in BAD_COMMANDS:
return
names = [ name ]
for i in node.findall("alias"):
names.append(i.attrib["name"])
for i in names:
c = self.commands.get(i, None)
if c is not None:
break
else:
c = Command(node)
for i in names:
c.aliases.add(i)
self.commands[i] = c
def find_commands(self):
commands = self.root.find("commands")
for c in commands.findall("command"):
self.add_command(c)
def find_enums(self):
for enums in self.root.findall("enums"):
for i in enums.findall("enum"):
value = i.attrib["value"]
name = i.attrib["name"]
self.enums[name] = value
alias = i.attrib.get("alias", None)
if alias is not None:
self.enums[alias] = value
def find_features(self):
for i in itertools.chain(
self.root.findall("feature"),
self.root.findall("extensions/extension")
):
name = i.attrib["name"]
f = Feature()
f.from_node(i)
self.features[name] = f
# print(name)
def select_features(self):
gl = Feature()
for i in GL_FEATURES:
gl = gl | self.features[i]
gles = Feature()
for i in GLES_FEATURES:
gles = gles | self.features[i]
f = gl & gles
self.features = f
def generate_uguugl_pxd(self, f):
f.write(UGUUGL_PXD_HEADER)
def w(s):
f.write(s + "\n")
w('cdef extern from "renpygl.h":')
w('')
for l in self.types:
w(f" {l}")
enums = list(self.features.enums)
enums.sort(key=lambda n : (int(self.enums[n], 0), n))
w(f'')
for i in enums:
w(f" GLenum {i}")
for i in sorted(self.features.commands):
typename = i + "_type"
c = self.commands[i]
w("")
w(c.typedef(typename))
w(f"cdef {typename} {i}")
def generate_uguugl_pyx(self, f):
f.write(UGUUGL_PYX_HEADER)
def w(s):
f.write(s + "\n")
for i in sorted(self.features.commands):
c = self.commands[i]
w("")
w(f"cdef {i}_type real_{i}")
w(f"cdef {i}_type {i}")
w("")
param_list = c.format_param_list()
proxy_call = c.format_proxy_call()
if c.return_type != "void":
w(f'cdef {c.return_type} check_{i}{param_list} nogil:')
w(f' cdef {c.return_type} rv')
w(f' rv = real_{i}{proxy_call}')
w(f' check_error("{i}")')
w(f' return rv')
else:
w(f'cdef {c.return_type} check_{i}{param_list} nogil:')
w(f' real_{i}{proxy_call}')
w(f' check_error("{i}")')
w("")
w("def load():")
for i in sorted(self.features.commands):
names = list(self.commands[i].aliases)
names.remove(i)
names.sort()
names.insert(0, i)
names = [ i.encode("utf-8") for i in names ]
w(f"")
w(f" global real_{i}, {i}")
w(f" real_{i} = <{i}_type> find_gl_command({names!r})")
w(f" {i} = real_{i}")
w("")
w("def enable_check_error():")
for i in sorted(self.features.commands):
w(f" global {i}")
w(f" {i} = check_{i}")
def generate_uguu_pyx(self, f):
def w(s):
f.write(s + "\n")
for l in self.type_names:
w(f"from uguugl cimport {l}")
w(f'')
f.write(UGUU_PYX_HEADER)
for l in self.type_names:
w(f"from uguugl cimport {l}")
for i in sorted(self.features.commands):
c = self.commands[i]
params = list(zip(c.parameters, c.parameter_types))
param_list = ", ".join(c.parameters)
w(f'')
w(f"def {i}({param_list}):")
for param, type_ in params:
if "*" in type_:
w(f" cdef ptr {param}_ptr = get_ptr({param})")
proxy = [ ]
for param, type_ in params:
if "*" in type_:
proxy.append(f"<{type_}> {param}_ptr.ptr")
else:
proxy.append(param)
proxy = ", ".join(proxy)
rt = c.return_type.strip()
if rt == "void":
w(f' uguugl.{i}({proxy})')
elif rt == "const GLubyte *":
w(f' return proxy_return_string(uguugl.{i}({proxy}))')
else:
w(f' return uguugl.{i}({proxy})')
# Expose the enums to python.
enums = list(self.features.enums)
enums.sort(key=lambda n : (int(self.enums[n], 0), n))
w(f'')
for i in enums:
w(f'{i} = uguugl.{i}')
if __name__ == "__main__":
XMLToPYX()