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

374 views
Skip to first unread message

Carlos Galvez

unread,
Jun 21, 2022, 10:02:07 AM6/21/22
to bazel-discuss
Hi,

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.

Thanks!

Alex Humesky

unread,
Jun 21, 2022, 7:48:52 PM6/21/22
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:
        print(library.pic_static_library)

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 bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/6d51490f-a993-418a-acf3-8b0d7d29efefn%40googlegroups.com.

Carlos Galvez

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