Bug 1728955: part 6) Log result of Windows' `OleSetClipboardResult`. r=masayuki
[gecko.git] / gfx / gl / GLConsts.py
blob274558012bf1eefca158916f56cbfac7697c7955
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 from typing import List # mypy!
27 import pathlib
28 import sys
29 import xml.etree.ElementTree
31 # -
33 (_, XML_DIR_STR) = sys.argv
34 XML_DIR = pathlib.Path(XML_DIR_STR)
36 # -
38 HEADER = b"""
39 /* This Source Code Form is subject to the terms of the Mozilla Public
40 * License, v. 2.0. If a copy of the MPL was not distributed with this
41 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
43 // clang-format off
45 #ifndef GLCONSTS_H_
46 #define GLCONSTS_H_
48 /**
49 * GENERATED FILE, DO NOT MODIFY DIRECTLY.
50 * This is a file generated directly from the official OpenGL registry
51 * xml available http://www.opengl.org/registry/#specfiles.
53 * To generate this file, see tutorial in \'GLConsts.py\'.
55 """[
59 FOOTER = b"""
60 #endif // GLCONSTS_H_
62 // clang-format on
63 """[
67 # -
70 def format_lib_constant(lib, name, value):
71 # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
72 # name is the name of the const (example: MAX_TEXTURE_SIZE)
73 # value is the value of the const (example: 0xABCD)
75 define = "#define LOCAL_" + lib + "_" + name
76 whitespace = 60 - len(define)
77 if whitespace < 0:
78 whitespace = whitespace % 8
80 return define + " " * whitespace + " " + value
83 class GLConst:
84 def __init__(self, lib, name, value, type):
85 self.lib = lib
86 self.name = name
87 self.value = value
88 self.type = type
91 class GLDatabase:
92 LIBS = ["GL", "EGL", "GLX", "WGL"]
94 def __init__(self):
95 self.consts = {}
96 self.libs = set(GLDatabase.LIBS)
97 self.vendors = set(["EXT", "ATI"])
98 # there is no vendor="EXT" and vendor="ATI" in gl.xml,
99 # so we manualy declare them
101 def load_xml(self, xml_path):
102 tree = xml.etree.ElementTree.parse(xml_path)
103 root = tree.getroot()
105 for enums in root.iter("enums"):
106 vendor = enums.get("vendor")
107 if not vendor:
108 # there some standart enums that do have the vendor attribute,
109 # so we fake them as ARB's enums
110 vendor = "ARB"
112 if vendor not in self.vendors:
113 # we map this new vendor in the vendors set.
114 self.vendors.add(vendor)
116 namespaceType = enums.get("type")
118 for enum in enums:
119 if enum.tag != "enum":
120 # this is not an enum => we skip it
121 continue
123 lib = enum.get("name").split("_")[0]
125 if lib not in self.libs:
126 # unknown library => we skip it
127 continue
129 name = enum.get("name")[len(lib) + 1 :]
130 value = enum.get("value")
131 type = enum.get("type")
133 if not type:
134 # if no type specified, we get the namespace's default type
135 type = namespaceType
137 self.consts[lib + "_" + name] = GLConst(lib, name, value, type)
142 db = GLDatabase()
143 db.load_xml(XML_DIR / "gl.xml")
144 db.load_xml(XML_DIR / "glx.xml")
145 db.load_xml(XML_DIR / "wgl.xml")
146 db.load_xml(XML_DIR / "egl.xml")
150 lines: List[str] = [] # noqa: E999 (bug 1573737)
152 keys = sorted(db.consts.keys())
154 for lib in db.LIBS:
155 lines.append("// " + lib)
157 for k in keys:
158 const = db.consts[k]
160 if const.lib != lib:
161 continue
163 const_str = format_lib_constant(lib, const.name, const.value)
164 lines.append(const_str)
166 lines.append("")
170 b_lines: List[bytes] = [HEADER] + [x.encode() for x in lines] + [FOOTER]
171 b_data: bytes = b"\n".join(b_lines)
173 dest = pathlib.Path("GLConsts.h")
174 dest.write_bytes(b_data)
176 print(f"Wrote {len(b_data)} bytes.") # Some indication that we're successful.