如何从工具链生成二进制和十六进制文件



我正在使用gnu-arm gcc交叉编译器工具链构建一个cc_binary hello_world应用程序。在我的toolchain_config.bzl文件中,我定义了工具路径,以包括gcc工具、ld工具、objcopy工具等。我如何编辑我的工具链,使其在运行bazel构建时自动调用obqcopy工具并生成.hex和.bin文件?目前,看起来只是调用了编译器和链接器工具以及相关的操作。

看起来action_config可能是正确的方法,然后从那里我可以指定一个具有与该操作相关联的标志组的功能(即调用objcopy -o binary hello_world hello_world.bin的操作(。然而,我似乎无法正确执行。目前,我有一个特性,它的操作与@bazel_tools//tools/build_defs/cc:action_names.bzl中的所有objcACTION_NAMES相关联。我的代码构建,但它不会触发任何objcopy操作。

我知道一种常见的方法是使用genrule,并让它在tools参数中调用我的cc_binary。我能够做到这一点,然而,似乎更干净的方法是从工具链中自动调用它,因为它无论如何都是在工具路径中定义的。如有任何建议或推荐,我们将不胜感激!

我已经编写了自己的"objcopy"规则,该规则使用"find_cc_toolchain(ctx(.cc_toolchain.objcopy_executable"来从cc_toolchain获取obqcopy工具的路径。可能不是你想要的,但至少比genrule更好。。。

load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
def _objcopy_impl(ctx):
cc_toolchain = find_cc_toolchain(ctx)
outfile = ctx.outputs.out
infile = ctx.file.src
ctx.actions.run_shell(
outputs = [outfile],
inputs = depset(
direct = [ctx.file.src],
transitive = [
cc_toolchain.all_files,
],
),
command = "{objcopy} {args} {src} {dst}".format(
objcopy = cc_toolchain.objcopy_executable,
args = ctx.attr.args,
src = ctx.file.src.path,
dst = ctx.outputs.out.path,
),
)
return [
DefaultInfo(
files = depset([outfile]),
),
]
objcopy = rule(
implementation = _objcopy_impl,
attrs = {
# Source file
"src": attr.label(allow_single_file = True, mandatory = True),
# Target file
"out": attr.output(mandatory = True),
# Arguments
"args": attr.string(mandatory = True),
},
executable = False,
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
fragments = ["cpp"],
)

最新更新