Commit | Line | Data |
---|---|---|
3719602c PC |
1 | #!/usr/bin/env python3 |
2 | ||
3 | """ | |
4 | License statement applies to this file (xglgen.py) only. | |
5 | ||
6 | Permission is hereby granted, free of charge, | |
7 | to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
8 | to deal in the Software without restriction, including without limitation the rights to | |
9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
10 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
13 | ||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
18 | 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 SOFTWARE. | |
20 | """ | |
21 | ||
22 | import sys | |
23 | import os | |
24 | import re | |
25 | ||
26 | banned_ext = [ 'AMD', 'APPLE', 'NV', 'NVX', 'ATI', '3DLABS', 'SUN', 'SGI', 'SGIX', 'SGIS', 'INTEL', '3DFX', 'IBM', 'MESA', 'GREMEDY', 'OML', 'PGI', 'I3D', 'INGL', 'MTX', 'QCOM', 'IMG', 'ANGLE', 'SUNX', 'INGR' ] | |
27 | ||
28 | def noext(sym): | |
29 | for ext in banned_ext: | |
30 | if sym.endswith(ext): | |
31 | return False | |
32 | return True | |
33 | ||
34 | def fix_multiline_functions(lines): | |
35 | fixed_lines = [] | |
36 | temp_lines = [] | |
37 | for line in lines: | |
38 | if line.count('(') > line.count(')'): | |
39 | temp_lines.append(line) | |
40 | else: | |
41 | if len(temp_lines) > 0: | |
42 | if line.count(')') > line.count('('): | |
43 | temp_lines.append(line) | |
44 | fixed_line = re.sub(' +',' ', ''.join(temp_lines).replace('\n','').replace('\t','')) | |
45 | fixed_lines.append(fixed_line) | |
46 | temp_lines = [] | |
47 | else: | |
48 | temp_lines.append(line) | |
49 | else: | |
50 | fixed_lines.append(line) | |
51 | return fixed_lines | |
52 | ||
53 | def find_gl_symbols(lines): | |
54 | typedefs = [] | |
55 | syms = [] | |
56 | for line in lines: | |
57 | # Note this doesn't work automated; this script is designed as a helper | |
58 | m = re.search(r'^typedef.+PFN(\S+)PROC.+$', line) | |
59 | g = re.search(r'^GLAPI\s(.+)\s(.+)\s(gl\S+)\W*\((.+)\).*', line) | |
60 | if g and noext(g.group(3)): | |
61 | typedefs.append('typedef ' + g.group(1) + ' (APIENTRYP RGLSYM' + g.group(3).upper() + 'PROC) (' + g.group(4) + ');') | |
62 | syms.append(g.group(3)) | |
63 | ||
64 | return (typedefs, syms) | |
65 | ||
66 | def generate_defines(gl_syms): | |
67 | res = [] | |
68 | for line in gl_syms: | |
69 | res.append('#define {} __rglgen_{}'.format(line, line)) | |
70 | return res | |
71 | ||
72 | def generate_declarations(gl_syms): | |
73 | return ['RGLSYM' + x.upper() + 'PROC ' + x + ';' for x in gl_syms] | |
74 | ||
75 | def generate_macros(gl_syms): | |
76 | return [' SYM(' + x.replace('gl', '') + '),' for x in gl_syms] | |
77 | ||
78 | def dump(f, lines): | |
79 | f.write('\n'.join(lines)) | |
80 | f.write('\n\n') | |
81 | ||
82 | if __name__ == '__main__': | |
83 | ||
84 | if len(sys.argv) > 4: | |
85 | for banned in sys.argv[4:]: | |
86 | banned_ext.append(banned) | |
87 | ||
88 | with open(sys.argv[1], 'r') as f: | |
89 | lines = fix_multiline_functions(f.readlines()) | |
90 | typedefs, syms = find_gl_symbols(lines) | |
91 | ||
92 | overrides = generate_defines(syms) | |
93 | declarations = generate_declarations(syms) | |
94 | externs = ['extern ' + x for x in declarations] | |
95 | ||
96 | macros = generate_macros(syms) | |
97 | ||
98 | with open(sys.argv[2], 'w') as f: | |
99 | f.write('#ifndef RGLGEN_DECL_H__\n') | |
100 | f.write('#define RGLGEN_DECL_H__\n') | |
101 | ||
102 | f.write('#ifdef __cplusplus\n') | |
103 | f.write('extern "C" {\n') | |
104 | f.write('#endif\n') | |
105 | ||
106 | f.write('#ifdef GL_APIENTRY\n') | |
107 | f.write('typedef void (GL_APIENTRY *RGLGENGLDEBUGPROC)(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, GLvoid*);\n') | |
108 | f.write('typedef void (GL_APIENTRY *RGLGENGLDEBUGPROCKHR)(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, GLvoid*);\n') | |
109 | f.write('#else\n') | |
110 | f.write('#ifndef APIENTRY\n') | |
111 | f.write('#define APIENTRY\n') | |
112 | f.write('#endif\n') | |
113 | f.write('#ifndef APIENTRYP\n') | |
114 | f.write('#define APIENTRYP APIENTRY *\n') | |
115 | f.write('#endif\n') | |
116 | f.write('typedef void (APIENTRY *RGLGENGLDEBUGPROCARB)(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, GLvoid*);\n') | |
117 | f.write('typedef void (APIENTRY *RGLGENGLDEBUGPROC)(GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar*, GLvoid*);\n') | |
118 | f.write('#endif\n') | |
119 | ||
120 | f.write('#ifndef GL_OES_EGL_image\n') | |
121 | f.write('typedef void *GLeglImageOES;\n') | |
122 | f.write('#endif\n') | |
123 | ||
124 | f.write('#if !defined(GL_OES_fixed_point) && !defined(HAVE_OPENGLES2)\n') | |
125 | f.write('typedef GLint GLfixed;\n') | |
126 | f.write('#endif\n') | |
127 | ||
128 | f.write('#if defined(OSX) && !defined(MAC_OS_X_VERSION_10_7)\n') | |
129 | f.write('typedef long long int GLint64;\n') | |
130 | f.write('typedef unsigned long long int GLuint64;\n') | |
131 | f.write('typedef unsigned long long int GLuint64EXT;\n') | |
132 | f.write('typedef struct __GLsync *GLsync;\n') | |
133 | f.write('#endif\n') | |
134 | ||
135 | dump(f, typedefs) | |
136 | dump(f, overrides) | |
137 | dump(f, externs) | |
138 | ||
139 | f.write('struct rglgen_sym_map { const char *sym; void *ptr; };\n') | |
140 | f.write('extern const struct rglgen_sym_map rglgen_symbol_map[];\n') | |
141 | ||
142 | f.write('#ifdef __cplusplus\n') | |
143 | f.write('}\n') | |
144 | f.write('#endif\n') | |
145 | ||
146 | f.write('#endif\n') | |
147 | ||
148 | with open(sys.argv[3], 'w') as f: | |
149 | f.write('#include "glsym/glsym.h"\n') | |
150 | f.write('#include <stddef.h>\n') | |
151 | f.write('#define SYM(x) { "gl" #x, &(gl##x) }\n') | |
152 | f.write('const struct rglgen_sym_map rglgen_symbol_map[] = {\n') | |
153 | dump(f, macros) | |
154 | f.write(' { NULL, NULL },\n') | |
155 | f.write('};\n') | |
156 | dump(f, declarations) |