首页 > 解决方案 > Bazel genrule:在命令中使用换行符

问题描述

我使用一个有很多来源的类型,它们有一个很长的标识符。该命令需要明确列出所有源,这将导致 cmd 非常长。因此我尝试使用换行符(从 bash 或 shell 命令中知道)......但是,bazel 抱怨未终止的字符串。

genrule(
  name = "Aggregate_Reports",
  srcs = ["//really/long/path/to/module/ModuleA/src:CoverageHtml",
          "//really/long/path/to/module/ModuleA/src:TestRun",
          "//really/long/path/to/module/ModuleB/src:CoverageHtml",],
  outs = ["UT_Summary.txt"],
  message = "Create unified report",
  tools = [":Create_Summary"],
  cmd = "$(location :Create_Summary) -t \
              $(location //really/long/path/to/module/ModuleA/src:TestRun) \
              $(location //really/long/path/to/module/ModuleB/src:TestRun) \
                                     -c \
              $(location //really/long/path/to/module/ModuleA/src:CoverageHtml) \
              $(location //really/long/path/to/module/ModuleB/src:CoverageHtml) \
              -o $(@)",
  executable = True,
  visibility=["//visibility:public"],
)

用 $ 转义 \ 不会改变任何东西......

标签: bashbazel

解决方案


与在 Python 中一样,您可以使用三引号来保留换行符:

  cmd = """$(location :Create_Summary) -t \
              $(location //really/long/path/to/module/ModuleA/src:TestRun) \
              $(location //really/long/path/to/module/ModuleB/src:TestRun) \
                                     -c \
              $(location //really/long/path/to/module/ModuleA/src:CoverageHtml) \
              $(location //really/long/path/to/module/ModuleB/src:CoverageHtml) \
              -o $(@)""",

推荐阅读