bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Windows Platform Toolchain Resolution Broken

Open jxy-s opened this issue 1 month ago • 7 comments

Description of the bug:

There appears to be a bug in toolchain resolution for bazel on Windows. There are two observed problems:

  1. --platforms for a target other than the host fails to resolve the toolchain.
  2. On an ARM64 Windows host the output binary is x64 when no platform constraints apply.

The consequence of this is that the toolchain resolution is incapable of producing a binary for anything other than x64. Counterintuitively, the ARM64 host produces an x64 binary

I've tested this on my Windows x64 and ARM64 machines. Both have all the MSVC installations necessary to cross-compile to another architecture. I believe the platform constraints should be applying here and the toolchain resolution should be locating the appropriate toolchain to use to compile for a target architecture other than the host. At very least it seems like the toolchain resolution on an ARM64 host should compile natively for that platform rather than produce a x64 binary.

Which category does this issue belong to?

C++ Rules

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

I have put together a minimal example project here: https://github.com/jxy-s/bazel_win_xcomp

Which operating system are you running Bazel on?

Windows

What is the output of bazel info release?

release 7.1.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

https://github.com/jxy-s/bazel_win_xcomp.git
dc6fc3cc910c94a799157a2d8516595cc01bcd7d

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

I believe so, I haven't been able to locate the commit yet.

Have you found anything relevant by searching the web?

I have found some suggestions and discussions on this topic. But nothing with concrete explanations as to why this is happening. This seems like a bug in the toolchain resolution. I do recognize that I could have a configuration problem in my example project. If that is the case I would appreciate guidance on what needs corrected.

https://github.com/bazelbuild/bazel/issues/10963 https://github.com/bazelbuild/bazel/pull/11938 https://github.com/bazelbuild/bazel/pull/11973

Any other information, logs, or outputs that you want to share?

➜ Get-ChildItem -Include cl.exe -Recurse "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools" | ForEach-Object{echo $_.FullName}
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\arm\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\arm64\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x64\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx64\x86\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx86\arm\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx86\arm64\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx86\x64\cl.exe
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\Hostx86\x86\cl.exe

