Preprocess the interface definitions.

29 views
Skip to first unread message

Lukas Lueg

unread,
Jan 15, 2014, 5:26:17 AM1/15/14
to pytho...@googlegroups.com
This is somewhat related to issue #5 (support for #define and friends). However, as long as a full preprocessor-run is not possible within CFFI, I'm looking for best practices.

My interface definition is rather large (several hundred types, structs etc.) and it becomes impossible to use one interface definition for all platforms without preprocessing it. The prime example is glib's gint64 which is defined as a "signed long long" on 32bit platforms but "signed long" on platforms where the "long"-type is guaranteed to be 64bit.

This will work on 32bit but throw a compiler warning on 64bit:

cffi.FFI().cdef("typedef signed long long gint64; void somelibfunc(gint64 thevalue);")

This will work on 64bit but fail on 32bit:

cffi.FFI().cdef("typedef signed long gint64; void somelibfunc(gint64 thevalue);")


I'm already using autoconf to discover various platform settings (they end up in extra_compile_args) and could discover the correct type definition for gint64 from there. My original thought was to use string.Template from python's stdlib to preprocess the interface definition and deal with discovered features there. This get's clunky however with dozens of if-then-else definitions...

Is there some best practice how the preprocess the interface definition string without issue #5 having been solved?

Armin Rigo

unread,
Jan 15, 2014, 6:30:29 AM1/15/14
to pytho...@googlegroups.com
Hi,

On Wed, Jan 15, 2014 at 11:26 AM, Lukas Lueg <lukas...@gmail.com> wrote:
> Is there some best practice how the preprocess the interface definition
> string without issue #5 having been solved?

Maybe I'm missing something, but it seems to me you want to do the
equivalent of:

ffi.cdef("""
#if SOME_CONDITION
typedef signed long long gint64;
#else
typedef signed long gint64;
#endif
MORE_THINGS
""")

If the condition can be expressed as a Python expression, you can
simply write the above as:

if some_condition:
ffi.cdef("typedef signed long long gint64;")
else:
ffi.cdef("typedef signed long gint64;")
ffi.cdef("""
MORE_THINGS
""")

Is this answer actually helping, or am I too naive?


A bientôt,

Armin.
Reply all
Reply to author
Forward
0 new messages