Skip to main content

CFFI Buildtool helps build CFFI-based Python extension modules without setuptools.

Project description

Overview

CFFI Buildtool lets you build CFFI-based Python extension modules without setuptools.

  • Free software: MIT license

Installation

pip install cffi-buildtool

You can also install the in-development version with:

pip install https://github.com/inklesspen/cffi-buildtool/archive/main.zip

Documentation

In the PEP 517/518 world, there are many choices for build backends. Of course, a lot of the choices only support pure-Python packages, but even for packages with C extensions, you can choose from meson-python, scikit-build-core, and probably a few others. Unfortunately, the CFFI docs only explain how to integrate with setuptools and a setup.py configuration. Worse, the normal ways of generating C source for your extension require distutils to be available, which was removed from the Python standard library in Python 3.12.

This tool exists to fill this gap. Examples are provided for projects using meson-python, but it should be useful for any PEP 517 build backend which allows you to run a helper tool during the build. It runs in two modes: "exec-python" and "read-sources".

exec-python mode

In the CFFI docs, under "Main mode of usage", you'll find an example build script that looks like this (but with more comments):

from cffi import FFI
ffibuilder = FFI()

ffibuilder.cdef("""
    float pi_approx(int n);
""")

ffibuilder.set_source("_pi_cffi",
"""
    #include "pi.h"
""",
    libraries=['piapprox'])

if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

This Python script, if executed, will generate a C source file named _pi_cffi.c and compile it. However, the CFFI docs go on to recommend using the setuptools cffi_modules option instead of directly executing the script. When you do this, setuptools and CFFI execute the build script, capture the ffibuilder object, and use it to add the C extension module to the build targets.

CFFI Buildtool's exec-python mode works similarly. It ignores the library names, though; linking against the appropriate libraries is a job for your build backend. (For example, with meson-python you would add the necessary library dependencies to the py.extension_module() invocation.)

$ gen-cffi-src exec-python piapprox_build.py _pi_cffi.c

Or, if you named the FFI object something other than ffibuilder:

$ gen-cffi-src exec-python --ffi-var=ffidef piapprox_build.py _pi_cffi.c

This mode is probably most useful for smaller modules. The larger and more complicated a module gets, the more annoying it is to have a Python build script that only exists to provide C source to a build tool. That's why the other mode exists.

read-sources mode

In this mode, the C definitions ("cdef") and C source prelude ("csrc") are provided as separate files, instead of being bundled into a Python script. If you are wrapping a large library, or if you have defined some functions in C for performance reasons, you should consider using this mode. It means your C source will be in a file ending in .c, so your editor and presubmits can work with it without friction.

/* piapprox.cdef.txt */
float pi_approx(int n);
/* piapprox.csrc.c */
#include "pi.h"

When you run the tool in this mode, you need to provide the desired module name on the command line.

$ gen-cffi-src read-sources _pi_cffi piapprox.cdef.txt piapprox.csrc.c _pi_cffi.c

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cffi_buildtool-1.1.1.tar.gz (39.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cffi_buildtool-1.1.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file cffi_buildtool-1.1.1.tar.gz.

File metadata

  • Download URL: cffi_buildtool-1.1.1.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cffi_buildtool-1.1.1.tar.gz
Algorithm Hash digest
SHA256 eeaebfaf6245832b45c97ae8acf62ced049aa72ef7460a1527ef2eaa46777a18
MD5 8490392d6318e03610ead6993818417e
BLAKE2b-256 f4a874d12057bf1ae92d0a89c73963d5e2c056caf162554887585d7195efb0f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cffi_buildtool-1.1.1.tar.gz:

Publisher: python-publish.yml on inklesspen/cffi-buildtool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cffi_buildtool-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: cffi_buildtool-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cffi_buildtool-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b987d63f5675d6e374c60d950b7e4075c6aba741e8dddfcabaf1c4e2eb9b7c8
MD5 e219515d54b8460bc050d0cc09842b3d
BLAKE2b-256 b8142f642aadc47ed3ce5a57b35db1dea3feb4db251c74df6be71a54ec6f8f91

See more details on using hashes here.

Provenance

The following attestation bundles were made for cffi_buildtool-1.1.1-py3-none-any.whl:

Publisher: python-publish.yml on inklesspen/cffi-buildtool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page