blob: b5d51b0b40458b96fa5fc08f324740827c22b3ca [file] [log] [blame]
Ian Romanick73f59b02004-05-18 18:33:40 +00001#!/usr/bin/python2
2
3# (C) Copyright IBM Corporation 2004
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
28from xml.sax import saxutils
29from xml.sax import make_parser
30from xml.sax.handler import feature_namespaces
31
32import license
33import gl_XML
34import sys, getopt
35
36class PrintGlProcs(gl_XML.FilterGLAPISpecBase):
37 name = "gl_procs.py (from Mesa)"
38
Ian Romanick78677992004-05-27 00:05:13 +000039 def __init__(self, long_strings):
40 self.long_strings = long_strings
Ian Romanick73f59b02004-05-18 18:33:40 +000041 gl_XML.FilterGLAPISpecBase.__init__(self)
42 self.license = license.bsd_license_template % ( \
43"""Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
44(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
45
Ian Romanick78677992004-05-27 00:05:13 +000046
Ian Romanick73f59b02004-05-18 18:33:40 +000047 def printRealHeader(self):
Ian Romanick73f59b02004-05-18 18:33:40 +000048 print '/* This file is only included by glapi.c and is used for'
49 print ' * the GetProcAddress() function'
50 print ' */'
51 print ''
Ian Romanick78677992004-05-27 00:05:13 +000052 print 'typedef struct {'
Brian Paula760ccf2004-12-03 15:24:34 +000053 print ' GLint Name_offset;'
Ian Romanick78677992004-05-27 00:05:13 +000054 print '#ifdef NEED_FUNCTION_POINTER'
Brian Paul767e15a2004-11-27 03:51:11 +000055 print ' _glapi_proc Address;'
Ian Romanick78677992004-05-27 00:05:13 +000056 print '#endif'
Brian Paula760ccf2004-12-03 15:24:34 +000057 print ' GLuint Offset;'
Ian Romanick78677992004-05-27 00:05:13 +000058 print '} glprocs_table_t;'
59 print ''
60 print '#ifdef NEED_FUNCTION_POINTER'
Brian Paul767e15a2004-11-27 03:51:11 +000061 print '# define NAME_FUNC_OFFSET(n,f,o) { n , (_glapi_proc) f , o }'
Ian Romanick78677992004-05-27 00:05:13 +000062 print '#else'
63 print '# define NAME_FUNC_OFFSET(n,f,o) { n , o }'
64 print '#endif'
65 print ''
Ian Romanick73f59b02004-05-18 18:33:40 +000066 return
67
68 def printRealFooter(self):
Ian Romanick78677992004-05-27 00:05:13 +000069 print ''
70 print '#undef NAME_FUNC_OFFSET'
71 return
72
73 def printFunctionString(self, f):
74 if self.long_strings:
75 print ' "gl%s\\0"' % (f.name)
76 else:
77 print " 'g','l',",
78 for c in f.name:
79 print "'%s'," % (c),
80
81 print "'\\0',"
82
83 def printFunctionOffset(self, f, offset_of_name):
84 print ' NAME_FUNC_OFFSET( % 5u, gl%s, _gloffset_%s ),' % (offset_of_name, f.name, f.real_name)
85
86
87 def printFunctions(self):
88 print ''
89 if self.long_strings:
90 print 'static const char gl_string_table[] ='
91 else:
92 print 'static const char gl_string_table[] = {'
93
Ian Romanick38e6e092005-01-25 23:53:13 +000094 for f in self.functionIterator():
95 self.printFunctionString(f)
Ian Romanick78677992004-05-27 00:05:13 +000096
97 if self.long_strings:
98 print ' ;'
99 else:
100 print '};'
101
102 print ''
103 print 'static const glprocs_table_t static_functions[] = {'
104
Ian Romanick78677992004-05-27 00:05:13 +0000105 base_offset = 0
Ian Romanick38e6e092005-01-25 23:53:13 +0000106
107 for f in self.functionIterator():
108 self.printFunctionOffset(f, base_offset)
Ian Romanick78677992004-05-27 00:05:13 +0000109
110 # The length of the function's name, plus 2 for "gl",
111 # plus 1 for the NUL.
112
Ian Romanick38e6e092005-01-25 23:53:13 +0000113 base_offset += len(f.name) + 3
Ian Romanick78677992004-05-27 00:05:13 +0000114
Brian Paula760ccf2004-12-03 15:24:34 +0000115 print ' NAME_FUNC_OFFSET( -1, NULL, 0 )'
Ian Romanick73f59b02004-05-18 18:33:40 +0000116 print '};'
117 return
118
Ian Romanick73f59b02004-05-18 18:33:40 +0000119
120def show_usage():
Ian Romanick78677992004-05-27 00:05:13 +0000121 print "Usage: %s [-f input_file_name] [-m mode]" % sys.argv[0]
122 print "mode can be one of:"
123 print " long - Create code for compilers that can handle very "
124 print " long string constants. (default)"
125 print " short - Create code for compilers that can only handle "
126 print " ANSI C89 string constants."
Ian Romanick73f59b02004-05-18 18:33:40 +0000127 sys.exit(1)
128
129if __name__ == '__main__':
130 file_name = "gl_API.xml"
131
132 try:
Ian Romanick78677992004-05-27 00:05:13 +0000133 (args, trail) = getopt.getopt(sys.argv[1:], "f:m:")
Ian Romanick73f59b02004-05-18 18:33:40 +0000134 except Exception,e:
135 show_usage()
136
Ian Romanick78677992004-05-27 00:05:13 +0000137 long_string = 1
Ian Romanick73f59b02004-05-18 18:33:40 +0000138 for (arg,val) in args:
139 if arg == "-f":
140 file_name = val
Ian Romanick78677992004-05-27 00:05:13 +0000141 elif arg == "-m":
142 if val == "short":
143 long_string = 0
144 elif val == "long":
145 long_string = 1
146 else:
147 show_usage()
Ian Romanick73f59b02004-05-18 18:33:40 +0000148
Ian Romanick78677992004-05-27 00:05:13 +0000149 dh = PrintGlProcs( long_string )
Ian Romanick73f59b02004-05-18 18:33:40 +0000150
151 parser = make_parser()
Ian Romanick2510ba62005-04-18 19:16:07 +0000152 parser.setFeature(feature_namespaces, 1)
Ian Romanick73f59b02004-05-18 18:33:40 +0000153 parser.setContentHandler(dh)
154
155 f = open(file_name)
156
157 dh.printHeader()
158 parser.parse(f)
159 dh.printFooter()