Monday, 30 September 2019

Python CFFI: Build single module from multiple source files

My aim is to build a single module/extension from multiple C source files. What is the best practice to achieve this?

What I tried:

I know that theffi.set_source function has the optional sources and include_dir kwargs and that there should be a possibility to use these kwargs to build a single shared object from multiple source files. Unfortunately I cannot find anything regarding these kwargs in the API reference of the CFFI Documentation. So I can't figure out the details. Is there any document which explains how to properly use these kwargs?

I currently use:

SOURCES = ('file_1.c', 'file_2.c')
SRC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src')

ffi_builder = FFI()
ffi_builder.set_source(
    'module_name._module_name',
    ''.join(f'#include "{source}"\n' for source in SOURCES),
    include_dirs=[SRC_ROOT],
)

...

Although this actually works, it seems a bit hacky to me. Is it possible to use sources kwarg instead of the 2nd positional arg source? This would allow me to get rid of the hacky ''.join(f'#include "{source}"\n' for source in SOURCES) part.

If there is even a simpler approach, I would also be interested!

Any help is appreciated. Thank you!



from Python CFFI: Build single module from multiple source files

No comments:

Post a Comment