Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Cache dependencies
id: cache-vcpkg-deps
uses: actions/cache@v4
with:
path: vcpkg_installed
key: vcpkg-${{ runner.os }}-${{ hashFiles('vcpkg.json') }}

- name: Set compile target
shell: bash
run: |
if [[ "$RUNNER_OS" == "Linux" ]]; then
VCPKG_HOST_TRIPLET=x64-linux-dynamic
elif [[ "$RUNNER_OS" == "Windows" ]]; then
VCPKG_HOST_TRIPLET=x64-mingw-dynamic
elif [[ "$RUNNER_OS" == "macOS" ]]; then
VCPKG_HOST_TRIPLET=arm64-osx-dynamic
fi
echo VCPKG_HOST_TRIPLET="$VCPKG_HOST_TRIPLET" >> $GITHUB_ENV
echo $VCPKG_HOST_TRIPLET

- name: Acquire vcpkg
if: steps.cache-vcpkg-deps.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
repository: "Microsoft/vcpkg"
path: vcpkg
ref: c9c17dcea3016bc241df0422e82b8aea212dcb93

- name: Install libraries with vcpkg.json
if: steps.cache-vcpkg-deps.outputs.cache-hit != 'true'
shell: bash
run: |
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install --host-triplet="$VCPKG_HOST_TRIPLET"

- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
Expand Down
42 changes: 38 additions & 4 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,55 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Cache dependencies
id: cache-vcpkg-deps
uses: actions/cache@v4
with:
path: vcpkg_installed
key: vcpkg-${{ runner.os }}-${{ hashFiles('vcpkg.json') }}

- name: Acquire vcpkg
uses: actions/checkout@v4
with:
submodules: true
repository: "Microsoft/vcpkg"
path: vcpkg
ref: c9c17dcea3016bc241df0422e82b8aea212dcb93

- name: Install libraries with vcpkg.json (macOS/Linux)
shell: bash
run: |
case "${RUNNER_OS}" in
"Windows")
VCPKG_HOST_TRIPLET="x64-mingw-dynamic"
;;
"Linux")
VCPKG_HOST_TRIPLET="x64-linux-dynamic"
;;
"macOS")
VCPKG_HOST_TRIPLET="arm64-osx-dynamic"
;;
*)
echo "Unsupported RUNNER_OS: ${RUNNER_OS}"
exit 1
;;
esac
echo VCPKG_HOST_TRIPLET="$VCPKG_HOST_TRIPLET" >> $GITHUB_ENV
echo $VCPKG_HOST_TRIPLET

- uses: pypa/[email protected]
env:
CIBW_BUILD: "cp313-*"
CIBW_SKIP: "*musllinux* *win32*"

CIBW_BEFORE_ALL_WINDOWS: bash tools\cibw_before_windows.sh
CIBW_BEFORE_ALL_WINDOWS: bash tools\cibw_before.sh
CIBW_ENVIRONMENT_WINDOWS: "CMAKE_PREFIX_PATH=D:/a/pycpp/pycpp/pybind11/pybind11/share/cmake"

CIBW_BEFORE_ALL_LINUX: "source tools/cibw_before_linux.sh"
CIBW_ENVIRONMENT_PASS_LINUX: "VCPKG_HOST_TRIPLET RUNNER_OS"
CIBW_BEFORE_ALL_LINUX: "source tools/cibw_before.sh"
CIBW_ENVIRONMENT_LINUX: "CMAKE_PREFIX_PATH=/project/pybind11/pybind11/share/cmake"

CIBW_BEFORE_ALL_MACOS: "source tools/cibw_before_linux.sh"
CIBW_BEFORE_ALL_MACOS: "source tools/cibw_before.sh"
CIBW_ENVIRONMENT_MACOS: "CMAKE_PREFIX_PATH=${{ github.workspace }}/pybind11/pybind11/share/cmake"
MACOSX_DEPLOYMENT_TARGET: "14.0"

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ _generate/

cfg/
pybind11/
vcpkg/
vcpkg_installed/
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ cmake_minimum_required(VERSION 3.4...3.18)

project(cppcore)

set(VCPKG_HOST_TRIPLET $ENV{VCPKG_HOST_TRIPLET})
set(VCPKG_HOST_TRIPLET $ENV{VCPKG_HOST_TRIPLET})

set(PYBIND11_FINDPYTHON ON)
find_package(pybind11 CONFIG REQUIRED)
message(STATUS "pybind11 found: ${pybind11_FOUND}")

message(STATUS "vcpkg Triplet: ${VCPKG_HOST_TRIPLET}")
set(EIGEN_PATH "${CMAKE_SOURCE_DIR}/vcpkg_installed/${VCPKG_HOST_TRIPLET}/include")
message(STATUS "Eigen Path: ${EIGEN_PATH}")

pybind11_add_module(cppcore src/cpp/main.cpp)

include_directories(${CMAKE_SOURCE_DIR}/src/cpp ${EIGEN_PATH})

target_compile_definitions(
cppcore
PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO}
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# cppcore for pybind11

MacOS installation:

```bash
mkdir vcpkg
cd vcpkg
git init
git remote add origin [email protected]:microsoft/vcpkg.git
git fetch --depth 1 origin c9c17dcea3016bc241df0422e82b8aea212dcb93
git checkout FETCH_HEAD
cd ..

# copy this path to cfg
uv pip install -e .
export VCPKG_HOST_TRIPLET=arm64-osx-dynamic
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install --host-triplet=$VCPKG_HOST_TRIPLET


uv pip install pybind11==3.0.0 --target=./pybind11
export VCPKG_HOST_TRIPLET=arm64-osx-dynamic
export CMAKE_PREFIX_PATH=/Users/andyshapiro/dev/pycpp/pybind11/pybind11/share/cmake
export CMAKE_BUILD_PARALLEL_LEVEL=$(nproc)
uv pip install -e . && pytest
```
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name = "demo"
dynamic = ["version"]
license = "MIT"
authors = [
{name = "Andy Shapiro", email = "[email protected]"},
{name = "Andy Shapiro", email = "[email protected]"},
]
requires-python = ">=3.11"
requires-python = ">=3.13"
dependencies = [
"numpy",
"pytest",
Expand Down
25 changes: 25 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <Eigen/Dense>

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
Expand All @@ -9,6 +11,27 @@ int add(int i, int j) {

namespace py = pybind11;

// EIGEN test
py::array_t<double> eigen_matmul(py::array_t<double, py::array::c_style | py::array::forcecast> a,
py::array_t<double, py::array::c_style | py::array::forcecast> b) {
if (a.ndim() != 2 || b.ndim() != 2)
throw std::runtime_error("Input must be two 2D arrays");
if (a.shape(1) != b.shape(0))
throw std::runtime_error("Matrix dimensions do not align for multiplication");

using MatrixRm = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
Eigen::Map<const MatrixRm> A(a.data(), a.shape(0), a.shape(1));
Eigen::Map<const MatrixRm> B(b.data(), b.shape(0), b.shape(1));
MatrixRm C = A * B;

// Create numpy array with correct strides (row major)
return py::array_t<double>(
{C.rows(), C.cols()},
{sizeof(double) * C.cols(), sizeof(double)},
C.data()
);
}

PYBIND11_MODULE(cppcore, m) {
m.doc() = R"pbdoc(
Pybind11 example plugin
Expand All @@ -34,4 +57,6 @@ PYBIND11_MODULE(cppcore, m) {

Some other explanation about the subtract function.
)pbdoc");

m.def("eigen_matmul", &eigen_matmul, "Matrix multiplication using Eigen");
}
10 changes: 10 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numpy as np

import demo
from demo import cppcore

Expand All @@ -6,3 +8,11 @@ def test_main():
assert demo.__version__ == "0.0.1"
assert cppcore.add(1, 2) == 3
assert cppcore.subtract(1, 2) == -1


def test_eigen_matmul():
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
result = cppcore.eigen_matmul(a, b)
expected = np.matmul(a, b)
np.testing.assert_allclose(result, expected)
13 changes: 13 additions & 0 deletions tools/cibw_before.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -eo pipefail

if [ "${RUNNER_OS}" = "Linux" ]; then
yum install -y zip
fi

echo $VCPKG_HOST_TRIPLET
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install --host-triplet=$VCPKG_HOST_TRIPLET

pip install pybind11==3.0.0 --target=./pybind11
5 changes: 0 additions & 5 deletions tools/cibw_before_linux.sh

This file was deleted.

18 changes: 0 additions & 18 deletions tools/cibw_before_windows.sh

This file was deleted.

15 changes: 15 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "demo",
"version-string": "0.0.1",
"dependencies": [
{"name": "gsl"},
{"name": "eigen3"},
{"name": "nlopt"}
],
"builtin-baseline": "c9c17dcea3016bc241df0422e82b8aea212dcb93",
"overrides": [
{ "name": "gsl", "version": "2.8#1"},
{ "name": "eigen3", "version": "3.4.0#5"},
{ "name": "nlopt", "version": "2.10.0"}
]
}
Loading