Skip to content

Instantly share code, notes, and snippets.

@mattip
Last active March 20, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattip/76be19a4cffab6991ee957059814d95a to your computer and use it in GitHub Desktop.
Save mattip/76be19a4cffab6991ee957059814d95a to your computer and use it in GitHub Desktop.
playing with clif and cppyy - WIP

This was done on Ubuntu 16.04

PyPY + CPPYY

create a pypy virtualenv virtualenv pypy2-v5.10/bin/pypy pypy_virt

activate it source pypy_virt/bin/activate

install cppyy to pypy pip install cppyy

it downloads and builds cppyy, cppyy-cling, cppyy-backend

after an hour or so, there is an error about installing the backend but that makes sense since PyPy has its own It seems to run the examples like this one:

import cppyy
cppyy.cppdef("""
class MyClass {
public:
    MyClass(int i) : m_data(i) {}
    int m_data;
};""")
#
from cppyy.gbl import MyClass
m = MyClass(42)
cppyy.cppdef("""
void say_hello(MyClass* m) {
    std::cout << "Hello, the number is: " << m->m_data << std::endl;
}""")
#
MyClass.say_hello = cppyy.gbl.say_hello
m.say_hello()
#Hello, the number is: 42
m.m_data = 13
m.say_hello()
#Hello, the number is: 13

Python + CLIF

clone clif git clone https://github.com/google/clif

read the examples/README.md. Decide to use the install script install.sh

it requires some things apt install subversion

The instructions say to use ninja, but the version in ubuntu 16.04 is too old. They also say to use protobuf, but the version is too old so install one.

clone protobuf git clone https://github.com/google/protobuf

build and install protobuf (cd protobuf; ./autogen.sh; ./configure; make; sudo make install; sudo ldconfig)

cd into clif and start the build cd clif; ./INSTALL.sh It will svn checkout llvm into clif_backend in the root directory (one up from clif) and build it

The script failed since the virtualenv, created by the script, does not have the protobuf module installed. Find the virtualenv (maybe $/tmp/examples/bin ?), cd into protobuf/python in protobuf clone, and /tmp/examples/bin/python setup.py install`. Also pip install pyparsing, Now cd back and rerun./INSTAL.sh``.

More long waiting

running the examples works. It

Preliminary Comparison and 5-minute Conclusions

cppyy uses cling to create a representation of the c++ code to run through a cppyy backend, these backends need to be written for each pytohn (Cpython, PyPy) specifically for cppyy.

clif uses a parser much like cython does to create a code file that is valid c++, and is built by setuptools into a python module. The code looks something like cython where it unwraps PyObject* objects, converts them to c++ objects, then calls the original function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment