Skip to content

Commit db49396

Browse files
committed
wip
1 parent 4c33530 commit db49396

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

MODULE.bazel

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ use_repo(
173173
"build_bazel_bazel_self",
174174
)
175175

176-
# EXPERIMENTAL: This is experimental and may be removed without notice
177176
uv = use_extension("//python/uv:extensions.bzl", "uv")
177+
178+
# Here is how we can define platforms for the `uv` binaries - this will affect
179+
# all of the downstream callers because we are using the extension without
180+
# `dev_dependency = True`.
178181
uv.platform(
179182
name = "aarch64-apple-darwin",
180183
compatible_with = [
@@ -245,10 +248,13 @@ uv_dev = use_extension(
245248
"uv",
246249
dev_dependency = True,
247250
)
248-
uv_dev.toolchain(version = "0.5.24")
249-
use_repo(uv_dev, "uv_toolchains")
251+
uv_dev.toolchain(
252+
name = "uv_0_5_24",
253+
version = "0.5.24",
254+
)
255+
use_repo(uv_dev, "uv_0_5_24")
250256

251257
register_toolchains(
252-
"@uv_toolchains//:all",
258+
"@uv_0_5_24//:all",
253259
dev_dependency = True,
254260
)

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ use_repo(python, "python_3_10", "python_3_9", "python_versions", "pythons_hub")
106106

107107
# EXPERIMENTAL: This is experimental and may be removed without notice
108108
uv = use_extension("@rules_python//python/uv:extensions.bzl", "uv")
109-
uv.toolchain(uv_version = "0.4.25")
109+
uv.toolchain(version = "0.4.25")
110110
use_repo(uv, "uv_toolchains")
111111

112112
register_toolchains("@uv_toolchains//:all")

python/uv/BUILD.bazel

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ filegroup(
2727
visibility = ["//:__subpackages__"],
2828
)
2929

30-
# For stardoc to reference the files
31-
exports_files(["defs.bzl"])
32-
3330
toolchain_type(
3431
name = "uv_toolchain_type",
3532
visibility = ["//visibility:public"],
@@ -47,13 +44,6 @@ current_toolchain(
4744
],
4845
)
4946

50-
bzl_library(
51-
name = "defs",
52-
srcs = ["defs.bzl"],
53-
# EXPERIMENTAL: Visibility is restricted to allow for changes.
54-
visibility = ["//:__subpackages__"],
55-
)
56-
5747
bzl_library(
5848
name = "extensions",
5949
srcs = ["extensions.bzl"],
@@ -74,8 +64,11 @@ bzl_library(
7464
)
7565

7666
bzl_library(
77-
name = "toolchain",
78-
srcs = ["toolchain.bzl"],
67+
name = "uv_toolchain",
68+
srcs = ["uv_toolchain.bzl"],
7969
# EXPERIMENTAL: Visibility is restricted to allow for changes.
8070
visibility = ["//:__subpackages__"],
71+
deps = [
72+
"//python/uv/private:uv_toolchain",
73+
],
8174
)

python/uv/private/extension.bzl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ platform = tag_class(attrs = {
4848
})
4949

5050
uv_toolchain = tag_class(attrs = {
51-
"uv_version": attr.string(doc = "IGNORED", mandatory = False),
51+
"name": attr.string(
52+
doc = "The name of the toolchain repo",
53+
default = "uv_toolchains",
54+
),
5255
"version": attr.string(
5356
doc = "Explicit version of uv.",
5457
mandatory = True,
@@ -57,21 +60,24 @@ uv_toolchain = tag_class(attrs = {
5760

5861
def _uv_toolchain_extension(module_ctx):
5962
config = {
60-
"base_url": _DEFAULT_BASE_URL,
6163
"platforms": {},
62-
"version": None,
6364
}
6465

6566
for mod in module_ctx.modules:
66-
if not mod.is_root:
67-
# Ignore any attempts to configure the `uv` toolchain from non-root modules
67+
if not mod.is_root and not mod.name == "rules_python":
68+
# Only rules_python and the root module can configure this.
69+
#
70+
# Ignore any attempts to configure the `uv` toolchain elsewhere
6871
#
6972
# Only the root module may configure the uv toolchain.
7073
# This prevents conflicting registrations with any other modules.
7174
#
7275
# NOTE: We may wish to enforce a policy where toolchain configuration is only allowed in the root module, or in rules_python. See https://github.com/bazelbuild/bazel/discussions/22024
7376
continue
7477

78+
# Note, that the first registration will always win, givin priority to
79+
# the root module.
80+
7581
for platform_attr in mod.tags.platform:
7682
config["platforms"].setdefault(platform_attr.name, struct(
7783
name = platform_attr.name.replace("-", "_").lower(),
@@ -80,19 +86,22 @@ def _uv_toolchain_extension(module_ctx):
8086
))
8187

8288
for config_attr in mod.tags.config:
83-
config["base_url"] = config_attr.base_url
89+
config.setdefault("base_url", config_attr.base_url)
8490

8591
for toolchain in mod.tags.toolchain:
86-
config["version"] = toolchain.version
92+
config.setdefault("version", toolchain.version)
93+
config.setdefault("name", toolchain.name)
8794

8895
if not config["version"]:
8996
return
9097

98+
config.setdefault("base_url", _DEFAULT_BASE_URL)
9199
config["urls"] = _get_tool_urls_from_dist_manifest(
92100
module_ctx,
93101
base_url = "{base_url}/{version}".format(**config),
94102
)
95103
uv_register_toolchains(
104+
name = config["name"],
96105
platforms = config["platforms"],
97106
urls = config["urls"],
98107
version = config["version"],

python/uv/private/repositories.bzl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ uv_repository = repository_rule(
7676
},
7777
)
7878

79-
# buildifier: disable=unnamed-macro
80-
def uv_register_toolchains(*, version, platforms, urls):
79+
def uv_register_toolchains(*, name, version, platforms, urls):
8180
"""Convenience macro which does typical toolchain setup
8281
8382
Skip this macro if you need more control over the toolchain setup.
8483
8584
Args:
85+
name: The name of the toolchains repo,
8686
version: The uv toolchain version to download.
8787
platforms: The platforms to register uv for.
8888
urls: The urls with sha256 values to register uv for.
@@ -94,26 +94,26 @@ def uv_register_toolchains(*, version, platforms, urls):
9494
toolchain_labels_by_toolchain = {}
9595
toolchain_compatible_with_by_toolchain = {}
9696

97-
for platform in platforms.keys():
98-
uv_repository_name = platforms[platform].name
97+
for platform_name, platform in platforms.items():
98+
uv_repository_name = "{}_{}".format(name, platform_name.lower().replace("-", "_"))
9999
uv_repository(
100100
name = uv_repository_name,
101101
version = version,
102-
platform = platform,
103-
urls = urls[platform].urls,
104-
sha256 = urls[platform].sha256,
102+
platform = platform_name,
103+
urls = urls[platform_name].urls,
104+
sha256 = urls[platform_name].sha256,
105105
)
106106

107107
toolchain_name = uv_repository_name + "_toolchain"
108108
toolchain_names.append(toolchain_name)
109109
toolchain_labels_by_toolchain[toolchain_name] = "@{}//:uv_toolchain".format(uv_repository_name)
110110
toolchain_compatible_with_by_toolchain[toolchain_name] = [
111111
str(label)
112-
for label in platforms[platform].compatible_with
112+
for label in platform.compatible_with
113113
]
114114

115115
uv_toolchains_repo(
116-
name = "uv_toolchains",
116+
name = name,
117117
toolchain_type = str(UV_TOOLCHAIN_TYPE),
118118
toolchain_names = toolchain_names,
119119
toolchain_labels = toolchain_labels_by_toolchain,

0 commit comments

Comments
 (0)