glapi: gl_gentable.py: Replace getopt with argparse
[mesa.git] / src / mapi / glapi / gen / gl_gentable.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
4 # (C) Copyright Apple Inc. 2011
5 # Copyright (C) 2015 Intel Corporation
6 # All Rights Reserved.
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a
9 # copy of this software and associated documentation files (the "Software"),
10 # to deal in the Software without restriction, including without limitation
11 # on the rights to use, copy, modify, merge, publish, distribute, sub
12 # license, and/or sell copies of the Software, and to permit persons to whom
13 # the Software is furnished to do so, subject to the following conditions:
14 #
15 # The above copyright notice and this permission notice (including the next
16 # paragraph) shall be included in all copies or substantial portions of the
17 # Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 # IN THE SOFTWARE.
26 #
27 # Authors:
28 # Jeremy Huddleston <jeremyhu@apple.com>
29 #
30 # Based on code ogiginally by:
31 # Ian Romanick <idr@us.ibm.com>
32
33 import argparse
34
35 import license
36 import gl_XML, glX_XML
37
38 header = """/* GLXEXT is the define used in the xserver when the GLX extension is being
39 * built. Hijack this to determine whether this file is being built for the
40 * server or the client.
41 */
42 #ifdef HAVE_DIX_CONFIG_H
43 #include <dix-config.h>
44 #endif
45
46 #if (defined(GLXEXT) && defined(HAVE_BACKTRACE)) \\
47 || (!defined(GLXEXT) && defined(DEBUG) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__))
48 #define USE_BACKTRACE
49 #endif
50
51 #ifdef USE_BACKTRACE
52 #include <execinfo.h>
53 #endif
54
55 #ifndef _WIN32
56 #include <dlfcn.h>
57 #endif
58 #include <stdlib.h>
59 #include <stdio.h>
60
61 #include "main/glheader.h"
62
63 #include "glapi.h"
64 #include "glapitable.h"
65
66 #ifdef GLXEXT
67 #include "os.h"
68 #endif
69
70 static void
71 __glapi_gentable_NoOp(void) {
72 const char *fstr = "Unknown";
73
74 /* Silence potential GCC warning for some #ifdef paths.
75 */
76 (void) fstr;
77 #if defined(USE_BACKTRACE)
78 #if !defined(GLXEXT)
79 if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
80 #endif
81 {
82 void *frames[2];
83
84 if(backtrace(frames, 2) == 2) {
85 Dl_info info;
86 dladdr(frames[1], &info);
87 if(info.dli_sname)
88 fstr = info.dli_sname;
89 }
90
91 #if !defined(GLXEXT)
92 fprintf(stderr, "Call to unimplemented API: %s\\n", fstr);
93 #endif
94 }
95 #endif
96 #if defined(GLXEXT)
97 LogMessage(X_ERROR, "GLX: Call to unimplemented API: %s\\n", fstr);
98 #endif
99 }
100
101 static void
102 __glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
103 GLuint entries = _glapi_get_dispatch_table_size();
104 void **dispatch = (void **) disp;
105 unsigned i;
106
107 /* ISO C is annoying sometimes */
108 union {_glapi_proc p; void *v;} p;
109 p.p = __glapi_gentable_NoOp;
110
111 for(i=0; i < entries; i++)
112 if(dispatch[i] == NULL)
113 dispatch[i] = p.v;
114 }
115
116 struct _glapi_table *
117 _glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
118 struct _glapi_table *disp = calloc(_glapi_get_dispatch_table_size(), sizeof(_glapi_proc));
119 char symboln[512];
120
121 if(!disp)
122 return NULL;
123
124 if(symbol_prefix == NULL)
125 symbol_prefix = "";
126 """
127
128 footer = """
129 __glapi_gentable_set_remaining_noop(disp);
130
131 return disp;
132 }
133 """
134
135 body_template = """
136 if(!disp->%(name)s) {
137 void ** procp = (void **) &disp->%(name)s;
138 snprintf(symboln, sizeof(symboln), "%%s%(entry_point)s", symbol_prefix);
139 #ifdef _WIN32
140 *procp = GetProcAddress(handle, symboln);
141 #else
142 *procp = dlsym(handle, symboln);
143 #endif
144 }
145 """
146
147 class PrintCode(gl_XML.gl_print_base):
148
149 def __init__(self):
150 gl_XML.gl_print_base.__init__(self)
151
152 self.name = "gl_gentable.py (from Mesa)"
153 self.license = license.bsd_license_template % ( \
154 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
155 (C) Copyright IBM Corporation 2004, 2005
156 (C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
157
158 return
159
160
161 def get_stack_size(self, f):
162 size = 0
163 for p in f.parameterIterator():
164 if p.is_padding:
165 continue
166
167 size += p.get_stack_size()
168
169 return size
170
171
172 def printRealHeader(self):
173 print header
174 return
175
176
177 def printRealFooter(self):
178 print footer
179 return
180
181
182 def printBody(self, api):
183 for f in api.functionIterateByOffset():
184 for entry_point in f.entry_points:
185 vars = { 'entry_point' : entry_point,
186 'name' : f.name }
187
188 print body_template % vars
189 return
190
191
192 def _parser():
193 """Parse arguments and return a namespace object."""
194 parser = argparse.ArgumentParser()
195 parser.add_argument('-f',
196 dest='filename',
197 default='gl_API.xml',
198 help='An XML file description of an API')
199
200 return parser.parse_args()
201
202
203 if __name__ == '__main__':
204 args = _parser()
205
206 printer = PrintCode()
207
208 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
209 printer.Print(api)