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

Parse C/C++ headers with libclang and emit output in any format.

headerkit is the parser engine behind ctypesgen2 (ctypes bindings) and autopxd2 (Cython .pxd generation).

Installation

pip install headerkit

Requires Python 3.10+. No Python runtime dependencies.

Then install libclang:

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.

Quick start

# CFFI bindings to stdout
headerkit mylib.h -w cffi

# ctypes binding module to a file
headerkit mylib.h -w ctypes:mylib.py

# Multiple outputs in one pass
headerkit mylib.h -w cython:mylib.pxd -w json:ir.json

# With include paths and preprocessor defines
headerkit mylib.h -I /usr/local/include -D VERSION=2 -w cffi

CLI reference

headerkit [options] FILE [FILE ...]

Multiple input files are merged into a single umbrella header before parsing.

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"]

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

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))

See the API reference for full documentation of backends, writers, and the IR.

Architecture

A backend parses headers and produces a language-neutral IR. Writers consume that IR and produce output. They are independent; any backend feeds any writer.

C/C++ headers --> [backend] --> IR --> [writer] --> output

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.8.1.tar.gz (410.0 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.8.1-py3-none-any.whl (256.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for headerkit-0.8.1.tar.gz
Algorithm Hash digest
SHA256 fbb577457609f4044087f3b8772c74cee48a7d76e46443432f08027c3155915c
MD5 872efd4fee441dc40f7928854d08f377
BLAKE2b-256 a5eb7d9b1a89e6840ed9133495cd3ef1d2ec3f1ec02a62c9581e7f71f16a55ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for headerkit-0.8.1.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.8.1-py3-none-any.whl.

File metadata

  • Download URL: headerkit-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 256.2 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.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d28ee286fa93552ec4ba34622b38b642441085504182fec262fab4838559ac66
MD5 5ffc4667fb72d0c0184b7d5e5599ff13
BLAKE2b-256 6f858526e96835b84a8a048903815560b71a18e91ba5b7b7a8610d0038a6c43c

See more details on using hashes here.

Provenance

The following attestation bundles were made for headerkit-0.8.1-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