Skip to content

Commit 3e13975

Browse files
authored
bazel: Add arch_alias repository_rule (#2945)
This allows you to use a single target for setting host_platform, and avoids the flag complexity/duplication of having per-arch flags for platform configuration Signed-off-by: Ryan Northey <[email protected]>
1 parent da6c9a0 commit 3e13975

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

bazel/repository/BUILD

Whitespace-only changes.

bazel/repository/utils.bzl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Repository rule for defining a host platform based on CPU architecture.
2+
3+
This allows you to set a single alias for multiple architectures, that resolve
4+
to arch-specific targets. Using `native.alias` and `select` for this doesn't work
5+
when using an arch as the selector.
6+
7+
This can be useful specifically for setting the `host_platform`.
8+
9+
Example usage:
10+
11+
In WORKSPACE:
12+
13+
```starlark
14+
arch_alias(
15+
name = "clang_platform",
16+
aliases = {
17+
"amd64": "@envoy//bazel/platforms/rbe:rbe_linux_x64_clang_platform",
18+
"aarch64": "@envoy//bazel/platforms/rbe:rbe_linux_arm64_clang_platform",
19+
},
20+
)
21+
```
22+
And then in .bazelrc:
23+
24+
```
25+
common:clang-common --host_platform=@clang_platform
26+
```
27+
28+
"""
29+
30+
ERROR_UNSUPPORTED = """
31+
Unsupported host architecture '{arch}'. Supported architectures are: {supported}
32+
"""
33+
34+
ALIAS_BUILD = """
35+
alias(
36+
name = "{name}",
37+
actual = "{actual}",
38+
visibility = ["//visibility:public"],
39+
)
40+
"""
41+
42+
def _arch_alias_impl(ctx):
43+
arch = ctx.os.arch
44+
actual = ctx.attr.aliases.get(arch)
45+
if not actual:
46+
fail(ERROR_UNSUPPORTED.format(
47+
arch = arch,
48+
supported = ctx.attr.aliases.keys(),
49+
))
50+
ctx.file(
51+
"BUILD.bazel",
52+
ALIAS_BUILD.format(
53+
name = ctx.name,
54+
actual = actual,
55+
),
56+
)
57+
58+
arch_alias = repository_rule(
59+
implementation = _arch_alias_impl,
60+
attrs = {
61+
"aliases": attr.string_dict(
62+
doc = "A dictionary of arch strings, mapped to associated aliases",
63+
),
64+
},
65+
)

0 commit comments

Comments
 (0)