Add a glFunctionIterator class to iterate over the functions stored in a
[mesa.git] / src / mesa / glapi / gl_procs.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 from xml.sax import saxutils
29 from xml.sax import make_parser
30 from xml.sax.handler import feature_namespaces
31
32 import license
33 import gl_XML
34 import sys, getopt
35
36 class PrintGlProcs(gl_XML.FilterGLAPISpecBase):
37 name = "gl_procs.py (from Mesa)"
38
39 def __init__(self, long_strings):
40 self.long_strings = long_strings
41 gl_XML.FilterGLAPISpecBase.__init__(self)
42 self.license = license.bsd_license_template % ( \
43 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
44 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
45
46
47 def printRealHeader(self):
48 print '/* This file is only included by glapi.c and is used for'
49 print ' * the GetProcAddress() function'
50 print ' */'
51 print ''
52 print 'typedef struct {'
53 print ' GLint Name_offset;'
54 print '#ifdef NEED_FUNCTION_POINTER'
55 print ' _glapi_proc Address;'
56 print '#endif'
57 print ' GLuint Offset;'
58 print '} glprocs_table_t;'
59 print ''
60 print '#ifdef NEED_FUNCTION_POINTER'
61 print '# define NAME_FUNC_OFFSET(n,f,o) { n , (_glapi_proc) f , o }'
62 print '#else'
63 print '# define NAME_FUNC_OFFSET(n,f,o) { n , o }'
64 print '#endif'
65 print ''
66 return
67
68 def printRealFooter(self):
69 print ''
70 print '#undef NAME_FUNC_OFFSET'
71 return
72
73 def printFunctionString(self, f):
74 if self.long_strings:
75 print ' "gl%s\\0"' % (f.name)
76 else:
77 print " 'g','l',",
78 for c in f.name:
79 print "'%s'," % (c),
80
81 print "'\\0',"
82
83 def printFunctionOffset(self, f, offset_of_name):
84 print ' NAME_FUNC_OFFSET( % 5u, gl%s, _gloffset_%s ),' % (offset_of_name, f.name, f.real_name)
85
86
87 def printFunctions(self):
88 print ''
89 if self.long_strings:
90 print 'static const char gl_string_table[] ='
91 else:
92 print 'static const char gl_string_table[] = {'
93
94 for f in self.functionIterator():
95 self.printFunctionString(f)
96
97 if self.long_strings:
98 print ' ;'
99 else:
100 print '};'
101
102 print ''
103 print 'static const glprocs_table_t static_functions[] = {'
104
105 base_offset = 0
106
107 for f in self.functionIterator():
108 self.printFunctionOffset(f, base_offset)
109
110 # The length of the function's name, plus 2 for "gl",
111 # plus 1 for the NUL.
112
113 base_offset += len(f.name) + 3
114
115 print ' NAME_FUNC_OFFSET( -1, NULL, 0 )'
116 print '};'
117 return
118
119
120 def show_usage():
121 print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0]
122 print "mode can be one of:"
123 print " long - Create code for compilers that can handle very "
124 print " long string constants. (default)"
125 print " short - Create code for compilers that can only handle "
126 print " ANSI C89 string constants."
127 sys.exit(1)
128
129 if __name__ == '__main__':
130 file_name = "gl_API.xml"
131
132 try:
133 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
134 except Exception,e:
135 show_usage()
136
137 long_string = 1
138 for (arg,val) in args:
139 if arg == "-f":
140 file_name = val
141 elif arg == "-m":
142 if val == "short":
143 long_string = 0
144 elif val == "long":
145 long_string = 1
146 else:
147 show_usage()
148
149 dh = PrintGlProcs( long_string )
150
151 parser = make_parser()
152 parser.setFeature(feature_namespaces, 0)
153 parser.setContentHandler(dh)
154
155 f = open(file_name)
156
157 dh.printHeader()
158 parser.parse(f)
159 dh.printFooter()