generate_registry_tables.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  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. import errno
  16. import io
  17. import os.path
  18. import platform
  19. from xml.etree.ElementTree import XML, XMLParser, TreeBuilder
  20. def mkdir_p(directory):
  21. """Make the directory, and all its ancestors as required. Any of the
  22. directories are allowed to already exist.
  23. This is compatible with Python down to 3.0.
  24. """
  25. if directory == "":
  26. # We're being asked to make the current directory.
  27. return
  28. try:
  29. os.makedirs(directory)
  30. except OSError as e:
  31. if e.errno == errno.EEXIST and os.path.isdir(directory):
  32. pass
  33. else:
  34. raise
  35. def generate_vendor_table(registry):
  36. """Returns a list of C style initializers for the registered vendors
  37. and their tools.
  38. Args:
  39. registry: The SPIR-V XMLregistry as an xml.ElementTree
  40. """
  41. lines = []
  42. for ids in registry.iter('ids'):
  43. if 'vendor' == ids.attrib['type']:
  44. for an_id in ids.iter('id'):
  45. value = an_id.attrib['value']
  46. vendor = an_id.attrib['vendor']
  47. if 'tool' in an_id.attrib:
  48. tool = an_id.attrib['tool']
  49. vendor_tool = vendor + ' ' + tool
  50. else:
  51. tool = ''
  52. vendor_tool = vendor
  53. line = '{' + '{}, "{}", "{}", "{}"'.format(value,
  54. vendor,
  55. tool,
  56. vendor_tool) + '},'
  57. lines.append(line)
  58. return '\n'.join(lines)
  59. def main():
  60. import argparse
  61. parser = argparse.ArgumentParser(description=
  62. 'Generate tables from SPIR-V XML registry')
  63. parser.add_argument('--xml', metavar='<path>',
  64. type=str, required=True,
  65. help='SPIR-V XML Registry file')
  66. parser.add_argument('--generator-output', metavar='<path>',
  67. type=str, required=True,
  68. help='output file for SPIR-V generators table')
  69. args = parser.parse_args()
  70. with io.open(args.xml, encoding='utf-8') as xml_in:
  71. # Python3 default str to UTF-8. But Python2.7 (in case of NDK build,
  72. # don't be fooled by the shebang) is returning a unicode string.
  73. # So depending of the version, we need to make sure the correct
  74. # encoding is used.
  75. content = xml_in.read()
  76. if platform.python_version_tuple()[0] == '2':
  77. content = content.encode('utf-8')
  78. parser = XMLParser(target=TreeBuilder(), encoding='utf-8')
  79. registry = XML(content, parser=parser)
  80. mkdir_p(os.path.dirname(args.generator_output))
  81. with open(args.generator_output, 'w') as f:
  82. f.write(generate_vendor_table(registry))
  83. if __name__ == '__main__':
  84. main()