Skip to content

Commit 7725409

Browse files
committed
Implement binary_toolchain
1 parent 31b4bb6 commit 7725409

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

lib/binary_toolchain.bzl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
allow_single_file = True,
25+
executable = True,
26+
cfg = "exec",
27+
),
28+
},
29+
)
30+
31+
binary_runtime_toolchain = rule(
32+
implementation = _toolchain_impl,
33+
attrs = {
34+
"bin": attr.label(
35+
mandatory = True,
36+
allow_single_file = True,
37+
executable = True,
38+
cfg = "target",
39+
),
40+
},
41+
)
42+
43+
def _resolved_binary_rule_impl(ctx, toolchain_type, template_variable):
44+
bin = ctx.toolchains[toolchain_type].binary_info.bin[DefaultInfo]
45+
46+
out = ctx.actions.declare_file(ctx.attr.name + ".exe")
47+
ctx.actions.symlink(
48+
target_file = bin.files_to_run.executable,
49+
output = out,
50+
is_executable = True,
51+
)
52+
53+
return [
54+
DefaultInfo(
55+
executable = out,
56+
files = bin.files,
57+
runfiles = bin.default_runfiles,
58+
),
59+
platform_common.TemplateVariableInfo({
60+
template_variable: out.path,
61+
} if template_variable != None else {}),
62+
]
63+
64+
def resolved_binary_rule(*, toolchain_type, template_variable = None):
65+
return rule(
66+
implementation = lambda ctx: _resolved_binary_rule_impl(ctx, toolchain_type, template_variable),
67+
executable = True,
68+
toolchains = [toolchain_type],
69+
)

0 commit comments

Comments
 (0)