From 71e29a7eba2924c5d5b2602c096b8b9e41210518 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Fri, 28 May 2021 09:29:30 +0200 Subject: [PATCH] Adding CMake example with new findPython --- .github/workflows/ci.yml | 51 ++++++++++++++++++++++++++++++ 14_cmake_findpython/CMakeLists.txt | 11 +++++++ 14_cmake_findpython/example.py | 5 +++ 14_cmake_findpython/main.cpp | 23 ++++++++++++++ README.md | 8 +++++ environment.yaml | 8 +++++ 6 files changed, 106 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 14_cmake_findpython/CMakeLists.txt create mode 100644 14_cmake_findpython/example.py create mode 100644 14_cmake_findpython/main.cpp create mode 100644 environment.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..792f15f --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/14_cmake_findpython/CMakeLists.txt b/14_cmake_findpython/CMakeLists.txt new file mode 100644 index 0000000..cb697d8 --- /dev/null +++ b/14_cmake_findpython/CMakeLists.txt @@ -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) diff --git a/14_cmake_findpython/example.py b/14_cmake_findpython/example.py new file mode 100644 index 0000000..6522b98 --- /dev/null +++ b/14_cmake_findpython/example.py @@ -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)) diff --git a/14_cmake_findpython/main.cpp b/14_cmake_findpython/main.cpp new file mode 100644 index 0000000..8f962bb --- /dev/null +++ b/14_cmake_findpython/main.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +namespace py = pybind11; + +template +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>, "Function description", py::arg("arg")); +} diff --git a/README.md b/README.md index 071cbd9..1dd4c12 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [09_numpy_cpp-custom-matrix](#09_numpy_cpp-custom-matrix) - [10_enum](#10_enum) - [11_class-parent-child](#11_class-parent-child) + - [14_cmake_findpython](#14_cmake_findpython) @@ -265,3 +266,10 @@ This example contains a classical example where one or more classes are derived This particular case requires more involved interface, as is described in [the documentation](http://pybind11.readthedocs.io/en/stable/advanced/classes.html). +## [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. diff --git a/environment.yaml b/environment.yaml new file mode 100644 index 0000000..1ee036d --- /dev/null +++ b/environment.yaml @@ -0,0 +1,8 @@ +channels: + - conda-forge +dependencies: + - cmake + - make + - pybind11 + - python + - numpy