Skip to content

Commit b1cbf2e

Browse files
committed
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
1 parent b3f3041 commit b1cbf2e

File tree

9 files changed

+169
-2
lines changed

9 files changed

+169
-2
lines changed

.github/workflows/action.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ jobs:
6969
strategy:
7070
matrix:
7171
version: [6.x, 7.x]
72-
path: ["bazel-bzlmod", "bazel-bzlmod-non-legacy-mode"]
72+
path:
73+
- bazel-bzlmod
74+
- bazel-bzlmod-non-legacy-mode
75+
- bazel-bzlmod-lock-file
7376

7477
runs-on: ubuntu-latest
7578
steps:

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ e2e-bzlmod-non-legacy-mode:
4242
) \
4343
done
4444

45+
e2e-bazel-bzlmod-lock-file:
46+
@for version in 6.x 7.x; do \
47+
( \
48+
cd e2e/bazel-bzlmod-lock-file && \
49+
echo "Testing $$version with bzlmod with lock file" > /dev/stderr && \
50+
USE_BAZEL_VERSION=$$version bazelisk --batch build //...\
51+
) \
52+
done
53+
4554
e2e-bzlmod-build-toolchain-6.x:
4655
( \
4756
cd e2e/bazel-bzlmod-toolchain-from-source && \

bazeldnf/extensions.bzl

+49-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ _alias_repository = repository_rule(
3333

3434
_DEFAULT_NAME = "bazeldnf"
3535

36+
def _handle_lock_file(lock_file, module_ctx):
37+
content = module_ctx.read(lock_file)
38+
lock_file_json = json.decode(content)
39+
name = lock_file_json.get("name", lock_file.name.rsplit(".json", 1)[0])
40+
41+
rpms = []
42+
43+
for rpm in lock_file_json.get("rpms", []):
44+
rpm_name = rpm.pop("name", None)
45+
if not rpm_name:
46+
urls = rpm.get("urls", [])
47+
if len(urls) < 1:
48+
fail("invalid entry in %s for %s" % (lock_file, rpm_name))
49+
rpm_name = urls[0].rsplit("/", 1)[-1]
50+
rpm_repository(name = rpm_name, **rpm)
51+
rpms.append(rpm_name)
52+
53+
_alias_repository(
54+
name = name,
55+
rpms = ["@@%s//rpm" % x for x in rpms],
56+
)
57+
58+
return name
59+
3660
def _toolchain_extension(module_ctx):
3761
repos = []
3862

@@ -58,6 +82,8 @@ def _toolchain_extension(module_ctx):
5882
if not config.legacy_mode:
5983
legacy = False
6084
name = config.name or name
85+
if config.lock_file:
86+
repos.append(_handle_lock_file(config.lock_file, module_ctx))
6187

6288
rpms = []
6389

@@ -74,7 +100,7 @@ def _toolchain_extension(module_ctx):
74100
else:
75101
rpms.append(rpm.name)
76102

77-
if not legacy:
103+
if not legacy and rpms:
78104
_alias_repository(
79105
name = name,
80106
rpms = ["@@%s//rpm" % x for x in rpms],
@@ -145,6 +171,28 @@ per rpm entry in this invocation of the bazel extension.
145171
doc = "Name of the generated proxy repository",
146172
default = "bazeldnf_rpms",
147173
),
174+
"lock_file": attr.label(
175+
doc = """\
176+
Label of the JSON file that contains the RPMs to expose, there's no legacy mode \
177+
for RPMs defined by a lock file.
178+
179+
The lock file content is as:
180+
```json
181+
{
182+
"name": "optional name for the proxy repository, defaults to the file name",
183+
"rpms": [
184+
{
185+
"name": "<name of the rpm>",
186+
"urls": ["<url0>", ...],
187+
"sha256": "<sha256 of the file>",
188+
"integrity": "<integrity of the file>"
189+
}
190+
]
191+
}
192+
```
193+
""",
194+
allow_single_file = [".json"],
195+
),
148196
},
149197
)
150198

e2e/bazel-bzlmod-lock-file/.bazelrc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Import Aspect bazelrc presets
2+
try-import %workspace%/../../.aspect/bazelrc/bazel7.bazelrc
3+
import %workspace%/../../.aspect/bazelrc/bazel6.bazelrc
4+
import %workspace%/../../.aspect/bazelrc/convenience.bazelrc
5+
import %workspace%/../../.aspect/bazelrc/correctness.bazelrc
6+
import %workspace%/../../.aspect/bazelrc/debug.bazelrc
7+
import %workspace%/../../.aspect/bazelrc/performance.bazelrc
8+
9+
common --enable_bzlmod
10+
11+
# Specific project flags go here if we have some
12+
13+
# Load any settings & overrides specific to the current user from `.bazelrc.user`.
14+
# This file should appear in `.gitignore` so that settings are not shared with team members. This
15+
# should be last statement in this config so the user configuration is able to overwrite flags from
16+
# this file. See https://bazel.build/configure/best-practices#bazelrc-file.
17+
try-import %workspace%/../../.bazelrc.user
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@bazeldnf//bazeldnf:defs.bzl", "bazeldnf", "rpmtree", "tar2files")
2+
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
3+
4+
bazeldnf(
5+
name = "bazeldnf",
6+
)
7+
8+
rpmtree(
9+
name = "something",
10+
rpms = [
11+
"@bazeldnf-rpms//libvirt-libs",
12+
"@bazeldnf-rpms//libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
13+
"@rpms-with-no-name-attribute//libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
14+
],
15+
)
16+
17+
tar2files(
18+
name = "something_libs",
19+
files = {
20+
"/usr/lib64": [
21+
"libvirt.so.0",
22+
"libvirt.so.0.6001.0",
23+
],
24+
},
25+
tar = ":something",
26+
visibility = ["//visibility:public"],
27+
)
28+
29+
pkg_tar(
30+
name = "whatever",
31+
deps = [":something"],
32+
)
33+
34+
cc_library(
35+
name = "bar",
36+
srcs = ["//:something_libs/usr/lib64"],
37+
)
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"example MODULE.bazel to test bzlmod integration for bazeldnf with a prebuilt toolchain"
2+
3+
module(name = "example-bazeldnf-with-bzlmod-lock-file")
4+
5+
bazel_dep(name = "bazeldnf")
6+
local_path_override(
7+
module_name = "bazeldnf",
8+
path = "../..",
9+
)
10+
11+
bazel_dep(name = "rules_pkg", version = "1.0.1")
12+
13+
bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
14+
bazeldnf.config(lock_file = "//:rpms.json")
15+
bazeldnf.config(lock_file = "//:rpms-with-no-name-attribute.json")
16+
use_repo(
17+
bazeldnf,
18+
"bazeldnf-rpms",
19+
"rpms-with-no-name-attribute",
20+
)

e2e/bazel-bzlmod-lock-file/WORKSPACE.bzlmod

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"rpms": [
3+
{
4+
"sha256": "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
5+
"urls": [
6+
"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",
7+
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845"
8+
]
9+
10+
}
11+
]
12+
}

e2e/bazel-bzlmod-lock-file/rpms.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "bazeldnf-rpms",
3+
"rpms": [
4+
{
5+
"name": "libvirt-libs",
6+
"sha256": "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
7+
"urls": [
8+
"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",
9+
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845"
10+
]
11+
12+
},
13+
{
14+
"sha256": "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
15+
"urls": [
16+
"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",
17+
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6"
18+
]
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)