0

Is there a way to control the Bazel build to generate wanted temp files for a list of source files instead of just using the command line option "--save_temps"?

One way is using a cc_binary, and add "-E" option in the "copts", but the obj file name will always have a ".o". This kind of ".o" files will be overwriten by the other build targets. I don't know how to control the compiler output file name in Bazel.

Any better ideas?

  • As for .o files you can get them by unpacking the .a file ("a" stands for archive) using a genrule. – Andreas Dec 14 '21 at 08:22

2 Answers2

0

cc_library has an output group with the static library, which you can then extract. Something like this:

filegroup(
    name = "extract_archive",
    srcs = [":some_cc_library"],
    output_group = "archive",
)

Many tools will accept the static archive instead of an object file. If the tool you're using does, then that's easy. If not, things get a bit more complicated.

Extracting the object file from the static archive is a bit trickier. You could use a genrule with the $(AR) Make variable, but that won't work with some C++ toolchains that require additional flags to configure architectures etc.

The better (but more complicated) answer is to follow the guidance in integrating with C++ rules. You can get the ar from the toolchain and the flags to use it in a custom rule, and then create an action to extract it. You could also access the OutputGroupInfo from the cc_library in the rule directly instead of using filegroup if you've already got a custom rule.

Brian Silverman
  • 3,085
  • 11
  • 13
0

Thanks all for your suggestions.

Now I think I can solve this problem in two steps(Seems Bazel does not allow to combine two rules into one): Step1, add a -E option like a normal cc_libary, we can call it a pp_library. It is easy.

Step2, in a new rules, its input is the target of pp_library, then in this rule find out the obj files(can be found via : action.outputs.to_list()) and copy them to the a new place via ctx.actions.run_shell() run_shell.

I take Bazel: copy multiple files to binary directory as a reference.