【问题标题】:Modifying include paths in custom C++ Bazel rule修改自定义 C++ Bazel 规则中的包含路径
【发布时间】: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 相同的功能的最佳方法是什么?

【问题讨论】:

标签: c++ include bazel bazel-rules


【解决方案1】:

您可以在包内的目录中创建所需的标题布局,然后通过create_compilation_outputs.includes 将其添加到包含路径。这基本上就是 cc_library 的实现。

cc_library 实现names it _virtual_includes,并使用getUniqueDirectoryArtifact 创建它,这是Java 代码中创建它的助手"in a directory that is unique to the rule"。我使用 "_%s_virtual_includes" % ctx.label.name 之类的东西从 Starlark 获得类似的功能,并暗示这是一个私有的实现细节,其他规则应该避免依赖它。

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多