Hey everyone! We're looking into using pants in ou...
# general
c
Hey everyone! We're looking into using pants in our monorepo. We have a rust package with PyO3 bindings that we then use in our python code (both in the same repo). Currently we just have a simple bash script to build the rust package, make a wheel with maturin and install it in the python enviroment. I was wondering if pants could build this package and install it in its sandbox. And also notice when the rust package changes so that it knows it has to rebuild it?
b
Off the top of my head, there's a few possible routes: • build the rust parts outside Pants and import via a
file(...)
target or similar (probably won't be a great experience, but might be first step) • get something going with the
shell_command
or
adhoc_tool
targets • use @gorgeous-winter-99296's plugin for rust/cargo: https://github.com/pantsbuild/pants/discussions/20119. Using a proper plugin like the last will give the best experience, but I don't know how much support it has for tools like maturin, yet!
g
Adding support for a maturin as a tool isn't hard, problem I think is Pants won't consume it as a wheel. I have a branch where I've hacked on building native extensions from Rust but it's a bit hacky to get the ABI right. And it definitely wouldn't work with cross-platform publishing etc, as I haven't found a satisfactory workflow in general for cross-compilation.
c
thnx for all the info! I think we'll start by looking into
shell_command
or
adhoc_tool
to get a working proof of concept. We can then maybe look into adapting the cargo plugin or making a new plugin to handle this a bit cleaner
g
Sounds like a plan! If you want to try your hand at building a plugin have a look at #C01CQHVDMMW. I'd love to get this properly supported in my plugin as well, so feel free to open an issue and/or PR there if you want, and we can look at it together.
👍 1
f
we have a working maturing pyo3 wheel publishing in out pants repo. id need to defer to one of our team members to know the details @modern-smartphone-82928
it's was / is for cross platform (win and linux)
m
Heyo, just saw. So, we were able to build/package python wheels with the rust PyO3 binding with pants. we have a pyproject.toml,
Copy code
[build-system]
requires = ["maturin==1.5.0"]
build-backend = "maturin"

[tool.maturin]
features = [
    "pyo3/extension-module",
]
The python_distribution is also parametrized with the following.
Copy code
**parametrize("windows-gnu", env_vars = ["RUSTUP_HOME", "CARGO_BUILD_TARGET=x86_64-pc-windows-gnu"]),
    **parametrize("linux", env_vars = ["RUSTUP_HOME", "CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu"])
In our Cargo.toml
Copy code
pyo3 = { version = "0.20.3", features = ["extension-module", "abi3-py310", "generate-import-lib"] }
producing wheels
Copy code
<package>-cp310-abi3-linux_x86_64.whl
<package>-cp310-abi3-win_amd64.whl
Hope this helps.
Resources are then archived, which can then be used to rebuild when there are changes in python/rust.
Ultimately, just using the maturin build-backend, was satisfactory at the moment. https://github.com/PyO3/maturin/blob/main/maturin/__init__.py
c
hey, thank you for all the info! I know it's been a while but I just got back into looking into this. I implemented the following which correctly generates the wheel file.
Copy code
files(
    name="files_relust",
    sources=["./Cargo.toml",
        "./pyproject.toml",
        "./Cargo.lock",
        './src/**/*.rs',],
)

adhoc_tool(
    name="build_relust",
    runnable="//:reqs#maturin",
    description="A Rust library for Python",
    execution_dependencies=[
        "//:bash",
        "//:reqs#maturin",
        ":files_relust",
        "//:cargo",
        "//:rustc",
    ],
    args=["build", "--release", "-i", "python3.11"],
    root_output_directory=".",
    output_files=["./target/wheels/relust-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl"],
    log_output=True,
)
I'm just struggling with installing the generated wheel file. This doesn't work because I can only use absolute paths
Copy code
python_requirement(
    name="relust",
    requirements=["relust @ file://./target/wheels/relust-0.1.0-cp311-cp311-manylinux_2_28_x86_64.whl"],
    dependencies=[":build_relust"],
    modules=["relust"],
)
Should I implement a plugin similar to
experimental_wrap_as_python_sources
to achieve this? Thanks a lot!!