[llvm] [WindowsDriver] Find Visual Studio through the Setup API on MinGW (PR #226794)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 27 07:47:57 PDT 2026


https://github.com/16bit-ykiko created https://github.com/llvm/llvm-project/pull/226794

The Setup API lookup of Visual Studio 2017 and later has been limited to MSVC builds since 545202b8f186. A MinGW-built clang, and every tool built on its libraries, finds Visual Studio 2017 and later only from a Developer Command Prompt (`VCToolsInstallDir` or `cl.exe` in `PATH`); otherwise it falls back to the registry and to the directories of Visual Studio 2010 to 2005.

mingw-w64 has the `comdef.h` and COM smart pointers the lookup uses. Its `__uuidof` is emulated and takes the interface IDs from `__CRT_UUID_DECL`, so declare them, and link the library with ole32 and oleaut32.

Tested: `MSVCPaths.cpp` builds without warnings for x86_64 and aarch64 MinGW. The lookup, built for MinGW, finds Visual Studio 18 on GitHub's windows-2025 runner and Visual Studio 2022 on windows-11-arm with no Visual Studio environment set up.

We ran into this in clice, a C++ language server built as a MinGW program (clice-io/clice#714), and carry this change in our toolchain build, [xclang](https://github.com/clice-io/xclang/tree/main/patches/0004-windows-driver-setup-api-mingw).

cc @mstorsjo


>From 5d0364a927c87808011a498ddb404c44d29b5220 Mon Sep 17 00:00:00 2001
From: ykiko <ykiko at clice.io>
Date: Sun, 27 Sep 2026 22:45:03 +0800
Subject: [PATCH] [WindowsDriver] Find Visual Studio through the Setup API on
 MinGW

The Setup API lookup of Visual Studio 2017 and later has been limited to
MSVC builds since 545202b8f186. A MinGW-built clang, and every tool built
on its libraries, finds Visual Studio 2017 and later only from a Developer
Command Prompt (VCToolsInstallDir or cl.exe in PATH); otherwise it falls
back to the registry and to the directories of Visual Studio 2010 to 2005.

mingw-w64 has the comdef.h and COM smart pointers the lookup uses. Its
__uuidof is emulated and takes the interface IDs from __CRT_UUID_DECL,
so declare them, and link the library with ole32 and oleaut32.
---
 llvm/lib/WindowsDriver/CMakeLists.txt |  5 +++++
 llvm/lib/WindowsDriver/MSVCPaths.cpp  | 21 +++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/WindowsDriver/CMakeLists.txt b/llvm/lib/WindowsDriver/CMakeLists.txt
index 4747df543762d..dbe00aed8edde 100644
--- a/llvm/lib/WindowsDriver/CMakeLists.txt
+++ b/llvm/lib/WindowsDriver/CMakeLists.txt
@@ -1,3 +1,8 @@
+if(MINGW)
+  # CoCreateInstance and the BSTR functions of the Setup API lookup.
+  set(imported_libs ole32 oleaut32)
+endif()
+
 add_llvm_component_library(LLVMWindowsDriver
   MSVCPaths.cpp
 
diff --git a/llvm/lib/WindowsDriver/MSVCPaths.cpp b/llvm/lib/WindowsDriver/MSVCPaths.cpp
index 3092ee2fbf5b9..d8158390e79b3 100644
--- a/llvm/lib/WindowsDriver/MSVCPaths.cpp
+++ b/llvm/lib/WindowsDriver/MSVCPaths.cpp
@@ -37,8 +37,7 @@
 #include <windows.h>
 #endif
 
-#ifdef _MSC_VER
-// Don't support SetupApi on MinGW.
+#if defined(_MSC_VER) || defined(__MINGW32__)
 #define USE_MSVC_SETUP_API
 
 // Make sure this comes before MSVCSetupApi.h
@@ -53,6 +52,24 @@
 #ifdef __clang__
 #pragma clang diagnostic pop
 #endif
+#ifdef __MINGW32__
+// mingw-w64 emulates __uuidof, which takes the IIDs from __CRT_UUID_DECL
+// rather than from the DECLSPEC_UUID in MSVCSetupApi.h.
+__CRT_UUID_DECL(ISetupInstance, 0xB41463C3, 0x8866, 0x43B5, 0xBC, 0x33, 0x2B,
+                0x06, 0x76, 0xF7, 0xF4, 0x2E)
+__CRT_UUID_DECL(ISetupInstance2, 0x89143C9A, 0x05AF, 0x49B0, 0xB7, 0x17, 0x72,
+                0xE2, 0x18, 0xA2, 0x18, 0x5C)
+__CRT_UUID_DECL(IEnumSetupInstances, 0x6380BCFF, 0x41D3, 0x4B2E, 0x8B, 0x2E,
+                0xBF, 0x8A, 0x68, 0x10, 0xC8, 0x48)
+__CRT_UUID_DECL(ISetupConfiguration, 0x42843719, 0xDB4C, 0x46C2, 0x8E, 0x7C,
+                0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B)
+__CRT_UUID_DECL(ISetupConfiguration2, 0x26AAB78C, 0x4A60, 0x49D6, 0xAF, 0x3B,
+                0x3C, 0x35, 0xBC, 0x93, 0x36, 0x5D)
+__CRT_UUID_DECL(ISetupHelper, 0x42B21B78, 0x6192, 0x463E, 0x87, 0xBF, 0xD5,
+                0x77, 0x83, 0x8F, 0x1D, 0x5C)
+__CRT_UUID_DECL(SetupConfiguration, 0x177F0C4A, 0x1CD3, 0x4DE7, 0xA3, 0x2C,
+                0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D)
+#endif
 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
 _COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));



More information about the llvm-commits mailing list