xref: /aosp_15_r20/external/swiftshader/third_party/SPIRV-Tools/utils/generate_registry_tables.py (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1#!/usr/bin/env python3
2# Copyright (c) 2016 Google Inc.
3
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Generates the vendor tool table from the SPIR-V XML registry."""
16
17import errno
18import io
19import os.path
20from xml.etree.ElementTree import XML, XMLParser, TreeBuilder
21
22
23def mkdir_p(directory):
24    """Make the directory, and all its ancestors as required.  Any of the
25    directories are allowed to already exist.
26    This is compatible with Python down to 3.0.
27    """
28
29    if directory == "":
30        # We're being asked to make the current directory.
31        return
32
33    try:
34        os.makedirs(directory)
35    except OSError as e:
36        if e.errno == errno.EEXIST and os.path.isdir(directory):
37            pass
38        else:
39            raise
40
41
42def generate_vendor_table(registry):
43    """Returns a list of C style initializers for the registered vendors
44    and their tools.
45
46    Args:
47      registry: The SPIR-V XMLregistry as an xml.ElementTree
48    """
49
50    lines = []
51    for ids in registry.iter('ids'):
52        if 'vendor' == ids.attrib['type']:
53            for an_id in ids.iter('id'):
54                value = an_id.attrib['value']
55                vendor = an_id.attrib['vendor']
56                if 'tool' in an_id.attrib:
57                    tool = an_id.attrib['tool']
58                    vendor_tool = vendor + ' ' + tool
59                else:
60                    tool = ''
61                    vendor_tool = vendor
62                line = '{' + '{}, "{}", "{}", "{}"'.format(value,
63                                                           vendor,
64                                                           tool,
65                                                           vendor_tool) + '},'
66                lines.append(line)
67    return '\n'.join(lines)
68
69
70def main():
71    import argparse
72    parser = argparse.ArgumentParser(description=
73                                     'Generate tables from SPIR-V XML registry')
74    parser.add_argument('--xml', metavar='<path>',
75                        type=str, required=True,
76                        help='SPIR-V XML Registry file')
77    parser.add_argument('--generator-output', metavar='<path>',
78                        type=str, required=True,
79                        help='output file for SPIR-V generators table')
80    args = parser.parse_args()
81
82    with io.open(args.xml, encoding='utf-8') as xml_in:
83      parser = XMLParser(target=TreeBuilder(), encoding='utf-8')
84      registry = XML(xml_in.read(), parser=parser)
85
86    mkdir_p(os.path.dirname(args.generator_output))
87    with open(args.generator_output, 'w') as f:
88      f.write(generate_vendor_table(registry))
89
90
91if __name__ == '__main__':
92    main()
93