Skip to content

Commit a6dee73

Browse files
committed
_libc: Helper FFI module to find and load proper libc for the system.
It's needed because different LIBC implementation use different shared library names, so this module abstracts operation of finding the correct one. Default search order: 1. libc.so. This is usually doesn't exist, but user can create such symlink, and it will be used fast. 2. libc.so.0, as used by current uClibc versions. 3. libc.so.6, as used by current Glibc versions. uClibc is tried first because system where it is used are usually underpowered to do array of attempts. User can also override default search names by calling _libc.set_names(), (which should be called before importing any other modules).
1 parent 41e738f commit a6dee73

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

_libc/_libc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ffi
2+
3+
4+
_h = None
5+
6+
names = ('libc.so', 'libc.so.0', 'libc.so.6')
7+
8+
def get():
9+
global _h
10+
if _h:
11+
return _h
12+
err = None
13+
for n in names:
14+
try:
15+
_h = ffi.open(n)
16+
return _h
17+
except OSError as e:
18+
err = e
19+
raise err
20+
21+
22+
def set_names(n):
23+
global names
24+
names = n

_libc/metadata.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dist_name = libc
2+
srctype = micropython-lib
3+
type = module
4+
version = 0.1
5+
author = Paul Sokolovsky
6+
desc = MicroPython FFI helper module
7+
long_desc = MicroPython FFI helper module to interface with underlying libc

_libc/setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys
2+
# Remove current dir from sys.path, otherwise setuptools will peek up our
3+
# module instead of system.
4+
sys.path.pop(0)
5+
from setuptools import setup
6+
7+
8+
setup(name='micropython-libc',
9+
version='0.1',
10+
description='MicroPython FFI helper module',
11+
long_description='MicroPython FFI helper module to interface with underlying libc',
12+
url='https://github.com/micropython/micropython/issues/405',
13+
author='Paul Sokolovsky',
14+
author_email='micro-python@googlegroups.com',
15+
maintainer='MicroPython Developers',
16+
maintainer_email='micro-python@googlegroups.com',
17+
license='MIT',
18+
py_modules=['_libc'])

0 commit comments

Comments
 (0)