Package: pybind11-weaver
Pybind11 Weaver: Python Binding Code GeneratorPybind11 Weaveris 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 asample.hfile
and transforms it into
asample_binding.cc.incfile usingcfg.yamlas a guide.
Following the binding with a single lineauto update_guard = DeclFn(m);inbinding.cc, all elements
from the header file become accessible in Python as demonstrated in
thisexample.A more pragmatic example is available inpylibclang, a comprehensive Python
wrapper for libclang that uses Pybind11 Weaver to generate the binding code.Its practicality stems from the fact thatPybind11 Weaver operates on itas well. Indeed, Pybind11 Weaver is
self-hosted and generates the binding code for its own use.Approximately 30k lines of C++ code are generated from a mere 10 lines
ofcfg.yaml.Somebinding codeis manually crafted to
handle special cases and integrates seamlessly with the generated code.pylibtoolingis a much more advanced example that uses Pybind11 Weaver to
generate the binding code forlibtooling, and will be used to demonstrate
the capabilities of Pybind11 Weaver when working with large C++ only libraries.Key FeaturesHighly 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 simplepip installgets it ready to work.Versatility:All generated code is under your control, you can easily modify/enhance/disable any part of
generated
code, and all generated code will work with your hand-written code seamlessly.Structure Preservation:It retains the module structure of the original C++ code.Features & RoadmapBinding for EnumBinding for Namespace (as submodule)Binding for Function, with support of function overloadingBinding for C style function pointer (usually used as callback functions)Binding for opaque pointer and pointer to incomplete typeBinding for Operator overloadingBinding for Class method, method overloading, static method, static method overloading, constructor, constructor
overloading, class fieldTrampoline class for virtual functionBinding for concreate template instance, that includes: implicit(explicit) class(struct) template instantiation,
full class(struct) template specialization, extern function template instance declaration.Support class inheritance hierarchyAuto ignore symbols by : Linkage (e.g.static), visibility (e.g.visibility=hidden), member access
control (e.g.private,protected)Docstring generation from c++ doxygen style commentNamespace hierarchy to Python module hierarchyDynamic update/disable binding by API call.Static update/disable binding by define macro (Mainly used to disable wrong binding code to avoid compilation
error)Auto snake caseBackground & RecommendationsThis project originated from an internal project aimed at creating a Python binding for aLARGEdeveloping 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 onlytediousbut alsoerror-prone.Because the C++ library was under active development, staying updated with daily additions and frequent code
modifications was amaintenance challenge.Some aspects of the C++ library, due to historical reasons, were incompatible with Python conventions, necessitatinghand-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, I 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.Typical workflow:Though most features should work out of the box, the more your API looks like "C With Class", the higher chance Pybind11
Weaver will do all the work for you. If you use too many advanced C++ features, you may need to write some binding
code by yourself.Create acfg.yamlfile, mainly to tell the generator which files to parse.Use Pybind11 Weaver to generate files, likepybind11-weaver --config cfg.yaml.Create abinding.cc, include the generated files, and call the binding code.Disable some generated binding code by define some macro, if there is any compilation error.Add some custom code to replace part of the generated code, or adding some new binding that generator had not
exported.Compile all code into a pybind11 module.Optionally, usepybind11-stubgento generate.pyistub files,
enhancing readability for both humans and MYPY in a static way.Test the module in Python, find bugs, and go to step 5 to fix them.Also, if you encountered too many problems, you are welcome to open an issue at github, or create a PR to fix it.InstallationVia PYPIpython3-mpipinstallpybind11-weaverFrom SourceTo install from source:gitclonehttps://github.com/edimetia3d/pybind11_weaver
python3-mpipinstall$(pwd)/pybind11_weaver/To run from source (Editable/Develop Mode):gitclonehttps://github.com/edimetia3d/pybind11_weaver
python3-mpipinstall-e$(pwd)/pybind11_weaver/-v--config-settingseditable_mode=compatHow it worksThe Pybind11 Weaver operates under the hood by utilizinglibclang, 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 astruct:During the construction of the struct, it creates some Pybind11 objects, such aspybind11::class_orpybind11::enum_.When theUpdate()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.