Skip to main content

CPython Extension Module Support for Flit

This is a PEP 517 build backend piggybacking (and hacking) Flit to support building C extensions.

Mostly a proof-of-concept, but could be further developed into something more generally useful if Flit can better support hooking into the build system.

Features

  • Can build C extensions.
  • Does not require a user to pre-install a compiler.
  • Very good caching; incremental compilation performs much better than setuptools from my totally non-scientific observation.
  • Produces a wheel with appropriate-ish tag for distribution.

Limitations

  • Since Flit only supports one single top-level Python module/package, and enforces the existence of that file/directory, this can only build extensions as a submodule of a package right now.
  • Since Flit's automatic metadata introspection (read version and description from module) needs to import the top-level module/package, you either need to jump through some hooks to make those work without the extension being available, or only write metadata in pyproject.toml.
  • Slower "cold" compilation time compared to setuptools and platform-provided compilers.
  • Does not allow custom compiler flags (possible to implement).
  • Only handles C for now. I believe it's possible to support C++ (and Zig).
  • Probably more, setuptools has so many years behind it and can therefore cover many edge cases I've never dreamt of.

Characteristics

  • Compiles extension modules directly into the top-level Python package. This makes it possible to run the extension "in-place" without installing, which I find useful. But it makes the source tree a bit messy (you probably need to run git clean once in a while to keep things sane).

How-To

There's a minimal example in examples/demo that has all the needed parts.

[build-system]
requires = ["zlig"]
build-backend = "zlig"

# ... Project metadata declaration.

[tool.flit.sdist]
# Exclude extension modules from sdist.
exclude = ["src/demo/*.so"]

[tool.zlig]
# Declare extensions and its sources.
extensions = [{name = "demo.demo", sources = ["src/**/*.c"]}]

Add the following entries to your .gitignore:

/build.zig
/zig-cache/
/zig-out/
# Flit builds things to /dist so add it too.
# Also *.pyd and *.so files but you should've already ignored them.

Details

As a PEP 517 backend, this module simply bridges most of Flit's build API, but re-implements build_wheel to do some additional things:

  • Compile extension modules before handing the package to Flit, which would add all the files (including compiled extensions) into the wheel.
  • When adding a file to the wheel, first check whether the file is an extension's source and exclude it.
  • Override Flit's logic deciding a wheel's file name to use a platform-specific wheel tag instead.

Compilation magic is provided by Zig's build system. A working Zig compiler is installed as a PEP 517 build dependency. During compilation, the backend generates a build script (build.zig) from pyproject.toml, and call the Zig compiler to do the rest. After compilation, those binaries are copied to the location Flit expects to fine modules.

Release files for zlig 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for zlig 0.0.0
File Interpreter ABI Platform
zlig-0.0.0-py3-none-any.whl Python 3 none any Details

Release files / zlig-0.0.0-py3-none-any.whl

Download URL zlig-0.0.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a60a3b7bcef838c28e4cf3aaa984b7c6da8539ac4217783915c80e2a5dfaab23
BLAKE2b-256 checksum
How to use checksums
b08e2de2043ded4c40e453c417421de55c1ae91deeda390810548f5b48caed47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release history Release notifications | RSS feed

This release

0.0.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page