forked from pupil-labs/cygl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglew_pxd.py
193 lines (152 loc) · 6.54 KB
/
glew_pxd.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
'''
Script taken from: https://github.com/orlp/pygrafix
Appropriate Licence applies!
'''
import re
import sys
import os
if __name__ == '__main__':
if len(sys.argv)<2:
raise Exception('Please supply path to glew.h')
generate_pxd(sys.argv[1])
def generate_pxd(glew_header_loc,dest = ''):
with open(glew_header_loc, "r") as fin:
data = fin.read()
# cython doesn't support const
data = re.sub(r"\bconst\b", "", data)
lines = data.split("\n")
handled_lines = set()
function_types = {}
export_functions = {}
function_defs = []
enums = []
# read in function types
for linenr, line in enumerate(lines):
try:
result = re.findall(r"typedef\s+([^(]+)\([^*]+\*\s*([a-zA-Z_][a-zA-Z0-9_]+)\)\s*(\(.+\))\s*;", line)[0]
except IndexError: continue
function_types[result[1]] = (result[0].strip(), result[2])
handled_lines.add(linenr)
# read in exported functions
for linenr, line in enumerate(lines):
try:
result = re.findall(r"GLEW_FUN_EXPORT\s+([a-zA-Z_][a-zA-Z0-9_]+)\s+([a-zA-Z_][a-zA-Z0-9_]+)", line)[0]
except IndexError: continue
export_functions[result[1]] = result[0]
handled_lines.add(linenr)
# match exported functions with function types
for linenr, line in enumerate(lines):
try:
result = re.findall(r"#define\s+([a-zA-Z_][a-zA-Z0-9_]+)\s+GLEW_GET_FUN\s*\(\s*([a-zA-Z_][a-zA-Z0-9_]+)\s*\)", line)[0]
except IndexError: continue
export_func = export_functions[result[1]]
function_defs.append(function_types[export_func][0] + " " + result[0] + function_types[export_func][1])
handled_lines.add(linenr)
# add GLAPIENTRY functions
for linenr, line in enumerate(lines):
try:
result = re.findall(r"GLAPI\s+([a-zA-Z_][a-zA-Z0-9_]+)[^a-zA-Z_]+GLAPIENTRY[^a-zA-Z_]+([a-zA-Z_][a-zA-Z0-9_]+)\s*(\(.+\))\s*;", line)[0]
except IndexError: continue
function_defs.append(" ".join(result))
handled_lines.add(linenr)
# read in numeric defines as enums
for linenr, line in enumerate(lines):
try:
result = re.findall(r"#define\s+([a-zA-Z_][a-zA-Z0-9_]+)\s+(?:(?:0x[0-9a-fA-F]+)|[0-9]+)", line)[0]
except IndexError: continue
enums.append(result)
handled_lines.add(linenr)
# read in GLEW vars as enums
for linenr, line in enumerate(lines):
try:
result = re.findall(r"#define\s+([a-zA-Z_][a-zA-Z0-9_]+)\s+GLEW_GET_VAR\(.+\)", line)[0]
except IndexError: continue
enums.append(result)
handled_lines.add(linenr)
# also accept GL to GL defines as enums
for linenr, line in enumerate(lines):
try:
result = re.findall(r"#define\s+(GL_[a-zA-Z0-9_]+)\s+GL_[a-zA-Z0-9_]+", line)[0]
except IndexError: continue
enums.append(result)
handled_lines.add(linenr)
pxdheader = """from libc.stdint cimport int64_t, uint64_t
cdef extern from "include_glew.h":
ctypedef struct _cl_context:
pass
ctypedef struct _cl_event:
pass
ctypedef struct __GLsync:
pass
ctypedef unsigned short wchar_t
ctypedef int ptrdiff_t
ctypedef unsigned int GLenum
ctypedef unsigned int GLbitfield
ctypedef unsigned int GLuint
ctypedef int GLint
ctypedef int GLsizei
ctypedef char GLchar
ctypedef unsigned char GLboolean
ctypedef signed char GLbyte
ctypedef short GLshort
ctypedef unsigned char GLubyte
ctypedef unsigned short GLushort
ctypedef unsigned long GLulong
ctypedef float GLfloat
ctypedef float GLclampf
ctypedef double GLdouble
ctypedef double GLclampd
ctypedef int GLfixed
ctypedef int GLclampx
ctypedef void GLvoid
ctypedef int64_t GLint64EXT
ctypedef uint64_t GLuint64EXT
ctypedef GLint64EXT GLint64
ctypedef GLuint64EXT GLuint64
ctypedef __GLsync *GLsync
ctypedef char GLcharARB
ctypedef ptrdiff_t GLintptr
ctypedef ptrdiff_t GLsizeiptr
ctypedef _cl_context *cl_context
ctypedef _cl_event *cl_event
ctypedef unsigned int GLhandleARB
ctypedef ptrdiff_t GLintptrARB
ctypedef ptrdiff_t GLsizeiptrARB
ctypedef unsigned short GLhalf
ctypedef GLintptr GLvdpauSurfaceNV
ctypedef void (__stdcall *GLDEBUGPROCAMD)(GLuint id, GLenum category, GLenum severity, GLsizei length, GLchar *message, GLvoid *userParam)
ctypedef void (__stdcall *GLDEBUGPROCARB)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, GLchar *message, GLvoid *userParam)
ctypedef void (__stdcall *GLDEBUGPROC)(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam)
ctypedef void (__stdcall *GLLOGPROCREGAL)(GLenum stream, GLsizei length, const GLchar *message, GLvoid *context)
GLenum glewInit()
GLboolean glewIsSupported(char *name)
GLboolean glewIsExtensionSupported(char *name)
GLboolean glewGetExtension(char* name)
GLubyte *glewGetErrorString(GLenum error)
GLubyte *glewGetString(GLenum name)
"""
with open(os.path.join(dest,"glew.pxd"), "w") as fout:
data = pxdheader
data += " enum:\n"
data += "\n".join(" " + enum for enum in enums)
data += "\n\n"
def mod_func(func):
keywords = ["and", "del", "for", "is", "raise", "assert", "elif", "from", "lambda", "return", "break", "else", "global", "not", "try", "class",
"except", "if", "or", "while", "continue", "exec", "import", "pass", "yield", "def", "finally", "in", "print"]
# beautify functions
func = re.sub(r"\s+", " ", func) # collapse whitespace
func = re.sub(r"\s*([()])\s*", r"\1", func) # no whitespace near brackets
func = re.sub(r"\s*,\s*", r", ", func) # only whitespace __after__ comma
func = re.sub(r"\s*(\*+)\s*", r" \1", func) # beautify pointers in functions
# cython doesn't support (void), need to do () for no arguments instead
func = re.sub(r"\(void\)", "()", func)
# keywords...
for keyword in keywords:
func = re.sub(r"\b%s\b" % keyword, keyword + "_", func)
return func
data += "\n".join(" " + mod_func(func) for func in function_defs)
fout.write(data)
with open(os.path.join(dest,"unhandled_glew.h"), "w") as fout:
data = "\n".join(lines[linenr] for linenr in range(len(lines)) if not linenr in handled_lines)
data = re.sub("\n\n+", "\n", data)
fout.write(data)