Skip to content

Commit e05fb17

Browse files
erock2112SkCQ
authored andcommitted
[bazel] Add module_extension support to gcs_mirror
Unfortunately we can't load() gcs_mirror.bzl into MODULE.bazel. The cleanest way to get around this is to use a module_extension which wraps http_archive and http_file. Bug: b/458681039 Change-Id: I6116db1b9927a6083afe8b23ce2e49ee705188ce Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/1125497 Reviewed-by: Brett Somocurcio <[email protected]> Commit-Queue: Eric Boren <[email protected]>
1 parent 3796eed commit e05fb17

File tree

3 files changed

+146
-8
lines changed

3 files changed

+146
-8
lines changed

MODULE.bazel

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "ht
2727

2828
http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
2929

30+
gcs_mirror = use_extension("//bazel:gcs_mirror.bzl", "gcs_mirror")
31+
3032
####################################################################################################
3133
# aspect_rules_ts setup
3234
####################################################################################################
@@ -584,19 +586,21 @@ PROTOC_BUILD_FILE_CONTENT = """
584586
exports_files(["bin/protoc"], visibility = ["//visibility:public"])
585587
"""
586588

587-
http_archive(
589+
gcs_mirror.http_archive(
588590
name = "protoc_linux_x64",
589591
build_file_content = PROTOC_BUILD_FILE_CONTENT,
590592
sha256 = "3a4c1e5f2516c639d3079b1586e703fc7bcfa2136d58bda24d1d54f949c315e8",
591593
url = "https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-x86_64.zip",
592594
)
595+
use_repo(gcs_mirror, "protoc_linux_x64")
593596

594-
http_archive(
597+
gcs_mirror.http_archive(
595598
name = "protoc_mac_x64",
596599
build_file_content = PROTOC_BUILD_FILE_CONTENT,
597600
sha256 = "9448ff40278504a7ae5139bb70c962acc78c32d8fc54b4890a55c14c68b9d10a",
598601
url = "https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-osx-x86_64.zip",
599602
)
603+
use_repo(gcs_mirror, "protoc_mac_x64")
600604

601605
####################################################################################################
602606
# pgadapter.
@@ -618,7 +622,7 @@ filegroup(
618622
# CockroachDB (used as an "emulator" for tests).
619623
####################################################################################################
620624

621-
http_archive(
625+
gcs_mirror.http_archive(
622626
name = "cockroachdb_linux",
623627
build_file_content = """
624628
filegroup(
@@ -627,12 +631,11 @@ filegroup(
627631
visibility = ["//visibility:public"]
628632
)
629633
""",
630-
# https://www.cockroachlabs.com/docs/v21.1/install-cockroachdb-linux does not currently
631-
# provide SHA256 signatures. kjlubick@ downloaded this file and computed this sha256 signature.
632634
sha256 = "05293e76dfb6443790117b6c6c05b1152038b49c83bd4345589e15ced8717be3",
633635
strip_prefix = "cockroach-v21.1.9.linux-amd64",
634636
url = "https://binaries.cockroachdb.com/cockroach-v21.1.9.linux-amd64.tgz",
635637
)
638+
use_repo(gcs_mirror, "cockroachdb_linux")
636639

637640
####################################################################################################
638641
# bazel-toolchains rbe_configs_gen (prebuilt).
@@ -653,13 +656,15 @@ rbe_exec_properties = use_extension("//bazel/external:rbe_exec_properties.bzl",
653656
rbe_exec_properties.setup(name = "exec_properties")
654657
use_repo(rbe_exec_properties, "exec_properties")
655658

656-
http_file(
659+
gcs_mirror.http_file(
657660
name = "rbe_configs_gen_linux_amd64",
658661
downloaded_file_path = "rbe_configs_gen",
659662
executable = True,
663+
no_extension = True,
660664
sha256 = "1206e8a79b41cb22524f73afa4f4ee648478f46ef6990d78e7cc953665a1db89",
661665
url = "https://github.com/bazelbuild/bazel-toolchains/releases/download/v5.1.2/rbe_configs_gen_linux_amd64",
662666
)
667+
use_repo(gcs_mirror, "rbe_configs_gen_linux_amd64")
663668

664669
####################################################################################################
665670
# Google Cloud SDK (needed for the Google Cloud Emulators).

MODULE.bazel.lock

Lines changed: 74 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/gcs_mirror.bzl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""This module provides the gcs_mirror_url macro."""
22

3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
4+
35
# Set to True to force the macro to only return the mirror URL.
46
_TEST_GCS_MIRROR = False
57

@@ -37,3 +39,62 @@ def gcs_mirror_url(url, sha256, ext = None):
3739

3840
mirror_url = "%s/%s%s" % (_GCS_MIRROR_PREFIX, sha256, extension)
3941
return [mirror_url] if _TEST_GCS_MIRROR else [mirror_url, url]
42+
43+
def _gcs_mirror_impl(ctx):
44+
def _url_helper(tag):
45+
# This is a weird effect resulting from the combination of
46+
# gcs_mirror_url treatment of None vs "" and the fact that
47+
# attrs.string() cannot be None.
48+
ext = tag.ext
49+
if tag.no_extension:
50+
ext = ""
51+
elif tag.ext == "":
52+
ext = None
53+
return gcs_mirror_url(tag.url, tag.sha256, ext)
54+
55+
for mod in ctx.modules:
56+
for tag in mod.tags.http_archive:
57+
http_archive(
58+
name = tag.name,
59+
build_file_content = tag.build_file_content,
60+
sha256 = tag.sha256,
61+
strip_prefix = tag.strip_prefix,
62+
urls = _url_helper(tag),
63+
)
64+
for tag in mod.tags.http_file:
65+
http_file(
66+
name = tag.name,
67+
downloaded_file_path = tag.downloaded_file_path,
68+
executable = tag.executable,
69+
sha256 = tag.sha256,
70+
urls = _url_helper(tag),
71+
)
72+
73+
_http_archive = tag_class(attrs = {
74+
"name": attr.string(),
75+
"build_file_content": attr.string(),
76+
"ext": attr.string(),
77+
"no_extension": attr.bool(),
78+
"url": attr.string(),
79+
"sha256": attr.string(),
80+
"strip_prefix": attr.string(),
81+
})
82+
83+
_http_file = tag_class(attrs = {
84+
"name": attr.string(),
85+
"downloaded_file_path": attr.string(),
86+
"executable": attr.bool(),
87+
"ext": attr.string(),
88+
"no_extension": attr.bool(),
89+
"sha256": attr.string(),
90+
"url": attr.string(),
91+
})
92+
93+
gcs_mirror = module_extension(
94+
doc = """Bzlmod extension wrapper around gcs_mirror_url.""",
95+
implementation = _gcs_mirror_impl,
96+
tag_classes = {
97+
"http_archive": _http_archive,
98+
"http_file": _http_file,
99+
},
100+
)

0 commit comments

Comments
 (0)