9 ### Command line processing ###
13 extgen.py [api] <extension> [<core_version>] [<secondary> ...]
14 extgen.py [api] <extfile> [<outfile>]
16 Reads gl.xml and generates files to use <extension>. Any promoted functions
17 are exposed with their promoted names. If <secondary> extensions are given,
18 any promoted functions from those are pulled in as well. <core_version> can
19 be given to override the version where <extension> was promoted to core.
21 In the second form, the parameters are read from <extfile>. If <outfile> is
22 absent, the extension's lowercased name is used. Anything after the last dot
23 in <outfile> is removed and replaced with cpp and h."""
29 if sys.argv[i].startswith("gl"):
30 target_api = sys.argv[i]
33 target_ext = sys.argv[i]
36 if target_ext.endswith(".glext"):
43 if parts[0]=="extension":
45 elif parts[0]=="core_version":
46 if parts[1]==target_api:
47 core_version = parts[2]
48 elif parts[0]=="secondary":
49 secondary.append(parts[1])
50 elif parts[0]=="backport":
51 backport_ext = parts[1]
53 out_base = os.path.splitext(sys.argv[i+1])[0]
55 secondary = sys.argv[i+1:]
57 if secondary and secondary[0][0].isdigit():
58 core_version = secondary.pop(0)
60 ext_type = target_ext.split('_')[0]
63 core_version = map(int, core_version.split('.'))
66 out_base = target_ext.lower()
68 ### XML file parsing ###
74 def __init__(self, name, kind):
79 self.supported_apis = {}
83 class Function(Thing):
84 def __init__(self, name):
85 Thing.__init__(self, name, Thing.FUNCTION)
86 self.return_type = "void"
91 def __init__(self, name):
92 Thing.__init__(self, name, Thing.ENUM)
94 self.bitmask = (name.endswith("_BIT") or "_BIT_" in name)
97 def __init__(self, name):
99 self.supported_apis = []
100 underscore = name.find('_')
101 self.ext_type = name[0:underscore]
102 self.base_name = name[underscore+1:]
107 def get_nested_elements(elem, path):
108 childElements = [c for c in elem.childNodes if c.nodeType==xml.dom.Node.ELEMENT_NODE]
110 head, tail = path.split('/', 1)
112 for c in childElements:
114 result += get_nested_elements(c, tail)
117 return [c for c in childElements if c.tagName==path]
119 def get_first_child(elem, tag):
120 for c in elem.childNodes:
121 if c.nodeType==xml.dom.Node.ELEMENT_NODE and c.tagName==tag:
125 def get_text_contents(node):
127 for c in node.childNodes:
128 if c.nodeType==xml.dom.Node.TEXT_NODE or c.nodeType==xml.dom.Node.CDATA_SECTION_NODE:
131 result += get_text_contents(c)
134 def parse_command(cmd):
135 proto = get_first_child(cmd, "proto")
136 name = get_text_contents(get_first_child(proto, "name"))
137 func = things.get(name)
139 func = Function(name)
142 aliases = get_nested_elements(cmd, "alias")
143 func.aliases = [a.getAttribute("name") for a in aliases]
145 ptype = get_first_child(proto, "ptype")
147 func.return_type = get_text_contents(ptype)
149 for c in proto.childNodes:
150 if c.nodeType==xml.dom.Node.TEXT_NODE and c.data.strip():
151 func.return_type = c.data.strip()
154 params = get_nested_elements(cmd, "param")
155 func.params = map(get_text_contents, params)
158 name = en.getAttribute("name")
159 enum = things.get(name)
164 enum.value = int(en.getAttribute("value"), 16)
166 def parse_feature(feat):
167 api = feat.getAttribute("api")
168 version = feat.getAttribute("number")
170 version = map(int, version.split('.'))
174 requires = get_nested_elements(feat, "require")
176 commands = get_nested_elements(req, "command")
177 enums = get_nested_elements(req, "enum")
178 for t in itertools.chain(commands, enums):
179 name = t.getAttribute("name")
180 thing = things.get(name)
182 thing.supported_apis.setdefault(api, version)
184 if not api or api==target_api:
185 removes = get_nested_elements(feat, "remove")
187 profile = rem.getAttribute("profile")
188 commands = get_nested_elements(rem, "command")
189 enums = get_nested_elements(rem, "enum")
191 for t in itertools.chain(commands, enums):
192 name = t.getAttribute("name")
193 if profile!="core" and name in things:
196 def parse_extension(ext):
197 ext_name = ext.getAttribute("name")
198 if ext_name.startswith("GL_"):
199 ext_name = ext_name[3:]
201 supported = ext.getAttribute("supported").split('|')
202 if target_api not in supported and ext_name!=target_ext:
205 extension = extensions.get(ext_name)
207 extension = Extension(ext_name)
208 extensions[ext_name] = extension
210 extension.supported_apis = supported
212 requires = get_nested_elements(ext, "require")
214 api = req.getAttribute("api")
218 supported = extension.supported_apis
220 commands = get_nested_elements(req, "command")
221 enums = get_nested_elements(req, "enum")
222 for t in itertools.chain(commands, enums):
223 name = t.getAttribute("name")
224 thing = things.get(name)
226 if thing.extension and extension.name!=target_ext:
227 if thing.extension.ext_type=="ARB" or thing.extension.name==target_ext:
229 if thing.extension.ext_type=="EXT" and extension.ext_type!="ARB":
232 thing.extension = extension
234 thing.supported_apis.setdefault(a, "ext")
237 doc = xml.dom.minidom.parse(fn)
238 root = doc.documentElement
240 commands = get_nested_elements(root, "commands/command")
244 enums = get_nested_elements(root, "enums/enum")
248 features = get_nested_elements(root, "feature")
249 for feat in features:
252 extensions = get_nested_elements(root, "extensions/extension")
253 for ext in extensions:
257 parse_file("gl.fixes.xml")
259 ### Additional processing ###
261 if target_ext in extensions:
262 target_ext = extensions[target_ext]
264 print "Extension %s not found"%target_ext
267 # Find aliases for enums
268 enums = [t for t in things.itervalues() if t.kind==Thing.ENUM]
269 core_enums = [e for e in enums if any(v!="ext" for v in e.supported_apis.itervalues())]
270 core_enums_by_value = dict((e.value, None) for e in core_enums)
272 def get_key_api(things):
273 common_apis = set(target_ext.supported_apis)
275 common_apis.intersection_update(t.supported_apis.keys())
277 return common_apis.pop()
282 if all(v=="ext" for v in e.supported_apis.values()) and e.value in core_enums_by_value:
283 if core_enums_by_value[e.value] is None:
284 candidates = [ce for ce in core_enums if ce.value==e.value]
285 key_api = get_key_api(candidates)
286 core_enums_by_value[e.value] = list(sorted(candidates, key=(lambda x: x.supported_apis.get(key_api, "ext"))))
287 for ce in core_enums_by_value[e.value]:
288 if ce.bitmask==e.bitmask:
289 e.aliases.append(ce.name)
292 # Create references from core things to their extension counterparts
293 for t in things.itervalues():
296 alias = things.get(a)
298 if target_api in t.supported_apis:
299 alias.sources.insert(0, t)
301 alias.sources.append(t)
303 # Find the things we want to include in this extension
305 # Unpromoted extension things are relevant
306 if t.extension and t.extension==target_ext and not t.aliases:
309 # Core things promoted from the extension are also relevant
311 if s.extension==target_ext or s.extension.name in secondary:
316 funcs = [t for t in things.itervalues() if t.kind==Thing.FUNCTION and is_relevant(t)]
317 funcs.sort(key=(lambda f: f.name))
318 enums = filter(is_relevant, enums)
319 enums.sort(key=(lambda e: e.value))
321 # Some final preparations for creating the files
322 backport_ext_candidates = []
323 for t in itertools.chain(funcs, enums):
324 if target_api in t.supported_apis and t.supported_apis[target_api]!="ext":
325 t.version = t.supported_apis[target_api]
327 core_version = t.version
329 # Things in backport extensions don't acquire an extension suffix
330 if t.extension and not t.name.endswith(ext_type) and target_api in t.supported_apis:
331 if t.extension not in backport_ext_candidates:
332 backport_ext_candidates.append(t.extension)
335 if backport_ext=="none":
338 backport_ext = extensions[backport_ext]
340 if backport_ext not in backport_ext_candidates:
341 print "Warning: explicitly specified backport extension %s does not look like a backport extension"
342 elif backport_ext_candidates:
343 if len(backport_ext_candidates)>1:
344 print "Warning: multiple backport extension candidates: %s"%(" ".join(e.name for e in backport_ext_candidates))
346 for e in backport_ext_candidates:
347 if e.base_name==target_ext.base_name:
350 if not backport_ext and len(backport_ext_candidates)==1:
351 print "Warning: potential backport extension has mismatched name: %s"%backport_ext_candidates[0].name
354 f.typedef = "FPtr_%s"%f.name
356 if target_api in target_ext.supported_apis:
357 source_ext = target_ext
360 for t in itertools.chain(funcs, enums):
362 if target_api in s.supported_apis:
363 candidates[s.extension.name] = candidates.get(s.extension.name, 0)+1
365 source_ext = extensions[max(candidates.iteritems(), key=(lambda x: x[1]))[0]]
370 any_supported = False
372 for t in itertools.chain(funcs, enums):
373 if target_api in t.supported_apis:
376 all_supported = False
378 if not any_supported:
379 print "Warning: %s is not supported by the target API"%target_ext.name
380 elif not all_supported:
381 print "Warning: %s is only partially supported by the target API"%target_ext.name
383 label = "Warning: Unsupported tokens: "
384 for t in itertools.chain(funcs, enums):
385 if target_api not in t.supported_apis:
386 if unsupported and len(label)+len(unsupported)+2+len(t.name)>78:
387 print label+unsupported
388 label = " "*len(label)
392 unsupported += t.name
394 print label+unsupported
398 out = file(out_base+".h", "w")
399 out.write("#ifndef MSP_GL_%s_\n"%target_ext.name.upper())
400 out.write("#define MSP_GL_%s_\n"%target_ext.name.upper())
403 #include <msp/gl/extension.h>
404 #include <msp/gl/gl.h>
414 out.write("typedef %s (APIENTRY *%s)(%s);\n"%(f.return_type, f.typedef, ", ".join(f.params)))
419 if target_api=="gles2":
422 enums_by_category = {}
426 cat = api_prefix+"_VERSION_"+"_".join(map(str, e.version))
428 cat = "GL_"+e.extension.name
429 enums_by_category.setdefault(cat, []).append(e)
431 for cat in sorted(enums_by_category.keys()):
433 out.write("#ifndef %s\n"%cat)
434 for e in enums_by_category[cat]:
435 out.write("#define %s 0x%04X\n"%(e.name, e.value))
437 out.write("#endif\n")
441 out.write("extern %s %s;\n"%(f.typedef, f.name))
443 out.write("extern Extension %s;\n"%target_ext.name)
452 out = file(out_base+".cpp", "w")
453 out.write("#include \"%s.h\"\n"%target_ext.name.lower())
458 #define GET_PROC_ADDRESS(x) &::x
460 #define GET_PROC_ADDRESS(x) get_proc_address(#x)
464 #define GET_PROC_ADDRESS_1_1(x) &::x
466 #define GET_PROC_ADDRESS_1_1(x) GET_PROC_ADDRESS(x)
476 out.write("%s %s = 0;\n"%(f.typedef, f.name))
478 out.write("\nExtension::SupportLevel init_%s()\n{\n"%target_ext.name.lower())
480 out.write("\tif(is_version_at_least(%d, %d)"%tuple(core_version))
482 out.write(" || is_supported(\"GL_%s\")"%backport_ext.name)
483 out.write(")\n\t{\n")
485 if target_api in f.supported_apis:
487 if f.version is not None and f.version<=[1, 1]:
489 out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS%s(%s));\n"%(f.name, f.typedef, gpa_suffix, f.name))
490 out.write("\t\treturn Extension::CORE;\n")
492 if source_ext and source_ext!=backport_ext:
493 out.write("\tif(is_supported(\"GL_%s\"))\n\t{\n"%(source_ext.name))
498 if target_api in s.supported_apis:
499 out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, s.name))
500 out.write("\t\treturn Extension::EXTENSION;\n")
502 out.write("\treturn Extension::UNSUPPORTED;\n")
505 out.write("\nExtension %s(\"GL_%s\", init_%s);\n"%(target_ext.name, target_ext.name, target_ext.name.lower()))