build(python): align toolchain with monorepo pattern
This commit is contained in:
24
MODULE.bazel
24
MODULE.bazel
@@ -7,29 +7,11 @@ bazel_dep(name = "bazel_skylib", version = "1.8.2")
|
|||||||
bazel_dep(name = "rules_python", version = "1.6.3")
|
bazel_dep(name = "rules_python", version = "1.6.3")
|
||||||
bazel_dep(name = "rules_rust", version = "0.74.0")
|
bazel_dep(name = "rules_rust", version = "0.74.0")
|
||||||
|
|
||||||
local_runtime_repo = use_repo_rule(
|
python = use_extension("//bazel:python.bzl", "python")
|
||||||
"@rules_python//python/local_toolchains:repos.bzl",
|
use_repo(python, "python")
|
||||||
"local_runtime_repo",
|
|
||||||
)
|
|
||||||
local_runtime_toolchains_repo = use_repo_rule(
|
|
||||||
"@rules_python//python/local_toolchains:repos.bzl",
|
|
||||||
"local_runtime_toolchains_repo",
|
|
||||||
)
|
|
||||||
|
|
||||||
local_runtime_repo(
|
|
||||||
name = "local_python3",
|
|
||||||
interpreter_path = "python3",
|
|
||||||
on_failure = "fail",
|
|
||||||
dev_dependency = True,
|
|
||||||
)
|
|
||||||
local_runtime_toolchains_repo(
|
|
||||||
name = "local_python_toolchains",
|
|
||||||
runtimes = ["local_python3"],
|
|
||||||
dev_dependency = True,
|
|
||||||
)
|
|
||||||
|
|
||||||
rust = use_extension("//bazel:rust.bzl", "rust")
|
rust = use_extension("//bazel:rust.bzl", "rust")
|
||||||
use_repo(rust, "rust")
|
use_repo(rust, "rust")
|
||||||
|
|
||||||
register_toolchains("@local_python_toolchains//:all", dev_dependency = True)
|
register_toolchains("@python//:python")
|
||||||
register_toolchains("@rust//:rust")
|
register_toolchains("@rust//:rust")
|
||||||
|
|||||||
17
MODULE.bazel.lock
generated
17
MODULE.bazel.lock
generated
@@ -146,6 +146,23 @@
|
|||||||
},
|
},
|
||||||
"selectedYankedVersions": {},
|
"selectedYankedVersions": {},
|
||||||
"moduleExtensions": {
|
"moduleExtensions": {
|
||||||
|
"//bazel:python.bzl%python": {
|
||||||
|
"general": {
|
||||||
|
"bzlTransitiveDigest": "CbGiMYJXjcea8dmoNxUWe/lWOjqRWfQPTXFHUUMheIY=",
|
||||||
|
"usagesDigest": "bcY43VsTrhTJ8N4Dj5FtsgMhZxLyKKjHP+wWK4ntJQ4=",
|
||||||
|
"recordedFileInputs": {},
|
||||||
|
"recordedDirentsInputs": {},
|
||||||
|
"envVariables": {},
|
||||||
|
"generatedRepoSpecs": {
|
||||||
|
"python": {
|
||||||
|
"bzlFile": "@@//bazel:python.bzl",
|
||||||
|
"ruleClassName": "python_repository",
|
||||||
|
"attributes": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"recordedRepoMappingEntries": []
|
||||||
|
}
|
||||||
|
},
|
||||||
"//bazel:rust.bzl%rust": {
|
"//bazel:rust.bzl%rust": {
|
||||||
"general": {
|
"general": {
|
||||||
"bzlTransitiveDigest": "4kUzQyjRjzchWIBZJKkNs8LuO4F+g/DwuwtTa0RTPcU=",
|
"bzlTransitiveDigest": "4kUzQyjRjzchWIBZJKkNs8LuO4F+g/DwuwtTa0RTPcU=",
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
exports_files([
|
exports_files([
|
||||||
|
"python.BUILD.template",
|
||||||
|
"python.bzl",
|
||||||
"rust.BUILD.template",
|
"rust.BUILD.template",
|
||||||
"rust.bzl",
|
"rust.bzl",
|
||||||
"utils.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,
|
||||||
|
)
|
||||||
@@ -13,6 +13,8 @@ in
|
|||||||
options.modules.shell.python.enable = lib.mkEnableOption "Python tools for the primary user's shell environment";
|
options.modules.shell.python.enable = lib.mkEnableOption "Python tools for the primary user's shell environment";
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
|
registry.env-var.${config.networking.hostName}.PYTHON_TOOLCHAIN_ROOT = "${pkgs.python3}";
|
||||||
|
|
||||||
home-manager.users.${user}.home.packages = [ pkgs.python3 ];
|
home-manager.users.${user}.home.packages = [ pkgs.python3 ];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
4
third_party/python/README.md
vendored
4
third_party/python/README.md
vendored
@@ -1,7 +1,7 @@
|
|||||||
# Vendored Python packages
|
# Vendored Python packages
|
||||||
|
|
||||||
This package exposes vendored Python dependencies through stable public Bazel targets.
|
This package exposes vendored Python dependencies through stable public Bazel targets.
|
||||||
Bazel runs Python targets with the local `python3` runtime.
|
Bazel uses `PYTHON_TOOLCHAIN_ROOT` to find the monorepo `python3` runtime.
|
||||||
|
|
||||||
Project code depends on aliases in `//third_party/python`.
|
Project code depends on aliases in `//third_party/python`.
|
||||||
It does not depend on versioned package directories directly.
|
It does not depend on versioned package directories directly.
|
||||||
@@ -22,7 +22,7 @@ Use the upstream package name for public aliases.
|
|||||||
For command-line tools, add a `_bin` suffix to the binary target.
|
For command-line tools, add a `_bin` suffix to the binary target.
|
||||||
|
|
||||||
Preserve upstream license files and package metadata.
|
Preserve upstream license files and package metadata.
|
||||||
Keep local Bazel files separate from upstream source when possible.
|
Keep repository Bazel files separate from upstream source when possible.
|
||||||
|
|
||||||
Add optional dependencies as separate public targets.
|
Add optional dependencies as separate public targets.
|
||||||
Do not bundle optional dependencies into the primary package target.
|
Do not bundle optional dependencies into the primary package target.
|
||||||
|
|||||||
Reference in New Issue
Block a user