-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_rtk.py
401 lines (389 loc) · 13 KB
/
gen_rtk.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import pycparser
from pycparser import parse_file
import re
import copy
def remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/') or s.startswith('#'):
return " " # note: a space and not an empty string
else:
return s
pattern = re.compile(
r'#.*?$|//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
def getDefine(txt,DEFINE):
pat = re.compile(r"\\\n",re.DOTALL)
src = re.sub(pat,"",txt)
pat = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"')
src = re.sub(pat,"",src)
pat = re.compile(" +",re.DOTALL)
src = re.sub(pat," ",src)
srcs = src.split('\n')
defs = []
i = 0
while i < len(srcs):
if srcs[i].find("#define") != -1 and srcs[i].find("thread") == -1:
j = srcs[i].strip().split(' ')
if len(j) > 2:
defs.append([j[1],j[2]])
i += 1
elif srcs[i].find("#ifdef") != -1:
j = srcs[i].strip().split(' ')
k = 0
while srcs[i+k].find("#endif") == -1:
k += 1
l = 0
while srcs[i+l].find("#else") == -1 and l < k:
l += 1
if l != k:
if j[1] in DEFINE:
start = 0
end = l
else:
start = l + 1
end = k
else:
start = 0
end = k
for m in range(end-start):
if srcs[i+start+m].find("#define") != -1 and srcs[i+start+m].find("thread") == -1:
n = srcs[i+start+m].strip().split(' ')
if len(n) > 2:
defs.append([n[1],n[2]])
i += k
else:
i += 1
return defs
#gen the bind file
header = '''#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "rtksrc/rtklib.h"
#include "iostream"
#include "cbind.h"
namespace py = pybind11;
std::map<void*,void*> gmap;
'''
footer = '''
}
'''
#bindTemp = ' BINDARR1D(%s);\n BINDARR2D(%s);\n'
bindTemp = ' bindArr1D<%s>(m,"%s");\n bindArr2D<%s>(m,"%s");\n'
structTemp = ' py::class_<%s>(m,"%s").def(py::init())\n'
structMemTemp = ' .def_readwrite("%s",&%s::%s)\n'
structProperTemp = ''' .def_property_readonly("%s",[](%s& o) {%s* tmp = new %s(%s);return tmp;},py::return_value_policy::reference)\n'''
# structProperTemp = ''' .def_property_readonly("%s",[](py::object& obj) {
# %s& o = obj.cast<%s&>();
# %s* retv;
# std::map<void*,void*>::iterator iter;
# iter = gmap.find((void*)&o);
# if(iter==gmap.end()){
# %s* tmp = new %s(%s);
# auto ret = gmap.insert(std::pair<void*,void*>((void*)&o,(void*)tmp));
# retv = (%s*)ret.first->second;
# }else{
# retv = (%s*)iter->second;
# }
# return retv;
# },py::return_value_policy::reference)\n'''
attrArrTemp = ' m.attr("%s") = new %s<%s>(%s);\n'
attrTemp = ' m.attr("%s") = new %s_t(%s);\n'
funcTemp = ' m.def("%s",&%s,"rtklib %s");\n'
def gen_struct(struct):
name = struct['name']
temp = structTemp%(name,name)
for i in struct['member']:
temp += structMemTemp%(i,name,i)
for i in struct['proper']:
dim = []
if len(i['dims']) == 1:
if i['dims'][0] == '*':
dim.append('-1')
ttype = 'Arr1D'+'<'+i['type']+'>'
temp += ''' .def_property("%s",[](%s& o) {%s* tmp = new %s(%s);return tmp;},[](%s& o,Arr1D<%s>arr){o.%s=arr.src;},py::return_value_policy::reference)\n'''%(i['name'],name,ttype,ttype,"o."+i['name']+',-1',name,i['type'],i['name'])
else:
if i['dims'][0] == '':
dim.append('-1')
else:
dim.append(i['dims'][0])
ttype = 'Arr1D'+'<'+i['type']+'>'
temp += structProperTemp%(i['name'],name,ttype,ttype,"o."+i['name']+','+dim[0])
else:
if i['dims'][0] == '':
dim.append('-1')
else:
dim.append(i['dims'][0])
if i['dims'][1] == '':
dim.append('-1')
else:
dim.append(i['dims'][1])
ttype = 'Arr2D'+'<'+i['type']+'>'
temp += structProperTemp%(i['name'],name,ttype,ttype,"o."+i['name']+','+dim[0]+','+dim[1])
temp+=' .def_property_readonly("ptr",[](%s& o){return &o;},py::return_value_policy::reference);\n\n'%(name)
return temp
def gen_attrarr(i):
dim = []
temp = ''
if len(i['dims']) == 1:
if i['dims'][0] == '':
dim.append('-1')
else:
dim.append(i['dims'][0])
temp += attrArrTemp%(i['name'],'Arr1D',i['type'],"(void*)"+i['name']+','+dim[0])
else:
if i['dims'][0] == '':
dim.append('-1')
else:
dim.append(i['dims'][0])
if i['dims'][1] == '':
dim.append('-1')
else:
dim.append(i['dims'][1])
temp += attrArrTemp%(i['name'],'Arr2D',i['type'],"(void*)"+i['name']+','+dim[0]+','+dim[1])
return temp
def gen_attr(attr):
return attrTemp%(attr,attr.replace("_default",""),attr)
def gen_func(func):
return funcTemp%(func,func,func)
def gen_multidef(funcs):
tmp = ""
i = funcs
tmp += i['multidef'].rstrip('; ')+"{\n"
for j in i['dconvert']:
if j[0] == "char":
tmp += " %s **%s = convertChar(D%s);\n"%(j[0],j[1],j[1])
else:
tmp += " %s **%s = convertType(D%s);\n"%(j[0],j[1],j[1])
for j in i['sconvert']:
tmp += " %s *%s = S%s.src;\n"%(j[0],j[1],j[1])
if i['fconvert']:
tmp += ' FILE *fp = Ffp.file;\n'
params = re.findall("\(.*\)",i['src'])[0].lstrip('(').rstrip(')')
params = params.split(',')
p = []
for k in params:
p.append(k.split(' ')[-1].strip('*'))
params = ', '.join(p)
if i['type'] != 'void':
tmp += " auto tmp = %s(%s);\n"%(i['name'],params)
for j in i['dconvert']:
tmp += ' free(%s);\n'%j[1]
if i['fconvert']:
#tmp += ' fclose(fp);\n'
pass
tmp += " return tmp;\n}\n"
else:
tmp += " %s(%s);\n"%(i['name'],params)
for j in i['dconvert']:
tmp += ' free(%s);\n'%j[1]
if i['fconvert']:
#tmp += ' fclose(fp);\n'
pass
tmp += "\n}\n"
return tmp
def gen_functionPointer(funcs):
tmp = ""
i = funcs
params = re.findall("\(.*\)",i['multidef'])[0].lstrip('(').rstrip(')')
tmp += ' m.def("%s",static_cast<%s(*)(%s)>(&%s),"rtklib %s");\n'%(i['name'],i['type'],params,i['name'],i['name'])
return tmp
def gen_defs(defs):
tmp = ""
for i in defs:
tmp+=' m.attr("%s")=%s;\n'%(i[0],i[1])
return tmp
DEFINE = ['ENACMP','ENAGAL','ENAGLO','ENAQZS','ENAIRN']
#parse the header
with open('pyrtklib5/rtksrc/rtklib.h','r') as f:
src = f.read()
defines = getDefine(src,DEFINE)
defines.append(['VER_RTKLIB','"demo5"'])
defines.append(['PATCH_LEVEL','"b34h"'])
src = remover(src)
rec = re.compile('extern "C" {(.*)}',re.DOTALL)
src = rec.findall(src)[0]
src = src.replace('"Copyright (C) 2007-2020 T.Takasu\\n',"")
src = src.replace('All rights reserved."',"")
src = src.replace('__attribute__ ((aligned (8)))',"")
src = src.replace('EXPORT void add_fatal(fatalfunc_t *func);',"")
src = src.replace('typedef void fatalfunc_t(const char *);',"")
src = src.replace('EXPORT','extern')
src = "typedef long time_t;\ntypedef long lock_t;\ntypedef long thread_t;\ntypedef long FILE;\ntypedef unsigned char uint8_t;\ntypedef unsigned short int uint16_t;\ntypedef unsigned int uint32_t;\ntypedef short int int16_t;\ntypedef int int32_t;\n"+src
src = re.sub('\\n+','\\n',re.sub(' \\n','\\n',src).strip())
parser = pycparser.CParser()
ast = parser.parse(src)
#ast = parse_file('rtklib.h',use_cpp=True,cpp_path='gcc',cpp_args=['-E',r'-I./pycparser/utils/fake_libc_include'])
elements = ast.children()
rtk_flag = False
struct = []
attr = []
function = []
functionDouble = []
bind_types = []
srcs = src.split('\n')
attrArr = []
for e in elements:
if e[1].name == "gtime_t":
rtk_flag = True
if rtk_flag:
if type(e[1]) == pycparser.c_ast.Typedef:#export struct
node = {}
node['name'] = e[1].name
node['member'] = []
node['proper'] = []
struct_mem = e[1].children()[0][1].children()[0][1].children()
for i in struct_mem:
if type(i[1].type) != pycparser.c_ast.ArrayDecl:
proper = {}
line = srcs[i[1].coord.line-1]
proper['name'] = i[1].name
proper['type'] = line.replace("const","",).replace('unsigned ','').strip().split(' ')[0]
proper['dims'] = []
if line.find('*') != -1:
proper['dims'].append('*')
node['proper'].append(proper)
else:
node['member'].append(i[1].name)
else:
proper = {}
line = srcs[i[1].coord.line-1]
proper['name'] = i[1].name
proper['type'] = line[:i[1].coord.column-1].strip()
proper['dims'] = []
if proper['type'].find(',')!= -1:
tmpType = proper['type']
tmpType = ' '.join(tmpType.split(' ')[:-1])
proper['type'] = tmpType
if proper['type'].find('*')!= -1:
proper['type'] = proper['type'].replace('*','').strip()
proper['dims'].append('')
bind_types.append(proper['type'].replace('const ','').replace('unsigned ',''))
dims = re.findall('\[.*?\]',line[i[1].coord.column-1:])
for j in dims:
k = j.rstrip(']').lstrip('[')
proper['dims'].append(k)
node['proper'].append(proper)
struct.append(node)
if type(e[1]) == pycparser.c_ast.Decl:#function or attr
if type(e[1].children()[0][1]) == pycparser.c_ast.FuncDecl:
k = e[1]
if srcs[k.coord.line-1].find(';') != -1:
lll = srcs[k.coord.line-1]
else:
lll = srcs[k.coord.line-1]
iii = k.coord.line
while(srcs[iii].find(';') == -1):
iii+=1
ext = ''.join([re.sub(" +",' ',kkk) for kkk in srcs[k.coord.line:iii+1]])
ext = ext.replace('\n','')
lll = lll + ext
if lll.find("...") != -1 or lll.find("stec_") != -1:
continue
if len(re.findall("\*[a-zA-Z]*?\[\]",lll)) != 0:
tttmp = re.findall("\*[a-zA-Z]*?\[\]",lll)[0]
lll = lll.replace(tttmp,"*"+tttmp[:-2])
if lll.find("*") == -1:
function.append(e[1].name)
else:
S =0
D = 0
F = 0
proper = {}
proper['name'] = e[1].name
proper['multidef'] = ''
proper['dconvert'] = []
proper['sconvert'] = []
proper['fconvert'] = False
proper['type'] = lll[:k.coord.column-1].replace('extern',"").replace('const',"").strip()
params = re.findall("\(.*\)",lll)[0].lstrip('(').rstrip(')')
proper['src'] = copy.deepcopy(lll)
ddp = params.split(',')
for k in ddp:
if k.find("FILE") != -1:
lll = lll.replace("FILE *fp","FileWrapper &Ffp")
proper['fconvert'] = True
F += 1
continue
if k.find('**') != -1:
ttt = k.strip().split(' ')[0]
nnn = k.strip().split(' ')[1].strip('*')
proper['dconvert'].append((ttt,nnn))
if ttt == 'char':
lll = lll.replace(k,'std::vector<std::string> '+"D"+nnn)
else:
lll = lll.replace(k,'std::vector<std::vector<%s>> '%ttt+"D"+nnn)
D += 1
continue
if k.find('*') != -1:
if k.find('const char') != -1:
continue
nnn = k.strip().split('*')[-1].strip()
ttt = k.strip().split('*')[0].strip()
# ttt = k.strip().split(' ')[0]
# nnn = k.strip().split(' ')[1].strip('*')
if ttt.find('double') != -1 or ttt.find('float') != -1 or ttt.find('char') != -1 or ttt.find('int') != -1:
proper['sconvert'].append((ttt,nnn))
lll = lll.replace(k,'Arr1D<%s> '%ttt.replace("const","").strip()+"S"+nnn)
S += 1
continue
if S == 0 and D == 0 and F == 0:
function.append(e[1].name)
else:
proper['multidef'] = lll
functionDouble.append(proper)
elif type(e[1].children()[0][1]) == pycparser.c_ast.ArrayDecl:
i = e[1].children()[0][1]
proper = {}
line = srcs[i.coord.line-1]
proper['name'] = e[1].name
proper['dims'] = []
proper['type'] = line[:i.coord.column-1].strip().replace('extern ','')
if proper['type'].find(',')!=-1:
tmpType = proper['type']
tmpType = ' '.join(tmpType.split(' ')[:-1])
proper['type'] = tmpType
if proper['type'].find('*')!= -1:
proper['type'] = proper['type'].replace('*','').strip()
proper['dims'].append('')
proper['type'] = proper['type'].replace('const ','').replace('unsigned ','')
bind_types.append(proper['type'])
dims = re.findall('\[.*?\]',line[i.coord.column-1:])
for j in dims:
k = j.rstrip(']').lstrip('[')
proper['dims'].append(k)
attrArr.append(proper)
else:
attr.append(e[1].name)
#generate the file
content = ""
#process multi first
for i in functionDouble:
content += gen_multidef(i)
content += '''PYBIND11_MODULE(pyrtklib5, m) {
m.doc() = "rtklib python interface by pybind11";
m.attr("NULL") = __null;
'''
content += gen_defs(defines)
# for i in set(bind_types):
# k = i.replace('const ','')
# content += bindTemp%(k,k)
for i in struct:
content += bindTemp%(i['name'],i['name'],i['name'],i['name'])
for i in ['long double','double','float','char','unsigned char','int','unsigned int','short','unsigned short','long','unsigned long']:
content += bindTemp%(i,i,i,i)
for i in struct:
content += gen_struct(i)
for i in attrArr:
content += gen_attrarr(i)
for i in attr:
content += gen_attr(i)
for i in functionDouble:
content += gen_functionPointer(i)
for i in function:
content += gen_func(i)
content = content.replace('new Arr1D<short>(o.y,-1)', 'new Arr1D<short>(const_cast<short*>(o.y),-1)')
content = content.replace('strsvr_t& o,Arr1D<char>arr','strsvr_t& o,Arr1D<unsigned char>arr')
with open('pyrtklib5/pyrtklib5_pre.cpp','w') as f:
f.write(header+content+footer)