Skip to main content

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

Project description

Deprecation notice

The features provided by this tool have been incorporated into CFFI, as of CFFI version 2.1.0.

No further updates will be made to the cffi-buildtool package. You should use CFFI's cffi-gen-src tool instead. Thank you!

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.2.0.tar.gz (36.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.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cffi_buildtool-1.2.0.tar.gz
Algorithm Hash digest
SHA256 67e75a9c848c6521a84d6a923bc13e744e7fe7c87bc6d663cf7b44aeccc7d5fe
MD5 d150e6436ab3ce008590163cfe7cbec4
BLAKE2b-256 32f529cd08259b15912c6b3082fc428c685cbaa20f7d46f5e3e9eedd2d21351a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cffi_buildtool-1.2.0.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.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for cffi_buildtool-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f12742514b0f41c9d217ff61b41a1506771756fa43f8c03cee555be141cadb23
MD5 e7f5fe5a53c6a870a778f00e0792b07c
BLAKE2b-256 c3292b3e484413a36f9473c405e6881fb8a9b1bba025d319613da4281c0b5cde

See more details on using hashes here.

Provenance

The following attestation bundles were made for cffi_buildtool-1.2.0-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