make_glwrapper.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #! /usr/bin/env python
  2. import sys
  3. if (len(sys.argv) < 2):
  4. print("usage: make_glwrapper.py <headers>")
  5. sys.exit(255)
  6. functions = []
  7. types = []
  8. constants = []
  9. READ_FUNCTIONS = 0
  10. READ_TYPES = 1
  11. READ_CONSTANTS = 2
  12. read_what = READ_TYPES
  13. def read_file(f):
  14. while(True):
  15. line = f.readline()
  16. if (line == ""):
  17. break
  18. line = line.replace("\n", "").strip()
  19. """
  20. if (line.find("[types]")!=-1):
  21. read_what=READ_TYPES
  22. continue
  23. elif (line.find("[constants]")!=-1):
  24. read=READ_TYPES
  25. continue
  26. elif (line.find("[functions]")!=-1):
  27. read_what=READ_FUNCTIONS
  28. continue
  29. """
  30. if (line.find("#define") != -1):
  31. if (line.find("0x") == -1 and line.find("GL_VERSION") == -1):
  32. continue
  33. constants.append(line)
  34. elif (line.find("typedef") != -1):
  35. if (line.find("(") != -1 or line.find(")") != -1 or line.find("ARB") != -1 or line.find("EXT") != -1 or line.find("GL") == -1):
  36. continue
  37. types.append(line)
  38. elif (line.find("APIENTRY") != -1 and line.find("GLAPI") != -1):
  39. if (line.find("ARB") != -1 or line.find("EXT") != -1 or line.find("NV") != -1):
  40. continue
  41. line = line.replace("APIENTRY", "")
  42. line = line.replace("GLAPI", "")
  43. glpos = line.find(" gl")
  44. if (glpos == -1):
  45. glpos = line.find("\tgl")
  46. if (glpos == -1):
  47. continue
  48. ret = line[:glpos].strip()
  49. line = line[glpos:].strip()
  50. namepos = line.find("(")
  51. if (namepos == -1):
  52. continue
  53. name = line[:namepos].strip()
  54. line = line[namepos:]
  55. argpos = line.rfind(")")
  56. if (argpos == -1):
  57. continue
  58. args = line[1:argpos]
  59. funcdata = {}
  60. funcdata["ret"] = ret
  61. funcdata["name"] = name
  62. funcdata["args"] = args
  63. functions.append(funcdata)
  64. print(funcdata)
  65. for path in sys.argv[1:]:
  66. with open(path, "r") as f:
  67. read_file(f)
  68. # print(types)
  69. # print(constants)
  70. # print(functions)
  71. f = open("glwrapper.h", "w")
  72. f.write("#ifndef GL_WRAPPER\n")
  73. f.write("#define GL_WRAPPER\n\n\n")
  74. header_code = """\
  75. #if defined(__gl_h_) || defined(__GL_H__)
  76. #error gl.h included before glwrapper.h
  77. #endif
  78. #if defined(__glext_h_) || defined(__GLEXT_H_)
  79. #error glext.h included before glwrapper.h
  80. #endif
  81. #if defined(__gl_ATI_h_)
  82. #error glATI.h included before glwrapper.h
  83. #endif
  84. #define __gl_h_
  85. #define __GL_H__
  86. #define __glext_h_
  87. #define __GLEXT_H_
  88. #define __gl_ATI_h_
  89. #define GL_TRUE 1
  90. #define GL_FALSE 0
  91. #define GL_ZERO 0
  92. #define GL_ONE 1
  93. #define GL_NONE 0
  94. #define GL_NO_ERROR 0
  95. \n\n
  96. """
  97. f.write("#include <stddef.h>\n\n\n")
  98. f.write(header_code)
  99. f.write("#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n\n")
  100. f.write("#if defined(_WIN32) && !defined(__CYGWIN__)\n")
  101. f.write("#define GLWRP_APIENTRY __stdcall\n")
  102. f.write("#else\n")
  103. f.write("#define GLWRP_APIENTRY \n")
  104. f.write("#endif\n\n")
  105. for x in types:
  106. f.write(x + "\n")
  107. f.write("\n\n")
  108. for x in constants:
  109. f.write(x + "\n")
  110. f.write("\n\n")
  111. for x in functions:
  112. f.write("extern " + x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ");\n")
  113. f.write("#define " + x["name"] + " __wrapper_" + x["name"] + "\n")
  114. f.write("\n\n")
  115. f.write("typedef void (*GLWrapperFuncPtr)(void);\n\n")
  116. f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) );\n")
  117. f.write("#ifdef __cplusplus\n}\n#endif\n")
  118. f.write("#endif\n\n")
  119. f.close()
  120. f = open("glwrapper.c", "w")
  121. f.write("\n\n")
  122. f.write("#include \"glwrapper.h\"\n")
  123. f.write("\n\n")
  124. for x in functions:
  125. f.write(x["ret"] + " GLWRP_APIENTRY (*__wrapper_" + x["name"] + ")(" + x["args"] + ")=NULL;\n")
  126. f.write("\n\n")
  127. f.write("void glWrapperInit( GLWrapperFuncPtr (*wrapperFunc)(const char*) ) {\n")
  128. f.write("\n")
  129. for x in functions:
  130. f.write("\t__wrapper_" + x["name"] + "=(" + x["ret"] + " GLWRP_APIENTRY (*)(" + x["args"] + "))wrapperFunc(\"" + x["name"] + "\");\n")
  131. f.write("\n\n")
  132. f.write("}\n")
  133. f.write("\n\n")
  134. f.close()