我知道通常当链接器在 C++ 中运行时,命令中链接参数的顺序很重要(如此处所述)。
Bazel 以什么顺序链接文件?它是否只是从构建文件的顶部到底部,有没有办法自定义它?
Bazel 使用“拓扑”顺序进行链接。来自文档;
“拓扑”(以前称为“链接”):从根到叶的拓扑排序。没有从左到右的保证。
此场景中的叶子是所有库依赖项,以树的形式表示。链接顺序与 BUILD 文件中列出的目标的顺序无关。
您可以通过检查源代码来确认这一点。源代码中的文档字符串比 depset 文档提供了更好的解释。
* <p>LINK_ORDER: a variation of left-to-right preorder that enforces topological sorting. In
* Starlark it is called "topological"; its older deprecated name is "link".
*
* <p>For example, for the nested set {A, C, {B, D}}, the iteration order is "A C B D"
* (parent-first).
*
* <p>This type of set would typically be used for artifacts where elements of nested sets go after
* the direct members of a set, for example when providing a list of libraries to the C++ compiler.
*
* <p>The custom ordering has the property that elements of nested sets always come before elements
* of descendant nested sets. Left-to-right order is preserved if possible, both for items and for
* references to nested sets.
*
* <p>The left-to-right pre-order-like ordering is implemented by running a right-to-left postorder
* traversal and then reversing the result.
Run Code Online (Sandbox Code Playgroud)
有一种方法可以定制它。尽管这并不简单,并且涉及编写您自己的 starlark 插件。
例如,如果您修改my_c_archive.bzl示例,为每次调用 depset 构造函数添加不同的排序参数,理论上您可以更改链接的顺序。例如 这个
* <p>LINK_ORDER: a variation of left-to-right preorder that enforces topological sorting. In
* Starlark it is called "topological"; its older deprecated name is "link".
*
* <p>For example, for the nested set {A, C, {B, D}}, the iteration order is "A C B D"
* (parent-first).
*
* <p>This type of set would typically be used for artifacts where elements of nested sets go after
* the direct members of a set, for example when providing a list of libraries to the C++ compiler.
*
* <p>The custom ordering has the property that elements of nested sets always come before elements
* of descendant nested sets. Left-to-right order is preserved if possible, both for items and for
* references to nested sets.
*
* <p>The left-to-right pre-order-like ordering is implemented by running a right-to-left postorder
* traversal and then reversing the result.
Run Code Online (Sandbox Code Playgroud)
会变成这样;
libraries = depset(direct = [
cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = output_file,
),
]),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |