-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
zstd_toolchain.bzl
177 lines (154 loc) · 5.44 KB
/
zstd_toolchain.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"Provide access to ZSTD"
ZSTD_PLATFORMS = {
"darwin_amd64": struct(
compatible_with = [
"@platforms//os:osx",
"@platforms//cpu:x86_64",
],
),
"darwin_arm64": struct(
compatible_with = [
"@platforms//os:osx",
"@platforms//cpu:aarch64",
],
),
"linux_amd64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
),
"linux_arm64": struct(
compatible_with = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
),
}
ZSTD_PREBUILT = {
"darwin_amd64": (
"https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_darwin_amd64",
"e4d517212005cf26f8b8d657455d1380318b071cb52a3ffd9dfbdf4c2ba71a13",
),
"darwin_arm64": (
"https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_darwin_arm64",
"6e210eeae08fb6ba38c3ac2d1857075c28113aef68296f7e396f1180f7e894b9",
),
"linux_amd64": (
"https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_linux_amd64",
"0f0bd1193509a598629d7fa745c4b0b6d5fa6719e0c94c01ef0f20e466d801a7",
),
"linux_arm64": (
"https://github.com/aspect-build/zstd-prebuilt/releases/download/v1.5.6/zstd_linux_arm64",
"82aacf8f1c67ff3c94e04afb0721a848bbba70fbf8249ee4bc4c9085afb84548",
),
}
def _zstd_binary_repo(rctx):
(url, sha256) = ZSTD_PREBUILT[rctx.attr.platform]
rctx.download(
url = url,
output = "zstd",
executable = True,
sha256 = sha256,
)
binary = "zstd"
rctx.file("BUILD.bazel", """\
# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl
load("@aspect_bazel_lib//lib/private:zstd_toolchain.bzl", "zstd_toolchain")
package(default_visibility = ["//visibility:public"])
zstd_toolchain(name = "zstd_toolchain", binary = "{}")
""".format(binary))
zstd_binary_repo = repository_rule(
implementation = _zstd_binary_repo,
attrs = {
"platform": attr.string(mandatory = True, values = ZSTD_PLATFORMS.keys()),
},
)
ZstdInfo = provider(
doc = "Provide info for executing zstd",
fields = {
"binary": "zstd executable",
},
)
def _zstd_toolchain_impl(ctx):
binary = ctx.executable.binary
# Make the $(ZSTD_BIN) variable available in places like genrules.
# See https://docs.bazel.build/versions/main/be/make-variables.html#custom_variables
template_variables = platform_common.TemplateVariableInfo({
"ZSTD_BIN": binary.path,
})
default_info = DefaultInfo(
files = depset(ctx.files.binary + ctx.files.files),
)
zstdinfo = ZstdInfo(
binary = binary,
)
# Export all the providers inside our ToolchainInfo
# so the resolved_toolchain rule can grab and re-export them.
toolchain_info = platform_common.ToolchainInfo(
zstdinfo = zstdinfo,
template_variables = template_variables,
default = default_info,
)
return [toolchain_info, template_variables, default_info]
zstd_toolchain = rule(
implementation = _zstd_toolchain_impl,
attrs = {
"binary": attr.label(
doc = "a command to find on the system path",
allow_files = True,
executable = True,
cfg = "exec",
),
"files": attr.label_list(allow_files = True),
},
)
def _zstd_toolchains_repo_impl(rctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or zstdget platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """\
# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl
# Forward all the providers
def _resolved_toolchain_impl(ctx):
toolchain_info = ctx.toolchains["@aspect_bazel_lib//lib:zstd_toolchain_type"]
return [
toolchain_info,
toolchain_info.default,
toolchain_info.zstdinfo,
toolchain_info.template_variables,
]
# Copied from java_toolchain_alias
# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
resolved_toolchain = rule(
implementation = _resolved_toolchain_impl,
toolchains = ["@aspect_bazel_lib//lib:zstd_toolchain_type"],
incompatible_use_toolchain_transition = True,
)
"""
rctx.file("defs.bzl", starlark_content)
build_content = """# @generated by @aspect_bazel_lib//lib/private:zstd_toolchain.bzl
load(":defs.bzl", "resolved_toolchain")
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"])"""
for [platform, meta] in ZSTD_PLATFORMS.items():
build_content += """
toolchain(
name = "{platform}_toolchain",
exec_compatible_with = {compatible_with},
toolchain = "@{user_repository_name}_{platform}//:zstd_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:zstd_toolchain_type",
)
""".format(
platform = platform,
user_repository_name = rctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)
rctx.file("BUILD.bazel", build_content)
zstd_toolchains_repo = repository_rule(
_zstd_toolchains_repo_impl,
doc = """Creates a repository that exposes a zstd_toolchain_type zstdget.""",
attrs = {
"user_repository_name": attr.string(doc = "Base name for toolchains repository"),
},
)