首页 > 解决方案 > Bazel cc_toolchain 用于非 gnu TI DSP 编译器

问题描述

我正在将 TI C6000 DSP 版本从 Makefile 移植到 Bazel。这个 DSP 运行在一个非常复杂的嵌入式系统上,我们已经在使用 Bazel 为多个其他处理器构建相同的代码,但是那些其他处理器使用 GNU 风格(例如gcc基于)的编译器。

cc_rules包似乎对标志、文件扩展名等做出了假设。我希望我可以避免创建一个完全自定义的工具链,这将涉及移植所有现有的 BUILD 文件以根据工具链使用不同的规则。

我似乎找不到任何有关自定义这些属性或其他属性的文档。我已经知道我需要定制的东西:

  1. 用于指定输出文件的标志:-fr=filename-fs=filenamevs-o filename
  2. 隐式依赖生成:有人告诉我,它会在后台cc_rules生成.d文件以检测您是否缺少依赖项。我不确定这是否属实,但如果是这样,我需要更改使用的标志和扩展名
  3. 对象和库文件的扩展名:如上所述,我们为多个CPU构建相同的文件,需要为规则自定义输出的扩展名。

可能还有其他我不知道的要求。很可能我做错了,应该采取不同的方法。我们从早期就一直在使用 Bazel v0.12

我们目前在v1.1.0,我从v0.12六个月前移植到我们。我很惊讶主分支已经打开了 v3.???!!!

任何帮助是极大的赞赏。如果我遗漏了任何重要的事情,请提醒我。

编辑:需要注意的一件事是编译器似乎基于clangand llvm。如果有基于clang/llvm的工具链的示例(我很确定有),那么我可以从那里开始。

我知道enscriptem文档中的示例在技术上是基于 LLVM 的编译器,但它使用脚本对参数等进行魔术处理。如果这样做是正确的,我可以这样做,但我想确保我我朝着正确的方向前进。

标签: buildcross-compilingbazel

解决方案


我认为添加您自己提供的自定义规则CcToolchainConfigInfo将解决您遇到的问题。

def _impl(ctx):
    tool_paths = [
        tool_path(name = "gcc", path = "/<abs>/<path>/clang"),
        tool_path(name = "ld", path = "/<abs>/<path>/ld"),
        tool_path(name = "ar", path = "/<abs>/<path>/ar"),
        tool_path(name = "cop", path = "/bin/false"),
        tool_path(name = "gcov", path = "/bin/false"),
        tool_path(name = "nm", path = "/bin/false"),
        tool_path(name = "objdump", path = "/bin/false"),
        tool_path(name = "strip", path = "/bin/false"),
    ]

    toolchain_compiler_flags = feature(
        name = "compiler_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = [
                    ACTION_NAMES.assemble,
                    ACTION_NAMES.preprocess_assemble,
                    ACTION_NAMES.linkstamp_compile,
                    ACTION_NAMES.c_compile,
                    ACTION_NAMES.cpp_compile,
                    ACTION_NAMES.cpp_header_parsing,
                    ACTION_NAMES.cpp_module_compile,
                    ACTION_NAMES.cpp_module_codegen,
                    ACTION_NAMES.lto_backend,
                    ACTION_NAMES.clif_match,
                ],
                flag_groups = [
                    flag_group(flags = ["<compiler-flags>"]),
                ],
            ),
        ],
    )

    toolchain_linker_flags = feature(
        name = "linker_flags",
        enabled = True,
        flag_sets = [
            flag_set(
                actions = [
                    ACTION_NAMES.linkstamp_compile,
                ],
                flag_groups = [
                    flag_group(flags = ["<linker-flags>"]),
                ],
            ),
        ],
    )

    return cc_common.create_cc_toolchain_config_info(
        ctx = ctx,
        toolchain_identifier = ctx.attr.toolchain_identifier,
        host_system_name = ctx.attr.host_system_name,
        target_system_name = "<your-system-name>",
        target_cpu = "<your-cpu>",
        target_libc = "<your-libc>",
        compiler = "<your-compiler>,
        abi_version = "<your-eabiu>",
        abi_libc_version = <your-version>,
        tool_paths = tool_paths,
        features = [
            toolchain_compiler_flags,
            toolchain_linker_flags,
            <more-custom-features>,
        ],
    )

cc_arm_none_eabi_config = rule(
    implementation = _impl,
    attrs = {
        "toolchain_identifier": attr.string(default = ""),
        "host_system_name": attr.string(default = ""),
    },
    provides = [CcToolchainConfigInfo],
)

我在 Github 上发布了一个关于使用 GCC 嵌入式工具链和 Bazel的示例,您可以将其用作模板。该示例适用于arm-none-eabi-gcc编译器,但原则上,它也适用于clang.


推荐阅读