30#ifndef WIN32_LEAN_AND_MEAN
31#define WIN32_LEAN_AND_MEAN
44#pragma clang diagnostic push
45#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
49#pragma clang diagnostic pop
54__CRT_UUID_DECL(
ISetupInstance, 0xB41463C3, 0x8866, 0x43B5, 0xBC, 0x33, 0x2B,
55 0x06, 0x76, 0xF7, 0xF4, 0x2E)
56__CRT_UUID_DECL(
ISetupInstance2, 0x89143C9A, 0x05AF, 0x49B0, 0xB7, 0x17, 0x72,
57 0xE2, 0x18, 0xA2, 0x18, 0x5C)
59 0xBF, 0x8A, 0x68, 0x10, 0xC8, 0x48)
61 0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B)
63 0x3C, 0x35, 0xBC, 0x93, 0x36, 0x5D)
64__CRT_UUID_DECL(
ISetupHelper, 0x42B21B78, 0x6192, 0x463E, 0x87, 0xBF, 0xD5,
65 0x77, 0x83, 0x8F, 0x1D, 0x5C)
66__CRT_UUID_DECL(SetupConfiguration, 0x177F0C4A, 0x1CD3, 0x4DE7, 0xA3, 0x2C,
67 0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D)
86 !EC && DirIt != DirEnd; DirIt.increment(EC)) {
92 if (Tuple.tryParse(CandidateName))
94 if (Tuple > HighestTuple) {
96 Highest = CandidateName.
str();
104 const std::string &SDKPath,
105 std::string &SDKVersion) {
109 return !SDKVersion.empty();
114 std::optional<llvm::StringRef> WinSdkVersion,
115 std::optional<llvm::StringRef> WinSysRoot, std::string &Path,
int &Major,
116 std::string &Version) {
117 if (WinSdkDir || WinSysRoot) {
122 SDKVersion.
tryParse(*WinSdkVersion);
127 if (!SDKVersion.
empty())
132 Path = std::string(SDKPath);
134 Path = WinSdkDir->str();
137 if (!SDKVersion.
empty()) {
149static bool readFullStringValue(HKEY hkey,
const char *valueName,
150 std::string &value) {
151 std::wstring WideValueName;
159 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, &type, NULL,
161 if (result != ERROR_SUCCESS || type != REG_SZ || !valueSize)
163 std::vector<BYTE> buffer(valueSize);
164 result = RegQueryValueExW(hkey, WideValueName.c_str(), NULL, NULL, &buffer[0],
166 if (result == ERROR_SUCCESS) {
167 std::wstring WideValue(
reinterpret_cast<const wchar_t *
>(buffer.data()),
168 valueSize /
sizeof(
wchar_t));
169 if (valueSize && WideValue.back() == L
'\0') {
170 WideValue.pop_back();
191 std::string &value, std::string *phValue) {
195 HKEY hRootKey = HKEY_LOCAL_MACHINE;
198 bool returnValue =
false;
200 const char *placeHolder = strstr(keyPath,
"$VERSION");
201 std::string bestName;
204 const char *keyEnd = placeHolder - 1;
205 const char *nextKey = placeHolder;
207 while ((keyEnd > keyPath) && (*keyEnd !=
'\\'))
210 while (*nextKey && (*nextKey !=
'\\'))
212 size_t partialKeyLength = keyEnd - keyPath;
213 char partialKey[256];
214 if (partialKeyLength >=
sizeof(partialKey))
215 partialKeyLength =
sizeof(partialKey) - 1;
216 strncpy(partialKey, keyPath, partialKeyLength);
217 partialKey[partialKeyLength] =
'\0';
219 lResult = RegOpenKeyExA(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
221 if (lResult == ERROR_SUCCESS) {
223 double bestValue = 0.0;
224 DWORD index, size =
sizeof(keyName) - 1;
225 for (index = 0; RegEnumKeyExA(hTopKey, index, keyName, &size, NULL, NULL,
226 NULL, NULL) == ERROR_SUCCESS;
228 const char *sp = keyName;
233 const char *ep = sp + 1;
237 strncpy(numBuf, sp,
sizeof(numBuf) - 1);
238 numBuf[
sizeof(numBuf) - 1] =
'\0';
239 double dvalue = strtod(numBuf, NULL);
240 if (dvalue > bestValue) {
245 bestName.append(nextKey);
246 lResult = RegOpenKeyExA(hTopKey, bestName.c_str(), 0,
247 KEY_READ | KEY_WOW64_32KEY, &hKey);
248 if (lResult == ERROR_SUCCESS) {
249 if (readFullStringValue(hKey, valueName, value)) {
258 size =
sizeof(keyName) - 1;
260 RegCloseKey(hTopKey);
264 RegOpenKeyExA(hRootKey, keyPath, 0, KEY_READ | KEY_WOW64_32KEY, &hKey);
265 if (lResult == ERROR_SUCCESS) {
266 if (readFullStringValue(hKey, valueName, value))
349 path = std::string(LibPath);
355 const std::string &VCToolChainPath,
358 const char *SubdirName;
359 const char *IncludeName;
363 IncludeName =
"include";
367 IncludeName =
"include";
376 if (!SubdirParent.
empty())
390 const bool HostIsX64 =
392 const char *
const HostName = HostIsX64 ?
"Hostx64" :
"Hostx86";
405 return std::string(Path);
409 const std::string &VCToolChainPath,
414 return !VFS.
exists(TestPath);
418 std::optional<StringRef> WinSdkDir,
419 std::optional<StringRef> WinSdkVersion,
420 std::optional<StringRef> WinSysRoot,
421 std::string &Path,
int &Major,
422 std::string &WindowsSDKIncludeVersion,
423 std::string &WindowsSDKLibVersion) {
426 Path, Major, WindowsSDKIncludeVersion)) {
427 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
435 std::string RegistrySDKVersion;
437 "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
438 "InstallationFolder", Path, &RegistrySDKVersion))
440 if (Path.empty() || RegistrySDKVersion.empty())
443 WindowsSDKIncludeVersion.clear();
444 WindowsSDKLibVersion.clear();
446 std::sscanf(RegistrySDKVersion.c_str(),
"v%d.", &Major);
453 const char *Tests[] = {
"winv6.3",
"win8",
"win7"};
454 for (
const char *
Test : Tests) {
457 if (VFS.
exists(TestPath)) {
458 WindowsSDKLibVersion =
Test;
462 return !WindowsSDKLibVersion.empty();
467 WindowsSDKIncludeVersion = WinSdkVersion->str();
468 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
474 WindowsSDKLibVersion = WindowsSDKIncludeVersion;
482 std::optional<StringRef> WinSdkDir,
483 std::optional<StringRef> WinSdkVersion,
484 std::optional<StringRef> WinSysRoot,
485 std::string &Path, std::string &UCRTVersion) {
490 Path, Major, UCRTVersion))
499 "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots",
"KitsRoot10",
505 UCRTVersion = WinSdkVersion->str();
514 std::optional<StringRef> VCToolsVersion,
515 std::optional<StringRef> WinSysRoot, std::string &Path,
519 if (VCToolsDir || WinSysRoot) {
523 std::string ToolsVersion;
525 ToolsVersion = VCToolsVersion->str();
529 Path = std::string(ToolsPath);
531 Path = VCToolsDir->str();
544 if (std::optional<std::string> VCToolsInstallDir =
548 Path = std::move(*VCToolsInstallDir);
552 if (std::optional<std::string> VCInstallDir =
558 Path = std::move(*VCInstallDir);
569 for (
StringRef PathEntry : PathEntries) {
570 if (PathEntry.empty())
576 ExeTestPath = PathEntry;
578 if (!VFS.
exists(ExeTestPath))
583 ExeTestPath = PathEntry;
585 if (!VFS.
exists(ExeTestPath))
600 Path = std::string(ParentPath);
608 Path = std::string(ParentPath);
618 StringRef ExpectedPrefixes[] = {
"",
"Host",
"bin",
"",
619 "MSVC",
"Tools",
"VC"};
623 for (
StringRef Prefix : ExpectedPrefixes) {
626 if (!It->starts_with_insensitive(Prefix))
634 for (
int i = 0; i < 3; ++i)
637 Path = std::string(ToolChainPath);
666 struct SuppressCOMErrorsRAII {
667 static void __stdcall handler(HRESULT hr, IErrorInfo *perrinfo) {}
669 SuppressCOMErrorsRAII() { _set_com_error_handler(handler); }
671 ~SuppressCOMErrorsRAII() { _set_com_error_handler(_com_raise_error); }
673 } COMErrorSuppressor;
675 ISetupConfigurationPtr Query;
676 HR = Query.CreateInstance(__uuidof(SetupConfiguration));
680 IEnumSetupInstancesPtr EnumInstances;
681 HR = ISetupConfiguration2Ptr(Query)->EnumAllInstances(&EnumInstances);
685 ISetupInstancePtr Instance;
686 HR = EnumInstances->Next(1, &Instance,
nullptr);
690 ISetupInstancePtr NewestInstance;
691 std::optional<uint64_t> NewestVersionNum;
693 bstr_t VersionString;
695 HR = Instance->GetInstallationVersion(VersionString.GetAddress());
698 HR = ISetupHelperPtr(Query)->ParseVersion(VersionString, &VersionNum);
701 if (!NewestVersionNum || (VersionNum > NewestVersionNum)) {
702 NewestInstance = Instance;
703 NewestVersionNum = VersionNum;
705 }
while ((HR = EnumInstances->Next(1, &Instance,
nullptr)) == S_OK);
711 HR = NewestInstance->ResolvePath(L
"VC", VCPathWide.GetAddress());
715 std::string VCRootPath;
718 std::string ToolsVersion;
719 if (VCToolsVersion.has_value()) {
720 ToolsVersion = *VCToolsVersion;
724 "Microsoft.VCToolsVersion.default.txt");
727 if (!ToolsVersionFile)
730 ToolsVersion = ToolsVersionFile->get()->getBuffer().rtrim();
740 Path = std::string(ToolchainPath.
str());
748 std::string VSInstallPath;
750 "InstallDir", VSInstallPath,
nullptr) ||
752 "InstallDir", VSInstallPath,
nullptr)) {
753 if (!VSInstallPath.empty()) {
754 auto pos = VSInstallPath.find(R
"(\Common7\IDE)");
755 if (pos == std::string::npos)
760 Path = std::string(VCPath);
Provides a library for accessing COM functionality of the Host OS.
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.
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
Get the contents as an std::string.
constexpr bool empty() const
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.
LLVM_ABI bool tryParse(StringRef string)
Try to parse the given string as a version number.
LLVM_ABI 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 LLVM_ABI 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.
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
LLVM_ABI reverse_iterator rend(StringRef path LLVM_LIFETIME_BOUND)
Get reverse end iterator over path.
LLVM_ABI reverse_iterator rbegin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get reverse begin iterator over path.
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
LLVM_ABI const char * archToWindowsSDKArch(llvm::Triple::ArchType Arch)
LLVM_ABI 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)
LLVM_ABI 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.
LLVM_ABI bool useUniversalCRT(ToolsetLayout VSLayout, const std::string &VCToolChainPath, llvm::Triple::ArchType TargetArch, llvm::vfs::FileSystem &VFS)
LLVM_ABI bool convertWideToUTF8(std::wstring_view Source, std::string &Result)
Converts a wide string view to a UTF-8 encoded std::string.
LLVM_ABI bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path, ToolsetLayout &VSLayout)
LLVM_ABI bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::optional< llvm::StringRef > VCToolsVersion, std::string &Path, ToolsetLayout &VSLayout)
LLVM_ABI const char * archToLegacyVCArch(llvm::Triple::ArchType Arch)
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
LLVM_ABI bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on WideCharWidth.
LLVM_ABI const char * archToDevDivInternalArch(llvm::Triple::ArchType Arch)
LLVM_ABI std::string getSubDirectoryPath(SubDirectoryType Type, ToolsetLayout VSLayout, const std::string &VCToolChainPath, llvm::Triple::ArchType TargetArch, llvm::StringRef SubdirParent="")
@ End
Run the machine verifier once, at the end of the pipeline.
LLVM_ABI bool appendArchToWindowsSDKLibPath(int SDKMajor, llvm::SmallString< 128 > LibPath, llvm::Triple::ArchType Arch, std::string &path)
LLVM_ABI bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout)
LLVM_ABI 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)