Search code examples
bashbazel

Bazel genrule: Use linebreaks in command


I use a genrule with a lot of sources, that have a long identifier. The command needs to list all sources explicitely, which would result in a reeaally long cmd. Therefore I tried to use linebreaks (as known from bash or shell commands)... However, bazel complains about unterminated strings.

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"],
)

Escaping the \ with $ does not change anything...


Solution

  • As in Python, you can use triple-quotes to preserve the newlines:

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