1# 2# Copyright (C) 2021 Google, Inc. 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the "Software"), 6# to deal in the Software without restriction, including without limitation 7# the rights to use, copy, modify, merge, publish, distribute, sublicense, 8# and/or sell copies of the Software, and to permit persons to whom the 9# Software is furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice (including the next 12# paragraph) shall be included in all copies or substantial portions of the 13# Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22 23from mako.template import Template 24from xml.etree import ElementTree 25import os 26import sys 27 28def dbg(str): 29 if False: 30 print(str) 31 32cnt = 0 33def cname(name): 34 global cnt 35 cnt = cnt + 1 36 return name + '_' + str(cnt) 37 38class Option(object): 39 def __init__(self, xml): 40 self.cname = cname('option') 41 self.name = xml.attrib['name'] 42 self.value = xml.attrib['value'] 43 44class Application(object): 45 def __init__(self, xml): 46 self.cname = cname('application') 47 self.name = xml.attrib['name'] 48 self.executable = xml.attrib.get('executable', None) 49 self.sha1 = xml.attrib.get('sha1', None) 50 self.application_name_match = xml.attrib.get('application_name_match', None) 51 self.application_versions = xml.attrib.get('application_versions', None) 52 self.options = [] 53 54 for option in xml.findall('option'): 55 self.options.append(Option(option)) 56 57class Engine(object): 58 def __init__(self, xml): 59 self.cname = cname('engine') 60 self.engine_name_match = xml.attrib['engine_name_match'] 61 self.engine_versions = xml.attrib.get('engine_versions', None) 62 self.options = [] 63 64 for option in xml.findall('option'): 65 self.options.append(Option(option)) 66 67class Device(object): 68 def __init__(self, xml): 69 self.cname = cname('device') 70 self.driver = xml.attrib.get('driver', None) 71 self.device = xml.attrib.get('device', None) 72 self.applications = [] 73 self.engines = [] 74 75 for application in xml.findall('application'): 76 self.applications.append(Application(application)) 77 78 for engine in xml.findall('engine'): 79 self.engines.append(Engine(engine)) 80 81class DriConf(object): 82 def __init__(self, xmlpath): 83 self.devices = [] 84 root = ElementTree.parse(xmlpath).getroot() 85 86 for device in root.findall('device'): 87 self.devices.append(Device(device)) 88 89 90template = """\ 91/* Copyright (C) 2021 Google, Inc. 92 * 93 * Permission is hereby granted, free of charge, to any person obtaining a 94 * copy of this software and associated documentation files (the "Software"), 95 * to deal in the Software without restriction, including without limitation 96 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 97 * and/or sell copies of the Software, and to permit persons to whom the 98 * Software is furnished to do so, subject to the following conditions: 99 * 100 * The above copyright notice and this permission notice (including the next 101 * paragraph) shall be included in all copies or substantial portions of the 102 * Software. 103 * 104 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 105 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 106 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 107 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 108 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 109 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 110 * IN THE SOFTWARE. 111 */ 112 113struct driconf_option { 114 const char *name; 115 const char *value; 116}; 117 118struct driconf_application { 119 const char *name; 120 const char *executable; 121 const char *sha1; 122 const char *application_name_match; 123 const char *application_versions; 124 unsigned num_options; 125 const struct driconf_option *options; 126}; 127 128struct driconf_engine { 129 const char *engine_name_match; 130 const char *engine_versions; 131 unsigned num_options; 132 const struct driconf_option *options; 133}; 134 135struct driconf_device { 136 const char *driver; 137 const char *device; 138 unsigned num_engines; 139 const struct driconf_engine *engines; 140 unsigned num_applications; 141 const struct driconf_application *applications; 142}; 143 144<%def name="render_options(cname, options)"> 145static const struct driconf_option ${cname}[] = { 146% for option in options: 147 { .name = "${option.name}", .value = "${option.value}" }, 148% endfor 149}; 150</%def> 151 152%for device in driconf.devices: 153% for engine in device.engines: 154 ${render_options(engine.cname + '_options', engine.options)} 155% endfor 156 157%if len(device.engines) > 0: 158static const struct driconf_engine ${device.cname}_engines[] = { 159% for engine in device.engines: 160 { .engine_name_match = "${engine.engine_name_match}", 161% if engine.engine_versions: 162 .engine_versions = "${engine.engine_versions}", 163% endif 164 .num_options = ${len(engine.options)}, 165 .options = ${engine.cname + '_options'}, 166 }, 167% endfor 168}; 169%endif 170 171% for application in device.applications: 172 ${render_options(application.cname + '_options', application.options)} 173% endfor 174 175%if len(device.applications) > 0: 176static const struct driconf_application ${device.cname}_applications[] = { 177% for application in device.applications: 178 { .name = "${application.name}", 179% if application.executable: 180 .executable = "${application.executable}", 181% endif 182% if application.sha1: 183 .sha1 = "${application.sha1}", 184% endif 185% if application.application_name_match: 186 .application_name_match = "${application.application_name_match}", 187% endif 188% if application.application_versions: 189 .application_versions = "${application.application_versions}", 190% endif 191 .num_options = ${len(application.options)}, 192 .options = ${application.cname + '_options'}, 193 }, 194% endfor 195}; 196%endif 197 198static const struct driconf_device ${device.cname} = { 199% if device.driver: 200 .driver = "${device.driver}", 201% endif 202% if device.device: 203 .device = "${device.device}", 204% endif 205 .num_engines = ${len(device.engines)}, 206% if len(device.engines) > 0: 207 .engines = ${device.cname}_engines, 208% endif 209 .num_applications = ${len(device.applications)}, 210% if len(device.applications) > 0: 211 .applications = ${device.cname}_applications, 212% endif 213}; 214%endfor 215 216static const struct driconf_device *driconf[] = { 217%for device in driconf.devices: 218 &${device.cname}, 219%endfor 220}; 221""" 222 223xml = sys.argv[1] 224dst = sys.argv[2] 225 226with open(dst, 'wb') as f: 227 f.write(Template(template, output_encoding='utf-8').render(driconf=DriConf(xml))) 228 229