➜ Get-ChildItem -Include vcvar*.bat -Recurse "C:\Program Files\Microsoft Visual Studio\2022\Community\VC" | ForEach-Object{echo $_.FullName}
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsamd64_arm.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsamd64_arm64.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsamd64_x86.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsx86_amd64.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsx86_arm.bat
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsx86_arm64.bat
➜ bazel query @local_config_cc//:toolchain --output=build
Starting local Bazel server and connecting to it...
# C:/users/jxy/_bazel_jxy/hvmoy72a/external/local_config_cc/BUILD:66:19
cc_toolchain_suite(
  name = "toolchain",
  tags = ["__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"],
  generator_name = "toolchain",
  generator_function = "cc_toolchain_suite",
  generator_location = "C:/users/jxy/_bazel_jxy/hvmoy72a/external/local_config_cc/BUILD:66:19",
  toolchains = {"armeabi-v7a|compiler": "@local_config_cc//:cc-compiler-armeabi-v7a", "x64_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_windows", "x64_x86_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_x86_windows", "x64_arm_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_arm_windows", "x64_arm64_windows|msvc-cl": "@local_config_cc//:cc-compiler-arm64_windows", "arm64_windows|msvc-cl": "@local_config_cc//:cc-compiler-arm64_windows", "x64_windows|msys-gcc": "@local_config_cc//:cc-compiler-x64_windows_msys", "x64_windows|mingw-gcc": "@local_config_cc//:cc-compiler-x64_windows_mingw", "x64_windows|clang-cl": "@local_config_cc//:cc-compiler-x64_windows-clang-cl", "x64_windows_msys": "@local_config_cc//:cc-compiler-x64_windows_msys", "x64_windows": "@local_config_cc//:cc-compiler-x64_windows", "x64_x86_windows": "@local_config_cc//:cc-compiler-x64_x86_windows", "x64_arm_windows": "@local_config_cc//:cc-compiler-x64_arm_windows", "x64_arm64_windows": "@local_config_cc//:cc-compiler-arm64_windows", "arm64_windows": "@local_config_cc//:cc-compiler-arm64_windows", "x64_arm64_windows|clang-cl": "@local_config_cc//:cc-compiler-arm64_windows-clang-cl", "arm64_windows|clang-cl": "@local_config_cc//:cc-compiler-arm64_windows-clang-cl", "armeabi-v7a": "@local_config_cc//:cc-compiler-armeabi-v7a"},
)
# Rule toolchain instantiated at (most recent call last):
#   C:/users/jxy/_bazel_jxy/hvmoy72a/external/local_config_cc/BUILD:66:19  in <toplevel>
#   C:/users/jxy/_bazel_jxy/hvmoy72a/external/rules_cc~/cc/defs.bzl:155:30 in cc_toolchain_suite
{
    "armeabi-v7a|compiler": "@local_config_cc//:cc-compiler-armeabi-v7a",
    "x64_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_windows",
    "x64_x86_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_x86_windows",
    "x64_arm_windows|msvc-cl": "@local_config_cc//:cc-compiler-x64_arm_windows",
    "x64_arm64_windows|msvc-cl": "@local_config_cc//:cc-compiler-arm64_windows",
    "arm64_windows|msvc-cl": "@local_config_cc//:cc-compiler-arm64_windows",
    "x64_windows|msys-gcc": "@local_config_cc//:cc-compiler-x64_windows_msys",
    "x64_windows|mingw-gcc": "@local_config_cc//:cc-compiler-x64_windows_mingw",
    "x64_windows|clang-cl": "@local_config_cc//:cc-compiler-x64_windows-clang-cl",
    "x64_windows_msys": "@local_config_cc//:cc-compiler-x64_windows_msys",
    "x64_windows": "@local_config_cc//:cc-compiler-x64_windows",
    "x64_x86_windows": "@local_config_cc//:cc-compiler-x64_x86_windows",
    "x64_arm_windows": "@local_config_cc//:cc-compiler-x64_arm_windows",
    "x64_arm64_windows": "@local_config_cc//:cc-compiler-arm64_windows",
    "arm64_windows": "@local_config_cc//:cc-compiler-arm64_windows",
    "x64_arm64_windows|clang-cl": "@local_config_cc//:cc-compiler-arm64_windows-clang-cl",
    "arm64_windows|clang-cl": "@local_config_cc//:cc-compiler-arm64_windows-clang-cl",
    "armeabi-v7a": "@local_config_cc//:cc-compiler-armeabi-v7a"
}
➜ bazel build //... --platforms=:windows_arm64 --toolchain_resolution_debug=.*
WARNING: Build option --toolchain_resolution_debug has changed, discarding analysis cache (this can be expensive, see https://bazel.build/advanced/performance/iteration-speed).
INFO: ToolchainResolution: Target platform //:windows_arm64: Selected execution platform @@local_config_platform//:host,
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform //:windows_arm64
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-x64_windows; mismatching values: x86_64
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-x64_windows; mismatching values: x86_64
      ToolchainResolution:   Rejected toolchain @@local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@local_config_cc//:cc-compiler-x64_windows; mismatching values: x86_64
      ToolchainResolution: No @@bazel_tools//tools/cpp:toolchain_type toolchain found for target platform //:windows_arm64.
INFO: ToolchainResolution: Target platform //:windows_arm64: Selected execution platform @@local_config_platform//:host,
ERROR: C:/users/jxy/_bazel_jxy/veelwacq/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @@bazel_tools//tools/cpp:current_cc_toolchain:
Traceback (most recent call last):
        File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
        File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 219, column 17, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@//:windows_arm64, Exec platform: @@local_config_platform//:host
ERROR: C:/users/jxy/_bazel_jxy/veelwacq/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: Analysis of target '//source:example' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.226s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

jxy-s avatar Apr 27 '24 22:04 jxy-s