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