From 010f2c418ec1682ff22d1287100f6b4d6791c3a2 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 2 Nov 2023 11:39:31 -0400 Subject: [PATCH 1/3] chore: Fix noxfile formatting running black --- noxfile.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/noxfile.py b/noxfile.py index 2001736..f0c509c 100644 --- a/noxfile.py +++ b/noxfile.py @@ -2,10 +2,25 @@ import nox -hello_list = ["hello-pure", "hello-cpp", "hello-pybind11", "hello-cython"] +hello_list = [ + "hello-pure", + "hello-cpp", + "hello-pybind11", + "hello-cython", +] if not sys.platform.startswith("win"): - hello_list.extend(["hello-cmake-package", "pi-fortran"]) -long_hello_list = [*hello_list, "pen2-cython", "core-c-hello", "core-pybind11-hello"] + hello_list.extend( + [ + "hello-cmake-package", + "pi-fortran", + ] + ) +long_hello_list = [ + *hello_list, + "pen2-cython", + "core-c-hello", + "core-pybind11-hello", +] @nox.session From eeefc9dd8cb75d41b8a6b537a3c034c0aae9016f Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 2 Nov 2023 12:37:00 -0400 Subject: [PATCH 2/3] feat(core-cython-hello): Add project using add_custom_command() and cython --- noxfile.py | 1 + projects/core-cython-hello/CMakeLists.txt | 11 ++++++++++ .../core-cython-hello/hello/CMakeLists.txt | 14 +++++++++++++ projects/core-cython-hello/hello/__init__.py | 3 +++ projects/core-cython-hello/hello/_hello.pyx | 8 ++++++++ projects/core-cython-hello/pyproject.toml | 20 +++++++++++++++++++ .../tests/test_hello_cython.py | 11 ++++++++++ 7 files changed, 68 insertions(+) create mode 100644 projects/core-cython-hello/CMakeLists.txt create mode 100644 projects/core-cython-hello/hello/CMakeLists.txt create mode 100644 projects/core-cython-hello/hello/__init__.py create mode 100644 projects/core-cython-hello/hello/_hello.pyx create mode 100644 projects/core-cython-hello/pyproject.toml create mode 100644 projects/core-cython-hello/tests/test_hello_cython.py diff --git a/noxfile.py b/noxfile.py index f0c509c..b866ebf 100644 --- a/noxfile.py +++ b/noxfile.py @@ -7,6 +7,7 @@ "hello-cpp", "hello-pybind11", "hello-cython", + "core-cython-hello", ] if not sys.platform.startswith("win"): hello_list.extend( diff --git a/projects/core-cython-hello/CMakeLists.txt b/projects/core-cython-hello/CMakeLists.txt new file mode 100644 index 0000000..76da05e --- /dev/null +++ b/projects/core-cython-hello/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.15...3.26) + +project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES C) + +find_package( + Python + COMPONENTS Interpreter Development.Module + REQUIRED) +find_program(CYTHON "cython") + +add_subdirectory(hello) diff --git a/projects/core-cython-hello/hello/CMakeLists.txt b/projects/core-cython-hello/hello/CMakeLists.txt new file mode 100644 index 0000000..74aa4c8 --- /dev/null +++ b/projects/core-cython-hello/hello/CMakeLists.txt @@ -0,0 +1,14 @@ +add_custom_command( + OUTPUT _hello.c + DEPENDS _hello.pyx + COMMAND "${CYTHON}" + "${CMAKE_CURRENT_SOURCE_DIR}/_hello.pyx" + --output-file "${CMAKE_CURRENT_BINARY_DIR}/_hello.c" + VERBATIM + ) + +Python_add_library(_hello + MODULE "${CMAKE_CURRENT_BINARY_DIR}/_hello.c" + WITH_SOABI) + +install(TARGETS _hello LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME}) diff --git a/projects/core-cython-hello/hello/__init__.py b/projects/core-cython-hello/hello/__init__.py new file mode 100644 index 0000000..c6909c7 --- /dev/null +++ b/projects/core-cython-hello/hello/__init__.py @@ -0,0 +1,3 @@ +from ._hello import elevation, hello + +__all__ = ("hello", "elevation") diff --git a/projects/core-cython-hello/hello/_hello.pyx b/projects/core-cython-hello/hello/_hello.pyx new file mode 100644 index 0000000..22a41fa --- /dev/null +++ b/projects/core-cython-hello/hello/_hello.pyx @@ -0,0 +1,8 @@ + +cpdef void hello(str strArg): + "Prints back 'Hello ', for example example: hello.hello('you')" + print("Hello, {}!".format(strArg)) + +cpdef long elevation(): + "Returns elevation of Nevado Sajama." + return 21463L diff --git a/projects/core-cython-hello/pyproject.toml b/projects/core-cython-hello/pyproject.toml new file mode 100644 index 0000000..f9a0100 --- /dev/null +++ b/projects/core-cython-hello/pyproject.toml @@ -0,0 +1,20 @@ +[build-system] +requires = ["scikit-build-core[pyproject]", "cython"] +build-backend = "scikit_build_core.build" + +[project] +name = "hello" +version = "1.2.3" +authors = [ + { name = "The scikit-build team" }, +] +description = "a minimal example package (cython version)" +requires-python = ">=3.8" +classifiers = [ + "License :: OSI Approved :: MIT License", +] +dependencies = [] + +[tool.scikit-build] +cmake.source-dir = "." +minimum-version = "0.5" diff --git a/projects/core-cython-hello/tests/test_hello_cython.py b/projects/core-cython-hello/tests/test_hello_cython.py new file mode 100644 index 0000000..8c0927f --- /dev/null +++ b/projects/core-cython-hello/tests/test_hello_cython.py @@ -0,0 +1,11 @@ +import hello + + +def test_hello(capfd): + hello.hello("World") + captured = capfd.readouterr() + assert captured.out == "Hello, World!\n" + + +def test_elevation(): + assert hello.elevation() == 21463 From edde6af927f0fa464ca0cf138947d49e73ad54fc Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Thu, 25 Jan 2024 06:06:18 -0500 Subject: [PATCH 3/3] feat(core-cython-hello): Switch to using cython-cmake --- projects/core-cython-hello/CMakeLists.txt | 5 ++++- projects/core-cython-hello/hello/CMakeLists.txt | 11 +++-------- projects/core-cython-hello/pyproject.toml | 2 +- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/projects/core-cython-hello/CMakeLists.txt b/projects/core-cython-hello/CMakeLists.txt index 76da05e..58f4f91 100644 --- a/projects/core-cython-hello/CMakeLists.txt +++ b/projects/core-cython-hello/CMakeLists.txt @@ -6,6 +6,9 @@ find_package( Python COMPONENTS Interpreter Development.Module REQUIRED) -find_program(CYTHON "cython") + +find_package( + Cython + REQUIRED) add_subdirectory(hello) diff --git a/projects/core-cython-hello/hello/CMakeLists.txt b/projects/core-cython-hello/hello/CMakeLists.txt index 74aa4c8..13d5a26 100644 --- a/projects/core-cython-hello/hello/CMakeLists.txt +++ b/projects/core-cython-hello/hello/CMakeLists.txt @@ -1,14 +1,9 @@ -add_custom_command( - OUTPUT _hello.c - DEPENDS _hello.pyx - COMMAND "${CYTHON}" - "${CMAKE_CURRENT_SOURCE_DIR}/_hello.pyx" - --output-file "${CMAKE_CURRENT_BINARY_DIR}/_hello.c" - VERBATIM +Cython_compile_pyx(_hello.pyx + OUTPUT_VAR _hello_source_file ) Python_add_library(_hello - MODULE "${CMAKE_CURRENT_BINARY_DIR}/_hello.c" + MODULE ${_hello_source_file} WITH_SOABI) install(TARGETS _hello LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME}) diff --git a/projects/core-cython-hello/pyproject.toml b/projects/core-cython-hello/pyproject.toml index f9a0100..01fecf3 100644 --- a/projects/core-cython-hello/pyproject.toml +++ b/projects/core-cython-hello/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["scikit-build-core[pyproject]", "cython"] +requires = ["scikit-build-core[pyproject]", "cython", "cython-cmake"] build-backend = "scikit_build_core.build" [project]