Bazel 的选择未被解释

发布于 2025-01-13 16:37:19 字数 468 浏览 2 评论 0原文

我试图在当前操作系统上设置一个条件(在 Bazel 4.2.2 中):

    is_darwin = select({
        "@bazel_tools//src/conditions:darwin": True,
        "//conditions:default": False,
    })

    if is_darwin:
        # a
    else:
        # b

但是,即使我在 Linux 上,我也会陷入 a

我尝试打印它,但似乎没有被评估:

DEBUG: WORKSPACE:65:6: select({"@bazel_tools//src/conditions:darwin": True, "//conditions:default": False})

我应该如何检查它是否正确?

I'm trying to set a conditional (in Bazel 4.2.2) over the current operating system:

    is_darwin = select({
        "@bazel_tools//src/conditions:darwin": True,
        "//conditions:default": False,
    })

    if is_darwin:
        # a
    else:
        # b

However I fall into a, even if I'm on Linux.

I've tried to print it, but it seems to not be evaluated:

DEBUG: WORKSPACE:65:6: select({"@bazel_tools//src/conditions:darwin": True, "//conditions:default": False})

How should I check it correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2025-01-20 16:37:19

Select 仅在规则实现函数中进行评估,而不是在您使用的宏中进行评估。您需要使用 attr.bool 创建属性,传递选择它,然后基于它执行你的逻辑。

您还可以使用 ctx.target_platform_has_constraint 直接执行此操作,不带 select:

def _impl(ctx):
  darwin_constraint = ctx.attr._darwin_constraint[platform_common.ConstraintValueInfo]
  if ctx.target_platform_has_constraint(darwin_constraint):
    print('on darwin')
  else:
    print('not darwin')


my_rule = rule(
  implementation = _impl,
  attrs = {
    ...
    '_darwin_constraint': attr.label(default = '@bazel_platforms//os:darwin'),
  },
)

查看差异的另一种方法是加载 BUILD 文件和运行宏​​发生在 加载阶段,但评估选择发生在加载和分析阶段之间。相关步骤是:

  1. 加载阶段生成目标及其之间的转换图。这些目标将规则与一组属性值联系起来,其中包括未评估的选择。
  2. 该图与命令行标志相结合,为每个目标生成一个或多个配置
  3. 每个目标都会使用适用于它的每个配置进行评估,并根据该配置进行选择解析。实际上,在评估选择之后,使用来自目标的属性值运行实现函数。这会产生配置的目标。

Select is only evaluated in rule implementation functions, not macros like you're using. You'll need to use attr.bool to create an attribute, pass the select to it, and then perform your logic based on that.

You can also use ctx.target_platform_has_constraint to do this directly, without select:

def _impl(ctx):
  darwin_constraint = ctx.attr._darwin_constraint[platform_common.ConstraintValueInfo]
  if ctx.target_platform_has_constraint(darwin_constraint):
    print('on darwin')
  else:
    print('not darwin')


my_rule = rule(
  implementation = _impl,
  attrs = {
    ...
    '_darwin_constraint': attr.label(default = '@bazel_platforms//os:darwin'),
  },
)

Another way to look at the difference is that loading BUILD files and running macros happens in the loading phase, but evaluating selects happens between the loading and analysis phases. The relevant steps are:

  1. The loading phase produces a graph of targets and transitions between them. These targets tie a rule to a set of attribute values, which includes unevaluated selects.
  2. This graph is combined with command-line flags to produce one or more configurations for each target.
  3. Each target is evaluated with each configuration that applies to it, with selects resolved as appropriate for that configuration. Effectively, the implementation functions are run with the attribute values from the target, after evaluating selects. This produces configured targets.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文