How to make cmdline with bazel genrule concatenate multiple srcs with cmdline options?

303 Views Asked by At

I have a cmdline tool which can accept multiple inputs with different cmd options.

mytool --include=some_include1 --include=some_include2 --inlcude=... source1 source2 ...

So I have to write a genrule with

filegroup(
  name = "include_group",
  srcs = ["some_include1", "some_include2"],
)

filegroup(
  name = "source_group",
  srcs = ["source1", "source2"],
)

genrule(
  name = "myrule",
  srcs = [":include_group"] + [":source_group"],
  outs = ["out.cpp"],
  tools = ["mytool"],
  cmd = "$(location :mytool) --include=$(locations :path_group) $(locations :source_group)"
)

The cmd line tunout to be:

mytool --include=some_include1 some_include2 source1 source2 

Notice that the option label of some_include2 is missing.

So my question is: how to add the option label to each label in :include_group.

1

There are 1 best solutions below

0
On

The genrule's cmd attribute is shell, which can be used for such expansion:

genrule(
  name = "myrule",
  srcs = [":include_group"] + [":source_group"],
  outs = ["out.cpp"],
  tools = ["mytool"],
  cmd = """
includes=($(locations :include_group))
$(location :mytool) $${includes[@]/#/--include=} $(locations :source_group)
"""
)

There are other, possibly more portable, ways such as writing a wrapper script for mytool that reads the includes from a file. A custom Bazel rule could also easily generate such a command line.