Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # coding=utf8 |
michael@0 | 3 | |
michael@0 | 4 | |
michael@0 | 5 | ################################################################################ |
michael@0 | 6 | # TUTORIAL |
michael@0 | 7 | # This script will generate GLConsts.h |
michael@0 | 8 | # |
michael@0 | 9 | # Step 1: |
michael@0 | 10 | # Download the last gl.xml, egl.xml, glx.xml and wgl.xml from |
michael@0 | 11 | # http://www.opengl.org/registry/#specfiles into a directory of your choice |
michael@0 | 12 | # |
michael@0 | 13 | # Step 2: |
michael@0 | 14 | # Execute this script ./GLParseRegistryXML.py [your dir containing XML files] |
michael@0 | 15 | # |
michael@0 | 16 | # Step 3: |
michael@0 | 17 | # Do not add the downloaded XML in the patch |
michael@0 | 18 | # |
michael@0 | 19 | # Step 4: |
michael@0 | 20 | # Enjoy =) |
michael@0 | 21 | # |
michael@0 | 22 | ################################################################################ |
michael@0 | 23 | |
michael@0 | 24 | # includes |
michael@0 | 25 | import os |
michael@0 | 26 | import sys |
michael@0 | 27 | import xml.etree.ElementTree |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | ################################################################################ |
michael@0 | 31 | # export management |
michael@0 | 32 | |
michael@0 | 33 | class GLConstHeader: |
michael@0 | 34 | def __init__(self, f): |
michael@0 | 35 | self.f = f |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | def write(self, arg): |
michael@0 | 39 | if isinstance(arg, list): |
michael@0 | 40 | self.f.write('\n'.join(arg) + '\n') |
michael@0 | 41 | elif isinstance(arg, (int, long)): |
michael@0 | 42 | self.f.write('\n' * arg) |
michael@0 | 43 | else: |
michael@0 | 44 | self.f.write(str(arg) + '\n') |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | def formatFileBegin(self): |
michael@0 | 48 | self.write([ |
michael@0 | 49 | '/* This Source Code Form is subject to the terms of the Mozilla Public', |
michael@0 | 50 | ' * License, v. 2.0. If a copy of the MPL was not distributed with this', |
michael@0 | 51 | ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */', |
michael@0 | 52 | '', |
michael@0 | 53 | '#ifndef GLCONSTS_H_', |
michael@0 | 54 | '#define GLCONSTS_H_', |
michael@0 | 55 | '', |
michael@0 | 56 | '/**', |
michael@0 | 57 | ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.', |
michael@0 | 58 | ' * This is a file generated directly from the official OpenGL registry', |
michael@0 | 59 | ' * xml available http://www.opengl.org/registry/#specfiles.', |
michael@0 | 60 | ' *', |
michael@0 | 61 | ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.', |
michael@0 | 62 | ' */', |
michael@0 | 63 | '' |
michael@0 | 64 | ]) |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | def formatLibBegin(self, lib): |
michael@0 | 68 | # lib would be 'GL', 'EGL', 'GLX' or 'WGL' |
michael@0 | 69 | self.write('// ' + lib) |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | def formatLibConstant(self, lib, name, value): |
michael@0 | 73 | # lib would be 'GL', 'EGL', 'GLX' or 'WGL' |
michael@0 | 74 | # name is the name of the const (example: MAX_TEXTURE_SIZE) |
michael@0 | 75 | # value is the value of the const (example: 0xABCD) |
michael@0 | 76 | |
michael@0 | 77 | define = '#define LOCAL_' + lib + '_' + name |
michael@0 | 78 | whitespace = 60 - len(define) |
michael@0 | 79 | |
michael@0 | 80 | if whitespace < 0: |
michael@0 | 81 | whitespace = whitespace % 8 |
michael@0 | 82 | |
michael@0 | 83 | self.write(define + ' ' * whitespace + ' ' + value) |
michael@0 | 84 | |
michael@0 | 85 | |
michael@0 | 86 | def formatLibEnd(self, lib): |
michael@0 | 87 | # lib would be 'GL', 'EGL', 'GLX' or 'WGL' |
michael@0 | 88 | self.write(2) |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | def formatFileEnd(self): |
michael@0 | 92 | self.write([ |
michael@0 | 93 | '', |
michael@0 | 94 | '#endif // GLCONSTS_H_' |
michael@0 | 95 | ]) |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | ################################################################################ |
michael@0 | 99 | # underground code |
michael@0 | 100 | |
michael@0 | 101 | def getScriptDir(): |
michael@0 | 102 | return os.path.dirname(__file__) + '/' |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | def getXMLDir(): |
michael@0 | 106 | if len(sys.argv) == 1: |
michael@0 | 107 | return './' |
michael@0 | 108 | |
michael@0 | 109 | dirPath = sys.argv[1] |
michael@0 | 110 | if dirPath[-1] != '/': |
michael@0 | 111 | dirPath += '/' |
michael@0 | 112 | |
michael@0 | 113 | return dirPath |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | class GLConst: |
michael@0 | 117 | def __init__(self, lib, name, value, type): |
michael@0 | 118 | self.lib = lib |
michael@0 | 119 | self.name = name |
michael@0 | 120 | self.value = value |
michael@0 | 121 | self.type = type |
michael@0 | 122 | |
michael@0 | 123 | |
michael@0 | 124 | class GLDatabase: |
michael@0 | 125 | |
michael@0 | 126 | LIBS = ['GL', 'EGL', 'GLX', 'WGL'] |
michael@0 | 127 | |
michael@0 | 128 | def __init__(self): |
michael@0 | 129 | self.consts = {} |
michael@0 | 130 | self.libs = set(GLDatabase.LIBS) |
michael@0 | 131 | self.vendors = set(['EXT', 'ATI']) |
michael@0 | 132 | # there is no vendor="EXT" and vendor="ATI" in gl.xml, |
michael@0 | 133 | # so we manualy declare them |
michael@0 | 134 | |
michael@0 | 135 | |
michael@0 | 136 | def loadXML(self, path): |
michael@0 | 137 | xmlPath = getXMLDir() + path |
michael@0 | 138 | |
michael@0 | 139 | if not os.path.isfile(xmlPath): |
michael@0 | 140 | print 'missing file "' + xmlPath + '"' |
michael@0 | 141 | return False |
michael@0 | 142 | |
michael@0 | 143 | tree = xml.etree.ElementTree.parse(xmlPath) |
michael@0 | 144 | root = tree.getroot() |
michael@0 | 145 | |
michael@0 | 146 | for enums in root.iter('enums'): |
michael@0 | 147 | vendor = enums.get('vendor') |
michael@0 | 148 | if not vendor: |
michael@0 | 149 | # there some standart enums that do have the vendor attribute, |
michael@0 | 150 | # so we fake them as ARB's enums |
michael@0 | 151 | vendor = 'ARB' |
michael@0 | 152 | |
michael@0 | 153 | if vendor not in self.vendors: |
michael@0 | 154 | # we map this new vendor in the vendors set. |
michael@0 | 155 | self.vendors.add(vendor) |
michael@0 | 156 | |
michael@0 | 157 | namespaceType = enums.get('type') |
michael@0 | 158 | |
michael@0 | 159 | for enum in enums: |
michael@0 | 160 | if enum.tag != 'enum': |
michael@0 | 161 | # this is not an enum => we skip it |
michael@0 | 162 | continue |
michael@0 | 163 | |
michael@0 | 164 | lib = enum.get('name').split('_')[0] |
michael@0 | 165 | |
michael@0 | 166 | if lib not in self.libs: |
michael@0 | 167 | # unknown library => we skip it |
michael@0 | 168 | continue |
michael@0 | 169 | |
michael@0 | 170 | name = enum.get('name')[len(lib) + 1:] |
michael@0 | 171 | value = enum.get('value') |
michael@0 | 172 | type = enum.get('type') |
michael@0 | 173 | |
michael@0 | 174 | if not type: |
michael@0 | 175 | # if no type specified, we get the namespace's default type |
michael@0 | 176 | type = namespaceType |
michael@0 | 177 | |
michael@0 | 178 | self.consts[lib + '_' + name] = GLConst(lib, name, value, type) |
michael@0 | 179 | |
michael@0 | 180 | return True |
michael@0 | 181 | |
michael@0 | 182 | |
michael@0 | 183 | def exportConsts(self, path): |
michael@0 | 184 | with open(getScriptDir() + path,'w') as f: |
michael@0 | 185 | |
michael@0 | 186 | headerFile = GLConstHeader(f) |
michael@0 | 187 | headerFile.formatFileBegin() |
michael@0 | 188 | |
michael@0 | 189 | constNames = self.consts.keys() |
michael@0 | 190 | constNames.sort() |
michael@0 | 191 | |
michael@0 | 192 | for lib in GLDatabase.LIBS: |
michael@0 | 193 | headerFile.formatLibBegin(lib) |
michael@0 | 194 | |
michael@0 | 195 | for constName in constNames: |
michael@0 | 196 | const = self.consts[constName] |
michael@0 | 197 | |
michael@0 | 198 | if const.lib != lib: |
michael@0 | 199 | continue |
michael@0 | 200 | |
michael@0 | 201 | headerFile.formatLibConstant(lib, const.name, const.value) |
michael@0 | 202 | |
michael@0 | 203 | headerFile.formatLibEnd(lib) |
michael@0 | 204 | |
michael@0 | 205 | headerFile.formatFileEnd() |
michael@0 | 206 | |
michael@0 | 207 | |
michael@0 | 208 | glDatabase = GLDatabase() |
michael@0 | 209 | |
michael@0 | 210 | success = glDatabase.loadXML('gl.xml') |
michael@0 | 211 | success = success and glDatabase.loadXML('egl.xml') |
michael@0 | 212 | success = success and glDatabase.loadXML('glx.xml') |
michael@0 | 213 | success = success and glDatabase.loadXML('wgl.xml') |
michael@0 | 214 | |
michael@0 | 215 | if success: |
michael@0 | 216 | glDatabase.exportConsts('GLConsts.h') |