glapi: Avoid argparse type argument for API XML input files.
[mesa.git] / src / mapi / glapi / gen / gl_table.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
4 # All Rights Reserved.
5 # Copyright (c) 2014 Intel Corporation
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # Authors:
27 # Ian Romanick <idr@us.ibm.com>
28
29 import argparse
30
31 import gl_XML
32 import license
33
34
35 class PrintGlTable(gl_XML.gl_print_base):
36 def __init__(self, es=False):
37 gl_XML.gl_print_base.__init__(self)
38
39 self.es = es
40 self.header_tag = '_GLAPI_TABLE_H_'
41 self.name = "gl_table.py (from Mesa)"
42 self.license = license.bsd_license_template % ( \
43 """Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
44 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
45 self.ifdef_emitted = False
46 return
47
48 def printBody(self, api):
49 for f in api.functionIterateByOffset():
50 if not f.is_abi() and not self.ifdef_emitted:
51 print '#if !defined HAVE_SHARED_GLAPI'
52 self.ifdef_emitted = True
53 arg_string = f.get_parameter_string()
54 print ' %s (GLAPIENTRYP %s)(%s); /* %d */' % (
55 f.return_type, f.name, arg_string, f.offset)
56
57 print '#endif /* !defined HAVE_SHARED_GLAPI */'
58
59 def printRealHeader(self):
60 print '#ifndef GLAPIENTRYP'
61 print '# ifndef GLAPIENTRY'
62 print '# define GLAPIENTRY'
63 print '# endif'
64 print ''
65 print '# define GLAPIENTRYP GLAPIENTRY *'
66 print '#endif'
67 print ''
68 print ''
69 print 'struct _glapi_table'
70 print '{'
71 return
72
73 def printRealFooter(self):
74 print '};'
75 return
76
77
78 class PrintRemapTable(gl_XML.gl_print_base):
79 def __init__(self, es=False):
80 gl_XML.gl_print_base.__init__(self)
81
82 self.es = es
83 self.header_tag = '_DISPATCH_H_'
84 self.name = "gl_table.py (from Mesa)"
85 self.license = license.bsd_license_template % (
86 "(C) Copyright IBM Corporation 2005", "IBM")
87 return
88
89
90 def printRealHeader(self):
91 print """
92 /**
93 * \\file main/dispatch.h
94 * Macros for handling GL dispatch tables.
95 *
96 * For each known GL function, there are 3 macros in this file. The first
97 * macro is named CALL_FuncName and is used to call that GL function using
98 * the specified dispatch table. The other 2 macros, called GET_FuncName
99 * can SET_FuncName, are used to get and set the dispatch pointer for the
100 * named function in the specified dispatch table.
101 */
102 """
103 return
104
105
106 def printBody(self, api):
107 print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
108 print ' (*(cast (GET_by_offset(disp, offset)))) parameters'
109 print '#define GET_by_offset(disp, offset) \\'
110 print ' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
111 print '#define SET_by_offset(disp, offset, fn) \\'
112 print ' do { \\'
113 print ' if ( (offset) < 0 ) { \\'
114 print ' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
115 print ' /* __func__, __LINE__, disp, offset, # fn); */ \\'
116 print ' /* abort(); */ \\'
117 print ' } \\'
118 print ' else { \\'
119 print ' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
120 print ' } \\'
121 print ' } while(0)'
122 print ''
123
124 functions = []
125 abi_functions = []
126 alias_functions = []
127 count = 0
128 for f in api.functionIterateByOffset():
129 if not f.is_abi():
130 functions.append([f, count])
131 count += 1
132 else:
133 abi_functions.append([f, -1])
134
135 if self.es:
136 # remember functions with aliases
137 if len(f.entry_points) > 1:
138 alias_functions.append(f)
139
140 print '/* total number of offsets below */'
141 print '#define _gloffset_COUNT %d' % (len(abi_functions + functions))
142 print ''
143
144 for f, index in abi_functions:
145 print '#define _gloffset_%s %d' % (f.name, f.offset)
146
147 if self.es:
148 remap_table = "esLocalRemapTable"
149
150 print '#define %s_size %u' % (remap_table, count)
151 print 'static int %s[ %s_size ];' % (remap_table, remap_table)
152 print ''
153 else:
154 remap_table = "driDispatchRemapTable"
155
156 print '#define %s_size %u' % (remap_table, count)
157 print 'extern int %s[ %s_size ];' % (remap_table, remap_table)
158 print ''
159
160 for f, index in functions:
161 print '#define %s_remap_index %u' % (f.name, index)
162
163 print ''
164
165 for f, index in functions:
166 print '#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name)
167
168 print ''
169
170 for f, index in abi_functions + functions:
171 arg_string = gl_XML.create_parameter_string(f.parameters, 0)
172
173 print 'typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string)
174 print '#define CALL_%s(disp, parameters) \\' % (f.name)
175 print ' (* GET_%s(disp)) parameters' % (f.name)
176 print 'static inline _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name)
177 print ' return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name)
178 print '}'
179 print
180 print 'static inline void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string)
181 print ' SET_by_offset(disp, _gloffset_%s, fn);' % (f.name)
182 print '}'
183 print
184
185 if alias_functions:
186 print ''
187 print '/* define aliases for compatibility */'
188 for f in alias_functions:
189 for name in f.entry_points:
190 if name != f.name:
191 print '#define CALL_%s(disp, parameters) CALL_%s(disp, parameters)' % (name, f.name)
192 print '#define GET_%s(disp) GET_%s(disp)' % (name, f.name)
193 print '#define SET_%s(disp, fn) SET_%s(disp, fn)' % (name, f.name)
194 print ''
195
196 for f in alias_functions:
197 for name in f.entry_points:
198 if name != f.name:
199 print '#define %s_remap_index %s_remap_index' % (name, f.name)
200 print ''
201
202 return
203
204
205 def _parser():
206 """Parse arguments and return a namespace."""
207 parser = argparse.ArgumentParser()
208 parser.add_argument('-f', '--filename',
209 default='gl_API.xml',
210 metavar="input_file_name",
211 dest='file_name',
212 help="Path to an XML description of OpenGL API.")
213 parser.add_argument('-m', '--mode',
214 choices=['table', 'remap_table'],
215 default='table',
216 metavar="mode",
217 help="Generate either a table or a remap_table")
218 parser.add_argument('-c', '--es-version',
219 choices=[None, 'es1', 'es2'],
220 default=None,
221 metavar="ver",
222 dest='es',
223 help="filter functions for es")
224 return parser.parse_args()
225
226
227 def main():
228 """Main function."""
229 args = _parser()
230
231 api = gl_XML.parse_GL_API(args.file_name)
232
233 if args.mode == "table":
234 printer = PrintGlTable(args.es)
235 elif args.mode == "remap_table":
236 printer = PrintRemapTable(args.es)
237
238 if args.es is not None:
239 api.filter_functions_by_api(args.es)
240
241 printer.Print(api)
242
243
244 if __name__ == '__main__':
245 main()