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