/ctypesgen2

Automatically generate ctypes Python bindings from C sources and shared objects, using ctypeslib2 with libclang. Plus a CMake wrapper.

Primary LanguagePythonMIT LicenseMIT

ctypesgen2

A wrapper tool around @trolldbois' ctypeslib (ctypeslib2 on PyPi) for auto generating ctypes Python bindings of C source files, headers, or shared objects, using libclang.

This tool is comparable to the clang2py tool provided by ctypeslib2, but with a slightly different command line interface. In particular the libclang path is exposed as an argument. This is useful when generating python bindings as a build step of a C or C++ library, particularly on Windows where the PATH is searched for dlls like libclang. Build tools like CMake seem to have trouble modifying the PATH for some reason, unlike LD_LIBRARY_PATH on mac or linux which can be adjusted without issues.

Also provided

  • A CMake script, defining a macro for invoking the tool, for generating ctypes bindings as part of the library build process.
  • ctypes_helpers, some utilities for making ctypes easier to use. Includes a conversion from a ctypes.Structure to a python dict, and a means to replace ctypes.Structure's __str__ method with the stringification of such conversion.

Other ctypes generators

Reference of other generators I've come across. I haven't had the chance to test these but the libclang ones are probably comparable. The non-libclang ones may not be cross-platform.

  • Using libclang
    • ctypes-binding-generator. Actually uses itself to generate the libclang bindings it uses!
    • @osandov's ctypesgen. Looks quite simple but tests appear to show comprehensive functionality.
  • Others
    • The original ctypeslib. The codegen tool is based on gcc-xml.
    • @davidjamesca's ctypesgen. Invokes the C preprocessor, uses Lex and Yacc.
    • PyCParser. Actually a runtime C parser and interpreter with an automatic ctypes interface generator.
    • pyclibrary. Uses pyparsing PEG parser. Fully encapsulates generated ctypes in an attempt to be a backend agnostic framework.
    • @aristanetworks' ctypegen. Requires C++ library building.