make_glwrapper.py 4.2 KB

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