2016-10-17 08:50:25 +02:00
|
|
|
#! /usr/bin/env python
|
2014-02-10 02:10:30 +01:00
|
|
|
import sys
|
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
if (len(sys.argv) < 2):
|
2016-10-30 18:44:57 +01:00
|
|
|
print("usage: make_glwrapper.py <headers>")
|
|
|
|
sys.exit(255)
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
functions = []
|
|
|
|
types = []
|
|
|
|
constants = []
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
READ_FUNCTIONS = 0
|
|
|
|
READ_TYPES = 1
|
|
|
|
READ_CONSTANTS = 2
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
read_what = READ_TYPES
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2018-03-10 18:37:33 +01:00
|
|
|
def read_file(f):
|
2016-10-30 18:44:57 +01:00
|
|
|
while(True):
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
line = f.readline()
|
|
|
|
if (line == ""):
|
2016-10-30 18:44:57 +01:00
|
|
|
break
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
line = line.replace("\n", "").strip()
|
2016-10-30 18:44:57 +01:00
|
|
|
"""
|
2014-02-10 02:10:30 +01:00
|
|
|
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
|
|
|
|
"""
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
if (line.find("#define") != -1):
|
|
|
|
if (line.find("0x") == -1 and line.find("GL_VERSION") == -1):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
|
|
|
constants.append(line)
|
2016-10-30 18:57:40 +01:00
|
|
|
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):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
|
|
|
types.append(line)
|
2016-10-30 18:57:40 +01:00
|
|
|
elif (line.find("APIENTRY") != -1 and line.find("GLAPI") != -1):
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
if (line.find("ARB") != -1 or line.find("EXT") != -1 or line.find("NV") != -1):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
line = line.replace("APIENTRY", "")
|
|
|
|
line = line.replace("GLAPI", "")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
glpos = line.find(" gl")
|
|
|
|
if (glpos == -1):
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
glpos = line.find("\tgl")
|
|
|
|
if (glpos == -1):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-11-01 00:24:30 +01:00
|
|
|
ret = line[:glpos].strip()
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
line = line[glpos:].strip()
|
|
|
|
namepos = line.find("(")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
if (namepos == -1):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
name = line[:namepos].strip()
|
|
|
|
line = line[namepos:]
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
argpos = line.rfind(")")
|
|
|
|
if (argpos == -1):
|
2016-10-30 18:44:57 +01:00
|
|
|
continue
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
args = line[1:argpos]
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
funcdata = {}
|
|
|
|
funcdata["ret"] = ret
|
|
|
|
funcdata["name"] = name
|
|
|
|
funcdata["args"] = args
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:44:57 +01:00
|
|
|
functions.append(funcdata)
|
|
|
|
print(funcdata)
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2018-03-10 18:37:33 +01:00
|
|
|
for path in sys.argv[1:]:
|
|
|
|
with open(path, "r") as f:
|
|
|
|
read_file(f)
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
# print(types)
|
|
|
|
# print(constants)
|
|
|
|
# print(functions)
|
2016-04-02 20:26:12 +02:00
|
|
|
|
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
f = open("glwrapper.h", "w")
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.write("#ifndef GL_WRAPPER\n")
|
|
|
|
f.write("#define GL_WRAPPER\n\n\n")
|
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
header_code = """\
|
2014-02-10 02:10:30 +01:00
|
|
|
#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
|
|
|
|
|
2016-04-02 20:26:12 +02:00
|
|
|
#define __gl_h_
|
|
|
|
#define __GL_H__
|
|
|
|
#define __glext_h_
|
|
|
|
#define __GLEXT_H_
|
2014-02-10 02:10:30 +01:00
|
|
|
#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")
|
|
|
|
|
2016-11-01 00:24:30 +01:00
|
|
|
f.write(header_code)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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")
|
2016-11-01 00:24:30 +01:00
|
|
|
f.write("#else\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
f.write("#define GLWRP_APIENTRY \n")
|
2016-11-01 00:24:30 +01:00
|
|
|
f.write("#endif\n\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
for x in types:
|
2016-10-30 18:57:40 +01:00
|
|
|
f.write(x + "\n")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
f.write("\n\n")
|
|
|
|
|
|
|
|
for x in constants:
|
2016-10-30 18:57:40 +01:00
|
|
|
f.write(x + "\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.write("\n\n")
|
|
|
|
|
|
|
|
for x in functions:
|
2016-10-30 18:57:40 +01:00
|
|
|
f.write("extern " + x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ");\n")
|
|
|
|
f.write("#define " + x["name"] + " __wrapper_" + x["name"] + "\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.write("\n\n")
|
2016-11-01 00:24:30 +01:00
|
|
|
f.write("typedef void (*GLWrapperFuncPtr)(void);\n\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) );\n")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
f.write("#ifdef __cplusplus\n}\n#endif\n")
|
|
|
|
|
|
|
|
f.write("#endif\n\n")
|
2018-03-10 18:37:33 +01:00
|
|
|
f.close()
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2016-10-30 18:57:40 +01:00
|
|
|
f = open("glwrapper.c", "w")
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.write("\n\n")
|
|
|
|
f.write("#include \"glwrapper.h\"\n")
|
|
|
|
f.write("\n\n")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
for x in functions:
|
2016-10-30 18:57:40 +01:00
|
|
|
f.write(x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ")=NULL;\n")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
f.write("\n\n")
|
|
|
|
f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) ) {\n")
|
|
|
|
f.write("\n")
|
2016-04-02 20:26:12 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
for x in functions:
|
2016-10-30 18:57:40 +01:00
|
|
|
f.write("\t__wrapper_" + x["name"] + "=(" + x["ret"] + " GLWRP_APIENTRY (*)(" + x["args"] + "))wrapperFunc(\"" + x["name"] + "\");\n")
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
f.write("\n\n")
|
|
|
|
f.write("}\n")
|
|
|
|
f.write("\n\n")
|
2018-03-10 18:37:33 +01:00
|
|
|
f.close()
|