summaryrefslogtreecommitdiff
path: root/misc/scripts/make_glwrapper.py
blob: 5694d2327e25c7af0378c249dc0e3f3f1ab75899 (plain)
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
#! /usr/bin/env python
import sys

if (len(sys.argv) < 2):
    print("usage: make_glwrapper.py <headers>")
    sys.exit(255)


functions = []
types = []
constants = []

READ_FUNCTIONS = 0
READ_TYPES = 1
READ_CONSTANTS = 2

read_what = READ_TYPES

for x in (range(len(sys.argv) - 1)):
    f = open(sys.argv[x + 1], "r")

    while(True):

        line = f.readline()
        if (line == ""):
            break

        line = line.replace("\n", "").strip()
        """
    if (line.find("[types]")!=-1):
      read_what=READ_TYPES
      continue
    elif (line.find("[constants]")!=-1):
      read=READ_TYPES
      continue
    elif (line.find("[functions]")!=-1):
      read_what=READ_FUNCTIONS
      continue
     """

        if (line.find("#define") != -1):
            if (line.find("0x") == -1 and line.find("GL_VERSION") == -1):
                continue
            constants.append(line)
        elif (line.find("typedef") != -1):
            if (line.find("(") != -1 or line.find(")") != -1 or line.find("ARB") != -1 or line.find("EXT") != -1 or line.find("GL") == -1):
                continue
            types.append(line)
        elif (line.find("APIENTRY") != -1 and line.find("GLAPI") != -1):

            if (line.find("ARB") != -1 or line.find("EXT") != -1 or line.find("NV") != -1):
                continue

            line = line.replace("APIENTRY", "")
            line = line.replace("GLAPI", "")

            glpos = line.find(" gl")
            if (glpos == -1):

                glpos = line.find("\tgl")
                if (glpos == -1):
                    continue

            ret = line[:glpos].strip()

            line = line[glpos:].strip()
            namepos = line.find("(")

            if (namepos == -1):
                continue

            name = line[:namepos].strip()
            line = line[namepos:]

            argpos = line.rfind(")")
            if (argpos == -1):
                continue

            args = line[1:argpos]

            funcdata = {}
            funcdata["ret"] = ret
            funcdata["name"] = name
            funcdata["args"] = args

            functions.append(funcdata)
            print(funcdata)


# print(types)
# print(constants)
# print(functions)


f = open("glwrapper.h", "w")

f.write("#ifndef GL_WRAPPER\n")
f.write("#define GL_WRAPPER\n\n\n")

header_code = """\
#if defined(__gl_h_) || defined(__GL_H__)
#error gl.h included before glwrapper.h
#endif
#if defined(__glext_h_) || defined(__GLEXT_H_)
#error glext.h included before glwrapper.h
#endif
#if defined(__gl_ATI_h_)
#error glATI.h included before glwrapper.h
#endif

#define __gl_h_
#define __GL_H__
#define __glext_h_
#define __GLEXT_H_
#define __gl_ATI_h_

#define GL_TRUE 1
#define GL_FALSE 0

#define GL_ZERO                           0
#define GL_ONE                            1
#define GL_NONE                           0
#define GL_NO_ERROR                       0

\n\n
"""

f.write("#include <stddef.h>\n\n\n")

f.write(header_code)

f.write("#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n\n")
f.write("#if defined(_WIN32) && !defined(__CYGWIN__)\n")
f.write("#define GLWRP_APIENTRY __stdcall\n")
f.write("#else\n")
f.write("#define GLWRP_APIENTRY \n")
f.write("#endif\n\n")
for x in types:
    f.write(x + "\n")

f.write("\n\n")

for x in constants:
    f.write(x + "\n")

f.write("\n\n")

for x in functions:
    f.write("extern " + x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ");\n")
    f.write("#define " + x["name"] + " __wrapper_" + x["name"] + "\n")

f.write("\n\n")
f.write("typedef void (*GLWrapperFuncPtr)(void);\n\n")
f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) );\n")

f.write("#ifdef __cplusplus\n}\n#endif\n")

f.write("#endif\n\n")

f = open("glwrapper.c", "w")

f.write("\n\n")
f.write("#include \"glwrapper.h\"\n")
f.write("\n\n")

for x in functions:
    f.write(x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ")=NULL;\n")

f.write("\n\n")
f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) )  {\n")
f.write("\n")

for x in functions:
    f.write("\t__wrapper_" + x["name"] + "=(" + x["ret"] + " GLWRP_APIENTRY (*)(" + x["args"] + "))wrapperFunc(\"" + x["name"] + "\");\n")

f.write("\n\n")
f.write("}\n")
f.write("\n\n")