headerkit
The universal C/C++ interop toolkit. Generates production-ready bindings for Python, Nim, Mojo, LuaJIT, TypeScript/Wasm, and C shims.
HeaderKit parses native C and C++ headers into a normalized intermediate representation (IR). From that single IR, it generates foreign language bindings, tracks breaking API/ABI diffs, reconciles multi-platform matrices, and compresses headers for LLM prompt windows.
What are you looking to do?
| Goal | Quick Command / Action | Section |
|---|---|---|
| Generate Python bindings (ctypes, CFFI, Cython) | headerkit mylib.h -w ctypes -o ctypes:bindings.py |
Python Bindings |
| Generate Mojo, Nim, or LuaJIT bindings | headerkit mylib.h -w mojo -o mojo:mylib.mojo |
Systems & Scripting |
Wrap C++ classes in a C-ABI shim (extern "C") |
headerkit mylib.hpp -w cshim -o cshim:mylib_cshim.cpp |
C Shim Wrappers |
| Parse C/C++ without LLVM/libclang | headerkit mylib.h -b tree-sitter -w ctypes |
Zero-Dependency Parsing |
| Detect breaking API changes between versions | DiffWriter(baseline=v1, format="markdown").write(v2) |
API Diffing |
| Compress headers for LLM prompt windows | headerkit mylib.h -w prompt |
LLM Context |
| Ship wheels without requiring libclang on user machines | headerkit cache populate mylib.h -w cffi |
Build Backend & Cache |
| Inspect IR programmatically | headerkit mylib.h -w json -o json:ast.json |
Programmatic IR |
| Transform IR with composable pipelines | unit >> StripPrefix("ma_") >> MakeOpaque("device") |
IR Pipelines |
| Reconcile multi-target configuration matrices | headerkit add-target bindings.nim --target x86_64-pc-windows-msvc |
Matrix Reconciliation |
Why HeaderKit?
Traditional binding generators (SWIG, bindgen, ctypesgen) couple parser internals directly to single-target code emitters. Keeping bindings updated across multiple languages and platforms as upstream libraries evolve is an error-prone chore.
HeaderKit solves this with three core design pillars:
- Strictly Layered Compiler Pipeline: HeaderKit separates C/C++ parsing (
LibclangBackend,TreeSitterBackend), composable AST transforms (IRTransform), and target code generation (BaseWriter) across a language-agnostic Intermediate Representation (SourceUnit). - Easily Extended to Any Source or Target Language: Built on a unified hook engine (
headerkit.hooks). Write a custom backend, transform, or target writer in ~50 lines of Python using typed [SourceUnit][headerkit.ir.SourceUnit] IR and register it via standard Python entry points or config files without touching core code. - Automated Upstream Updates & Zero-Dependency Downstream: Keep bindings in lockstep with upstream releases. HeaderKit's PEP 517 build backend and committed cache store (
.headerkit/) let downstream consumerspip installwheels without needinglibclangor system compilers installed.
Quick Solutions
Every example below assumes this sample C header:
// mylib.h
typedef struct { int x, y; } Point;
int distance(const Point *a, const Point *b);
1. Generate Python Bindings (ctypes, CFFI, Cython)
Generate zero-build ctypes modules, CFFI cdef declarations, or compiled Cython .pxd headers:
# Drop-in Python ctypes module (zero build step)
headerkit mylib.h -w ctypes -o ctypes:bindings.py
# CFFI declarations for ffibuilder.cdef()
headerkit mylib.h -w cffi -o cffi:_defs.cdef.txt
# Cython .pxd declaration file for compiled C/C++ interop
headerkit mylib.h -w cython -o cython:mylib.pxd
See the ctypes Reference, CFFI Guide, and Cython Reference.
2. Generate Systems & Scripting Bindings (Mojo, Nim, LuaJIT)
Bridge C and C++ libraries into modern systems and scripting runtimes:
# Mojo module with DLHandle dynamic symbol loading
headerkit mylib.h -w mojo -o mojo:mylib.mojo
# Native Nim module with {.importc.} pragma bindings
headerkit mylib.h -w nim -o nim:mylib.nim
# LuaJIT FFI bindings with ffi.cdef[[ ... ]]
headerkit mylib.h -w lua -o lua:mylib_ffi.lua
See the Mojo Reference, Nim Reference, and LuaJIT Reference.
3. Wrap C++ Classes in a C-ABI Shim
Directly binding complex C++ classes across foreign function interfaces is fragile due to mangled symbols and exception boundaries. The cshim writer automatically generates an extern "C" wrapper library with opaque handles and exception guards:
headerkit mylib.hpp -w cshim -o cshim:mylib_cshim.cpp
// generated mylib_cshim.cpp
#include "mylib.hpp"
extern "C" {
typedef void* PointHandle;
PointHandle Point_create(int x, int y) { return new Point(x, y); }
void Point_destroy(PointHandle self) { delete static_cast<Point*>(self); }
}
See the CShim Reference.
4. Zero-Dependency Parsing with Tree-Sitter
When system LLVM / libclang is not installed or in lightweight CI environments, HeaderKit can parse C and C++ headers directly using precompiled Tree-sitter grammars (tree-sitter-c and tree-sitter-cpp):
# Parse C header using tree-sitter backend
headerkit mylib.h -b tree-sitter -w ctypes -o ctypes:bindings.py
# Parse C++ header using tree-sitter backend
headerkit mylib.hpp -b tree-sitter -w cython -o cython:mylib.pxd
See the Tree-sitter Backend Guide.
5. Detect Breaking API Changes
Compare two versions of a C/C++ header to detect signature mutations, missing struct fields, altered enum values, and type changes:
from headerkit.backends import get_backend
from headerkit.writers.diff import DiffWriter
backend = get_backend("libclang")
old_api = backend.parse('#include "mylib_v1.h"', "v1.h")
new_api = backend.parse('#include "mylib_v2.h"', "v2.h")
print(DiffWriter(baseline=old_api, format="markdown").write(new_api))
See the Diff Writer Reference.
6. Compress Headers for LLM Prompts
Large C/C++ headers waste tokens and clutter context windows with preprocessor noise and implementation details. The prompt writer produces a dense, token-optimized summary designed for LLM prompts:
headerkit mylib.h -w prompt
// mylib.h (headerkit compact)
STRUCT Point {x:int, y:int}
FUNC distance(a:const Point*, b:const Point*) -> int
See the Prompt Writer Reference.
7. Ship Wheels Without Requiring libclang
HeaderKit includes a two-layer cache (.headerkit/) storing parsed IR and generated bindings. Commit the cache to version control and downstream users can install wheels without having libclang or LLVM installed:
# Populate cache across multiple architecture targets
headerkit cache populate mylib.h -w cffi --platform linux/amd64 --platform linux/arm64
git add .headerkit/ && git commit -m "cache: populate bindings"
In your project's pyproject.toml, declare the PEP 517 build backend to automatically regenerate bindings during pip install:
[build-system]
requires = ["headerkit", "hatchling"]
build-backend = "headerkit.build_backend"
See the Cache Guide and Build Backend Guide.
8. Inspect IR Programmatically
Parse headers directly into a strongly typed Python AST or serialize them to JSON for downstream code generators, linters, or analysis tools:
from headerkit import generate
from headerkit.backends import get_backend
# Serialized JSON IR
json_ir = generate("mylib.h", "json")
# Typed Python AST
backend = get_backend("libclang")
unit = backend.parse('#include "mylib.h"', "mylib.h")
for decl in unit.declarations:
print(decl.name, type(decl))
See the IR Reference and JSON Reference.
9. Transform IR with Composable Pipelines
Mutate, filter, rename, or splice IR trees between parser extraction and code generation using visitor pipelines and the >> operator:
from headerkit.backends import get_backend
from headerkit.transforms import MakeOpaque, StripPrefix
backend = get_backend("libclang")
unit = backend.parse_source("include/miniaudio.h")
# Strip naming prefixes and make internal structs opaque
pipeline = StripPrefix("ma_") >> MakeOpaque("device")
transformed_unit = pipeline.transform(unit)
See the IR Transformations Guide and Transforms Reference.
10. Reconcile Multi-Platform Matrices & Incremental Updates
Evaluate headers across multi-dimensional matrices (target platforms, library versions, feature flags), minimize boolean guards algebraically, and evolve bindings incrementally:
from headerkit.matrix import ConfigMatrix, probe_configuration
from headerkit.reconcile import reconcile_matrix
from headerkit.writers import get_writer
# 1. Define configuration matrix
matrix = ConfigMatrix()
matrix.add_dimension(
"target", ["x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"], dimension_type="target"
)
# 2. Probe and reconcile
matrix_units = [(cond, probe_configuration("audio.h", cond)) for cond in matrix.resolve()]
reconciled = reconcile_matrix(matrix_units, matrix=matrix, target_lang="nim")
# 3. Emit polyglot conditional bindings
code = get_writer("nim").write(reconciled)
To incrementally add a new platform target to an existing binding file without losing manual edits inside @headerkit:override blocks:
headerkit add-target bindings/audio.nim --target aarch64-unknown-linux-gnu
See the Configuration Matrix Guide, Reconciliation Guide, and Reconcile Reference.
Installation
pip install headerkit
Requires Python 3.11+ (zero runtime dependencies).
To install the optional libclang parser backend (if not already present on your system):
headerkit install-libclang
Or install it via your system package manager:
| Platform | Command |
|---|---|
| macOS | brew install llvm or Xcode Command Line Tools |
| Ubuntu / Debian | sudo apt install libclang-dev |
| Fedora / RHEL | sudo dnf install clang-devel |
| Windows | winget install LLVM.LLVM or LLVM releases |
HeaderKit vendors LLVM bindings supporting libclang 14 through 23.
CLI Reference
headerkit [options] HEADER_OR_GLOB [HEADER_OR_GLOB ...]
Options
| Flag | Description |
|---|---|
-b NAME, --backend NAME |
Parser backend (default: libclang, or tree-sitter) |
-w WRITER, --writer WRITER |
Output writer to invoke (repeatable) |
-o WRITER:PATH, --output |
Output destination template (repeatable, e.g. ctypes:bindings.py) |
-I DIR, --include-dir DIR |
Add include directory (repeatable) |
-D MACRO[=VALUE] |
Define preprocessor macro (repeatable) |
--backend-arg ARG |
Pass extra argument to the parser backend |
--writer-opt WRITER:KEY=VALUE |
Pass writer-specific options (repeatable) |
--store-dir DIR |
Cache store directory (default: .headerkit/) |
--target TRIPLE |
Target triple for cross-compilation (e.g. aarch64-apple-darwin) |
--no-cache |
Disable all cache lookups |
--config PATH |
Path to explicit .headerkit.toml config file |
--no-config |
Skip loading configuration files |
--version |
Display version and exit |
Configuration File
HeaderKit automatically reads configuration from .headerkit.toml or the [tool.headerkit] table in pyproject.toml:
# .headerkit.toml
backend = "libclang"
writers = ["ctypes", "cffi"]
include_dirs = ["/usr/local/include"]
[writer.ctypes]
lib_name = "mylib"
[writer.cffi]
exclude_patterns = ["^__", "^_internal"]
Values support ${ENV_VAR} expansion for build-time paths injected by CMake or CI systems.
Extensibility & Plugins
HeaderKit's hook architecture makes it straightforward to add new source languages, transforms, or target writers without modifying the core repository.
Register third-party plugins in your own package via pyproject.toml entry points:
[project.entry-points."headerkit.backends"]
mybackend = "mypkg.backend:MyBackend"
[project.entry-points."headerkit.writers"]
mywriter = "mypkg.writer:MyWriter"
Or write a custom writer in Python:
from headerkit.ir import SourceUnit
from headerkit.writers import BaseWriter, register_writer
class RubyFfiWriter(BaseWriter):
@property
def name(self) -> str:
return "ruby"
def write(self, unit: SourceUnit) -> str:
lines = ["require 'ffi'", "module MyLib", " extend FFI::Library"]
# Iterate over unit.declarations...
return "\n".join(lines)
register_writer("ruby", RubyFfiWriter)
See the Architecture Guide and Custom Writers Guide.
Roadmap
- Origin Library Versioning & ABI Multi-Version Metadata: Track signatures across multiple upstream release versions to generate version-guarded symbols and automated migration shims.
- Bi-directional Bridges: Generate C/C++ header interfaces and C export shims from high-level Python and Mojo source definitions.
- Grammar-Based Polyglot Source Extraction: Extract C-ABI interfaces from Rust, Zig, and Nim source code into normalized IR using formal Tree-sitter grammars (
tree-sitter-rust,tree-sitter-zig,tree-sitter-nim). - Expanded Target Writers: Additional turnkey writers for Rust (
bindgen-free FFI), Go (cgo), and Swift.
Development
git clone https://github.com/axiomantic/headerkit.git
cd headerkit
pip install -e '.[dev]'
pytest
License
HeaderKit is open source licensed under the MIT License.
Vendored LLVM clang Python bindings in headerkit/_clang/ are licensed under the Apache License v2.0 with LLVM Exceptions.
Metadata
Release files for headerkit 0.50.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| headerkit-0.50.1.tar.gz | 1.5 MB | With provenance | Details for headerkit-0.50.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| headerkit-0.50.1-py3-none-any.whl | Python 3 | none | any | Details for headerkit-0.50.1-py3-none-any.whl |
Total release size: 2.3 MB
Release files / headerkit-0.50.1.tar.gz
| Download URL | headerkit-0.50.1.tar.gz |
|---|---|
| Size | 1.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
344f39cb1062df58832ab2f02446c374c6303c651f1985e82b7178fe57af1aae
|
|
BLAKE2b-256 checksum How to use checksums |
a464d6b2ce624a9aeb55d7e0ee69f197a1fa5a50abbc38d332e907c722b2df1b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / headerkit-0.50.1-py3-none-any.whl
| Download URL | headerkit-0.50.1-py3-none-any.whl |
|---|---|
| Size | 810.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b67daf439a953149f889879c29add81e7e0a884616489d5937184c26b14e0149
|
|
BLAKE2b-256 checksum How to use checksums |
d219141df767286e0e681f9e76f2363c62ba7730588166b2d3a8e1b4c6fb2f82
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for headerkit 0.50.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for headerkit-0.50.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.