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