30#define WIN32_LEAN_AND_MEAN
40#define USE_MSVC_SETUP_API
47#pragma clang diagnostic push
48#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
52#pragma clang diagnostic pop
71 !EC && DirIt != DirEnd; DirIt.increment(EC)) {
79 if (Tuple > HighestTuple) {
81 Highest = CandidateName.
str();
89 const std::string &SDKPath,
90 std::string &SDKVersion) {
94 return !SDKVersion.empty();
99 std::optional<llvm::StringRef> WinSdkVersion,
100 std::optional<llvm::StringRef> WinSysRoot, std::string &Path,
int &Major,
101 std::string &Version) {
102 if (WinSdkDir || WinSysRoot) {
107 SDKVersion.
tryParse(*WinSdkVersion);
112 if (!SDKVersion.
empty())
117 Path = std::string(SDKPath);
119 Path = WinSdkDir->str();
122 if (!SDKVersion.
empty()) {
134static bool readFullStringValue(HKEY hkey,
const char *valueName,
135 std::string &
value) {
136 std::wstring WideValueName;
144 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
146 if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
148 std::vector<BYTE> buffer(valueSize);
149 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
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();
176 std::string &
value, std::string *phValue) {
180 HKEY hRootKey = HKEY_LOCAL_MACHINE;
183 bool returnValue =
false;
185 const char *placeHolder = strstr(keyPath,
"$VERSION");
186 std::string bestName;
189 const char *keyEnd = placeHolder - 1;
190 const char *nextKey = placeHolder;
192 while ((keyEnd > keyPath) && (*keyEnd !=
'\\'))
195 while (*nextKey && (*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';
204 lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
206 if (lResult == ERROR_SUCCESS) {
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;
213 const char *sp = keyName;
214 while (*sp && !llvm::isDigit(*sp))
218 const char *ep = sp + 1;
219 while (*ep && (llvm::isDigit(*ep) || (*ep ==
'.')))
222 strncpy(numBuf, sp,
sizeof(numBuf) - 1);
223 numBuf[
sizeof(numBuf) - 1] =
'\0';
224 double dvalue = strtod(numBuf, NULL);
225 if (dvalue > bestValue) {
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)) {
243 size =
sizeof(keyName) - 1;
245 RegCloseKey(hTopKey);
249 RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
250 if (lResult == ERROR_SUCCESS) {
251 if (readFullStringValue(hKey, valueName,
value))
335 path = std::string(LibPath);
340 const std::string &VCToolChainPath,
343 const char *SubdirName;
344 const char *IncludeName;
348 IncludeName =
"include";
352 IncludeName =
"include";
361 if (!SubdirParent.
empty())
375 const bool HostIsX64 =
377 const char *
const HostName = HostIsX64 ?
"Hostx64" :
"Hostx86";
390 return std::string(Path);
398 return !VFS.
exists(TestPath);
402 std::optional<StringRef> WinSdkVersion,
403 std::optional<StringRef> WinSysRoot, std::string &Path,
404 int &Major, std::string &WindowsSDKIncludeVersion,
405 std::string &WindowsSDKLibVersion) {
408 Path, Major, WindowsSDKIncludeVersion)) {
409 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
417 std::string RegistrySDKVersion;
419 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
420 "InstallationFolder", Path, &RegistrySDKVersion))
422 if (Path.empty() || RegistrySDKVersion.empty())
425 WindowsSDKIncludeVersion.clear();
426 WindowsSDKLibVersion.clear();
428 std::sscanf(RegistrySDKVersion.c_str(),
"v%d.", &Major);
435 const char *Tests[] = {
"winv6.3",
"win8",
"win7"};
436 for (
const char *
Test : Tests) {
439 if (VFS.
exists(TestPath)) {
440 WindowsSDKLibVersion =
Test;
444 return !WindowsSDKLibVersion.empty();
449 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
457 std::optional<StringRef> WinSdkDir,
458 std::optional<StringRef> WinSdkVersion,
459 std::optional<StringRef> WinSysRoot,
460 std::string &Path, std::string &UCRTVersion) {
465 Path, Major, UCRTVersion))
474 "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots",
"KitsRoot10",
482 std::optional<StringRef> VCToolsDir,
483 std::optional<StringRef> VCToolsVersion,
484 std::optional<StringRef> WinSysRoot,
488 if (VCToolsDir || WinSysRoot) {
492 std::string ToolsVersion;
494 ToolsVersion = VCToolsVersion->str();
498 Path = std::string(ToolsPath);
500 Path = VCToolsDir->str();
512 if (std::optional<std::string> VCToolsInstallDir =
516 Path = std::move(*VCToolsInstallDir);
520 if (std::optional<std::string> VCInstallDir =
526 Path = std::move(*VCInstallDir);
537 for (
StringRef PathEntry : PathEntries) {
538 if (PathEntry.empty())
544 ExeTestPath = PathEntry;
546 if (!VFS.
exists(ExeTestPath))
551 ExeTestPath = PathEntry;
553 if (!VFS.
exists(ExeTestPath))
568 Path = std::string(ParentPath);
576 Path = std::string(ParentPath);
586 StringRef ExpectedPrefixes[] = {
"",
"Host",
"bin",
"",
587 "MSVC",
"Tools",
"VC"};
591 for (
StringRef Prefix : ExpectedPrefixes) {
594 if (!It->starts_with_insensitive(Prefix))
602 for (
int i = 0; i < 3; ++i)
605 Path = std::string(ToolChainPath);
618 std::optional<StringRef> VCToolsVersion,
620#if !defined(USE_MSVC_SETUP_API)
634 struct SuppressCOMErrorsRAII {
635 static void __stdcall handler(HRESULT hr, IErrorInfo *perrinfo) {}
637 SuppressCOMErrorsRAII() { _set_com_error_handler(handler); }
639 ~SuppressCOMErrorsRAII() { _set_com_error_handler(_com_raise_error); }
641 } COMErrorSuppressor;
643 ISetupConfigurationPtr Query;
644 HR = Query.CreateInstance(__uuidof(SetupConfiguration));
648 IEnumSetupInstancesPtr EnumInstances;
649 HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
653 ISetupInstancePtr Instance;
654 HR = EnumInstances->Next(1, &Instance,
nullptr);
658 ISetupInstancePtr NewestInstance;
659 std::optional<uint64_t> NewestVersionNum;
661 bstr_t VersionString;
663 HR = Instance->GetInstallationVersion(VersionString.GetAddress());
666 HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
669 if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
670 NewestInstance = Instance;
671 NewestVersionNum = VersionNum;
673 }
while ((HR = EnumInstances->Next(1, &Instance,
nullptr)) == S_OK);
679 HR = NewestInstance->ResolvePath(L
"VC", VCPathWide.GetAddress());
683 std::string VCRootPath;
686 std::string ToolsVersion;
687 if (VCToolsVersion.has_value()) {
688 ToolsVersion = *VCToolsVersion;
692 "Microsoft.VCToolsVersion.default.txt");
695 if (!ToolsVersionFile)
698 ToolsVersion = ToolsVersionFile->get()->getBuffer().rtrim();
708 Path = std::string(ToolchainPath.
str());
715 std::string VSInstallPath;
717 "InstallDir", VSInstallPath,
nullptr) ||
719 "InstallDir", VSInstallPath,
nullptr)) {
720 if (!VSInstallPath.empty()) {
721 auto pos = VSInstallPath.find(R
"(\Common7\IDE)");
722 if (pos == std::string::npos)
727 Path = std::string(VCPath);
Provides a library for accessing COM functionality of the Host OS.
Given that RA is a live value
static bool getSystemRegistryString(const char *keyPath, const char *valueName, std::string &value, std::string *phValue)
Read registry string.
static bool getWindowsSDKDirViaCommandLine(llvm::vfs::FileSystem &VFS, std::optional< llvm::StringRef > WinSdkDir, std::optional< llvm::StringRef > WinSdkVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, int &Major, std::string &Version)
static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS, const std::string &SDKPath, std::string &SDKVersion)
static std::string getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS, llvm::StringRef Directory)
struct ISetupInstance2 ISetupInstance2
struct ISetupInstance ISetupInstance
struct ISetupConfiguration ISetupConfiguration
struct ISetupHelper ISetupHelper
struct ISetupConfiguration2 ISetupConfiguration2
struct IEnumSetupInstances IEnumSetupInstances
Provides a library for accessing information about this process and other processes on the operating ...
This file defines the SmallString class.
This file defines the SmallVector class.
Defines the llvm::VersionTuple class, which represents a version in the form major[....
Defines the virtual file system interface vfs::FileSystem.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef str() const
Explicit conversion to StringRef.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Triple - Helper class for working with autoconf configuration names.
ArchType getArch() const
Get the parsed architecture type of this triple.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
Represents a version number in the form major[.minor[.subminor[.build]]].
unsigned getMajor() const
Retrieve the major version number.
bool tryParse(StringRef string)
Try to parse the given string as a version number.
std::string getAsString() const
Retrieve a string representation of the version number.
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
static std::optional< std::string > GetEnv(StringRef name)
The virtual file system interface.
virtual bool exists(const Twine &Path)
Check whether Path exists.
virtual directory_iterator dir_begin(const Twine &Dir, std::error_code &EC)=0
Get a directory_iterator for Dir.
virtual llvm::ErrorOr< Status > status(const Twine &Path)=0
Get the status of the entry at Path, if one exists.
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
This is an optimization pass for GlobalISel generic memory operations.
const char * archToWindowsSDKArch(llvm::Triple::ArchType Arch)
bool findVCToolChainViaCommandLine(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsDir, std::optional< llvm::StringRef > VCToolsVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, ToolsetLayout &VSLayout)
bool getWindowsSDKDir(vfs::FileSystem &VFS, std::optional< llvm::StringRef > WinSdkDir, std::optional< llvm::StringRef > WinSdkVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, int &Major, std::string &WindowsSDKIncludeVersion, std::string &WindowsSDKLibVersion)
Get Windows SDK installation directory.
bool useUniversalCRT(ToolsetLayout VSLayout, const std::string &VCToolChainPath, llvm::Triple::ArchType TargetArch, llvm::vfs::FileSystem &VFS)
bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path, ToolsetLayout &VSLayout)
bool convertWideToUTF8(const std::wstring &Source, std::string &Result)
Converts a std::wstring to a UTF-8 encoded std::string.
bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsVersion, std::string &Path, ToolsetLayout &VSLayout)
const char * archToLegacyVCArch(llvm::Triple::ArchType Arch)
bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on WideCharWidth.
const char * archToDevDivInternalArch(llvm::Triple::ArchType Arch)
std::string getSubDirectoryPath(SubDirectoryType Type, ToolsetLayout VSLayout, const std::string &VCToolChainPath, llvm::Triple::ArchType TargetArch, llvm::StringRef SubdirParent="")
bool appendArchToWindowsSDKLibPath(int SDKMajor, llvm::SmallString< 128 > LibPath, llvm::Triple::ArchType Arch, std::string &path)
bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout)
bool getUniversalCRTSdkDir(vfs::FileSystem &VFS, std::optional< llvm::StringRef > WinSdkDir, std::optional< llvm::StringRef > WinSdkVersion, std::optional< llvm::StringRef > WinSysRoot, std::string &Path, std::string &UCRTVersion)