glapi: Add API that can create a _glapi_table from a dlfcn handle
[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 #include <dlfcn.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40
41 #include <GL/gl.h>
42
43 #include "glapi.h"
44 #include "glapitable.h"
45 #include "main/dispatch.h"
46
47 struct _glapi_table *
48 _glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
49 struct _glapi_table *disp = calloc(1, sizeof(struct _glapi_table));
50 char symboln[512];
51
52 if(!disp)
53 return NULL;
54 """
55
56 footer = """
57 return disp;
58 }
59 """
60
61 body_template = """
62 if(!disp->%(name)s) {
63 snprintf(symboln, sizeof(symboln), "%%s%(entry_point)s", symbol_prefix);
64 SET_%(name)s(disp, dlsym(handle, symboln));
65 }
66 """
67
68 class PrintCode(gl_XML.gl_print_base):
69
70 def __init__(self):
71 gl_XML.gl_print_base.__init__(self)
72
73 self.name = "gl_gen_table.py (from Mesa)"
74 self.license = license.bsd_license_template % ( \
75 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
76 (C) Copyright IBM Corporation 2004, 2005
77 (C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
78
79 return
80
81
82 def get_stack_size(self, f):
83 size = 0
84 for p in f.parameterIterator():
85 if p.is_padding:
86 continue
87
88 size += p.get_stack_size()
89
90 return size
91
92
93 def printRealHeader(self):
94 print header
95 return
96
97
98 def printRealFooter(self):
99 print footer
100 return
101
102
103 def printBody(self, api):
104 for f in api.functionIterateByOffset():
105 for entry_point in f.entry_points:
106 vars = { 'entry_point' : entry_point,
107 'name' : f.name }
108
109 print body_template % vars
110 return
111
112 def show_usage():
113 print "Usage: %s [-f input_file_name]" % sys.argv[0]
114 sys.exit(1)
115
116 if __name__ == '__main__':
117 file_name = "gl_API.xml"
118
119 try:
120 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
121 except Exception,e:
122 show_usage()
123
124 for (arg,val) in args:
125 if arg == "-f":
126 file_name = val
127
128 printer = PrintCode()
129
130 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
131 printer.Print(api)