blob: 821cc93a662f0671d1d35bf6d6748407a84204a4 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
2
3import sys
4
5import xgl
6
7class Subcommand(object):
8 def __init__(self, argv):
9 self.argv = argv
10 self.protos = ()
11
12 def run(self):
13 self.protos = xgl.core
14 print(self.generate())
15
16 def generate(self):
17 copyright = self.generate_copyright()
18 header = self.generate_header()
19 body = self.generate_body()
20 footer = self.generate_footer()
21
22 contents = []
23 if copyright:
24 contents.append(copyright)
25 if header:
26 contents.append(header)
27 if body:
28 contents.append(body)
29 if footer:
30 contents.append(footer)
31
32 return "\n\n".join(contents)
33
34 def generate_copyright(self):
35 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
36
37/*
38 * XGL
39 *
40 * Copyright (C) 2014 LunarG, Inc.
41 *
42 * Permission is hereby granted, free of charge, to any person obtaining a
43 * copy of this software and associated documentation files (the "Software"),
44 * to deal in the Software without restriction, including without limitation
45 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
46 * and/or sell copies of the Software, and to permit persons to whom the
47 * Software is furnished to do so, subject to the following conditions:
48 *
49 * The above copyright notice and this permission notice shall be included
50 * in all copies or substantial portions of the Software.
51 *
52 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
53 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
54 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
55 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
56 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
57 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
58 * DEALINGS IN THE SOFTWARE.
59 */"""
60
61 def generate_header(self):
62 pass
63
64 def generate_body(self):
65 pass
66
67 def generate_footer(self):
68 pass
69
Chia-I Wufb2559d2014-08-01 11:19:52 +080070 def _generate_icd_dispatch_table(self):
71 proto_map = {}
72 for proto in self.protos:
73 proto_map[proto.name] = proto
74
75 entries = []
76 for name in xgl.icd_dispatch_table:
77 proto = proto_map[name]
78 entries.append(proto.c_typedef(attr="XGLAPI"))
79
80 return """struct icd_dispatch_table {
81 %s;
82};""" % ";\n ".join(entries)
83
Chia-I Wu19300602014-08-04 08:03:57 +080084 def _generate_icd_dispatch_entrypoints(self, qual=""):
85 if qual:
86 qual += " "
87
Chia-I Wudac3e492014-08-02 23:49:43 +080088 funcs = []
89 for proto in self.protos:
90 if not xgl.is_dispatchable(proto):
91 continue
92
93 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
94 stmt = "(*disp)->%s" % proto.c_call()
95 if proto.ret != "XGL_VOID":
96 stmt = "return " + stmt
97
Chia-I Wu19300602014-08-04 08:03:57 +080098 funcs.append("%s%s\n"
Chia-I Wudac3e492014-08-02 23:49:43 +080099 "{\n"
100 " const struct icd_dispatch_table * const *disp =\n"
101 " (const struct icd_dispatch_table * const *) %s;\n"
102 " %s;\n"
Chia-I Wu19300602014-08-04 08:03:57 +0800103 "}" % (qual, decl, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800104
105 return "\n\n".join(funcs)
106
Chia-I Wu1e122262014-08-01 11:58:32 +0800107class LoaderSubcommand(Subcommand):
Chia-I Wu1e122262014-08-01 11:58:32 +0800108 def generate_header(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800109 return "#include \"loader.h\""
Chia-I Wu1e122262014-08-01 11:58:32 +0800110
Chia-I Wufb2559d2014-08-01 11:19:52 +0800111 def generate_body(self):
112 body = [self._generate_icd_dispatch_table(),
Chia-I Wu19300602014-08-04 08:03:57 +0800113 self._generate_icd_dispatch_entrypoints("LOADER_EXPORT")]
Chia-I Wufb2559d2014-08-01 11:19:52 +0800114
115 return "\n\n".join(body)
116
Chia-I Wu03537f72014-08-03 09:55:18 +0800117class IcdDispatchTableSubcommand(Subcommand):
118 def generate_header(self):
119 return "\n".join([
120 "#include <xgl.h>",
121 "#include <xglDbg.h>"])
122
123 def generate_body(self):
124 return self._generate_icd_dispatch_table()
125
126class IcdDispatchTableSubcommand(Subcommand):
127 def generate_header(self):
128 return "\n".join([
129 "#include <xgl.h>",
130 "#include <xglDbg.h>"])
131
132 def generate_body(self):
133 return self._generate_icd_dispatch_table()
134
135class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu03537f72014-08-03 09:55:18 +0800136 def generate_header(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800137 return "#include \"icd.h\""
Chia-I Wu03537f72014-08-03 09:55:18 +0800138
139 def generate_body(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800140 return self._generate_icd_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu03537f72014-08-03 09:55:18 +0800141
Chia-I Wudf3c4322014-08-04 10:08:08 +0800142class IcdDispatchDummyImplSubcommand(Subcommand):
143 def run(self):
144 if len(self.argv) != 1:
145 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
146 return
147
148 self.prefix = self.argv[0]
149
150 super().run()
151
152 def generate_header(self):
153 return "#include \"icd.h\""
154
155 def _generate_stub_decl(self, proto):
156 plist = []
157 for param in proto.params:
158 idx = param.ty.find("[")
159 if idx < 0:
160 idx = len(param.ty)
161
162 pad = 44 - idx
163 if pad <= 0:
164 pad = 1
165
166 plist.append(" %s%s%s%s" % (param.ty[:idx],
167 " " * pad, param.name, param.ty[idx:]))
168
169 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
170 proto.name, ",\n".join(plist))
171
172 def _generate_stubs(self):
173 stubs = []
174 for proto in self.protos:
175 if not xgl.is_dispatchable(proto):
176 continue
177
178 decl = self._generate_stub_decl(proto)
179 if proto.ret != "XGL_VOID":
180 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
181 else:
182 stmt = ""
183
184 stubs.append("static %s\n{\n%s}" % (decl, stmt))
185
186 return "\n\n".join(stubs)
187
188
189 def _generate_tables(self):
190 initializer = []
191 for proto in self.protos:
192 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
193 initializer.append(".%s = %s%s" %
194 (proto.name, prefix, proto.name))
195
196 return """const struct icd_dispatch_table %s_normal_dispatch_table = {
197 %s,
198};
199
200const struct icd_dispatch_table %s_debug_dispatch_table = {
201 %s,
202};""" % (self.prefix, ",\n ".join(initializer),
203 self.prefix, ",\n ".join(initializer))
204
205 def generate_body(self):
206 body = [self._generate_stubs(),
207 self._generate_tables()]
208
209 return "\n\n".join(body)
210
Chia-I Wufb2559d2014-08-01 11:19:52 +0800211def main():
212 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800213 "loader": LoaderSubcommand,
Chia-I Wu03537f72014-08-03 09:55:18 +0800214 "icd-dispatch-table": IcdDispatchTableSubcommand,
215 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wudf3c4322014-08-04 10:08:08 +0800216 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800217 }
218
219 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
220 print("Usage: %s <subcommand> [options]" % sys.argv[0])
221 print
222 print("Available sucommands are: %s" % " ".join(subcommands))
223 exit(1)
224
225 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
226 subcmd.run()
227
228if __name__ == "__main__":
229 main()