From 7917ec9ea76866152e6905dcff54248dcb35e750 Mon Sep 17 00:00:00 2001 From: Manuel Naranjo Date: Mon, 22 Jul 2024 11:29:29 +0200 Subject: [PATCH] bzlmod: adding basic lock file support Adding a very basic lock file support, we don't generate the lock file yet, this will come in the future. It allows reducing complexity from MODULE.bazel though --- .github/workflows/action.yml | 5 +- Makefile | 9 ++++ bazeldnf/extensions.bzl | 50 ++++++++++++++++++- e2e/bazel-bzlmod-lock-file/.bazelrc | 17 +++++++ e2e/bazel-bzlmod-lock-file/BUILD.bazel | 37 ++++++++++++++ e2e/bazel-bzlmod-lock-file/MODULE.bazel | 20 ++++++++ e2e/bazel-bzlmod-lock-file/WORKSPACE.bzlmod | 0 .../rpms-with-no-name-attribute.json | 12 +++++ e2e/bazel-bzlmod-lock-file/rpms.json | 21 ++++++++ 9 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 e2e/bazel-bzlmod-lock-file/.bazelrc create mode 100644 e2e/bazel-bzlmod-lock-file/BUILD.bazel create mode 100644 e2e/bazel-bzlmod-lock-file/MODULE.bazel create mode 100644 e2e/bazel-bzlmod-lock-file/WORKSPACE.bzlmod create mode 100644 e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json create mode 100644 e2e/bazel-bzlmod-lock-file/rpms.json diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 622aeaa..0d9a092 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -69,7 +69,10 @@ jobs: strategy: matrix: version: [6.x, 7.x] - path: ["bazel-bzlmod", "bazel-bzlmod-non-legacy-mode"] + path: + - bazel-bzlmod + - bazel-bzlmod-non-legacy-mode + - bazel-bzlmod-lock-file runs-on: ubuntu-latest steps: diff --git a/Makefile b/Makefile index 35c8119..ff1de7a 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,15 @@ e2e-bzlmod-non-legacy-mode: ) \ done +e2e-bazel-bzlmod-lock-file: + @for version in 6.x 7.x; do \ + ( \ + cd e2e/bazel-bzlmod-lock-file && \ + echo "Testing $$version with bzlmod with lock file" > /dev/stderr && \ + USE_BAZEL_VERSION=$$version bazelisk --batch build //...\ + ) \ + done + e2e-bzlmod-build-toolchain-6.x: ( \ cd e2e/bazel-bzlmod-toolchain-from-source && \ diff --git a/bazeldnf/extensions.bzl b/bazeldnf/extensions.bzl index 277c236..942f76f 100644 --- a/bazeldnf/extensions.bzl +++ b/bazeldnf/extensions.bzl @@ -33,6 +33,30 @@ _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) + name = lock_file_json.get("name", lock_file.name.rsplit(".json", 1)[0]) + + rpms = [] + + for rpm in lock_file_json.get("rpms", []): + rpm_name = rpm.pop("name", None) + if not rpm_name: + urls = rpm.get("urls", []) + if len(urls) < 1: + fail("invalid entry in %s for %s" % (lock_file, rpm_name)) + rpm_name = urls[0].rsplit("/", 1)[-1] + rpm_repository(name = rpm_name, **rpm) + rpms.append(rpm_name) + + _alias_repository( + name = name, + rpms = ["@@%s//rpm" % x for x in rpms], + ) + + return name + def _toolchain_extension(module_ctx): repos = [] @@ -58,6 +82,8 @@ def _toolchain_extension(module_ctx): if not config.legacy_mode: legacy = False name = config.name or name + if config.lock_file: + repos.append(_handle_lock_file(config.lock_file, module_ctx)) rpms = [] @@ -74,7 +100,7 @@ def _toolchain_extension(module_ctx): else: rpms.append(rpm.name) - if not legacy: + if not legacy and rpms: _alias_repository( name = name, rpms = ["@@%s//rpm" % x for x in rpms], @@ -145,6 +171,28 @@ per rpm entry in this invocation of the bazel extension. doc = "Name of the generated proxy repository", default = "bazeldnf_rpms", ), + "lock_file": attr.label( + doc = """\ +Label of the JSON file that contains the RPMs to expose, there's no legacy mode \ +for RPMs defined by a lock file. + +The lock file content is as: +```json + { + "name": "optional name for the proxy repository, defaults to the file name", + "rpms": [ + { + "name": "", + "urls": ["", ...], + "sha256": "", + "integrity": "" + } + ] + } +``` +""", + allow_single_file = [".json"], + ), }, ) diff --git a/e2e/bazel-bzlmod-lock-file/.bazelrc b/e2e/bazel-bzlmod-lock-file/.bazelrc new file mode 100644 index 0000000..b69c65e --- /dev/null +++ b/e2e/bazel-bzlmod-lock-file/.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-lock-file/BUILD.bazel b/e2e/bazel-bzlmod-lock-file/BUILD.bazel new file mode 100644 index 0000000..19407b7 --- /dev/null +++ b/e2e/bazel-bzlmod-lock-file/BUILD.bazel @@ -0,0 +1,37 @@ +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", + "@bazeldnf-rpms//libvirt-devel-6.1.0-2.fc32.x86_64.rpm", + "@rpms-with-no-name-attribute//libvirt-libs-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-lock-file/MODULE.bazel b/e2e/bazel-bzlmod-lock-file/MODULE.bazel new file mode 100644 index 0000000..314effc --- /dev/null +++ b/e2e/bazel-bzlmod-lock-file/MODULE.bazel @@ -0,0 +1,20 @@ +"example MODULE.bazel to test bzlmod integration for bazeldnf with a prebuilt toolchain" + +module(name = "example-bazeldnf-with-bzlmod-lock-file") + +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(lock_file = "//:rpms.json") +bazeldnf.config(lock_file = "//:rpms-with-no-name-attribute.json") +use_repo( + bazeldnf, + "bazeldnf-rpms", + "rpms-with-no-name-attribute", +) diff --git a/e2e/bazel-bzlmod-lock-file/WORKSPACE.bzlmod b/e2e/bazel-bzlmod-lock-file/WORKSPACE.bzlmod new file mode 100644 index 0000000..e69de29 diff --git a/e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json b/e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json new file mode 100644 index 0000000..0f59f68 --- /dev/null +++ b/e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json @@ -0,0 +1,12 @@ +{ + "rpms": [ + { + "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" + ] + + } + ] +} diff --git a/e2e/bazel-bzlmod-lock-file/rpms.json b/e2e/bazel-bzlmod-lock-file/rpms.json new file mode 100644 index 0000000..b6f2859 --- /dev/null +++ b/e2e/bazel-bzlmod-lock-file/rpms.json @@ -0,0 +1,21 @@ +{ + "name": "bazeldnf-rpms", + "rpms": [ + { + "name": "libvirt-libs", + "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" + ] + + }, + { + "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" + ] + } + ] +}