文档说,“ stamp”参数将“启用链接标记”,并将“将构建信息编码为二进制”和“将构建信息压缩为二进制”
但是这些信息实际上在哪里,如何获取呢?
它似乎不是简单的搜索和替换,变量定义,环境变量或格式字符串变量。测试python脚本:
BUILD_HOST="ASDF"
print("{BUILD_HOST}")
print(BUILD_HOST)
构建规则:
py_binary(name="catself", srcs=["catself.py"], stamp=1)
输出:
$ bazel run --stamp catself
INFO: Analyzed target //:catself (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:catself up-to-date:
bazel-bin/catself
INFO: Elapsed time: 0.083s, Critical Path: 0.01s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
{BUILD_HOST}
ASDF
答案 0 :(得分:2)
看来py_binary
可能未启用标记。不过,它应该适用于genrule
,因此类似这样的简单事情应该起作用:
py_binary(
name = "foo",
srcs = ["foo.py"],
data = [":stable-status.txt"],
)
genrule(
name = "copy_stable-status.txt",
outs = ["stable-status.txt"],
cmd = "cp bazel-out/stable-status.txt $@",
stamp = 1,
)
foo.py
:
build_info = {}
with open("stable-status.txt") as stable_status:
for line in stable_status.readlines():
key, val = line.split(" ", 1)
build_info[key] = val.strip()
print("Build label is:")
print(build_info['BUILD_EMBED_LABEL'])
然后:
$ bazel run foo --embed_label=foobar
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:foo (17 packages loaded, 102 targets configured).
INFO: Found 1 target...
Target //:foo up-to-date:
bazel-bin/foo
INFO: Elapsed time: 2.985s, Critical Path: 0.11s
INFO: 1 process: 1 linux-sandbox.
INFO: Build completed successfully, 6 total actions
INFO: Build completed successfully, 6 total actions
Build label is:
foobar
或更喜欢的东西:
py_binary(
name = "bar",
srcs = ["bar.py"],
deps = [":buildinfo"],
)
py_library(
name = "buildinfo",
srcs = [":buildinfo.py"],
)
genrule(
name = "gen_buildinfo",
outs = ["buildinfo.py"],
cmd = r"""sed -E 's/(.*) (.*)/\1 = "\2"/' bazel-out/stable-status.txt > $@""",
stamp = 1,
)
bar.py
:
import buildinfo
print("Build label is:")
print(buildinfo.BUILD_EMBED_LABEL)