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