2015-06-04 21:46:08 +00:00
|
|
|
import sys
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-12-18 18:21:17 +00:00
|
|
|
try:
|
|
|
|
import ffi
|
|
|
|
except ImportError:
|
|
|
|
ffi = None
|
2015-06-04 21:46:08 +00:00
|
|
|
|
|
|
|
_cache = {}
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-06-04 21:46:08 +00:00
|
|
|
def open(name, maxver=10, extra=()):
|
2015-12-18 18:21:17 +00:00
|
|
|
if not ffi:
|
|
|
|
return None
|
2015-06-04 21:46:08 +00:00
|
|
|
try:
|
|
|
|
return _cache[name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-06-04 21:46:08 +00:00
|
|
|
def libs():
|
|
|
|
if sys.platform == "linux":
|
2021-05-27 05:50:04 +00:00
|
|
|
yield "%s.so" % name
|
2015-06-04 21:46:08 +00:00
|
|
|
for i in range(maxver, -1, -1):
|
2021-05-27 05:50:04 +00:00
|
|
|
yield "%s.so.%u" % (name, i)
|
2015-06-04 21:46:08 +00:00
|
|
|
else:
|
2021-05-27 05:50:04 +00:00
|
|
|
for ext in ("dylib", "dll"):
|
|
|
|
yield "%s.%s" % (name, ext)
|
2015-06-04 21:46:08 +00:00
|
|
|
for n in extra:
|
|
|
|
yield n
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-06-04 21:46:08 +00:00
|
|
|
err = None
|
|
|
|
for n in libs():
|
|
|
|
try:
|
|
|
|
l = ffi.open(n)
|
|
|
|
_cache[name] = l
|
|
|
|
return l
|
|
|
|
except OSError as e:
|
|
|
|
err = e
|
|
|
|
raise err
|
2015-06-06 19:23:58 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-06-06 19:23:58 +00:00
|
|
|
def libc():
|
|
|
|
return open("libc", 6)
|
2015-08-30 00:13:37 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-08-30 00:13:37 +00:00
|
|
|
# Find out bitness of the platform, even if long ints are not supported
|
|
|
|
# TODO: All bitness differences should be removed from micropython-lib, and
|
|
|
|
# this snippet too.
|
|
|
|
bitness = 1
|
|
|
|
v = sys.maxsize
|
|
|
|
while v:
|
|
|
|
bitness += 1
|
|
|
|
v >>= 1
|