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