No copts in CcInfo?

256 views
Skip to first unread message

Carlos Galvez

unread,
Mar 8, 2021, 2:43:42 PM3/8/21
to bazel-discuss
Hi,

I'm implementing a rule to process C++ targets (cc_library, cc_binary). There I'm trying to extract the "copts" information that has been passed to the cc_* target. However I notice that the CcInfo.CompilationContext doesn't contain anything related to copts? It has everything else (defines, includes, etc) but not copts. 

Is that intended? How can I extract the copts from a cc_* target?

Thanks!

Konstantin

unread,
Mar 15, 2021, 6:36:11 PM3/15/21
to bazel-discuss
CcInfo only carries the information which supposed to propagate from the dependency cc target to dependent cc target and copts is not that kind of information. Where are you trying to get copts? In the dependent cc target? I think your best bet would be to use aspect to collect arbitrary data about your dependencies.

Konstantin

Message has been deleted
Message has been deleted

Carlos Galvez

unread,
Mar 16, 2021, 9:07:11 AM3/16/21
to bazel-discuss

Hi!

I am indeed implementing an aspect to run a linter tool (clang-tidy) on the .cpp files that my cc target is made of. The linter is based on this aspect implementation:

Inside that aspect implementation, I need to obtain the copts that were passed to that particular target. However "target" only contains CcInfo, which doesn't contain copts. So, how can I obtain the copts that were passed to that particular cc_target?

Note that I don't care about dependencies here. I want to obtain information about a particular target, in isolation from dependencies/dependees. In other words, if I create the target like:

cc_library(
   name = "foo",
   copts =["-DMY_DEFINE"],
)

I want to be able to get that copt inside the aspect. 

Let me know if anything is unclear. Thanks!

/Carlos

Konstantin

unread,
Mar 16, 2021, 11:27:14 AM3/16/21
to bazel-discuss
Easy! Aspect implementation gets two parameters - target and ctx. What you need is ctx.rule.attr.copts!

Konstantin

Brian Silverman

unread,
Mar 16, 2021, 2:13:16 PM3/16/21
to Konstantin, bazel-discuss
For a bit more clarification: the CcInfo of that target is for its dependencies. Things like includes show up there because dependent targets use those. The copts attr doesn't, because those don't propagate.

Konstantin's answer about using ctx.rule.attr.copts is how I would do it.

--
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/323b18d9-ee74-49dd-aa51-05335529a332n%40googlegroups.com.

Carlos Galvez

unread,
Mar 17, 2021, 10:28:33 AM3/17/21
to bazel-discuss
Amazing, that's exactly what I needed, thanks a lot! I suddently understand aspects much better now :) 
Reply all
Reply to author
Forward
0 new messages