Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelnaranjo committed Jul 15, 2024
1 parent 650f881 commit d3d2a28
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 208 deletions.
5 changes: 1 addition & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import %workspace%/.aspect/bazelrc/performance.bazelrc
# Specific project flags go here if we have some
common --incompatible_enable_proto_toolchain_resolution

# register toolchains with prebuilts
common:prebuilt-toolchain --extra_toolchains=@bazeldnf_toolchains//:all

common:built-toolchain --extra_toolchains=//cmd:bazeldnf-host-toolchain
common --extra_toolchains=//cmd:bazeldnf-host-toolchain

common --enable_bzlmod

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ jobs:
repository-cache: true
bazelrc: |
import %workspace%/.github/workflows/ci.bazelrc
common --config=built-toolchain
- run: bazel build //... && bazel test //...

Expand Down
4 changes: 2 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@bazeldnf-dev-rpms//:libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf-dev-rpms//:libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
],
)

Expand Down
32 changes: 31 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ module(

bazeldnf = use_extension("//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.toolchain()
use_repo(bazeldnf, "bazeldnf_toolchains")
use_repo(
bazeldnf,
"bazeldnf_toolchains",
)

register_toolchains("@bazeldnf_toolchains//:all")

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "bazel_features", version = "1.13.0")

bazel_dep(name = "gazelle", version = "0.37.0", dev_dependency = True)
bazel_dep(name = "rules_go", version = "0.49.0", dev_dependency = True, repo_name = "io_bazel_rules_go")
Expand Down Expand Up @@ -41,3 +45,29 @@ use_repo(
"io_k8s_sigs_yaml",
"org_golang_x_crypto",
)

bazeldnf_dev = use_extension("//bazeldnf:extensions.bzl", "bazeldnf", dev_dependency = True)
bazeldnf_dev.alias(
name = "bazeldnf-dev-rpms",
dev_dependency = True,
)
bazeldnf_dev.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_dev.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_dev,
"bazeldnf-dev-rpms",
)
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gazelle:
bazelisk run //:gazelle

test: gazelle
bazelisk build --config=built-toolchain //... && bazelisk test --config=built-toolchain //...
bazelisk build //... && bazelisk test //...

buildifier:
bazelisk run //:buildifier
Expand All @@ -18,7 +18,9 @@ gofmt:
e2e:
(cd e2e/bazel-5 && bazelisk build //...)
(cd e2e/bazel-6 && bazelisk build //...)
(cd e2e/bazel-6-bzlmod && bazelisk build //...)
(cd e2e/bazel-7 && bazelisk build //...)
(cd e2e/bazel-7-bzlmod && bazelisk build //...)

fmt: gofmt buildifier

Expand Down
4 changes: 0 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
workspace(name = "bazeldnf")

load(":test_deps.bzl", "bazeldnf_test_dependencies")

bazeldnf_test_dependencies()
133 changes: 118 additions & 15 deletions bazeldnf/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,139 @@ Installs the bazeldnf toolchain.
based on: https://github.com/bazel-contrib/rules-template/blob/0dadcb716f06f672881681155fe6d9ff6fc4a4f4/mylang/extensions.bzl
"""

load("//tools:version.bzl", "VERSION")
load("@bazel_features//:features.bzl", "bazel_features")
load("//bazeldnf/private:toolchains_repo.bzl", "toolchains_repo")
load("//internal:rpm.bzl", rpm_repository = "rpm")
load(":repositories.bzl", "bazeldnf_register_toolchains")

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

def _proxy_repo_impl(repository_ctx):
aliases = [
_ALIAS_TEMPLATE.format(
name = v,
actual = k.workspace_name,
)
for k, v in repository_ctx.attr.rpms.items()
]
repository_ctx.file("WORKSPACE", "workspace(name = \"{name}\")".format(name = repository_ctx.name))
repository_ctx.file("BUILD.bazel", "\n".join(aliases))

_proxy_repo = repository_rule(
implementation = _proxy_repo_impl,
attrs = {
"rpms": attr.label_keyed_string_dict(),
},
)

_DEFAULT_NAME = "bazeldnf"

bazeldnf_toolchain = tag_class(attrs = {
"name": attr.string(doc = """\
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
Overriding the default is only permitted in the root module.
""", default = _DEFAULT_NAME),
})
def _handle_rpms(alias, mod, module_ctx):
if not mod.tags.rpm:
return {}

rpms = []

for rpm in mod.tags.rpm:
name = rpm.name

if not rpm.urls:
fail("urls must be specified for %s" % name)

if not name:
url = rpm.urls[0]

# expect the url to be something like host/name-{version}-{release}.{arch}.rpm
name, _, _ = url.rsplit("-", 2)
name = name.rsplit("/", 1)[-1]

rpm_repository(
name = name,
urls = rpm.urls,
sha256 = rpm.sha256,
integrity = rpm.integrity,
)

rpms.append(name)

_proxy_repo(
name = alias,
rpms = dict([["@@%s" % x, x] for x in rpms]),
)

return rpms

def _toolchain_extension(module_ctx):
registrations = {}
repos = {}
dev_repos = {}

for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
if toolchain.name != _DEFAULT_NAME and not mod.is_root:
fail("""\
Only the root module may override the default name for the bazeldnf toolchain.
This prevents conflicting registrations in the global namespace of external repos.
""")
registrations[toolchain.name] = 1

for name in registrations.keys():
bazeldnf_register_toolchains(
name = name,
register = False,
)
bazeldnf_register_toolchains(
name = toolchain.name,
register = False,
)
if mod.is_root:
repos["%s_toolchains" % toolchain.name] = 1

alias = "%s-rpms" % _DEFAULT_NAME
dev_dependency = False
for _alias in mod.tags.alias:
alias = _alias.name
dev_dependency = _alias.dev_dependency

rpms = _handle_rpms(alias, mod, module_ctx)
if rpms:
if not dev_dependency:
repos[alias] = 1
else:
dev_repos[alias] = 1

kwargs = {
"root_module_direct_deps": repos.keys(),
"root_module_direct_dev_deps": dev_repos.keys(),
}

# once we move to bazel 7 this becomes True by default
if bazel_features.external_deps.extension_metadata_has_reproducible:
kwargs["reproducible"] = True

return module_ctx.extension_metadata(**kwargs)

bazeldnf_toolchain = tag_class(attrs = {
"name": attr.string(doc = """\
Base name for generated repositories, allowing more than one bazeldnf toolchain to be registered.
Overriding the default is only permitted in the root module.
""", default = _DEFAULT_NAME),
})

rpm_tag = tag_class(attrs = {
"integrity": attr.string(doc = "Subresource Integrity (SRI) hash of the RPM file"),
"name": attr.string(doc = "Exposed name for the RPM Bazel repository, defaults to extract from the first url"),
"sha256": attr.string(doc = "SHA256 hash of the RPM file"),
"urls": attr.string_list(doc = "URLs of the RPM file to download from"),
})

bazeldnf = module_extension(
implementation = _toolchain_extension,
tag_classes = {"toolchain": bazeldnf_toolchain},
tag_classes = {
"toolchain": bazeldnf_toolchain,
"rpm": rpm_tag,
"alias": tag_class(attrs = {
"name": attr.string(doc = "The name of the alias repository"),
"dev_dependency": attr.bool(doc = "Whether the alias repository is a dev dependency", default = False),
}),
},
)
File renamed without changes.
2 changes: 1 addition & 1 deletion bazeldnf/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This guidance tells us how to avoid that: we put the toolchain targets in the al
with only the toolchain attribute pointing into the platform-specific repositories.
"""

load("@bazeldnf//tools:platforms.bzl", "PLATFORMS")
load("@bazeldnf//bazeldnf:platforms.bzl", "PLATFORMS")

def _toolchains_repo_impl(repository_ctx):
build_content = """# Generated by toolchains_repo.bzl
Expand Down
6 changes: 3 additions & 3 deletions bazeldnf/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file", _http_archive
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//bazeldnf/private:prebuilts.bzl", "PREBUILTS", "REPO_URL", "VERSION")
load("//bazeldnf/private:toolchains_repo.bzl", "toolchains_repo")
load("//tools:platforms.bzl", "PLATFORMS")
load(":platforms.bzl", "PLATFORMS")

def http_archive(name, **kwargs):
maybe(_http_archive, name = name, **kwargs)
Expand Down Expand Up @@ -61,7 +61,7 @@ bazeldnf_toolchain(
# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", build_content)

bazeldnf_repositories = repository_rule(
bazeldnf_prebuilt_repository = repository_rule(
_bazeldnf_repo_impl,
doc = _DOC,
attrs = _ATTRS,
Expand Down Expand Up @@ -100,7 +100,7 @@ def bazeldnf_register_toolchains(name, register = True, **kwargs):
executable = True,
url = url,
)
bazeldnf_repositories(
bazeldnf_prebuilt_repository(
name = "%s_%s" % (name, platform),
tool = name_,
**kwargs
Expand Down
4 changes: 2 additions & 2 deletions e2e/bazel-6-bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@bazeldnf-rpms//:libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf-rpms//:libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
],
)

Expand Down
22 changes: 22 additions & 0 deletions e2e/bazel-6-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,25 @@ local_path_override(
bazel_dep(name = "rules_pkg", version = "0.10.1", dev_dependency = True)

bazel_dep(name = "bazel_skylib", version = "1.7.1")

bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
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",
)
4 changes: 0 additions & 4 deletions e2e/bazel-6-bzlmod/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
# dependencies for integration tests
load("@bazeldnf//:test_deps.bzl", "bazeldnf_test_dependencies")

bazeldnf_test_dependencies()
4 changes: 2 additions & 2 deletions e2e/bazel-7-bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@bazeldnf-rpms//:libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf-rpms//:libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
],
)

Expand Down
22 changes: 22 additions & 0 deletions e2e/bazel-7-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,25 @@ local_path_override(
bazel_dep(name = "rules_pkg", version = "0.10.1", dev_dependency = True)

bazel_dep(name = "bazel_skylib", version = "1.7.1")

bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
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",
)
Loading

0 comments on commit d3d2a28

Please sign in to comment.