From 3b56e252630ae53a44053897b64b49f4dc8fd315 Mon Sep 17 00:00:00 2001 From: Alexy Pellegrini Date: Mon, 25 Mar 2024 10:41:24 +0100 Subject: [PATCH] Add support for packaging VTK SDK as a wheel This pulls a tar.xz archive from https://vtk.org/files/wheel-sdks and package it as a wheel using scikit-build-core. This wheel will then be added to a pip repository to be fetch through build requirements. --- CMakeLists.txt | 65 ++++++++++++++++++++++++++++--- cmake/vtk-config-version.cmake.in | 1 + cmake/vtk-config.cmake.in | 1 + cmake/vtk-sdk-urls.cmake | 13 +++++++ pyproject.toml | 2 + src/main.cpp | 27 ------------- src/vtk_sdk/_core.pyi | 4 -- src/vtk_sdk/cmake/__init__.py | 1 + tests/test_compiled.py | 11 ------ 9 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 cmake/vtk-config-version.cmake.in create mode 100644 cmake/vtk-config.cmake.in create mode 100644 cmake/vtk-sdk-urls.cmake delete mode 100644 src/main.cpp delete mode 100644 src/vtk_sdk/_core.pyi create mode 100644 src/vtk_sdk/cmake/__init__.py delete mode 100644 tests/test_compiled.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 791b1e4..7496dc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,61 @@ -cmake_minimum_required(VERSION 3.15...3.26) -project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX) +cmake_minimum_required(VERSION 3.21) -set(PYBIND11_FINDPYTHON ON) -find_package(pybind11 CONFIG REQUIRED) +set(VTK_VERSION + "9.2.6" + CACHE STRING "VTK SDK version to package") +project( + vtk-sdk + VERSION ${VTK_VERSION} + DESCRIPTION "VTK SDK python distributions" + HOMEPAGE_URL "https://github.com/Kitware/vtk-sdk-python-distributions" + LANGUAGES NONE) -pybind11_add_module(_core MODULE src/main.cpp) -install(TARGETS _core DESTINATION ${SKBUILD_PROJECT_NAME}) +find_package( + Python + COMPONENTS Interpreter Development.Module + REQUIRED) + +include(cmake/vtk-sdk-urls.cmake) +include(FetchContent) + +set(download_dir ${PROJECT_BINARY_DIR}) +set(extract_dir ${PROJECT_BINARY_DIR}/vtk-wheel-sdk) +# FetchContent will download and extract our archive! +FetchContent_Populate( + vtkwheelsdk + URL ${VTK_SDK_BINARY_URL} DOWNLOAD_DIR ${download_dir} + SOURCE_DIR ${extract_dir}) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/vtk-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/vtk_sdk/cmake/vtk-config.cmake @ONLY) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/vtk-config-version.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/vtk_sdk/cmake/vtk-config-version.cmake @ONLY) + +# "/" after ${extract_dir} is required so the folder content is copied to dest, +# instead of the folder itself! +install( + DIRECTORY ${extract_dir}/ + DESTINATION vtk_sdk + PATTERN "bin/*" EXCLUDE) + +install( + DIRECTORY ${extract_dir}/bin/ + DESTINATION vtk_sdk/bin + PATTERN + "*" + PERMISSIONS + OWNER_READ + OWNER_WRITE + OWNER_EXECUTE + GROUP_READ + GROUP_EXECUTE + WORLD_READ + WORLD_EXECUTE) + +install( + FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/vtk_sdk/cmake/__init__.py + ${CMAKE_CURRENT_BINARY_DIR}/vtk_sdk/cmake/vtk-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/vtk_sdk/cmake/vtk-config-version.cmake + DESTINATION vtk_sdk/cmake) diff --git a/cmake/vtk-config-version.cmake.in b/cmake/vtk-config-version.cmake.in new file mode 100644 index 0000000..aa0b38b --- /dev/null +++ b/cmake/vtk-config-version.cmake.in @@ -0,0 +1 @@ +include(${CMAKE_CURRENT_LIST_DIR}/../vtk-@VTK_VERSION@.data/headers/cmake/vtk-config-version.cmake) diff --git a/cmake/vtk-config.cmake.in b/cmake/vtk-config.cmake.in new file mode 100644 index 0000000..79b77d5 --- /dev/null +++ b/cmake/vtk-config.cmake.in @@ -0,0 +1 @@ +include(${CMAKE_CURRENT_LIST_DIR}/../vtk-@VTK_VERSION@.data/headers/cmake/vtk-config.cmake) diff --git a/cmake/vtk-sdk-urls.cmake b/cmake/vtk-sdk-urls.cmake new file mode 100644 index 0000000..608593a --- /dev/null +++ b/cmake/vtk-sdk-urls.cmake @@ -0,0 +1,13 @@ +# VTK SDK is currently stored as a tar.xz archive on a Kitware hosted server +set(python_cp "cp${Python_VERSION_MAJOR}${Python_VERSION_MINOR}") + +# enable any linux to download the right tar.xz +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + set(VTK_SDK_BINARY_URL + "https://vtk.org/files/wheel-sdks/vtk-wheel-sdk-${VTK_VERSION}-${python_cp}-${python_cp}-manylinux_2_17_x86_64.manylinux2014_x86_64.tar.xz" + ) +else() + set(VTK_SDK_BINARY_URL + "https://vtk.org/files/wheel-sdks/vtk-wheel-sdk-${VTK_VERSION}-${python_cp}-${Python_SOABI}.tar.xz" + ) +endif() diff --git a/pyproject.toml b/pyproject.toml index 0002726..0af9b33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -56,6 +56,8 @@ Homepage = "https://github.com/Kitware/vtk-sdk-python-distributions" Discussions = "https://github.com/Kitware/vtk-sdk-python-distributions/discussions" Changelog = "https://github.com/Kitware/vtk-sdk-python-distributions/releases" +[project.entry-points."cmake.prefix"] +any = "vtk_sdk.cmake" [tool.scikit-build] minimum-version = "0.8.2" diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index d570cd0..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include - -int add(int i, int j) { return i + j; } - -namespace py = pybind11; - -PYBIND11_MODULE(_core, m) { - m.doc() = R"pbdoc( - Pybind11 example plugin - ----------------------- - .. currentmodule:: python_example - .. autosummary:: - :toctree: _generate - add - subtract - )pbdoc"; - - m.def("add", &add, R"pbdoc( - Add two numbers - Some other explanation about the add function. - )pbdoc"); - - m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc( - Subtract two numbers - Some other explanation about the subtract function. - )pbdoc"); -} diff --git a/src/vtk_sdk/_core.pyi b/src/vtk_sdk/_core.pyi deleted file mode 100644 index 537c611..0000000 --- a/src/vtk_sdk/_core.pyi +++ /dev/null @@ -1,4 +0,0 @@ -from __future__ import annotations - -def add(_x: int, _y: int) -> int: ... -def subtract(_x: int, _y: int) -> int: ... diff --git a/src/vtk_sdk/cmake/__init__.py b/src/vtk_sdk/cmake/__init__.py new file mode 100644 index 0000000..7739ada --- /dev/null +++ b/src/vtk_sdk/cmake/__init__.py @@ -0,0 +1 @@ +# This module is here because the path of the actual vtk-config.cmake file is not a valid module name diff --git a/tests/test_compiled.py b/tests/test_compiled.py deleted file mode 100644 index d0342ca..0000000 --- a/tests/test_compiled.py +++ /dev/null @@ -1,11 +0,0 @@ -from __future__ import annotations - -import vtk_sdk._core as m - - -def test_add(): - assert m.add(2, 3) == 5 - - -def test_subtract(): - assert m.subtract(7, 5) == 2