pybind / pybind11_bazel

Bazel wrapper around the pybind11 repository
Other
100 stars 54 forks source link

Bzlmod python_interpreter_target usage #66

Closed mering closed 7 months ago

mering commented 9 months ago

How can I specify a specific Python toolchain with Bzlmod.

I have the following toolchain:

bazel_dep(name = "rules_python", version = "0.27.1")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    is_default = True,
    python_version = "3.11",
)
use_repo(python, "python_3_11")

It works to specify the toolchain via resolved repo name but this requires hard-coding all versions:

bazel_dep(name = "pybind11_bazel", version = "2.11.1")
python_configure = use_extension("@pybind11_bazel//:python_configure.bzl", "extension")
python_configure.toolchain(python_version = "3", python_interpreter_target = "@@rules_python~0.27.1~python~python_3_11_x86_64-unknown-linux-gnu//:bin/python3")
use_repo(python_configure, "local_config_python", "pybind11")

I tried using the following alias target but it got not resolved correctly:

bazel_dep(name = "pybind11_bazel", version = "2.11.1")
python_configure = use_extension("@pybind11_bazel//:python_configure.bzl", "extension")
python_configure.toolchain(python_version = "3", python_interpreter_target = "@python_3_11//:python3")
use_repo(python_configure, "local_config_python", "pybind11")

My guess would be that the following is not resolved correctly: https://github.com/pybind/pybind11_bazel/blob/23926b00e2b2eb2fc46b17e587cf0c0cfd2f2c4b/python_configure.bzl#L159-L160

Maybe this is because generated files are not supported by repository_ctx.path():

Returns a path from a string, label or path. If the path is relative, it will resolve relative to the repository directory. If the path is a label, it will resolve to the path of the corresponding file. Note that remote repositories are executed during the analysis phase and thus cannot depends on a target result (the label should point to a non-generated file). If path is a path, it will return that path as is.

junyer commented 9 months ago

I don't think pybind11_bazel can do better right now, but @rickeylev has (much) greater Bazel knowledge than I do, so there might be some way to get this working ahead of initial support for rules_python. :)

finn-ball commented 8 months ago

I think this may be a critical bug for those on multiple platforms:

"@@rules_python~0.27.1~python~python_3_11_x86_64-unknown-linux-gnu//:bin/python3"

Will not register for non-linux and non-x86_64 platforms.

junyer commented 8 months ago

Ping, @rickeylev!

junyer commented 7 months ago

As of the v2.11.1.bzl.2 release, pybind11_bazel uses rules_python. Please refer to the latter's documentation for details about registering toolchains.