Skip to content

Commit a52116b

Browse files
committed
bazel: getting ready for BCR
splitting our extension in 2 entry points, one specific to the toolchain and another to the package manager, this allows us to make proper dev_dependency splits in our MODULE.bazel
1 parent c9a9646 commit a52116b

File tree

3 files changed

+65
-40
lines changed

3 files changed

+65
-40
lines changed

MODULE.bazel

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ module(
77
compatibility_level = 0,
88
)
99

10-
# registers toolchain for consumers of the repo
11-
bazeldnf = use_extension("//bazeldnf:extensions.bzl", "bazeldnf")
12-
bazeldnf.toolchain()
13-
use_repo(bazeldnf, "bazeldnf_toolchains")
10+
bazeldnf_toolchain = use_extension("//bazeldnf:extensions.bzl", "bazeldnf_toolchain")
11+
bazeldnf_toolchain.register()
12+
use_repo(
13+
bazeldnf_toolchain,
14+
"bazeldnf_toolchains",
15+
)
1416

1517
register_toolchains("@bazeldnf_toolchains//:all")
1618

@@ -40,7 +42,6 @@ use_repo(
4042
)
4143

4244
# DEV DEPENDENCIES
43-
4445
bazeldnf_dev = use_extension("//bazeldnf:extensions.bzl", "bazeldnf", dev_dependency = True)
4546
bazeldnf_dev.rpm(
4647
name = "bazeldnf_internal_abseil-cpp-devel",

bazeldnf/extensions.bzl

+56-34
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,60 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
1010
load("//internal:rpm.bzl", rpm_repository = "rpm")
1111
load(":repositories.bzl", "bazeldnf_register_toolchains")
1212

13+
_DEFAULT_NAME = "bazeldnf"
14+
15+
def _bazeldnf_toolchain_extension(module_ctx):
16+
repos = []
17+
for mod in module_ctx.modules:
18+
for toolchain in mod.tags.register:
19+
if toolchain.name != _DEFAULT_NAME and not mod.is_root:
20+
fail("""\
21+
Only the root module may override the default name for the bazeldnf toolchain.
22+
This prevents conflicting registrations in the global namespace of external repos.
23+
""")
24+
if mod.is_root and toolchain.disable:
25+
break
26+
bazeldnf_register_toolchains(
27+
name = toolchain.name,
28+
register = False,
29+
)
30+
if mod.is_root:
31+
repos.append(toolchain.name + "_toolchains")
32+
33+
kwargs = {}
34+
if bazel_features.external_deps.extension_metadata_has_reproducible:
35+
kwargs["reproducible"] = True
36+
37+
if module_ctx.root_module_has_non_dev_dependency:
38+
kwargs["root_module_direct_deps"] = repos
39+
kwargs["root_module_direct_dev_deps"] = []
40+
else:
41+
kwargs["root_module_direct_deps"] = []
42+
kwargs["root_module_direct_dev_deps"] = repos
43+
44+
return module_ctx.extension_metadata(**kwargs)
45+
46+
_toolchain_tag = tag_class(
47+
attrs = {
48+
"name": attr.string(
49+
doc = """\
50+
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
51+
Overriding the default is only permitted in the root module.
52+
""",
53+
default = _DEFAULT_NAME,
54+
),
55+
"disable": attr.bool(default = False),
56+
},
57+
doc = "Allows registering a prebuilt bazeldnf toolchain",
58+
)
59+
60+
bazeldnf_toolchain = module_extension(
61+
implementation = _bazeldnf_toolchain_extension,
62+
tag_classes = {
63+
"register": _toolchain_tag,
64+
},
65+
)
66+
1367
_ALIAS_TEMPLATE = """\
1468
alias(
1569
name = "{name}",
@@ -32,8 +86,6 @@ _alias_repository = repository_rule(
3286
},
3387
)
3488

35-
_DEFAULT_NAME = "bazeldnf"
36-
3789
def _handle_lock_file(lock_file, module_ctx):
3890
content = module_ctx.read(lock_file)
3991
lock_file_json = json.decode(content)
@@ -58,25 +110,10 @@ def _handle_lock_file(lock_file, module_ctx):
58110

59111
return name
60112

61-
def _toolchain_extension(module_ctx):
113+
def _bazeldnf_extension(module_ctx):
62114
repos = []
63115

64116
for mod in module_ctx.modules:
65-
for toolchain in mod.tags.toolchain:
66-
if toolchain.name != _DEFAULT_NAME and not mod.is_root:
67-
fail("""\
68-
Only the root module may override the default name for the bazeldnf toolchain.
69-
This prevents conflicting registrations in the global namespace of external repos.
70-
""")
71-
if mod.is_root and toolchain.disable:
72-
break
73-
bazeldnf_register_toolchains(
74-
name = toolchain.name,
75-
register = False,
76-
)
77-
if mod.is_root:
78-
repos.append(toolchain.name + "_toolchains")
79-
80117
legacy = True
81118
name = "bazeldnf_rpms"
82119
for config in mod.tags.config:
@@ -121,20 +158,6 @@ def _toolchain_extension(module_ctx):
121158

122159
return module_ctx.extension_metadata(**kwargs)
123160

124-
_toolchain_tag = tag_class(
125-
attrs = {
126-
"name": attr.string(
127-
doc = """\
128-
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
129-
Overriding the default is only permitted in the root module.
130-
""",
131-
default = _DEFAULT_NAME,
132-
),
133-
"disable": attr.bool(default = False),
134-
},
135-
doc = "Allows registering a prebuilt bazeldnf toolchain",
136-
)
137-
138161
_rpm_tag = tag_class(
139162
attrs = {
140163
"name": attr.string(doc = "Name of the generated repository"),
@@ -198,9 +221,8 @@ The lock file content is as:
198221
)
199222

200223
bazeldnf = module_extension(
201-
implementation = _toolchain_extension,
224+
implementation = _bazeldnf_extension,
202225
tag_classes = {
203-
"toolchain": _toolchain_tag,
204226
"rpm": _rpm_tag,
205227
"config": _config_tag,
206228
},

e2e/bazel-bzlmod-toolchain-from-source/MODULE.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ use_repo(
2727
"org_golang_x_crypto",
2828
)
2929

30+
bazeldnf_toolchain = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "toolchain")
31+
bazeldnf_toolchain.toolchain(disable = True)
32+
3033
bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
31-
bazeldnf.toolchain(disable = True)
3234
bazeldnf.rpm(
3335
name = "libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
3436
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",

0 commit comments

Comments
 (0)