ToolchainResolution 为错误的平台选择了工具链

发布于 2025-01-12 19:50:22 字数 1910 浏览 1 评论 0原文

我定义了一个新的 Rust 工具链,如下所示(从 https://github.com/bazelbuild/rules_rust 分叉):

toolchain(
    name = "toolchain_for_armv7-linux-androideabi",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:windows"],
    target_compatible_with = ["@platforms//cpu:armv7", "@platforms//os:android"],
    toolchain = "@rust_windows_x86_64//:toolchain_for_armv7-linux-androideabi_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

还有一个如下定义的现有工具链:

toolchain(
    name = "toolchain_for_x86_64-apple-darwin",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    target_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    toolchain = "@rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

并为该平台构建目标:

bazel build --toolchain_resolution_debug=@rules_rust//rust:toolchain --experimental_platforms_api vrst-test/android --platforms=//platforms:android_armv7

这是 ToolchainResolution 调试信息的片段:

INFO: ToolchainResolution:   Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl
INFO: ToolchainResolution:     Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: Rejected toolchain @rust_darwin_x86_64//:toolchain_for_armv7-linux-androideabi_impl; mismatching values: armv7, android

出于某种原因,目标平台是 @local_config_platform//:host尽管我明确指定平台为 android_armv7,其定义如下:

platform(
    name = "android_armv7",
    constraint_values = [
      "@platforms//cpu:armv7",
      "@platforms//os:android",
    ],
)```

I defined a new rust toolchain as follows (forked from https://github.com/bazelbuild/rules_rust):

toolchain(
    name = "toolchain_for_armv7-linux-androideabi",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:windows"],
    target_compatible_with = ["@platforms//cpu:armv7", "@platforms//os:android"],
    toolchain = "@rust_windows_x86_64//:toolchain_for_armv7-linux-androideabi_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

There is also an existing toolchain defined like this:

toolchain(
    name = "toolchain_for_x86_64-apple-darwin",
    exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    target_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:osx"],
    toolchain = "@rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl",
    toolchain_type = "@rules_rust//rust:toolchain",
)

And building a target for that platform:

bazel build --toolchain_resolution_debug=@rules_rust//rust:toolchain --experimental_platforms_api vrst-test/android --platforms=//platforms:android_armv7

Here is a snippet of ToolchainResolution debug info:

INFO: ToolchainResolution:   Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: execution @local_config_platform//:host: Selected toolchain @rust_darwin_x86_64//:toolchain_for_x86_64-apple-darwin_impl
INFO: ToolchainResolution:     Type @rules_rust//rust:toolchain: target platform @local_config_platform//:host: Rejected toolchain @rust_darwin_x86_64//:toolchain_for_armv7-linux-androideabi_impl; mismatching values: armv7, android

For some reason, target platform is @local_config_platform//:host despite me explicitly specifying the platform as android_armv7, which is defined as follows:

platform(
    name = "android_armv7",
    constraint_values = [
      "@platforms//cpu:armv7",
      "@platforms//os:android",
    ],
)```

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

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

发布评论

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

评论(2

忆离笙 2025-01-19 19:50:22

虽然这个问题是一年前提出的,但我还是尝试回答一下,希望对您有所帮助。

来自 bazel 文档:https://bazel.build/extending/toolchains#toolchain-resolution

target_known_with 或 exec_known_with 子句与
平台如果对于其列表中的每个constraint_value,平台还
具有该constraint_value(显式或默认)。

和:

对于每个可用的执行平台,您可以关联每个工具链
使用兼容的第一个可用工具链(如果有)键入
与此执行平台和目标平台。

因此,您的工具链与您的执行平台不兼容,即 @local_config_platform//:host

该平台 @local_config_platform//:host 由 bazel 自动生成,并且根据不同的系统而有所不同。因此,为了确保选择您的工具链,您需要:

  1. 确保您的工具链与执行平台兼容。您可以通过问题 bazel query @local_config_platform//:host --output=build 进行检查。
  2. 通过在命令行上指定来明确指定您的目标平台:--platforms=//label/to/your/toolchain:toolchain_name

Although this question was asked one years ago, I tried to answer and hope it helps.

From bazel documentation: https://bazel.build/extending/toolchains#toolchain-resolution

A target_compatible_with or exec_compatible_with clause matches a
platform if, for each constraint_value in its list, the platform also
has that constraint_value (either explicitly or as a default).

And:

For each available execution platform, you associate each toolchain
type with the first available toolchain, if any, that is compatible
with this execution platform and the target platform.

Hence, your toolchain is not compatible with your execution platform, which is @local_config_platform//:host

This platform, @local_config_platform//:host, is generated by bazel automatically and it varies for different systems. Hence, to ensure your toolchain is selected, you need to:

  1. ensure your toolchain is compatible with execution platform. You can check it by issue bazel query @local_config_platform//:host --output=build.
  2. specify your target platform explictly by specifying it on command line : --platforms=//label/to/your/toolchain:toolchain_name
围归者 2025-01-19 19:50:22

Bazel 解析由 --platforms 指定的目标平台和 主机平台,由--host_platform指定。如果您确实希望为 toolchain_for_armv7-linux-androideabi 构建主机工具,则需要设置 --host_platform

Bazel resolves toolchains both for the target platforms, specified by --platforms, and the host platform, specified by --host_platform. If you actually want host tools to be built for toolchain_for_armv7-linux-androideabi, you will want to set --host_platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文