blob: 16698b56c605c8971a4a3840027cfd7a0094084e [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 = ()
Chia-I Wu6ae460f2014-09-13 13:36:06 +080036 self.headers = ()
Chia-I Wufb2559d2014-08-01 11:19:52 +080037
38 def run(self):
39 self.protos = xgl.core
Chia-I Wu6ae460f2014-09-13 13:36:06 +080040 self.headers = xgl.core_headers
Chia-I Wufb2559d2014-08-01 11:19:52 +080041 print(self.generate())
42
43 def generate(self):
44 copyright = self.generate_copyright()
45 header = self.generate_header()
46 body = self.generate_body()
47 footer = self.generate_footer()
48
49 contents = []
50 if copyright:
51 contents.append(copyright)
52 if header:
53 contents.append(header)
54 if body:
55 contents.append(body)
56 if footer:
57 contents.append(footer)
58
59 return "\n\n".join(contents)
60
61 def generate_copyright(self):
62 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
63
64/*
65 * XGL
66 *
67 * Copyright (C) 2014 LunarG, Inc.
68 *
69 * Permission is hereby granted, free of charge, to any person obtaining a
70 * copy of this software and associated documentation files (the "Software"),
71 * to deal in the Software without restriction, including without limitation
72 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
73 * and/or sell copies of the Software, and to permit persons to whom the
74 * Software is furnished to do so, subject to the following conditions:
75 *
76 * The above copyright notice and this permission notice shall be included
77 * in all copies or substantial portions of the Software.
78 *
79 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
81 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
82 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
83 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
84 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
85 * DEALINGS IN THE SOFTWARE.
86 */"""
87
88 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080089 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080090
91 def generate_body(self):
92 pass
93
94 def generate_footer(self):
95 pass
96
Chia-I Wufb2559d2014-08-01 11:19:52 +080097 def _generate_icd_dispatch_table(self):
98 proto_map = {}
99 for proto in self.protos:
100 proto_map[proto.name] = proto
101
102 entries = []
103 for name in xgl.icd_dispatch_table:
104 proto = proto_map[name]
105 entries.append(proto.c_typedef(attr="XGLAPI"))
106
107 return """struct icd_dispatch_table {
108 %s;
109};""" % ";\n ".join(entries)
110
Chia-I Wu468e3c32014-08-04 08:03:57 +0800111 def _generate_icd_dispatch_entrypoints(self, qual=""):
112 if qual:
113 qual += " "
114
Chia-I Wudac3e492014-08-02 23:49:43 +0800115 funcs = []
116 for proto in self.protos:
117 if not xgl.is_dispatchable(proto):
118 continue
119
120 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
121 stmt = "(*disp)->%s" % proto.c_call()
122 if proto.ret != "XGL_VOID":
123 stmt = "return " + stmt
124
Chia-I Wu468e3c32014-08-04 08:03:57 +0800125 funcs.append("%s%s\n"
Chia-I Wudac3e492014-08-02 23:49:43 +0800126 "{\n"
127 " const struct icd_dispatch_table * const *disp =\n"
128 " (const struct icd_dispatch_table * const *) %s;\n"
129 " %s;\n"
Chia-I Wu468e3c32014-08-04 08:03:57 +0800130 "}" % (qual, decl, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800131
132 return "\n\n".join(funcs)
133
Chia-I Wu1e122262014-08-01 11:58:32 +0800134class LoaderSubcommand(Subcommand):
Chia-I Wu1e122262014-08-01 11:58:32 +0800135 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800136 return "#include \"loader.h\""
Chia-I Wu1e122262014-08-01 11:58:32 +0800137
Chia-I Wufb2559d2014-08-01 11:19:52 +0800138 def generate_body(self):
139 body = [self._generate_icd_dispatch_table(),
Chia-I Wu468e3c32014-08-04 08:03:57 +0800140 self._generate_icd_dispatch_entrypoints("LOADER_EXPORT")]
Chia-I Wufb2559d2014-08-01 11:19:52 +0800141
142 return "\n\n".join(body)
143
Chia-I Wu84dace92014-08-03 09:55:18 +0800144class IcdDispatchTableSubcommand(Subcommand):
Chia-I Wu84dace92014-08-03 09:55:18 +0800145 def generate_body(self):
146 return self._generate_icd_dispatch_table()
147
148class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu84dace92014-08-03 09:55:18 +0800149 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800150 return "#include \"icd.h\""
Chia-I Wu84dace92014-08-03 09:55:18 +0800151
152 def generate_body(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800153 return self._generate_icd_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu84dace92014-08-03 09:55:18 +0800154
Chia-I Wu30c78292014-08-04 10:08:08 +0800155class IcdDispatchDummyImplSubcommand(Subcommand):
156 def run(self):
157 if len(self.argv) != 1:
158 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
159 return
160
161 self.prefix = self.argv[0]
162
163 super().run()
164
165 def generate_header(self):
166 return "#include \"icd.h\""
167
168 def _generate_stub_decl(self, proto):
169 plist = []
170 for param in proto.params:
171 idx = param.ty.find("[")
172 if idx < 0:
173 idx = len(param.ty)
174
175 pad = 44 - idx
176 if pad <= 0:
177 pad = 1
178
179 plist.append(" %s%s%s%s" % (param.ty[:idx],
180 " " * pad, param.name, param.ty[idx:]))
181
182 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
183 proto.name, ",\n".join(plist))
184
185 def _generate_stubs(self):
186 stubs = []
187 for proto in self.protos:
188 if not xgl.is_dispatchable(proto):
189 continue
190
191 decl = self._generate_stub_decl(proto)
192 if proto.ret != "XGL_VOID":
193 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
194 else:
195 stmt = ""
196
197 stubs.append("static %s\n{\n%s}" % (decl, stmt))
198
199 return "\n\n".join(stubs)
200
201
202 def _generate_tables(self):
203 initializer = []
204 for proto in self.protos:
205 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
206 initializer.append(".%s = %s%s" %
207 (proto.name, prefix, proto.name))
208
209 return """const struct icd_dispatch_table %s_normal_dispatch_table = {
210 %s,
211};
212
213const struct icd_dispatch_table %s_debug_dispatch_table = {
214 %s,
215};""" % (self.prefix, ",\n ".join(initializer),
216 self.prefix, ",\n ".join(initializer))
217
218 def generate_body(self):
219 body = [self._generate_stubs(),
220 self._generate_tables()]
221
222 return "\n\n".join(body)
223
Chia-I Wufb2559d2014-08-01 11:19:52 +0800224def main():
225 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800226 "loader": LoaderSubcommand,
Chia-I Wu84dace92014-08-03 09:55:18 +0800227 "icd-dispatch-table": IcdDispatchTableSubcommand,
228 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wu30c78292014-08-04 10:08:08 +0800229 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800230 }
231
232 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
233 print("Usage: %s <subcommand> [options]" % sys.argv[0])
234 print
235 print("Available sucommands are: %s" % " ".join(subcommands))
236 exit(1)
237
238 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
239 subcmd.run()
240
241if __name__ == "__main__":
242 main()