Skip to main content

C/C++ header analysis toolkit with pluggable backends and writers for ctypes, Cython, CFFI, LuaJIT FFI, and more

Project description

headerkit

CI Docs PyPI Python License

headerkit: A CLI tool and Python library for parsing C/C++ headers.

Generates:

  • Bindings: ctypes modules, CFFI definitions, Cython .pxd files, and LuaJIT FFI.
  • Data: JSON Intermediate Representation (IR) and API diffs.
  • LLMs: Token-optimized header summaries for prompt windows.
  • Builds: PEP 517 backend for standard Python packaging.

Parse once. Build anywhere.

Quick examples

Every example below assumes this input header:

// mylib.h
typedef struct { int x, y; } Point;
int distance(const Point *a, const Point *b);

ctypes -- drop-in Python module, no build step:

headerkit mylib.h -w ctypes:bindings.py
# generated bindings.py
class Point(ctypes.Structure):
    _fields_ = [
        ("x", ctypes.c_int),
        ("y", ctypes.c_int),
    ]

_lib.distance.argtypes = [ctypes.POINTER(Point), ctypes.POINTER(Point)]
_lib.distance.restype = ctypes.c_int

CFFI -- declarations for ffibuilder.cdef():

headerkit mylib.h -w cffi:_defs.py
/* generated  _defs.py */
typedef struct Point {
    int x;
    int y;
} Point;
int distance(const Point *a, const Point *b);

Cython -- .pxd for compiled C/C++ interop:

headerkit mylib.h -w cython:mylib.pxd
# generated mylib.pxd
cdef extern from "mylib.h":

    ctypedef struct Point:
        int x
        int y

    int distance(const Point *a, const Point *b)

LuaJIT FFI -- ffi.cdef bindings for LuaJIT:

headerkit mylib.h -w lua:mylib_ffi.lua
/* generated mylib_ffi.lua */
local ffi = require("ffi")

ffi.cdef[[

/* Structs */
typedef struct {
    int x;
    int y;
} Point;

/* Functions */
int distance(const Point *a, const Point *b);

]]

JSON -- full IR for custom tooling:

headerkit mylib.h -w json:mylib.json
{
  "path": "mylib.h",
  "declarations": [
    {"kind": "struct", "name": "Point", "fields": [
      {"name": "x", "type": {"kind": "ctype", "name": "int"}},
      {"name": "y", "type": {"kind": "ctype", "name": "int"}}
    ]},
    {"kind": "function", "name": "distance", ...}
  ]
}

Prompt -- token-optimized summary for LLM context windows:

headerkit mylib.h -w prompt
// mylib.h (headerkit compact)
STRUCT Point {x:int, y:int}
FUNC distance(a:const Point*, b:const Point*) -> int

Diff -- API compatibility reports between header versions:

from headerkit.backends import get_backend
from headerkit.writers.diff import DiffWriter

backend = get_backend("libclang")
old = backend.parse('#include "mylib_v1.h"', "v1.h")
new = backend.parse('#include "mylib_v2.h"', "v2.h")
print(DiffWriter(baseline=old, format="markdown").write(new))
## Breaking Changes
### function_signature_changed
- **distance**: parameter 0 type changed from 'const Point *' to 'const Point3D *'

Build backend -- generate cacheable bindings at pip install time:

# In your project's pyproject.toml:
[build-system]
requires = ["headerkit", "hatchling"]
build-backend = "headerkit.build_backend"

Python API -- parse and generate from code:

from headerkit import generate

output = generate("mylib.h", "cffi")
graph LR
    A[C/C++ headers] --> B[backend]
    B --> C[IR]
    C --> D[writer]
    D --> E[output]

Features

  • One parse, many outputs: generate multiple bindings in a single pass with -w ctypes:lib.py -w cython:lib.pxd
  • Config file support: .headerkit.toml or [tool.headerkit] in pyproject.toml
  • Multi-header merging: pass multiple .h files and they are merged into a single umbrella header

Installation

pip install headerkit

Requires Python 3.10+.

Then install libclang (if not already present):

headerkit install-libclang

Or install it manually:

Platform Command
macOS brew install llvm or Xcode Command Line Tools
Ubuntu sudo apt install libclang-dev
Fedora sudo dnf install clang-devel
Windows winget install LLVM.LLVM or LLVM installer

Supports LLVM 18, 19, 20, and 21.

CLI reference

headerkit [options] FILE [FILE ...]

Flags

Flag Description
-b NAME, --backend NAME Parser backend (default: libclang)
-I DIR Add include directory (repeatable)
-D MACRO[=VALUE] Define preprocessor macro (repeatable)
--backend-arg ARG Pass extra argument to the backend (repeatable)
-w WRITER[:FILE] Write output to a file, or omit :FILE for stdout (repeatable)
--writer-opt WRITER:KEY=VALUE Pass an option to a writer (repeatable)
--config PATH Load config from PATH instead of searching
--no-config Skip all config file loading
--version Print version and exit

