How to get path to cc_library output inside a custom rule?

Skip to first unread message

Carlos Galvez

Jun 21, 2022, 10:02:07 AMJun 21
to bazel-discuss

I have a cc_library target to build some static library (libfoo.a). I need to run some post-processing on such library, so I have created a custom rule that receives a number of libraries ("srcs") as input via "attr.label_list".

Now, inside my custom rule implementation function, I need the path to libfoo.a. How can I get it? If I try "ctx.attr.srcs[0].path" Bazel complains that "cc_library has no attribute 'path'"

Printing it I see it contains the following attributes: CcInfo, InstrumentedFilesInfo, OutputGroupInfo. The last 2 are empty, and the CcInfo has some undocumented fields that I can't find any info about.


Alex Humesky

Jun 21, 2022, 7:48:52 PMJun 21
to Carlos Galvez, bazel-discuss
You might try something like this:

def _my_rule_impl(ctx):
  for src in ctx.attr.srcs:
    lc = src[CcInfo].linking_context
    for linker_input in lc.linker_inputs.to_list():
      print("from %s" % linker_input.owner)
      for library in linker_input.libraries:

my_rule = rule(
  implementation = _my_rule_impl,
  attrs = {
    "srcs": attr.label_list(),

Note also that you might use these APIs to create a CcInfo to provide from your rule:

Those also give some idea of how CcInfo is structured.

You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Carlos Galvez

Jun 23, 2022, 5:29:08 AMJun 23
to bazel-discuss
Awesome, that's exactly what I needed, thanks!
Reply all
Reply to author
0 new messages