0

I have a binary that takes as input a single file and produces an unknown number of header and source C++ files into a single directory. I would like to be able to write a target like:

x_library(
  name = "my_x_library",
  src = "source.x",
)

where x_library is a macro that ultimately produces the cc_library from the output files. However, I can't bundle all the output files inside the rule implementation or inside the macro. I tried this answer but it doesn't seem to work anymore.

What's the common solution to this problem? Is it possible at all?

ale64bit
  • 6,232
  • 3
  • 24
  • 44

1 Answers1

0

Small example of a macro using a genrule (not a huge fan) to get one C file and one header and provide them as a cc_library:

def x_library(name, src):

    srcfile = "{}.c".format(name)
    hdrfile = "{}.h".format(name)

    native.genrule(
       name = "files_{}".format(name),
       srcs = [src],
       outs = [srcfile, hdrfile],
       cmd = "./generator.sh $< $(OUTS)",
       tools = ["generator.sh"],
    )

    native.cc_library(
       name = name,
       srcs = [srcfile],
       hdrs = [hdrfile],
    )

Used it like this then:

load(":myfile.bzl", "x_library")

x_library(
  name = "my_x_library",
  src = "source.x",
)

cc_binary(
  name = "tgt",
  srcs = ["mysrc.c"],
  deps = ["my_x_library"],
)

You should be able to extend that with any number of files (and for C++ content; IIRC the suffices are use for automagic decision how to call the tools) as long as your generator input -> generated content is known and stable (generally a good thing for a build). Otherwise you can no longer use genrule as you need your custom rule (probably a good thing anyways) to use TreeArtifact as described in the linked answer. Or two, one with .cc suffix and one with .hh so that you can pass them to cc_library.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39