CFFI - Cross Compilation

642 views
Skip to first unread message

Sarvi Shanmugham

unread,
Aug 13, 2012, 4:12:14 PM8/13/12
to pytho...@googlegroups.com
I am looking for a FFI for some C libraries in a cross compiled environment.

Not sure if the cffi setup.py works for cross compilation?
That said, setting setup.py in a cross compiled build environment doesn't seem  to be easy in the first place.

What would the recommendation be for using CFFI in a cross compilation environment with CPython

Thanks,
Sarvi

Armin Rigo

unread,
Aug 13, 2012, 4:57:23 PM8/13/12
to pytho...@googlegroups.com
Hi Sarvi,

On Mon, Aug 13, 2012 at 10:12 PM, Sarvi Shanmugham <sarv...@gmail.com> wrote:
> What would the recommendation be for using CFFI in a cross compilation
> environment with CPython

Can you describe more precisely what the use case is?


Armin

Sarvi Shanmugham

unread,
Aug 13, 2012, 6:36:36 PM8/13/12
to pytho...@googlegroups.com, ar...@tunes.org

Ours is a cross build environment.
Build Host is x86
Target Host is MIPS

We cross build python on the Build Host(x86) so that the produced binaries run on MIPS

I have a simple C Library, say libhelloworld.so that is cross compiled for MIPS as well.

I would like to use CFFI to invoke that C library from python on MIPS

Here is how I would normally Cross compile another python extension written in C
$(CROSS_CC) -I$(CROSS_PYTHON_INC_DIR) -o samplepyext.o -c samplepyext.c
$(CROSS_CC) -o samplepyext.so -L$(CROSS_PYTHON_LIB_DIR) -lpython$(CROSSPY_VER) samplepyext.o

Thanks,
Sarvi

Armin Rigo

unread,
Aug 14, 2012, 4:05:43 AM8/14/12
to pytho...@googlegroups.com
Hi Sarvi,

On Tue, Aug 14, 2012 at 12:36 AM, Sarvi Shanmugham <sarv...@gmail.com> wrote:
> Here is how I would normally Cross compile another python extension written
> in C
> $(CROSS_CC) -I$(CROSS_PYTHON_INC_DIR) -o samplepyext.o -c samplepyext.c
> $(CROSS_CC) -o samplepyext.so -L$(CROSS_PYTHON_LIB_DIR)
> -lpython$(CROSSPY_VER) samplepyext.o

Ok, so directly by calling the compiler and not with e.g. a setup.py .
Yes, you can do that with CFFI too.

- first kill the __pycache__ directory completely

- import the module that contains "ffi.verify()" on the host x86

- it will create for you one file __pycache__/_cffi_x*.c and compile
it into __pycache__/_cffi_x*.so.

Then if an .so with the same name is available on the MIPS for
importing, then ffi.verify() will import it directly without
recompiling anything. So use the $(CROSS_CC) lines above to
cross-compile __pycache__/_cffi_x*.c into _cffi_x*.so, but put the
result *outside* the __pycache__ file in a place along the PYTHONPATH
(e.g. in the current directory).


A bientôt,

Armin.

Sarvi Shanmugham

unread,
Aug 14, 2012, 4:53:15 PM8/14/12
to pytho...@googlegroups.com, ar...@tunes.org
Thanks. Will give this a shot. 

From what I can tell, I can still use CFFI without the verify/compilation portion. 

Is it fair to say that without the verify/compilation, cffi just parses C syntax automatically to 
understand the Python ctypes/libffi style definitions needed to access a shared library ?

Or is there more to it than that I am not getting.

Apart from a compiled wrapper and the associated compile time error checking, is there anything I can do with verify()/compiled that I cannot do without it?

Sarvi

Armin Rigo

unread,
Aug 15, 2012, 10:43:15 AM8/15/12
to pytho...@googlegroups.com
Hi Sarvi,

On Tue, Aug 14, 2012 at 10:53 PM, Sarvi Shanmugham <sarv...@gmail.com> wrote:
> Is it fair to say that without the verify/compilation, cffi just parses C
> syntax automatically to
> understand the Python ctypes/libffi style definitions needed to access a
> shared library ?
>
> Or is there more to it than that I am not getting.
>
> Apart from a compiled wrapper and the associated compile time error
> checking, is there anything I can do with verify()/compiled that I cannot do
> without it?

You are mostly correct: when you use CFFI without verify() you get
ABI-mode access like ctypes. The real strength of CFFI however, imho,
is that verify() gives you API compatibility, which is important when
accessing third-party C libraries. If the goal in your case is only
to access a library that you wrote yourself, or that you otherwise
control completely, then ABI-level access is indeed enough. Then you
don't have issues with cross-compilation.


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages