Bazel:如何将 headers 合并到一个包含路径中

Bazel: how to glob headers into one include path

在 Buck 中,可以这样写:

exported_headers = subdir_glob([
    ("lib/source", "video/**/*.h"),
    ("lib/source", "audio/**/*.h"),
],
excludes = [
    "lib/source/video/codecs/*.h",
],
prefix = "MediaLib/")

此行将使那些 headers 在 MediaLib/ 下可用。 Bazel 中的等价物是什么?

所以我没有测试它,但从文档来看它应该类似于:

cc_library(
name = "foo",
srcs = glob([
    "video/**/*.h",
    "audio/**/*.h",
 ],
excludes = [ "lib/source/video/codecs/*.h" ]
),
include_prefix = "MediaLib/"
)

https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.include_prefix https://docs.bazel.build/versions/master/be/functions.html#glob

我最终写了一条规则来做到这一点。它提供类似于文件组输出的内容,并且可以在宏中与 cc_library 组合。

def _impl_flat_hdr_dir(ctx):
    path = ctx.attr.include_path
    d = ctx.actions.declare_directory(path)
    dests = [ctx.actions.declare_file(path + "/" + h.basename)
             for h in ctx.files.hdrs]

    cmd = """
        mkdir -p {path};
        cp {hdrs} {path}/.
        """.format(path=d.path, hdrs=" ".join([h.path for h in ctx.files.hdrs]))

    ctx.actions.run_shell(
       command = cmd,
       inputs = ctx.files.hdrs,
       outputs = dests + [d],
       progress_message = "doing stuff!!!"
    )

    return struct(
       files = depset(dests)
    )

flat_hdr_dir = rule(
    _impl_flat_hdr_dir,
    attrs = {
        "hdrs": attr.label_list(allow_files = True),
        "include_path": attr.string(mandatory = True),
    },
    output_to_genfiles = True,
)