Skip to content

Integrate cffi-buildtool into CFFI itself#241

Merged
mattip merged 12 commits into
python-cffi:mainfrom
ngoldbaum:integrate-buildtool
Jun 24, 2026
Merged

Integrate cffi-buildtool into CFFI itself#241
mattip merged 12 commits into
python-cffi:mainfrom
ngoldbaum:integrate-buildtool

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Closes #47. Revives #76.

This adds a new submodule for cffi named cffi.buildtool and a new CLI script distributed by CFFI named gen-cffi-src. Also adds new tests and documentation. See the new documentation and tests for usage details.

I realize that this is a big change that makes a number of opinionated choices. I'm very happy to adjust things to suit the maintainers' taste.

My ultimate goal here is to make it possible for projects using CFFI to use arbitrary build tools and remove the perceived tight coupling between CFFI and setuptools/distutils. If there isn't appetite to add the code in cffi-buildtool to CFFI itself, I'd also happily document how to depend on and use cffi-buildtool instead.

See inklesspen/cffi-buildtool#2 where I got @inklesspen's blessing to do this.

The is based on code and documentation that were originally written by Rose Davidson (@inklesspen on GitHub) for the cffi-buildtool project: https://github.com/inklesspen/cffi-buildtool.

This adds code and documentation that was originally written by Rose Davidson
(@inklesspen on GitHub) for the cffi-buildtool project: https://github.com/inklesspen/cffi-buildtool
@inklesspen

Copy link
Copy Markdown
Contributor

Just commenting here to confirm I give my full support for this upstreaming, and after a release is cut which includes these changes, I will mark my existing project as deprecated.

Also I'm available to answer any questions about the original code upon which this is based.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts that occurred to me since yesterday and a typo fix

Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/cffi-gen-src.rst
Comment thread testing/cffi1/buildtool_example2/src/csrc/square.c Outdated
@mattip

mattip commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

There is a clang-tsan failure

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

@mattip see #242 and #243, the failure is unrelated.

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

Sorry for taking so long to come back to this. I think this is ready for another round of review now.

@mattip mattip left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very helpful, and will provide a path to move away from distutils/setuptools only cffi support.

Some of my comments might miss the mark. In general I think the concept of "build" is not clear enough. A cffi out-of-line module has three parts: generate C code, compile the C code into a c-extension, and import the module. Does "build" address all three?

An in-line mode module (no compiler used) is different, I assume this does not address the inline-mode usage.

Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/cdef.rst Outdated
Comment thread src/cffi/buildtool/__init__.py Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/cdef.rst Outdated
Comment thread src/cffi/buildtool/_gen.py Outdated
@rgommers

rgommers commented Jun 8, 2026

Copy link
Copy Markdown

I had a read through out of interest; overall this LGTM. The CLI taking the two main build modes and source as input, generated C code as output is useful and matches how Cython, f2py and other such codegen/binding tools are integrated into meson-python and scikit-build-core; the pattern should indeed work for other build backends that are able to build extension modules from C code as well.

ngoldbaum and others added 4 commits June 9, 2026 11:42
Thanks for the review comments! I'm going to apply the review suggestions that are pending now, pull, and address the remaining comments.

Co-authored-by: Matti Picus <matti.picus@gmail.com>
@ngoldbaum

Copy link
Copy Markdown
Contributor Author

I think I've responded to all the review comments. I'd appreciate another look :)

@mattip mattip left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be nit-picking but cffi is confusing anyway, and I think it is made more so by the use of generic “build” when a more specific concept is intended. As I understand it the intention of the tool is to expose the c-code generation mechanism provided by cffi. Then the c code is handed over to a Python compilation framework like meson or setuptools, and the resulting c-extension module can be packaged into a wheel by a wheel producing framework. I would be happier if the tool was called gen-cffi-code or gen-c-code and not buildtool. Additionally, it would be nice to scan for “build” and use a better word instead where possible.

Does that make sense or is it too restrictive?

Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/buildtool.rst Outdated
<https://scikit-build-core.readthedocs.io/>`_, or similar.

Installing CFFI installs the ``gen-cffi-src`` script; running ``python
-m cffi.buildtool`` invokes the same command line and behaves

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find the name buildtool confusing. What does the module actually do? It does not replace mason/setuptools, does it do more than generate c code?

Comment thread doc/source/buildtool.rst Outdated
----------------------------

This mode takes a Python script that dynamically defines an FFI interface and
accompanying C extension source code. The FFI definition script is the same

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And does what exactly? What is the output?

Comment thread doc/source/buildtool.rst Outdated
Comment thread doc/source/index.rst Outdated
using
ref
cdef
buildtool

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the references to not use the word “build”?

Suggested change
buildtool
gen-cffi-src

Comment thread src/cffi/_buildtool.py Outdated
interface. Two subcommands:

``exec-python``
Execute a Python build script that constructs a :class:`cffi.FFI`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Execute a Python build script that constructs a :class:`cffi.FFI`
Execute a Python script that constructs a :class:`cffi.FFI`

Comment thread src/cffi/_buildtool.py Outdated
describes) and emit the generated C source.

``read-sources``
Build the :class:`cffi.FFI` from a separate ``cdef`` file and C

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Build the :class:`cffi.FFI` from a separate ``cdef`` file and C
Create the :class:`cffi.FFI` from a separate ``cdef`` file and C

@rgommers

Copy link
Copy Markdown

I would be happier if the tool was called gen-cffi-code or gen-c-code and not buildtool.

"buildtool" is the filename, the executable is now called gen-cffi-src. While that's a reasonable name: if that's being optimized, then I'd suggest starting with cffi (easier to identify). So cffi-gen-src or cffi-gen-sources/code.

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

I just pushed a commit that globally renames cffi.buildtool to cffi.gen_src and the CLI script from gen-cffi-src to cffi-gen-src. I also tried to remove references to builds and build scripts, replacing them with references to compilation steps that happen after cffi-gen-src finish and FFI definition scripts. I also tried to explain better that the FFI definition script should define an FFI object named ffibuilder by default and how to override that default.

@mattip

mattip commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Perfect. Thanks!

@mattip mattip merged commit 26b3d3a into python-cffi:main Jun 24, 2026
38 checks passed
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.

Document how to use meson to build a cffi cextension

4 participants