2

How do I properly include cython source files in a python meson project managed by the meson build system?

Scrooge McDuck
  • 372
  • 2
  • 14

2 Answers2

5

Cython as a first class language is still in progress (I'm the author of that work), right now the right way is with a generator or custom target, then compile an extension module:

pyx_c = custom_target(
  'cython_file.c',
  output : 'cython_file.c',
  input : 'cython_file.pyx',
  command : [cython, '@INPUT@', '-o', '@OUTPUT@'],
)

import('python').find_installation().extension_module(
  'my_extension_module'
  pyx_c,
  install : true
)

Here's an example from the meson test suite, which is pretty close to my example above.

EDIT: cython as a first class language has landed. So if you can rely on Meson 0.59.0, you can just do:

import('python').find_installation().extension_module(
  'my_extension_module'
  'cython_file.pyx',
  install : true
)
dcbaker
  • 643
  • 2
  • 7
  • 1
    I just [published](https://gitlab.gnome.org/tallero/listview-example) a complete application example for my use case (pygobject)! there were many steps missing! Some were meson-related, other flatpak related, check the diffs for the `wip/cython` MR if needed. – Scrooge McDuck Jun 03 '21 at 04:27
1

The simplest way I found (completely code-side) was to add them like any other source file and when you need to import them in a python file

just add

from pyximport import install
install(language_level=3)

before importing.

Best way is @dcbaker's though. I wrote an example pygobject cython application to show that here.

Scrooge McDuck
  • 372
  • 2
  • 14
  • 1
    That seems like the wrong answer. It adds a runtime dependency on Cython (and pyximport is really only suitable for simple cases with few dependencies or customisation). I'd think a better answer would build them once and distribute the built files like distutils does. – DavidW Jun 02 '21 at 15:15
  • building and cythonizing the modules with meson was the primary objective but I found no working examples. I haven't encountered any problem so far. – Scrooge McDuck Jun 02 '21 at 15:25