我一直在尝试编写一个 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/