generate_registry_tables.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python
  2. # Copyright (c) 2016 Google Inc.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Generates the vendor tool table from the SPIR-V XML registry."""
  15. from __future__ import print_function
  16. import distutils.dir_util
  17. import os.path
  18. import xml.etree.ElementTree
  19. def generate_vendor_table(registry):
  20. """Returns a list of C style initializers for the registered vendors
  21. and their tools.
  22. Args:
  23. registry: The SPIR-V XMLregistry as an xml.ElementTree
  24. """
  25. lines = []
  26. for ids in registry.iter('ids'):
  27. if 'vendor' == ids.attrib['type']:
  28. for an_id in ids.iter('id'):
  29. value = an_id.attrib['value']
  30. vendor = an_id.attrib['vendor']
  31. if 'tool' in an_id.attrib:
  32. tool = an_id.attrib['tool']
  33. vendor_tool = vendor + ' ' + tool
  34. else:
  35. tool = ''
  36. vendor_tool = vendor
  37. line = '{' + '{}, "{}", "{}", "{}"'.format(value,
  38. vendor,
  39. tool,
  40. vendor_tool) + '},'
  41. lines.append(line)
  42. return '\n'.join(lines)
  43. def main():
  44. import argparse
  45. parser = argparse.ArgumentParser(description=
  46. 'Generate tables from SPIR-V XML registry')
  47. parser.add_argument('--xml', metavar='<path>',
  48. type=str, required=True,
  49. help='SPIR-V XML Registry file')
  50. parser.add_argument('--generator-output', metavar='<path>',
  51. type=str, required=True,
  52. help='output file for SPIR-V generators table')
  53. args = parser.parse_args()
  54. with open(args.xml) as xml_in:
  55. registry = xml.etree.ElementTree.fromstring(xml_in.read())
  56. distutils.dir_util.mkpath(os.path.dirname(args.generator_output))
  57. print(generate_vendor_table(registry), file=open(args.generator_output, 'w'))
  58. if __name__ == '__main__':
  59. main()