0

我在 BUILD 文件中有这个 genrule,但 bazel build 因以下错误而失败:

在 genrule 规则的 cmd 属性中 //example:create_version_pom: $(BUILD_TAG) not defined

genrule(
    name = "create_version_pom",
    srcs = ["pom_template.xml"],
    outs = ["version_pom.xml"],
    cmd = "sed 's/BUILD_TAG/$(BUILD_TAG)/g' $< > $@",
)

请问是什么原因,怎么解决?

4

1 回答 1

1

cmd属性genrule会在执行命令之前对 Bazel 构建变量进行变量扩展。输入文件和输出文件的$<$@变量是一些预定义的变量。变量可以用 定义--define,例如:

$ cat BUILD
genrule(
  name = "genfoo",
  outs = ["foo"],
  cmd = "echo $(bar) > $@",
)

$ bazel build foo --define=bar=123
INFO: Analyzed target //:foo (5 packages loaded, 8 targets configured).
INFO: Found 1 target...
Target //:foo up-to-date:
  bazel-bin/foo
INFO: Elapsed time: 0.310s, Critical Path: 0.01s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions

$ cat bazel-bin/foo
123

所以要$(BUILD_TAG)在一般情况下工作,你会想要通过
--define=BUILD_TAG=the_build_tag

除非你想BUILD_TAG用字面意思替换$(BUILD_TAG),在这种情况下$需要用另一个$:转义$$(BUILD_TAG)


https://docs.bazel.build/versions/main/be/general.html#genrule.cmd
https://docs.bazel.build/versions/main/be/make-variables.html

请注意,Bazel 还具有“构建标记”机制,用于将构建时间和版本号等信息带入构建:
https ://docs.bazel.build/versions/main/user-manual.html#workspace_status
https:// docs.bazel.build/versions/main/command-line-reference.html#flag--embed_label

使用--workspace_status_commandand--embed_label有点复杂。

于 2021-09-07T23:57:39.560 回答