mapi/gen: remove shebang from python scripts
[mesa.git] / src / mapi / glapi / gen / gl_gentable.py
1
2 # (C) Copyright IBM Corporation 2004, 2005
3 # (C) Copyright Apple Inc. 2011
4 # Copyright (C) 2015 Intel Corporation
5 # All Rights Reserved.
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 # Jeremy Huddleston <jeremyhu@apple.com>
28 #
29 # Based on code ogiginally by:
30 # Ian Romanick <idr@us.ibm.com>
31
32 import argparse
33
34 import license
35 import gl_XML, glX_XML
36
37 header = """/* GLXEXT is the define used in the xserver when the GLX extension is being
38 * built. Hijack this to determine whether this file is being built for the
39 * server or the client.
40 */
41 #ifdef HAVE_DIX_CONFIG_H
42 #include <dix-config.h>
43 #endif
44
45 #if (defined(GLXEXT) && defined(HAVE_BACKTRACE)) \\
46 || (!defined(GLXEXT) && defined(DEBUG) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__))
47 #define USE_BACKTRACE
48 #endif
49
50 #ifdef USE_BACKTRACE
51 #include <execinfo.h>
52 #endif
53
54 #ifndef _WIN32
55 #include <dlfcn.h>
56 #endif
57 #include <stdlib.h>
58 #include <stdio.h>
59
60 #include "main/glheader.h"
61
62 #include "glapi.h"
63 #include "glapitable.h"
64
65 #ifdef GLXEXT
66 #include "os.h"
67 #endif
68
69 static void
70 __glapi_gentable_NoOp(void) {
71 const char *fstr = "Unknown";
72
73 /* Silence potential GCC warning for some #ifdef paths.
74 */
75 (void) fstr;
76 #if defined(USE_BACKTRACE)
77 #if !defined(GLXEXT)
78 if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
79 #endif
80 {
81 void *frames[2];
82
83 if(backtrace(frames, 2) == 2) {
84 Dl_info info;
85 dladdr(frames[1], &info);
86 if(info.dli_sname)
87 fstr = info.dli_sname;
88 }
89
90 #if !defined(GLXEXT)
91 fprintf(stderr, "Call to unimplemented API: %s\\n", fstr);
92 #endif
93 }
94 #endif
95 #if defined(GLXEXT)
96 LogMessage(X_ERROR, "GLX: Call to unimplemented API: %s\\n", fstr);
97 #endif
98 }
99
100 static void
101 __glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
102 GLuint entries = _glapi_get_dispatch_table_size();
103 void **dispatch = (void **) disp;
104 unsigned i;
105
106 /* ISO C is annoying sometimes */
107 union {_glapi_proc p; void *v;} p;
108 p.p = __glapi_gentable_NoOp;
109
110 for(i=0; i < entries; i++)
111 if(dispatch[i] == NULL)
112 dispatch[i] = p.v;
113 }
114
115 """
116
117 footer = """
118 struct _glapi_table *
119 _glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
120 struct _glapi_table *disp = calloc(_glapi_get_dispatch_table_size(), sizeof(_glapi_proc));
121 char symboln[512];
122
123 if(!disp)
124 return NULL;
125
126 if(symbol_prefix == NULL)
127 symbol_prefix = "";
128
129 /* Note: This code relies on _glapi_table_func_names being sorted by the
130 * entry point index of each function.
131 */
132 for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) {
133 const char *name = _glapi_table_func_names[func_index];
134 void ** procp = &((void **)disp)[func_index];
135
136 snprintf(symboln, sizeof(symboln), \"%s%s\", symbol_prefix, name);
137 #ifdef _WIN32
138 *procp = GetProcAddress(handle, symboln);
139 #else
140 *procp = dlsym(handle, symboln);
141 #endif
142 }
143 __glapi_gentable_set_remaining_noop(disp);
144
145 return disp;
146 }
147 """
148
149
150 class PrintCode(gl_XML.gl_print_base):
151
152 def __init__(self):
153 gl_XML.gl_print_base.__init__(self)
154
155 self.name = "gl_gentable.py (from Mesa)"
156 self.license = license.bsd_license_template % ( \
157 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
158 (C) Copyright IBM Corporation 2004, 2005
159 (C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
160
161 return
162
163
164 def get_stack_size(self, f):
165 size = 0
166 for p in f.parameterIterator():
167 if p.is_padding:
168 continue
169
170 size += p.get_stack_size()
171
172 return size
173
174
175 def printRealHeader(self):
176 print header
177 return
178
179
180 def printRealFooter(self):
181 print footer
182 return
183
184
185 def printBody(self, api):
186
187 # Determine how many functions have a defined offset.
188 func_count = 0
189 for f in api.functions_by_name.itervalues():
190 if f.offset != -1:
191 func_count += 1
192
193 # Build the mapping from offset to function name.
194 funcnames = [None] * func_count
195 for f in api.functions_by_name.itervalues():
196 if f.offset != -1:
197 if not (funcnames[f.offset] is None):
198 raise Exception("Function table has more than one function with same offset (offset %d, func %s)" % (f.offset, f.name))
199 funcnames[f.offset] = f.name
200
201 # Check that the table has no gaps. We expect a function at every offset,
202 # and the code which generates the table relies on this.
203 for i in xrange(0, func_count):
204 if funcnames[i] is None:
205 raise Exception("Function table has no function at offset %d" % (i))
206
207 print "#define GLAPI_TABLE_COUNT %d" % func_count
208 print "static const char * const _glapi_table_func_names[GLAPI_TABLE_COUNT] = {"
209 for i in xrange(0, func_count):
210 print " /* %5d */ \"%s\"," % (i, funcnames[i])
211 print "};"
212
213 return
214
215
216 def _parser():
217 """Parse arguments and return a namespace object."""
218 parser = argparse.ArgumentParser()
219 parser.add_argument('-f',
220 dest='filename',
221 default='gl_API.xml',
222 help='An XML file description of an API')
223
224 return parser.parse_args()
225
226
227 def main():
228 """Main function."""
229 args = _parser()
230
231 printer = PrintCode()
232
233 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
234 printer.Print(api)
235
236
237 if __name__ == '__main__':
238 main()