Skip to content

Commit 1043735

Browse files
authored
feat: add CcInfo to allowed providers of py_library deps. (#550)
Hi Aspect Team This PR enables better compatibility with pybind11_bazel and makes the aspect py rules more of a drop-in replacement of the official rules_python. Please let me know if you are open to this contribution, then I can also add tests for it. More specifically: This change is useful when using a [pybind_extension](https://github.com/pybind/pybind11_bazel/blob/2b6082a4d9d163a52299718113fa41e4b7978db5/build_defs.bzl#L28) in the `deps` of a `py_libary` as the `pybind_extension` is just a `cc_binary` under the hood. This PR enables using it like so: ```py load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") load("@aspect_rules_python//python:defs.bzl", aspect_py_library="py_library") pybind_extension( name = "my_pybind_extension", srcs = ["my_binding.cpp"], ) aspect_py_library( name = "my_python_library", srcs = [":my_python_library.py"], deps = [":my_pybind_extension"], ) ``` The `py_libary` from the official `rules_python` also already supports using `CcInfo` providers in the `deps` attribute. Without this change a workaround is to wrap the `pybind_extension` in a `py_library` from the official `rules_python`: ```py load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") load("@rules_python//python:defs.bzl", official_py_library="py_library") load("@aspect_rules_python//python:defs.bzl", aspect_py_library="py_library") pybind_extension( name = "my_pybind_extension", srcs = ["my_binding.cpp"], ) official_py_library( name = "my_pybind_extension_wrapper", srcs = [], deps = [":my_pybind_extension"], ) aspect_py_library( name = "my_python_library", srcs = [":my_python_library.py"], deps = [":my_pybind_extension_wrapper"], ) ``` --- ### Changes are visible to end-users: no ### Test plan TODO
1 parent 89b0712 commit 1043735

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

py/private/py_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ _attrs = dict({
205205
),
206206
"deps": attr.label_list(
207207
doc = "Targets that produce Python code, commonly `py_library` rules.",
208-
providers = [[PyInfo], [PyVirtualInfo]],
208+
providers = [[PyInfo], [PyVirtualInfo], [CcInfo]],
209209
),
210210
"data": attr.label_list(
211211
doc = """Runtime dependencies of the program.

py/tests/cc-deps/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("//py:defs.bzl", "py_test")
2+
3+
# Test that cc targets can be used as deps for py targets.
4+
# We only add a very simple test to not pull in all the deps
5+
# to build proper py-bindings.
6+
cc_library(
7+
name = "example_library",
8+
srcs = ["example_library.cpp"],
9+
)
10+
11+
py_test(
12+
name = "test_smoke",
13+
srcs = ["test_smoke.py"],
14+
deps = [":example_library"],
15+
)

py/tests/cc-deps/example_library.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
int add(int i, int j) { return i + j; }
3+

py/tests/cc-deps/test_smoke.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def main():
2+
print("Running successfully!")
3+
4+
if __name__ == "__main__":
5+
main()

0 commit comments

Comments
 (0)