Merge branch 'gallium-polygon-stipple'
[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 = """
37 #if defined(DEBUG) && !defined(_WIN32_WCE)
38 #include <execinfo.h>
39 #endif
40
41 #include <dlfcn.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44
45 #include <GL/gl.h>
46
47 #include "glapi.h"
48 #include "glapitable.h"
49
50 static void
51 __glapi_gentable_NoOp(void) {
52 #if defined(DEBUG) && !defined(_WIN32_WCE)
53 if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) {
54 const char *fstr = "Unknown";
55 void *frames[2];
56
57 if(backtrace(frames, 2) == 2) {
58 Dl_info info;
59 dladdr(frames[1], &info);
60 if(info.dli_sname)
61 fstr = info.dli_sname;
62 }
63
64 fprintf(stderr, "Call to unimplemented API: %s\\n", fstr);
65 }
66 #endif
67 }
68
69 static void
70 __glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
71 GLuint entries = _glapi_get_dispatch_table_size();
72 void **dispatch = (void **) disp;
73 int i;
74
75 /* ISO C is annoying sometimes */
76 union {_glapi_proc p; void *v;} p;
77 p.p = __glapi_gentable_NoOp;
78
79 for(i=0; i < entries; i++)
80 if(dispatch[i] == NULL)
81 dispatch[i] = p.v;
82 }
83
84 struct _glapi_table *
85 _glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
86 struct _glapi_table *disp = calloc(1, sizeof(struct _glapi_table));
87 char symboln[512];
88
89 if(!disp)
90 return NULL;
91
92 if(symbol_prefix == NULL)
93 symbol_prefix = "";
94 """
95
96 footer = """
97 __glapi_gentable_set_remaining_noop(disp);
98
99 return disp;
100 }
101 """
102
103 body_template = """
104 if(!disp->%(name)s) {
105 void ** procp = (void **) &disp->%(name)s;
106 snprintf(symboln, sizeof(symboln), "%%s%(entry_point)s", symbol_prefix);
107 *procp = dlsym(handle, symboln);
108 }
109 """
110
111 class PrintCode(gl_XML.gl_print_base):
112
113 def __init__(self):
114 gl_XML.gl_print_base.__init__(self)
115
116 self.name = "gl_gen_table.py (from Mesa)"
117 self.license = license.bsd_license_template % ( \
118 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
119 (C) Copyright IBM Corporation 2004, 2005
120 (C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
121
122 return
123
124
125 def get_stack_size(self, f):
126 size = 0
127 for p in f.parameterIterator():
128 if p.is_padding:
129 continue
130
131 size += p.get_stack_size()
132
133 return size
134
135
136 def printRealHeader(self):
137 print header
138 return
139
140
141 def printRealFooter(self):
142 print footer
143 return
144
145
146 def printBody(self, api):
147 for f in api.functionIterateByOffset():
148 for entry_point in f.entry_points:
149 vars = { 'entry_point' : entry_point,
150 'name' : f.name }
151
152 print body_template % vars
153 return
154
155 def show_usage():
156 print "Usage: %s [-f input_file_name]" % sys.argv[0]
157 sys.exit(1)
158
159 if __name__ == '__main__':
160 file_name = "gl_API.xml"
161
162 try:
163 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
164 except Exception,e:
165 show_usage()
166
167 for (arg,val) in args:
168 if arg == "-f":
169 file_name = val
170
171 printer = PrintCode()
172
173 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
174 printer.Print(api)