Skip to content

Commit 4513aa7

Browse files
committed
Implement binary_toolcahin
1 parent 31b4bb6 commit 4513aa7

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

lib/binary_toolcahin.bzl

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

0 commit comments

Comments
 (0)