[WIP] Cython language#48
Conversation
|
With a6ad971 it somewhat works:
|
|
🤖 AI text below 🤖 Rebased onto current The
|
Register a `Cython` CMake language so projects can enable it with `project(... LANGUAGES C Cython)` and add `.pyx`/`.py` sources directly to targets, instead of invoking `cython_transpile` explicitly. The language transpiles each source to C with the `cython` executable, then hands the generated C to the C compiler; linking and shared-module flags are inherited from the C language so Cython targets link like C ones. Locating Python development components is left to the consuming project. The `find_package(Python)` call must not run during language determination: at that point `CMAKE_SHARED_LIBRARY_SUFFIX` is empty, so FindPython caches a malformed `Python_SOABI` (e.g. `cpython-314t-darwin.so`) that `WITH_SOABI` then turns into a double `.so.so` extension, producing an unimportable module. The feature is still experimental: it needs CMake >= 3.25 (binary-dir-less try_compile) and does not yet install the built module on Windows multi-config generators, so its test is skipped there. Assisted-by: ClaudeCode:claude-opus-4.8
Wire three cython_transpile features into the Cython CMake language: - Include directories: <INCLUDES> is forwarded to the cython step, so target_include_directories() resolves cimported .pxd files (the same dirs still reach the C compiler for headers). - CYTHON_ARGS: baked into the compile rule when the language is enabled (set it before project()). It is joined to a single token and kept off <FLAGS>, which carries C-only options (PIC, build-type flags) that cython rejects. - Dependency tracking: cython -M writes a gcc-style depfile that is copied onto CMake's <DEP_FILE> (with CMAKE_Cython_DEPFILE_FORMAT=gcc), so editing a cimported .pxd triggers a rebuild. A new language_features test package exercises all three: a cimport resolved via an include dir, multi-value CYTHON_ARGS (the --annotate report is asserted), and the .pxd depfile. Remaining gaps vs cython_transpile: per-source C/CXX selection (the language has a single compile rule), genex inside CYTHON_ARGS, and the existing Windows / CMake < 3.25 limitations. Assisted-by: ClaudeCode:claude-opus-4.8
|
🤖 AI text below 🤖 Pushed a follow-up commit ( Now supported in the language
A new Remaining gaps vs
|
|
🤖 AI text below 🤖 C++ support via a source property — feasibility (prototyped, works)Short answer: yes, a source property is the right mechanism — but it has to be the Why a plain property on the single
What does work: a sibling project(mixed LANGUAGES C CXX Cython CythonCXX)
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)
# .pyx defaults to the C `Cython` language; opt individual files into C++:
set_source_files_properties(cppmod.pyx PROPERTIES LANGUAGE CythonCXX)
python_add_library(cppmod MODULE WITH_SOABI)
target_sources(cppmod PRIVATE cppmod.pyx)
To productionize
|
Custom CMake languages are only supported by the Makefile/Ninja generators; the Visual Studio generator has no concept of a custom-language compile rule, so the build produced no module and install() failed (hence the previous Windows skip). scikit-build-core defaults to Visual Studio on Windows, so force Ninja by passing -GNinja through scikit-build-core's cmake.args. Ninja then picks the first C compiler on PATH, which must be MSVC (MinGW cannot compile against the MSVC-built CPython), so CI activates the MSVC dev environment on Windows. Assisted-by: ClaudeCode:claude-opus-4.8
|
I think we should probably split this out into a separate package, as it doesn't seem like this approach has an on/off switch. If there ever is a Cython language, I wouldn't want to interfere with it, and there are some drawbacks (like C++ vs C selection). We could add a new cython-lang-cmake package. |
Ah as in another package also defining the language or worse CMake itself? I think it is fine since the user would probably not have a use for the other parts of this if that were the case? Splitting it on the spec side is trivial, and for pypi, probably a sub project? |
So I am playing around with creating a
Cythonlanguage to try and simplify the way to make Cython files. The basic files that need to be created are documented here and I am looking at other implementedFortran,CSharp, etc. languages for reference.The end-goal is to make the
CMakeLists.txtas:Approach 1:
set(CMAKE_Cython_OUTPUT_EXTENSION ".c")The hope here is that we could define only the bare minimum to create the necessary
.cfiles and have the C compiler part take over from there. Unfortunately CMake does not seem to chain the parts accordingly.Maybe if we can manipulate the dependencies in
.depthis approach would work.Approach 2: chain
CMAKE_Cython_COMPILE_OBJECTwithCMAKE_C_COMPILE_OBJECTThis should probably work with the current implementation, and at first glance it seems to do what it needs to do, but the link-library part doesn't seem to be configured correctly yet