Path: blob/21.2-virgl/src/util/driconf_static.py
4550 views
#1# Copyright (C) 2021 Google, Inc.2#3# Permission is hereby granted, free of charge, to any person obtaining a4# copy of this software and associated documentation files (the "Software"),5# to deal in the Software without restriction, including without limitation6# the rights to use, copy, modify, merge, publish, distribute, sublicense,7# and/or sell copies of the Software, and to permit persons to whom the8# Software is furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice (including the next11# paragraph) shall be included in all copies or substantial portions of the12# Software.13#14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20# IN THE SOFTWARE.2122from mako.template import Template23from xml.etree import ElementTree24import os25import sys2627def dbg(str):28if False:29print(str)3031cnt = 032def cname(name):33global cnt34cnt = cnt + 135return name + '_' + str(cnt)3637class Option(object):38def __init__(self, xml):39self.cname = cname('option')40self.name = xml.attrib['name']41self.value = xml.attrib['value']4243class Application(object):44def __init__(self, xml):45self.cname = cname('application')46self.name = xml.attrib['name']47self.executable = xml.attrib.get('executable', None)48self.sha1 = xml.attrib.get('sha1', None)49self.application_name_match = xml.attrib.get('application_name_match', None)50self.application_versions = xml.attrib.get('application_versions', None)51self.options = []5253for option in xml.findall('option'):54self.options.append(Option(option))5556class Engine(object):57def __init__(self, xml):58self.cname = cname('engine')59self.engine_name_match = xml.attrib['engine_name_match']60self.engine_versions = xml.attrib.get('engine_versions', None)61self.options = []6263for option in xml.findall('option'):64self.options.append(Option(option))6566class Device(object):67def __init__(self, xml):68self.cname = cname('device')69self.driver = xml.attrib.get('driver', None)70self.applications = []71self.engines = []7273for application in xml.findall('application'):74self.applications.append(Application(application))7576for engine in xml.findall('engine'):77self.engines.append(Engine(engine))7879class DriConf(object):80def __init__(self, xmlpath):81self.devices = []82root = ElementTree.parse(xmlpath).getroot()8384for device in root.findall('device'):85self.devices.append(Device(device))868788template = """\89/* Copyright (C) 2021 Google, Inc.90*91* Permission is hereby granted, free of charge, to any person obtaining a92* copy of this software and associated documentation files (the "Software"),93* to deal in the Software without restriction, including without limitation94* the rights to use, copy, modify, merge, publish, distribute, sublicense,95* and/or sell copies of the Software, and to permit persons to whom the96* Software is furnished to do so, subject to the following conditions:97*98* The above copyright notice and this permission notice (including the next99* paragraph) shall be included in all copies or substantial portions of the100* Software.101*102* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR103* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,104* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL105* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER106* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING107* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS108* IN THE SOFTWARE.109*/110111struct driconf_option {112const char *name;113const char *value;114};115116struct driconf_application {117const char *name;118const char *executable;119const char *sha1;120const char *application_name_match;121const char *application_versions;122unsigned num_options;123const struct driconf_option *options;124};125126struct driconf_engine {127const char *engine_name_match;128const char *engine_versions;129unsigned num_options;130const struct driconf_option *options;131};132133struct driconf_device {134const char *driver;135unsigned num_engines;136const struct driconf_engine *engines;137unsigned num_applications;138const struct driconf_application *applications;139};140141<%def name="render_options(cname, options)">142static const struct driconf_option ${cname}[] = {143% for option in options:144{ .name = "${option.name}", .value = "${option.value}" },145% endfor146};147</%def>148149%for device in driconf.devices:150% for engine in device.engines:151${render_options(engine.cname + '_options', engine.options)}152% endfor153154%if len(device.engines) > 0:155static const struct driconf_engine ${device.cname}_engines[] = {156% for engine in device.engines:157{ .engine_name_match = "${engine.engine_name_match}",158% if engine.engine_versions:159.engine_versions = "${engine.engine_versions}",160% endif161.num_options = ${len(engine.options)},162.options = ${engine.cname + '_options'},163},164% endfor165};166%endif167168% for application in device.applications:169${render_options(application.cname + '_options', application.options)}170% endfor171172%if len(device.applications) > 0:173static const struct driconf_application ${device.cname}_applications[] = {174% for application in device.applications:175{ .name = "${application.name}",176% if application.executable:177.executable = "${application.executable}",178% endif179% if application.sha1:180.sha1 = "${application.sha1}",181% endif182% if application.application_name_match:183.application_name_match = "${application.application_name_match}",184% endif185% if application.application_versions:186.application_versions = "${application.application_versions}",187% endif188.num_options = ${len(application.options)},189.options = ${application.cname + '_options'},190},191% endfor192};193%endif194195static const struct driconf_device ${device.cname} = {196% if device.driver:197.driver = "${device.driver}",198% endif199.num_engines = ${len(device.engines)},200% if len(device.engines) > 0:201.engines = ${device.cname}_engines,202% endif203.num_applications = ${len(device.applications)},204% if len(device.applications) > 0:205.applications = ${device.cname}_applications,206% endif207};208%endfor209210static const struct driconf_device *driconf[] = {211%for device in driconf.devices:212&${device.cname},213%endfor214};215"""216217xml = sys.argv[1]218dst = sys.argv[2]219220with open(dst, 'wb') as f:221f.write(Template(template, output_encoding='utf-8').render(driconf=DriConf(xml)))222223224225