tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GLConsts.py (4215B)


      1 #!/usr/bin/env python3
      2 
      3 """
      4 This script will regenerate and update GLConsts.h.
      5 
      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
     13 
     14 Step 2:
     15  `py ./GLConsts.py <XML_DIR>`
     16 
     17 Step 3:
     18  Do not add the downloaded XML in the patch
     19 
     20 Step 4:
     21  Enjoy =)
     22 """
     23 
     24 # includes
     25 import pathlib
     26 import sys
     27 import xml.etree.ElementTree
     28 
     29 # -
     30 
     31 (_, XML_DIR_STR) = sys.argv
     32 XML_DIR = pathlib.Path(XML_DIR_STR)
     33 
     34 # -
     35 
     36 HEADER = b"""
     37 /* This Source Code Form is subject to the terms of the Mozilla Public
     38 * License, v. 2.0. If a copy of the MPL was not distributed with this
     39 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     40 
     41 // clang-format off
     42 
     43 #ifndef GLCONSTS_H_
     44 #define GLCONSTS_H_
     45 
     46 /**
     47 * GENERATED FILE, DO NOT MODIFY DIRECTLY.
     48 * This is a file generated directly from the official OpenGL registry
     49 * xml available http://www.opengl.org/registry/#specfiles.
     50 *
     51 * To generate this file, see tutorial in \'GLConsts.py\'.
     52 */
     53 """[1:]
     54 
     55 FOOTER = b"""
     56 #endif // GLCONSTS_H_
     57 
     58 // clang-format on
     59 """[1:]
     60 
     61 # -
     62 
     63 
     64 def format_lib_constant(lib, name, value):
     65    # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
     66    # name is the name of the const (example: MAX_TEXTURE_SIZE)
     67    # value is the value of the const (example: 0xABCD)
     68 
     69    define = "#define LOCAL_" + lib + "_" + name
     70    whitespace = 60 - len(define)
     71    if whitespace < 0:
     72        whitespace = whitespace % 8
     73 
     74    return define + " " * whitespace + " " + value
     75 
     76 
     77 class GLConst:
     78    def __init__(self, lib, name, value, type):
     79        self.lib = lib
     80        self.name = name
     81        self.value = value
     82        self.type = type
     83 
     84 
     85 class GLDatabase:
     86    LIBS = ["GL", "EGL", "GLX", "WGL"]
     87 
     88    def __init__(self):
     89        self.consts = {}
     90        self.libs = set(GLDatabase.LIBS)
     91        self.vendors = set(["EXT", "ATI"])
     92        # there is no vendor="EXT" and vendor="ATI" in gl.xml,
     93        # so we manualy declare them
     94 
     95    def load_xml(self, xml_path):
     96        tree = xml.etree.ElementTree.parse(xml_path)
     97        root = tree.getroot()
     98 
     99        for enums in root.iter("enums"):
    100            vendor = enums.get("vendor")
    101            if not vendor:
    102                # there some standart enums that do have the vendor attribute,
    103                # so we fake them as ARB's enums
    104                vendor = "ARB"
    105 
    106            if vendor not in self.vendors:
    107                # we map this new vendor in the vendors set.
    108                self.vendors.add(vendor)
    109 
    110            namespaceType = enums.get("type")
    111 
    112            for enum in enums:
    113                if enum.tag != "enum":
    114                    # this is not an enum => we skip it
    115                    continue
    116 
    117                lib = enum.get("name").split("_")[0]
    118 
    119                if lib not in self.libs:
    120                    # unknown library => we skip it
    121                    continue
    122 
    123                name = enum.get("name")[len(lib) + 1 :]
    124                value = enum.get("value")
    125                type = enum.get("type")
    126 
    127                if not type:
    128                    # if no type specified, we get the namespace's default type
    129                    type = namespaceType
    130 
    131                self.consts[lib + "_" + name] = GLConst(lib, name, value, type)
    132 
    133 
    134 # -
    135 
    136 db = GLDatabase()
    137 db.load_xml(XML_DIR / "gl.xml")
    138 db.load_xml(XML_DIR / "glx.xml")
    139 db.load_xml(XML_DIR / "wgl.xml")
    140 db.load_xml(XML_DIR / "egl.xml")
    141 
    142 # -
    143 
    144 lines: list[str] = []
    145 
    146 keys = sorted(db.consts.keys())
    147 
    148 for lib in db.LIBS:
    149    lines.append("// " + lib)
    150 
    151    for k in keys:
    152        const = db.consts[k]
    153 
    154        if const.lib != lib:
    155            continue
    156 
    157        const_str = format_lib_constant(lib, const.name, const.value)
    158        lines.append(const_str)
    159 
    160    lines.append("")
    161 
    162 # -
    163 
    164 b_lines: list[bytes] = [HEADER] + [x.encode() for x in lines] + [FOOTER]
    165 b_data: bytes = b"\n".join(b_lines)
    166 
    167 dest = pathlib.Path("GLConsts.h")
    168 dest.write_bytes(b_data)
    169 
    170 print(f"Wrote {len(b_data)} bytes.")  # Some indication that we're successful.