1*4882a593Smuzhiyun#!/usr/bin/python3 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# python script to generate cdecl to stdcall wrappers for GL functions 4*4882a593Smuzhiyun# adapted from genheaders.py 5*4882a593Smuzhiyun# 6*4882a593Smuzhiyun# Copyright (c) 2013 The Khronos Group Inc. 7*4882a593Smuzhiyun# 8*4882a593Smuzhiyun# Permission is hereby granted, free of charge, to any person obtaining a 9*4882a593Smuzhiyun# copy of this software and/or associated documentation files (the 10*4882a593Smuzhiyun# "Materials"), to deal in the Materials without restriction, including 11*4882a593Smuzhiyun# without limitation the rights to use, copy, modify, merge, publish, 12*4882a593Smuzhiyun# distribute, sublicense, and/or sell copies of the Materials, and to 13*4882a593Smuzhiyun# permit persons to whom the Materials are furnished to do so, subject to 14*4882a593Smuzhiyun# the following conditions: 15*4882a593Smuzhiyun# 16*4882a593Smuzhiyun# The above copyright notice and this permission notice shall be included 17*4882a593Smuzhiyun# in all copies or substantial portions of the Materials. 18*4882a593Smuzhiyun# 19*4882a593Smuzhiyun# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20*4882a593Smuzhiyun# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21*4882a593Smuzhiyun# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 22*4882a593Smuzhiyun# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23*4882a593Smuzhiyun# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24*4882a593Smuzhiyun# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25*4882a593Smuzhiyun# MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. 26*4882a593Smuzhiyun 27*4882a593Smuzhiyunimport sys, time, pdb, string, cProfile 28*4882a593Smuzhiyunfrom reg import * 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun# Default input / log files 31*4882a593SmuzhiyunerrFilename = None 32*4882a593SmuzhiyundiagFilename = 'diag.txt' 33*4882a593SmuzhiyunregFilename = 'gl.xml' 34*4882a593SmuzhiyunoutFilename = 'gen_gl_wrappers.c' 35*4882a593Smuzhiyun 36*4882a593Smuzhiyunprotect=True 37*4882a593Smuzhiyunprefix="gl" 38*4882a593Smuzhiyunpreresolve=False 39*4882a593Smuzhiyunwrapper=False 40*4882a593Smuzhiyunshim=False 41*4882a593Smuzhiyunthunk=False 42*4882a593Smuzhiyunthunkdefs=False 43*4882a593Smuzhiyunstaticwrappers=False 44*4882a593Smuzhiyunnodebug=False 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun# list of WGL extension functions we use 47*4882a593Smuzhiyunused_wgl_ext_fns = {key: 1 for key in [ 48*4882a593Smuzhiyun "wglSwapIntervalEXT", 49*4882a593Smuzhiyun "wglGetExtensionsStringARB", 50*4882a593Smuzhiyun "wglDestroyPbufferARB", 51*4882a593Smuzhiyun "wglGetPbufferDCARB", 52*4882a593Smuzhiyun "wglReleasePbufferDCARB", 53*4882a593Smuzhiyun "wglCreatePbufferARB", 54*4882a593Smuzhiyun "wglMakeContextCurrentARB", 55*4882a593Smuzhiyun "wglChoosePixelFormatARB", 56*4882a593Smuzhiyun "wglGetPixelFormatAttribivARB", 57*4882a593Smuzhiyun "wglGetPixelFormatAttribivARB" 58*4882a593Smuzhiyun]} 59*4882a593Smuzhiyun 60*4882a593Smuzhiyunif __name__ == '__main__': 61*4882a593Smuzhiyun i = 1 62*4882a593Smuzhiyun while (i < len(sys.argv)): 63*4882a593Smuzhiyun arg = sys.argv[i] 64*4882a593Smuzhiyun i = i + 1 65*4882a593Smuzhiyun if (arg == '-noprotect'): 66*4882a593Smuzhiyun print('Disabling inclusion protection in output headers', file=sys.stderr) 67*4882a593Smuzhiyun protect = False 68*4882a593Smuzhiyun elif (arg == '-registry'): 69*4882a593Smuzhiyun regFilename = sys.argv[i] 70*4882a593Smuzhiyun i = i+1 71*4882a593Smuzhiyun print('Using registry', regFilename, file=sys.stderr) 72*4882a593Smuzhiyun elif (arg == '-outfile'): 73*4882a593Smuzhiyun outFilename = sys.argv[i] 74*4882a593Smuzhiyun i = i+1 75*4882a593Smuzhiyun elif (arg == '-preresolve'): 76*4882a593Smuzhiyun preresolve=True 77*4882a593Smuzhiyun elif (arg == '-wrapper'): 78*4882a593Smuzhiyun wrapper=True 79*4882a593Smuzhiyun elif (arg == '-shim'): 80*4882a593Smuzhiyun shim=True 81*4882a593Smuzhiyun elif (arg == '-thunk'): 82*4882a593Smuzhiyun thunk=True 83*4882a593Smuzhiyun elif (arg == '-thunkdefs'): 84*4882a593Smuzhiyun thunkdefs=True 85*4882a593Smuzhiyun elif (arg == '-staticwrappers'): 86*4882a593Smuzhiyun staticwrappers=True 87*4882a593Smuzhiyun elif (arg == '-prefix'): 88*4882a593Smuzhiyun prefix = sys.argv[i] 89*4882a593Smuzhiyun i = i+1 90*4882a593Smuzhiyun elif (arg == '-nodebug'): 91*4882a593Smuzhiyun nodebug = True 92*4882a593Smuzhiyun elif (arg[0:1] == '-'): 93*4882a593Smuzhiyun print('Unrecognized argument:', arg, file=sys.stderr) 94*4882a593Smuzhiyun exit(1) 95*4882a593Smuzhiyun 96*4882a593Smuzhiyunprint('Generating', outFilename, file=sys.stderr) 97*4882a593Smuzhiyun 98*4882a593Smuzhiyun# Load & parse registry 99*4882a593Smuzhiyunreg = Registry() 100*4882a593Smuzhiyuntree = etree.parse(regFilename) 101*4882a593Smuzhiyunreg.loadElementTree(tree) 102*4882a593Smuzhiyun 103*4882a593Smuzhiyunif shim: 104*4882a593Smuzhiyun versions = '1\.[012]' 105*4882a593Smuzhiyunelse: 106*4882a593Smuzhiyun versions = '.*' 107*4882a593Smuzhiyun 108*4882a593SmuzhiyungenOpts = CGeneratorOptions( 109*4882a593Smuzhiyun apiname = prefix, 110*4882a593Smuzhiyun profile = 'compatibility', 111*4882a593Smuzhiyun versions = versions, 112*4882a593Smuzhiyun emitversions = versions, 113*4882a593Smuzhiyun defaultExtensions = prefix, # Default extensions for GL 114*4882a593Smuzhiyun protectFile = protect, 115*4882a593Smuzhiyun protectFeature = protect, 116*4882a593Smuzhiyun protectProto = protect, 117*4882a593Smuzhiyun ) 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun# create error/warning & diagnostic files 120*4882a593Smuzhiyunif (errFilename): 121*4882a593Smuzhiyun errWarn = open(errFilename,'w') 122*4882a593Smuzhiyunelse: 123*4882a593Smuzhiyun errWarn = sys.stderr 124*4882a593Smuzhiyundiag = open(diagFilename, 'w') 125*4882a593Smuzhiyun 126*4882a593Smuzhiyundef ParseCmdRettype(cmd): 127*4882a593Smuzhiyun proto=noneStr(cmd.elem.find('proto')) 128*4882a593Smuzhiyun rettype=noneStr(proto.text) 129*4882a593Smuzhiyun if rettype.lower()!="void ": 130*4882a593Smuzhiyun plist = ([t for t in proto.itertext()]) 131*4882a593Smuzhiyun rettype = ''.join(plist[:-1]) 132*4882a593Smuzhiyun rettype=rettype.strip() 133*4882a593Smuzhiyun return rettype 134*4882a593Smuzhiyun 135*4882a593Smuzhiyundef ParseCmdParams(cmd): 136*4882a593Smuzhiyun params = cmd.elem.findall('param') 137*4882a593Smuzhiyun plist=[] 138*4882a593Smuzhiyun for param in params: 139*4882a593Smuzhiyun # construct the formal parameter definition from ptype and name 140*4882a593Smuzhiyun # elements, also using any text found around these in the 141*4882a593Smuzhiyun # param element, in the order it appears in the document 142*4882a593Smuzhiyun paramtype = '' 143*4882a593Smuzhiyun # also extract the formal parameter name from the name element 144*4882a593Smuzhiyun paramname = '' 145*4882a593Smuzhiyun for t in param.iter(): 146*4882a593Smuzhiyun if t.tag == 'ptype' or t.tag == 'param': 147*4882a593Smuzhiyun paramtype = paramtype + noneStr(t.text) 148*4882a593Smuzhiyun if t.tag == 'name': 149*4882a593Smuzhiyun paramname = t.text + '_' 150*4882a593Smuzhiyun paramtype = paramtype + ' ' + paramname 151*4882a593Smuzhiyun if t.tail is not None: 152*4882a593Smuzhiyun paramtype = paramtype + t.tail.strip() 153*4882a593Smuzhiyun plist.append((paramtype, paramname)) 154*4882a593Smuzhiyun return plist 155*4882a593Smuzhiyun 156*4882a593Smuzhiyunclass PreResolveOutputGenerator(OutputGenerator): 157*4882a593Smuzhiyun def __init__(self, 158*4882a593Smuzhiyun errFile = sys.stderr, 159*4882a593Smuzhiyun warnFile = sys.stderr, 160*4882a593Smuzhiyun diagFile = sys.stdout): 161*4882a593Smuzhiyun OutputGenerator.__init__(self, errFile, warnFile, diagFile) 162*4882a593Smuzhiyun self.wrappers={} 163*4882a593Smuzhiyun def beginFile(self, genOpts): 164*4882a593Smuzhiyun self.outFile.write('/* Automatically generated from %s - DO NOT EDIT */\n\n'%regFilename) 165*4882a593Smuzhiyun def endFile(self): 166*4882a593Smuzhiyun self.outFile.write('\nvoid ' + prefix + 'ResolveExtensionProcs(void)\n{\n') 167*4882a593Smuzhiyun for funcname in self.wrappers.keys(): 168*4882a593Smuzhiyun self.outFile.write( ' PRERESOLVE(PFN' + funcname.upper() + 'PROC, "' + funcname + '");\n') 169*4882a593Smuzhiyun self.outFile.write('}\n\n') 170*4882a593Smuzhiyun def beginFeature(self, interface, emit): 171*4882a593Smuzhiyun OutputGenerator.beginFeature(self, interface, emit) 172*4882a593Smuzhiyun def endFeature(self): 173*4882a593Smuzhiyun OutputGenerator.endFeature(self) 174*4882a593Smuzhiyun def genType(self, typeinfo, name): 175*4882a593Smuzhiyun OutputGenerator.genType(self, typeinfo, name) 176*4882a593Smuzhiyun def genEnum(self, enuminfo, name): 177*4882a593Smuzhiyun OutputGenerator.genEnum(self, enuminfo, name) 178*4882a593Smuzhiyun def genCmd(self, cmd, name): 179*4882a593Smuzhiyun OutputGenerator.genCmd(self, cmd, name) 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun if prefix == 'wgl' and not name in used_wgl_ext_fns: 182*4882a593Smuzhiyun return 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun self.outFile.write('RESOLVE_DECL(PFN' + name.upper() + 'PROC);\n') 185*4882a593Smuzhiyun self.wrappers[name]=1 186*4882a593Smuzhiyun 187*4882a593Smuzhiyunclass WrapperOutputGenerator(OutputGenerator): 188*4882a593Smuzhiyun def __init__(self, 189*4882a593Smuzhiyun errFile = sys.stderr, 190*4882a593Smuzhiyun warnFile = sys.stderr, 191*4882a593Smuzhiyun diagFile = sys.stdout): 192*4882a593Smuzhiyun OutputGenerator.__init__(self, errFile, warnFile, diagFile) 193*4882a593Smuzhiyun def beginFile(self, genOpts): 194*4882a593Smuzhiyun self.outFile.write('/* Automatically generated from %s - DO NOT EDIT */\n\n'%regFilename) 195*4882a593Smuzhiyun def endFile(self): 196*4882a593Smuzhiyun pass 197*4882a593Smuzhiyun def beginFeature(self, interface, emit): 198*4882a593Smuzhiyun OutputGenerator.beginFeature(self, interface, emit) 199*4882a593Smuzhiyun self.OldVersion = self.featureName.startswith('GL_VERSION_1_0') or self.featureName.startswith('GL_VERSION_1_1') 200*4882a593Smuzhiyun def endFeature(self): 201*4882a593Smuzhiyun OutputGenerator.endFeature(self) 202*4882a593Smuzhiyun def genType(self, typeinfo, name): 203*4882a593Smuzhiyun OutputGenerator.genType(self, typeinfo, name) 204*4882a593Smuzhiyun def genEnum(self, enuminfo, name): 205*4882a593Smuzhiyun OutputGenerator.genEnum(self, enuminfo, name) 206*4882a593Smuzhiyun def genCmd(self, cmd, name): 207*4882a593Smuzhiyun OutputGenerator.genCmd(self, cmd, name) 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun if prefix == 'wgl' and not name in used_wgl_ext_fns: 210*4882a593Smuzhiyun return 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun rettype=ParseCmdRettype(cmd) 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun if staticwrappers: self.outFile.write("static ") 215*4882a593Smuzhiyun self.outFile.write("%s %sWrapper("%(rettype, name)) 216*4882a593Smuzhiyun plist=ParseCmdParams(cmd) 217*4882a593Smuzhiyun Comma="" 218*4882a593Smuzhiyun if len(plist): 219*4882a593Smuzhiyun for ptype, pname in plist: 220*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, ptype)) 221*4882a593Smuzhiyun Comma=", " 222*4882a593Smuzhiyun else: 223*4882a593Smuzhiyun self.outFile.write("void") 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun self.outFile.write(")\n{\n") 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun # for GL 1.0 and 1.1 functions, generate stdcall wrappers which call the function directly 228*4882a593Smuzhiyun if self.OldVersion: 229*4882a593Smuzhiyun if not nodebug: 230*4882a593Smuzhiyun self.outFile.write(' if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");\n'%(prefix.upper(), name)) 231*4882a593Smuzhiyun self.outFile.write(" glWinDirectProcCalls++;\n") 232*4882a593Smuzhiyun self.outFile.write("\n") 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun if rettype.lower()=="void": 235*4882a593Smuzhiyun self.outFile.write(" %s( "%(name)) 236*4882a593Smuzhiyun else: 237*4882a593Smuzhiyun self.outFile.write(" return %s( "%(name)) 238*4882a593Smuzhiyun 239*4882a593Smuzhiyun Comma="" 240*4882a593Smuzhiyun for ptype, pname in plist: 241*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, pname)) 242*4882a593Smuzhiyun Comma=", " 243*4882a593Smuzhiyun 244*4882a593Smuzhiyun # for GL 1.2+ functions, generate stdcall wrappers which use wglGetProcAddress() 245*4882a593Smuzhiyun else: 246*4882a593Smuzhiyun if rettype.lower()=="void": 247*4882a593Smuzhiyun self.outFile.write(' RESOLVE(PFN%sPROC, "%s");\n'%(name.upper(), name)) 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun if not nodebug: 250*4882a593Smuzhiyun self.outFile.write("\n") 251*4882a593Smuzhiyun self.outFile.write(' if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");\n'%(prefix.upper(), name)) 252*4882a593Smuzhiyun self.outFile.write("\n") 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun self.outFile.write(" RESOLVED_PROC(PFN%sPROC)( """%(name.upper())) 255*4882a593Smuzhiyun else: 256*4882a593Smuzhiyun self.outFile.write(' RESOLVE_RET(PFN%sPROC, "%s", FALSE);\n'%(name.upper(), name)) 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun if not nodebug: 259*4882a593Smuzhiyun self.outFile.write("\n") 260*4882a593Smuzhiyun self.outFile.write(' if (glxWinDebugSettings.enable%scallTrace) ErrorF("%s\\n");\n'%(prefix.upper(), name)) 261*4882a593Smuzhiyun self.outFile.write("\n") 262*4882a593Smuzhiyun 263*4882a593Smuzhiyun self.outFile.write(" return RESOLVED_PROC(PFN%sPROC)("%(name.upper())) 264*4882a593Smuzhiyun 265*4882a593Smuzhiyun Comma="" 266*4882a593Smuzhiyun for ptype, pname in plist: 267*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, pname)) 268*4882a593Smuzhiyun Comma=", " 269*4882a593Smuzhiyun self.outFile.write(" );\n}\n\n") 270*4882a593Smuzhiyun 271*4882a593Smuzhiyunclass ThunkOutputGenerator(OutputGenerator): 272*4882a593Smuzhiyun def __init__(self, 273*4882a593Smuzhiyun errFile = sys.stderr, 274*4882a593Smuzhiyun warnFile = sys.stderr, 275*4882a593Smuzhiyun diagFile = sys.stdout): 276*4882a593Smuzhiyun OutputGenerator.__init__(self, errFile, warnFile, diagFile) 277*4882a593Smuzhiyun def beginFile(self, genOpts): 278*4882a593Smuzhiyun self.outFile.write('/* Automatically generated from %s - DO NOT EDIT */\n\n'%regFilename) 279*4882a593Smuzhiyun def endFile(self): 280*4882a593Smuzhiyun pass 281*4882a593Smuzhiyun def beginFeature(self, interface, emit): 282*4882a593Smuzhiyun OutputGenerator.beginFeature(self, interface, emit) 283*4882a593Smuzhiyun self.OldVersion = (self.featureName in ['GL_VERSION_1_0', 'GL_VERSION_1_1']) 284*4882a593Smuzhiyun def endFeature(self): 285*4882a593Smuzhiyun OutputGenerator.endFeature(self) 286*4882a593Smuzhiyun def genType(self, typeinfo, name): 287*4882a593Smuzhiyun OutputGenerator.genType(self, typeinfo, name) 288*4882a593Smuzhiyun def genEnum(self, enuminfo, name): 289*4882a593Smuzhiyun OutputGenerator.genEnum(self, enuminfo, name) 290*4882a593Smuzhiyun def genCmd(self, cmd, name): 291*4882a593Smuzhiyun OutputGenerator.genCmd(self, cmd, name) 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun rettype=ParseCmdRettype(cmd) 294*4882a593Smuzhiyun self.outFile.write("%s %sWrapper("%(rettype, name)) 295*4882a593Smuzhiyun plist=ParseCmdParams(cmd) 296*4882a593Smuzhiyun 297*4882a593Smuzhiyun Comma="" 298*4882a593Smuzhiyun if len(plist): 299*4882a593Smuzhiyun for ptype, pname in plist: 300*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, ptype)) 301*4882a593Smuzhiyun Comma=", " 302*4882a593Smuzhiyun else: 303*4882a593Smuzhiyun self.outFile.write("void") 304*4882a593Smuzhiyun 305*4882a593Smuzhiyun self.outFile.write(")\n{\n") 306*4882a593Smuzhiyun 307*4882a593Smuzhiyun # for GL 1.0 and 1.1 functions, generate stdcall thunk wrappers which call the function directly 308*4882a593Smuzhiyun if self.OldVersion: 309*4882a593Smuzhiyun if rettype.lower()=="void": 310*4882a593Smuzhiyun self.outFile.write(" %s( "%(name)) 311*4882a593Smuzhiyun else: 312*4882a593Smuzhiyun self.outFile.write(" return %s( "%(name)) 313*4882a593Smuzhiyun 314*4882a593Smuzhiyun Comma="" 315*4882a593Smuzhiyun for ptype, pname in plist: 316*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, pname)) 317*4882a593Smuzhiyun Comma=", " 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun # for GL 1.2+ functions, generate wrappers which use wglGetProcAddress() 320*4882a593Smuzhiyun else: 321*4882a593Smuzhiyun if rettype.lower()=="void": 322*4882a593Smuzhiyun self.outFile.write(' RESOLVE(PFN%sPROC, "%s");\n'%(name.upper(), name)) 323*4882a593Smuzhiyun self.outFile.write(" RESOLVED_PROC(PFN%sPROC)( """%(name.upper())) 324*4882a593Smuzhiyun else: 325*4882a593Smuzhiyun self.outFile.write(' RESOLVE_RET(PFN%sPROC, "%s", FALSE);\n'%(name.upper(), name)) 326*4882a593Smuzhiyun self.outFile.write(" return RESOLVED_PROC(PFN%sPROC)("%(name.upper())) 327*4882a593Smuzhiyun 328*4882a593Smuzhiyun Comma="" 329*4882a593Smuzhiyun for ptype, pname in plist: 330*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, pname)) 331*4882a593Smuzhiyun Comma=", " 332*4882a593Smuzhiyun self.outFile.write(" );\n}\n\n") 333*4882a593Smuzhiyun 334*4882a593Smuzhiyunclass ThunkDefsOutputGenerator(OutputGenerator): 335*4882a593Smuzhiyun def __init__(self, 336*4882a593Smuzhiyun errFile = sys.stderr, 337*4882a593Smuzhiyun warnFile = sys.stderr, 338*4882a593Smuzhiyun diagFile = sys.stdout): 339*4882a593Smuzhiyun OutputGenerator.__init__(self, errFile, warnFile, diagFile) 340*4882a593Smuzhiyun def beginFile(self, genOpts): 341*4882a593Smuzhiyun self.outFile.write("EXPORTS\n"); # this must be the first line for libtool to realize this is a .def file 342*4882a593Smuzhiyun self.outFile.write('; Automatically generated from %s - DO NOT EDIT\n\n'%regFilename) 343*4882a593Smuzhiyun def endFile(self): 344*4882a593Smuzhiyun pass 345*4882a593Smuzhiyun def beginFeature(self, interface, emit): 346*4882a593Smuzhiyun OutputGenerator.beginFeature(self, interface, emit) 347*4882a593Smuzhiyun def endFeature(self): 348*4882a593Smuzhiyun OutputGenerator.endFeature(self) 349*4882a593Smuzhiyun def genType(self, typeinfo, name): 350*4882a593Smuzhiyun OutputGenerator.genType(self, typeinfo, name) 351*4882a593Smuzhiyun def genEnum(self, enuminfo, name): 352*4882a593Smuzhiyun OutputGenerator.genEnum(self, enuminfo, name) 353*4882a593Smuzhiyun def genCmd(self, cmd, name): 354*4882a593Smuzhiyun OutputGenerator.genCmd(self, cmd, name) 355*4882a593Smuzhiyun 356*4882a593Smuzhiyun # export the wrapper function with the name of the function it wraps 357*4882a593Smuzhiyun self.outFile.write("%s = %sWrapper\n"%(name, name)) 358*4882a593Smuzhiyun 359*4882a593Smuzhiyunclass ShimOutputGenerator(OutputGenerator): 360*4882a593Smuzhiyun def __init__(self, 361*4882a593Smuzhiyun errFile = sys.stderr, 362*4882a593Smuzhiyun warnFile = sys.stderr, 363*4882a593Smuzhiyun diagFile = sys.stdout): 364*4882a593Smuzhiyun OutputGenerator.__init__(self, errFile, warnFile, diagFile) 365*4882a593Smuzhiyun def beginFile(self, genOpts): 366*4882a593Smuzhiyun self.outFile.write('/* Automatically generated from %s - DO NOT EDIT */\n\n'%regFilename) 367*4882a593Smuzhiyun def endFile(self): 368*4882a593Smuzhiyun pass 369*4882a593Smuzhiyun def beginFeature(self, interface, emit): 370*4882a593Smuzhiyun OutputGenerator.beginFeature(self, interface, emit) 371*4882a593Smuzhiyun self.OldVersion = (self.featureName in ['GL_VERSION_1_0', 'GL_VERSION_1_1', 'GL_VERSION_1_2', 'GL_ARB_imaging', 'GL_ARB_multitexture', 'GL_ARB_texture_compression']) 372*4882a593Smuzhiyun def endFeature(self): 373*4882a593Smuzhiyun OutputGenerator.endFeature(self) 374*4882a593Smuzhiyun def genType(self, typeinfo, name): 375*4882a593Smuzhiyun OutputGenerator.genType(self, typeinfo, name) 376*4882a593Smuzhiyun def genEnum(self, enuminfo, name): 377*4882a593Smuzhiyun OutputGenerator.genEnum(self, enuminfo, name) 378*4882a593Smuzhiyun def genCmd(self, cmd, name): 379*4882a593Smuzhiyun OutputGenerator.genCmd(self, cmd, name) 380*4882a593Smuzhiyun 381*4882a593Smuzhiyun if not self.OldVersion: 382*4882a593Smuzhiyun return 383*4882a593Smuzhiyun 384*4882a593Smuzhiyun # for GL functions which are in the ABI, generate a shim which calls the function via GetProcAddress 385*4882a593Smuzhiyun rettype=ParseCmdRettype(cmd) 386*4882a593Smuzhiyun self.outFile.write("%s %s("%(rettype, name)) 387*4882a593Smuzhiyun plist=ParseCmdParams(cmd) 388*4882a593Smuzhiyun 389*4882a593Smuzhiyun Comma="" 390*4882a593Smuzhiyun if len(plist): 391*4882a593Smuzhiyun for ptype, pname in plist: 392*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, ptype)) 393*4882a593Smuzhiyun Comma=", " 394*4882a593Smuzhiyun else: 395*4882a593Smuzhiyun self.outFile.write("void") 396*4882a593Smuzhiyun 397*4882a593Smuzhiyun self.outFile.write(")\n{\n") 398*4882a593Smuzhiyun 399*4882a593Smuzhiyun self.outFile.write(' typedef %s (* PFN%sPROC)(' % (rettype, name.upper())) 400*4882a593Smuzhiyun 401*4882a593Smuzhiyun if len(plist): 402*4882a593Smuzhiyun Comma="" 403*4882a593Smuzhiyun for ptype, pname in plist: 404*4882a593Smuzhiyun self.outFile.write("%s %s"%(Comma, ptype)) 405*4882a593Smuzhiyun Comma=", " 406*4882a593Smuzhiyun else: 407*4882a593Smuzhiyun self.outFile.write("void") 408*4882a593Smuzhiyun 409*4882a593Smuzhiyun self.outFile.write(');\n') 410*4882a593Smuzhiyun 411*4882a593Smuzhiyun if rettype.lower()=="void": 412*4882a593Smuzhiyun self.outFile.write(' RESOLVE(PFN%sPROC, "%s");\n'%(name.upper(), name)) 413*4882a593Smuzhiyun self.outFile.write(' RESOLVED_PROC(') 414*4882a593Smuzhiyun else: 415*4882a593Smuzhiyun self.outFile.write(' RESOLVE_RET(PFN%sPROC, "%s", 0);\n'%(name.upper(), name)) 416*4882a593Smuzhiyun self.outFile.write(' return RESOLVED_PROC(') 417*4882a593Smuzhiyun 418*4882a593Smuzhiyun Comma="" 419*4882a593Smuzhiyun for ptype, pname in plist: 420*4882a593Smuzhiyun self.outFile.write("%s%s"%(Comma, pname)) 421*4882a593Smuzhiyun Comma=", " 422*4882a593Smuzhiyun 423*4882a593Smuzhiyun self.outFile.write(" );\n}\n\n") 424*4882a593Smuzhiyun 425*4882a593Smuzhiyundef genHeaders(): 426*4882a593Smuzhiyun outFile = open(outFilename,"w") 427*4882a593Smuzhiyun 428*4882a593Smuzhiyun if preresolve: 429*4882a593Smuzhiyun gen = PreResolveOutputGenerator(errFile=errWarn, 430*4882a593Smuzhiyun warnFile=errWarn, 431*4882a593Smuzhiyun diagFile=diag) 432*4882a593Smuzhiyun gen.outFile=outFile 433*4882a593Smuzhiyun reg.setGenerator(gen) 434*4882a593Smuzhiyun reg.apiGen(genOpts) 435*4882a593Smuzhiyun 436*4882a593Smuzhiyun if wrapper: 437*4882a593Smuzhiyun gen = WrapperOutputGenerator(errFile=errWarn, 438*4882a593Smuzhiyun warnFile=errWarn, 439*4882a593Smuzhiyun diagFile=diag) 440*4882a593Smuzhiyun gen.outFile=outFile 441*4882a593Smuzhiyun reg.setGenerator(gen) 442*4882a593Smuzhiyun reg.apiGen(genOpts) 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun if shim: 445*4882a593Smuzhiyun gen = ShimOutputGenerator(errFile=errWarn, 446*4882a593Smuzhiyun warnFile=errWarn, 447*4882a593Smuzhiyun diagFile=diag) 448*4882a593Smuzhiyun gen.outFile=outFile 449*4882a593Smuzhiyun reg.setGenerator(gen) 450*4882a593Smuzhiyun reg.apiGen(genOpts) 451*4882a593Smuzhiyun 452*4882a593Smuzhiyun if thunk: 453*4882a593Smuzhiyun gen = ThunkOutputGenerator(errFile=errWarn, 454*4882a593Smuzhiyun warnFile=errWarn, 455*4882a593Smuzhiyun diagFile=diag) 456*4882a593Smuzhiyun gen.outFile=outFile 457*4882a593Smuzhiyun reg.setGenerator(gen) 458*4882a593Smuzhiyun reg.apiGen(genOpts) 459*4882a593Smuzhiyun 460*4882a593Smuzhiyun 461*4882a593Smuzhiyun if thunkdefs: 462*4882a593Smuzhiyun gen = ThunkDefsOutputGenerator(errFile=errWarn, 463*4882a593Smuzhiyun warnFile=errWarn, 464*4882a593Smuzhiyun diagFile=diag) 465*4882a593Smuzhiyun gen.outFile=outFile 466*4882a593Smuzhiyun reg.setGenerator(gen) 467*4882a593Smuzhiyun reg.apiGen(genOpts) 468*4882a593Smuzhiyun 469*4882a593Smuzhiyun outFile.close() 470*4882a593Smuzhiyun 471*4882a593SmuzhiyungenHeaders() 472