我一直在尝试编写一条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有什么见解吗?或者有没有更好的方法来做到这一点?文档似乎对此一无所知。
发布于 2020-01-24 08:43:01
糟糕--在浏览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。如果有人知道如何让cc_toolchain_suite不可知,我很想听听。干杯。
https://stackoverflow.com/questions/59888331
复制相似问题