Repository: godotengine/godot (language: cpp)

Issue:
[Windows] Scons isn't detecting MinGW and fails when building
### Tested versions

- Reproducible in master (4.5.dev) - commit [6b5b84c0c50135e88691e035d3e00c68a2ae5aa4]
- **Not** reproducible in 4.4.stable

### System information

Godot v4.4.stable - Windows 11 (build 22631) - Multi-window, 1 monitor - OpenGL 3 (Compatibility) - NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA; 32.0.15.6636) - AMD Ryzen 7 7735HS with Radeon Graphics (16 threads)

### Issue description

Scons fails to detect MinGW (I'm using [LLVM-MinGW](https://github.com/mstorsjo/llvm-mingw/releases/)) which is included in my PATH, then complains it can't find MSVC and errors out. Scons works perfectly fine for me in 4.4 stable.

This is what I get when trying to build on master:

```
> scons
scons: Reading SConscript files ...
Automatically detected platform: windows

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc', 'mslink']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc', 'mslink', 'mslib']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>
Auto-detected 16 CPU cores available for build parallelism. Using 15 cores by default. You can override it with the `-j` or `num_jobs` arguments.
Building for platform "windows", architecture "x86_64", target "template_release".
KeyError: 'VSWHERE':
  File "D:\Godot Dev\godot\SConstruct", line 623:
    cc_version = methods.get_compiler_version(env)
  File "D:\Godot Dev\godot\methods.py", line 698:
    env["VSWHERE"],
  File "C:\Users\ahmou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\SCons\Environment.py", line 603:
    return self._dict[key]
```

### Steps to reproduce

Try building on Windows with MinGW, without MSVC installed

### Minimal reproduction project (MRP)

N/A


Discussion / hints:
What commit specifically are you building from to get an idea of where this is occuring
Updated the post to include the commit SHA, it's `6b5b84c0c50135e88691e035d3e00c68a2ae5aa4`
To build with MinGW on Windows, you need to specific `mingw=yes` explicitly, otherwise it defaults to MSVC.

I'm surprised it would work differently in 4.4.stable.

CC @Repiteo in case there were recent changes.
> To build with MinGW on Windows, you need to specific mingw=yes explicitly, otherwise it defaults to MSVC.

I never had to specify it specifically before. For what it's worth I think falling back to mingw if you don't have MSVC should be the intended behavior.

~~Also, it turns out compiling with `mingw=yes` doesn't work either. Same error.~~ Nevermind, The command is `use_mingw=yes` and that works fine. My bad!

<details>
<summary> output </summary>
<pre><code>> scons mingw=yes
scons: Reading SConscript files ...
Automatically detected platform: windows

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc', 'mslink']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>

scons: warning: No versions of the MSVC compiler were found.
  Visual Studio C/C++ compilers may not be set correctly.
  Requested tool(s) are: ['msvc', 'mslink', 'mslib']
File "D:\Godot Dev\godot\SConstruct", line 441, in <module>
Auto-detected 16 CPU cores available for build parallelism. Using 15 cores by default. You can override it with the `-j` or `num_jobs` arguments.
Building for platform "windows", architecture "x86_64", target "editor".
KeyError: 'VSWHERE':
  File "D:\Godot Dev\godot\SConstruct", line 623:
    cc_version = methods.get_compiler_version(env)
  File "D:\Godot Dev\godot\methods.py", line 698:
    env["VSWHERE"],
  File "C:\Users\ahmou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\SCons\Environment.py", line 603:
    return self._dict[key]</code></pre>
</details>
I haven't tested, but I suspect it may be a regression from #103864.
I use `scons use_mingw=yes use_llvm=yes` with [LLVM-MinGW](https://github.com/mstorsjo/llvm-mingw/releases/), it works fine for me.
MinGW should absolutely be the fallback option if MSVC isn't available, but that'll be tricky to test with on a PC that already has that environment integrated. I'll run some tests on a virtual machine & see what I can find.

> Also, it turns out compiling with `mingw=yes` doesn't work either. Same error.

Oop, that's the wrong name. You'll want `use_mingw=yes` instead. I'll make `mingw` an alias in the fix PR.
> MinGW should absolutely be the fallback option if MSVC isn't available, but that'll be tricky to test with on a PC that already has that environment integrated. I'll run some tests on a virtual machine & see what I can find.

This is supposed to happen already (since 4.0 if not earlier), but it's not working for some reason.

Fix patch:
diff --git a/platform/windows/detect.py b/platform/windows/detect.py
index 31a8d07fea80..789909b22351 100644
--- a/platform/windows/detect.py
+++ b/platform/windows/detect.py
@@ -148,7 +148,9 @@ def detect_build_env_arch():
 
 
 def get_tools(env: "SConsEnvironment"):
-    if os.name != "nt" or env.get("use_mingw"):
+    from SCons.Tool.MSCommon import msvc_exists
+
+    if os.name != "nt" or env.get("use_mingw") or not msvc_exists():
         return ["mingw"]
     else:
         msvc_arch_aliases = {"x86_32": "x86", "arm32": "arm"}


Test patch: