29 #define WIN32_LEAN_AND_MEAN
39 #define USE_MSVC_SETUP_API
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
51 #pragma clang diagnostic pop
70 !EC && DirIt != DirEnd; DirIt.increment(EC)) {
78 if (Tuple > HighestTuple) {
88 const std::string &SDKPath,
89 std::string &SDKVersion) {
93 return !SDKVersion.empty();
106 SDKVersion.
tryParse(*WinSdkVersion);
111 if (!SDKVersion.
empty())
116 Path = std::string(SDKPath.
str());
118 Path = WinSdkDir->
str();
121 if (!SDKVersion.
empty()) {
133 static bool readFullStringValue(HKEY hkey,
const char *valueName,
134 std::string &value) {
135 std::wstring WideValueName;
143 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &
type, NULL,
145 if (
result != ERROR_SUCCESS ||
type != REG_SZ || !valueSize)
147 std::vector<BYTE> buffer(valueSize);
148 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
150 if (
result == ERROR_SUCCESS) {
151 std::wstring WideValue(
reinterpret_cast<const wchar_t *
>(buffer.data()),
152 valueSize /
sizeof(
wchar_t));
153 if (valueSize && WideValue.back() == L
'\0') {
154 WideValue.pop_back();
175 std::string &value, std::string *phValue) {
179 HKEY hRootKey = HKEY_LOCAL_MACHINE;
182 bool returnValue =
false;
184 const char *placeHolder = strstr(keyPath,
"$VERSION");
185 std::string bestName;
188 const char *keyEnd = placeHolder - 1;
189 const char *nextKey = placeHolder;
191 while ((keyEnd > keyPath) && (*keyEnd !=
'\\'))
194 while (*nextKey && (*nextKey !=
'\\'))
196 size_t partialKeyLength = keyEnd - keyPath;
197 char partialKey[256];
198 if (partialKeyLength >=
sizeof(partialKey))
199 partialKeyLength =
sizeof(partialKey) - 1;
200 strncpy(partialKey, keyPath, partialKeyLength);
201 partialKey[partialKeyLength] =
'\0';
203 lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
205 if (lResult == ERROR_SUCCESS) {
207 double bestValue = 0.0;
209 for (
index = 0; RegEnumKeyExA(hTopKey,
index, keyName, &
size, NULL, NULL,
210 NULL, NULL) == ERROR_SUCCESS;
212 const char *
sp = keyName;
217 const char *ep =
sp + 1;
221 strncpy(numBuf,
sp,
sizeof(numBuf) - 1);
222 numBuf[
sizeof(numBuf) - 1] =
'\0';
223 double dvalue = strtod(numBuf, NULL);
224 if (dvalue > bestValue) {
229 bestName.append(nextKey);
230 lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
231 KEY_READ | KEY_WOW64_32KEY, &hKey);
232 if (lResult == ERROR_SUCCESS) {
233 if (readFullStringValue(hKey, valueName, value)) {
242 size =
sizeof(keyName) - 1;
244 RegCloseKey(hTopKey);
248 RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
249 if (lResult == ERROR_SUCCESS) {
250 if (readFullStringValue(hKey, valueName, value))
267 case Triple::ArchType::x86_64:
269 case Triple::ArchType::arm:
271 case Triple::ArchType::aarch64:
284 case Triple::ArchType::x86_64:
286 case Triple::ArchType::arm:
288 case Triple::ArchType::aarch64:
299 case Triple::ArchType::x86_64:
301 case Triple::ArchType::arm:
303 case Triple::ArchType::aarch64:
330 path = std::string(LibPath.
str());
335 const std::string &VCToolChainPath,
338 const char *SubdirName;
339 const char *IncludeName;
343 IncludeName =
"include";
347 IncludeName =
"include";
356 if (!SubdirParent.
empty())
370 const bool HostIsX64 =
372 const char *
const HostName = HostIsX64 ?
"Hostx64" :
"Hostx86";
385 return std::string(Path.str());
393 return !VFS.
exists(TestPath);
399 int &Major, std::string &WindowsSDKIncludeVersion,
400 std::string &WindowsSDKLibVersion) {
403 Path, Major, WindowsSDKIncludeVersion)) {
404 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
412 std::string RegistrySDKVersion;
414 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
415 "InstallationFolder", Path, &RegistrySDKVersion))
417 if (Path.empty() || RegistrySDKVersion.empty())
420 WindowsSDKIncludeVersion.clear();
421 WindowsSDKLibVersion.clear();
423 std::sscanf(RegistrySDKVersion.c_str(),
"v%d.", &Major);
430 const char *Tests[] = {
"winv6.3",
"win8",
"win7"};
431 for (
const char *
Test : Tests) {
434 if (VFS.
exists(TestPath)) {
435 WindowsSDKLibVersion =
Test;
439 return !WindowsSDKLibVersion.empty();
444 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
454 std::string &UCRTVersion) {
459 Path, Major, UCRTVersion))
468 "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots",
"KitsRoot10",
486 std::string ToolsVersion;
488 ToolsVersion = VCToolsVersion->
str();
492 Path = std::string(ToolsPath.
str());
494 Path = VCToolsDir->
str();
531 for (
StringRef PathEntry : PathEntries) {
532 if (PathEntry.empty())
538 ExeTestPath = PathEntry;
540 if (!VFS.
exists(ExeTestPath))
545 ExeTestPath = PathEntry;
547 if (!VFS.
exists(ExeTestPath))
562 Path = std::string(ParentPath);
570 Path = std::string(ParentPath);
580 StringRef ExpectedPrefixes[] = {
"",
"Host",
"bin",
"",
581 "MSVC",
"Tools",
"VC"};
588 if (!It->startswith_insensitive(
Prefix))
596 for (
int i = 0;
i < 3; ++
i)
599 Path = std::string(ToolChainPath);
613 #if !defined(USE_MSVC_SETUP_API)
627 struct SuppressCOMErrorsRAII {
628 static void __stdcall handler(HRESULT hr, IErrorInfo *perrinfo) {}
630 SuppressCOMErrorsRAII() { _set_com_error_handler(handler); }
632 ~SuppressCOMErrorsRAII() { _set_com_error_handler(_com_raise_error); }
634 } COMErrorSuppressor;
636 ISetupConfigurationPtr Query;
637 HR = Query.CreateInstance(__uuidof(SetupConfiguration));
641 IEnumSetupInstancesPtr EnumInstances;
642 HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
646 ISetupInstancePtr Instance;
647 HR = EnumInstances->Next(1, &Instance,
nullptr);
651 ISetupInstancePtr NewestInstance;
654 bstr_t VersionString;
656 HR = Instance->GetInstallationVersion(VersionString.GetAddress());
659 HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
662 if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
663 NewestInstance = Instance;
664 NewestVersionNum = VersionNum;
666 }
while ((HR = EnumInstances->Next(1, &Instance,
nullptr)) == S_OK);
672 HR = NewestInstance->ResolvePath(L
"VC", VCPathWide.GetAddress());
676 std::string VCRootPath;
681 "Microsoft.VCToolsVersion.default.txt");
684 if (!ToolsVersionFile)
689 ToolsVersionFile->get()->getBuffer().rtrim());
694 Path = std::string(ToolchainPath.
str());
701 std::string VSInstallPath;
703 "InstallDir", VSInstallPath,
nullptr) ||
705 "InstallDir", VSInstallPath,
nullptr)) {
706 if (!VSInstallPath.empty()) {
708 VSInstallPath.find(R
"(\Common7\IDE)")));
711 Path = std::string(VCPath.
str());