【问题标题】:Bazel: genrule to touch (create) two filesBazel:触摸(创建)两个文件的类型
【发布时间】:2020-06-15 15:23:45
【问题描述】:

我正在尝试创建一个适用于 windows 和 unix 的 genrule,如果它们尚不存在,则会创建两个文件。

对于单个文件,以下工作:

genrule(
  name = "create_files",
  outs = ["path/file1.h"],
  cmd_bat = "echo. >> $@",
  cmd = "touch $@",
)

但现在我想创建两个文件,以下似乎不起作用:

genrule(
  name = "create_files",
  outs = ["path/file1.h",
          "path/file2.h"],
  cmd_bat = "echo. >> $(OUTS)[0] && echo. >> $(OUTS)[1]",
  cmd = "touch $(OUTS)[0] && touch $(OUTS)[1]",
)

我收到了错误

declared output '...path/file2.h' was not created by genrule.

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: bazel


    【解决方案1】:

    问题在于变量替换的工作方式和数组在 shell 中的工作方式。

    cmd = "touch $(OUTS)[0] && touch $(OUTS)[1]"

    Bazel 将$(OUTS) 替换为输出文件的路径,使用空格作为分隔符。见https://docs.bazel.build/versions/master/be/make-variables.html

    所以命令变成了

    touch path/file1.h path/file2.h[0] && touch path/file1.h path/file2.h[1]

    所以你得到了两次path/file1.h,以及两个无关的文件。

    要完成这项工作,您只需要:

    cmd = "touch $(OUTS)"

    我不确定如何在 Windows 上为 cmd_bat 执行此操作。我不知道有一个内置工具可以创建多个空文件,例如touch。您可能需要遍历 $(OUTS) 并执行 type NUL > file 或类似的操作。

    【讨论】:

    • 我想在执行本质上是一个 cmd 批处理时,可以在 $(OUTS) 中的项目上运行 for
    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2014-02-17
    • 2011-12-10
    相关资源
    最近更新 更多