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

A general-purpose C/C++ header analysis toolkit. Parse once with libclang, output to any format.

headerkit carries the torch for ctypesgen2 as a ctypes binding generator and serves as the new engine behind autopxd2 for Cython .pxd generation.

Installation

pip install headerkit

Requires Python 3.10+. No Python package dependencies.

Then install libclang:

headerkit-install-libclang   # bundled installer

Or install 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] Route writer 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 stdout writers is an error).

Writers

Writer Output Description
cffi CFFI cdef strings Declarations for ffibuilder.cdef()
ctypes Python modules Complete ctypes binding modules
cython .pxd files Cython declaration files with C++ support
diff JSON or Markdown API compatibility reports between header versions
json JSON Full IR serialization for inspection and tooling
lua LuaJIT FFI bindings ffi.cdef() declarations for LuaJIT
prompt Compact text Token-optimized IR output 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 [tool.headerkit] 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"

Config values serve as defaults; command-line flags override them.

Plugins

Third-party backends and writers self-register 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"

Plugins can also be loaded explicitly:

# .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

headerkit separates parsing from output generation. A backend produces a language-neutral IR, and writers consume that IR to generate output.

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

Add a backend: all writers benefit. Add a writer: all backends feed it.

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.0.tar.gz (409.8 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.0-py3-none-any.whl (256.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: headerkit-0.8.0.tar.gz
  • Upload date:
  • Size: 409.8 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.0.tar.gz
Algorithm Hash digest
SHA256 0042e5dc6b707ca83d09a45a2365b07f1461259651930d65218136d721e76eaa
MD5 935d39f75114f1a7bc66d34043746332
BLAKE2b-256 4c9e32c584144a12d54f30d06b2e268bca27babd1f915faeb94fb137b89f158f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: headerkit-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 256.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65ea70c90381676dee263f1104b3d03b88e1dae3b158e1f7b9f36b4520c02bba
MD5 96c9c7dfcdde39722738882dad0741b0
BLAKE2b-256 675573e609e3bc5c5304015b8672eb9fb8d2ba9daef0f15c8473be2d54f2f388

See more details on using hashes here.

Provenance

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