From b3f3041f3b53ca1b1c215b2a3c224c97b1b4fc9f Mon Sep 17 00:00:00 2001 From: Manuel Naranjo Date: Mon, 22 Jul 2024 10:21:19 +0200 Subject: [PATCH] bzlmod: introducing proxy repository Introducing a proxy repository to make it easier to consume rpms by not polluting the public repository namespace --- .github/workflows/action.yml | 3 +- Makefile | 13 +++- bazeldnf/extensions.bzl | 70 ++++++++++++++++--- e2e/bazel-bzlmod-non-legacy-mode/.bazelrc | 17 +++++ e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel | 36 ++++++++++ e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel | 31 ++++++++ .../WORKSPACE.bzlmod | 0 7 files changed, 158 insertions(+), 12 deletions(-) create mode 100644 e2e/bazel-bzlmod-non-legacy-mode/.bazelrc create mode 100644 e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel create mode 100644 e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel create mode 100644 e2e/bazel-bzlmod-non-legacy-mode/WORKSPACE.bzlmod diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index d893694a..622aeaae 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -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: @@ -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: diff --git a/Makefile b/Makefile index f6ac6061..35c8119f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ deps-update: gazelle: bazelisk run //:gazelle -test: gazelle e2e +test: gazelle buildifier e2e bazelisk build //... && bazelisk test //... buildifier: @@ -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 && \ @@ -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 diff --git a/bazeldnf/extensions.bzl b/bazeldnf/extensions.bzl index 7924735f..277c236c 100644 --- a/bazeldnf/extensions.bzl +++ b/bazeldnf/extensions.bzl @@ -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: @@ -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, @@ -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: @@ -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, }, ) diff --git a/e2e/bazel-bzlmod-non-legacy-mode/.bazelrc b/e2e/bazel-bzlmod-non-legacy-mode/.bazelrc new file mode 100644 index 00000000..b69c65e7 --- /dev/null +++ b/e2e/bazel-bzlmod-non-legacy-mode/.bazelrc @@ -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 diff --git a/e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel b/e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel new file mode 100644 index 00000000..768c7a2e --- /dev/null +++ b/e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel @@ -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"], +) diff --git a/e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel b/e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel new file mode 100644 index 00000000..916e7ce8 --- /dev/null +++ b/e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel @@ -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") diff --git a/e2e/bazel-bzlmod-non-legacy-mode/WORKSPACE.bzlmod b/e2e/bazel-bzlmod-non-legacy-mode/WORKSPACE.bzlmod new file mode 100644 index 00000000..e69de29b