I would like to propagate a custom build setting from a set of bazel targets to some of their transitive dependencies. In other words, I would like a target to say "use X variant of Y library", and that setting gets propagated down to the target(s) in Y library where that setting is consumed.
My naive implementation just adds an outgoing transition at the top-level target where I want to set the setting. However, I would like to contain the non-default setting value to the targets between the ones that set it and the ones that consume it. This should avoid the setting "polluting" all downstream deps and forcing re-build of other deps which don't consume the library. This problem is described here.
My thought is I could wrap cc_library
with a macro. The macro would know how to inject the build setting and will only forward the setting to its deps which are declared as the same macro:
custom_cc_library(
name="top_level",
custom_setting="non_default_value",
deps=[
":middle_level", # custom_setting is set to "non_default_value" for this dep
":lib_which_doesnt_consume_setting", # custom_setting is set to the default value for this dep
],
...
)
custom_cc_library(
name="middle_level",
deps=[
":setting_consumer", # setting is set to "non_default_value" for this dep
],
...
)
cc_library(
name="lib_which_doesnt_consume_setting",
...
)
setting_consumer(
name="setting_consumer",
...
)
However, outgoing transitions are configured per rule attribute, so I can't see a way to specify that some deps of custom_cc_library
should receive a different version of the build setting than others (given that I can't modify the implementation of cc_library
). It seems the best I can do is add a separate custom_deps
attribute to custom_cc_library
which I pass the new setting to. This is doable, just less ergonomic.
Is there any way to apply different values of an outgoing transition per dep? And/or am I suffering from the X/Y problem and there's a better way to select library versions from targets in bazel?