glapi: Use GetProcAddress instead of dlsym on Windows.
[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 # 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 license
33 import gl_XML, glX_XML
34 import sys, getopt
35
36 header = """/* GLXEXT is the define used in the xserver when the GLX extension is being
37 * built. Hijack this to determine whether this file is being built for the
38 * server or the client.
39 */
40 #ifdef HAVE_DIX_CONFIG_H
41 #include <dix-config.h>
42 #endif
43
44 #if (defined(GLXEXT) && defined(HAVE_BACKTRACE)) \\
45 || (!defined(GLXEXT) && defined(DEBUG) && !defined(_WIN32_WCE) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__OpenBSD__) && !defined(__NetBSD__))
46 #define USE_BACKTRACE
47 #endif
48
49 #ifdef USE_BACKTRACE
50 #include <execinfo.h>
51 #endif
52
53 #ifndef _WIN32
54 #include <dlfcn.h>
55 #endif
56 #include <stdlib.h>
57 #include <stdio.h>
58
59 #include "main/glheader.h"
60
61 #include "glapi.h"
62 #include "glapitable.h"
63
64 #ifdef GLXEXT
65 #include "os.h"
66 #endif
67
68 static void
69 __glapi_gentable_NoOp(void) {
70 const char *fstr = "Unknown";
71
72 /* Silence potential GCC warning for some #ifdef paths.
73 */
74 (void) fstr;
75 #if defined(USE_BACKTRACE)
76 #if !defined(GLXEXT)
77 if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
78 #endif
79 {
80 void *frames[2];
81
82 if(backtrace(frames, 2) == 2) {
83 Dl_info info;
84 dladdr(frames[1], &info);
85 if(info.dli_sname)
86 fstr = info.dli_sname;
87 }
88
89 #if !defined(GLXEXT)
90 fprintf(stderr, "Call to unimplemented API: %s\\n", fstr);
91 #endif
92 }
93 #endif
94 #if defined(GLXEXT)
95 LogMessage(X_ERROR, "GLX: Call to unimplemented API: %s\\n", fstr);
96 #endif
97 }
98
99 static void
100 __glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
101 GLuint entries = _glapi_get_dispatch_table_size();
102 void **dispatch = (void **) disp;
103 int i;
104
105 /* ISO C is annoying sometimes */
106 union {_glapi_proc p; void *v;} p;
107 p.p = __glapi_gentable_NoOp;
108
109 for(i=0; i < entries; i++)
110 if(dispatch[i] == NULL)
111 dispatch[i] = p.v;
112 }
113
114 struct _glapi_table *
115 _glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
116 struct _glapi_table *disp = calloc(1, _glapi_get_dispatch_table_size() * sizeof(_glapi_proc));
117 char symboln[512];
118
119 if(!disp)
120 return NULL;
121
122 if(symbol_prefix == NULL)
123 symbol_prefix = "";
124 """
125
126 footer = """
127 __glapi_gentable_set_remaining_noop(disp);
128
129 return disp;
130 }
131 """
132
133 body_template = """
134 if(!disp->%(name)s) {
135 void ** procp = (void **) &disp->%(name)s;
136 snprintf(symboln, sizeof(symboln), "%%s%(entry_point)s", symbol_prefix);
137 #ifdef _WIN32
138 *procp = GetProcAddress(handle, symboln);
139 #else
140 *procp = dlsym(handle, symboln);
141 #endif
142 }
143 """
144
145 class PrintCode(gl_XML.gl_print_base):
146
147 def __init__(self):
148 gl_XML.gl_print_base.__init__(self)
149
150 self.name = "gl_gen_table.py (from Mesa)"
151 self.license = license.bsd_license_template % ( \
152 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
153 (C) Copyright IBM Corporation 2004, 2005
154 (C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
155
156 return
157
158
159 def get_stack_size(self, f):
160 size = 0
161 for p in f.parameterIterator():
162 if p.is_padding:
163 continue
164
165 size += p.get_stack_size()
166
167 return size
168
169
170 def printRealHeader(self):
171 print header
172 return
173
174
175 def printRealFooter(self):
176 print footer
177 return
178
179
180 def printBody(self, api):
181 for f in api.functionIterateByOffset():
182 for entry_point in f.entry_points:
183 vars = { 'entry_point' : entry_point,
184 'name' : f.name }
185
186 print body_template % vars
187 return
188
189 def show_usage():
190 print "Usage: %s [-f input_file_name]" % sys.argv[0]
191 sys.exit(1)
192
193 if __name__ == '__main__':
194 file_name = "gl_API.xml"
195
196 try:
197 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
198 except Exception,e:
199 show_usage()
200
201 for (arg,val) in args:
202 if arg == "-f":
203 file_name = val
204
205 printer = PrintCode()
206
207 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
208 printer.Print(api)