Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel Windows Cross Compile Broken #7

Open
jxy-s opened this issue Apr 26, 2024 · 3 comments
Open

Bazel Windows Cross Compile Broken #7

jxy-s opened this issue Apr 26, 2024 · 3 comments
Labels
third party Related to third party

Comments

@jxy-s
Copy link
Collaborator

jxy-s commented Apr 26, 2024

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
@jxy-s jxy-s added the bug Something isn't working label Apr 26, 2024
@jxy-s jxy-s changed the title This was partially implemented in Bazel Windows non-x64 compilation broken Apr 26, 2024
@jxy-s
Copy link
Collaborator Author

jxy-s commented Apr 28, 2024

This is at least a regression in Bazel. And at worst the platforms constraints never worked correctly - I have submitted a bug to Bazel: bazelbuild/bazel#22164

In the meantime, I have implemented bzlmod support in the EWDK toolchain repo. That will allow us to re-integrate with EWDK toolchain resolution as a workaround: 0xf005ba11/bazel_ewdk_cc#1

I will be putting together a PR on this repo to provide a workaround. Hopefully Bazel can address the problem or advise on another solution.

@jxy-s
Copy link
Collaborator Author

jxy-s commented Apr 28, 2024

I've tracked down another problem in bazelisk that explains the problem with trying to compile natively on ARM64:
bazelbuild/bazelisk#572
bazelbuild/bazel#22164 (comment)

@jxy-s jxy-s mentioned this issue Apr 28, 2024
@jxy-s jxy-s changed the title Bazel Windows non-x64 compilation broken Bazel Windows Cross Compile Broken Apr 28, 2024
@jxy-s jxy-s added third party Related to third party and removed bug Something isn't working labels Apr 28, 2024
@jxy-s
Copy link
Collaborator Author

jxy-s commented Apr 28, 2024

I've implemented the workaround for cross compiling here: #9

Keeping this open for tracking the issues brought to Bazel and for visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
third party Related to third party
Projects
None yet
Development

No branches or pull requests

1 participant