Added basic example of using pybind11 (#86)
* added basic example of using pybind11
Signed-off-by: Ross <rocebxyz@gmail.com>
* cleaned up module.bazel and removed Workspace
Signed-off-by: Ross <rocebxyz@gmail.com>
* cleaned up module.bazel and lock file
Signed-off-by: Ross <rocebxyz@gmail.com>
* renamed cpp and py files, and simplifed the example
Signed-off-by: Ross <rocebxyz@gmail.com>
* updated build.bazel to be easier to understand and simplier
Signed-off-by: Ross <rocebxyz@gmail.com>
* removed python toolchain
Signed-off-by: Ross <rocebxyz@gmail.com>
---------
Signed-off-by: Ross <rocebxyz@gmail.com>
Co-authored-by: Ross <rocebxyz@gmail.com>
diff --git a/examples/basic/BUILD.bazel b/examples/basic/BUILD.bazel
new file mode 100644
index 0000000..b58ddd3
--- /dev/null
+++ b/examples/basic/BUILD.bazel
@@ -0,0 +1,17 @@
+load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
+load("@rules_python//python:defs.bzl", "py_library", "py_test")
+
+pybind_extension(
+ name = "basic",
+ srcs = ["basic.cpp"],
+)
+py_library(
+ name = "basic_lib",
+ data = [":basic"],
+ imports = ["."],
+)
+py_test(
+ name = "basic_test",
+ srcs = ["basic_test.py"],
+ deps = [":basic_lib"],
+)
diff --git a/examples/basic/MODULE.bazel b/examples/basic/MODULE.bazel
new file mode 100644
index 0000000..d049a6f
--- /dev/null
+++ b/examples/basic/MODULE.bazel
@@ -0,0 +1,3 @@
+bazel_dep(name = "pybind11_bazel", version = "2.12.0")
+bazel_dep(name = "rules_python", version = "0.33.1")
+
diff --git a/examples/basic/basic.cpp b/examples/basic/basic.cpp
new file mode 100644
index 0000000..bd3d35c
--- /dev/null
+++ b/examples/basic/basic.cpp
@@ -0,0 +1,10 @@
+#include <pybind11/pybind11.h>
+
+int add(int i, int j) {
+ return i + j;
+}
+
+PYBIND11_MODULE(basic, module) {
+ module.doc() = "A basic pybind11 extension";
+ module.def("add", &add, "A function that adds two numbers");
+}
diff --git a/examples/basic/basic_test.py b/examples/basic/basic_test.py
new file mode 100644
index 0000000..6219734
--- /dev/null
+++ b/examples/basic/basic_test.py
@@ -0,0 +1,13 @@
+import unittest
+
+import basic
+
+
+class TestBasic(unittest.TestCase):
+ def test_add(self):
+ self.assertEqual(basic.add(1, 2), 3)
+ self.assertEqual(basic.add(2, 2), 4)
+
+
+if __name__ == "__main__":
+ unittest.main()