From 2255f6b2698802a96b3cdd3a26938870f7044896 Mon Sep 17 00:00:00 2001 From: Boleyn Su Date: Mon, 1 Jul 2024 16:09:04 +0800 Subject: [PATCH] Implement binary_toolchain --- lib/binary_toolchain.bzl | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib/binary_toolchain.bzl diff --git a/lib/binary_toolchain.bzl b/lib/binary_toolchain.bzl new file mode 100644 index 000000000..e55c919e9 --- /dev/null +++ b/lib/binary_toolchain.bzl @@ -0,0 +1,68 @@ +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, + 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], + )