Bug 1470678 [wpt PR 11640] - Add a test for text-decoration in tables quirk, a=testonly
[gecko.git] / gfx / gl / GLParseRegistryXML.py
blob1fe24b2dfe3e323cf1ed4edf4881aded6475c319
1 #!/usr/bin/env python
2 # coding=utf8
5 ################################################################################
6 # TUTORIAL
7 # This script will generate GLConsts.h
9 # Step 1:
10 # Download the last gl.xml, egl.xml, glx.xml and wgl.xml from
11 # http://www.opengl.org/registry/#specfiles into a directory of your choice
13 # Step 2:
14 # Execute this script ./GLParseRegistryXML.py [your dir containing XML files]
16 # Step 3:
17 # Do not add the downloaded XML in the patch
19 # Step 4:
20 # Enjoy =)
22 ################################################################################
24 # includes
25 from __future__ import print_function
26 import os
27 import sys
28 import xml.etree.ElementTree
31 ################################################################################
32 # export management
34 class GLConstHeader:
35 def __init__(self, f):
36 self.f = f
38 def write(self, arg):
39 if isinstance(arg, list):
40 self.f.write('\n'.join(arg) + '\n')
41 elif isinstance(arg, (int, long)):
42 self.f.write('\n' * arg)
43 else:
44 self.f.write(str(arg) + '\n')
46 def formatFileBegin(self):
47 self.write([
48 '/* This Source Code Form is subject to the terms of the Mozilla Public',
49 ' * License, v. 2.0. If a copy of the MPL was not distributed with this',
50 ' * file, You can obtain one at http://mozilla.org/MPL/2.0/. */',
51 '',
52 '#ifndef GLCONSTS_H_',
53 '#define GLCONSTS_H_',
54 '',
55 '/**',
56 ' * GENERATED FILE, DO NOT MODIFY DIRECTLY.',
57 ' * This is a file generated directly from the official OpenGL registry',
58 ' * xml available http://www.opengl.org/registry/#specfiles.',
59 ' *',
60 ' * To generate this file, see tutorial in \'GLParseRegistryXML.py\'.',
61 ' */',
65 def formatLibBegin(self, lib):
66 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
67 self.write('// ' + lib)
69 def formatLibConstant(self, lib, name, value):
70 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
71 # name is the name of the const (example: MAX_TEXTURE_SIZE)
72 # value is the value of the const (example: 0xABCD)
74 define = '#define LOCAL_' + lib + '_' + name
75 whitespace = 60 - len(define)
77 if whitespace < 0:
78 whitespace = whitespace % 8
80 self.write(define + ' ' * whitespace + ' ' + value)
82 def formatLibEnd(self, lib):
83 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
84 self.write(2)
86 def formatFileEnd(self):
87 self.write([
88 '',
89 '#endif // GLCONSTS_H_'
93 ################################################################################
94 # underground code
96 def getScriptDir():
97 return os.path.dirname(__file__) + '/'
100 def getXMLDir():
101 if len(sys.argv) == 1:
102 return './'
104 dirPath = sys.argv[1]
105 if dirPath[-1] != '/':
106 dirPath += '/'
108 return dirPath
111 class GLConst:
112 def __init__(self, lib, name, value, type):
113 self.lib = lib
114 self.name = name
115 self.value = value
116 self.type = type
119 class GLDatabase:
121 LIBS = ['GL', 'EGL', 'GLX', 'WGL']
123 def __init__(self):
124 self.consts = {}
125 self.libs = set(GLDatabase.LIBS)
126 self.vendors = set(['EXT', 'ATI'])
127 # there is no vendor="EXT" and vendor="ATI" in gl.xml,
128 # so we manualy declare them
130 def loadXML(self, path):
131 xmlPath = getXMLDir() + path
133 if not os.path.isfile(xmlPath):
134 print('missing file "' + xmlPath + '"')
135 return False
137 tree = xml.etree.ElementTree.parse(xmlPath)
138 root = tree.getroot()
140 for enums in root.iter('enums'):
141 vendor = enums.get('vendor')
142 if not vendor:
143 # there some standart enums that do have the vendor attribute,
144 # so we fake them as ARB's enums
145 vendor = 'ARB'
147 if vendor not in self.vendors:
148 # we map this new vendor in the vendors set.
149 self.vendors.add(vendor)
151 namespaceType = enums.get('type')
153 for enum in enums:
154 if enum.tag != 'enum':
155 # this is not an enum => we skip it
156 continue
158 lib = enum.get('name').split('_')[0]
160 if lib not in self.libs:
161 # unknown library => we skip it
162 continue
164 name = enum.get('name')[len(lib) + 1:]
165 value = enum.get('value')
166 type = enum.get('type')
168 if not type:
169 # if no type specified, we get the namespace's default type
170 type = namespaceType
172 self.consts[lib + '_' + name] = GLConst(lib, name, value, type)
174 return True
176 def exportConsts(self, path):
177 with open(getScriptDir() + path, 'w') as f:
179 headerFile = GLConstHeader(f)
180 headerFile.formatFileBegin()
182 constNames = self.consts.keys()
183 constNames.sort()
185 for lib in GLDatabase.LIBS:
186 headerFile.formatLibBegin(lib)
188 for constName in constNames:
189 const = self.consts[constName]
191 if const.lib != lib:
192 continue
194 headerFile.formatLibConstant(lib, const.name, const.value)
196 headerFile.formatLibEnd(lib)
198 headerFile.formatFileEnd()
201 glDatabase = GLDatabase()
203 success = glDatabase.loadXML('gl.xml')
204 success = success and glDatabase.loadXML('egl.xml')
205 success = success and glDatabase.loadXML('glx.xml')
206 success = success and glDatabase.loadXML('wgl.xml')
208 if success:
209 glDatabase.exportConsts('GLConsts.h')