Skip to content

Commit b332517

Browse files
committed
Implement rules_rust_pyo3
1 parent 3a9e570 commit b332517

19 files changed

Lines changed: 657 additions & 39 deletions

MODULE.bazel

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ bazel_dep(name = "platforms", version = "1.1.0")
5757
bazel_dep(name = "protobuf", version = "34.0.bcr.1", repo_name = "com_google_protobuf")
5858
bazel_dep(name = "rules_cc", version = "0.2.8")
5959
bazel_dep(name = "rules_proto", version = "7.1.0")
60+
bazel_dep(name = "rules_python", version = "1.6.3")
6061
bazel_dep(name = "llvm", version = "0.7.7")
6162
bazel_dep(name = "aspect_tools_telemetry", version = "0.3.3")
6263

@@ -97,6 +98,19 @@ crate.from_cargo(
9798
"x86_64-pc-windows-msvc",
9899
],
99100
)
101+
crate.from_cargo(
102+
name = "rpyo3c",
103+
cargo_lock = "//rs/private/pyo3:Cargo.lock",
104+
cargo_toml = "//rs/private/pyo3:Cargo.toml",
105+
platform_triples = [
106+
"aarch64-apple-darwin",
107+
"aarch64-pc-windows-msvc",
108+
"aarch64-unknown-linux-gnu",
109+
"x86_64-apple-darwin",
110+
"x86_64-pc-windows-msvc",
111+
"x86_64-unknown-linux-gnu",
112+
],
113+
)
100114
crate.annotation(
101115
crate = "protoc-gen-prost",
102116
gen_binaries = ["protoc-gen-prost"],
@@ -105,11 +119,45 @@ crate.annotation(
105119
crate = "protoc-gen-tonic",
106120
gen_binaries = ["protoc-gen-tonic"],
107121
)
122+
crate.annotation(
123+
build_script_data = [
124+
"@rules_rust//extensions/pyo3:current_pyo3_toolchain",
125+
],
126+
build_script_env = {
127+
"PYO3_CROSS": "$(PYO3_CROSS)",
128+
"PYO3_CROSS_LIB_DIR": "$(PYO3_CROSS_LIB_DIR)",
129+
"PYO3_CROSS_PYTHON_IMPLEMENTATION": "$(PYO3_CROSS_PYTHON_IMPLEMENTATION)",
130+
"PYO3_CROSS_PYTHON_VERSION": "$(PYO3_CROSS_PYTHON_VERSION)",
131+
"PYO3_NO_PYTHON": "$(PYO3_NO_PYTHON)",
132+
"PYO3_PYTHON": "$(PYO3_PYTHON)",
133+
},
134+
build_script_toolchains = [
135+
"@rules_rust//extensions/pyo3:current_pyo3_toolchain",
136+
],
137+
crate = "pyo3-build-config",
138+
patch_args = ["-p1"],
139+
patches = ["@rules_rust//extensions/pyo3/3rdparty/patches:resolve_cross_compile_config_path.patch"],
140+
)
141+
crate.annotation(
142+
build_script_data = [
143+
"@rules_rust//extensions/pyo3:current_pyo3_toolchain",
144+
],
145+
build_script_env = {
146+
"PYO3_CROSS": "$(PYO3_CROSS)",
147+
"PYO3_CROSS_LIB_DIR": "$(PYO3_CROSS_LIB_DIR)",
148+
"PYO3_CROSS_PYTHON_IMPLEMENTATION": "$(PYO3_CROSS_PYTHON_IMPLEMENTATION)",
149+
"PYO3_CROSS_PYTHON_VERSION": "$(PYO3_CROSS_PYTHON_VERSION)",
150+
"PYO3_NO_PYTHON": "$(PYO3_NO_PYTHON)",
151+
"PYO3_PYTHON": "$(PYO3_PYTHON)",
152+
},
153+
build_script_toolchains = [
154+
"@rules_rust//extensions/pyo3:current_pyo3_toolchain",
155+
],
156+
crate = "pyo3-ffi",
157+
)
108158
use_repo(crate, "rrra")
109159
use_repo(crate, "rrprd")
110-
111-
rules_rust_prost = use_extension("//rs:rules_rust_prost.bzl", "rules_rust_prost")
112-
use_repo(rules_rust_prost, "rules_rust_prost")
160+
use_repo(crate, "rpyo3c")
113161

114162
bazel_dep(name = "rules_go", version = "0.59.0", dev_dependency = True)
115163
bazel_dep(name = "gazelle", version = "0.47.0", dev_dependency = True)
@@ -121,6 +169,13 @@ register_toolchains(
121169
dev_dependency = True,
122170
)
123171

172+
register_toolchains(
173+
"@rules_rust//extensions/prost:default_prost_toolchain",
174+
"@rules_rust//extensions/pyo3/toolchains:rust_toolchain",
175+
"@rules_rust//extensions/pyo3/toolchains:toolchain",
176+
dev_dependency = True,
177+
)
178+
124179
register_toolchains(
125180
"@llvm//toolchain:all",
126181
dev_dependency = True,

MODULE.bazel.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,59 @@ Overriding with a version that does not include required patches from [hermeticb
251251
</details>
252252

253253
<details>
254-
<summary>Protobuf with rules_rust_prost</summary>
254+
<summary>Protobuf with prost</summary>
255255

256-
`rules_rs` exports a `rules_rust_prost` module extension for prost integration:
256+
Load prost rules and default toolchains from the reexported `@rules_rust` repository:
257+
258+
```bzl
259+
load("@rules_rust//extensions/prost:defs.bzl", "rust_prost_library")
260+
```
257261

258262
```bzl
259263
bazel_dep(name = "rules_proto", version = "7.1.0")
260-
bazel_dep(name = "protobuf", version = "34.0.bcr.1", repo_name = "com_google_protobuf")
264+
bazel_dep(name = "protobuf", version = "34.0.bcr.1")
265+
266+
register_toolchains(
267+
"@rules_rust//extensions/prost:default_prost_toolchain",
268+
"@//path/to/proto_toolchain",
269+
)
270+
```
261271

262-
rules_rust_prost = use_extension("@rules_rs//rs:rules_rust_prost.bzl", "rules_rust_prost")
272+
If you need different prost, tonic, or plugin versions, define your own `rust_prost_toolchain` from `@rules_rust//extensions/prost:defs.bzl`.
273+
274+
`rules_rs` also exposes a `@rules_rust_prost` compatibility repository to ease migration of existing code:
275+
276+
```bzl
277+
rules_rust_prost = use_extension("//rs:rules_rust_prost.bzl", "rules_rust_prost")
263278
use_repo(rules_rust_prost, "rules_rust_prost")
279+
```
280+
281+
</details>
264282

265-
register_toolchains("@rules_rust_prost//:default_prost_toolchain")
266-
register_toolchains("@//path/to/proto_toolchain")
283+
<details>
284+
<summary>Python extensions with PyO3</summary>
285+
286+
Load PyO3 rules and default toolchains from the reexported `@rules_rust` repository:
287+
288+
```bzl
289+
load("@rules_rust//extensions/pyo3:defs.bzl", "pyo3_extension")
267290
```
268291

269-
The default prost toolchain and its Cargo dependencies are provided by `rules_rs`. If you need different prost, tonic, or plugin versions, define your own `rust_prost_toolchain` from `@rules_rust_prost//:defs.bzl`.
292+
```bzl
293+
register_toolchains(
294+
"@rules_rust//extensions/pyo3/toolchains:toolchain",
295+
"@rules_rust//extensions/pyo3/toolchains:rust_toolchain",
296+
)
297+
```
298+
299+
If you need different PyO3 versions or Python discovery behavior, define your own `pyo3_toolchain` or `rust_pyo3_toolchain` from `@rules_rust//extensions/pyo3:defs.bzl`.
300+
301+
`rules_rs` also exposes a `@rules_rust_pyo3` compatibility repository to ease migration of existing cod:
302+
303+
```bzl
304+
rules_rust_pyo3 = use_extension("//rs:rules_rust_pyo3.bzl", "rules_rust_pyo3")
305+
use_repo(rules_rust_pyo3, "rules_rust_pyo3")
306+
```
270307

271308
</details>
272309

rs/BUILD.bazel

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ bzl_library(
1515
name = "rules_rust_prost",
1616
srcs = ["rules_rust_prost.bzl"],
1717
visibility = ["//visibility:public"],
18-
deps = [
19-
"@bazel_tools//tools/build_defs/repo:local.bzl",
20-
],
18+
)
19+
20+
bzl_library(
21+
name = "rules_rust_pyo3",
22+
srcs = ["rules_rust_pyo3.bzl"],
23+
visibility = ["//visibility:public"],
2124
)
2225

2326
bzl_library(

rs/private/prost/BUILD.bazel

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
2+
load("@rules_rust//extensions/prost:defs.bzl", "rust_prost_toolchain")
23
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library_group")
3-
load("@rules_rust_prost//:defs.bzl", "rust_prost_toolchain")
44

55
package(default_visibility = ["//visibility:public"])
66

@@ -11,6 +11,24 @@ exports_files([
1111
"Cargo.toml",
1212
])
1313

14+
[
15+
alias(
16+
name = name,
17+
actual = "@rrprd//:" + name,
18+
)
19+
for name in [
20+
"h2",
21+
"prost",
22+
"prost-types",
23+
"protoc-gen-prost__protoc-gen-prost",
24+
"protoc-gen-tonic__protoc-gen-tonic",
25+
"tokio",
26+
"tokio-stream",
27+
"tonic",
28+
"tonic-prost",
29+
]
30+
]
31+
1432
bool_flag(
1533
name = "compile_well_known_types",
1634
build_setting_default = True,
@@ -25,7 +43,7 @@ config_setting(
2543

2644
rust_binary(
2745
name = "protoc_wrapper",
28-
srcs = ["@rules_rust_prost//private:protoc_wrapper_source"],
46+
srcs = ["@rules_rust//extensions/prost/private:protoc_wrapper.rs"],
2947
edition = PROST_RUST_EDITION,
3048
tags = ["manual"],
3149
deps = [

rs/private/pyo3/BUILD.bazel

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("@rules_rust//extensions/pyo3:defs.bzl", "rust_pyo3_toolchain")
2+
load(
3+
"@rules_rust//extensions/pyo3/private:pyo3_toolchain.bzl",
4+
"current_rust_pyo3_introspection_toolchain",
5+
"current_rust_pyo3_toolchain",
6+
)
7+
load("@rules_rust//rust:defs.bzl", "rust_binary")
8+
9+
package(default_visibility = ["//visibility:public"])
10+
11+
exports_files([
12+
"Cargo.lock",
13+
"Cargo.toml",
14+
])
15+
16+
current_rust_pyo3_toolchain(
17+
name = "current_rust_pyo3_toolchain",
18+
tags = [
19+
"no-clippy",
20+
"no-rustfmt",
21+
],
22+
)
23+
24+
current_rust_pyo3_introspection_toolchain(
25+
name = "current_rust_pyo3_introspection_toolchain",
26+
tags = [
27+
"no-clippy",
28+
"no-rustfmt",
29+
],
30+
)
31+
32+
rust_pyo3_toolchain(
33+
name = "rust_pyo3_toolchain",
34+
pyo3 = "@rpyo3c//:pyo3",
35+
pyo3_introspection = "@rpyo3c//:pyo3-introspection",
36+
)
37+
38+
rust_binary(
39+
name = "stubgen",
40+
srcs = ["@rules_rust//extensions/pyo3/private:stubgen.rs"],
41+
edition = "2021",
42+
deps = [":current_rust_pyo3_introspection_toolchain"],
43+
)

0 commit comments

Comments
 (0)