You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Bazel工具链自动生成.bin与.hex文件?

实现Bazel Arm GCC工具链自动生成.bin和.hex文件

核心问题修正

你之前错误关联了Objective-C的ACTION_NAMES,这和C/C++的链接后动作无关,正确的思路是通过自定义动作配置+Feature,让Bazel在链接完成后自动调用objcopy生成目标文件。

具体实现步骤

1. 在toolchain_config.bzl中添加objcopy动作配置

导入必要模块后,定义生成.bin和.hex的动作配置:

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "action_config", "feature", "flag_group", "flag_set", "tool_path")

# 定义生成bin文件的objcopy动作
def _objcopy_bin_action():
    return action_config(
        name = "objcopy_bin",
        action_name = "objcopy_bin",
        tools = [":objcopy"],  # 对应你在tool_path中定义的objcopy工具名
        flag_sets = [
            flag_set(
                flags = [
                    "-O", "binary",
                    "%{input}", "%{output}",
                ],
                inputs = ["%{input}"],
                outputs = ["%{output}"],
            ),
        ],
    )

# 定义生成hex文件的objcopy动作
def _objcopy_hex_action():
    return action_config(
        name = "objcopy_hex",
        action_name = "objcopy_hex",
        tools = [":objcopy"],
        flag_sets = [
            flag_set(
                flags = [
                    "-O", "ihex",
                    "%{input}", "%{output}",
                ],
                inputs = ["%{input}"],
                outputs = ["%{output}"],
            ),
        ],
    )

2. 创建Feature绑定动作

定义一个Feature,指定动作依赖链接动作,确保在链接完成后执行:

def _generate_bin_hex_feature():
    return feature(
        name = "generate_bin_hex",
        actions = ["objcopy_bin", "objcopy_hex"],
        requires = ["link"],  # 依赖链接动作,保证执行顺序
    )

3. 整合到工具链配置

在工具链实现函数中,添加上述动作配置和Feature:

def _cc_toolchain_config_impl(ctx):
    # 原有工具路径定义(确保objcopy已正确配置)
    tool_paths = [
        tool_path(name = "gcc", path = ctx.attr.gcc_path),
        tool_path(name = "ld", path = ctx.attr.ld_path),
        tool_path(name = "objcopy", path = ctx.attr.objcopy_path),
        # 补充as、ar等其他必要工具路径
    ]

    # 原有动作配置 + 新增的objcopy动作
    action_configs = [
        # 保留你已有的编译、链接等动作配置
        # ...
        _objcopy_bin_action(),
        _objcopy_hex_action(),
    ]

    # 原有Feature + 新增的生成bin/hex的Feature
    features = [
        # 保留你已有的Feature
        # ...
        _generate_bin_hex_feature(),
    ]

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        toolchain_identifier = "arm-gcc-toolchain",
        host_system_name = "local",
        target_system_name = ctx.attr.target_system_name,
        target_cpu = ctx.attr.target_cpu,
        target_libc = ctx.attr.target_libc,
        compiler = "gcc",
        abi_version = "none",
        abi_libc_version = "none",
        tool_paths = tool_paths,
        action_configs = action_configs,
        features = features,
    )

4. 启用Feature

有两种方式启用该Feature:

  • 单个目标启用:在BUILD文件的cc_binary中指定features:
    cc_binary(
        name = "hello_world",
        srcs = ["hello_world.c"],
        features = ["generate_bin_hex"],
        toolchain = "@your_toolchain//:toolchain",  # 替换为你的工具链标签
    )
    
  • 全局启用:在.bazelrc中添加全局配置:
    build --features=generate_bin_hex
    

验证与排查

  • 确保objcopy的工具路径在tool_paths中正确配置,Bazel能找到该工具
  • 检查Feature是否被正确关联,可通过bazel build --verbose_failures查看动作执行日志
  • 若仍未触发,可尝试在Feature中添加enabled = True强制启用,或检查工具链是否被正确加载

内容的提问来源于stack exchange,提问作者CNC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 06:49:06