Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import sys |
| 4 | |
| 5 | import xgl |
| 6 | |
| 7 | class 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 | |
| 70 | class LoaderSubcommand(Subcommand): |
| 71 | # functions that the loader implements |
| 72 | impl = ("InitAndEnumerateGpus", |
| 73 | "DbgRegisterMsgCallback", |
| 74 | "DbgUnregisterMsgCallback", |
| 75 | "DbgSetGlobalOption") |
| 76 | |
| 77 | def generate_header(self): |
| 78 | return "\n".join([ |
| 79 | "#include <xgl.h>", |
| 80 | "#include <xglDbg.h>"]) |
| 81 | |
| 82 | def _generate_icd_dispatch_table(self): |
| 83 | proto_map = {} |
| 84 | for proto in self.protos: |
| 85 | proto_map[proto.name] = proto |
| 86 | |
| 87 | entries = [] |
| 88 | for name in xgl.icd_dispatch_table: |
| 89 | proto = proto_map[name] |
| 90 | entries.append(proto.c_typedef(attr="XGLAPI")) |
| 91 | |
| 92 | return """struct icd_dispatch_table { |
| 93 | %s; |
| 94 | };""" % ";\n ".join(entries) |
| 95 | |
| 96 | def _generate_api(self): |
| 97 | funcs = [] |
| 98 | for proto in self.protos: |
| 99 | if proto.name in self.impl: |
| 100 | continue |
| 101 | |
| 102 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 103 | stmt = "(*disp)->%s" % proto.c_call() |
| 104 | if proto.ret != "XGL_VOID": |
| 105 | stmt = "return " + stmt |
| 106 | |
| 107 | funcs.append("%s\n" |
| 108 | "{\n" |
| 109 | " const struct icd_dispatch_table **disp = (const struct icd_dispatch_table **) %s;\n" |
| 110 | " %s;\n" |
| 111 | "}" % (decl, proto.params[0].name, stmt)) |
| 112 | |
| 113 | return "\n\n".join(funcs) |
| 114 | |
| 115 | def generate_body(self): |
| 116 | body = [self._generate_icd_dispatch_table(), |
| 117 | self._generate_api()] |
| 118 | |
| 119 | return "\n\n".join(body) |
| 120 | |
| 121 | def main(): |
| 122 | subcommands = { |
| 123 | "loader": LoaderSubcommand, |
| 124 | } |
| 125 | |
| 126 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 127 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 128 | print |
| 129 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 130 | exit(1) |
| 131 | |
| 132 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 133 | subcmd.run() |
| 134 | |
| 135 | if __name__ == "__main__": |
| 136 | main() |