blob: a885c685888228f2ddedee9465823e69e5327f02 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu44e42362014-09-02 08:32:09 +08002#
3# XGL
4#
5# Copyright (C) 2014 LunarG, Inc.
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# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the 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 NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS 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
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080027
28import sys
29
30import xgl
31
32class Subcommand(object):
33 def __init__(self, argv):
34 self.argv = argv
35 self.protos = ()
36
37 def run(self):
38 self.protos = xgl.core
39 print(self.generate())
40
41 def generate(self):
42 copyright = self.generate_copyright()
43 header = self.generate_header()
44 body = self.generate_body()
45 footer = self.generate_footer()
46
47 contents = []
48 if copyright:
49 contents.append(copyright)
50 if header:
51 contents.append(header)
52 if body:
53 contents.append(body)
54 if footer:
55 contents.append(footer)
56
57 return "\n\n".join(contents)
58
59 def generate_copyright(self):
60 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
61
62/*
63 * XGL
64 *
65 * Copyright (C) 2014 LunarG, Inc.
66 *
67 * Permission is hereby granted, free of charge, to any person obtaining a
68 * copy of this software and associated documentation files (the "Software"),
69 * to deal in the Software without restriction, including without limitation
70 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
71 * and/or sell copies of the Software, and to permit persons to whom the
72 * Software is furnished to do so, subject to the following conditions:
73 *
74 * The above copyright notice and this permission notice shall be included
75 * in all copies or substantial portions of the Software.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
82 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
83 * DEALINGS IN THE SOFTWARE.
84 */"""
85
86 def generate_header(self):
87 pass
88
89 def generate_body(self):
90 pass
91
92 def generate_footer(self):
93 pass
94
Chia-I Wufb2559d2014-08-01 11:19:52 +080095 def _generate_icd_dispatch_table(self):
96 proto_map = {}
97 for proto in self.protos:
98 proto_map[proto.name] = proto
99
100 entries = []
101 for name in xgl.icd_dispatch_table:
102 proto = proto_map[name]
103 entries.append(proto.c_typedef(attr="XGLAPI"))
104
105 return """struct icd_dispatch_table {
106 %s;
107};""" % ";\n ".join(entries)
108
Chia-I Wu468e3c32014-08-04 08:03:57 +0800109 def _generate_icd_dispatch_entrypoints(self, qual=""):
110 if qual:
111 qual += " "
112
Chia-I Wudac3e492014-08-02 23:49:43 +0800113 funcs = []
114 for proto in self.protos:
115 if not xgl.is_dispatchable(proto):
116 continue
117
118 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
119 stmt = "(*disp)->%s" % proto.c_call()
120 if proto.ret != "XGL_VOID":
121 stmt = "return " + stmt
122
Chia-I Wu468e3c32014-08-04 08:03:57 +0800123 funcs.append("%s%s\n"
Chia-I Wudac3e492014-08-02 23:49:43 +0800124 "{\n"
125 " const struct icd_dispatch_table * const *disp =\n"
126 " (const struct icd_dispatch_table * const *) %s;\n"
127 " %s;\n"
Chia-I Wu468e3c32014-08-04 08:03:57 +0800128 "}" % (qual, decl, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800129
130 return "\n\n".join(funcs)
131
Chia-I Wu1e122262014-08-01 11:58:32 +0800132class LoaderSubcommand(Subcommand):
Chia-I Wu1e122262014-08-01 11:58:32 +0800133 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800134 return "#include \"loader.h\""
Chia-I Wu1e122262014-08-01 11:58:32 +0800135
Chia-I Wufb2559d2014-08-01 11:19:52 +0800136 def generate_body(self):
137 body = [self._generate_icd_dispatch_table(),
Chia-I Wu468e3c32014-08-04 08:03:57 +0800138 self._generate_icd_dispatch_entrypoints("LOADER_EXPORT")]
Chia-I Wufb2559d2014-08-01 11:19:52 +0800139
140 return "\n\n".join(body)
141
Chia-I Wu84dace92014-08-03 09:55:18 +0800142class IcdDispatchTableSubcommand(Subcommand):
143 def generate_header(self):
144 return "\n".join([
145 "#include <xgl.h>",
146 "#include <xglDbg.h>"])
147
148 def generate_body(self):
149 return self._generate_icd_dispatch_table()
150
151class IcdDispatchTableSubcommand(Subcommand):
152 def generate_header(self):
153 return "\n".join([
154 "#include <xgl.h>",
155 "#include <xglDbg.h>"])
156
157 def generate_body(self):
158 return self._generate_icd_dispatch_table()
159
160class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu84dace92014-08-03 09:55:18 +0800161 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800162 return "#include \"icd.h\""
Chia-I Wu84dace92014-08-03 09:55:18 +0800163
164 def generate_body(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800165 return self._generate_icd_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu84dace92014-08-03 09:55:18 +0800166
Chia-I Wu30c78292014-08-04 10:08:08 +0800167class IcdDispatchDummyImplSubcommand(Subcommand):
168 def run(self):
169 if len(self.argv) != 1:
170 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
171 return
172
173 self.prefix = self.argv[0]
174
175 super().run()
176
177 def generate_header(self):
178 return "#include \"icd.h\""
179
180 def _generate_stub_decl(self, proto):
181 plist = []
182 for param in proto.params:
183 idx = param.ty.find("[")
184 if idx < 0:
185 idx = len(param.ty)
186
187 pad = 44 - idx
188 if pad <= 0:
189 pad = 1
190
191 plist.append(" %s%s%s%s" % (param.ty[:idx],
192 " " * pad, param.name, param.ty[idx:]))
193
194 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
195 proto.name, ",\n".join(plist))
196
197 def _generate_stubs(self):
198 stubs = []
199 for proto in self.protos:
200 if not xgl.is_dispatchable(proto):
201 continue
202
203 decl = self._generate_stub_decl(proto)
204 if proto.ret != "XGL_VOID":
205 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
206 else:
207 stmt = ""
208
209 stubs.append("static %s\n{\n%s}" % (decl, stmt))
210
211 return "\n\n".join(stubs)
212
213
214 def _generate_tables(self):
215 initializer = []
216 for proto in self.protos:
217 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
218 initializer.append(".%s = %s%s" %
219 (proto.name, prefix, proto.name))
220
221 return """const struct icd_dispatch_table %s_normal_dispatch_table = {
222 %s,
223};
224
225const struct icd_dispatch_table %s_debug_dispatch_table = {
226 %s,
227};""" % (self.prefix, ",\n ".join(initializer),
228 self.prefix, ",\n ".join(initializer))
229
230 def generate_body(self):
231 body = [self._generate_stubs(),
232 self._generate_tables()]
233
234 return "\n\n".join(body)
235
Chia-I Wufb2559d2014-08-01 11:19:52 +0800236def main():
237 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800238 "loader": LoaderSubcommand,
Chia-I Wu84dace92014-08-03 09:55:18 +0800239 "icd-dispatch-table": IcdDispatchTableSubcommand,
240 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wu30c78292014-08-04 10:08:08 +0800241 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800242 }
243
244 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
245 print("Usage: %s <subcommand> [options]" % sys.argv[0])
246 print
247 print("Available sucommands are: %s" % " ".join(subcommands))
248 exit(1)
249
250 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
251 subcmd.run()
252
253if __name__ == "__main__":
254 main()