gfx/gl/GLParseRegistryXML.py

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rwxr-xr-x

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 #!/usr/bin/env python
     2 # coding=utf8
     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 ################################################################################
    24 # includes
    25 import os
    26 import sys
    27 import xml.etree.ElementTree
    30 ################################################################################
    31 # export management
    33 class GLConstHeader:
    34     def __init__(self, f):
    35         self.f = f
    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')
    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         ])
    67     def formatLibBegin(self, lib):
    68         # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
    69         self.write('// ' + lib)
    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)
    77         define = '#define LOCAL_' + lib + '_' + name
    78         whitespace = 60 - len(define)
    80         if whitespace < 0:
    81             whitespace = whitespace % 8
    83         self.write(define + ' ' * whitespace + ' ' + value)
    86     def formatLibEnd(self, lib):
    87         # lib would be 'GL', 'EGL', 'GLX' or 'WGL'
    88         self.write(2)
    91     def formatFileEnd(self):
    92         self.write([
    93                     '',
    94                     '#endif // GLCONSTS_H_'
    95                    ])
    98 ################################################################################
    99 # underground code
   101 def getScriptDir():
   102     return os.path.dirname(__file__) + '/'
   105 def getXMLDir():
   106     if len(sys.argv) == 1:
   107         return './'
   109     dirPath = sys.argv[1]
   110     if dirPath[-1] != '/':
   111         dirPath += '/'
   113     return dirPath
   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
   124 class GLDatabase:
   126     LIBS = ['GL', 'EGL', 'GLX', 'WGL']
   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
   136     def loadXML(self, path):
   137         xmlPath = getXMLDir() + path
   139         if not os.path.isfile(xmlPath):
   140             print 'missing file "' + xmlPath + '"'
   141             return False
   143         tree = xml.etree.ElementTree.parse(xmlPath)
   144         root = tree.getroot()
   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'
   153             if vendor not in self.vendors:
   154                 # we map this new vendor in the vendors set.
   155                 self.vendors.add(vendor)
   157             namespaceType = enums.get('type')
   159             for enum in enums:
   160                 if enum.tag != 'enum':
   161                     # this is not an enum => we skip it
   162                     continue
   164                 lib = enum.get('name').split('_')[0]
   166                 if lib not in self.libs:
   167                     # unknown library => we skip it
   168                     continue
   170                 name = enum.get('name')[len(lib) + 1:]
   171                 value = enum.get('value')
   172                 type = enum.get('type')
   174                 if not type:
   175                     # if no type specified, we get the namespace's default type
   176                     type = namespaceType
   178                 self.consts[lib + '_' + name] = GLConst(lib, name, value, type)
   180         return True
   183     def exportConsts(self, path):
   184         with open(getScriptDir() + path,'w') as f:
   186             headerFile = GLConstHeader(f)
   187             headerFile.formatFileBegin()
   189             constNames = self.consts.keys()
   190             constNames.sort()
   192             for lib in GLDatabase.LIBS:
   193                 headerFile.formatLibBegin(lib)
   195                 for constName in constNames:
   196                     const = self.consts[constName]
   198                     if const.lib != lib:
   199                         continue
   201                     headerFile.formatLibConstant(lib, const.name, const.value)
   203                 headerFile.formatLibEnd(lib)
   205             headerFile.formatFileEnd()
   208 glDatabase = GLDatabase()
   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')
   215 if success:
   216     glDatabase.exportConsts('GLConsts.h')

mercurial