Bazel:将多个文件复制到二进制目录

Ser*_*Ch. 8 bazel

我需要在保留文件名的同时将一些文件复制到二进制目录。到目前为止,我得到的是:

filegroup(
    name = "resources",
    srcs = glob(["resources/*.*"]),
)

genrule(
    name = "copy_resources",
    srcs = ["//some/package:resources"],
    outs = [ ],
    cmd = "cp $(SRCS) $(@D)",
    local = 1,
    output_to_bindir = 1,
)
Run Code Online (Sandbox Code Playgroud)

现在,我必须在其中指定文件名,outs但似乎无法弄清楚如何解析标签以获得实际的文件名。

pes*_*ous 14

为了使 afilegroup可用于二进制文件(使用 执行bazel run)或测试(使用 执行时bazel test),通常将 afilegroup列为data二进制文件的一部分,如下所示:

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    data = [
        "//your_project/other/deeply/nested/resources:other_test_files",
    ],
)
# known to work at least as of bazel version 0.22.0
Run Code Online (Sandbox Code Playgroud)

通常以上就足够了。

但是,可执行文件必须递归遍历目录结构"other/deeply/nested/resources/"才能从指定的filegroup.

换句话说,在填充runfiles可执行文件时,bazel保留从WORKSPACE根目录到包含给定filegroup.

有时,这种保留的目录嵌套是不可取的。


挑战:

在我的例子中,我filegroup在我的项目目录树中的不同位置有几个s,我希望这些组的所有单个文件最终并排出现runfiles将使用它们的测试二进制文件的集合中。


我用 a 来做这件事的尝试genrule没有成功。

为了从多个filegroups复制单个文件,保留每个文件的基本名称但扁平化输出目录,有必要在bazel 扩展名中创建自定义规则bzl

值得庆幸的是,自定义规则相当简单。

cp在 shell 命令genrule中的使用与原始问题中列出的未完成命令非常相似。

扩展文件:

# contents of a file you create named: copy_filegroups.bzl
# known to work in bazel version 0.22.0
def _copy_filegroup_impl(ctx):
    all_input_files = [
        f for t in ctx.attr.targeted_filegroups for f in t.files
    ]

    all_outputs = []
    for f in all_input_files:
        out = ctx.actions.declare_file(f.basename)
        all_outputs += [out]
        ctx.actions.run_shell(
            outputs=[out],
            inputs=depset([f]),
            arguments=[f.path, out.path],
            # This is what we're all about here. Just a simple 'cp' command.
            # Copy the input to CWD/f.basename, where CWD is the package where
            # the copy_filegroups_to_this_package rule is invoked.
            # (To be clear, the files aren't copied right to where your BUILD
            # file sits in source control. They are copied to the 'shadow tree'
            # parallel location under `bazel info bazel-bin`)
            command="cp $1 $2")

    # Small sanity check
    if len(all_input_files) != len(all_outputs):
        fail("Output count should be 1-to-1 with input count.")

    return [
        DefaultInfo(
            files=depset(all_outputs),
            runfiles=ctx.runfiles(files=all_outputs))
    ]


copy_filegroups_to_this_package = rule(
    implementation=_copy_filegroup_impl,
    attrs={
        "targeted_filegroups": attr.label_list(),
    },
)
Run Code Online (Sandbox Code Playgroud)

使用它:

# inside the BUILD file of your exe
load(
    "//your_project:copy_filegroups.bzl",
    "copy_filegroups_to_this_package",
)

copy_filegroups_to_this_package(
    name = "other_files_unnested",
    # you can list more than one filegroup:
    targeted_filegroups = ["//your_project/other/deeply/nested/library:other_test_files"],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    data = [
        ":other_files_unnested",
    ],
)
Run Code Online (Sandbox Code Playgroud)

您可以在此处克隆一个完整的工作示例

  • @DavidSauter 添加 `to_list()` 适用于行 `f for t in ctx.attr.targeted_filegroups for f in t.files.to_list()`。这是来自https://docs.bazel.build/versions/0.18.1/skylark/backward-compatibility.html#depset-is-no-longer-iterable (2认同)