Skip to content

Commit

Permalink
bzlmod: adding basic lock file support
Browse files Browse the repository at this point in the history
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
  • Loading branch information
manuelnaranjo committed Jul 22, 2024
1 parent 7fce2de commit 7917ec9
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
50 changes: 49 additions & 1 deletion bazeldnf/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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 = []

Expand All @@ -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],
Expand Down Expand Up @@ -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": "<name of the rpm>",
"urls": ["<url0>", ...],
"sha256": "<sha256 of the file>",
"integrity": "<integrity of the file>"
}
]
}
```
""",
allow_single_file = [".json"],
),
},
)

Expand Down
17 changes: 17 additions & 0 deletions e2e/bazel-bzlmod-lock-file/.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
37 changes: 37 additions & 0 deletions e2e/bazel-bzlmod-lock-file/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
20 changes: 20 additions & 0 deletions e2e/bazel-bzlmod-lock-file/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
Empty file.
12 changes: 12 additions & 0 deletions e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json
Original file line number Diff line number Diff line change
@@ -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"
]

}
]
}
21 changes: 21 additions & 0 deletions e2e/bazel-bzlmod-lock-file/rpms.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}

0 comments on commit 7917ec9

Please sign in to comment.