| 1 |
--- |
| 2 |
title: Cython |
| 3 |
short-description: Support for Cython in Meson |
| 4 |
... |
| 5 |
|
| 6 |
# Cython |
| 7 |
|
| 8 |
Meson provides native support for cython programs starting with version 0.59.0. |
| 9 |
This means that you can include it as a normal language, and create targets like |
| 10 |
any other supported language: |
| 11 |
|
| 12 |
```meson |
| 13 |
lib = static_library( |
| 14 |
'foo', |
| 15 |
'foo.pyx', |
| 16 |
) |
| 17 |
``` |
| 18 |
|
| 19 |
Generally Cython is most useful when combined with the python module's |
| 20 |
extension_module method: |
| 21 |
|
| 22 |
```meson |
| 23 |
project('my project', 'cython') |
| 24 |
|
| 25 |
py = import('python').find_installation() |
| 26 |
dep_py = py.dependency() |
| 27 |
|
| 28 |
py.extension_module( |
| 29 |
'foo', |
| 30 |
'foo.pyx', |
| 31 |
dependencies : dep_py, |
| 32 |
) |
| 33 |
``` |
| 34 |
|
| 35 |
You can pass arguments accepted by the `cython` CLI script with the |
| 36 |
`cython_args` argument: |
| 37 |
|
| 38 |
```meson |
| 39 |
py.extension_module( |
| 40 |
'foo-bounds' |
| 41 |
'foo.pyx', |
| 42 |
dependencies : dep_py, |
| 43 |
cython_args : ['-Xboundscheck=False'], |
| 44 |
) |
| 45 |
``` |
| 46 |
|
| 47 |
## C++ intermediate support |
| 48 |
|
| 49 |
*(New in 0.60.0)* |
| 50 |
|
| 51 |
An option has been added to control this, called `cython_language`. This can be |
| 52 |
either `'c'` or `'cpp'`. |
| 53 |
|
| 54 |
For those coming from setuptools/distutils, they will find two things. First, |
| 55 |
meson ignores `# distutils: language = c++` inline directives. Second that Meson |
| 56 |
allows options only on a per-target granularity. This means that if you need to mix |
| 57 |
cython files being transpiled to C and to C++ you need two targets: |
| 58 |
|
| 59 |
```meson |
| 60 |
project('my project', 'cython') |
| 61 |
|
| 62 |
cython_cpp_lib = static_library( |
| 63 |
'helper_lib', |
| 64 |
'foo_cpp.pyx', # will be transpiled to C++ |
| 65 |
override_options : ['cython_language=cpp'], |
| 66 |
) |
| 67 |
|
| 68 |
py.extension_module( |
| 69 |
'foo', |
| 70 |
'foo.pyx', # will be transpiled to C |
| 71 |
link_with : [cython_cpp_lib], |
| 72 |
dependencies : dep_py, |
| 73 |
) |
| 74 |
``` |