【问题标题】:What is the most efficient way to extract/collect files from a list of targets/providers in Bazel?从 Bazel 中的目标/提供者列表中提取/收集文件的最有效方法是什么?
【发布时间】:2020-01-22 12:44:25
【问题描述】:

我正在编写一些规则并随着我的进步学习 Starlark。

假设我有自己的提供者:

ModularResources = provider(
    doc = "Modular resources",
    fields = {
        "artifactId": "Former Maven artifact id (don't ask me why)",
        "srcs": "List of labels (a glob(..) thing)",
    },
)

def _modular_resources_impl(ctx):
    return ModularResources(
        artifactId = ctx.attr.artifactId,
        srcs = ctx.attr.srcs,
    )

modular_resources = rule(
    implementation = _modular_resources_impl,
    attrs = {
        "artifactId": attr.string(
            mandatory = True,
        ),
        "srcs": attr.label_list(
            allow_files = True,
            mandatory = True,
        ),
    },
)

然后我有一个需要这些的生成器规则:

some_generator = rule(
    attrs = {
        "deps": attr.label_list(
            providers = [ ModularResources ]
        ),
        ...
    },
    ...
)

在我的实现中,我发现我需要进行几次解包才能获取文件:

def _get_files(deps):
    result = []
    for dep in deps:
        for target in dep[ModularResources].srcs:
            result += target.files.to_list()
    return result

有没有更有效的方式来执行收集?

至于为什么我这样做,生成器实际上需要一个特殊的文件列表,如下所示:

def _format_files(deps):
    formatted = ""
    for dep in deps:
        for target in dep[ModularResources].srcs:
            formatted += ",".join([dep[ModularResources].artifactId + ":" + f.path for f in target.files.to_list()])
    return formatted

FWIW,这是一个如何使用的示例:

a/BUILD:

modular_resources(
    name = "generator_resources",
    srcs = glob(
        ["some/path/**/*.whatever"],
    ),
    artifactId = "a",
    visibility = ["//visibility:public"],
)

b/BUILD:

some_generator(
    name = "...",
    deps = [
        "//a:generator_resources"
    ]
)

【问题讨论】:

  • 你的实现在我看来是合理的。你真的观察到性能问题吗?
  • 没有。我还没有到我正在调查性能问题的地步。只是好奇正确的方法(tm)。

标签: bazel starlark bazel-rules


【解决方案1】:

如果你想用内存换取更好的性能,如果在提供程序中完成,也许操作可以更容易地被 blaze 并行化:

def _modular_resources_impl(ctx):
    return ModularResources(
        artifactId = ctx.attr.artifactId,
        formatted_srcs = ",".join([artifactId + ":" + f.path for f in ctx.files.src])
    )

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2010-09-25
    • 2011-04-28
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多