【问题标题】:custom cc_toolchain used in bazel rulebazel 规则中使用的自定义 cc_toolchain
【发布时间】:2020-01-23 23:09:58
【问题描述】:

我一直在尝试编写一个 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 有任何见解吗?或者有没有更好的方法来做到这一点?文档似乎对此一无所知。

【问题讨论】:

    标签: build bazel riscv


    【解决方案1】:

    哎呀——在浏览 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"]
    )
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2022-06-15
      • 2013-04-08
      相关资源
      最近更新 更多