Skip to content

Commit

Permalink
Implement binary_toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
BoleynSu committed Jul 1, 2024
1 parent 31b4bb6 commit 7725409
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions lib/binary_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
BinaryInfo = provider(
doc = "Provide info for binary",
fields = {
"bin": "Target for the binary",
},
)

def _toolchain_impl(ctx):
binary_info = BinaryInfo(
bin = ctx.attr.bin,
)

toolchain_info = platform_common.ToolchainInfo(
binary_info = binary_info,
)

return [toolchain_info]

binary_toolchain = rule(
implementation = _toolchain_impl,
attrs = {
"bin": attr.label(
mandatory = True,
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)

binary_runtime_toolchain = rule(
implementation = _toolchain_impl,
attrs = {
"bin": attr.label(
mandatory = True,
allow_single_file = True,
executable = True,
cfg = "target",
),
},
)

def _resolved_binary_rule_impl(ctx, toolchain_type, template_variable):
bin = ctx.toolchains[toolchain_type].binary_info.bin[DefaultInfo]

out = ctx.actions.declare_file(ctx.attr.name + ".exe")
ctx.actions.symlink(
target_file = bin.files_to_run.executable,
output = out,
is_executable = True,
)

return [
DefaultInfo(
executable = out,
files = bin.files,
runfiles = bin.default_runfiles,
),
platform_common.TemplateVariableInfo({
template_variable: out.path,
} if template_variable != None else {}),
]

def resolved_binary_rule(*, toolchain_type, template_variable = None):
return rule(
implementation = lambda ctx: _resolved_binary_rule_impl(ctx, toolchain_type, template_variable),
executable = True,
toolchains = [toolchain_type],
)

0 comments on commit 7725409

Please sign in to comment.