1#!/usr/bin/env python
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
17from __future__ import print_function
18
19import distutils.dir_util
20import os.path
21import xml.etree.ElementTree
22
23
24def generate_vendor_table(registry):
25    """Returns a list of C style initializers for the registered vendors
26    and their tools.
27
28    Args:
29      registry: The SPIR-V XMLregistry as an xml.ElementTree
30    """
31
32    lines = []
33    for ids in registry.iter('ids'):
34        if 'vendor' == ids.attrib['type']:
35            for an_id in ids.iter('id'):
36                value = an_id.attrib['value']
37                vendor = an_id.attrib['vendor']
38                if 'tool' in an_id.attrib:
39                    tool = an_id.attrib['tool']
40                    vendor_tool = vendor + ' ' + tool
41                else:
42                    tool = ''
43                    vendor_tool = vendor
44                line = '{' + '{}, "{}", "{}", "{}"'.format(value,
45                                                           vendor,
46                                                           tool,
47                                                           vendor_tool) + '},'
48                lines.append(line)
49    return '\n'.join(lines)
50
51
52def main():
53    import argparse
54    parser = argparse.ArgumentParser(description=
55                                     'Generate tables from SPIR-V XML registry')
56    parser.add_argument('--xml', metavar='<path>',
57                        type=str, required=True,
58                        help='SPIR-V XML Registry file')
59    parser.add_argument('--generator-output', metavar='<path>',
60                        type=str, required=True,
61                        help='output file for SPIR-V generators table')
62    args = parser.parse_args()
63
64    with open(args.xml) as xml_in:
65       registry = xml.etree.ElementTree.fromstring(xml_in.read())
66
67    distutils.dir_util.mkpath(os.path.dirname(args.generator_output))
68    print(generate_vendor_table(registry), file=open(args.generator_output, 'w'))
69
70
71if __name__ == '__main__':
72    main()
73