首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在bazel规则中使用的自定义cc_toolchain

在bazel规则中使用的自定义cc_toolchain
EN

Stack Overflow用户
提问于 2020-01-24 07:09:58
回答 1查看 536关注 0票数 2

我一直在尝试编写一条bazel规则来对risc-v源文件进行包装编译,做一些其他的事情,等等,但我在获得CcToolchainInfo提供商方面遇到了一些问题。

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

代码语言:javascript
复制
rv_cc_toolchain_config = rule(
    implementation = _impl,
    attrs = {},
    provides = [CcToolchainConfigInfo],
) 

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

代码语言:javascript
复制
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编译我的自定义规则:

代码语言:javascript
复制
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有什么见解吗?或者有没有更好的方法来做到这一点?文档似乎对此一无所知。

EN

回答 1

Stack Overflow用户

发布于 2020-01-24 08:43:01

糟糕--在浏览github之后就明白了这一点。原来问题是直接引用cc_toolchain是不正确的,而且CcToolchainInfo是通过cc_toolchain_suite提供的

toolchains/BUILD更新为如下所示

代码语言:javascript
复制
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编译规则类似于

代码语言:javascript
复制
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。如果有人知道如何让cc_toolchain_suite不可知,我很想听听。干杯。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59888331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档