At most one -w flag may omit the output path. Multiple writers sending to stdout is an error.

Writers

Writer Output Notes
cffi CFFI cdef strings Declarations for ffibuilder.cdef()
ctypes Python module Complete ctypes binding module
cython .pxd file Cython declaration file with C++ support
diff JSON or Markdown API compatibility report between two header versions
json JSON Full IR serialization
lua LuaJIT FFI bindings ffi.cdef() declarations for LuaJIT
prompt Compact text Token-optimized IR for LLM context windows

Pass writer options with --writer-opt:

headerkit mylib.h -w cffi --writer-opt cffi:exclude_patterns=^__
headerkit mylib.h -w ctypes:mylib.py --writer-opt ctypes:lib_name=mylib

Config file

headerkit searches from the current directory upward for .headerkit.toml, or for a [tool.headerkit] section in pyproject.toml. Use --no-config to skip this.

# .headerkit.toml
backend = "libclang"
writers = ["cffi"]
include_dirs = ["/usr/local/include"]
plugins = ["mypkg.headerkit_plugin"]

[writer.cffi]
exclude_patterns = ["^__", "^_internal"]

[writer.ctypes]
lib_name = "mylib"

Command-line flags override config file values.

Plugins

Register third-party backends and writers via Python entry points:

# In your package's pyproject.toml
[project.entry-points."headerkit.backends"]
mybackend = "mypkg.backend:MyBackend"

[project.entry-points."headerkit.writers"]
mywriter = "mypkg.writer:MyWriter"

Or load plugins explicitly from the config file:

# .headerkit.toml
plugins = ["mypkg.headerkit_plugin"]

Cache and build backend

headerkit includes a two-layer cache that stores parsed IR and generated output in .hkcache/. Commit the cache to version control and downstream consumers can build without libclang installed.

from headerkit import generate

# First run: parses with libclang, caches result
output = generate("mylib.h", "cffi")

# Second run: loads from cache, no libclang needed
output = generate("mylib.h", "cffi")
# CLI: generate with caching (on by default)
headerkit mylib.h -w cffi:bindings.py --cache-dir .hkcache

headerkit also ships a PEP 517 build backend. Consumer projects declare it in pyproject.toml and get bindings generated automatically during pip install or python -m build, with no libclang required when the cache is committed:

[build-system]
requires = ["headerkit", "hatchling"]
build-backend = "headerkit.build_backend"

See the Cache Strategy Guide for cache layout, bypass flags, and CI integration, and the Build Backend Guide for full setup instructions.

Python API

from headerkit.backends import get_backend
from headerkit.writers import get_writer

backend = get_backend("libclang")
header = backend.parse('#include "mylib.h"', "wrapper.h", include_dirs=["/path/to/include"])

writer = get_writer("cffi")
print(writer.write(header))

Full documentation, guides, and API reference: axiomantic.github.io/headerkit

Development

git clone https://github.com/axiomantic/headerkit.git
cd headerkit
pip install -e '.[dev]'
pytest

License

This project is licensed under the MIT License.

The vendored clang Python bindings in headerkit/_clang/v*/ are from the LLVM Project and are licensed under the Apache License v2.0 with LLVM Exceptions.

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

headerkit-0.11.0.tar.gz (444.6 kB view details)

Uploaded Source

Built Distribution

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

headerkit-0.11.0-py3-none-any.whl (278.8 kB view details)

Uploaded Python 3

File details

Details for the file headerkit-0.11.0.tar.gz.

File metadata

  • Download URL: headerkit-0.11.0.tar.gz
  • Upload date:
  • Size: 444.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for headerkit-0.11.0.tar.gz
Algorithm Hash digest
SHA256 90ccce12701c4b4142babb8af5618650009431e322be1558bdb5f920072e21f0
MD5 658119fbc9fadf384900a75d1c9c26dc
BLAKE2b-256 f588e2d083f188e432beec54409287b2a576f49a31a2cfcc88a097cf15fc40be

See more details on using hashes here.

Provenance

The following attestation bundles were made for headerkit-0.11.0.tar.gz:

Publisher: release.yml on axiomantic/headerkit

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

File details

Details for the file headerkit-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: headerkit-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 278.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for headerkit-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb5942452356fe802a50ec2cf15151c99dc86ec41428b539a55b50e3c047358a
MD5 55d6acea0a331bcf72b0f0b4a3f5a803
BLAKE2b-256 01dc892eb35d8b8945796ff6d2ddeb333431c1a5107993258d9810ab04b3f084

See more details on using hashes here.

Provenance

The following attestation bundles were made for headerkit-0.11.0-py3-none-any.whl:

Publisher: release.yml on axiomantic/headerkit

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