问题
I have a C++ Bazel project where each target should receive different compiler flags (via copts
) depending on their parent target. For example, consider the dependency graph:
I would like the different copts
defined in target 1
and target 2
to propagate down to each of the dependency
targets independently. In other words, I want each dependency
target to receive the copts
defined in their parent target(s). This means that there will be two different compiled binaries produced by compiling each of the dependency
targets, depending on which parent target is depending on it.
Is this possible with Bazel? If not, are there alternative ways of achieving the same result?
回答1:
The simplest approach is to explicitly duplicate all of the dependency targets. You could avoid unnecessary duplication in your BUILD
files with a macro. Then when you want to build or depend on one of those, you just pick which version to use because they'd have different names.
More complicated approaches give you different ways to pick which version of Dependency A
to use. If the dependency
targets are binaries (for example), then I don't see any advantage in more complicated approaches.
If they're libraries in the middle of your overall dependency graph, then a user-defined build setting might make sense instead. That duplicates all the intermediate targets automatically, without you needing to modify all the other rule definitions to use a macro that duplicates them. You could then use select to between Target 1
and Target 2
, and use the matching copts
.
Getting even more complicated, you could use aspects or fully custom C++ rules, but I don't see any reason to do either of those.
来源:https://stackoverflow.com/questions/64186487/propagate-copts-to-all-dependencies-in-bazel