Skip to content
Open
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
51 changes: 51 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: CI

on:
workflow_dispatch:
pull_request:
push:
branches:
- master

jobs:

standard:

strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest, macos-latest, windows-latest]
example:
- 14_cmake_findpython

defaults:
run:
shell: bash -l {0}

name: "${{ matrix.example }} • ${{ matrix.runs-on }}"
runs-on: ${{ matrix.runs-on }}

steps:

- name: Basic GitHub action setup
uses: actions/checkout@v2

- name: Set conda environment
uses: mamba-org/provision-with-micromamba@main
with:
environment-file: environment.yaml
environment-name: myenv

- name: Configure
working-directory: ${{ matrix.example }}
run: cmake -Bbuild -DPython_EXECUTABLE=`which python`

- name: Compile
working-directory: ${{ matrix.example }}/build
run: cmake --build .

- name: Example
working-directory: ${{ matrix.example }}/build
run: |
cp ../example.py .
python example.py
11 changes: 11 additions & 0 deletions 14_cmake_findpython/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.18..3.20)

project(mymodule)

find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
find_package(pybind11 REQUIRED CONFIG)

pybind11_add_module(mymodule main.cpp)
target_link_libraries(mymodule PUBLIC pybind11::module Python::NumPy)

target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)
5 changes: 5 additions & 0 deletions 14_cmake_findpython/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mymodule
import numpy as np

A = (np.random.random([100]) * 100).astype(np.uint64)
assert np.all(A * 2 == mymodule.foo(A))
23 changes: 23 additions & 0 deletions 14_cmake_findpython/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

namespace py = pybind11;

template <class T>
inline T foo(T& arg)
{
T ret = arg;

for (auto& i : ret) {
i *= 2;
}

return ret;
}

PYBIND11_MODULE(mymodule, m)
{
m.doc() = "Module description";
m.def("foo", &foo<std::vector<int>>, "Function description", py::arg("arg"));
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [11_class-parent-child](#11_class-parent-child)
- [12_crtp](#12_crtp)
- [13_static_cast](#13_static_cast)
- [14_cmake_findpython](#14_cmake_findpython)

<!-- /MarkdownTOC -->

Expand Down Expand Up @@ -279,3 +280,11 @@ for example when the return type cannot be inferred.
In that case you can be explicit by `static_cast`ing a pointer to your function
More information can be found in [the documentation](https://pybind11.readthedocs.io/en/stable/classes.html?highlight=static_cast#overloaded-methods).

## [14_cmake_findpython](14_cmake_findpython)

This example features the use of the new *FindPython* in *CMake*.
In order to use it with *pybind11* one should take care to:

1. Call `find_package(Python REQUIRED COMPONENTS Interpreter Development ...)` before `find_package(pybind11 REQUIRED)`.
2. Using the command-line `cmake ... -DPython_EXECUTABLE=\`which python\`` to for *CMake* to use a specific *Python* version.

8 changes: 8 additions & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
channels:
- conda-forge
dependencies:
- cmake
- make
- pybind11
- python
- numpy