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