Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created January 14, 2025 19:58
Show Gist options
  • Save fzakaria/9a1bdd5fff05cfd5c857f50a0322d98e to your computer and use it in GitHub Desktop.
Save fzakaria/9a1bdd5fff05cfd5c857f50a0322d98e to your computer and use it in GitHub Desktop.
Using Bazel Build Event Service (BES)
"""An external repository for fetching build_event_stream protobufs.
This was largely inspired from public sources such as but made to work with MODULE.bazel:
https://github.com/wlynch/kythe/blob/55231f7e76f202827e4fe3fcf36270d36543414f/tools/build_rules/build_event_stream/repo.bzl#L66
"""
load("@bazel_skylib//lib:paths.bzl", "paths")
_FILES = [
"src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.proto",
"src/main/protobuf/command_line.proto",
"src/main/protobuf/invocation_policy.proto",
"src/main/protobuf/option_filters.proto",
"src/main/java/com/google/devtools/build/lib/packages/metrics/package_load_metrics.proto",
"src/main/protobuf/action_cache.proto",
"src/main/protobuf/failure_details.proto",
]
_URL_TEMPLATE = "https://raw.githubusercontent.com/bazelbuild/bazel/{revision}/{path}"
_BUILD_FILE_TEMPLATE = """
load("@rules_proto//proto:defs.bzl", "proto_library")
proto_library(
name = "build_event_stream_proto",
deps = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:descriptor_proto",
],
srcs = [
{filenames},
],
visibility = ["//visibility:public"],
)
java_proto_library(
name = "build_event_stream_java_proto",
deps = [":build_event_stream_proto"],
visibility = ["//visibility:public"],
)
"""
def _bes_repo_rule(repository_ctx):
attrs = {
"name": repository_ctx.attr.name,
"revision": repository_ctx.attr.revision,
"sha256s": dict(repository_ctx.attr.sha256s),
}
proto_srcs = []
for path in _FILES:
url = _URL_TEMPLATE.format(
revision = repository_ctx.attr.revision,
path = path,
)
filename = paths.basename(path)
proto_srcs.append("\"%s\"" % path)
attrs["sha256s"][filename] = repository_ctx.download(
url,
output = path,
canonical_id = url,
sha256 = repository_ctx.attr.sha256s.get(filename, ""),
).sha256
repository_ctx.file(
"BUILD.bazel",
_BUILD_FILE_TEMPLATE.format(
filenames = ",\n ".join(proto_srcs),
),
)
return attrs
bes_repo_rule = repository_rule(
implementation = _bes_repo_rule,
attrs = {
"revision": attr.string(
doc = "A tag, commit or branch string at which to fetch the required files.",
default = "master",
),
"sha256s": attr.string_dict(
doc = "A mapping of basename to sha256 for the fetched files",
),
},
)
def _build_event_service_repo(ctx):
for mod in ctx.modules:
for settings in mod.tags.settings:
bes_repo_rule(
name = settings.name,
revision = settings.revision,
sha256s = settings.sha256s,
)
build_event_service_repo = module_extension(
implementation = _build_event_service_repo,
tag_classes = {
"settings" : tag_class(
attrs = {
"name": attr.string(
mandatory = True,
doc = "The name of the repository.",
),
"revision": attr.string(
mandatory = True,
doc = "A tag, commit or branch string at which to fetch the required files.",
),
"sha256s": attr.string_dict(
mandatory = True,
doc = "A mapping of basename to sha256 for the fetched files",
),
}
),
},
)
build_event_stream_repo = use_extension("//tools/build_defs:bes_repo.bzl", "build_event_service_repo")
build_event_stream_repo.settings(
name = "bazel_bes",
revision = "7.4.1",
sha256s = {
"build_event_stream.proto": "7388663cc11b0642ffce0e3a14bd4c4902b913fb5697e5cf47b7a4d7c8ba5640",
"command_line.proto": "a6fb6591aa50794431787169bc4fae16105ef5c401e7c30ecf0f775e0ab25c2c",
"invocation_policy.proto": "7a1f9074d64aaea2c5fc01df093a25fbbd68d84db63fbfb355f5aa15683a75cd",
"option_filters.proto": "bc1cc29e53e2856c3ae25149cc6661bb218737a9146e7417eb29f8caaa715821",
"failure_details.proto": "cdb3ba1112034f751484bbec47d76b9e7b46356d68f81138e35b7a7c86dee51c",
"package_load_metrics.proto": "9dfdd36b1bdb15e7e0b212cd15d2f31c1e21607e4ba25afd334e33dd46feaef5",
"action_cache.proto": "cdd08eacf08c11f6876f181dcc7f8eb7a4d0743d6be51137bdb6d8bd764107dd",
},
)
use_repo(build_event_stream_repo, "bazel_bes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment