Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak Python bindings to create standalone sdist package #3

Closed
wants to merge 1 commit into from

Conversation

Gadgetoid
Copy link
Owner

@Gadgetoid Gadgetoid commented Oct 12, 2023

⚠️ Replaced by #5
⚠️ Submitted upstream - https://lore.kernel.org/linux-gpio/20231013112812.148021-1-phil@gadgetoid.com/T/#t

This changeset vendors the gpiod library into the Python package.

This works by symlinking the lib and include directories up to the parent module, and updating MANIFEST.in to include the source files.

If "LINK_SYSTEM_LIBGPIOD =1" is not specified then the gpiod._ext C Extension is amended to include all of the C sources for gpiod, so it can be built as a standalone module without depending upon a shared distro library.

Why?

So that it can produce an sdist that is installable irrespective of the availability or version of a distro-supplied libgpiod. This prevents a libgpiod pypi package install balking because the distro libgpiod is outdated or otherwise incompatible. This happens with the currently available libgpiod on pypi.

This also ensures that libgpiod can be installed via pypi into an isolated virtual environment, specified as a dependency for Python packages and allow Python developers to target the newest API version.

Fallback to system libgpiod

Installing from an sdist with a "LINK_SYSTEM_LIBGPIOD=1" environment variable will drop the vendored library in favour of the system, however the system library must be compatible with the bindings.

Building

This package has been tested on Raspberry Pi OS (Debian Bookworm based) and Ubuntu Mantic (Pi 5 ARM64 release).

You'll need:

sudo apt install git build-essential make autoconf autoconf-archive automake pkg-config libtool
sudo apt install python3-dev python3-setuptools python3-pip python3-wheel python3-venv

Then clone this branch:

git clone https://github.com/gadgetoid/libgpiod -b python-bindings-3

Configure and build:

cd libgpiod
./autogen.sh
./configure --enable-bindings-python
make

Create yourself a venv so you can install Python packages:

python3 -m venv ~/libgpiod_test_env
source ~/libgpiod_test_env/bin/activate

Now you can attempt to install the sdist:

pip install bindings/python/dist/libgpiod-2.0.1.tar.gz

Testing

This package should, in theory, work on any platform with Python >= 3.10.0 and GPIO character device support.

To see if you have any supported devices:

ls /dev/gpiochip*

The following test script should toggle line 15 1000 times and report how long it took-

⚠️ Make sure you don't try to import gpiod or run the below from the libgpiod/bindings/python directory.

import gpiod
import time

CONSUMER = "Benchmark"
LINE = 15

chip = gpiod.Chip("/dev/gpiochip4")

lines = chip.request_lines(consumer=CONSUMER, config={
    LINE: gpiod.LineSettings(
        direction=gpiod.line.Direction.OUTPUT,
        output_value=gpiod.line.Value.INACTIVE
    ) 
})

t_start = time.time()

n = 1000

for x in range(n):
    lines.set_value(LINE, gpiod.line.Value.ACTIVE)
    lines.set_value(LINE, gpiod.line.Value.INACTIVE)

t_end = time.time()

print(f"Toggling {n} times took: {(t_end - t_start) * 1000:0.4f}ms")

@Gadgetoid Gadgetoid changed the title Python bindings 3 Tweak Python bindings to create standalone sdist package Oct 12, 2023
@Gadgetoid Gadgetoid changed the base branch from master to python-bindings October 12, 2023 20:12
@Gadgetoid Gadgetoid changed the base branch from python-bindings to master October 12, 2023 20:12
@Gadgetoid Gadgetoid force-pushed the python-bindings-3 branch 2 times, most recently from 5b8714c to 9c8a382 Compare October 13, 2023 11:06
@Gadgetoid Gadgetoid changed the base branch from master to python-bindings October 13, 2023 11:10
@Gadgetoid Gadgetoid changed the base branch from python-bindings to master October 13, 2023 11:10
@Gadgetoid Gadgetoid force-pushed the python-bindings-3 branch 2 times, most recently from 03e39df to 5d03bfe Compare October 16, 2023 14:28
Optionally vendor libgpiod source into sdist so that the
Python module can be built from source, even with a missing
or mismatched system libgpiod.

Add optional environment variable "LINK_SYSTEM_LIBGPIOD=1"
so that the sdist package can optionally be built and
linked against a compatible system libgpiod.

eg: LINK_SYSTEM_LIBGPIOD=1 pip install libgpiod

Update build to add an additional sdist target for upload
to pypi. Call setup.py with "GPIOD_VERSION_STR" set, which
triggers a vendored package build. "GPIOD_VERSION_STR" is
saved to "gpiod-version-str.txt" and included in the sdist
for standalone builds.

"GPIOD_VERSION_STR" must be specified in order to produce
a standalone buildable sdist package, this requirement
implicitly preserves the old build behaviour.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
@Gadgetoid
Copy link
Owner Author

Replaced

@Gadgetoid Gadgetoid closed this Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant