forked from GoogleContainerTools/container-structure-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repositories.bzl
92 lines (78 loc) · 3.67 KB
/
repositories.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Repository rules for fetching pre-built container-test binaries"""
load("//bazel:toolchains_repo.bzl", "PLATFORMS", "toolchains_repo")
# TODO(alexeagle): automate updates when new releases
_VERSION = "v1.16.0"
_HASHES = {
"darwin-amd64": "sha256-jOe4gU9MwonDp3K8lSZGmjmKDLLdl4ejPT6Ke86lYhs=",
"linux-amd64": "sha256-nMIaLb5vcqahAZXMO2/R2163bEqurAm0Kwam8Bf03ko=",
"linux-arm64": "sha256-EWuxmIv/YLcRQV++8o3KjaVmVUT5sHSIZoeMSjuKKJQ=",
"linux-ppc64le": "sha256-NEiUQYRgZtnhCzKXDTjB0lO95WkVcxvoPFnVt6neN0A=",
"linux-s390x": "sha256-6Hvn2XJZtLyP11IQlgqty7jMZ+6ETro3CRzayNslQAw=",
"windows-amd64.exe": "sha256-vdYQeDJ+DUyXNeLV8UtE5cGjdHF7+aQfFRG6Xo8Q0hY="
}
STRUCTURE_TEST_BUILD_TMPL = """\
# Generated by container/repositories.bzl
load("@container_structure_test//bazel:toolchain.bzl", "structure_test_toolchain")
structure_test_toolchain(
name = "structure_test_toolchain",
structure_test = "structure_test"
)
"""
def _structure_test_repo_impl(repository_ctx):
platform = repository_ctx.attr.platform.replace("_", "-")
# There is no arm64 version of structure test binary.
# TODO: remove this after we start publishing one.
if platform.find("darwin") != -1:
platform = platform.replace("arm64", "amd64")
elif platform.find("windows") != -1:
platform = platform + ".exe"
url = "https://github.com/GoogleContainerTools/container-structure-test/releases/download/{version}/container-structure-test-{platform}".format(
version = _VERSION,
platform = platform,
)
repository_ctx.download(
url = url,
output = "structure_test",
integrity = _HASHES[platform],
executable = True,
)
repository_ctx.file("BUILD.bazel", STRUCTURE_TEST_BUILD_TMPL)
structure_test_repositories = repository_rule(
_structure_test_repo_impl,
doc = "Fetch external tools needed for structure test toolchain",
attrs = {
"platform": attr.string(mandatory = True, values = PLATFORMS.keys()),
},
)
# Wrapper macro around everything above, this is the primary API
def container_structure_test_register_toolchain(name, register = True):
"""Convenience macro for users which does typical setup.
- create a repository for each built-in platform like "container_linux_amd64" -
this repository is lazily fetched when node is needed for that platform.
- create a repository exposing toolchains for each platform like "container_platforms"
- register a toolchain pointing at each platform
Users can avoid this macro and do these steps themselves, if they want more control.
Args:
name: base name for all created repos, like "container7"
register: whether to call through to native.register_toolchains.
Should be True for WORKSPACE users, but false when used under bzlmod extension
"""
st_toolchain_name = "structure_test_toolchains"
for platform in PLATFORMS.keys():
structure_test_repositories(
name = "{name}_st_{platform}".format(name = name, platform = platform),
platform = platform,
)
if register:
native.register_toolchains("@{}//:{}_toolchain".format(st_toolchain_name, platform))
toolchains_repo(
name = st_toolchain_name,
toolchain_type = "@container_structure_test//bazel:structure_test_toolchain_type",
# avoiding use of .format since {platform} is formatted by toolchains_repo for each platform.
toolchain = "@%s_st_{platform}//:structure_test_toolchain" % name,
)
def _st_extension_impl(_):
container_structure_test_register_toolchain("structure_test", register = False)
extension = module_extension(
implementation = _st_extension_impl,
)