I have two Skylark extension rules: blah_library
and blah_binary
. All of a blah_library
's transitive dependencies are propagated by returning a provider(transitive_deps=...)
, and are handled appropriately by any ultimate dependent blah_binary
target.
I want each blah_library
to also create a filegroup
with all the transitive dependencies mentioned above, so that I can access them separately. E.g., I'd like to be able to pass them in as data dependencies to a cc_binary
. In other words:
# Somehow have this automatically create a target named `foo__trans_deps`?
blah_library(
name = "foo",
srcs = [...],
deps = [...],
)
cc_binary(
...,
data = [":foo__trans_deps"],
)
How should I do this? Any help would be appreciated!
I tried making a macro like so:
_real_blah_library = rule(...)
def blah_library(name, *args, **kwargs):
native.filegroup(
name = name + "__trans_deps",
srcs = ???,
)
_real_blah_library(name=name, *args, **kwargs)
But I'm not sure how to access the provider
provided by _real_blah_library
from within the macro, so I don't know how to populate the filegroup
's srcs
field...
blah_library
rule's implementationRight now I have something like:
_blah_provider = provider(fields=['transitive_deps'])
def _blah_library_impl(ctx):
...
trans_deps = []
for dep in ctx.attr.deps:
trans_deps += dep[_blah_provider].trans_deps
return _blah_provider(trans_deps=trans_deps)
blah_library = rule(impl=_blah_library_impl, ...)
I tried adding the following to _blah_library_impl
, but it didn't work because apparently native.filegroup
can't be called within a rule's implementation ("filegroup() cannot be called during the analysis phase
"):
def _blah_library_impl(ctx):
...
trans_deps = []
for dep in ctx.attr.deps:
trans_deps += dep[_blah_provider].trans_deps
native.filegroup(
name = ctx.attr.name + "__trans_deps",
srcs = trans_deps,
)
return _blah_provider(trans_deps=trans_deps)
You can't easily create a filegroup like that, but you can still achieve what you want.
If you want to use the rule in genrule.srcs
, filegroup.srcs
, cc_binary.data
, etc., then return a DefaultInfo
provider (along with _blah_provider
) and set the files
field to the transitive closure of files.
You can refine the solution if you want a different set of files when the rule is in a data
attribute vs. when in any other (e.g. srcs
): just also set the runfiles-related members in DefaultInfo
. (Frankly I don't know the difference between them, I'd just set all runfiles-fields to the same value.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With