-
Notifications
You must be signed in to change notification settings - Fork 426
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
pyo3 python_calling_rust example #753
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2020 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@rules_python//python:defs.bzl", "py_test") | ||
load("@rules_rust//python:py_rust_library.bzl", "py_rust_library") | ||
|
||
py_rust_library( | ||
name = "hello_py", | ||
srcs = ["hello_py.rs"], | ||
edition = "2018", | ||
deps = ["//ffi/python_calling_rust/raze:pyo3"], | ||
) | ||
|
||
py_test( | ||
name = "test", | ||
srcs = ["test.py"], | ||
deps = [":hello_py"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use pyo3::prelude::*; | ||
use pyo3::wrap_pyfunction; | ||
|
||
#[pyfunction] | ||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> { | ||
Ok((a + b).to_string()) | ||
} | ||
|
||
#[pymodule] | ||
fn hello_py(_py: Python, m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from ffi.python_calling_rust.hello_py import sum_as_string | ||
|
||
assert sum_as_string(1, 1) == "2" | ||
print("Ok!") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
bzl_library( | ||
name = "rules", | ||
srcs = glob(["**/*.bzl"]), | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
mfarrugi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
load("@bazel_skylib//rules:copy_file.bzl", "copy_file") | ||
load("@rules_python//python:python.bzl", "py_library") | ||
load("@rules_rust//rust:rust.bzl", "rust_library") | ||
|
||
def py_rust_library(name, **kwargs): | ||
""" | ||
Wraps a rust shared library that implements a python native extension to be usable by @rules_python targets. | ||
|
||
Typically this is done through the use of a crate such as `pyo3` or `cpython`. | ||
|
||
Args: | ||
name (str): A unique name for this target. | ||
**kwargs: Passed directly to rust_library. | ||
""" | ||
|
||
mangled = name + "_mangled_so" | ||
rust_library( | ||
name = mangled, | ||
crate_type = "cdylib", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One problem with this macro is that rules_rust doesn't have the equivalent of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mfarrugi@a483a7f illustrates the issue, and fails like so:
The same thing can happen with the cc macro linked above if there is a dependency that can only be linked statically. Fixing this pedantically would require each py_binary rule to link all of its native extensions at the same time (or require them to be fully dynamically linked), which is far beyond the scope of this PR. |
||
rustc_flags = select({ | ||
"@rules_rust//rust/platform:osx": [ | ||
"--codegen=link-arg=-undefined", | ||
"--codegen=link-arg=dynamic_lookup", | ||
], | ||
"//conditions:default": [], | ||
}), | ||
**kwargs | ||
) | ||
|
||
unix = name + "_unix" | ||
copy_file( | ||
name = unix, | ||
src = mangled, | ||
out = name + ".so", | ||
) | ||
windows = name + "_windows" | ||
copy_file( | ||
name = windows, | ||
src = mangled, | ||
out = name + ".pyd", | ||
) | ||
py_library( | ||
name = name, | ||
data = select({ | ||
"@rules_rust//rust/platform:windows": [windows], | ||
"//conditions:default": [unix], | ||
}), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed. Perhaps it should be removed unless we're going to be documenting some python rules in the root package.