【发布时间】:2020-05-24 19:50:23
【问题描述】:
我正在构建一些自定义 C++ Bazel 规则,并且我需要添加对修改 C++ 标头的包含路径的支持,就像 cc_library 标头可以使用 strip_include_prefix 修改一样。
我的自定义规则是使用ctx.actions.run 实现的,如下所示:
custom_cc_library = rule(
_impl,
attrs = {
...
"hdrs": attr.label_list(allow_files = [".h"]),
"strip_include_prefix": attr.string(),
...
},
)
然后在_impl内我调用下面的函数重写hdrs:
def _strip_prefix(ctx, hdrs, prefix):
stripped = []
for hdr in hdrs:
stripped = hdr
if file.path.startswith(strip_prefix):
stripped_file = ctx.actions.declare_file(file.path[len(strip_prefix):])
ctx.actions.run_shell(
command = "mkdir -p {dest} && cp {src} {dest};".format(src=hdr.path, dest=stripped.path),
inputs = [hdr],
outputs = [stripped],
)
stripped.append(stripped_file)
return stripped
这不起作用,因为 Bazel 不会将文件复制到他们的包目录之外,而且感觉实现它的方法完全错误。
修改依赖项的 C++ 头目录以实现与 cc_library 的参数 strip_include_prefix 相同的功能的最佳方法是什么?
【问题讨论】:
-
不幸的是,cc_library rule 及其stripIncludePrefix implementation 在 Bazel 的 Java 代码中。
-
@Laurenz 是的,我知道,很遗憾。但我希望找到一种在 Starlark 中实现类似功能的方法。
标签: c++ include bazel bazel-rules