José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ########################################################################## |
| 3 | # |
| 4 | # Copyright 2014 VMware, Inc |
| 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the "Software"), to deal |
| 9 | # in the Software without restriction, including without limitation the rights |
| 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | # copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # 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 THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | # THE SOFTWARE. |
| 24 | # |
| 25 | ##########################################################################/ |
| 26 | |
| 27 | |
| 28 | # |
| 29 | # Script to half-generate *api.py based on Khronos' *.xml |
| 30 | # |
| 31 | |
| 32 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 33 | import optparse |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 34 | import sys |
| 35 | import xml.etree.ElementTree as ET |
| 36 | |
| 37 | import c2api |
| 38 | |
| 39 | |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 40 | def appendToken(tokens, text): |
| 41 | for token in text.split(): |
| 42 | if token.startswith('*'): |
| 43 | for c in token: |
| 44 | tokens.append(c) |
| 45 | else: |
| 46 | tokens.append(token) |
| 47 | |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 48 | |
| 49 | def getType(node): |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 50 | tokens = [] |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 51 | |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 52 | if node.text is not None: |
| 53 | appendToken(tokens, node.text) |
| 54 | |
| 55 | ptype = node.find('ptype') |
| 56 | if ptype is not None: |
| 57 | appendToken(tokens, ptype.text) |
| 58 | appendToken(tokens, ptype.tail) |
| 59 | |
| 60 | # Array |
| 61 | lenExpr = node.get('len') |
| 62 | if lenExpr is not None: |
| 63 | assert tokens[-1] == '*' |
| 64 | tokens = tokens[:-1] |
| 65 | if lenExpr == "COMPSIZE(pname)": |
| 66 | lenExpr = "_gl_param_size(pname)" |
| 67 | |
| 68 | typeText = ' '.join(tokens) |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 69 | parser = c2api.DeclParser() |
| 70 | parser.tokenize(typeText + ';') |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 71 | typeExpr = parser.parse_type() |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 72 | |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 73 | if lenExpr is not None: |
José Fonseca | 9b1b942 | 2014-12-25 16:21:43 +0000 | [diff] [blame^] | 74 | if lenExpr == "1": |
| 75 | typeExpr = 'Pointer(%s)' % (typeExpr) |
| 76 | else: |
| 77 | if not lenExpr.isdigit(): |
| 78 | lenExpr = '"' + lenExpr + '"' |
| 79 | typeExpr = 'Array(%s, %s)' % (typeExpr, lenExpr) |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 80 | |
| 81 | return typeExpr |
| 82 | |
| 83 | |
| 84 | def processCommand(prototypes, command): |
| 85 | proto = command.find('proto') |
| 86 | |
| 87 | functionName = proto.find('name').text |
| 88 | |
| 89 | retType = getType(proto) |
| 90 | |
| 91 | args = [] |
| 92 | for param in command.findall('param'): |
| 93 | argName = param.find('name').text |
| 94 | #print argName, param.text |
| 95 | argType = getType(param) |
| 96 | if argName.lower() == 'hdc': |
| 97 | argName = 'hDC' |
| 98 | arg = '(%s, "%s")' % (argType, argName) |
| 99 | args.append(arg) |
| 100 | |
| 101 | if namespace == 'WGL': |
| 102 | constructor = 'StdFunction' |
| 103 | else: |
| 104 | constructor = 'GlFunction' |
| 105 | |
| 106 | prototype = '%s(%s, "%s", [%s])' % (constructor, retType, functionName, ', '.join(args)) |
| 107 | |
| 108 | prototypes[functionName] = prototype |
| 109 | |
| 110 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 111 | def processRequire(prototypes, node, filterName): |
| 112 | nodeName = node.get('name') |
| 113 | if filterName is not None and nodeName != filterName: |
| 114 | return |
| 115 | |
José Fonseca | 1449075 | 2014-08-13 18:41:14 +0100 | [diff] [blame] | 116 | commands = [] |
| 117 | for requireNode in node.findall('require'): |
| 118 | commands.extend(requireNode.findall('command')) |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 119 | if not commands: |
| 120 | return |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 121 | print ' # %s' % nodeName |
José Fonseca | 4563c46 | 2014-05-30 15:57:36 +0100 | [diff] [blame] | 122 | for command in commands: |
| 123 | functionName = command.get('name') |
| 124 | prototype = prototypes[functionName] |
| 125 | print ' %s,' % prototype |
| 126 | print |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 127 | |
| 128 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 129 | def main(): |
| 130 | optparser = optparse.OptionParser( |
| 131 | usage='\n\t%prog [options] <xml> ...', |
| 132 | version='%%prog') |
| 133 | optparser.add_option( |
| 134 | '--filter', metavar='NAME', |
| 135 | type='string', dest='filter', |
| 136 | help='filter feature/extension') |
| 137 | (options, args) = optparser.parse_args(sys.argv[1:]) |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 138 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 139 | global prototypes |
| 140 | global namespace |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 141 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 142 | for arg in args: |
| 143 | tree = ET.parse(arg) |
| 144 | root = tree.getroot() |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 145 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 146 | prototypes = {} |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 147 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 148 | for commands in root.findall('commands'): |
| 149 | namespace = commands.get('namespace') |
| 150 | for command in commands.findall('command'): |
| 151 | processCommand(prototypes, command) |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 152 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 153 | for feature in root.findall('feature'): |
| 154 | processRequire(prototypes, feature, options.filter) |
| 155 | |
| 156 | extensions = root.find('extensions') |
| 157 | for extension in extensions.findall('extension'): |
| 158 | processRequire(prototypes, extension, options.filter) |
| 159 | |
| 160 | |
| 161 | if __name__ == '__main__': |
| 162 | main() |