build(python): align toolchain with monorepo pattern
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
exports_files([
|
||||
"python.BUILD.template",
|
||||
"python.bzl",
|
||||
"rust.BUILD.template",
|
||||
"rust.bzl",
|
||||
"utils.bzl",
|
||||
|
||||
25
bazel/python.BUILD.template
Normal file
25
bazel/python.BUILD.template
Normal file
@@ -0,0 +1,25 @@
|
||||
load("@rules_python//python:py_runtime.bzl", "py_runtime")
|
||||
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
|
||||
|
||||
py_runtime(
|
||||
name = "runtime",
|
||||
implementation_name = "cpython",
|
||||
interpreter_path = "{interpreter_path}",
|
||||
interpreter_version_info = {
|
||||
"major": "{major}",
|
||||
"minor": "{minor}",
|
||||
"micro": "{micro}",
|
||||
},
|
||||
python_version = "PY3",
|
||||
)
|
||||
|
||||
py_runtime_pair(
|
||||
name = "runtime_pair",
|
||||
py3_runtime = ":runtime",
|
||||
)
|
||||
|
||||
toolchain(
|
||||
name = "python",
|
||||
toolchain = ":runtime_pair",
|
||||
toolchain_type = "@rules_python//python:toolchain_type",
|
||||
)
|
||||
46
bazel/python.bzl
Normal file
46
bazel/python.bzl
Normal file
@@ -0,0 +1,46 @@
|
||||
load("//bazel:utils.bzl", "execute", "require_env")
|
||||
|
||||
|
||||
def _python_repository_impl(repository_ctx):
|
||||
root = repository_ctx.path(require_env(repository_ctx, "PYTHON_TOOLCHAIN_ROOT"))
|
||||
python3 = root.get_child("bin/python3")
|
||||
|
||||
version = execute(
|
||||
repository_ctx,
|
||||
[
|
||||
str(python3),
|
||||
"-c",
|
||||
"import sys; print('{}.{}.{}'.format(*sys.version_info[:3]))",
|
||||
],
|
||||
)
|
||||
parts = version.split(".")
|
||||
if len(parts) != 3:
|
||||
fail("python3 did not report a major.minor.micro version")
|
||||
|
||||
repository_ctx.file("WORKSPACE.bazel", "")
|
||||
repository_ctx.template(
|
||||
"BUILD.bazel",
|
||||
Label("//bazel:python.BUILD.template"),
|
||||
{
|
||||
"{interpreter_path}": str(python3),
|
||||
"{major}": parts[0],
|
||||
"{minor}": parts[1],
|
||||
"{micro}": parts[2],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
python_repository = repository_rule(
|
||||
implementation = _python_repository_impl,
|
||||
local = True,
|
||||
environ = ["PYTHON_TOOLCHAIN_ROOT"],
|
||||
)
|
||||
|
||||
|
||||
def _python_impl(module_ctx):
|
||||
python_repository(name = "python")
|
||||
|
||||
|
||||
python = module_extension(
|
||||
implementation = _python_impl,
|
||||
)
|
||||
Reference in New Issue
Block a user