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