forked from pfalcon/pycopy-ffigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
micropython-ffigen
executable file
·361 lines (266 loc) · 9.71 KB
/
micropython-ffigen
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
#!/usr/bin/env python3.5
import sys
import json
import re
import os
import glob
MODULE = sys.stdout
def py_ffi_out(*code):
global MODULE
print( ' '.join(code), file=MODULE )
comment_level = 0
# https://github.com/ffi/ffi/wiki/Types
#
# nm --dynamic --demangle libNAME.so
# nm -g libNAME.a
#
FFI_TYPE_MAP = {
":void": "v",
":int": "i",
":signed-char": "b",
":unsigned-char": "B",
":short": "h",
":unsigned-short": "H",
":int": "i",
":unsigned-int": "I",
":long": "l",
":unsigned-long": "L",
":long-long": "q",
":unsigned-long-long": "Q",
":enum": "i",
":float": "f",
":double": "d",
"uint8_t": "B",
"uint16_t": "H",
"uint32_t": "I",
"ssize_t": "l",
":_Bool" : "B",
}
UCTYPES_TYPE_MAP = {
":void": "uctypes.VOID",
":char": "uctypes.INT8",
":signed-char": "uctypes.INT8",
":unsigned-char": "uctypes.UINT8",
":short": "uctypes.INT16",
":unsigned-short": "uctypes.UINT16",
":int": "uctypes.INT32",
":unsigned-int": "uctypes.UINT32",
":long": "uctypes.INT32",
":unsigned-long": "uctypes.UINT32",
":long-long": "uctypes.INT64",
":unsigned-long-long": "uctypes.UINT64",
":enum": "uctypes.INT32",
":float": "uctypes.FLOAT",
":double": "uctypes.DOUBLE",
"uint8_t": "uctypes.UINT8",
"uint16_t": "uctypes.UINT16",
"uint32_t": "uctypes.UINT32",
":_Bool" : "uctypes.UINT8",
}
def matches_filters(loc, proj):
process = False
if "include" in proj:
for pat in proj["include"]:
if re.search(pat, loc):
process = True
break
return process
def conv_ffi_type(rec):
tag = rec["tag"]
if tag == ":pointer":
return "p"
if tag == ":array":
return "p"
if tag in FFI_TYPE_MAP:
return FFI_TYPE_MAP[rec["tag"]]
assert 0, rec["tag"]
def conv_uctypes_type(rec, offset):
tag = rec["tag"]
if tag in UCTYPES_TYPE_MAP:
v = UCTYPES_TYPE_MAP[rec["tag"]]
if "%d" in v:
return v % offset
return "%s | %d" % (v, offset)
if tag == ":pointer":
if rec["type"]["tag"] == ":struct":
return "(uctypes.PTR | %d, %s)" % (offset, rec["type"]["name"])
elif rec["type"]["tag"] == ":pointer":
return "(uctypes.PTR | %d, %s)" % (offset, "uctypes.VOID")
if rec["type"]["tag"] not in UCTYPES_TYPE_MAP:
assert 0, rec
return "(uctypes.PTR | %d, %s)" % (offset, UCTYPES_TYPE_MAP[rec["type"]["tag"]])
if tag == ":array":
if rec["type"]["tag"] == ":function-pointer":
return "(uctypes.ARRAY | %d, %d, (uctypes.PTR,)) # TODO: array of func-ptr" % \
(offset, rec["size"])
return "(uctypes.ARRAY | %d, %d, %s)" % (offset, rec["size"], UCTYPES_TYPE_MAP[rec["type"]["tag"]])
if tag == ":function-pointer":
return "(uctypes.PTR | %d, %s) # TODO: func-ptr" % (offset, "uctypes.VOID")
if tag == "union":
return "# TODO: union"
assert 0, rec["tag"]
def handle_typedef(desc, should_output=False):
tag = desc["type"]["tag"]
if tag in (":struct", "struct"):
FFI_TYPE_MAP[desc["name"]] = desc["type"]["name"]
UCTYPES_TYPE_MAP[desc["name"]] = desc["type"]["name"]
if comment_level > 1:
py_ffi_out("# typedef struct %s" % desc["name"])
return
if tag in (":union", "union"):
FFI_TYPE_MAP[desc["name"]] = desc["type"]["name"]
UCTYPES_TYPE_MAP[desc["name"]] = desc["type"]["name"]
if comment_level > 1:
py_ffi_out("# typedef union %s" % desc["name"])
return
if tag == ":pointer":
FFI_TYPE_MAP[desc["name"]] = "p"
# TODO
UCTYPES_TYPE_MAP[desc["name"]] = "(uctypes.PTR | %d, uctypes.VOID)"
if comment_level > 1:
py_ffi_out("# typedef ptr %s" % desc["name"])
return
if tag == ":function-pointer":
FFI_TYPE_MAP[desc["name"]] = "C"
UCTYPES_TYPE_MAP[desc["name"]] = "(uctypes.PTR | %d, uctypes.VOID)"
if comment_level > 1:
py_ffi_out("# typedef func-ptr %s" % desc["name"])
return
FFI_TYPE_MAP[desc["name"]] = FFI_TYPE_MAP[desc["type"]["tag"]]
UCTYPES_TYPE_MAP[desc["name"]] = UCTYPES_TYPE_MAP[desc["type"]["tag"]]
if comment_level > 1:
py_ffi_out("# typedef %s" % desc["name"])
#======================== auto builder ==========================================================
TREE='src'
HOME = os.getcwd()
site_packages='%s/site-packages' % os.getenv('MICROPYHOME','.')
lib_dynload = '%s/lib-dynload' % os.getenv('MICROPYHOME','.')
if os.access('/system/bin/linker64',0):
ARCH = 'android-armv8'
elif os.access('/system/bin/linker',0):
ARCH = 'android-armv7'
os.system('mkdir -p %s'% site_packages)
os.system('mkdir -p %s'% lib_dynload)
for module_file in glob.glob('%s/mod_*.py' % TREE):
if os.path.exists('%s.skip' % module_file ):
print('Skipping %s' % module_file)
continue
print()
print()
module_name = module_file.rsplit('/mod_',1)[-1][:-3]
print('[%s]'% module_name)
module_dest ='%s/%s' % ( site_packages, module_name)
os.system('mkdir -p %s' % module_dest)
soname = '%s/_%s-%s.so' % (lib_dynload,module_name, ARCH )
print('\tBuilding shared (bin)',soname)
cbuild = 'mod_%s.build' % module_name
if os.path.isfile('%s/%s' %(TREE, cbuild )):
#custom build will take soname as $1
print("\tCustom build for :", module_name)
os.chdir( TREE )
os.system('./%s %s' %( cbuild , soname) )
comment_level = 1
else:
os.chdir('%s/%s' % (TREE, module_name ) )
objs=[]
for c_file in glob.glob('*.c'):
build_obj = 'gcc -O3 -c -fsanitize=bool -Wall -Werror -fpic -o %s.o %s' % (c_file[:-2],c_file)
print('\t', build_obj)
os.system(build_obj)
objs.append(c_file[:-2]+'.o')
for c_file in glob.glob('*.cpp'):
build_obj = 'gcc -O3 -c -fsanitize=bool -Wall -Werror -fpic -o %s.o %s' % (c_file[:-2],c_file)
print('\t', build_obj)
os.system(build_obj)
objs.append(c_file[:-2]+'.o')
build_so = 'gcc -O3 -shared -o %s %s' %( soname , ' '.join( objs ) )
print("\t",build_so)
os.system(build_so)
#os.system('rm *.o')
os.system('cat *.h > ../%s.h' % module_name )
os.chdir('..')
os.system('c2ffi %s.h > %s.c2ffi' % (module_name,module_name) )
with open( '%s.c2ffi' % module_name ) as f:
data = json.load(f)
proj = { 'lib' : """ '%s/lib-dynload/_%s.so' % ( os.getenv('MICROPYHOME','.'), '"""+ module_name + "')" }
os.unlink('%s.c2ffi' % module_name )
os.chdir(HOME)
print('\tBuilding loader (source)',module_dest )
MODULE = open('%s/%s/__init__.py' % (site_packages,module_name) ,'w')
py_ffi_out("""import os
import sys
import ffi
import uctypes
if os.access("/system/bin/linker64",0):l="android-armv8"
elif os.access("/system/bin/linker",0):l="android-armv7"
else: l= "linux-%s" % os.popen('arch').read().strip()
l = "_""" + module_name +"""-%s.so" % l
l = "%s/%s" %( '%s/lib-dynload' % os.getenv('MICROPYHOME','.') , l )
print("Loading native lib",l,file=sys.stderr)
l = ffi.open(l)
""" )
for desc in data:
loc = desc["location"]
process = matches_filters(loc, proj)
# We need to collect all types for future references
if desc["tag"] == "typedef":
handle_typedef(desc, process)
continue
if not process:
if comment_level > 0:
py_ffi_out("# Ignoring %s" % loc)
#continue
if comment_level > 0:
py_ffi_out("# Processing %s" % loc)
if desc["tag"] == "function":
params = [conv_ffi_type(x["type"]) for x in desc["parameters"]]
s = '%s = l.func("%s", "%s", "%s")' % (desc["name"], conv_ffi_type(desc["return-type"]), desc["name"], "".join(params))
if desc.get("inline", False):
py_ffi_out("#%s" % s)
else:
py_ffi_out(s)
elif desc["tag"] == "enum":
py_ffi_out("# %s" % desc["name"])
for f in desc["fields"]:
py_ffi_out("%s = %s" % (f["name"], f["value"]))
elif desc["tag"] == "struct":
py_ffi_out("%s = {" % desc["name"])
for f in desc["fields"]:
py_ffi_out(' "%s": %s,' % (f["name"], conv_uctypes_type(f["type"], f["bit-offset"] / 8)))
if desc["bit-size"] != 0:
# This doesn't have to be true due to alignment
#assert desc["bit-size"] == f["bit-offset"] + f["bit-size"]
pass
py_ffi_out("}")
else:
py_ffi_out(desc)
assert 0, desc["tag"]
pass
py_ffi_out("\ndel l")
py_ffi_out("#","="*79)
py_ffi_out("\n"*2)
MODULE.close()
os.system('cat %s >> %s/__init__.py' % ( module_file, module_dest ) )
#
#if len(sys.argv) < 3 or len(sys.argv) > 4:
# py_ffi_out("usage: %s <proj>.json <c2ffi>.json [<c2ffi-macro>.h]" % sys.argv[0])
# sys.exit(1)
#
#
#if len(sys.argv) > 3:
# with open(sys.argv[3]) as f:
# py_ffi_out("\n# Constants\n")
# items = []
# for l in f:
# if l.startswith("/* "):
# loc = l[3:-4]
# elif l.startswith("#define "):
# if matches_filters(loc, proj):
# _, name, val = l.rstrip().split(" ", 2)
# loc = loc.split(":")
# items.append((loc[0], int(loc[1]), name, val))
# items.sort()
# for fname, line, name, val in items:
# #py_ffi_out("#%s:%s" % (fname, line))
# py_ffi_out("%s = %s" % (name, val))
#