Skip to content

Bazel Windows Cross Compile Broken #7

Closed
@jxy-s

Description

@jxy-s

Brief description of your issue

Compiling non-x64 for Windows through Bazel is broken. This is necessary to do testing for other architectures on Windows. This probably changed when we removed the ability to compile with the EWDK. The EWDK toolchain previously supported cross compilation. The cross compilation platform defintions here are broken somehow with the "normal" Bazel tool-chain resolution:

radiant-cpp/BUILD.bazel

Lines 45 to 79 in e32b683

platform(
name = "windows_x86",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_32",
],
visibility = [":__subpackages__"],
)
platform(
name = "windows_x64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
visibility = [":__subpackages__"],
)
platform(
name = "windows_arm",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:arm",
],
visibility = [":__subpackages__"],
)
platform(
name = "windows_arm64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:arm64",
],
visibility = [":__subpackages__"],
)

Steps to reproduce (optional)

On an x64 or ARM64 Windows host:

➜ rad build --win_arm64
ERROR: C:/users/jxy/_bazel_jxy/ytddry7e/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/ytddry7e/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: C:/users/jxy/source/repos/archonitelabs/radiant-cpp/test/BUILD.bazel:20:8: errors encountered resolving select() keys for //test:all_test14
ERROR: Analysis of target '//test:all_test14' failed; build aborted
INFO: Elapsed time: 0.479s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
2024-04-26 11:02:03 ERROR    build failed!

Interestingly an ARM64 host resolves the default host tool-chain to x64 and outputs x64 binaries instead of ARM64. This seems like a bug in the MSVC Bazel tool-chain?

On my x64 host:

➜ bazel build --toolchain_resolution_debug=.* --platforms=:windows_arm64 //...
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/ytddry7e/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/ytddry7e/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: C:/users/jxy/source/repos/archonitelabs/radiant-cpp/test/BUILD.bazel:28:8: errors encountered resolving select() keys for //test:all_test17
ERROR: Analysis of target '//test:all_test17' failed; build aborted
INFO: Elapsed time: 0.703s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
➜ 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

Seems like a bug in Bazel tool-chain resolution for a standard MSVC (Visual Studio) install. A workaround might be manually activating the build environment before triggering Bazel tool-chain resolution:

➜ 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

Expected behavior (optional)

Specifying a platform to build should cross-compile for that architecture for Windows. Not specifying an architecture should build using the native host tool-chain.

Actual behavior (optional)

Compilation fails if you try to cross compile. On an ARM64 host the located tool-chain is x64 and chooses the x64 target, so you end up with a x64 binary instead of the intended native ARM64 one.

Environment (optional)

Windows x64 and ARM64
Bazel MSVC

Metadata

Metadata

Assignees

No one assigned

    Labels

    third partyRelated to third party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions