1 //===-- MSVCPaths.cpp - MSVC path-parsing helpers -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/WindowsDriver/MSVCPaths.h"
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/ConvertUTF.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/VersionTuple.h"
25 #include "llvm/Support/VirtualFileSystem.h"
26 #include <string>
27 
28 #ifdef _WIN32
29 #define WIN32_LEAN_AND_MEAN
30 #define NOGDI
31 #ifndef NOMINMAX
32 #define NOMINMAX
33 #endif
34 #include <windows.h>
35 #endif
36 
37 #ifdef _MSC_VER
38 // Don't support SetupApi on MinGW.
39 #define USE_MSVC_SETUP_API
40 
41 // Make sure this comes before MSVCSetupApi.h
42 #include <comdef.h>
43 
44 #include "llvm/Support/COM.h"
45 #ifdef __clang__
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
48 #endif
49 #include "llvm/WindowsDriver/MSVCSetupApi.h"
50 #ifdef __clang__
51 #pragma clang diagnostic pop
52 #endif
53 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
54 _COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
55 _COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
56 _COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
57 _COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
58 _COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
59 #endif
60 
61 static std::string
62 getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
63                                   llvm::StringRef Directory) {
64   std::string Highest;
65   llvm::VersionTuple HighestTuple;
66 
67   std::error_code EC;
68   for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
69                                      DirEnd;
70        !EC && DirIt != DirEnd; DirIt.increment(EC)) {
71     auto Status = VFS.status(DirIt->path());
72     if (!Status || !Status->isDirectory())
73       continue;
74     llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
75     llvm::VersionTuple Tuple;
76     if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
77       continue;
78     if (Tuple > HighestTuple) {
79       HighestTuple = Tuple;
80       Highest = CandidateName.str();
81     }
82   }
83 
84   return Highest;
85 }
86 
87 static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
88                                            const std::string &SDKPath,
89                                            std::string &SDKVersion) {
90   llvm::SmallString<128> IncludePath(SDKPath);
91   llvm::sys::path::append(IncludePath, "Include");
92   SDKVersion = getHighestNumericTupleInDirectory(VFS, IncludePath);
93   return !SDKVersion.empty();
94 }
95 
96 static bool getWindowsSDKDirViaCommandLine(llvm::vfs::FileSystem &VFS,
97                                            llvm::StringRef WinSdkDir,
98                                            llvm::StringRef WinSdkVersion,
99                                            llvm::StringRef WinSysRoot,
100                                            std::string &Path, int &Major,
101                                            std::string &Version) {
102   if (!WinSdkDir.empty() || !WinSysRoot.empty()) {
103     // Don't validate the input; trust the value supplied by the user.
104     // The motivation is to prevent unnecessary file and registry access.
105     llvm::VersionTuple SDKVersion;
106     if (!WinSdkVersion.empty())
107       SDKVersion.tryParse(WinSdkVersion);
108 
109     if (!WinSysRoot.empty()) {
110       llvm::SmallString<128> SDKPath(WinSysRoot);
111       llvm::sys::path::append(SDKPath, "Windows Kits");
112       if (!SDKVersion.empty())
113         llvm::sys::path::append(SDKPath, llvm::Twine(SDKVersion.getMajor()));
114       else
115         llvm::sys::path::append(
116             SDKPath, getHighestNumericTupleInDirectory(VFS, SDKPath));
117       Path = std::string(SDKPath.str());
118     } else {
119       Path = WinSdkDir.str();
120     }
121 
122     if (!SDKVersion.empty()) {
123       Major = SDKVersion.getMajor();
124       Version = SDKVersion.getAsString();
125     } else if (getWindows10SDKVersionFromPath(VFS, Path, Version)) {
126       Major = 10;
127     }
128     return true;
129   }
130   return false;
131 }
132 
133 #ifdef _WIN32
134 static bool readFullStringValue(HKEY hkey, const char *valueName,
135                                 std::string &value) {
136   std::wstring WideValueName;
137   if (!llvm::ConvertUTF8toWide(valueName, WideValueName))
138     return false;
139 
140   DWORD result = 0;
141   DWORD valueSize = 0;
142   DWORD type = 0;
143   // First just query for the required size.
144   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
145                             &valueSize);
146   if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
147     return false;
148   std::vector<BYTE> buffer(valueSize);
149   result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
150                             &valueSize);
151   if (result == ERROR_SUCCESS) {
152     std::wstring WideValue(reinterpret_cast<const wchar_t *>(buffer.data()),
153                            valueSize / sizeof(wchar_t));
154     if (valueSize && WideValue.back() == L'\0') {
155       WideValue.pop_back();
156     }
157     // The destination buffer must be empty as an invariant of the conversion
158     // function; but this function is sometimes called in a loop that passes in
159     // the same buffer, however. Simply clear it out so we can overwrite it.
160     value.clear();
161     return llvm::convertWideToUTF8(WideValue, value);
162   }
163   return false;
164 }
165 #endif
166 
167 /// Read registry string.
168 /// This also supports a means to look for high-versioned keys by use
169 /// of a $VERSION placeholder in the key path.
170 /// $VERSION in the key path is a placeholder for the version number,
171 /// causing the highest value path to be searched for and used.
172 /// I.e. "SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
173 /// There can be additional characters in the component.  Only the numeric
174 /// characters are compared.  This function only searches HKLM.
175 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
176                                     std::string &value, std::string *phValue) {
177 #ifndef _WIN32
178   return false;
179 #else
180   HKEY hRootKey = HKEY_LOCAL_MACHINE;
181   HKEY hKey = NULL;
182   long lResult;
183   bool returnValue = false;
184 
185   const char *placeHolder = strstr(keyPath, "$VERSION");
186   std::string bestName;
187   // If we have a $VERSION placeholder, do the highest-version search.
188   if (placeHolder) {
189     const char *keyEnd = placeHolder - 1;
190     const char *nextKey = placeHolder;
191     // Find end of previous key.
192     while ((keyEnd > keyPath) && (*keyEnd != '\\'))
193       keyEnd--;
194     // Find end of key containing $VERSION.
195     while (*nextKey && (*nextKey != '\\'))
196       nextKey++;
197     size_t partialKeyLength = keyEnd - keyPath;
198     char partialKey[256];
199     if (partialKeyLength >= sizeof(partialKey))
200       partialKeyLength = sizeof(partialKey) - 1;
201     strncpy(partialKey, keyPath, partialKeyLength);
202     partialKey[partialKeyLength] = '\0';
203     HKEY hTopKey = NULL;
204     lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
205                             &hTopKey);
206     if (lResult == ERROR_SUCCESS) {
207       char keyName[256];
208       double bestValue = 0.0;
209       DWORD index, size = sizeof(keyName) - 1;
210       for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL,
211                                     NULL, NULL) == ERROR_SUCCESS;
212            index++) {
213         const char *sp = keyName;
214         while (*sp && !llvm::isDigit(*sp))
215           sp++;
216         if (!*sp)
217           continue;
218         const char *ep = sp + 1;
219         while (*ep && (llvm::isDigit(*ep) || (*ep == '.')))
220           ep++;
221         char numBuf[32];
222         strncpy(numBuf, sp, sizeof(numBuf) - 1);
223         numBuf[sizeof(numBuf) - 1] = '\0';
224         double dvalue = strtod(numBuf, NULL);
225         if (dvalue > bestValue) {
226           // Test that InstallDir is indeed there before keeping this index.
227           // Open the chosen key path remainder.
228           bestName = keyName;
229           // Append rest of key.
230           bestName.append(nextKey);
231           lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
232                                   KEY_READ | KEY_WOW64_32KEY, &hKey);
233           if (lResult == ERROR_SUCCESS) {
234             if (readFullStringValue(hKey, valueName, value)) {
235               bestValue = dvalue;
236               if (phValue)
237                 *phValue = bestName;
238               returnValue = true;
239             }
240             RegCloseKey(hKey);
241           }
242         }
243         size = sizeof(keyName) - 1;
244       }
245       RegCloseKey(hTopKey);
246     }
247   } else {
248     lResult =
249         RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
250     if (lResult == ERROR_SUCCESS) {
251       if (readFullStringValue(hKey, valueName, value))
252         returnValue = true;
253       if (phValue)
254         phValue->clear();
255       RegCloseKey(hKey);
256     }
257   }
258   return returnValue;
259 #endif // _WIN32
260 }
261 
262 namespace llvm {
263 
264 const char *archToWindowsSDKArch(Triple::ArchType Arch) {
265   switch (Arch) {
266   case Triple::ArchType::x86:
267     return "x86";
268   case Triple::ArchType::x86_64:
269     return "x64";
270   case Triple::ArchType::arm:
271     return "arm";
272   case Triple::ArchType::aarch64:
273     return "arm64";
274   default:
275     return "";
276   }
277 }
278 
279 const char *archToLegacyVCArch(Triple::ArchType Arch) {
280   switch (Arch) {
281   case Triple::ArchType::x86:
282     // x86 is default in legacy VC toolchains.
283     // e.g. x86 libs are directly in /lib as opposed to /lib/x86.
284     return "";
285   case Triple::ArchType::x86_64:
286     return "amd64";
287   case Triple::ArchType::arm:
288     return "arm";
289   case Triple::ArchType::aarch64:
290     return "arm64";
291   default:
292     return "";
293   }
294 }
295 
296 const char *archToDevDivInternalArch(Triple::ArchType Arch) {
297   switch (Arch) {
298   case Triple::ArchType::x86:
299     return "i386";
300   case Triple::ArchType::x86_64:
301     return "amd64";
302   case Triple::ArchType::arm:
303     return "arm";
304   case Triple::ArchType::aarch64:
305     return "arm64";
306   default:
307     return "";
308   }
309 }
310 
311 bool appendArchToWindowsSDKLibPath(int SDKMajor, SmallString<128> LibPath,
312                                    Triple::ArchType Arch, std::string &path) {
313   if (SDKMajor >= 8) {
314     sys::path::append(LibPath, archToWindowsSDKArch(Arch));
315   } else {
316     switch (Arch) {
317     // In Windows SDK 7.x, x86 libraries are directly in the Lib folder.
318     case Triple::x86:
319       break;
320     case Triple::x86_64:
321       sys::path::append(LibPath, "x64");
322       break;
323     case Triple::arm:
324       // It is not necessary to link against Windows SDK 7.x when targeting ARM.
325       return false;
326     default:
327       return false;
328     }
329   }
330 
331   path = std::string(LibPath.str());
332   return true;
333 }
334 
335 std::string getSubDirectoryPath(SubDirectoryType Type, ToolsetLayout VSLayout,
336                                 const std::string &VCToolChainPath,
337                                 Triple::ArchType TargetArch,
338                                 StringRef SubdirParent) {
339   const char *SubdirName;
340   const char *IncludeName;
341   switch (VSLayout) {
342   case ToolsetLayout::OlderVS:
343     SubdirName = archToLegacyVCArch(TargetArch);
344     IncludeName = "include";
345     break;
346   case ToolsetLayout::VS2017OrNewer:
347     SubdirName = archToWindowsSDKArch(TargetArch);
348     IncludeName = "include";
349     break;
350   case ToolsetLayout::DevDivInternal:
351     SubdirName = archToDevDivInternalArch(TargetArch);
352     IncludeName = "inc";
353     break;
354   }
355 
356   SmallString<256> Path(VCToolChainPath);
357   if (!SubdirParent.empty())
358     sys::path::append(Path, SubdirParent);
359 
360   switch (Type) {
361   case SubDirectoryType::Bin:
362     if (VSLayout == ToolsetLayout::VS2017OrNewer) {
363       const bool HostIsX64 = Triple(sys::getProcessTriple()).isArch64Bit();
364       const char *const HostName = HostIsX64 ? "Hostx64" : "Hostx86";
365       sys::path::append(Path, "bin", HostName, SubdirName);
366     } else { // OlderVS or DevDivInternal
367       sys::path::append(Path, "bin", SubdirName);
368     }
369     break;
370   case SubDirectoryType::Include:
371     sys::path::append(Path, IncludeName);
372     break;
373   case SubDirectoryType::Lib:
374     sys::path::append(Path, "lib", SubdirName);
375     break;
376   }
377   return std::string(Path.str());
378 }
379 
380 bool useUniversalCRT(ToolsetLayout VSLayout, const std::string &VCToolChainPath,
381                      Triple::ArchType TargetArch, vfs::FileSystem &VFS) {
382   SmallString<128> TestPath(getSubDirectoryPath(
383       SubDirectoryType::Include, VSLayout, VCToolChainPath, TargetArch));
384   sys::path::append(TestPath, "stdlib.h");
385   return !VFS.exists(TestPath);
386 }
387 
388 bool getWindowsSDKDir(vfs::FileSystem &VFS, StringRef WinSdkDir,
389                       StringRef WinSdkVersion, StringRef WinSysRoot,
390                       std::string &Path, int &Major,
391                       std::string &WindowsSDKIncludeVersion,
392                       std::string &WindowsSDKLibVersion) {
393   // Trust /winsdkdir and /winsdkversion if present.
394   if (getWindowsSDKDirViaCommandLine(VFS, WinSdkDir, WinSdkVersion, WinSysRoot,
395                                      Path, Major, WindowsSDKIncludeVersion)) {
396     WindowsSDKLibVersion = WindowsSDKIncludeVersion;
397     return true;
398   }
399 
400   // FIXME: Try env vars (%WindowsSdkDir%, %UCRTVersion%) before going to
401   // registry.
402 
403   // Try the Windows registry.
404   std::string RegistrySDKVersion;
405   if (!getSystemRegistryString(
406           "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
407           "InstallationFolder", Path, &RegistrySDKVersion))
408     return false;
409   if (Path.empty() || RegistrySDKVersion.empty())
410     return false;
411 
412   WindowsSDKIncludeVersion.clear();
413   WindowsSDKLibVersion.clear();
414   Major = 0;
415   std::sscanf(RegistrySDKVersion.c_str(), "v%d.", &Major);
416   if (Major <= 7)
417     return true;
418   if (Major == 8) {
419     // Windows SDK 8.x installs libraries in a folder whose names depend on the
420     // version of the OS you're targeting.  By default choose the newest, which
421     // usually corresponds to the version of the OS you've installed the SDK on.
422     const char *Tests[] = {"winv6.3", "win8", "win7"};
423     for (const char *Test : Tests) {
424       SmallString<128> TestPath(Path);
425       sys::path::append(TestPath, "Lib", Test);
426       if (VFS.exists(TestPath)) {
427         WindowsSDKLibVersion = Test;
428         break;
429       }
430     }
431     return !WindowsSDKLibVersion.empty();
432   }
433   if (Major == 10) {
434     if (!getWindows10SDKVersionFromPath(VFS, Path, WindowsSDKIncludeVersion))
435       return false;
436     WindowsSDKLibVersion = WindowsSDKIncludeVersion;
437     return true;
438   }
439   // Unsupported SDK version
440   return false;
441 }
442 
443 bool getUniversalCRTSdkDir(vfs::FileSystem &VFS, StringRef WinSdkDir,
444                            StringRef WinSdkVersion, StringRef WinSysRoot,
445                            std::string &Path, std::string &UCRTVersion) {
446   // If /winsdkdir is passed, use it as location for the UCRT too.
447   // FIXME: Should there be a dedicated /ucrtdir to override /winsdkdir?
448   int Major;
449   if (getWindowsSDKDirViaCommandLine(VFS, WinSdkDir, WinSdkVersion, WinSysRoot,
450                                      Path, Major, UCRTVersion))
451     return true;
452 
453   // FIXME: Try env vars (%UniversalCRTSdkDir%, %UCRTVersion%) before going to
454   // registry.
455 
456   // vcvarsqueryregistry.bat for Visual Studio 2015 queries the registry
457   // for the specific key "KitsRoot10". So do we.
458   if (!getSystemRegistryString(
459           "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", "KitsRoot10",
460           Path, nullptr))
461     return false;
462 
463   return getWindows10SDKVersionFromPath(VFS, Path, UCRTVersion);
464 }
465 
466 bool findVCToolChainViaCommandLine(vfs::FileSystem &VFS, StringRef VCToolsDir,
467                                    StringRef VCToolsVersion,
468                                    StringRef WinSysRoot, std::string &Path,
469                                    ToolsetLayout &VSLayout) {
470   // Don't validate the input; trust the value supplied by the user.
471   // The primary motivation is to prevent unnecessary file and registry access.
472   if (!VCToolsDir.empty() || !WinSysRoot.empty()) {
473     if (!WinSysRoot.empty()) {
474       SmallString<128> ToolsPath(WinSysRoot);
475       sys::path::append(ToolsPath, "VC", "Tools", "MSVC");
476       std::string ToolsVersion;
477       if (!VCToolsVersion.empty())
478         ToolsVersion = VCToolsVersion.str();
479       else
480         ToolsVersion = getHighestNumericTupleInDirectory(VFS, ToolsPath);
481       sys::path::append(ToolsPath, ToolsVersion);
482       Path = std::string(ToolsPath.str());
483     } else {
484       Path = VCToolsDir.str();
485     }
486     VSLayout = ToolsetLayout::VS2017OrNewer;
487     return true;
488   }
489   return false;
490 }
491 
492 bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path,
493                                    ToolsetLayout &VSLayout) {
494   // These variables are typically set by vcvarsall.bat
495   // when launching a developer command prompt.
496   if (Optional<std::string> VCToolsInstallDir =
497           sys::Process::GetEnv("VCToolsInstallDir")) {
498     // This is only set by newer Visual Studios, and it leads straight to
499     // the toolchain directory.
500     Path = std::move(*VCToolsInstallDir);
501     VSLayout = ToolsetLayout::VS2017OrNewer;
502     return true;
503   }
504   if (Optional<std::string> VCInstallDir =
505           sys::Process::GetEnv("VCINSTALLDIR")) {
506     // If the previous variable isn't set but this one is, then we've found
507     // an older Visual Studio. This variable is set by newer Visual Studios too,
508     // so this check has to appear second.
509     // In older Visual Studios, the VC directory is the toolchain.
510     Path = std::move(*VCInstallDir);
511     VSLayout = ToolsetLayout::OlderVS;
512     return true;
513   }
514 
515   // We couldn't find any VC environment variables. Let's walk through PATH and
516   // see if it leads us to a VC toolchain bin directory. If it does, pick the
517   // first one that we find.
518   if (Optional<std::string> PathEnv = sys::Process::GetEnv("PATH")) {
519     SmallVector<StringRef, 8> PathEntries;
520     StringRef(*PathEnv).split(PathEntries, sys::EnvPathSeparator);
521     for (StringRef PathEntry : PathEntries) {
522       if (PathEntry.empty())
523         continue;
524 
525       SmallString<256> ExeTestPath;
526 
527       // If cl.exe doesn't exist, then this definitely isn't a VC toolchain.
528       ExeTestPath = PathEntry;
529       sys::path::append(ExeTestPath, "cl.exe");
530       if (!VFS.exists(ExeTestPath))
531         continue;
532 
533       // cl.exe existing isn't a conclusive test for a VC toolchain; clang also
534       // has a cl.exe. So let's check for link.exe too.
535       ExeTestPath = PathEntry;
536       sys::path::append(ExeTestPath, "link.exe");
537       if (!VFS.exists(ExeTestPath))
538         continue;
539 
540       // whatever/VC/bin --> old toolchain, VC dir is toolchain dir.
541       StringRef TestPath = PathEntry;
542       bool IsBin = sys::path::filename(TestPath).equals_insensitive("bin");
543       if (!IsBin) {
544         // Strip any architecture subdir like "amd64".
545         TestPath = sys::path::parent_path(TestPath);
546         IsBin = sys::path::filename(TestPath).equals_insensitive("bin");
547       }
548       if (IsBin) {
549         StringRef ParentPath = sys::path::parent_path(TestPath);
550         StringRef ParentFilename = sys::path::filename(ParentPath);
551         if (ParentFilename.equals_insensitive("VC")) {
552           Path = std::string(ParentPath);
553           VSLayout = ToolsetLayout::OlderVS;
554           return true;
555         }
556         if (ParentFilename.equals_insensitive("x86ret") ||
557             ParentFilename.equals_insensitive("x86chk") ||
558             ParentFilename.equals_insensitive("amd64ret") ||
559             ParentFilename.equals_insensitive("amd64chk")) {
560           Path = std::string(ParentPath);
561           VSLayout = ToolsetLayout::DevDivInternal;
562           return true;
563         }
564 
565       } else {
566         // This could be a new (>=VS2017) toolchain. If it is, we should find
567         // path components with these prefixes when walking backwards through
568         // the path.
569         // Note: empty strings match anything.
570         StringRef ExpectedPrefixes[] = {"",     "Host",  "bin", "",
571                                         "MSVC", "Tools", "VC"};
572 
573         auto It = sys::path::rbegin(PathEntry);
574         auto End = sys::path::rend(PathEntry);
575         for (StringRef Prefix : ExpectedPrefixes) {
576           if (It == End)
577             goto NotAToolChain;
578           if (!It->startswith_insensitive(Prefix))
579             goto NotAToolChain;
580           ++It;
581         }
582 
583         // We've found a new toolchain!
584         // Back up 3 times (/bin/Host/arch) to get the root path.
585         StringRef ToolChainPath(PathEntry);
586         for (int i = 0; i < 3; ++i)
587           ToolChainPath = sys::path::parent_path(ToolChainPath);
588 
589         Path = std::string(ToolChainPath);
590         VSLayout = ToolsetLayout::VS2017OrNewer;
591         return true;
592       }
593 
594     NotAToolChain:
595       continue;
596     }
597   }
598   return false;
599 }
600 
601 bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::string &Path,
602                                    ToolsetLayout &VSLayout) {
603 #if !defined(USE_MSVC_SETUP_API)
604   return false;
605 #else
606   // FIXME: This really should be done once in the top-level program's main
607   // function, as it may have already been initialized with a different
608   // threading model otherwise.
609   sys::InitializeCOMRAII COM(sys::COMThreadingMode::SingleThreaded);
610   HRESULT HR;
611 
612   // _com_ptr_t will throw a _com_error if a COM calls fail.
613   // The LLVM coding standards forbid exception handling, so we'll have to
614   // stop them from being thrown in the first place.
615   // The destructor will put the regular error handler back when we leave
616   // this scope.
617   struct SuppressCOMErrorsRAII {
618     static void __stdcall handler(HRESULT hr, IErrorInfo *perrinfo) {}
619 
620     SuppressCOMErrorsRAII() { _set_com_error_handler(handler); }
621 
622     ~SuppressCOMErrorsRAII() { _set_com_error_handler(_com_raise_error); }
623 
624   } COMErrorSuppressor;
625 
626   ISetupConfigurationPtr Query;
627   HR = Query.CreateInstance(__uuidof(SetupConfiguration));
628   if (FAILED(HR))
629     return false;
630 
631   IEnumSetupInstancesPtr EnumInstances;
632   HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
633   if (FAILED(HR))
634     return false;
635 
636   ISetupInstancePtr Instance;
637   HR = EnumInstances->Next(1, &Instance, nullptr);
638   if (HR != S_OK)
639     return false;
640 
641   ISetupInstancePtr NewestInstance;
642   Optional<uint64_t> NewestVersionNum;
643   do {
644     bstr_t VersionString;
645     uint64_t VersionNum;
646     HR = Instance->GetInstallationVersion(VersionString.GetAddress());
647     if (FAILED(HR))
648       continue;
649     HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
650     if (FAILED(HR))
651       continue;
652     if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
653       NewestInstance = Instance;
654       NewestVersionNum = VersionNum;
655     }
656   } while ((HR = EnumInstances->Next(1, &Instance, nullptr)) == S_OK);
657 
658   if (!NewestInstance)
659     return false;
660 
661   bstr_t VCPathWide;
662   HR = NewestInstance->ResolvePath(L"VC", VCPathWide.GetAddress());
663   if (FAILED(HR))
664     return false;
665 
666   std::string VCRootPath;
667   convertWideToUTF8(std::wstring(VCPathWide), VCRootPath);
668 
669   SmallString<256> ToolsVersionFilePath(VCRootPath);
670   sys::path::append(ToolsVersionFilePath, "Auxiliary", "Build",
671                     "Microsoft.VCToolsVersion.default.txt");
672 
673   auto ToolsVersionFile = MemoryBuffer::getFile(ToolsVersionFilePath);
674   if (!ToolsVersionFile)
675     return false;
676 
677   SmallString<256> ToolchainPath(VCRootPath);
678   sys::path::append(ToolchainPath, "Tools", "MSVC",
679                     ToolsVersionFile->get()->getBuffer().rtrim());
680   auto Status = VFS.status(ToolchainPath);
681   if (!Status || !Status->isDirectory())
682     return false;
683 
684   Path = std::string(ToolchainPath.str());
685   VSLayout = ToolsetLayout::VS2017OrNewer;
686   return true;
687 #endif
688 }
689 
690 bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout) {
691   std::string VSInstallPath;
692   if (getSystemRegistryString(R"(SOFTWARE\Microsoft\VisualStudio\$VERSION)",
693                               "InstallDir", VSInstallPath, nullptr) ||
694       getSystemRegistryString(R"(SOFTWARE\Microsoft\VCExpress\$VERSION)",
695                               "InstallDir", VSInstallPath, nullptr)) {
696     if (!VSInstallPath.empty()) {
697       SmallString<256> VCPath(StringRef(VSInstallPath.c_str(),
698                                         VSInstallPath.find(R"(\Common7\IDE)")));
699       sys::path::append(VCPath, "VC");
700 
701       Path = std::string(VCPath.str());
702       VSLayout = ToolsetLayout::OlderVS;
703       return true;
704     }
705   }
706   return false;
707 }
708 
709 } // namespace llvm
710