|
15 | 15 |
|
16 | 16 | <!-- SPHINX-START --> |
17 | 17 |
|
| 18 | +This provides helpers for using Cython. Use: |
| 19 | + |
| 20 | +```cmake |
| 21 | +find_package(Cython MODULE REQUIRED VERSION 3.0) |
| 22 | +include(UseCython) |
| 23 | +``` |
| 24 | + |
| 25 | +If you find Python beforehand, the search will take this into account. You can |
| 26 | +specify a version range on CMake 3.19+. This will define a `Cython::Cython` |
| 27 | +target (along with a matching `CYTHON_EXECUTABLE` variable). It will also |
| 28 | +provide the following helper function: |
| 29 | + |
| 30 | +```cmake |
| 31 | +Cython_compile_pyx(<pyx_file> |
| 32 | + [LANGUAGE C | CXX] |
| 33 | + [CYTHON_ARGS <args> ...] |
| 34 | + [OUTPUT <OutputFile>] |
| 35 | + [OUTPUT_VARIABLE <OutputVariable>] |
| 36 | + ) |
| 37 | +``` |
| 38 | + |
| 39 | +This function takes a pyx file and makes a matching `.c` / `.cxx` file in the |
| 40 | +current binary directory (exact path can be specified with `OUTPUT`). The |
| 41 | +location of the produced file is placed in the variable specified by |
| 42 | +`OUTPUT_VARIABLE` if given. Extra arguments to the Cython executable can be |
| 43 | +given with `CYTHON_ARGS`, and if this is not set, it will take a default from a |
| 44 | +`CYTHON_ARGS` variable. |
| 45 | + |
| 46 | +If the `LANGUAGE` is not given, and both `C` and `CXX` are enabled globally, |
| 47 | +then the language will try to be deduced from a `# distutils: language=...` |
| 48 | +comment in the source file, and C will be used if not found. |
| 49 | + |
| 50 | +This utility relies on the `DEPFILE` feature introduced for Ninja in CMake 3.7, |
| 51 | +and added for Make in CMake 3.20, and Visual Studio & Xcode in CMake 3.21. |
| 52 | + |
| 53 | +## Example |
| 54 | + |
| 55 | +```cmake |
| 56 | +find_package( |
| 57 | + Python |
| 58 | + COMPONENTS Interpreter Development.Module |
| 59 | + REQUIRED) |
| 60 | +find_package(Cython MODULE REQUIRED) |
| 61 | +
|
| 62 | +cython_compile_pyx(simple.pyx LANGUAGE C OUTPUT_VARIABLE simple_c) |
| 63 | +
|
| 64 | +python_add_library(simple MODULE "${simple_c}" WITH_SOABI) |
| 65 | +``` |
| 66 | + |
| 67 | +## scikit-build-core |
| 68 | + |
| 69 | +To use this package with scikit-build-core, you need to include it in your build |
| 70 | +requirements: |
| 71 | + |
| 72 | +```toml |
| 73 | +[build-system] |
| 74 | +requires = ["scikit-build-core", "cython", "cython-cmake"] |
| 75 | +build-backend = "scikit_build_core.build" |
| 76 | +``` |
| 77 | + |
| 78 | +It is also recommended to require CMake 3.21: |
| 79 | + |
| 80 | +```toml |
| 81 | +[tool.scikit-build] |
| 82 | +cmake.version = ">=3.21" |
| 83 | +``` |
| 84 | + |
18 | 85 | <!-- prettier-ignore-start --> |
19 | 86 | [actions-badge]: https://github.com/scikit-build/cython-cmake/workflows/CI/badge.svg |
20 | 87 | [actions-link]: https://github.com/scikit-build/cython-cmake/actions |
|
0 commit comments