Skip to content

Commit

Permalink
Implement Python bindings (#10)
Browse files Browse the repository at this point in the history
* bindings created

* pip install . working

* python -m build working (on Windows anyway)

* get it building on linux

* Remove test stub (save for the next PR)

* Undo an overzealous rename

* Add Python wheel build job

* Prepare for Python bindings

* Revert CMake minimum version and use cmake_policy. Using builtin `BUILD_TESTING` cmake option.

* Update build.yml

* Update release.yml
  • Loading branch information
aliddell authored Oct 10, 2024
1 parent fac7ea4 commit c1cb57b
Show file tree
Hide file tree
Showing 7 changed files with 635 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- "main"
pull_request: # TODO (aliddell): remove this
branches:
- "main"

jobs:
windows-and-linux-build:
Expand Down Expand Up @@ -115,3 +118,51 @@ jobs:
with:
name: macos-latest ${{matrix.build_type}} binaries
path: ${{github.workspace}}/*.zip

build-wheel:
strategy:
matrix:
platform:
- "windows-latest"
- "ubuntu-latest"
- "macos-latest" # TODO (aliddell): universal binary?

runs-on: ${{ matrix.platform }}

permissions:
actions: write

steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- uses: actions/checkout@v3
with:
submodules: true

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install vcpkg
run: |
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg && ./bootstrap-vcpkg.sh
echo "VCPKG_ROOT=${{github.workspace}}/vcpkg" >> $GITHUB_ENV
echo "${{github.workspace}}/vcpkg" >> $GITHUB_PATH
./vcpkg integrate install
shell: bash

- name: Install dependencies
run: python -m pip install -U pip "pybind11[global]" cmake build

- name: Build
run: python -m build

- name: Upload wheel
uses: actions/upload-artifact@v3
with:
path: ${{github.workspace}}/dist/*.whl
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(BUILD_PYTHON "Build Python bindings" OFF)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
endif()
Expand All @@ -31,4 +33,10 @@ else ()
message(STATUS "Skipping test targets")
endif ()

if (${BUILD_PYTHON})
add_subdirectory(python)
else ()
message(STATUS "Skipping Python bindings")
endif ()

include(CPack)
10 changes: 10 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include CMakeLists.txt
include CMakePresets.json
include vcpkg.json
include vcpkg-configuration.json
include cmake/*.cmake
recursive-include src *
recursive-include include *
recursive-include python *
include LICENSE
include README.md
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"ninja",
"cmake>=3.12",
"pybind11[global]",
]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"python/tests",
]
27 changes: 27 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
project(acquire-zarr-py)
cmake_policy(SET CMP0057 NEW)

execute_process(COMMAND python3 -m pybind11 --cmakedir
RESULT_VARIABLE pybind11_NOT_FOUND
OUTPUT_VARIABLE pybind11_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if (pybind11_NOT_FOUND)
message(FATAL_ERROR "pybind11 not found in the current environment. Please install pybind11 via pip.")
else ()
LIST(APPEND CMAKE_MODULE_PATH ${pybind11_DIR})
cmake_path(CONVERT CMAKE_MODULE_PATH TO_CMAKE_PATH_LIST CMAKE_MODULE_PATH)
endif ()

find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
find_package(pybind11 REQUIRED)

pybind11_add_module(acquire_zarr acquire-zarr-py.cpp)

target_include_directories(acquire_zarr PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(acquire_zarr PRIVATE acquire-zarr)

set_target_properties(acquire_zarr PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
)
Loading

0 comments on commit c1cb57b

Please sign in to comment.