generated from bazel-contrib/rules-template
/
repo.bzl
77 lines (69 loc) · 3.29 KB
/
repo.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
"""Create a repository to hold the toolchains
This follows guidance here:
https://docs.bazel.build/versions/main/skylark/deploying.html#registering-toolchains
"
Note that in order to resolve toolchains in the analysis phase
Bazel needs to analyze all toolchain targets that are registered.
Bazel will not need to analyze all targets referenced by toolchain.toolchain attribute.
If in order to register toolchains you need to perform complex computation in the repository,
consider splitting the repository with toolchain targets
from the repository with <LANG>_toolchain targets.
Former will be always fetched,
and the latter will only be fetched when user actually needs to build <LANG> code.
"
The "complex computation" in our case is simply downloading our pre-built rust binaries.
This guidance tells us how to avoid that: we put the toolchain targets in the alias repository
with only the toolchain attribute pointing into the platform-specific repositories.
"""
load("//py/private/toolchain:tools.bzl", "RUST_BIN_CFG", "TOOLCHAIN_PLATFORMS")
def _toolchains_repo_impl(repository_ctx):
build_content = """# Generated by toolchains_repo.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains flag.
# By default all these toolchains are registered by the py_register_toolchains macro
# so you don't normally need to interact with these targets.
"""
for tool, cfg in RUST_BIN_CFG.items():
for [platform, meta] in TOOLCHAIN_PLATFORMS.items():
build_content += """
# Declare a toolchain Bazel will select for running {tool} on the {cfg} platform.
toolchain(
name = "{tool}_{platform}_toolchain",
{cfg}_compatible_with = {compatible_with},
# Bazel does not follow this attribute during analysis, so the referenced repo
# will only be fetched if this toolchain is selected.
toolchain = "@{user_repository_name}.{platform}//:{tool}_toolchain",
toolchain_type = "@aspect_rules_py//py/private/toolchain:{tool}_toolchain_type",
)
""".format(
cfg = cfg,
tool = tool,
platform = platform,
user_repository_name = repository_ctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)
# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", build_content)
toolchains_repo = repository_rule(
_toolchains_repo_impl,
doc = """\
Creates a single repository with toolchain definitions for all known platforms
that can be registered or selected.
""",
attrs = {
"user_repository_name": attr.string(mandatory = True, doc = """\
What the user chose for the base name.
Needed since bzlmod apparent name has extra tilde segments.
"""),
},
)
def _prerelease_toolchains_repo_impl(repository_ctx):
repository_ctx.file("BUILD.bazel", "# No toolchains created for pre-releases")
prerelease_toolchains_repo = repository_rule(
_prerelease_toolchains_repo_impl,
doc = """Create a repo with an empty BUILD file, which registers no toolchains.
This is used for pre-releases, which have no pre-built binaries, but still want to call
register_toolchains("@this_repo//:all")
By doing this, we can avoid those register_toolchains callsites needing to be conditional on IS_PRERELEASE
""",
)