Skip to content

Commit

Permalink
bzlmod: introducing proxy repository
Browse files Browse the repository at this point in the history
Introducing a proxy repository to make it easier to consume rpms by not
polluting the public repository namespace
  • Loading branch information
manuelnaranjo committed Jul 22, 2024
1 parent 7b9b7c5 commit b3f3041
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
strategy:
matrix:
version: [6.x, 7.x]
path: ["bazel-bzlmod", "bazel-bzlmod-non-legacy-mode"]

runs-on: ubuntu-latest
steps:
Expand All @@ -85,7 +86,7 @@ jobs:
bazelrc: |
import %workspace%/../../.aspect/bazelrc/ci.bazelrc
import %workspace%/../../.github/workflows/ci.bazelrc
- run: cd e2e/bazel-bzlmod && USE_BAZEL_VERSION=${{ matrix.version }} bazelisk build //...
- run: cd e2e/${{ matrix.path }} && USE_BAZEL_VERSION=${{ matrix.version }} bazelisk build //...

e2e-bzlmod-build-toolchain-matrix:
strategy:
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ deps-update:
gazelle:
bazelisk run //:gazelle

test: gazelle e2e
test: gazelle buildifier e2e
bazelisk build //... && bazelisk test //...

buildifier:
Expand All @@ -33,6 +33,15 @@ e2e-bzlmod:
) \
done

e2e-bzlmod-non-legacy-mode:
@for version in 6.x 7.x; do \
( \
cd e2e/bazel-bzlmod-non-legacy-mode && \
echo "Testing $$version with bzlmod with non-legacy mode" > /dev/stderr && \
USE_BAZEL_VERSION=$$version bazelisk --batch build //...\
) \
done

e2e-bzlmod-build-toolchain-6.x:
( \
cd e2e/bazel-bzlmod-toolchain-from-source && \
Expand All @@ -47,7 +56,7 @@ e2e-bzlmod-build-toolchain-7.x:

e2e-bzlmod-build-toolchain: e2e-bzlmod-build-toolchain-6.x e2e-bzlmod-build-toolchain-7.x

e2e: e2e-workspace e2e-bzlmod e2e-bzlmod-build-toolchain
e2e: e2e-workspace e2e-bzlmod e2e-bzlmod-build-toolchain e2e-bzlmod-non-legacy-mode

fmt: gofmt buildifier

Expand Down
70 changes: 61 additions & 9 deletions bazeldnf/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@ load("@bazel_features//:features.bzl", "bazel_features")
load("//internal:rpm.bzl", rpm_repository = "rpm")
load(":repositories.bzl", "bazeldnf_register_toolchains")

_ALIAS_TEMPLATE = """\
alias(
name = "{name}",
actual = "@{name}//rpm",
visibility = ["//visibility:public"],
)
"""

def _alias_repository_impl(repository_ctx):
"""Creates a repository that aliases other repositories."""
repository_ctx.file("WORKSPACE", "")
for rpm in repository_ctx.attr.rpms:
repo_name = rpm.repo_name
repository_ctx.file("%s/BUILD.bazel" % repo_name, _ALIAS_TEMPLATE.format(name = repo_name))

_alias_repository = repository_rule(
implementation = _alias_repository_impl,
attrs = {
"rpms": attr.label_list(),
},
)

_DEFAULT_NAME = "bazeldnf"

def _toolchain_extension(module_ctx):
registrations = {}
repos = []

for mod in module_ctx.modules:
Expand All @@ -24,17 +45,22 @@ def _toolchain_extension(module_ctx):
""")
if mod.is_root and toolchain.disable:
break
registrations[toolchain.name] = 1
bazeldnf_register_toolchains(
name = toolchain.name,
register = False,
)
if mod.is_root:
repos.append(toolchain.name + "_toolchains")

for name in registrations.keys():
bazeldnf_register_toolchains(
name = name,
register = False,
)
legacy = True
name = "bazeldnf_rpms"
for config in mod.tags.config:
if not config.legacy_mode:
legacy = False
name = config.name or name

rpms = []

for mod in module_ctx.modules:
for rpm in mod.tags.rpm:
rpm_repository(
name = rpm.name,
Expand All @@ -43,8 +69,17 @@ def _toolchain_extension(module_ctx):
integrity = rpm.integrity,
)

if mod.is_root:
if mod.is_root and legacy:
repos.append(rpm.name)
else:
rpms.append(rpm.name)

if not legacy:
_alias_repository(
name = name,
rpms = ["@@%s//rpm" % x for x in rpms],
)
repos.append(name)

kwargs = {}
if bazel_features.external_deps.extension_metadata_has_reproducible:
Expand Down Expand Up @@ -97,10 +132,27 @@ It is optional to make development easier but either this attribute or
doc = "Allows registering a Bazel repository wrapping an RPM file",
)

_config_tag = tag_class(
attrs = {
"legacy_mode": attr.bool(
default = True,
doc = """\
If true, the module is loaded in legacy mode and exposes one Bazel repository \
per rpm entry in this invocation of the bazel extension.
""",
),
"name": attr.string(
doc = "Name of the generated proxy repository",
default = "bazeldnf_rpms",
),
},
)

bazeldnf = module_extension(
implementation = _toolchain_extension,
tag_classes = {
"toolchain": _toolchain_tag,
"rpm": _rpm_tag,
"config": _config_tag,
},
)
17 changes: 17 additions & 0 deletions e2e/bazel-bzlmod-non-legacy-mode/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Import Aspect bazelrc presets
try-import %workspace%/../../.aspect/bazelrc/bazel7.bazelrc
import %workspace%/../../.aspect/bazelrc/bazel6.bazelrc
import %workspace%/../../.aspect/bazelrc/convenience.bazelrc
import %workspace%/../../.aspect/bazelrc/correctness.bazelrc
import %workspace%/../../.aspect/bazelrc/debug.bazelrc
import %workspace%/../../.aspect/bazelrc/performance.bazelrc

common --enable_bzlmod

# Specific project flags go here if we have some

# Load any settings & overrides specific to the current user from `.bazelrc.user`.
# This file should appear in `.gitignore` so that settings are not shared with team members. This
# should be last statement in this config so the user configuration is able to overwrite flags from
# this file. See https://bazel.build/configure/best-practices#bazelrc-file.
try-import %workspace%/../../.bazelrc.user
36 changes: 36 additions & 0 deletions e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@bazeldnf//bazeldnf:defs.bzl", "bazeldnf", "rpmtree", "tar2files")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

bazeldnf(
name = "bazeldnf",
)

rpmtree(
name = "something",
rpms = [
"@bazeldnf_rpms//libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf_rpms//libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
],
)

tar2files(
name = "something_libs",
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
],
},
tar = ":something",
visibility = ["//visibility:public"],
)

pkg_tar(
name = "whatever",
deps = [":something"],
)

cc_library(
name = "bar",
srcs = ["//:something_libs/usr/lib64"],
)
31 changes: 31 additions & 0 deletions e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"example MODULE.bazel to test bzlmod integration for bazeldnf with a prebuilt toolchain"

module(name = "example-bazeldnf-with-bzlmod-non-legacy-mode")

bazel_dep(name = "bazeldnf")
local_path_override(
module_name = "bazeldnf",
path = "../..",
)

bazel_dep(name = "rules_pkg", version = "1.0.1")

bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.config(legacy_mode = False)
bazeldnf.rpm(
name = "libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
sha256 = "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
],
)
bazeldnf.rpm(
name = "libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
sha256 = "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
],
)
use_repo(bazeldnf, "bazeldnf_rpms")
Empty file.

0 comments on commit b3f3041

Please sign in to comment.