如何强制Bazel为所有二进制文件打上时间戳?

huangapple go评论82阅读模式
英文:

How to force Bazel to stamp all binaries?

问题

--stamp参数在Bazel用户手册中的文档中说明:

> 如果依赖项未更改,指定--stamp不会强制重新构建受影响的目标。

是否有一种方法可以强制构建受影响的目标,以便所有输出二进制文件都具有相同的标记,即使它们的依赖项未更改?

具体的用例是,我正在构建大量相关的Go二进制文件(使用rules_go),我希望可靠地为它们全部打上相同的版本号(取自最新的git提交哈希)。我可以在之前运行bazel clean,但这在某种程度上违背了使用Bazel的初衷 如何强制Bazel为所有二进制文件打上时间戳?

谢谢!

英文:

The documentation for --stamp in the Bazel User Manual states:

> Specifying --stamp does not force affected targets to be rebuilt, if their dependencies have not changed.

Is there a way to force affected targets to be built so that all output binaries have the same stamp, even if their dependencies have not changed?

The specific use case is that I'm building a large number of related Go binaries (using rules_go), and I'd like to reliably stamp them all with the same version number (taken from the latest git commit hash). I could do a bazel clean beforehand, but this somewhat defeats the point of using Bazel 如何强制Bazel为所有二进制文件打上时间戳?

Thanks!

答案1

得分: 1

你能将.git/refs/heads/<release branch>文件添加为数据输入吗?这样当提交发生变化时,你的输入将会自动改变。你可以将它包装在一个genrule中,以添加一些检查或避免在开发分支上重新构建所有内容:

genrule(
    name = "stamper",
    outs = ["stamper.out"],
    srcs = [
        ".git/HEAD",
        ".git/refs/heads/master",
    ],
    cmd = """
    if [[ $$(cat $(location :.git/HEAD)) = "refs: refs/heads/<release branch>" ]]; then
        cat $(location :.git/refs/heads/master)
    else
        # 如果我们不在发布分支上,不要在提交时取消缓存。
        echo "dev"
    fi
    """,
)

如果你感兴趣,可以跟踪/评论这个错误,它可以强制重新运行操作。

英文:

Could you add the .git/refs/heads/&lt;release branch&gt; file as a data input? Then when the commit changes, your inputs will change "automatically." You could wrap it in a genrule to add some check or avoid rebuilding everything on the development branch:

genrule(
    name = &quot;stamper&quot;,
    outs = [&quot;stamper.out&quot;],
    srcs = [
      	&quot;.git/HEAD&quot;,
        &quot;.git/refs/heads/master&quot;,
    ],
    cmd = &quot;&quot;&quot;
if [[ $$(cat $(location :.git/HEAD)) = &quot;refs: refs/heads/&lt;release branch&gt;&quot; ]]; then
  cat $(location :.git/refs/heads/master)
else
  # If we&#39;re not on the release branch, don&#39;t uncache things on commit.
  echo &quot;dev&quot;  
fi
&quot;&quot;&quot;,
)

There's a bug for forcing actions to be rerun that you could track/comment on, if interested.

huangapple
  • 本文由 发表于 2017年6月23日 00:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/44704728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定