You are not logged in.

#1 2021-08-17 12:11:17

Palmitoxico
Member
From: Brazil
Registered: 2015-09-24
Posts: 16

Python setuptools build c library, access via cdll.LoadLibrary

Hi,

I spent countless hours reading API documentation and searching through the web with no avail. I'm developing a python library as a part of an automated test jig for a PCB we are planing to manufacture. The library calls some C code using ctypes that I've developed earlier for configuring one specific IC via I2C. I could rewrite the entire C library in python as speed is not an issue here, but it would be a major hassle. I built a shared library, loaded using ctypes.cdll.LoadLibrary and wrote a python wrapper to make the necessary type conversions when calling the lib, so far so good. Now I want to package it using setuptools, firts I tried using setuptools.Extension to build and install the shared lib:

#!/usr/bin/env python3

import os
from setuptools import setup, Extension

cdce906_mod = Extension("libcdce906", sources = ["cdce906_cfg.c"])

setup(
    name = "rtm_test_jig",
    version = "0.0.1",
    author = "Augusto Fraga Giachero",
    description = ("Utilities to test and configure RTM-LAMP boards."),
    license = "GPLv3",
    keywords = "rtm-lamp test jig",
    packages = ["rtm_lamp_libs"],
    long_description = "Utilities to test and configure RTM-LAMP boards.",
    install_requires = ["smbus2"],
    ext_modules = [cdce906_mod],
    platforms = ["Linux"],
    classifiers = [
        "Development Status :: 3 - Alpha",
        "Topic :: Utilities",
        "License :: OSI Approved :: GPLv3 License"
    ],
)

Running 'python3 setup.py install --user' build and installs the package and I'm left with the following file structure:

/home/augusto/.local/lib/python3.9/site-packages/rtm_test_jig-0.0.1-py3.9-linux-x86_64.egg/
├── EGG-INFO
│   ├── dependency_links.txt
│   ├── native_libs.txt
│   ├── not-zip-safe
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── libcdce906.cpython-39-x86_64-linux-gnu.so
├── libcdce906.py
└── rtm_lamp_libs
    ├── cdce906.py
    ├── ee24xx64.py
    ├── __init__.py
    ├── lm75.py
    ├── max116xx.py
    ├── pca9554.py
    └── __pycache__

Now the are two problems:

  • The native library has a name that depends on the platform and python version instead of 'libcdce906';

  • The library path isn't included in LD_LIBRARY_PATH, so calling ctypes.cdll.LoadLibrary("libcdce906.cpython-39-x86_64-linux-gnu.so") will fail.

Every tutorial / doc I've come across when searching for how to deal with C libraries in setuptools assumes that you are building a python extension, but that is not the case here, libcdce906 is a plain C library, you can not load it as a python module, you need a proper ctypes wrapper to interact with it.

I could write a a separate Makefile for building and installing libcdce906 to /usr/local/lib, but it would ugly and I would lose the advantages of using setuptools in the first place.

Any hints how to deal with it?

Thanks,
Augusto.

Offline

#2 2021-10-06 23:47:12

eric.meehan
Member
Registered: 2020-01-01
Posts: 52

Re: Python setuptools build c library, access via cdll.LoadLibrary

The native library name is probably predictable - you should be able to ascertain it from within the python shell (probably with the os library) and just append it to the end of the file name.  For loading the library, you could use the full path instead of the relative path.

Offline

Board footer

Powered by FluxBB