Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / glapi / gl_procs.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 import license
29 import gl_XML, glX_XML
30 import sys, getopt
31
32 class PrintGlProcs(gl_XML.gl_print_base):
33 def __init__(self, long_strings):
34 gl_XML.gl_print_base.__init__(self)
35
36 self.long_strings = long_strings
37 self.name = "gl_procs.py (from Mesa)"
38 self.license = license.bsd_license_template % ( \
39 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
40 (C) Copyright IBM Corporation 2004, 2006""", "BRIAN PAUL, IBM")
41
42
43 def printRealHeader(self):
44 print """
45 /* This file is only included by glapi.c and is used for
46 * the GetProcAddress() function
47 */
48
49 typedef struct {
50 GLint Name_offset;
51 #if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)
52 _glapi_proc Address;
53 #endif
54 GLuint Offset;
55 } glprocs_table_t;
56
57 #if !defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
58 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , o }
59 #elif defined(NEED_FUNCTION_POINTER) && !defined(GLX_INDIRECT_RENDERING)
60 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f1 , o }
61 #elif defined(NEED_FUNCTION_POINTER) && defined(GLX_INDIRECT_RENDERING)
62 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f2 , o }
63 #elif !defined(NEED_FUNCTION_POINTER) && defined(GLX_INDIRECT_RENDERING)
64 # define NAME_FUNC_OFFSET(n,f1,f2,f3,o) { n , (_glapi_proc) f3 , o }
65 #endif
66
67 """
68 return
69
70 def printRealFooter(self):
71 print ''
72 print '#undef NAME_FUNC_OFFSET'
73 return
74
75 def printFunctionString(self, name):
76 if self.long_strings:
77 print ' "gl%s\\0"' % (name)
78 else:
79 print " 'g','l',",
80 for c in name:
81 print "'%s'," % (c),
82
83 print "'\\0',"
84
85
86 def printBody(self, api):
87 print ''
88 if self.long_strings:
89 print 'static const char gl_string_table[] ='
90 else:
91 print 'static const char gl_string_table[] = {'
92
93 base_offset = 0
94 table = []
95 for func in api.functionIterateByOffset():
96 name = func.dispatch_name()
97 self.printFunctionString(func.name)
98 table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.name))
99
100 # The length of the function's name, plus 2 for "gl",
101 # plus 1 for the NUL.
102
103 base_offset += len(func.name) + 3
104
105
106 for func in api.functionIterateByOffset():
107 for n in func.entry_points:
108 if n != func.name:
109 name = func.dispatch_name()
110 self.printFunctionString( n )
111
112 if func.has_different_protocol(n):
113 alt_name = "gl" + func.static_glx_name(n)
114 table.append((base_offset, "gl" + name, alt_name, alt_name, func.name))
115 else:
116 table.append((base_offset, "gl" + name, "gl" + name, "NULL", func.name))
117
118 base_offset += len(n) + 3
119
120
121 if self.long_strings:
122 print ' ;'
123 else:
124 print '};'
125
126 print ''
127 print ''
128 print "#ifdef USE_MGL_NAMESPACE"
129 for func in api.functionIterateByOffset():
130 for n in func.entry_points:
131 if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
132 print '#define gl_dispatch_stub_%u mgl_dispatch_stub_%u' % (func.offset, func.offset)
133 break
134 print "#endif /* USE_MGL_NAMESPACE */"
135 print ''
136 print ''
137 print '#if defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING)'
138 for func in api.functionIterateByOffset():
139 for n in func.entry_points:
140 if (not func.is_static_entry_point(func.name)) or (func.has_different_protocol(n) and not func.is_static_entry_point(n)):
141 print '%s GLAPIENTRY gl_dispatch_stub_%u(%s);' % (func.return_type, func.offset, func.get_parameter_string())
142 break
143
144 print '#endif /* defined(NEED_FUNCTION_POINTER) || defined(GLX_INDIRECT_RENDERING) */'
145
146 print ''
147 print 'static const glprocs_table_t static_functions[] = {'
148
149 for info in table:
150 print ' NAME_FUNC_OFFSET(%5u, %s, %s, %s, _gloffset_%s),' % info
151
152 print ' NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)'
153 print '};'
154 return
155
156
157 def show_usage():
158 print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0]
159 print "mode can be one of:"
160 print " long - Create code for compilers that can handle very"
161 print " long string constants. (default)"
162 print " short - Create code for compilers that can only handle"
163 print " ANSI C89 string constants."
164 sys.exit(1)
165
166 if __name__ == '__main__':
167 file_name = "gl_API.xml"
168
169 try:
170 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
171 except Exception,e:
172 show_usage()
173
174 long_string = 1
175 for (arg,val) in args:
176 if arg == "-f":
177 file_name = val
178 elif arg == "-m":
179 if val == "short":
180 long_string = 0
181 elif val == "long":
182 long_string = 1
183 else:
184 show_usage()
185
186 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
187 printer = PrintGlProcs(long_string)
188 printer.Print(api)