-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
285 lines (194 loc) · 6.57 KB
/
generator.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from wheezy.template.engine import Engine
from wheezy.template.ext.core import CoreExtension
from wheezy.template.loader import FileLoader
from types import MethodType
from sets import Set
import sys
import os
import platform
# set path to franca module before import
sys.path.append(os.getcwd() + "/build/lib."
+ platform.system().lower() + "-"
+ platform.machine() + "-"
+ ".".join(platform.python_version_tuple()[0:2]))
import franca
# extend package
def namespaces_open(self):
rc = ""
p = self;
while not p.is_root() :
rc = "namespace " + p.name() + " {\n" + rc
p = p.package()
return rc
def namespaces_close(self):
rc = ""
p = self;
while not p.is_root() :
rc += "} // namespace " + p.name() + "\n"
p = p.package()
return rc
franca.package.namespaces_open = MethodType(namespaces_open, None, franca.package);
franca.package.namespaces_close = MethodType(namespaces_close, None, franca.package);
def collectIncludes(includes, typecol):
for tc in typecol.dependencies:
includes.append("#include \"" + tc.fqn("/")[1:] + ".hpp\"\n")
collectIncludes(includes, tc)
# extend interface
def dependent_includes(self):
includes = []
dep = self.dependencies
for tc in self.dependencies:
includes.append("#include \"" + tc.fqn("/")[1:] + ".hpp\"\n")
collectIncludes(includes, tc)
# remove doubles
rc = []
[rc.append(x) for x in includes if x not in rc]
ret = ""
for e in rc:
ret += e
return ret
franca.interface.dependent_includes = MethodType(dependent_includes, None, franca.interface);
franca.typecollection.dependent_includes = MethodType(dependent_includes, None, franca.typecollection);
# extend type
intrinsic_types = [ 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Int8', 'Int16', 'Int32', 'Int64', 'Float' ]
intrinsic_types_mapping = {
'UInt8' :'unsigned char',
'UInt16':'unsigned short',
'UInt32':'unsigned int',
'UInt64':'unsigned int64_t',
'Int8' :'signed char',
'Int16' :'short',
'Int32' :'int',
'Int64' :'int64_t',
'Float' :'double'
}
def cpp_type(self) :
if self.name() in intrinsic_types :
return intrinsic_types_mapping[self.name()]
elif self.name() == "String" :
return "std::string"
else :
return self.fqn("::")
franca.type.cpp_type = MethodType(cpp_type, None, franca.type);
def in_signature(self) :
if self.name() in intrinsic_types :
return self.cpp_type()
else :
return "const " + self.cpp_type() + "&"
def out_signature(self) :
return self.cpp_type() + "&"
franca.type.in_signature = MethodType(in_signature, None, franca.type);
franca.type.out_signature = MethodType(out_signature, None, franca.type);
# extend argument list
def for_method_decl_in(self) :
rc = ""
for i in range(0, len(self)) :
rc += self[i].type().in_signature() + " " + self[i].name() + "_in"
if i < len(self)-1 :
rc += ", "
return rc
def for_method_decl_out(self) :
rc = ""
for i in range(0, len(self)) :
rc += self[i].type().out_signature() + " " + self[i].name() + "_out"
if i < len(self)-1 :
rc += ", "
return rc
franca.arg_vector.for_method_decl_in = MethodType(for_method_decl_in, None, franca.arg_vector);
franca.arg_vector.for_method_decl_out = MethodType(for_method_decl_out, None, franca.arg_vector);
def typedef(self) :
if len(self) == 0:
return "void"
if len(self) == 1:
return self[0].type().cpp_type()
rc = "std::tuple<"
for i in range(0, len(self)) :
rc += self[i].type().cpp_type()
if i < len(self)-1 :
rc += ", "
rc += ">"
return rc
franca.arg_vector.typedef = MethodType(typedef, None, franca.arg_vector);
def print_enumerators(self):
rc = ""
i = 0
for e in self.enumerators:
if i > 0:
rc += ",\r\n"
rc += " " + e.name()
i += 1
return rc
franca.enumeration.print_enumerators = MethodType(print_enumerators, None, franca.enumeration);
# ### type collections #################################################
def gen_tc_headers(pack):
for tc in pack.typecollections:
template = engine.get_template('tc_template.hpp')
try:
os.makedirs("." + pack.fqn('/'))
except os.error:
True # ignore
f = open("." + pack.fqn('/') + '/' + tc.name() + '.hpp', 'w')
f.write(template.render({'typecollection': tc}))
f.close()
# recurse
for p in pack.packages:
gen_tc_headers(p)
# ### interfaces #######################################################
def gen_iface_headers(pack):
for iface in pack.interfaces:
template = engine.get_template('if_template.hpp')
try:
os.makedirs("." + pack.fqn('/'))
except os.error:
True # ignore
f = open("." + pack.fqn('/') + '/' + iface.name() + '.hpp', 'w')
f.write(template.render({'interface': iface}))
f.close()
# recurse
for p in pack.packages:
gen_iface_headers(p)
def gen_stub_headers(pack):
for iface in pack.interfaces:
template = engine.get_template('stub_template.hpp')
try:
os.makedirs("." + pack.fqn('/'))
except os.error:
True # ignore
f = open("." + pack.fqn('/') + '/' + iface.name() + 'Stub.hpp', 'w')
f.write(template.render({'interface': iface}))
f.close()
# recurse
for p in pack.packages:
gen_stub_headers(p)
def gen_skeleton_headers(pack):
for iface in pack.interfaces:
template = engine.get_template('skeleton_template.hpp')
try:
os.makedirs("." + pack.fqn('/'))
except os.error:
True # ignore
f = open("." + pack.fqn('/') + '/' + iface.name() + 'Skeleton.hpp', 'w')
f.write(template.render({'interface': iface}))
f.close()
# recurse
for p in pack.packages:
gen_skeleton_headers(p)
# ### START of main program ############################################
# load template
searchpath = ['.']
engine = Engine(
loader=FileLoader(searchpath),
extensions=[CoreExtension()]
)
engine.global_vars.update({
'franca': franca,
})
template = engine.get_template('client_template.hpp')
# load franca
root = franca.package()
franca.builder.parse_and_build(root, "printengine.fidl")
# generate output
gen_tc_headers(root)
gen_iface_headers(root)
gen_stub_headers(root)
gen_skeleton_headers(root)