In old days, I used setup.py
to build my project which hybird cython and plain python. Recently, I’ve heard that using setup.py
to build is not recommend, and pip
will remove the support of it recently.
Therefore I decided to migrate to uv
, uv
recommend me to use scikit-build-core
to build cython project. I tried, and successful built a project contains only cython files. But I failed to build a project hybird cython and python.
I tried to use cmake script, but I’ve only succeed in building b.pyx
, but not c/e.pyx
.
setup.py
script:
from setuptools import setup
from Cython.Build import cythonize
setup(
name='example',
version='0.0.1',
packages=['example', "example.c"],
url='',
license='',
author='Colinxu2020',
author_email='',
description='',
ext_modules=cythonize("src/example/**/*.pyx", annotate=True, language_level = "3")
)
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX)
find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED)
include(UseCython)
#file(GLOB_RECURSE pyx_files src/example/*.pyx)
#foreach(pyx_file ${pyx_files})
# set(cpp_file)
# message(${pyx_file})
# cython_transpile(${pyx_file} LANGUAGE CXX OUTPUT_VARIABLE cpp_file)
# string(REGEX REPLACE ".pyx" "" output_name ${pyx_file})
# python_add_library(${output_name} MODULE WITH_SOABI "${cpp_file}")
#endforeach()
cython_transpile(src/example/b.pyx LANGUAGE CXX OUTPUT_VARIABLE cpp_files)
python_add_library(testcase MODULE WITH_SOABI "${cpp_files}")
install(TARGETS example DESTINATION ${SKBUILD_PROJECT_NAME})
pyproject.toml
[project]
name = "example"
version = "0.1.0"
description = "Add your description here"
authors = [
{ name = "Colinxu2020", email = "[email protected]" }
]
[tool.scikit-build]
minimum-version = "build-system.requires"
build-dir = "build/{wheel_tag}"
[build-system]
requires = ["scikit-build-core>=0.10", "cython", "cython-cmake"]
build-backend = "scikit_build_core.build"
My source file structed like:
- a.py
- b.pyx
- c
- d.py
- e.pyx
When using pure setup.py
, the whl contains:
- a.py
- b.pyd
- c
- d.py
- e.pyd
But when using scikit-build-core
, I get the following result.
- a.py
- c.pyx
- d.py
- e.pyx