Skip to content

Commit 2255f6b

Browse files
committed
Implement binary_toolchain
1 parent 31b4bb6 commit 2255f6b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

lib/binary_toolchain.bzl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
BinaryInfo = provider(
2+
doc = "Provide info for binary",
3+
fields = {
4+
"bin": "Target for the binary",
5+
},
6+
)
7+
8+
def _toolchain_impl(ctx):
9+
binary_info = BinaryInfo(
10+
bin = ctx.attr.bin,
11+
)
12+
13+
toolchain_info = platform_common.ToolchainInfo(
14+
binary_info = binary_info,
15+
)
16+
17+
return [toolchain_info]
18+
19+
binary_toolchain = rule(
20+
implementation = _toolchain_impl,
21+
attrs = {
22+
"bin": attr.label(
23+
mandatory = True,
24+
executable = True,
25+
cfg = "exec",
26+
),
27+
},
28+
)
29+
30+
binary_runtime_toolchain = rule(
31+
implementation = _toolchain_impl,
32+
attrs = {
33+
"bin": attr.label(
34+
mandatory = True,
35+
allow_single_file = True,
36+
executable = True,
37+
cfg = "target",
38+
),
39+
},
40+
)
41+
42+
def _resolved_binary_rule_impl(ctx, toolchain_type, template_variable):
43+
bin = ctx.toolchains[toolchain_type].binary_info.bin[DefaultInfo]
44+
45+
out = ctx.actions.declare_file(ctx.attr.name + ".exe")
46+
ctx.actions.symlink(
47+
target_file = bin.files_to_run.executable,
48+
output = out,
49+
is_executable = True,
50+
)
51+
52+
return [
53+
DefaultInfo(
54+
executable = out,
55+
files = bin.files,
56+
runfiles = bin.default_runfiles,
57+
),
58+
platform_common.TemplateVariableInfo({
59+
template_variable: out.path,
60+
} if template_variable != None else {}),
61+
]
62+
63+
def resolved_binary_rule(*, toolchain_type, template_variable = None):
64+
return rule(
65+
implementation = lambda ctx: _resolved_binary_rule_impl(ctx, toolchain_type, template_variable),
66+
executable = True,
67+
toolchains = [toolchain_type],
68+
)

0 commit comments

Comments
 (0)