Bug 1886451: Add missing ifdef Nightly guards. r=dminor
[gecko.git] / gfx / gl / GLConsts.py
blob07d70c662a13983935e12cdc08d1b7767380cf8a
1 #!/usr/bin/env python3
3 """
4 This script will regenerate and update GLConsts.h.
6 Step 1:
7 Download the last gl.xml, egl.xml, glx.xml and wgl.xml from
8 http://www.opengl.org/registry/#specfiles into some XML_DIR:
9 wget https://www.khronos.org/registry/OpenGL/xml/gl.xml
10 wget https://www.khronos.org/registry/OpenGL/xml/glx.xml
11 wget https://www.khronos.org/registry/OpenGL/xml/wgl.xml
12 wget https://www.khronos.org/registry/EGL/api/egl.xml
14 Step 2:
15 `py ./GLConsts.py <XML_DIR>`
17 Step 3:
18 Do not add the downloaded XML in the patch
20 Step 4:
21 Enjoy =)
22 """
24 # includes
25 import pathlib
26 import sys
27 import xml.etree.ElementTree
28 from typing import List # mypy!
30 # -
32 (_, XML_DIR_STR) = sys.argv
33 XML_DIR = pathlib.Path(XML_DIR_STR)
35 # -
37 HEADER = b"""
38 /* This Source Code Form is subject to the terms of the Mozilla Public
39 * License, v. 2.0. If a copy of the MPL was not distributed with this
40 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
42 // clang-format off
44 #ifndef GLCONSTS_H_
45 #define GLCONSTS_H_
47 /**
48 * GENERATED FILE, DO NOT MODIFY DIRECTLY.
49 * This is a file generated directly from the official OpenGL registry
50 * xml available http://www.opengl.org/registry/#specfiles.
52 * To generate this file, see tutorial in \'GLConsts.py\'.
54 """[
58 FOOTER = b"""
59 #endif // GLCONSTS_H_
61 // clang-format on
62 """[
66 # -
69 def format_lib_constant(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)
76 if whitespace < 0:
77 whitespace = whitespace % 8
79 return define + " " * whitespace + " " + value
82 class GLConst:
83 def __init__(self, lib, name, value, type):
84 self.lib = lib
85 self.name = name
86 self.value = value
87 self.type = type
90 class GLDatabase:
91 LIBS = ["GL", "EGL", "GLX", "WGL"]
93 def __init__(self):
94 self.consts = {}
95 self.libs = set(GLDatabase.LIBS)
96 self.vendors = set(["EXT", "ATI"])
97 # there is no vendor="EXT" and vendor="ATI" in gl.xml,
98 # so we manualy declare them
100 def load_xml(self, xml_path):
101 tree = xml.etree.ElementTree.parse(xml_path)
102 root = tree.getroot()
104 for enums in root.iter("enums"):
105 vendor = enums.get("vendor")
106 if not vendor:
107 # there some standart enums that do have the vendor attribute,
108 # so we fake them as ARB's enums
109 vendor = "ARB"
111 if vendor not in self.vendors:
112 # we map this new vendor in the vendors set.
113 self.vendors.add(vendor)
115 namespaceType = enums.get("type")
117 for enum in enums:
118 if enum.tag != "enum":
119 # this is not an enum => we skip it
120 continue
122 lib = enum.get("name").split("_")[0]
124 if lib not in self.libs:
125 # unknown library => we skip it
126 continue
128 name = enum.get("name")[len(lib) + 1 :]
129 value = enum.get("value")
130 type = enum.get("type")
132 if not type:
133 # if no type specified, we get the namespace's default type
134 type = namespaceType
136 self.consts[lib + "_" + name] = GLConst(lib, name, value, type)
141 db = GLDatabase()
142 db.load_xml(XML_DIR / "gl.xml")
143 db.load_xml(XML_DIR / "glx.xml")
144 db.load_xml(XML_DIR / "wgl.xml")
145 db.load_xml(XML_DIR / "egl.xml")
149 lines: List[str] = [] # noqa: E999 (bug 1573737)
151 keys = sorted(db.consts.keys())
153 for lib in db.LIBS:
154 lines.append("// " + lib)
156 for k in keys:
157 const = db.consts[k]
159 if const.lib != lib:
160 continue
162 const_str = format_lib_constant(lib, const.name, const.value)
163 lines.append(const_str)
165 lines.append("")
169 b_lines: List[bytes] = [HEADER] + [x.encode() for x in lines] + [FOOTER]
170 b_data: bytes = b"\n".join(b_lines)
172 dest = pathlib.Path("GLConsts.h")
173 dest.write_bytes(b_data)
175 print(f"Wrote {len(b_data)} bytes.") # Some indication that we're successful.