Sort the enums in the Python code, instead of at runtime. (Zack Rusin)
[mesa.git] / src / mesa / glapi / gl_enums.py
1 #!/usr/bin/python2
2 # -*- Mode: Python; py-indent-offset: 8 -*-
3
4 # (C) Copyright Zack Rusin 2005
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 # Zack Rusin <zack@kde.org>
28
29 from xml.sax import saxutils
30 from xml.sax import make_parser
31 from xml.sax.handler import feature_namespaces
32
33 import license
34 import gl_XML
35 import sys, getopt
36
37 class PrintGlEnums(gl_XML.FilterGLAPISpecBase):
38 name = "gl_enums.py (from Mesa)"
39
40 def __init__(self):
41 gl_XML.FilterGLAPISpecBase.__init__(self)
42 self.license = license.bsd_license_template % ( \
43 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
44
45
46 def printRealHeader(self):
47 print '#include "glheader.h"'
48 print '#include "enums.h"'
49 print '#include "imports.h"'
50 print ''
51 print 'typedef struct {'
52 print ' const char *c;'
53 print ' int n;'
54 print '} enum_elt;'
55 print ''
56 print 'static const enum_elt all_enums[] ='
57 print '{'
58 return
59
60 def printBody(self):
61 print """
62 };
63
64 #define Elements(x) sizeof(x)/sizeof(*x)
65
66 typedef int (*cfunc)(const void *, const void *);
67
68 /* note the extra level of indirection
69 */
70 static int compar_name( const enum_elt **a, const enum_elt **b )
71 {
72 return _mesa_strcmp((*a)->c, (*b)->c);
73 }
74
75 static int compar_nr( const enum_elt *a, const enum_elt *b )
76 {
77 return a->n - b->n;
78 }
79
80
81 static char token_tmp[20];
82
83 const char *_mesa_lookup_enum_by_nr( int nr )
84 {
85 enum_elt tmp, *e;
86
87 tmp.n = nr;
88
89 e = (enum_elt *)bsearch( &tmp, all_enums, Elements(all_enums),
90 sizeof(*all_enums), (cfunc) compar_nr );
91
92 if (e) {
93 return e->c;
94 }
95 else {
96 /* this isn't re-entrant safe, no big deal here */
97 _mesa_sprintf(token_tmp, "0x%x", nr);
98 return token_tmp;
99 }
100 }
101
102 int _mesa_lookup_enum_by_name( const char *symbol )
103 {
104 enum_elt tmp, *e, **f;
105 static const enum_elt * const index1[] = {"""
106
107 def printRealFooter(self):
108 print """ };
109
110 if (!symbol)
111 return 0;
112
113 tmp.c = symbol;
114 e = &tmp;
115
116 f = (enum_elt **)bsearch( &e, index1, Elements(all_enums),
117 sizeof(*index1), (cfunc) compar_name );
118
119 return f ? (*f)->n : -1;
120 }
121
122 """
123 return
124
125 def printFunctions(self):
126 self.enums.sort(lambda x,y: x.value - y.value)
127 for enum in self.enums:
128 print ' { "%s", 0x%X },' % (enum.name, enum.value)
129 nameEnums = self.enums[:]
130 self.printBody();
131 self.enums.sort(lambda x,y: cmp(x.name, y.name))
132 for enum in self.enums:
133 print ' &all_enums[%d], ' % (nameEnums.index(enum))
134 return
135
136
137 def show_usage():
138 print "Usage: %s [-f input_file_name]" % sys.argv[0]
139 sys.exit(1)
140
141 if __name__ == '__main__':
142 file_name = "gl_API.xml"
143
144 try:
145 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
146 except Exception,e:
147 show_usage()
148
149 for (arg,val) in args:
150 if arg == "-f":
151 file_name = val
152
153 dh = PrintGlEnums()
154
155 parser = make_parser()
156 parser.setFeature(feature_namespaces, 0)
157 parser.setContentHandler(dh)
158
159 f = open(file_name)
160
161 dh.printHeader()
162 parser.parse(f)
163 dh.printFooter()