Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rollforward): example: Add example for pybind11 #447

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module(
# py_image_layer requires 2.x for the `tar` rule.
# py_image_layer needs compute_unused_inputs attribute
# py_image_layer needs repo_mapping fix.
bazel_dep(name = "aspect_bazel_lib", version = "2.9.4")
bazel_dep(name = "aspect_bazel_lib", version = "2.9.4")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "rules_python", version = "0.29.0")
bazel_dep(name = "platforms", version = "0.0.7")
Expand Down Expand Up @@ -85,3 +85,5 @@ crate.from_cargo(
)

use_repo(crate, "crate_index")

bazel_dep(name = "pybind11_bazel", version = "2.13.6", dev_dependency = True)
19 changes: 19 additions & 0 deletions examples/pybind11/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_test")
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")

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"],
)
10 changes: 10 additions & 0 deletions examples/pybind11/basic.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
14 changes: 14 additions & 0 deletions examples/pybind11/basic_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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()
16 changes: 16 additions & 0 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,19 @@ def rules_py_internal_deps():
sha256 = "0523026398aea9c8b5f7a4a6d5c0829c285b4fbd960c17b5967a369342e21e01",
downloaded_file_path = "sqlparse-0.4.0-py3-none-any.whl",
)

http_archive(
name = "pybind11_bazel",
strip_prefix = "pybind11_bazel-2.13.6",
sha256 = "9df284330336958c837fb70dc34c0a6254dac52a5c983b3373a8c2bbb79ac35e",
urls = ["https://github.com/pybind/pybind11_bazel/archive/v2.13.6.zip"],
)

# We still require the pybind library.
http_archive(
name = "pybind11",
build_file = "@pybind11_bazel//:pybind11-BUILD.bazel",
strip_prefix = "pybind11-2.13.6",
urls = ["https://github.com/pybind/pybind11/archive/v2.13.6.zip"],
sha256 = "d0a116e91f64a4a2d8fb7590c34242df92258a61ec644b79127951e821b47be6",
)
Loading