build - bazel 规则中使用的自定义 cc_toolchain

标签 build bazel riscv

我一直在尝试编写一个 bazel 规则来包装 risc-v 源文件的编译,执行一些其他操作等,但我在获取 CcToolchainInfo 提供程序时遇到了一些麻烦.

我有一个有效的规则,看起来像

rv_cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
) 

为了提供配置信息。我在 toolchains/BUILD 中有以下内容:

load(":cc_toolchain_config.bzl", "rv_cc_toolchain_config")

package(default_visibility = ['//visibility:public'])


rv_cc_toolchain_config(name="rv_toolchain_cfg")


cc_toolchain(
    name='rv_toolchain',
    toolchain_identifier='rv-toolchain',
    toolchain_config=':rv_toolchain_cfg',
    all_files=':nofile',
    strip_files=':nofile',
    objcopy_files=':nofile',
    dwp_files=':nofile',
    compiler_files=':nofile',
    linker_files=':nofile',
)

这似乎一切正常;然后我使用 riscv 编译自定义规则:

def _compile_impl(ctx):
    deps = []
    cc_toolchain = find_cpp_toolchain(ctx)
    print(ctx.attr._cc_toolchain)
    compilation_contexts = [dep[CcInfo].compilation_context for dep in deps]
    print(type(cc_toolchain))
    feature_configuration = cc_common.configure_features( #fails here
        ctx = ctx,
        cc_toolchain = cc_toolchain,
        requested_features = ctx.features,  #currently does nothing
        unsupported_features = ctx.disabled_features,
    )




rv_compile = rule(
    _compile_impl,
    output_to_genfiles = True,
    attrs = {
        "srcs": attr.label_list(
            doc = "List of source files",
            mandatory = False,
            allow_files = [".cc", ".cpp", ".h", ".c"],
        ),
        "hdrs": attr.label_list(
            doc = "List of header files",
            allow_files = [".h"],
        ),
        "_cc_toolchain": attr.label(
            #default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
            default = Label("//toolchains:rv_toolchain")
        ),
    },
    provides = [
        DefaultInfo,
        CcInfo,
    ],
    toolchains = [
        "@bazel_tools//tools/cpp:toolchain_type",
    ],
    fragments = ["cpp"]
)

尝试配置工具链时失败,因为 cc_toolchain 的类型为 ToolchainInfo,而不是所需的 CcToolchainInfo。有谁对如何在规则中提供 CcToolchainInfo 有任何见解吗?或者有更好的方法吗?文档似乎对此一无所知。

最佳答案

哎呀——在浏览 github 后发现了这一点。事实证明,问题是直接引用 cc_toolchain 不正确,并且 CcToolchainInfo 是通过 cc_toolchain_suite

提供的

更新toolchains/BUILD以使其看起来像

load(":cc_toolchain_config.bzl", "rv_cc_toolchain_config")

package(default_visibility = ['//visibility:public'])


rv_cc_toolchain_config(name="rv_toolchain_cfg")


filegroup(name = 'empty')
cc_toolchain(
    name='rv_toolchain',
    toolchain_identifier='sanity-toolchain',
    toolchain_config=':rv_toolchain_cfg',
    all_files=':empty',
    strip_files=':empty',
    objcopy_files=':empty',
    dwp_files=':empty',
    compiler_files=':empty',
    linker_files=':empty',
)


cc_toolchain_suite(
    name='rv',
    toolchains={
        'darwin': ':rv_toolchain', #use whatever OS you need here... 
    }
)

并且 rv 编译规则类似于


rv_compile = rule(
    _compile_impl,
    output_to_genfiles = True,
    attrs = {
        "srcs": attr.label_list(
            doc = "List of source files",
            mandatory = False,
            allow_files = [".cc", ".cpp", ".h", ".c"],
        ),
        "hdrs": attr.label_list(
            doc = "List of header files",
            allow_files = [".h"],
        ),
        "_cc_toolchain": attr.label(
            #default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
            default = Label("//toolchains:rv")
        ),
    },
    provides = [
        DefaultInfo,
        CcInfo,
    ],
    toolchains = [
        "@bazel_tools//tools/cpp:toolchain_type",
    ],
    fragments = ["cpp"]
)

工作起来就像一个魅力:)任何阅读本文的人都应该也启用实验性的 lark cpp api。如果有人知道如何使 cc_toolchain_suite cpu 不可知,我很想听听。干杯。

关于build - bazel 规则中使用的自定义 cc_toolchain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59888331/

相关文章:

c++ - Bazel 和自动生成的 cpp/hpp 文件

disassembly - RISCV 反汇编选项数字和无别名

gcc - Riscv GCC 4.9.2无法编译linux 3.14.29

c++ - 在 Meson 中,我是否可以避免不断地从源代码跳转到构建目录并返回?

java - 让 Maven 复制构建 jar 中的其他文件(不是资源,而是任何包中的任何文件)?

java - 为什么我通过 Bazel 构建的应用程序在添加 Firebase 依赖项后一打开就会崩溃?

go - Bazel go_binary c-共享链接模式 : Where is the header?

riscv - 关于 "jump"和 "jump and link"之间翻译的问题

Go 服务的部署策略?

ios - 如何使用适用于 Apache Cordova 和 Xcode 8 的 Visual Studio 工具构建 iOS 应用程序