bindings: python: optionally include module in sdist
v6 cover bindings: python: optionally include module in sdist
This changeset vendors the gpiod library into the Python package.
Why?
So that setup.py 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 when attempting to install the current libgpiod from pypi onto - for example - the Debian Bookworm based Raspberry Pi OS or Ubuntu 23.10 Mantic which both ship with libgpiod v1.6.3.
The availability of a distro agnostic package also ensures that libgpiod can be installed via pypi into an isolated virtual environment, safely specified as a dependency for Python packages and allows Python developers to target the newest API version irrespective of their distro supplied libgpiod.
This is essential, since a venv is now widely *required* for user Python projects due to recommendations in pep-688 [1]
For Raspberry Pi this sdist can also be converted into a precompiled wheel by piwheels [2] which is, by default, added to Raspberry Pi OS as a pip index.
How?
If "LINK_SYSTEM_LIBGPIOD=1" is not specified and a valid "GPIOD_VERSION" is supplied then setup.py will fetch the requested, stable tarball from mirrors.edge.kernel.org, unpack the lib and include directories and vendor them into the output sdist. It will also drop a "gpiod-version.txt" so that the sdist knows what GPIOD_VERSION_STR to pass into the gpiod build, eg:
GPIOD_VERSION="2.0.2" python3 -m build . --sdist
Will output dist/libgpiod-2.0.1.tar.gz vendoring libgpiod v2.0.2.
Runtime dependencies for fetching and vendoring a tarball are only imported when an sdist or build_ext command is triggered with "GPIOD_VERSION".
When a user builds or installs an sdist (via pip install or otherwise) if a "gpiod-version.txt" exists and "LINK_SYSTEM_LIBGPIOD=1" is not specified in their environment then the gpiod._ext C Extension is amended to include all of the vendored C sources for gpiod and the resulting module build will function independently of the system libgpiod.
Fetching libgpiod tarballs is an effort to reconsile the fact that both libgpiod and the Python bindings live in the same source tree but are versioned independently of each other. Bugfixes and changes to Python bindings should not vendor unstable, development versions of libgpiod.
No effort has been made to allow an unstable, vendored build since it's assumed developers will build and install libgpiod and use "LINK_SYSTEM_LIBGPIOD".
While the output sdist will - by default - build a standalone gpiod module it's possible for the end user to supply the "LINK_SYSTEM_LIBGPIOD=1" env variable to override this behaviour and attempt to link their system libgpiod (this will fail if it's incompatible with the bindings) eg:
LINK_SYSTEM_LIBPGIOD=1 pip install libgpiod
[1] - https://peps.python.org/pep-0668/
[2] - https://www.piwheels.org/
Changes v5 -> v6
- Change SRC_BASE_URL to mirrors.edge.kernel.org
- Fetch sha256sums.asc and verify tarball signature
- Moved tarfile and urllib imports to fetch_tarball for fewer runtime deps
- Download and extract into a temporary directory
- Changed print to log.info
- Changed print/return errors to raise BaseError
- Check for lib/include in build tree and bail early
Changes v4 -> v5
- Move logic from main program flow to build_ext and sdist overloads
- GPIOD_VERSION_STR is now GPIOD_VERSION
- Fetch sources from a libgpiod tarball if GPIOD_VERSION is supplied
- Removed changes to bindings/python/Makefile.am
- Moved and rephrased comment about build_ext rmtree("tests")
Changes v3 -> v4
- Check for lack of "GPIOD_VERSION_STR" and revert to original behaviour
- Add status messages to setup.py, hinting at the package build mode
Changes v2 -> v3
- Pass in correct "GPIOD_VERSION_STR" from build
- Output an sdist build for pypi
Changes v1 -> v2
- Switch from symlinks to explicit file copy
Phil Howard (1):
bindings: python: optionally include module in sdist
bindings/python/MANIFEST.in | 5 + bindings/python/setup.py | 190 +++++++++++++++++++++++++++++++++--- 2 files changed, 184 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 1/1 bindings: python: optionally include module in sdist
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 two new environment variables "LINK_SYSTEM_LIBGPIOD" and "GPIOD_VERSION" to control what kind of package setup.py will build.
In order to build an sdist or wheel package with a vendored libgpiod a version must be specified via the "GPIOD_VERSION" environment variable.
This will instruct setup.py to fetch the tarball matching the requested version from mirrors.edge.kernel.org, verify the sha256 signature, unpack it, and copy the lib and include directories into the package root so they can be included in sdist or used to build a binary wheel.
eg: GPIOD_VERSION=2.0.2 python3 setup.py sdist
Will build a source distribution with gpiod version 2.0.2 source included.
It will also save the gpiod version into "gpiod-version.txt" so that it can be passed to the build when the sdist is built by pip.
Requiring an explicit version ensures that the Python bindings - which can be changed and versions independent of libgpiod - are built against a stable libgpiod release.
In order to force a package with vendored gpiod source to link the system libgpiod, the "LINK_SYSTEM_LIBGPIOD" environment variable can be used:
eg: LINK_SYSTEM_LIBGPIOD=1 pip install libgpiod
--- bindings/python/MANIFEST.in | 5 + bindings/python/setup.py | 190 +++++++++++++++++++++++++++++++++--- 2 files changed, 184 insertions(+), 11 deletions(-)
bindings/python/MANIFEST.in +5 -0
@@ -2,6 +2,7 @@ # SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <[email protected]> include setup.py +include gpiod-version.txt recursive-include gpiod *.py recursive-include tests *.py
@@ -11,3 +12,7 @@ recursive-include gpiod/ext *.h recursive-include tests/gpiosim *.c recursive-include tests/procname *.c + +recursive-include lib *.c +recursive-include lib *.h +recursive-include include *.h
bindings/python/setup.py +179 -11
222 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 25 lines · Phil Howard
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 two new environment variables "
LINK_SYSTEM_LIBGPIOD" and "GPIOD_VERSION" to control what kind of packagesetup.pywill build.In order to build an sdist or wheel package with a vendored libgpiod a version must be specified via the "
GPIOD_VERSION" environment variable.This will instruct
setup.pyto fetch the tarball matching the requested version from mirrors.edge.kernel.org, verify the sha256 signature, unpack it, and copy the lib and include directories into the package root so they can be included in sdist or used to build a binary wheel.eg: GPIOD_VERSION=2.0.2 python3
setup.pysdistWill build a source distribution with gpiod version 2.0.2 source included.
It will also save the gpiod version into "gpiod-version.txt" so that it can be passed to the build when the sdist is built by pip.
Requiring an explicit version ensures that the Python bindings - which can be changed and versions independent of libgpiod - are built against a stable libgpiod release.
Quoted 7 lines
In order to force a package with vendored gpiod source to link the system libgpiod, the "
LINK_SYSTEM_LIBGPIOD" environment variable can be used:eg: LINK_SYSTEM_LIBGPIOD=1 pip install libgpiod
Signed-off-by Phil Howard <[email protected]>---
Eh... we have one more thing to figure out here.
gpiod_line_request_get_chip_name() has been added to the library and supported in python bindings in current master but hasn't been released yet. So when I go
GPIOD_VERSION=2.0.2 python3 setup.py sdist
it builds fine but then I get the following when trying to import gpiod:
Quoted 1 line
Quoted 1 line
Quoted 1 line
import gpiod
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.11/site-packages/gpiod/__init__.py", line 10,
in <module>
from . import _ext
ImportError: /usr/lib/python3.11/site-packages/gpiod/_ext.cpython-311-x86_64-linux-gnu.so:
undefined symbol: gpiod_line_request_get_chip_name
I can of course make a libgpiod v2.1.0 release - we already have quite a bit of new material but we should probably add something like a "minimum libgpiod version" switch somewhere.
Do you have any ideas?
Bart