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 | |
| 33 | import sys |
| 34 | import xml.etree.ElementTree as ET |
| 35 | |
| 36 | import c2api |
| 37 | |
| 38 | |
| 39 | |
| 40 | def getType(node): |
| 41 | ptype = node.find('ptype') |
| 42 | if ptype is None: |
| 43 | typeText = node.text.strip() |
| 44 | else: |
| 45 | typeText = ptype.text |
| 46 | |
| 47 | parser = c2api.DeclParser() |
| 48 | parser.tokenize(typeText + ';') |
| 49 | |
| 50 | return parser.parse_type() |
| 51 | |
| 52 | |
| 53 | for arg in sys.argv[1:]: |
| 54 | tree = ET.parse(arg) |
| 55 | root = tree.getroot() |
| 56 | |
| 57 | prototypes = {} |
| 58 | |
| 59 | for commands in root.findall('commands'): |
| 60 | namespace = commands.get('namespace') |
| 61 | for command in commands.findall('command'): |
| 62 | proto = command.find('proto') |
| 63 | retType = getType(proto) |
| 64 | functionName = proto.find('name').text |
| 65 | |
| 66 | args = [] |
| 67 | for param in command.findall('param'): |
| 68 | argType = getType(param) |
| 69 | argName = param.find('name').text |
| 70 | if argName.lower() == 'hdc': |
| 71 | argName = 'hDC' |
| 72 | arg = '(%s, "%s")' % (argType, argName) |
| 73 | args.append(arg) |
| 74 | |
| 75 | if namespace == 'WGL': |
| 76 | constructor = 'StdFunction' |
| 77 | else: |
| 78 | constructor = 'GlFunction' |
| 79 | |
| 80 | prototype = '%s(%s, "%s", [%s])' % (constructor, retType, functionName, ', '.join(args)) |
| 81 | prototypes[functionName] = prototype |
| 82 | |
| 83 | for feature in root.findall('feature'): |
| 84 | print ' # %s' % feature.get('name') |
| 85 | require = feature.find('require') |
| 86 | for command in require.findall('command'): |
| 87 | functionName = command.get('name') |
| 88 | prototype = prototypes[functionName] |
| 89 | print ' %s,' % prototype |
| 90 | print |
| 91 | |
| 92 | extensions = root.find('extensions') |
| 93 | for extension in extensions.findall('extension'): |
| 94 | print ' # %s' % extension.get('name') |
| 95 | require = extension.find('require') |
| 96 | for command in require.findall('command'): |
| 97 | functionName = command.get('name') |
| 98 | prototype = prototypes[functionName] |
| 99 | print ' %s,' % prototype |
| 100 | print |
| 101 | |