Skip to content

Automatic ctypes bindings generator functions for cmake

License

Notifications You must be signed in to change notification settings

aniongithub/cpython-interop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cpython-interop

cpython-interop is a set of cmake functions and utilties that generate code to seamlessly interoperate between a C API and Python via ctypes bindings.

For example, a C function that looks like

EXPORT_API Result createHandle(HandleParams params, Handle* handle);

can be called using the Python code

handle=Handle()
params=HandleParams()
assert(createHandle(params, byref(handle)) == OK)

Callbacks in a C API can also be easily declared and used, like so

@HandleCallbackFuncType def handleCallback(context, contextSizeBytes, handle) -> Result:
    return OK
...
# Set our callback function in HandleParams
params.handleCallbackFunc = handleCallback

In order to generate the bindings, all we need to do is to add the following lines of CMake in the CMakeLists.txt of the C library

# Include our function(s) from cpython-interop
include(${CMAKE_SOURCE_DIR}/cpython-interop/ctypes_bindings.cmake)

...

# Generate ctypes bindings for Python -> C calls
ctypes_bindings(${PROJECT_NAME} 
    ${CMAKE_CURRENT_SOURCE_DIR}/capi.h
    ${CMAKE_CURRENT_BINARY_DIR}/capi.py)

And that's it! The function will also add an install target to copy the generated bindings to the Python site library. See the example(s) here.

About

Automatic ctypes bindings generator functions for cmake

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published