将副本传播到Bazel中的所有依赖项 [英] Propagate copts to all dependencies in Bazel

查看:77
本文介绍了将副本传播到Bazel中的所有依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ Bazel项目,其中每个目标都应根据其父目标接收不同的编译器标志(通过 copts ).例如,考虑依赖关系图:

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:

我希望在 target 1 target 2 中定义的不同的 copts 向下传播到每个依赖项分别定位.换句话说,我希望每个 dependency 目标都接收其父目标中定义的 copts .这意味着通过编译每个 dependency 目标(取决于哪个父目标取决于它)将产生两种不同的编译二进制文件.

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.

Bazel可以做到吗?如果没有,是否有其他方法可以达到相同的结果?

Is this possible with Bazel? If not, are there alternative ways of achieving the same result?

推荐答案

最简单的方法是显式复制所有依赖关系目标.您可以使用.然后,当您要构建或依赖其中之一时,只需选择要使用的版本,因为它们的名称会有所不同.

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.

更复杂的方法为您提供了不同的方法来选择要使用的 Dependency A 版本.例如,如果 dependency 目标是二进制文件,那么在更复杂的方法中我看不到任何优势.

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.

如果它们是整体依赖关系图中间的库,则用户定义的构建设置可能更有意义.这将自动复制所有中间目标,而您无需修改​​所有其他规则定义以使用复制它们的宏.然后,您可以使用选择 Target 1 Target 2 之间,并使用匹配的 copts .

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.

更复杂的是,您可以使用方面或完全使用自定义C ++规则,但我看不到任何原因都可以做.

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.

这篇关于将副本传播到Bazel中的所有依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