michael@0: #!/usr/bin/env python michael@0: # coding=utf8 michael@0: michael@0: michael@0: ################################################################################ michael@0: # TUTORIAL michael@0: # This script will generate GLConsts.h michael@0: # michael@0: # Step 1: michael@0: # Download the last gl.xml, egl.xml, glx.xml and wgl.xml from michael@0: # http://www.opengl.org/registry/#specfiles into a directory of your choice michael@0: # michael@0: # Step 2: michael@0: # Execute this script ./GLParseRegistryXML.py [your dir containing XML files] michael@0: # michael@0: # Step 3: michael@0: # Do not add the downloaded XML in the patch michael@0: # michael@0: # Step 4: michael@0: # Enjoy =) michael@0: # michael@0: ################################################################################ michael@0: michael@0: # includes michael@0: import os michael@0: import sys michael@0: import xml.etree.ElementTree michael@0: michael@0: michael@0: ################################################################################ michael@0: # export management michael@0: michael@0: class GLConstHeader: michael@0: def __init__(self, f): michael@0: self.f = f michael@0: michael@0: michael@0: def write(self, arg): michael@0: if isinstance(arg, list): michael@0: self.f.write('\n'.join(arg) + '\n') michael@0: elif isinstance(arg, (int, long)): michael@0: self.f.write('\n' * arg) michael@0: else: michael@0: self.f.write(str(arg) + '\n') michael@0: michael@0: michael@0: def formatFileBegin(self): michael@0: self.write([ michael@0: '/* This Source Code Form is subject to the terms of the Mozilla Public', michael@0: ' * License, v. 2.0. If a copy of the MPL was not distributed with this', michael@0: ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */', michael@0: '', michael@0: '#ifndef GLCONSTS_H_', michael@0: '#define GLCONSTS_H_', michael@0: '', michael@0: '/**', michael@0: ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.', michael@0: ' * This is a file generated directly from the official OpenGL registry', michael@0: ' * xml available http://www.opengl.org/registry/#specfiles.', michael@0: ' *', michael@0: ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.', michael@0: ' */', michael@0: '' michael@0: ]) michael@0: michael@0: michael@0: def formatLibBegin(self, lib): michael@0: # lib would be 'GL', 'EGL', 'GLX' or 'WGL' michael@0: self.write('// ' + lib) michael@0: michael@0: michael@0: def formatLibConstant(self, lib, name, value): michael@0: # lib would be 'GL', 'EGL', 'GLX' or 'WGL' michael@0: # name is the name of the const (example: MAX_TEXTURE_SIZE) michael@0: # value is the value of the const (example: 0xABCD) michael@0: michael@0: define = '#define LOCAL_' + lib + '_' + name michael@0: whitespace = 60 - len(define) michael@0: michael@0: if whitespace < 0: michael@0: whitespace = whitespace % 8 michael@0: michael@0: self.write(define + ' ' * whitespace + ' ' + value) michael@0: michael@0: michael@0: def formatLibEnd(self, lib): michael@0: # lib would be 'GL', 'EGL', 'GLX' or 'WGL' michael@0: self.write(2) michael@0: michael@0: michael@0: def formatFileEnd(self): michael@0: self.write([ michael@0: '', michael@0: '#endif // GLCONSTS_H_' michael@0: ]) michael@0: michael@0: michael@0: ################################################################################ michael@0: # underground code michael@0: michael@0: def getScriptDir(): michael@0: return os.path.dirname(__file__) + '/' michael@0: michael@0: michael@0: def getXMLDir(): michael@0: if len(sys.argv) == 1: michael@0: return './' michael@0: michael@0: dirPath = sys.argv[1] michael@0: if dirPath[-1] != '/': michael@0: dirPath += '/' michael@0: michael@0: return dirPath michael@0: michael@0: michael@0: class GLConst: michael@0: def __init__(self, lib, name, value, type): michael@0: self.lib = lib michael@0: self.name = name michael@0: self.value = value michael@0: self.type = type michael@0: michael@0: michael@0: class GLDatabase: michael@0: michael@0: LIBS = ['GL', 'EGL', 'GLX', 'WGL'] michael@0: michael@0: def __init__(self): michael@0: self.consts = {} michael@0: self.libs = set(GLDatabase.LIBS) michael@0: self.vendors = set(['EXT', 'ATI']) michael@0: # there is no vendor="EXT" and vendor="ATI" in gl.xml, michael@0: # so we manualy declare them michael@0: michael@0: michael@0: def loadXML(self, path): michael@0: xmlPath = getXMLDir() + path michael@0: michael@0: if not os.path.isfile(xmlPath): michael@0: print 'missing file "' + xmlPath + '"' michael@0: return False michael@0: michael@0: tree = xml.etree.ElementTree.parse(xmlPath) michael@0: root = tree.getroot() michael@0: michael@0: for enums in root.iter('enums'): michael@0: vendor = enums.get('vendor') michael@0: if not vendor: michael@0: # there some standart enums that do have the vendor attribute, michael@0: # so we fake them as ARB's enums michael@0: vendor = 'ARB' michael@0: michael@0: if vendor not in self.vendors: michael@0: # we map this new vendor in the vendors set. michael@0: self.vendors.add(vendor) michael@0: michael@0: namespaceType = enums.get('type') michael@0: michael@0: for enum in enums: michael@0: if enum.tag != 'enum': michael@0: # this is not an enum => we skip it michael@0: continue michael@0: michael@0: lib = enum.get('name').split('_')[0] michael@0: michael@0: if lib not in self.libs: michael@0: # unknown library => we skip it michael@0: continue michael@0: michael@0: name = enum.get('name')[len(lib) + 1:] michael@0: value = enum.get('value') michael@0: type = enum.get('type') michael@0: michael@0: if not type: michael@0: # if no type specified, we get the namespace's default type michael@0: type = namespaceType michael@0: michael@0: self.consts[lib + '_' + name] = GLConst(lib, name, value, type) michael@0: michael@0: return True michael@0: michael@0: michael@0: def exportConsts(self, path): michael@0: with open(getScriptDir() + path,'w') as f: michael@0: michael@0: headerFile = GLConstHeader(f) michael@0: headerFile.formatFileBegin() michael@0: michael@0: constNames = self.consts.keys() michael@0: constNames.sort() michael@0: michael@0: for lib in GLDatabase.LIBS: michael@0: headerFile.formatLibBegin(lib) michael@0: michael@0: for constName in constNames: michael@0: const = self.consts[constName] michael@0: michael@0: if const.lib != lib: michael@0: continue michael@0: michael@0: headerFile.formatLibConstant(lib, const.name, const.value) michael@0: michael@0: headerFile.formatLibEnd(lib) michael@0: michael@0: headerFile.formatFileEnd() michael@0: michael@0: michael@0: glDatabase = GLDatabase() michael@0: michael@0: success = glDatabase.loadXML('gl.xml') michael@0: success = success and glDatabase.loadXML('egl.xml') michael@0: success = success and glDatabase.loadXML('glx.xml') michael@0: success = success and glDatabase.loadXML('wgl.xml') michael@0: michael@0: if success: michael@0: glDatabase.exportConsts('GLConsts.h')