1#! /usr/bin/env python
2"""Script to pull out OpenGL core definitions from the linux opengl header"""
3import re
4import get_gl_extensions
5constant = re.compile( '^[#]define\W+(?P<name>[A-Z_0-9]+)\W+(?P<value>0x[a-fA-F0-9]+)$', re.M )
6function = re.compile( '^GLAPI\W+(?P<return>[A-Z_0-9a-z *]+)\W+GLAPIENTRY\W+(?P<name>[a-zA-Z_0-9]+)(?P<signature>[(].*?[)])\W*[;]', re.DOTALL|re.M )
7
8class CoreModule( get_gl_extensions.VersionModule ):
9    _header = None
10    dll = 'None'
11    GLGET_CONSTANT = '_GLGET_CONSTANTS[ %(name)s ] = %(size)s'
12    @property
13    def RAW_MODULE_TEMPLATE( self ):
14        assert not 'glget' in get_gl_extensions.WRAPPER_TEMPLATE
15        return get_gl_extensions.WRAPPER_TEMPLATE + get_gl_extensions.INIT_TEMPLATE
16    def read_header( self ):
17        if not self._header:
18            content = open( '/usr/include/GL/gl.h' ).read()
19            start = content.index( ' * Constants' )
20            stop = content.index( ' * OpenGL 1.2' )
21            self._header = content[start:stop]
22        return self._header
23
24    def findFunctions( self ):
25        declarations = self.read_header()
26        functions = []
27        for match in function.finditer(declarations):
28            match_dict = match.groupdict()
29            f = get_gl_extensions.Function( match_dict['return'], match_dict['name'], " ".join(match_dict['signature'].splitlines()) )
30            if 'ptr' in f.argNames:
31                f.argNames[f.argNames.index('ptr')] = 'pointer'
32            functions.append( f )
33        self.functions = functions
34
35def core():
36    # avoid import loops...
37    get_gl_extensions.WRAPPER_TEMPLATE = get_gl_extensions.WRAPPER_TEMPLATE.replace(
38        'from OpenGL.GL import glget', '_GLGET_CONSTANTS = {}',
39    )
40    assert not 'glget' in get_gl_extensions.WRAPPER_TEMPLATE
41    class FakeHeader( object ):
42        includeOverviews = False
43        registry = {}
44        glGetSizes = {}
45        def loadGLGetSizes( self ):
46            """Load manually-generated table of glGet* sizes"""
47            table = self.glGetSizes
48            try:
49                lines = [
50                    line.split('\t')
51                    for line in open( 'glgetsizes.csv' ).read().splitlines()
52                ]
53            except IOError, err:
54                pass
55            else:
56                for line in lines:
57                    if line and line[0]:
58                        table[line[0].strip('"')] = [
59                            v for v in [
60                                v.strip('"') for v in line[1:]
61                            ]
62                            if v
63                        ]
64    f = FakeHeader()
65    f.loadGLGetSizes()
66    c = CoreModule( 'GL_VERSION_GL_1_1', [], f )
67    c.segments = [c.read_header()]
68    c.process()
69
70if __name__ == "__main__":
71    core()
72