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