Jose Fonseca | 5b3c68c | 2019-04-28 14:13:01 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 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 | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 111 | def processRequire(node, filterName): |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 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 | fb19526 | 2014-12-25 16:23:19 +0000 | [diff] [blame] | 121 | |
| 122 | functionNames = [command.get('name') for command in commands] |
| 123 | |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 124 | return nodeName, functionNames |
| 125 | |
| 126 | |
| 127 | |
Jose Fonseca | 5c6ca06 | 2015-01-24 09:39:55 +0000 | [diff] [blame] | 128 | def printPrototypes(prototypes, extensionName, functionNames, skip=set()): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 129 | print(' # %s' % extensionName) |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 130 | |
| 131 | if extensionName == 'GL_EXT_direct_state_access': |
José Fonseca | fb19526 | 2014-12-25 16:23:19 +0000 | [diff] [blame] | 132 | functionNames.sort() |
| 133 | |
| 134 | for functionName in functionNames: |
Jose Fonseca | 5c6ca06 | 2015-01-24 09:39:55 +0000 | [diff] [blame] | 135 | if functionName not in skip: |
| 136 | prototype = prototypes[functionName] |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 137 | print(' %s,' % prototype) |
José Fonseca | fb19526 | 2014-12-25 16:23:19 +0000 | [diff] [blame] | 138 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 139 | print() |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 140 | |
| 141 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 142 | def main(): |
| 143 | optparser = optparse.OptionParser( |
| 144 | usage='\n\t%prog [options] <xml> ...', |
| 145 | version='%%prog') |
| 146 | optparser.add_option( |
| 147 | '--filter', metavar='NAME', |
| 148 | type='string', dest='filter', |
| 149 | help='filter feature/extension') |
| 150 | (options, args) = optparser.parse_args(sys.argv[1:]) |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 151 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 152 | global prototypes |
| 153 | global namespace |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 154 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 155 | for arg in args: |
| 156 | tree = ET.parse(arg) |
| 157 | root = tree.getroot() |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 158 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 159 | prototypes = {} |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 160 | |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 161 | for commands in root.findall('commands'): |
| 162 | namespace = commands.get('namespace') |
| 163 | for command in commands.findall('command'): |
| 164 | processCommand(prototypes, command) |
José Fonseca | 8e0d104 | 2014-05-27 20:02:34 +0100 | [diff] [blame] | 165 | |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 166 | # Extract features |
| 167 | features = [] |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 168 | for feature in root.findall('feature'): |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 169 | ret = processRequire(feature, options.filter) |
| 170 | if ret is not None: |
| 171 | features.append(ret) |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 172 | |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 173 | # Extract extensions |
| 174 | extensions = [] |
| 175 | for extension in root.find('extensions').findall('extension'): |
| 176 | ret = processRequire(extension, options.filter) |
| 177 | if ret is not None: |
| 178 | extensions.append(ret) |
| 179 | |
| 180 | # Eliminate the functions from features that are in extensions |
| 181 | for extensionName, extensionFunctionNames in extensions: |
| 182 | for featureName, featureFunctionNames in features: |
| 183 | for functionName in extensionFunctionNames: |
| 184 | try: |
| 185 | featureFunctionNames.remove(functionName) |
| 186 | except ValueError: |
| 187 | pass |
| 188 | |
| 189 | # Print all |
Jose Fonseca | 5c6ca06 | 2015-01-24 09:39:55 +0000 | [diff] [blame] | 190 | skip = set() |
José Fonseca | 251aa36 | 2014-12-31 19:37:44 +0000 | [diff] [blame] | 191 | for extensionName, functionNames in features + extensions: |
Jose Fonseca | 5c6ca06 | 2015-01-24 09:39:55 +0000 | [diff] [blame] | 192 | printPrototypes(prototypes, extensionName, functionNames, skip) |
| 193 | skip.update(functionNames) |
José Fonseca | 0296e3a | 2014-08-13 19:00:31 +0100 | [diff] [blame] | 194 | |
| 195 | |
| 196 | if __name__ == '__main__': |
| 197 | main() |