/pywrap

🐉 LLVM-based tool for automatic generation of Python bindings for C++

Primary LanguageC++MIT LicenseMIT

Pywrap

Pywrap is a simple tool which implements an LLVM FrontendAction to create Pyspot-based Python bindings for C++ classes, structs, and enums marked with __attribute__( ( annotate( "pyspot" ) ) ).

#define PYSPOT_EXPORT __attribute__( ( annotate( "pyspot" ) ) )

enum class PYSPOT_EXPORT Color { R, G, B };

// Will export the public interface only
class PYSPOT_EXPORT Test { /* ... */ };

The following guide is very much based on Clang's.

Prerequisites

In order to build Pywrap, you need to fulfill the requirements, and some tools already installed:

Then you can proceed to clone LLVM-project, and pywrap under the clang-tools-extra directory.

# LLVM
git clone -b release_90 https://github.com/llvm/llvm-project.git

# Pywrap
cd llvm-project/clang-tools-extra
git clone https://github.com/Fahien/pywrap.git pywrap

# Go up
cd ..

Build

Modify clang-tools-extra/CMakeLists.txt by adding the following line:

add_subdirectory(pywrap)

Generate the project with cmake:

cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra

Compile it.

cmake --build build --config Release

Run the executable.

build\bin\pywrap.exe

Command line arguments

Pywrap expects a list of source files as arguments. If it encounters the -- separator, it passes the subsequent arguments to the compiler.

pywrap.exe foo.cpp bar.cpp -- -Iinclude -DANSWER=42 -xc++ -std=c++14

This will generate two headers and two source files under the current working directory:

  • include/pyspot/Bindings.h, containing declarations of Python bindings;
  • include/pyspot/Extension.h, containing declarations of the Python module;
  • src/pyspot/Bindings.cpp, definitions of the bindings;
  • src/pyspot/Extension.cpp, definitions of the module.

License

Mit License © 2018-2019 Antonio Caggiano