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