【问题标题】:Create and read temporary file with Bazel使用 Bazel 创建和读取临时文件
【发布时间】:2017-09-13 08:45:03
【问题描述】:

我正在尝试使用 Bazel 为 TensorFlow 设置我的自定义构建系统。 我需要在配置步骤中创建一个临时文件,我将在构建步骤中将其用作缓存。 我能够在我的 .bzl 文件中创建它:

repository_ctx.file(filename)

配置后我可以在文件夹中看到它:

$HOME/.cache/bazel/_bazel_$USERNAME/%MD5%/external/%repository_name%

其中 repository_name 实际上是 repository_ctx.name

但在构建期间,我无法从我的 .tpl 脚本访问此路径。我想通过 repository_ctx.template 的替换从 .bzl 脚本发送它,但即使在这里我也没有找到如何找到这条路径!

看起来我无法使用 bazel-out 或 bazel-genfiles 之类的符号链接访问此文件夹(这听起来很有希望,但不是......)。我也无法在此文件夹之外创建文件。

这是一个非常简单的问题,我无法相信除了硬编码路径或 find 它之外别无他法......

【问题讨论】:

    标签: bazel custom-build


    【解决方案1】:

    我猜问题是您没有在 repo 的 BUILD 文件中公开该文件以供您的构建使用它。您需要执行以下操作:

    repository_ctx.file('whatever')
    repository_ctx.file('BUILD', 'exports_files(["whatever"])')
    

    然后您可以在任何其他 BUILD 文件中使用@repository_name//:whatever

    exports_files 用于让包依赖其他包的源文件。

    然后,在构建期间加载的 .bzl 文件中,将该文件添加为属性,例如,

    attrs = {
        '_my_external_src' : attr.label(
            default = Label('@my_repository//:whatever'),
            allow_single_file = True,
        ),
    },
    

    那么,在规则实现中,就可以通过上下文来访问了:

    def _impl(ctx):
        print(ctx.file._my_external_src.path)
    

    .path 获取文件的路径。

    【讨论】:

    • 嗯,我明白了。所以我必须找到一种方法将这个@repository_name//:whatever 从 BUILD 文件发送到 tpl 文件。我想我会接受你的回答,谢谢!
    • 没问题!添加了如何从 .bzl 文件访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多