A fully customizable pybind11 generator to help generate code for exsiting c/c++ library.
Project description
Pybind11 Weaver: Python Binding Code Generator
Pybind11 Weaver is a powerful code generator designed to automate the generation of pybind11 code from C++ header files. It streamlines the process of creating Python bindings, enabling users to focus on writing critical pybind11 code and offloading the tedious work to Pybind11 Weaver.
This tool takes a sample.h file and transforms it into a sample_binding.cc.inc file using cfg.yaml as a guide. Following the binding with a single line auto update_guard = DeclFn(m);
in binding.cc, all elements from the header file become accessible in Python as demonstrated in this example.
Key Features
- Highly Customizable: While the default configuration is super simple and suitable for most cases, it allows for high customization.
- Ease of Use: As a pure Python package, a simple
pip install
gets it ready to work. - Versatility: It supports the merging of generated code with hand-written code, a practice we highly recommend.
- Structure Preservation: It retains the module structure of the original C++ code.
Features & Roadmap
- Namespace hierarchy to Python submodules
- Enum
- Function, function overload
- Class method, method overloading, static method, static method overloading, constructor, constructor overloading
- Class field
- Class access control
- Generate docstring from c++ comment, for enum, enum item, function, class, class method, class field
- Trampoline class for virtual function
- Support working with hand-written code
- Auto snake case
Background & Recommendations
This project originated from an internal project aimed at creating a Python binding for a LARGE developing C++ library. This posed significant challenges:
- The C++ library interface contained a vast number of classes, functions, and enums. Creating bindings for all these elements was not only tedious but also error-prone.
- Because the C++ library was under active development, staying updated with daily additions and frequent code modifications was a maintenance challenge.
- Some aspects of the C++ library, due to historical reasons, were incompatible with Python conventions, necessitating hand-written binding codes.
- The sheer size of the library added to the complexity, making it difficult to develop a generator smart enough to handle everything, hence the need for manual binding code writing.
In light of these challenges, we designed Pybind11 Weaver as a tool to generate the majority of the binding code, leaving users to handcraft the remaining parts as needed. If this approach suits your needs, this tool will be a valuable asset.
Recommended Usage:
- Create a
cfg.yaml
file to describe what needs to be generated. - Use the generator to create
inc
files. - Create a
binding.cc
, include all theinc
files, and call all the binding code. - Compile all code into a binary to finish.
- Optionally, use pybind11-stubgen to generate
.pyi
stub files, enhancing readability for both humans and MYPY in a static way.
Installation
Via PYPI
python3 -m pip install pybind11-weaver
From Source
- To install from source:
git clone https://github.com/edimetia3d/pybind11_weaver
python3 -m pip install $(pwd)/pybind11_weaver/
- To run from source (Editable/Develop Mode):
git clone https://github.com/edimetia3d/pybind11_weaver
python3 -m pip install -e $(pwd)/pybind11_weaver/ -v --config-settings editable_mode=compat
How it works
The Pybind11 Weaver operates under the hood by utilizing libclang, a library that parses C++ header files. This enables us to obtain all APIs from the header file, which are then used to generate the binding code on your behalf.
Notably, only header files are required, as we need declarations, not definitions. However, to ensure accurate parsing of the code, some compiler flags, especially for macros, are necessary.
The code generated is structured into a struct
:
- During the construction of the struct, it creates some Pybind11 objects, such as
pybind11::class_
orpybind11::enum_
. - When the
Update()
API is invoked, the Pybind11 object experiences an update.
The use of a struct permits us to:
- Separate the processes of object creation and updates, ensuring that Pybind11 consistently acknowledges all exported classes, which aids in the generation of accurate documentation.
- Increase the readability of the generated code, making it simpler to debug.
- Simplify customization, as you can easily inherit the struct and override or reimplement necessary elements.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pybind11_weaver-0.0.3.tar.gz
.
File metadata
- Download URL: pybind11_weaver-0.0.3.tar.gz
- Upload date:
- Size: 25.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9b31726b9f5f31477d2f002f8dabd080c3cfe505cc8ed29d99c36ca82898f6a |
|
MD5 | 9cc07897cb5665249ea4ad3c56fb1635 |
|
BLAKE2b-256 | fbbb68ff17b71f97b4393c10cffbfd3e2d9f4270cdb3d7030d855439c05c01ac |
Provenance
File details
Details for the file pybind11_weaver-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: pybind11_weaver-0.0.3-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af2a28f14d49e21aa0e2db3ad6cea70bb1af20f186913ac7070c93a160d762b4 |
|
MD5 | 5fa84593182160c0c820c68ba0d1f087 |
|
BLAKE2b-256 | 07ea1a217bb4cef51ea008839286d35d85496203104457abbd44551e2e284d60 |