Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 2 | # |
| 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 Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 27 | |
| 28 | import sys |
| 29 | |
| 30 | import xgl |
| 31 | |
| 32 | class Subcommand(object): |
| 33 | def __init__(self, argv): |
| 34 | self.argv = argv |
| 35 | self.protos = () |
Chia-I Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame^] | 36 | self.headers = () |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 37 | |
| 38 | def run(self): |
| 39 | self.protos = xgl.core |
Chia-I Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame^] | 40 | self.headers = xgl.core_headers |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 41 | 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 Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame^] | 89 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 90 | |
| 91 | def generate_body(self): |
| 92 | pass |
| 93 | |
| 94 | def generate_footer(self): |
| 95 | pass |
| 96 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 97 | 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 Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 111 | def _generate_icd_dispatch_entrypoints(self, qual=""): |
| 112 | if qual: |
| 113 | qual += " " |
| 114 | |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 115 | 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 Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 125 | funcs.append("%s%s\n" |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 126 | "{\n" |
| 127 | " const struct icd_dispatch_table * const *disp =\n" |
| 128 | " (const struct icd_dispatch_table * const *) %s;\n" |
| 129 | " %s;\n" |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 130 | "}" % (qual, decl, proto.params[0].name, stmt)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 131 | |
| 132 | return "\n\n".join(funcs) |
| 133 | |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 134 | class LoaderSubcommand(Subcommand): |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 135 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 136 | return "#include \"loader.h\"" |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 137 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 138 | def generate_body(self): |
| 139 | body = [self._generate_icd_dispatch_table(), |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 140 | self._generate_icd_dispatch_entrypoints("LOADER_EXPORT")] |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 141 | |
| 142 | return "\n\n".join(body) |
| 143 | |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 144 | class IcdDispatchTableSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 145 | def generate_body(self): |
| 146 | return self._generate_icd_dispatch_table() |
| 147 | |
| 148 | class IcdDispatchEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 149 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 150 | return "#include \"icd.h\"" |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 151 | |
| 152 | def generate_body(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 153 | return self._generate_icd_dispatch_entrypoints("ICD_EXPORT") |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 154 | |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 155 | class 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 | |
| 213 | const 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 Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 224 | def main(): |
| 225 | subcommands = { |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 226 | "loader": LoaderSubcommand, |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 227 | "icd-dispatch-table": IcdDispatchTableSubcommand, |
| 228 | "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand, |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 229 | "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 230 | } |
| 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 | |
| 241 | if __name__ == "__main__": |
| 242 | main() |