ecab65d3f0c077915638eeeb17c127bd35d28a0d
[mesa.git] / src / mesa / glapi / glX_proto_size.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
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 gl_XML
33 import glX_XML
34 import license
35 import sys, getopt, copy
36
37
38 class SizeStubFunctionIterator:
39 """Iterate over functions that need "size" information.
40
41 Iterate over the functions that have variable sized data. First the
42 "set"-type functions are iterated followed by the "get"-type
43 functions.
44 """
45
46 def __init__(self, context):
47 self.data = []
48 self.index = 0
49
50 set_functions = []
51 get_functions = []
52 extra_data = []
53
54 for f in gl_XML.glFunctionIterator(context):
55 if f.fn_offset < 0: break
56
57 if context.glx_enum_functions.has_key(f.name):
58 ef = context.glx_enum_functions[f.name]
59 if ef.is_set():
60 set_functions.append(f)
61 else:
62 get_functions.append(f)
63
64
65 if (context.which_functions & PrintGlxSizeStubs_c.do_set) != 0:
66 self.data += set_functions
67 elif context.get_alias_set:
68 extra_data = set_functions
69
70 if (context.which_functions & PrintGlxSizeStubs_c.do_get) != 0:
71 self.data += get_functions
72
73
74 for f in extra_data + self.data:
75 sig = context.glx_enum_functions[f.name].signature()
76
77 if not context.glx_enum_sigs.has_key(sig):
78 context.glx_enum_sigs[sig] = f.name;
79
80 return
81
82
83 def __iter__(self):
84 return self
85
86
87 def next(self):
88 if self.index == len(self.data):
89 raise StopIteration
90
91 f = self.data[ self.index ]
92 self.index += 1
93
94 return f
95
96
97 class PrintGlxSizeStubs_common(glX_XML.GlxProto):
98 do_get = (1 << 0)
99 do_set = (1 << 1)
100 do_get_alias_set = (1 << 2)
101
102 def __init__(self, which_functions):
103 glX_XML.GlxProto.__init__(self)
104 self.license = license.bsd_license_template % ( "(C) Copyright IBM Corporation 2004", "IBM")
105 self.aliases = []
106 self.glx_enum_sigs = {}
107 self.name = "glX_proto_size.py (from Mesa)"
108 self.which_functions = which_functions
109
110 if (((which_functions & PrintGlxSizeStubs_common.do_set) != 0) and ((which_functions & PrintGlxSizeStubs_common.do_get) != 0)) or ((which_functions & PrintGlxSizeStubs_common.do_get_alias_set) != 0):
111 self.get_alias_set = 1
112 else:
113 self.get_alias_set = 0
114
115
116 def functionIterator(self):
117 return SizeStubFunctionIterator(self)
118
119
120 class PrintGlxSizeStubs_c(PrintGlxSizeStubs_common):
121 def printRealHeader(self):
122 print ''
123 print '#include <GL/gl.h>'
124 print '#include "indirect_size.h"'
125
126 print ''
127 glX_XML.printHaveAlias()
128 print ''
129 glX_XML.printPure()
130 print ''
131 glX_XML.printFastcall()
132 print ''
133 glX_XML.printVisibility( "INTERNAL", "internal" )
134 print ''
135 print ''
136 print '#ifdef HAVE_ALIAS'
137 print '# define ALIAS2(from,to) \\'
138 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
139 print ' __attribute__ ((alias( # to )));'
140 print '# define ALIAS(from,to) ALIAS2( from, __gl ## to ## _size )'
141 print '#else'
142 print '# define ALIAS(from,to) \\'
143 print ' INTERNAL PURE FASTCALL GLint __gl ## from ## _size( GLenum e ) \\'
144 print ' { return __gl ## to ## _size( e ); }'
145 print '#endif'
146 print ''
147 print ''
148
149
150 def printRealFooter(self):
151 for a in self.aliases:
152 print a
153
154
155 def printFunction(self, f):
156 ef = self.glx_enum_functions[f.name]
157 n = self.glx_enum_sigs[ ef.signature() ];
158
159 if n != f.name:
160 a = 'ALIAS( %s, %s )' % (f.name, n)
161 self.aliases.append(a)
162 else:
163 ef.Print( f.name )
164
165
166
167 class PrintGlxSizeStubs_h(PrintGlxSizeStubs_common):
168 def printRealHeader(self):
169 print """
170 /**
171 * \\file
172 * Prototypes for functions used to determine the number of data elements in
173 * various GLX protocol messages.
174 *
175 * \\author Ian Romanick <idr@us.ibm.com>
176 */
177
178 #if !defined( _GLXSIZE_H_ )
179 # define _GLXSIZE_H_
180
181 """
182 glX_XML.printPure();
183 print ''
184 glX_XML.printFastcall();
185 print ''
186 glX_XML.printVisibility( "INTERNAL", "internal" );
187 print ''
188
189 def printRealFooter(self):
190 print ''
191 print "# undef INTERNAL"
192 print "# undef PURE"
193 print "# undef FASTCALL"
194 print "#endif /* !defined( _GLXSIZE_H_ ) */"
195
196
197 def printFunction(self, f):
198 ef = self.glx_enum_functions[f.name]
199 print 'extern INTERNAL PURE FASTCALL GLint __gl%s_size(GLenum);' % (f.name)
200
201
202 def show_usage():
203 print "Usage: %s [-f input_file_name] -m output_mode [--only-get | --only-set] [--get-alias-set]" % sys.argv[0]
204 print " -m output_mode Output mode can be one of 'size_c' or 'size_h'."
205 print " --only-get Only emit 'get'-type functions."
206 print " --only-set Only emit 'set'-type functions."
207 print " --get-alias-set When only 'get'-type functions are emitted, allow them"
208 print " to be aliases to 'set'-type funcitons."
209 print ""
210 print "By default, both 'get' and 'set'-type functions are emitted."
211 sys.exit(1)
212
213
214 if __name__ == '__main__':
215 file_name = "gl_API.xml"
216
217 try:
218 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:", ["only-get", "only-set", "get-alias-set"])
219 except Exception,e:
220 show_usage()
221
222 mode = None
223 which_functions = PrintGlxSizeStubs_common.do_get | PrintGlxSizeStubs_common.do_set
224
225 for (arg,val) in args:
226 if arg == "-f":
227 file_name = val
228 elif arg == "-m":
229 mode = val
230 elif arg == "--only-get":
231 which_functions = PrintGlxSizeStubs_common.do_get
232 elif arg == "--only-set":
233 which_functions = PrintGlxSizeStubs_common.do_set
234 elif arg == "--get-alias-set":
235 which_functions |= PrintGlxSizeStubs_common.do_get_alias_set
236
237 if mode == "size_c":
238 dh = PrintGlxSizeStubs_c( which_functions )
239 elif mode == "size_h":
240 dh = PrintGlxSizeStubs_h( which_functions )
241 else:
242 show_usage()
243
244 parser = make_parser()
245 parser.setFeature(feature_namespaces, 0)
246 parser.setContentHandler(dh)
247
248 f = open(file_name)
249
250 dh.printHeader()
251 parser.parse(f)
252 dh.printFooter()