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 7 no longer compiles with mingw and instead uses visual studio #20492

Open
NightShadeI opened this issue Dec 11, 2023 · 7 comments
Open
Assignees
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: bug

Comments

@NightShadeI
Copy link

Description of the bug:

in v6.4.0 I had bazel configured so that it would use mingw-gcc , my .bazelrc was as follows:

build:windows --compiler=mingw-gcc
build:windows --enable_runfiles
build:windows --cxxopt=-std=c++2a
build:windows --cxxopt=-Wall
build:windows --cxxopt=-Wno-restrict
build:windows --cxxopt=-O3

build:macos --cxxopt=-std=c++2a
build:macos --cxxopt=-Wall
build:macos --cxxopt=-O3

build --enable_platform_specific_config

test --test_output=all

Everything compiled and worked correctly to this point. Now upgrading to bazel 7.0.0 , I get this error:

WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your 
external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to https://github.com/bazelbuild/bazel/issues/18958.
INFO: Analyzed target <mytarget> (93 packages loaded, 2584 targets configured).
ERROR: C:/users/<user>/documents/programming/c++/projects/<projectname>/<projectname>/assets/BUILD:3:12: Compiling <projectname>/assets/<filename>.cpp failed: (Exit 2): cl.exe failed: error executing CppCompile command (from target //<projectname>/assets:<targetname>) C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX64\x64\cl.exe @bazel-out/x64_windows-fastbuild/bin/<projectname>/assets/_objs/<targetdir>/<targetdir>_gen.obj.params
cl : Command line error D8021 : invalid numeric argument '/Wno-restrict'
ERROR: C:/users/<user>/documents/programming/c++/projects/<projectname>/<projectname>/assets/BUILD:12:12: Compiling <projectname>/assets/minimap-cursor_gen.cpp failed: (Exit 2): cl.exe failed: error executing CppCompile command (from target //<projectname>/assets:minimap-cursor) C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX64\x64\cl.exe ... (remaining 1 argument skipped)
cl : Command line error D8021 : invalid numeric argument '/Wno-restrict'
Target <mytarget> failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 15.841s, Critical Path: 1.23s
INFO: 9 processes: 6 internal, 3 local.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

As you can see its now trying to use visual studio despite specifying --compiler=mingw-gcc

This is on windows 10, and I have some of the following relevant ENV set:

BAZEL_SH=C:\msys64\usr\bin\bash.exe
BAZEL_VC=C:\Program Files\Microsoft Visual Studio\2022\Community\VC
CC=C:\msys64\mingw64\bin\gcc.exe

As well as mingw being available in the PATH. Again, this DOES work in v6.4.0, it stops working in v7.0.0

Thanks for any help

Which category does this issue belong to?

C++/Objective-C Rules

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

Attempt to use mingw with above flags / ENV vars in the new bazel 7.0.0

Which operating system are you running Bazel on?

Windows 10

What is the output of bazel info release?

release 7.0.0

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 master; git rev-parse HEAD ?

My repository is private, and don't intend to make it public (yet)

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

No response

Have you found anything relevant by searching the web?

No

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

No response

@NightShadeI
Copy link
Author

Is there anyone I should be assigning to this issue? Still have the same issue, and seems pretty urgent given it breaks building on windows OS. (cc @iancha1992 or other)

@iancha1992
Copy link
Member

cc: @buildbreaker2021

@comius comius self-assigned this Dec 27, 2023
@comius comius added P2 We'll consider working on this in future. (Assignee optional) P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged P2 We'll consider working on this in future. (Assignee optional) labels Dec 27, 2023
@russellsch
Copy link

I've also encountered this issue.
With msys2 mingw_gcc via --compiler=mingw-gcc, gcc is used in Bazel 6.4.0, but when swapping to Bazel 7.0.0 (via bazelisk) MSVC is used instead.
I am setting environment variables same as @NightShadeI

@fmeum
Copy link
Collaborator

fmeum commented Jan 7, 2024

This is probably due to the flip of --incompatible_enable_cc_toolchain_resolution. Could you try disabling that flag?

You could also try to adapt to use the second approach described in https://bazel.build/configure/windows#clang to

name = "mingw",
.

@russellsch
Copy link

@fmeum That worked, I'll give the second approach linked a try as well. Much appreciated, thank you!

@tanzislam
Copy link

Bazel 8 has removed the --incompatible_enable_cc_toolchain_resolution=false setting, so now we need to migrate. My use case is compiling GoogleTest with MSYS2's MINGW64 GCC, ideally using the command line without editing the BAZEL.build file.

With Bazel 8 I tried:

$ bazel --batch build '--toolchain_resolution_debug=.*' --compiler=mingw-gcc --cxxopt=-std=c++14 --nobuild gtest gtest_main

and got:

INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Compatible execution platform @@platforms//host:host
      ToolchainResolution:   All execution platforms have been assigned a @@bazel_tools//tools/cpp:toolchain_type toolchain, stopping
      ToolchainResolution: Recap of selected @@bazel_tools//tools/cpp:toolchain_type toolchains for target platform @@platforms//host:host:
      ToolchainResolution:   Selected @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows to run on execution platform @@platforms//host:host
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,

Looking inside %USERPROFILE%\_bazel_%USERNAME%\2m76tw4a\external I see a rules_cc++cc_configure_extension+local_config_cc directory inside which the BUILD file seems to be expanded from this template file.

I tried specifying the highlighted toolchain with --extra_toolchains. Specifying the cc_toolchain() name (i.e. cc-compiler-x64_windows_mingw) or the cc_toolchain_config() name (i.e. msys_x64_mingw) quickly errored out like this:

ERROR: <path_to_googletest>/BUILD.bazel:170:11: While resolving toolchains for target //:gtest_main (c392b1e): invalid registered toolchain '@@rules_cc++cc_configure_extension+local_config_cc//:msys_x64_mingw': target does not provide the DeclaredToolchainInfo provider

... but when I tried with the toolchain() name (i.e. cc-toolchain-x64_windows_mingw) like this:

$ bazel --batch build '--toolchain_resolution_debug=.*' --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows_mingw --compiler=mingw-gcc --cxxopt=-std=c++14 --nobuild gtest gtest_main

... I got a different sequence of events:

INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: mingw
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Compatible execution platform @@platforms//host:host
      ToolchainResolution:   All execution platforms have been assigned a @@bazel_tools//tools/cpp:toolchain_type toolchain, stopping
      ToolchainResolution: Recap of selected @@bazel_tools//tools/cpp:toolchain_type toolchains for target platform @@platforms//host:host:
      ToolchainResolution:   Selected @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows to run on execution platform @@platforms//host:host
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,

Seems like the "@rules_cc//cc/private/toolchain:mingw" entry (line 224) in the exec_compatible_with array causes that incompatibility.

I even tried adding --experimental_add_exec_constraints_to_targets=gtest,gtest_main=@@rules_cc+//cc/private/toolchain:mingw:

$ bazel --batch build '--toolchain_resolution_debug=.*' --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows_mingw --experimental_add_exec_constraints_to_targets=gtest,gtest_main=@@rules_cc+//cc/private/toolchain:mingw --cxxopt=-std=c++14 --nobuild gtest gtest_main

... but got this:

INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host,
INFO: ToolchainResolution: Removed execution platform @@platforms//host:host from available execution platforms, it is missing constraint @@rules_cc+//cc/private/toolchain:mingw
INFO: ToolchainResolution: Target platform null: Selected execution platform null,
INFO: ToolchainResolution: Removed execution platform @@platforms//host:host from available execution platforms, it is missing constraint @@rules_cc+//cc/private/toolchain:mingw
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw is compatible with target platform, searching for execution platforms:
      ToolchainResolution:   All execution platforms have been assigned a @@bazel_tools//tools/cpp:toolchain_type toolchain, stopping
      ToolchainResolution: No @@bazel_tools//tools/cpp:toolchain_type toolchain found for target platform @@platforms//host:host.
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: mingw
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Compatible execution platform @@platforms//host:host
      ToolchainResolution:   All execution platforms have been assigned a @@bazel_tools//tools/cpp:toolchain_type toolchain, stopping
      ToolchainResolution: Recap of selected @@bazel_tools//tools/cpp:toolchain_type toolchains for target platform @@platforms//host:host:
      ToolchainResolution:   Selected @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows to run on execution platform @@platforms//host:host
INFO: ToolchainResolution: Removed execution platform @@platforms//host:host from available execution platforms, it is missing constraint @@rules_cc+//cc/private/toolchain:mingw
INFO: ToolchainResolution: Target platform null: Selected execution platform null,
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform @@platforms//host:host, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows
ERROR: <path to googletest>/BUILD.bazel:86:11: While resolving toolchains for target //:gtest (31ca0f3): Unable to find an execution platform for target platform @@platforms//host:host from available execution platforms []
ERROR: Analysis of target '//:gtest' failed; build aborted

No idea how to proceed from here. 😕

@tanzislam
Copy link

Eventually I gave up and edited three files (BUILD.bazel, MODULE.bazel and WORKSPACE) in the GoogleTest repo:

diff --git a/BUILD.bazel b/BUILD.bazel
index 0306468e..ca9b77d1 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -234,3 +234,12 @@ cc_test(
     srcs = ["googletest/samples/sample10_unittest.cc"],
     deps = [":gtest"],
 )
+
+platform(
+    name = "my_platform",
+    constraint_values = [
+        "@platforms//cpu:x86_64",
+        "@platforms//os:windows",
+        "@rules_cc//cc/private/toolchain:mingw",
+    ],
+)
diff --git a/MODULE.bazel b/MODULE.bazel
index e213d43e..39782319 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -47,6 +47,10 @@ bazel_dep(
     name = "platforms",
     version = "0.0.10",
 )
+bazel_dep(
+    name = "rules_cc",
+    version = "0.0.16",
+)
 bazel_dep(
     name = "re2",
     version = "2024-07-02",
diff --git a/WORKSPACE b/WORKSPACE
index 63f76813..489bbb3b 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -60,3 +60,10 @@ http_archive(
     ],
     sha256 = "218efe8ee736d26a3572663b374a253c012b716d8af0c07e842e82f238a0a7ee",
 )
+
+http_archive(
+    name = "rules_cc",
+    urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.16/rules_cc-0.0.16.tar.gz"],
+    sha256 = "bbf1ae2f83305b7053b11e4467d317a7ba3517a12cef608543c1b1c5bf48a4df",
+    strip_prefix = "rules_cc-0.0.16",
+)

and then it worked:

$ bazel --batch build '--toolchain_resolution_debug=.*' --extra_toolchains=@@rules_cc++cc_configure_extension+local_config_cc//:cc-toolchain-x64_windows_mingw --extra_execution_platforms=//:my_platform --cxxopt=-std=c++14 gtest gtest_main
...
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform //:my_platform,
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@platforms//host:host
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Compatible execution platform //:my_platform
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: mingw
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Skipping execution platform //:my_platform; it has already selected a toolchain
      ToolchainResolution:     Compatible execution platform @@platforms//host:host
      ToolchainResolution:   All execution platforms have been assigned a @@bazel_tools//tools/cpp:toolchain_type toolchain, stopping
      ToolchainResolution: Recap of selected @@bazel_tools//tools/cpp:toolchain_type toolchains for target platform @@platforms//host:host:
      ToolchainResolution:   Selected @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw to run on execution platform //:my_platform
      ToolchainResolution:   Selected @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows to run on execution platform @@platforms//host:host
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform //:my_platform, type @@bazel_tools//tools/cpp:toolchain_type -> toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-x64_windows_mingw
INFO: ToolchainResolution: Target platform @@platforms//host:host: Selected execution platform //:my_platform,
INFO: ToolchainResolution: Target platform //:my_platform: Selected execution platform //:my_platform,
INFO: Analyzed 2 targets (68 packages loaded, 524 targets configured).
INFO: Found 2 targets...
INFO: Elapsed time: 38.050s, Critical Path: 21.88s
INFO: 20 processes: 3 internal, 17 local.
INFO: Build completed successfully, 20 total actions

Seems like there is no way to avoid editing BUILD/BUILD.bazel (and enlisting rules_cc as a dependency) to define my platform just to add the constraint.

tanzislam added a commit to tanzislam/cryptopals that referenced this issue Dec 28, 2024
See bazelbuild/bazel#20492. "Toolchain resolution" can no longer be disabled in Bazel 8, so we are forced to edit 3 files in the GoogleTest repo to build it with MSYS2's mingw64 GCC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-CPP Issues for C++ rules type: bug
Projects
None yet
Development

No branches or pull requests

8 participants