Skip to main content

CMake helpers for building Cython modules

Project description

cython-cmake

Actions Status

PyPI version PyPI platforms

This provides helpers for using Cython. Use:

find_package(Cython MODULE REQUIRED VERSION 3.0)
include(UseCython)

If you find Python beforehand, the search will take this into account. You can specify a version range on CMake 3.19+. This will define a Cython::Cython target (along with a matching CYTHON_EXECUTABLE variable). The UseCython module will provide the following helper function:

cython_transpile(<pyx_file>
                 [LANGUAGE C | CXX]
                 [CYTHON_ARGS <args> ...]
                 [INCLUDE_DIRECTORIES <dir> ...]
                 [MODULE_NAME <module_name>]
                 [OUTPUT <OutputFile>]
                 [OUTPUT_VARIABLE <OutputVariable>]
                 [PUBLIC_HEADER_VARIABLE <PublicHeaderVariable>]
                 [API_HEADER_VARIABLE <ApiHeaderVariable>]
                 [DEPENDS <depends> ...]
                 )

This function takes a pyx file and makes a matching .c / .cxx file in the current binary directory (exact path can be specified with OUTPUT). A .py file can be given instead, to support Cython's pure Python mode. The location of the produced file is placed in the variable specified by OUTPUT_VARIABLE if given. Extra arguments to the Cython executable can be given with CYTHON_ARGS, and if this is not set, it will take a default from a CYTHON_ARGS variable.

INCLUDE_DIRECTORIES lists directories to search for cimported .pxd files; each is passed to Cython as -I <dir>, with relative paths resolved against the current source directory. Prefer this over adding -I to CYTHON_ARGS, which is a single escape hatch. See Sharing .pxd files between packages.

MODULE_NAME sets the fully qualified dotted module name (e.g. pkg.sub.mod), forwarded as --module-name. Without it, Cython derives the name from the filename and can only qualify it with the containing package if it can walk __init__ files from the source location — set this explicitly when that is not the case, since it affects __module__, pickling, and error messages.

A .pyx with cdef public or cdef api declarations makes Cython write a <name>.h or <name>_api.h header next to the generated source. Pass PUBLIC_HEADER_VARIABLE and/or API_HEADER_VARIABLE to declare those headers as build outputs (so other targets can depend on them) and receive their paths in the given variables. This is useful when embedding Cython in a larger C/C++ application.

cimported .pxd files are tracked automatically through the depfile. Use DEPENDS for extra files or targets the transpilation needs but that do not exist at configure time, such as a .pxd generated by another target (e.g. autopxd).

If the LANGUAGE is not given, and both C and CXX are enabled globally, then the language will try to be deduced from a # distutils: language=... comment in the source file, and C will be used if not found. Only the language directive is read; unlike cythonize, other # distutils: directives (libraries, include_dirs, extra_compile_args, ...) are ignored, since CMake owns the build configuration — express those as properties on the target you build from the generated source.

This utility relies on the DEPFILE feature introduced for Ninja in CMake 3.7, and added for Make in CMake 3.20, and Visual Studio & Xcode in CMake 3.21.

Example

find_package(
  Python
  COMPONENTS Interpreter Development.Module
  REQUIRED)
include(UseCython)

cython_transpile(simple.pyx LANGUAGE C CYTHON_ARGS -X language_level=3 OUTPUT_VARIABLE simple_c)

python_add_library(simple MODULE "${simple_c}" WITH_SOABI)
install(TARGETS simple DESTINATION .)

Sharing .pxd files between packages

If your package exposes declarations that others should be able to cimport (the NumPy pattern), install the .pxd files next to the compiled modules so they ship in the wheel:

install(TARGETS mymod DESTINATION mypkg)
install(FILES mymod.pxd DESTINATION mypkg)

A consumer then locates the installed package and points INCLUDE_DIRECTORIES at it. The provider's install root can be found by querying its module location with the Python interpreter you already found:

find_package(
  Python
  COMPONENTS Interpreter Development.Module
  REQUIRED)

execute_process(
  COMMAND "${Python_EXECUTABLE}" -c
          "import mypkg, os; print(os.path.dirname(mypkg.__file__))"
  OUTPUT_VARIABLE mypkg_include_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE)

cython_transpile(consumer.pyx
  INCLUDE_DIRECTORIES "${mypkg_include_dir}"
  OUTPUT_VARIABLE consumer_c
)

The consumer's .pyx can now cimport from the provider (e.g. from mypkg.mymod cimport ...). cimported .pxd files reached this way are tracked in the depfile like any other, so incremental rebuilds work.

scikit-build-core

To use this package with scikit-build-core, you need to include it in your build requirements:

[build-system]
requires = ["scikit-build-core", "cython", "cython-cmake"]
build-backend = "scikit_build_core.build"

It is also recommended to require CMake 3.21 or newer in your CMakeLists.txt.

Vendoring

You can vendor FindCython and/or UseCython into your package, as well. This avoids requiring a dependency at build time and protects you against changes in this package, at the expense of requiring manual re-vendoring to get bugfixes and/or improvements. This mechanism is also ideal if you want to support direct builds, outside of scikit-build-core.

You should make a CMake helper directory, such as cmake. Add this to your CMakeLists.txt like this:

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

Then, you can vendor our files into that folder:

pipx run cython-cmake vendor cmake

If you want to just vendor one of the two files, use --member FindCython or --member UseCython. You can rerun this command to revendor. The directory must already exist.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cython_cmake-0.3.0.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cython_cmake-0.3.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file cython_cmake-0.3.0.tar.gz.

File metadata

  • Download URL: cython_cmake-0.3.0.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cython_cmake-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1f6091f8003daf121592601eecddb4c318189232d2d35adf968aa609653a0b4e
MD5 4c0f45200add4691f217cd9ccbb1b75e
BLAKE2b-256 fe9104f2ecbf3a0544922364a7df9ce54f14b991b4ad5a3feb98bdf11af9ae55

See more details on using hashes here.

Provenance

The following attestation bundles were made for cython_cmake-0.3.0.tar.gz:

Publisher: cd.yml on scikit-build/cython-cmake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cython_cmake-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: cython_cmake-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cython_cmake-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67ac5c790316b0e7f8f196328cf76479a2f181a67205f305b36e44e1559978ba
MD5 0dc60e32031410ce0b48888a0ca934c4
BLAKE2b-256 1743d10ff6948c1606592aba69c58a24c7640da091123e10ead57617196f168f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cython_cmake-0.3.0-py3-none-any.whl:

Publisher: cd.yml on scikit-build/cython-cmake

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page