如何正确使用Bazel Transition进行多元构建
我正在尝试定义一个Bazel规则,该规则将为2个不同的平台构建2个不同的CC_Binaries
,只有1个不同的平台,只有1个Bazel Build
Invocation。我为如何正确定义过渡并将其连接而苦苦挣扎。
我最终希望能够做类似的事情:
cc_binary(
name = "binary_platform_a"
...
)
cc_binary(
name = "binary_platform_b"
...
)
my_custom_multi_arch_rule(
name = "multiarch_build",
binary_a = ":binary_platform_a",
binary_b = ":binary_platform_b",
...
)
我从Bazel文件中推论出来:[https://bazel.build/rules/config#user-defined-transitions]我需要在<代码> defs.bzl :
def _impl(settings, attr):
_ignore = (settings, attr)
return {
"Platform A": {"//command_line_option:platform": "platform_a"},
"Platform B": {"//command_line_option:platform": "platform_b"},
}
multi_arch_transition = transition(
implementation = _impl,
inputs = [],
outputs = ["//command_line_option:platform"]
)
def _rule_impl(ctx):
# How to implement this?
my_custom_multi_arch_rule = rule(
implementation = _rule_impl,
attrs = {
"binary_a": attr.label(cfg = multi_arch_transition)
"binary_b": attr.label(cfg = multi_arch_transition)
...
})
最佳案例最终方案将能够发行:
bazel build //path/to/my:multiarch_build
它成功地为其各自平台构建了我的2个单独的二进制文件。
I'm trying to define a bazel rule that will build 2 different cc_binaries
for 2 different platforms with just 1 bazel build
invocation. I'm struggling with how to define the transition properly and attach it.
I would like ultimately to be able to do something like:
cc_binary(
name = "binary_platform_a"
...
)
cc_binary(
name = "binary_platform_b"
...
)
my_custom_multi_arch_rule(
name = "multiarch_build",
binary_a = ":binary_platform_a",
binary_b = ":binary_platform_b",
...
)
I have deduced from bazel documents: [https://bazel.build/rules/config#user-defined-transitions] that I need to do something like the following in a defs.bzl
:
def _impl(settings, attr):
_ignore = (settings, attr)
return {
"Platform A": {"//command_line_option:platform": "platform_a"},
"Platform B": {"//command_line_option:platform": "platform_b"},
}
multi_arch_transition = transition(
implementation = _impl,
inputs = [],
outputs = ["//command_line_option:platform"]
)
def _rule_impl(ctx):
# How to implement this?
my_custom_multi_arch_rule = rule(
implementation = _rule_impl,
attrs = {
"binary_a": attr.label(cfg = multi_arch_transition)
"binary_b": attr.label(cfg = multi_arch_transition)
...
})
The best-case final scenario would be able to issue:
bazel build //path/to/my:multiarch_build
and it successfully builds my 2 separate binaries for their respective platforms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个不同的实现,但包括所有零件,包括平台(“@Platforms // cpu:x86_64”)。
我发现的每个示例中不清楚的部分是
platforms =
必须指向platform()
,该使用@platforms //
来定义约束。正如我在这里证明的那样:(续)在建造焦油并解开包装后,证明它有效:
警告:这是我第一次进行过渡,没有告诉我我是否犯了菜鸟的错误,但肯定适用于我想要什么!
Here's a different implementation, but complete with all the parts, including the platform ("@platforms//cpu:x86_64").
The unclear part in every example I've found is that
platforms=
must point to aplatform()
which defines constraints using@platforms//
as I demonstrate here: (cont'd)After building the tar and unpacking it, proof that it worked:
CAVEAT: This is my first time working with transitions, there's no telling whether I'm making noob mistakes, but it definitely works for what I want!
使用
ctx.split_attr。
https://bazel.build/rules/config#accessing-attributes-with-过渡
Use
ctx.split_attr.<attr name>[<transition key>]
to get the configuredTarget
object representing a particular arch configuration of a binary.https://bazel.build/rules/config#accessing-attributes-with-transitions
有了一些错别字和其他修复程序,我可以同时使用您和 jin's 示例。
With a few typos and other fixes, I got this to work using both you and jin's examples.