From 80f7acf8078f8b8a391a92ff67743ffa23401cbb Mon Sep 17 00:00:00 2001
From: Hameer Abbasi <2190658+hameerabbasi@users.noreply.github.com>
Date: Tue, 17 May 2022 07:33:36 +0200
Subject: [PATCH 1/2] Add example for NumPy and FindPython.

---
 projects/hello-numpy/CMakeLists.txt           | 19 ++++++++++++++
 projects/hello-numpy/README.md                | 25 +++++++++++++++++++
 projects/hello-numpy/pyproject.toml           | 12 +++++++++
 projects/hello-numpy/setup.py                 | 13 ++++++++++
 projects/hello-numpy/src/hello/__init__.py    |  4 +++
 projects/hello-numpy/src/hello/hello_py.cpp   | 21 ++++++++++++++++
 .../hello-numpy/tests/test_hello_numpy.py     | 22 ++++++++++++++++
 projects/hello-numpy/tox.ini                  |  2 ++
 8 files changed, 118 insertions(+)
 create mode 100644 projects/hello-numpy/CMakeLists.txt
 create mode 100644 projects/hello-numpy/README.md
 create mode 100644 projects/hello-numpy/pyproject.toml
 create mode 100644 projects/hello-numpy/setup.py
 create mode 100644 projects/hello-numpy/src/hello/__init__.py
 create mode 100644 projects/hello-numpy/src/hello/hello_py.cpp
 create mode 100644 projects/hello-numpy/tests/test_hello_numpy.py
 create mode 100644 projects/hello-numpy/tox.ini

diff --git a/projects/hello-numpy/CMakeLists.txt b/projects/hello-numpy/CMakeLists.txt
new file mode 100644
index 0000000..4e5f595
--- /dev/null
+++ b/projects/hello-numpy/CMakeLists.txt
@@ -0,0 +1,19 @@
+cmake_minimum_required(VERSION 3.18...3.22)
+
+project(hello-numpy VERSION "0.1")
+
+# Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
+include(GNUInstallDirs)
+include(FetchContent)
+
+find_package(Python COMPONENTS Development NumPy)
+
+set(python_module_name _hello)
+pybind11_add_module(${python_module_name} MODULE
+  src/hello/hello_py.cpp
+  )
+
+install(TARGETS ${python_module_name} DESTINATION .)
+
+# Quiet a warning, since this project is only valid with SKBUILD
+set(ignoreMe "${SKBUILD}")
diff --git a/projects/hello-numpy/README.md b/projects/hello-numpy/README.md
new file mode 100644
index 0000000..92e0e01
--- /dev/null
+++ b/projects/hello-numpy/README.md
@@ -0,0 +1,25 @@
+# PyBind11 + Scikit Build example
+
+
+## Building
+
+To build, you must have pip 10 or greater, *or* you need to manually install
+`scikit-build` and `cmake`. Once you create a wheel, that wheel can be used in
+earlier versions of pip.
+
+Example build and install sequence:
+
+```bash
+pip install .
+python -c "import hello; hello.hello()"
+```
+
+This should print "Hello, World!".
+
+## Testing
+
+Testing is managed by tox. This will build the package in a temp directory and runs the tests in the test dir.
+
+```shell
+tox
+```
diff --git a/projects/hello-numpy/pyproject.toml b/projects/hello-numpy/pyproject.toml
new file mode 100644
index 0000000..bedab52
--- /dev/null
+++ b/projects/hello-numpy/pyproject.toml
@@ -0,0 +1,12 @@
+[build-system]
+requires = [
+    "setuptools",
+    "scikit-build>=0.15",
+    "cmake",
+    "ninja",
+    "numpy>=1.21",
+]
+build-backend = "setuptools.build_meta"
+
+[tool.pytest.ini_options]
+testpaths = ["tests"]
diff --git a/projects/hello-numpy/setup.py b/projects/hello-numpy/setup.py
new file mode 100644
index 0000000..9d8ae0a
--- /dev/null
+++ b/projects/hello-numpy/setup.py
@@ -0,0 +1,13 @@
+from skbuild import setup
+
+setup(
+    name="hello-numpy",
+    version="1.2.3",
+    description="a minimal example package (with pybind11 and NumPy)",
+    author='Hameer Abbasi',
+    license="MIT",
+    packages=['hello'],
+    package_dir={'': 'src'},
+    cmake_install_dir='src/hello',
+    python_requires='>=3.7',
+)
diff --git a/projects/hello-numpy/src/hello/__init__.py b/projects/hello-numpy/src/hello/__init__.py
new file mode 100644
index 0000000..e558081
--- /dev/null
+++ b/projects/hello-numpy/src/hello/__init__.py
@@ -0,0 +1,4 @@
+from ._hello import hello, return_two
+
+
+__all__ = ("hello", "return_two")
diff --git a/projects/hello-numpy/src/hello/hello_py.cpp b/projects/hello-numpy/src/hello/hello_py.cpp
new file mode 100644
index 0000000..912f594
--- /dev/null
+++ b/projects/hello-numpy/src/hello/hello_py.cpp
@@ -0,0 +1,21 @@
+#include <iostream>
+#include <pybind11/pybind11.h>
+#include <numpy/arrayobject.h>
+
+namespace py = pybind11;
+
+void hello() {
+    std::cout << "Hello, World!" << std::endl;
+}
+
+py::object zeros2x2() {
+    return py::reinterpret_steal<py::object>(
+        PyArray_ZEROS(2, {2, 2}, NPY_FLOAT64, 0);
+    );
+}
+
+PYBIND11_MODULE(_hello, m) {
+    m.doc() = "_hello";
+    m.def("hello", &hello, "Prints \"Hello, World!\"");
+    m.def("zeros2x2", &zeros2x2, "Returns a 2x2 array of zeros.");
+}
diff --git a/projects/hello-numpy/tests/test_hello_numpy.py b/projects/hello-numpy/tests/test_hello_numpy.py
new file mode 100644
index 0000000..d9667ee
--- /dev/null
+++ b/projects/hello-numpy/tests/test_hello_numpy.py
@@ -0,0 +1,22 @@
+# This kind of import is automatically done when importing hello from outside
+import hello
+import unittest
+import numpy as np
+
+
+class TestHello(unittest.TestCase):
+    def test_hello(self):
+        hello.hello()
+
+    def test_return_two(self):
+        expected = np.zeros((2, 2), dtype=np.float64)
+        actual = hello.zeros2x2()
+        self.assertEqual(expected.dtype, actual.dtype)
+        self.assert_(np.all(expected == actual))
+
+
+if __name__ == "__main__":
+    unittest.main()
+    # You can run all python test with:
+    # ctest -R python -V
+    # from the build folder
diff --git a/projects/hello-numpy/tox.ini b/projects/hello-numpy/tox.ini
new file mode 100644
index 0000000..311cae8
--- /dev/null
+++ b/projects/hello-numpy/tox.ini
@@ -0,0 +1,2 @@
+[testenv]
+commands = python -m unittest discover -s tests/

From 51a4a98102ac71fefac6a840df1aa603f5780cae Mon Sep 17 00:00:00 2001
From: Henry Schreiner <henryschreineriii@gmail.com>
Date: Fri, 18 Nov 2022 00:24:54 -0500
Subject: [PATCH 2/2] fix: trying to fix broken example

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
---
 noxfile.py                          |  2 +-
 projects/hello-numpy/CMakeLists.txt | 10 ++++++----
 projects/hello-numpy/pyproject.toml |  3 ++-
 projects/hello-numpy/setup.py       |  1 +
 4 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/noxfile.py b/noxfile.py
index 9a503fa..64b86c4 100644
--- a/noxfile.py
+++ b/noxfile.py
@@ -3,7 +3,7 @@
 import nox
 
 
-hello_list = ["hello-pure", "hello-cpp", "hello-pybind11", "hello-cython"]
+hello_list = ["hello-pure", "hello-cpp", "hello-pybind11", "hello-cython", "hello-numpy"]
 if not sys.platform.startswith("win"):
     hello_list.append("hello-cmake-package")
 long_hello_list = hello_list + ["pen2-cython"]
diff --git a/projects/hello-numpy/CMakeLists.txt b/projects/hello-numpy/CMakeLists.txt
index 4e5f595..2b6d53f 100644
--- a/projects/hello-numpy/CMakeLists.txt
+++ b/projects/hello-numpy/CMakeLists.txt
@@ -4,14 +4,16 @@ project(hello-numpy VERSION "0.1")
 
 # Define CMAKE_INSTALL_xxx: LIBDIR, INCLUDEDIR
 include(GNUInstallDirs)
-include(FetchContent)
 
-find_package(Python COMPONENTS Development NumPy)
+find_package(Python COMPONENTS Interpreter Development.Module NumPy)
+
+find_package(pybind11 CONFIG REQUIRED)
 
 set(python_module_name _hello)
 pybind11_add_module(${python_module_name} MODULE
-  src/hello/hello_py.cpp
-  )
+    src/hello/hello_py.cpp
+)
+target_link_libraries(${python_module_name} PRIVATE Python::NumPy)
 
 install(TARGETS ${python_module_name} DESTINATION .)
 
diff --git a/projects/hello-numpy/pyproject.toml b/projects/hello-numpy/pyproject.toml
index bedab52..bd25656 100644
--- a/projects/hello-numpy/pyproject.toml
+++ b/projects/hello-numpy/pyproject.toml
@@ -4,7 +4,8 @@ requires = [
     "scikit-build>=0.15",
     "cmake",
     "ninja",
-    "numpy>=1.21",
+    "pybind11-global",
+    "oldest-supported-numpy",
 ]
 build-backend = "setuptools.build_meta"
 
diff --git a/projects/hello-numpy/setup.py b/projects/hello-numpy/setup.py
index 9d8ae0a..b06c454 100644
--- a/projects/hello-numpy/setup.py
+++ b/projects/hello-numpy/setup.py
@@ -9,5 +9,6 @@
     packages=['hello'],
     package_dir={'': 'src'},
     cmake_install_dir='src/hello',
+    install_requires=["numpy"],
     python_requires='>=3.7',
 )