Skip to content

Commit

Permalink
bazel: getting ready for BCR
Browse files Browse the repository at this point in the history
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
  • Loading branch information
manuelnaranjo committed Feb 10, 2025
1 parent 135db4b commit f4c93e7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
11 changes: 6 additions & 5 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module(
compatibility_level = 0,
)

# registers toolchain for consumers of the repo
bazeldnf = use_extension("//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.toolchain()
use_repo(bazeldnf, "bazeldnf_toolchains")
bazeldnf_toolchain = use_extension("//bazeldnf:extensions.bzl", "bazeldnf_toolchain")
bazeldnf_toolchain.register()
use_repo(
bazeldnf_toolchain,
"bazeldnf_toolchains",
)

register_toolchains("@bazeldnf_toolchains//:all")

Expand Down Expand Up @@ -40,7 +42,6 @@ use_repo(
)

# DEV DEPENDENCIES

bazeldnf_dev = use_extension("//bazeldnf:extensions.bzl", "bazeldnf", dev_dependency = True)
bazeldnf_dev.rpm(
name = "bazeldnf_internal_abseil-cpp-devel",
Expand Down
90 changes: 56 additions & 34 deletions bazeldnf/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,60 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
load("//internal:rpm.bzl", rpm_repository = "rpm")
load(":repositories.bzl", "bazeldnf_register_toolchains")

_DEFAULT_NAME = "bazeldnf"

def _bazeldnf_toolchain_extension(module_ctx):
repos = []
for mod in module_ctx.modules:
for toolchain in mod.tags.register:
if toolchain.name != _DEFAULT_NAME and not mod.is_root:
fail("""\
Only the root module may override the default name for the bazeldnf toolchain.
This prevents conflicting registrations in the global namespace of external repos.
""")
if mod.is_root and toolchain.disable:
break
bazeldnf_register_toolchains(
name = toolchain.name,
register = False,
)
if mod.is_root:
repos.append(toolchain.name + "_toolchains")

kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
kwargs["reproducible"] = True

if module_ctx.root_module_has_non_dev_dependency:
kwargs["root_module_direct_deps"] = repos
kwargs["root_module_direct_dev_deps"] = []
else:
kwargs["root_module_direct_deps"] = []
kwargs["root_module_direct_dev_deps"] = repos

return module_ctx.extension_metadata(**kwargs)

_toolchain_tag = tag_class(
attrs = {
"name": attr.string(
doc = """\
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
Overriding the default is only permitted in the root module.
""",
default = _DEFAULT_NAME,
),
"disable": attr.bool(default = False),
},
doc = "Allows registering a prebuilt bazeldnf toolchain",
)

bazeldnf_toolchain = module_extension(
implementation = _bazeldnf_toolchain_extension,
tag_classes = {
"register": _toolchain_tag,
},
)

_ALIAS_TEMPLATE = """\
alias(
name = "{name}",
Expand All @@ -32,8 +86,6 @@ _alias_repository = repository_rule(
},
)

_DEFAULT_NAME = "bazeldnf"

def _handle_lock_file(lock_file, module_ctx):
content = module_ctx.read(lock_file)
lock_file_json = json.decode(content)
Expand All @@ -58,25 +110,10 @@ def _handle_lock_file(lock_file, module_ctx):

return name

def _toolchain_extension(module_ctx):
def _bazeldnf_extension(module_ctx):
repos = []

for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
if toolchain.name != _DEFAULT_NAME and not mod.is_root:
fail("""\
Only the root module may override the default name for the bazeldnf toolchain.
This prevents conflicting registrations in the global namespace of external repos.
""")
if mod.is_root and toolchain.disable:
break
bazeldnf_register_toolchains(
name = toolchain.name,
register = False,
)
if mod.is_root:
repos.append(toolchain.name + "_toolchains")

legacy = True
name = "bazeldnf_rpms"
for config in mod.tags.config:
Expand Down Expand Up @@ -121,20 +158,6 @@ def _toolchain_extension(module_ctx):

return module_ctx.extension_metadata(**kwargs)

_toolchain_tag = tag_class(
attrs = {
"name": attr.string(
doc = """\
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
Overriding the default is only permitted in the root module.
""",
default = _DEFAULT_NAME,
),
"disable": attr.bool(default = False),
},
doc = "Allows registering a prebuilt bazeldnf toolchain",
)

_rpm_tag = tag_class(
attrs = {
"name": attr.string(doc = "Name of the generated repository"),
Expand Down Expand Up @@ -198,9 +221,8 @@ The lock file content is as:
)

bazeldnf = module_extension(
implementation = _toolchain_extension,
implementation = _bazeldnf_extension,
tag_classes = {
"toolchain": _toolchain_tag,
"rpm": _rpm_tag,
"config": _config_tag,
},
Expand Down
4 changes: 3 additions & 1 deletion e2e/bazel-bzlmod-toolchain-from-source/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ use_repo(
"org_golang_x_crypto",
)

bazeldnf_toolchain = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "toolchain")
bazeldnf_toolchain.toolchain(disable = True)

bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.toolchain(disable = True)
bazeldnf.rpm(
name = "libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
Expand Down

0 comments on commit f4c93e7

Please sign in to comment.