Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 1 | #!/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 | |
| 28 | from xml.sax import saxutils |
| 29 | from xml.sax import make_parser |
| 30 | from xml.sax.handler import feature_namespaces |
| 31 | |
| 32 | import license |
| 33 | import gl_XML |
| 34 | import sys, getopt |
| 35 | |
| 36 | class PrintGlProcs(gl_XML.FilterGLAPISpecBase): |
| 37 | name = "gl_procs.py (from Mesa)" |
| 38 | |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 39 | def __init__(self, long_strings): |
| 40 | self.long_strings = long_strings |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 41 | 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 Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 46 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 47 | def printRealHeader(self): |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 48 | print '/* This file is only included by glapi.c and is used for' |
| 49 | print ' * the GetProcAddress() function' |
| 50 | print ' */' |
| 51 | print '' |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 52 | print 'typedef struct {' |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 53 | print ' GLint Name_offset;' |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 54 | print '#ifdef NEED_FUNCTION_POINTER' |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 55 | print ' _glapi_proc Address;' |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 56 | print '#endif' |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 57 | print ' GLuint Offset;' |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 58 | print '} glprocs_table_t;' |
| 59 | print '' |
| 60 | print '#ifdef NEED_FUNCTION_POINTER' |
Brian Paul | 767e15a | 2004-11-27 03:51:11 +0000 | [diff] [blame] | 61 | print '# define NAME_FUNC_OFFSET(n,f,o) { n , (_glapi_proc) f , o }' |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 62 | print '#else' |
| 63 | print '# define NAME_FUNC_OFFSET(n,f,o) { n , o }' |
| 64 | print '#endif' |
| 65 | print '' |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 66 | return |
| 67 | |
| 68 | def printRealFooter(self): |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 69 | 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 Romanick | 38e6e09 | 2005-01-25 23:53:13 +0000 | [diff] [blame] | 94 | for f in self.functionIterator(): |
| 95 | self.printFunctionString(f) |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 96 | |
| 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 Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 105 | base_offset = 0 |
Ian Romanick | 38e6e09 | 2005-01-25 23:53:13 +0000 | [diff] [blame] | 106 | |
| 107 | for f in self.functionIterator(): |
| 108 | self.printFunctionOffset(f, base_offset) |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 109 | |
| 110 | # The length of the function's name, plus 2 for "gl", |
| 111 | # plus 1 for the NUL. |
| 112 | |
Ian Romanick | 38e6e09 | 2005-01-25 23:53:13 +0000 | [diff] [blame] | 113 | base_offset += len(f.name) + 3 |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 114 | |
Brian Paul | a760ccf | 2004-12-03 15:24:34 +0000 | [diff] [blame] | 115 | print ' NAME_FUNC_OFFSET( -1, NULL, 0 )' |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 116 | print '};' |
| 117 | return |
| 118 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 119 | |
| 120 | def show_usage(): |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 121 | 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 Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 127 | sys.exit(1) |
| 128 | |
| 129 | if __name__ == '__main__': |
| 130 | file_name = "gl_API.xml" |
| 131 | |
| 132 | try: |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 133 | (args, trail) = getopt.getopt(sys.argv[1:], "f:m:") |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 134 | except Exception,e: |
| 135 | show_usage() |
| 136 | |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 137 | long_string = 1 |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 138 | for (arg,val) in args: |
| 139 | if arg == "-f": |
| 140 | file_name = val |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 141 | 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 Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 148 | |
Ian Romanick | 7867799 | 2004-05-27 00:05:13 +0000 | [diff] [blame] | 149 | dh = PrintGlProcs( long_string ) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 150 | |
| 151 | parser = make_parser() |
Ian Romanick | 2510ba6 | 2005-04-18 19:16:07 +0000 | [diff] [blame^] | 152 | parser.setFeature(feature_namespaces, 1) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 153 | parser.setContentHandler(dh) |
| 154 | |
| 155 | f = open(file_name) |
| 156 | |
| 157 | dh.printHeader() |
| 158 | parser.parse(f) |
| 159 | dh.printFooter() |