【问题标题】:Bazel: why lint failed for variable reference?Bazel:为什么 lint 因变量引用而失败?
【发布时间】:2021-09-07 23:15:42
【问题描述】:

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

在 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' $< > $@",
)

请问是什么原因,如何解决?

【问题讨论】:

    标签: bazel bazel-rules bazel-aspect


    【解决方案1】:

    genrulecmd 属性将在执行命令之前对 Bazel 构建变量进行变量扩展。输入文件和输出文件的$&lt;$@ 变量是一些预定义变量。变量可以用--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) 在 genrule 中工作,您需要通过
    --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_command--embed_label 有点复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 2012-04-06
      • 2012-09-08
      • 2013-09-24
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多