Skip to content

Commit 71e29a7

Browse files
committed
Adding CMake example with new findPython
1 parent df26eec commit 71e29a7

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

.github/workflows/ci.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
branches:
8+
- master
9+
10+
jobs:
11+
12+
standard:
13+
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
runs-on: [ubuntu-latest, macos-latest, windows-latest]
18+
example:
19+
- 14_cmake_findpython
20+
21+
defaults:
22+
run:
23+
shell: bash -l {0}
24+
25+
name: "${{ matrix.example }} • ${{ matrix.runs-on }}"
26+
runs-on: ${{ matrix.runs-on }}
27+
28+
steps:
29+
30+
- name: Basic GitHub action setup
31+
uses: actions/checkout@v2
32+
33+
- name: Set conda environment
34+
uses: mamba-org/provision-with-micromamba@main
35+
with:
36+
environment-file: environment.yaml
37+
environment-name: myenv
38+
39+
- name: Configure
40+
working-directory: ${{ matrix.example }}
41+
run: cmake -Bbuild -DPython_EXECUTABLE=`which python`
42+
43+
- name: Compile
44+
working-directory: ${{ matrix.example }}/build
45+
run: cmake --build .
46+
47+
- name: Example
48+
working-directory: ${{ matrix.example }}/build
49+
run: |
50+
cp ../example.py .
51+
python example.py

14_cmake_findpython/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.18..3.20)
2+
3+
project(mymodule)
4+
5+
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
6+
find_package(pybind11 REQUIRED CONFIG)
7+
8+
pybind11_add_module(mymodule main.cpp)
9+
target_link_libraries(mymodule PUBLIC pybind11::module Python::NumPy)
10+
11+
target_compile_definitions(mymodule PRIVATE VERSION_INFO=0.1.0)

14_cmake_findpython/example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import mymodule
2+
import numpy as np
3+
4+
A = (np.random.random([100]) * 100).astype(np.uint64)
5+
assert np.all(A * 2 == mymodule.foo(A))

14_cmake_findpython/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <vector>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/stl.h>
4+
5+
namespace py = pybind11;
6+
7+
template <class T>
8+
inline T foo(T& arg)
9+
{
10+
T ret = arg;
11+
12+
for (auto& i : ret) {
13+
i *= 2;
14+
}
15+
16+
return ret;
17+
}
18+
19+
PYBIND11_MODULE(mymodule, m)
20+
{
21+
m.doc() = "Module description";
22+
m.def("foo", &foo<std::vector<int>>, "Function description", py::arg("arg"));
23+
}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [09_numpy_cpp-custom-matrix](#09_numpy_cpp-custom-matrix)
2525
- [10_enum](#10_enum)
2626
- [11_class-parent-child](#11_class-parent-child)
27+
- [14_cmake_findpython](#14_cmake_findpython)
2728

2829
<!-- /MarkdownTOC -->
2930

@@ -265,3 +266,10 @@ This example contains a classical example where one or more classes are derived
265266
266267
This particular case requires more involved interface, as is described in [the documentation](http://pybind11.readthedocs.io/en/stable/advanced/classes.html).
267268
269+
## [14_cmake_findpython](14_cmake_findpython)
270+
271+
This example features the use of the new *FindPython* in *CMake*.
272+
In order to use it with *pybind11* one should take care to:
273+
274+
1. Call `find_package(Python REQUIRED COMPONENTS Interpreter Development ...)` before `find_package(pybind11 REQUIRED)`.
275+
2. Using the command-line `cmake ... -DPython_EXECUTABLE=\`which python\`` to for *CMake* to use a specific *Python* version.

environment.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
channels:
2+
- conda-forge
3+
dependencies:
4+
- cmake
5+
- make
6+
- pybind11
7+
- python
8+
- numpy

0 commit comments

Comments
 (0)