-
Notifications
You must be signed in to change notification settings - Fork 23
/
extensions.bzl
56 lines (47 loc) · 2.22 KB
/
extensions.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
"""Extensions for bzlmod.
Installs a mylang toolchain.
Every module can define a toolchain version under the default name, "mylang".
The latest of those versions will be selected (the rest discarded),
and will always be registered by rules_mylang.
Additionally, the root module can define arbitrarily many more toolchain versions under different
names (the latest version will be picked for each name) and can register them as it sees fit,
effectively overriding the default named toolchain due to toolchain resolution precedence.
"""
load(":repositories.bzl", "mylang_register_toolchains")
_DEFAULT_NAME = "mylang"
mylang_toolchain = tag_class(attrs = {
"name": attr.string(doc = """\
Base name for generated repositories, allowing more than one mylang toolchain to be registered.
Overriding the default is only permitted in the root module.
""", default = _DEFAULT_NAME),
"mylang_version": attr.string(doc = "Explicit version of mylang.", mandatory = True),
})
def _toolchain_extension(module_ctx):
registrations = {}
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 mylang toolchain.
This prevents conflicting registrations in the global namespace of external repos.
""")
if toolchain.name not in registrations.keys():
registrations[toolchain.name] = []
registrations[toolchain.name].append(toolchain.mylang_version)
for name, versions in registrations.items():
if len(versions) > 1:
# TODO: should be semver-aware, using MVS
selected = sorted(versions, reverse = True)[0]
# buildifier: disable=print
print("NOTE: mylang toolchain {} has multiple versions {}, selected {}".format(name, versions, selected))
else:
selected = versions[0]
mylang_register_toolchains(
name = name,
mylang_version = selected,
register = False,
)
mylang = module_extension(
implementation = _toolchain_extension,
tag_classes = {"toolchain": mylang_toolchain},
)